Skip to content

Instantly share code, notes, and snippets.

@Zoxc
Last active January 4, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zoxc/8629159 to your computer and use it in GitHub Desktop.
Save Zoxc/8629159 to your computer and use it in GitHub Desktop.
use "../core"
enum Token
None
Unknown
Id
struct Lexer
struct State
lexer *Lexer
type Token
start *char
stop *char
end()
stop = lexer.i.pos
str()
return String.from_data(start, int_ptr stop - int_ptr start)
struct Input
pos *char
end *char
step()
pos = ptr_idx(pos, 1)
return *pos
c()
return *pos
eq(c char)
return *pos == c
tok State
content String
i Input
jump_table Table(256, () -> ())
set(str)
content = str
IO.puts("content: " ~ content)
i.pos = content.data
i.end = ptr_idx(content.data, content.size)
tok.lexer = self
step()
action create
setup_table()
setup_table()
.set_chars = |start char, stop, f|
for_range(start, stop, |i|
jump_table(force_cast i) = f)
set_chars(0, 255, unknown)
step()
IO.puts("step " ~ show(force_cast[uint] i.c()))
tok.type = Token.None
tok.start = i.pos
jump_table(force_cast i.c())()
assert -> int_ptr tok.stop >= int_ptr tok.start
assert -> tok.type != Token.None
unknown()
IO.puts("Unknown")
i.step()
tok.type = Token.None
tok.end()
IO.puts("Unknown character: " ~ tok.str())
step()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment