-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
418 lines (323 loc) · 13.2 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from Token import *
from Expr import *
from Stmt import *
class Parser:
class ParseError(Exception):
def __init__(self, message) -> None:
super().__init__(message)
def __init__(self, tokens: list[Token]) -> None:
self.tokens: list[Token] = tokens
self.current: int = 0
def parse(self) -> list[Stmt]:
statements: list[Stmt] = []
while not self.isAtEnd():
statements.append(self.declaration())
return statements
def declaration(self) -> Stmt:
try:
if self.match(TokenType.CLASS):
return self.classDeclaration()
elif self.match(TokenType.FUN):
return self.function("function")
elif self.match(TokenType.VAR):
return self.varDeclaration()
return self.statement()
except self.ParseError:
self.synchronize()
return Stmt()
def classDeclaration(self) -> Stmt:
name: Token = self.consume(TokenType.IDENTIFIER, "Expect class name.")
superclass: Variable | None = None
if self.match(TokenType.LESS):
self.consume(TokenType.IDENTIFIER, "Expected superclass name.")
superclass = Variable(self.previous())
self.consume(TokenType.LEFT_BRACE, "Expect '{' before class body.")
methods: list[Function] = []
while not self.check(TokenType.RIGHT_BRACE) and not self.isAtEnd():
methods.append(self.function("method"))
self.consume(TokenType.RIGHT_BRACE, "Expect '}' after class body.")
return Class(name, superclass, methods)
def function(self, kind: str) -> Function:
name: Token = self.consume(
TokenType.IDENTIFIER, f"Expected {kind} name.")
self.consume(
TokenType.LEFT_PAREN,
f"Expected '(' after {kind} name.")
parameters: list[Token] = []
if not self.check(TokenType.RIGHT_PAREN):
parameters.append(
self.consume(
TokenType.IDENTIFIER,
"Expected parameter name."))
while self.match(TokenType.COMMA):
if len(parameters) >= 255:
self.error(self.peek(),
"Can't have more than 255 parameters.")
parameters.append(
self.consume(
TokenType.IDENTIFIER,
"Expected parameter name."))
self.consume(TokenType.RIGHT_PAREN,
"Expected ')' after parameters.")
self.consume(TokenType.LEFT_BRACE,
f"Expected '{{' before {kind} body.")
body: list[Stmt] = self.block()
return Function(name, parameters, body)
def varDeclaration(self) -> Stmt:
name: Token = self.consume(
TokenType.IDENTIFIER, "Expected variable name.")
initializer: Expr | None = None
if self.match(TokenType.EQUAL):
initializer = self.expression()
self.consume(
TokenType.SEMICOLON,
"Expect ';' after variable declaration.")
return Var(name, initializer)
def statement(self) -> Stmt:
if self.match(TokenType.FOR):
return self.forStatement()
elif self.match(TokenType.IF):
return self.ifStatement()
elif self.match(TokenType.RETURN):
return self.returnStatement()
elif self.match(TokenType.PRINT):
return self.printStatement()
elif self.match(TokenType.WHILE):
return self.whileStatement()
elif self.match(TokenType.LEFT_BRACE):
return Block(self.block())
return self.expressionStatement()
def returnStatement(self) -> Stmt:
keyword: Token = self.previous()
value: Expr | None = None
if not self.check(TokenType.SEMICOLON):
value = self.expression()
self.consume(
TokenType.SEMICOLON, "Expected ';' after return value")
return Return(keyword, value)
def forStatement(self) -> Stmt:
self.consume(TokenType.LEFT_PAREN, "Expected '(' after 'for'.")
initializer: Stmt | None
if self.match(TokenType.SEMICOLON):
initializer = None
elif self.match(TokenType.VAR):
initializer = self.varDeclaration()
else:
initializer = self.expressionStatement()
condition: Expr | None = None
if not self.check(TokenType.SEMICOLON):
condition = self.expression()
self.consume(
TokenType.SEMICOLON, "Expected ';' after loop condition.")
increment: Expr | None = None
if not self.check(TokenType.RIGHT_PAREN):
increment = self.expression()
self.consume(
TokenType.RIGHT_PAREN, "Expected ')' after for clauses.")
body: Stmt = self.statement()
if increment is not None:
body = Block([body, Expression(increment)])
if condition is None:
condition = Literal(True)
body = While(condition, body)
if initializer is not None:
body = Block([initializer, body])
return body
def whileStatement(self) -> Stmt:
self.consume(TokenType.LEFT_PAREN, "Expected '(' after 'while'.")
condition: Expr = self.expression()
self.consume(
TokenType.RIGHT_PAREN, "Expected ')' after condition.")
body: Stmt = self.statement()
return While(condition, body)
def ifStatement(self) -> Stmt:
self.consume(TokenType.LEFT_PAREN, "Expected '(' after 'if'.")
condition: Expr = self.expression()
self.consume(
TokenType.RIGHT_PAREN, "Expected ')' after 'if' condition.")
thenBranch: Stmt = self.statement()
elseBranch: Stmt | None = None
if self.match(TokenType.ELSE):
elseBranch = self.statement()
return If(condition, thenBranch, elseBranch)
def block(self) -> list[Stmt]:
statements: list[Stmt] = []
while (not self.check(TokenType.RIGHT_BRACE)
and not self.isAtEnd()):
statements.append(self.declaration())
self.consume(TokenType.RIGHT_BRACE, "Expected '}' after block.")
return statements
def printStatement(self) -> Stmt:
value: Expr = self.expression()
self.consume(TokenType.SEMICOLON, "Expected ';' after value.")
return Print(value)
def expressionStatement(self) -> Stmt:
expr: Expr = self.expression()
self.consume(TokenType.SEMICOLON, "Expected ';' after value.")
return Expression(expr)
def expression(self) -> Expr:
return self.assignment()
def assignment(self) -> Expr:
expr: Expr = self.orExpr()
if self.match(TokenType.EQUAL):
equals: Token = self.previous()
value: Expr = self.assignment()
match expr:
case Variable():
name: Token = expr.name
return Assign(name, value)
case Get():
get: Get = expr
return Set(get.thing, get.name, value)
import Lox
Lox.parse_error(equals, "Invalid assignment target.")
return expr
def orExpr(self) -> Expr:
expr: Expr = self.andExpr()
while self.match(TokenType.OR):
operator: Token = self.previous()
right: Expr = self.andExpr()
expr = Logical(expr, operator, right)
return expr
def andExpr(self) -> Expr:
expr: Expr = self.equality()
while self.match(TokenType.AND):
operator: Token = self.previous()
right: Expr = self.equality()
expr = Logical(expr, operator, right)
return expr
def equality(self) -> Expr:
expr: Expr = self.comparison()
while self.match(TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL):
operator: Token = self.previous()
right: Expr = self.comparison()
expr = Binary(expr, operator, right)
return expr
def comparison(self) -> Expr:
expr: Expr = self.term()
while self.match(TokenType.GREATER,
TokenType.GREATER_EQUAL,
TokenType.LESS,
TokenType.LESS_EQUAL):
operator: Token = self.previous()
right: Expr = self.term()
expr = Binary(expr, operator, right)
return expr
def term(self) -> Expr:
expr: Expr = self.factor()
while self.match(TokenType.MINUS, TokenType.PLUS):
operator: Token = self.previous()
right: Expr = self.factor()
expr = Binary(expr, operator, right)
return expr
def factor(self) -> Expr:
expr: Expr = self.unary()
while self.match(TokenType.SLASH, TokenType.STAR):
operator: Token = self.previous()
right: Expr = self.unary()
expr = Binary(expr, operator, right)
return expr
def unary(self) -> Expr:
while self.match(TokenType.SLASH, TokenType.STAR):
operator: Token = self.previous()
right: Expr = self.unary()
return Unary(operator, right)
return self.call()
def call(self) -> Expr:
expr: Expr = self.primary()
while True:
if self.match(TokenType.LEFT_PAREN):
expr = self.finishCall(expr)
elif self.match(TokenType.DOT):
name: Token = self.consume(
TokenType.IDENTIFIER,
"Expected property name after '.'.")
expr = Get(expr, name)
else:
break
return expr
def finishCall(self, callee: Expr) -> Expr:
arguments: list[Expr] = []
if not self.check(TokenType.RIGHT_PAREN):
arguments.append(self.expression())
while self.match(TokenType.COMMA):
if len(arguments) >= 255:
self.error(self.peek(),
"Can't have more than 255 arguments.")
arguments.append(self.expression())
paren: Token = self.consume(TokenType.RIGHT_PAREN, "Expected ')' after arguments.")
return Call(callee, paren, arguments)
def primary(self) -> Expr:
if self.match(TokenType.FALSE):
return Literal(False)
elif self.match(TokenType.TRUE):
return Literal(True)
elif self.match(TokenType.NIL):
return Literal(None)
elif self.match(TokenType.NUMBER, TokenType.STRING):
return Literal(self.previous().literal)
elif self.match(TokenType.SUPER):
keyword: Token = self.previous()
self.consume(TokenType.DOT, "Expected '.' after 'super'.")
method: Token = self.consume(TokenType.IDENTIFIER, "Expected superclass method name.")
return Super(keyword, method)
elif self.match(TokenType.THIS):
return This(self.previous())
elif self.match(TokenType.IDENTIFIER):
return Variable(self.previous())
elif self.match(TokenType.LEFT_PAREN):
expr: Expr = self.expression()
self.consume(
TokenType.RIGHT_PAREN, "Expected ')' after expression.")
return Group(expr)
raise self.error(self.peek(), "Expected expression.")
def consume(self, token_type: TokenType, message: str) -> Token:
if self.check(token_type):
return self.advance()
raise self.error(self.peek(), message)
def error(self, token: Token, message: str) -> ParseError:
import Lox
Lox.parse_error(token, message)
return self.ParseError(message)
def synchronize(self) -> None:
self.advance()
while not self.isAtEnd():
if self.previous().token_type == TokenType.SEMICOLON:
match self.peek().token_type:
case TokenType.CLASS:
return
case TokenType.FUN:
return
case TokenType.VAR:
return
case TokenType.FOR:
return
case TokenType.IF:
return
case TokenType.WHILE:
return
case TokenType.PRINT:
return
case TokenType.RETURN:
return
self.advance()
def match(self, *token_types: TokenType) -> bool:
for token_type in token_types:
if self.check(token_type):
self.advance()
return True
return False
def check(self, token_type: TokenType) -> bool:
if self.isAtEnd():
return False
return self.peek().token_type == token_type
def advance(self):
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.tokens[self.current]
def previous(self) -> Token:
return self.tokens[self.current - 1]