-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
357 lines (250 loc) · 11.5 KB
/
parser.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# type: ignore
from syntax.tokens import *
from syntax.callable import *
from syntax.stmt import Stmt
from syntax.expr import Expr
import syntax.stmt as stmt
import syntax.expr as expr
class Parser():
def __init__(self, grape, tokens: list[Token]):
self.current = 0
self.errorHandler = grape.errorHandler
self.tokens = tokens
self.statements = []
def parse(self) -> list[Stmt]:
while not self.isAtEnd():
statement = self.statement()
if statement: self.statements.append(statement)
return self.statements
def statement(self) -> Stmt:
try:
if not self.match(TokenType.NEWLINE): # Skip empty lines
statement = self.exprStmt()
self.expect(TokenType.NEWLINE, "Unterminated statement, no newline present.")
return statement
except ParseError:
self.synchronize()
def exprStmt(self) -> Stmt:
return stmt.ExprStmt(self.expression())
def expression(self) -> Expr:
if self.check_sequence([TokenType.IDENTIFIER, TokenType.EQUAL]):
return self.variableDecl()
if self.match(TokenType.FN):
if self.match(TokenType.IDENTIFIER):
return self.functionDecl()
else:
return self.lambdaExpr()
if self.match(TokenType.IF):
return self.conditionalIf()
elif self.match(TokenType.DO):
return expr.Block(self.block())
else:
return self.logicOr()
def variableDecl(self) -> Expr:
# Consumes the identifier.
# Needed because we use `check` and not `match`
# in the callee of this function.
self.advance()
name = self.previous()
# Concums the equal (=) sign.
self.advance()
initializer = self.expression()
return expr.VariableDecl(name, initializer)
def functionDecl(self) -> Expr:
name = self.previous()
(parameters, body) = self.parseFunction()
return expr.Function(name, parameters, body)
def lambdaExpr(self) -> Expr:
(parameters, body) = self.parseFunction()
return expr.Lambda(parameters, body)
def parseFunction(self) -> (list[Token], expr.Block):
self.expect(TokenType.LEFT_PAREN, "Missing opening \"(\" before function arguments.")
parameters = []
if not self.check(TokenType.RIGHT_PAREN):
invalidParameterErrorMessage = "Parameters for an function can only be identifiers."
parameter = self.expect(TokenType.IDENTIFIER, invalidParameterErrorMessage)
parameters.append(parameter)
while self.match(TokenType.COMMA):
parameter = self.expect(TokenType.IDENTIFIER, invalidParameterErrorMessage)
parameters.append(parameter)
self.expect(TokenType.RIGHT_PAREN, "Missing closing \"(\" after function arguments.")
self.expect(TokenType.DO, "Expected do-end block after function header.")
body = self.block()
return (parameters, body)
def conditionalIf(self) -> Expr:
self.expect(TokenType.LEFT_PAREN, "Missing opening \"(\" before if-statement condition.")
condition = self.logicOr()
self.expect(TokenType.RIGHT_PAREN, "Missing closing \")\" after if-statement condition.")
thenBranch = None
elseBranch = None
if(self.match(TokenType.DO)):
doStatements = self.parseBlock([TokenType.END, TokenType.ELSE])
thenBranch = expr.Block(doStatements)
if self.match(TokenType.ELSE):
elseStatements = self.parseBlock([TokenType.END])
elseBranch = expr.Block(elseStatements)
self.expect(TokenType.END, "Expected \"end\" to terminate do-else-block.")
else:
thenBranch = self.blockify(self.expression())
if(self.match(TokenType.ELSE)):
elseBranch = self.blockify(self.expression())
return expr.If(condition, thenBranch, elseBranch)
def blockify(self, expression: Expr) -> expr.Block:
statement = stmt.ExprStmt(expression)
return expr.Block([statement])
def parseBlock(self, endTokens: list[TokenType]):
statements = []
while not self.check_multiple(endTokens) and not self.isAtEnd():
statement = self.statement()
if statement: statements.append(statement)
return statements
def block(self) -> list[Stmt]:
statements = self.parseBlock([TokenType.END])
self.expect(TokenType.END, "Expected \"end\" to terminate do-block.")
return statements
def logicOr(self) -> Expr:
expression = self.logicAnd()
while(self.match(TokenType.OR)):
operator = self.previous()
right = self.logicAnd()
expression = expr.Logical(expression, operator, right)
return expression
def logicAnd(self) -> Expr:
expression = self.equality()
while(self.match(TokenType.AND)):
operator = self.previous()
right = self.equality()
expression = expr.Logical(expression, operator, right)
return expression
def equality(self) -> Expr:
expression = self.comparison()
while (self.match_either([TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL])):
operator = self.previous()
right = self.comparison()
expression = expr.Binary(expression, operator, right)
return expression
def comparison(self)-> Expr:
expression = self.term()
while (self.match_either([TokenType.GREATER, TokenType.GREATER_EQUAL, TokenType.LESS, TokenType.LESS_EQUAL])):
operator = self.previous()
right = self.term()
expression = expr.Binary(expression, operator, right)
return expression
def term(self)-> Expr:
expression = self.factor()
while (self.match_either([TokenType.MINUS, TokenType.PLUS])):
operator = self.previous()
right = self.factor()
expression = expr.Binary(expression, operator, right)
return expression
def factor(self) -> Expr:
expression = self.unary()
while (self.match_either([TokenType.SLASH, TokenType.STAR])):
operator = self.previous()
right = self.unary()
expression = expr.Binary(expression, operator, right)
return expression
def unary(self) -> Expr:
if self.match_either([TokenType.NOT, TokenType.MINUS]):
operator = self.previous()
right = self.unary()
return expr.Unary(operator, right)
return self.call()
def call(self) -> Expr:
expression = self.primary()
while self.match(TokenType.LEFT_PAREN):
expression = self.finishCall(expression)
return expression
def finishCall(self, callee: Expr) -> Expr:
arguments = []
if not self.check(TokenType.RIGHT_PAREN):
arguments.append(self.expression())
while self.match(TokenType.COMMA):
if len(arguments) >= 255: self.syntaxError(self.peek(), "Can't have more than 255 arguments in a function call.")
arguments.append(self.expression())
closingParenToken = self.expect(TokenType.RIGHT_PAREN, "Expected \"\" after arguments in function call.")
return expr.Call(callee, closingParenToken, arguments)
def primary(self) -> Expr:
if self.match(TokenType.FALSE): return expr.Literal(False)
if self.match(TokenType.TRUE): return expr.Literal(True)
if self.match(TokenType.IDENTIFIER): return expr.VariableExpr(self.previous())
if self.match_either([TokenType.NUMBER, TokenType.STRING, TokenType.ATOM]):
return expr.Literal(self.previous().literal)
if self.match(TokenType.LEFT_BRACKET):
if not self.check(TokenType.RIGHT_BRACKET):
items = self.collection()
else:
items = []
self.expect(TokenType.RIGHT_BRACKET, "Missing \"]\" to close list.")
return expr.List(items)
if self.match(TokenType.LEFT_BRACE):
if not self.check(TokenType.RIGHT_BRACE):
items = self.collection()
else:
items = []
self.expect(TokenType.RIGHT_BRACE, "Missing \"}\" to close tuple.")
return expr.Tuple(items)
if self.match(TokenType.LEFT_PAREN):
expression = self.expression()
self.expect(TokenType.RIGHT_PAREN, "Missing \")\" after expression.")
return expr.Grouping(expression)
self.syntaxError(self.peek(), "Expected expression.")
def collection(self) -> list[Expr]:
expression = self.expression()
items = [expression]
while self.match(TokenType.COMMA):
expression = self.expression()
items.append(expression)
return items
def match_either(self, token_types: list[TokenType]) -> bool:
for token_type in token_types:
if self.match(token_type): return True
return False
def match(self, token_type: TokenType) -> bool:
if self.check(token_type):
self.advance()
return True
else:
return False
def check_multiple(self, token_types: list[TokenType]) -> bool:
for token_type in token_types:
if self.check(token_type): return True
return False
def check_sequence(self, token_types: list[TokenType]) -> bool:
acc = []
for i in range(len(token_types)):
token = self.getTokenByIndex(self.current + i)
acc.append(token_types[i] == token.token_type)
return all(acc)
def check(self, token_type: TokenType) -> bool:
if self.isAtEnd(): return False
return self.peek().token_type == token_type
def advance(self) -> Token:
if not self.isAtEnd(): self.current += 1
return self.previous()
def isAtEnd(self) -> bool:
return self.peek().token_type == TokenType.EOF
def peek(self) -> Token:
return self.getTokenByIndex(self.current)
def previous(self) -> Token:
return self.getTokenByIndex(self.current - 1)
def getTokenByIndex(self, index: int) -> Token:
return self.tokens[index]
def expect(self, token_type: TokenType, fail_message: str) -> Token | None:
if self.check(token_type): return self.advance()
self.syntaxError(self.peek(), fail_message)
def syntaxError(self, token: Token, message: str) -> None:
if token.token_type == TokenType.EOF:
error = self.errorHandler.report("Syntax error", token.line, token.col, "EOF", message)
else:
error = self.errorHandler.report("Syntax error", token.line, token.col, "'" + token.lexeme + "'", message)
raise ParseError(error)
def synchronize(self) -> None:
self.advance()
while not self.isAtEnd():
if self.previous().token_type == TokenType.NEWLINE: return
if self.peek().token_type in [TokenType.NAMESPACE, TokenType.FN, TokenType.IF, TokenType.ELSEIF, TokenType.ELSE]:
return
self.advance()
class ParseError(Exception):
pass