Skip to content

Commit 5b73ca2

Browse files
committed
wip(boot): add lexer matcher
1 parent e531df8 commit 5b73ca2

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/bootstrap/lexer/lexer.jik

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ func advance(l: Lexer) -> void:
3434
end
3535

3636

37+
func match_next(l: Lexer, expected: char) -> bool:
38+
if l.at_end() or l.peek()? != expected:
39+
return false
40+
end
41+
l.advance()
42+
return true
43+
end
44+
45+
3746
func is_whitespace(ch: char) -> bool:
3847
return ch == ' ' or ch == '\t' or ch == '\r'
3948
end
@@ -132,6 +141,12 @@ func next_token(l: Lexer, r: Region) -> token::Token:
132141
elif ch == '=':
133142
l.advance()
134143
return token::Token{type = token::TokenType.ASSIGN, span = l.span(start, l.pos, r)}[r]
144+
elif ch == ':':
145+
l.advance()
146+
if l.match_next('='):
147+
return token::Token{type = token::TokenType.DECLARE, span = l.span(start, l.pos, r)}[r]
148+
end
149+
return token::Token{type = token::TokenType.COLON, span = l.span(start, l.pos, r)}[r]
135150
elif ch == '(':
136151
l.advance()
137152
return token::Token{type = token::TokenType.LPAREN, span = l.span(start, l.pos, r)}[r]
@@ -141,9 +156,9 @@ func next_token(l: Lexer, r: Region) -> token::Token:
141156
elif ch == ',':
142157
l.advance()
143158
return token::Token{type = token::TokenType.COMMA, span = l.span(start, l.pos, r)}[r]
144-
elif ch == ':':
159+
elif ch == '+':
145160
l.advance()
146-
return token::Token{type = token::TokenType.COLON, span = l.span(start, l.pos, r)}[r]
161+
return token::Token{type = token::TokenType.OP_PLUS, span = l.span(start, l.pos, r)}[r]
147162
elif ch == '"':
148163
return l.scan_string(r)
149164
elif char::isdigit(ch):

src/bootstrap/lexer/token.jik

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ enum TokenType:
66
STRING
77
IDENTIFIER
88
ASSIGN
9+
DECLARE
10+
COLON
911
NEWLINE
1012
LPAREN
1113
RPAREN
1214
COMMA
13-
COLON
15+
OP_PLUS
1416
KWD_FUNC
1517
KWD_USE
1618
KWD_AS

0 commit comments

Comments
 (0)