Skip to content

Commit 0b336b4

Browse files
committed
Add comments in parser.nim
1 parent 6593bbf commit 0b336b4

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/parser.nim

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import hashes
1010

1111
type
1212
# Parser takes in a list of tokens (seq[Token]) and
13-
# generates an abstract syntax tree
13+
# generates an abstract syntax tree.
1414
Parser* = object
1515
tokens*: seq[Token]
1616
current*: int
1717
loopDepth*: int
1818
statements*: seq[Stmt]
1919

20+
# newParser creates a new Parser and returns it.
2021
proc newParser*(tokens: seq[Token]): Parser =
2122
return Parser(
2223
tokens: tokens,
@@ -27,7 +28,7 @@ proc newParser*(tokens: seq[Token]): Parser =
2728

2829
# ----------------------------------------------------------------------
2930

30-
# forward declaration
31+
# forward declarations
3132
proc expression(p: var Parser): Expr
3233
proc parseBlock(p: var Parser): seq[Stmt]
3334
proc ifStatement(p: var Parser): Stmt
@@ -39,6 +40,7 @@ proc functionBody(p: var Parser, kind: string): FuncExpr
3940
proc statement(p: var Parser): Stmt
4041
proc parse*(p: var Parser): seq[Stmt]
4142

43+
# This is for one-line, anonymous functions.
4244
var dontNeedSemicolon = false
4345

4446
# returns the previous token
@@ -74,6 +76,8 @@ proc doesMatch(p: var Parser, types: varargs[TokenType]): bool =
7476
return true
7577
return false
7678

79+
# This checks wheter the current token is the expected type or not.
80+
# If not, throws an error.
7781
proc expect(p: var Parser, ttype: TokenType, message: string): Token {.discardable.} =
7882
if p.checkCurrentTok(ttype): return p.advance()
7983
else:
@@ -105,7 +109,7 @@ proc primary(p: var Parser): Expr =
105109
p.expect(RightParen, "Expected ')'")
106110
return GroupingExpr(expression: expre)
107111

108-
elif p.doesMatch(LeftBracket): # list literal
112+
elif p.doesMatch(LeftBracket): # list literal
109113
let keyword = p.previousToken()
110114

111115
if p.doesMatch(RightBracket):
@@ -120,7 +124,7 @@ proc primary(p: var Parser): Expr =
120124

121125
return ListLiteralExpr(values: values, keyword: keyword)
122126

123-
elif p.doesMatch(LeftBrace): # map literal
127+
elif p.doesMatch(LeftBrace): # map literal
124128
let keyword = p.previousToken()
125129
if p.doesMatch(RightBrace):
126130
return MapLiteralExpr(keys: @[], values: @[], keyword: keyword)
@@ -452,6 +456,7 @@ proc parseBlock(p: var Parser): seq[Stmt] =
452456
p.expect(RightBrace, "Expected '}' after a block")
453457
return statements
454458

459+
# This is where all the parsing starts.
455460
proc parse*(p: var Parser): seq[Stmt] =
456461
while not p.isAtEnd():
457462
p.statements.add(p.declaration())

0 commit comments

Comments
 (0)