-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.nim
40 lines (30 loc) · 1.14 KB
/
parser.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import types
type Parser = ref object of RootObj
position: uint
lexemes: var seq[Node]
length: uint
func init_parser(lexemes: var seq[Node]): Parser =
return Parser(
position: 0'u,
length: uint(lexemes.len),
lexemes: lexemes
)
func current(parser: Parser): Node =
assert parser.position < parser.length, "Out of bounds on current."
return parser.lexemes[parser.position]
func expect_kind(lexeme: Node, kinds: varargs[typedesc[Node]]): typedesc[Node] =
for kind in kinds:
if lexeme of kind:
return kind
assert false, "Unexpected lexemes."
func expect_keyword(lexeme: Node, kind: KeywordKind): Keyword =
assert lexeme of Keyword, "Expected a keyword."
assert cast[Keyword](lexeme).kind == kind, "Unexpected keyword kind."
return cast[Keyword](lexeme)
proc parse_val_statement(parser: var Parser, keyword: Keyword): Statement =
discard
proc parse_statement(parser: var Parser): Statement =
let keyword: Keyword = expect_keyword(parser.current, Keyword)
proc parse_block(parser: var Parser): Block =
while not (parser.current of EndOfFile):
discard