-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
603 lines (511 loc) · 27.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/env python3
from typing import List, Any, Union, Dict
from lexer import *
from ast_nodes import *
from errors import InvalidStateError, InvalidSyntaxError
from binascii import unhexlify
class Parser:
def __init__(self, tokens: List[Token]):
self.tokens: List[Token] = tokens
self.token_index: int = -1
# self.tokens contains at least a TT_EOF token
self.current_token: Token = self.tokens[0]
self.advance()
def _rollback_to(self, token: Token):
"""
Rollback parser to specified token.
The token must be in the self.tokens.
:param token: Token to rollback to.
:type token: Token, must be an instance present in the self.tokens list.
"""
for i, t in enumerate(self.tokens):
if t == token:
self.current_token = t
self.token_index = i
def run(self) -> list:
"""
Returns a list of nodes (AST).
"""
return self.statements()
def advance(self) -> Token:
"""
Return the next token.
"""
self.token_index += 1
if self.token_index < len(self.tokens):
self.current_token = self.tokens[self.token_index]
if self.current_token.type == TT_COMMENT:
# just ignore comments, we don't need them when parsing
self.advance()
return self.current_token
def statements(self) -> list:
"""
<statements> ::= <statement>+
"""
res: list = []
while self.current_token.type != TT_EOF:
res.append(self.statement())
return res
def statement(self):
"""
<statement> ::= <bitfield_stmt> | <struct_stmt>
"""
token: Token = self.current_token
if token.type == TT_KEYWORD:
if token.value == STRUCT_KEYWORD or token.value in ENDIANNESS_KEYWORDS:
return self.struct_stmt()
elif token.value == BITFIELD_KEYWORD:
return self.bitfield_stmt()
else:
raise InvalidSyntaxError(token.pos_start, token.pos_end, "expected struct or bitfield statement")
self.advance()
def bitfield_member_def(self) -> BitfieldMemberNode:
"""
<bitfield_member_def> ::= <identifier> "(" <no_identifier_expr> ")"? ","
"""
if self.current_token.type != TT_IDENTIFIER:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected identifier")
name_token: Token = self.current_token
self.advance()
bits_count: ASTNode = None # size of the member
if self.current_token.type == TT_LPAREN:
self.advance()
bits_count = self.no_identifier_expr()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
if self.current_token.type != TT_COMMA:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ','")
self.advance()
return BitfieldMemberNode(name_token, bits_count)
def bitfield_stmt(self) -> BitfieldDefNode:
"""
<bitfield_stmt> ::= "bitfield" <identifier> "{" <bitfield_member_def>+ "}"
| "bitfield" <identifier> "(" <no_identifier_expr> ")" "{" <bitfield_member_def>+ "}"
"""
self.advance()
name: Token = self.current_token
self.advance()
bytes_count: Optional[ASTNode] = None # size of the bitfield
if self.current_token.type == TT_LPAREN:
self.advance()
bytes_count = self.no_identifier_expr()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
if self.current_token.type != TT_LCURLY:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '{'")
self.advance()
members: List = []
while self.current_token.type not in [TT_RCURLY, TT_EOF]:
members.append(self.bitfield_member_def())
if self.current_token.type == TT_EOF:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '}'")
self.advance()
return BitfieldDefNode(name, members, bytes_count)
def struct_stmt(self) -> StructDefNode:
"""
<struct_stmt> ::= (<endian> | <ternary_endian>)+ "struct" <identifier> "{" (<struct_member_def> | <match_stmt>)+ "}"
<endian> ::= "LE" | "BE"
<ternary_endian> ::= "(" <comparison> "?" <endian> ":" <endian> ")"
"""
endian = Endian.BIG
if self.current_token.value == LITTLE_ENDIAN_KEYWORD:
endian = Endian.LITTLE
self.advance()
elif self.current_token.value == BIG_ENDIAN_KEYWORD:
endian = Endian.BIG
self.advance()
self.advance()
if self.current_token.type != TT_IDENTIFIER:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected identifier")
struct_name = self.current_token
self.advance()
if self.current_token.type != TT_LCURLY: # '{', start of struct
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '{'")
self.advance()
struct_members: List[Union[StructMemberDeclareNode, MatchNode]] = []
while self.current_token.type not in [TT_RCURLY, TT_EOF]:
if self.current_token.type == TT_KEYWORD and self.current_token.value == MATCH_KEYWORD:
struct_members.append(self.match_stmt(endian))
else:
struct_members.append(self.struct_member_def(endian))
if self.current_token.type == TT_EOF: # if missing '}'
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '}'")
self.advance()
return StructDefNode(struct_name, struct_members, endian)
def struct_member_type(self, struct_endian: Endian) -> StructMemberInfoNode:
"""
<struct_member_type> ::= (<endian> | <ternary_endian>)+ (<data_type> | <ternary_data_type> | <identifier>)
| (<endian> | <ternary_endian>)+ (<data_type> | <ternary_data_type> | <identifier>) "[" <expr> "]"
| (<endian> | <ternary_endian>)+ (<data_type> | <ternary_data_type> | <identifier>) "[" <comparison> "]" ;; repeat until the comparison is false
| (<endian> | <ternary_endian>)+ (<data_type> | <ternary_data_type> | <identifier>) "[]" ;; repeat this member as much as possible
<endian> ::= "LE" | "BE"
<ternary_endian> ::= "(" <comparison> "?" <endian> ":" <endian> ")"
"""
endian: Union[Endian, TernaryEndianNode] = struct_endian
# check endian without ternary
if self.current_token.type == TT_KEYWORD and self.current_token.value == LITTLE_ENDIAN_KEYWORD:
endian = Endian.LITTLE
self.advance()
elif self.current_token.type == TT_KEYWORD and self.current_token.value == BIG_ENDIAN_KEYWORD:
endian = Endian.BIG
self.advance()
if self.current_token.type not in [TT_DATA_TYPE, TT_IDENTIFIER, TT_LPAREN]:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected data-type, identifier or ternary operator")
member_type: Union[Token, TernaryDataTypeNode]
if self.current_token.type == TT_LPAREN:
# either endian or type, we cannot know now
res: Union[TernaryEndianNode, TernaryDataTypeNode] = self._handle_ternary_member_type_or_endian(struct_endian)
if isinstance(res, TernaryDataTypeNode):
member_type = res
else:
endian = res
# now that we are sure we passed the endian, we can check for the type
if self.current_token.type == TT_LPAREN:
member_type = self.ternary_data_type(struct_endian)
else:
member_type = self.current_token
self.advance()
if member_type.value == "bytes" and self.current_token.type != TT_LPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '(' for bytes data-type")
else:
# endian and ternary data type was check just before
# we are sure that we are dealing with the type here
member_type = self.current_token
self.advance()
if member_type.value == "bytes" and self.current_token.type != TT_LPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '(' for bytes data-type")
is_list: bool = False
list_length_node: Any = None
if self.current_token.type == TT_IDENTIFIER: # nothing to do, just continue parsing
return StructMemberInfoNode(member_type, endian)
elif self.current_token.type == TT_LPAREN and isinstance(member_type, Token) and member_type.value in ("string", "bytes"):
self.advance()
delimiter: Union[CharNode, StringNode, IntNumberNode] = self._read_delimiter()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
return StructMemberInfoNode(member_type, endian, delimiter=delimiter)
elif self.current_token.type == TT_LBRACK:
is_list = True
self.advance()
list_length_node = None
if self.current_token.type != TT_RBRACK:
current_token_tmp = self.current_token
list_length_node = self.expr()
if self.current_token.type != TT_RBRACK:
self._rollback_to(current_token_tmp)
list_length_node = self.comparison()
if self.current_token.type != TT_RBRACK:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ']'")
self.advance()
return StructMemberInfoNode(member_type, endian, is_list, list_length_node)
def _read_delimiter(self) -> Union[CharNode, StringNode, IntNumberNode]:
"""
Parse a delimiter for string and bytes data-types.
"""
has_slash: bool = False
if self.current_token.type == TT_BACKSLASH:
has_slash = True
self.advance()
if self.current_token.type == TT_STRING:
delimiter = self.current_token
self.advance()
return StringNode(delimiter)
elif self.current_token.type == TT_CHAR:
delimiter = self.current_token
self.advance()
return CharNode(delimiter)
elif self.current_token.type == TT_NUM_INT:
delimiter = self.current_token
self.advance()
return IntNumberNode(delimiter)
elif self.current_token.type == TT_IDENTIFIER and self.current_token.value.startswith("x") and has_slash:
try:
value = int.from_bytes(unhexlify(self.current_token.value[1:]), "big")
self.advance()
return IntNumberNode(Token(TT_NUM_INT, str(value)))
except Exception as e:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "invalid hex value: " + str(e))
elif self.current_token.type == TT_IDENTIFIER:
delimiter = self.current_token
self.advance()
return IdentifierAccessNode(delimiter.value)
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "invalid character as delimiter")
def struct_member_def(self, struct_endian: Endian) -> StructMemberDeclareNode:
"""
<struct_member_def> ::= <struct_member_type> <identifier> ","
"""
member_type: StructMemberInfoNode = self.struct_member_type(struct_endian)
if self.current_token.type != TT_IDENTIFIER:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected identifier")
member_name: Token = self.current_token
self.advance()
if self.current_token.type == TT_COMMA:
self.advance()
else:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ','")
return StructMemberDeclareNode(member_type, member_name)
def match_stmt(self, struct_endian: Endian) -> MatchNode:
"""
<match_stmt> ::= "match (" <expr> ") {" (<expr> ":" <struct_member_type> ",")+ "}" <identifier> ","
| "match (" <expr> ") {" (<expr> ": {" <struct_member_def>+ "},")+ "}," ;; multiple members in the match case
"""
self.advance()
if self.current_token.type != TT_LPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '('")
self.advance()
condition: ASTNode = self.expr()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
if self.current_token.type != TT_LCURLY:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '{'")
self.advance()
# start of match cases
cases: Dict = {}
is_multiple_members: bool = False # need to know to check after this while loop if an identifier is needed
while self.current_token.type != TT_RCURLY:
case_value: ASTNode = self.expr()
if self.current_token.type != TT_COLON:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ':'")
self.advance()
if self.current_token.type == TT_LCURLY: # multiple members
is_multiple_members = True
self.advance()
while self.current_token.type != TT_RCURLY:
if case_value in cases.keys():
cases[case_value].append(self.struct_member_def(struct_endian))
else:
cases.update({case_value: [self.struct_member_def(struct_endian)]})
self.advance()
else:
cases.update({case_value: self.struct_member_type(struct_endian)})
if self.current_token.type != TT_COMMA:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ','")
self.advance()
self.advance()
member_name: str = None
if not is_multiple_members:
if self.current_token.type != TT_IDENTIFIER:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected identifier")
member_name = self.current_token.value
self.advance()
if self.current_token.type != TT_COMMA:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ','")
self.advance()
return MatchNode(condition, cases, member_name)
def _handle_ternary_member_type_or_endian(self, struct_endian) -> Union[TernaryEndianNode, TernaryDataTypeNode]:
"""
Handle parsing ternary endian and ternary type by checking if an error is thrown when parsing ternary endian.
If an error is thrown when parsing with ternary endian, it means it is a ternary data-type.
"""
tmp_curr_token = self.current_token
try:
return self.ternary_endian()
except InvalidStateError:
self._rollback_to(tmp_curr_token)
return self.ternary_data_type(struct_endian)
def ternary_endian(self) -> TernaryEndianNode:
"""
<endian> ::= "LE" | "BE"
<ternary_endian> ::= "(" <comparison> "?" <endian> ":" <endian> ")"
"""
self.advance()
comparison_node: ComparisonNode = self.comparison()
if self.current_token.type != TT_QUESTION_MARK:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '?'")
self.advance()
if self.current_token.type != TT_KEYWORD or self.current_token.value not in ENDIANNESS_KEYWORDS:
raise InvalidStateError("Not a ternary endian.")
if_true: Endian = Endian.from_token(self.current_token)
self.advance()
if self.current_token.type != TT_COLON:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ':'")
self.advance()
if self.current_token.type != TT_KEYWORD or self.current_token.value not in ENDIANNESS_KEYWORDS:
raise InvalidStateError("Not a ternary endian.")
if_false: Endian = Endian.from_token(self.current_token)
self.advance()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
return TernaryEndianNode(comparison_node, if_true, if_false)
def ternary_data_type(self, struct_endian: Endian) -> TernaryDataTypeNode:
"""
<ternary_data_type> ::= "(" <comparison> "?" <data_type> ":" <data_type> ")"
"""
self.advance()
comparison_node: ComparisonNode = self.comparison()
if self.current_token.type != TT_QUESTION_MARK:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '?'")
self.advance()
if_true_data_type: StructMemberInfoNode = StructMemberInfoNode(self.current_token)
self.advance()
if if_true_data_type.type == "bytes" and self.current_token.type != TT_LPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '(' for bytes data-type")
if self.current_token.type == TT_LPAREN and if_true_data_type.type in ("string", "bytes"):
self.advance()
if_true_data_type._delimiter = self._read_delimiter()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
elif self.current_token.type == TT_LBRACK:
self.advance()
is_list = True
list_length_node = None
if self.current_token.type != TT_RBRACK:
current_token_tmp = self.current_token
list_length_node = self.expr()
if self.current_token.type != TT_RBRACK:
self._rollback_to(current_token_tmp)
list_length_node = self.comparison()
if self.current_token.type != TT_RBRACK:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ']'")
self.advance()
if_true_data_type._is_list = is_list
if_true_data_type._list_length_node = list_length_node
if self.current_token.type != TT_COLON:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ':'")
self.advance()
if_false_data_type: StructMemberInfoNode = StructMemberInfoNode(self.current_token)
self.advance()
if if_false_data_type.type == "bytes" and self.current_token.type != TT_LPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected '(' for bytes data-type")
if self.current_token.type == TT_LPAREN and if_false_data_type.type in ("string", "bytes"):
self.advance()
if_false_data_type._delimiter = self._read_delimiter()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
elif self.current_token.type == TT_LBRACK:
is_list = True
list_length_node = None
self.advance()
if self.current_token.type != TT_RBRACK:
current_token_tmp = self.current_token
list_length_node = self.expr()
if self.current_token.type != TT_RBRACK:
self._rollback_to(current_token_tmp)
list_length_node = self.comparison()
if self.current_token.type != TT_RBRACK:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ']'")
self.advance()
if_false_data_type._is_list = is_list
if_false_data_type._list_length_node = list_length_node
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
return TernaryDataTypeNode(comparison_node, if_true_data_type, if_false_data_type)
def comparison(self) -> ComparisonNode:
"""
<comparison> ::= <expr> <comparison_operator> <expr>
<comparison_operator> ::= "<=" | "<" | "==" | ">" | ">=" | "!=" | "&&" | "||"
"""
left_node: ASTNode = self.expr()
comparison_op_token: Token = self.current_token
comparison_op_dict: Dict = {TT_COMP_EQ: "==", TT_COMP_NE: "!=", TT_COMP_GT: ">", TT_COMP_LT: "<", TT_COMP_GEQ: ">=", TT_COMP_LEQ: "<=", TT_COMP_AND: "&&", TT_COMP_OR: "||"}
if comparison_op_token.type not in comparison_op_dict.keys():
list_comp: str = ", ".join([f"'{c}'" for c in comparison_op_dict.values()])
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected one of: " + list_comp)
self.advance()
right_node: ASTNode = self.expr()
return ComparisonNode(left_node, ComparisonOperatorNode(comparison_op_token), right_node)
def no_identifier_factor(self) -> Any:
"""
<no_identifier_factor> ::= <num_int> | <num_float>
| ("+" | "-" | <logical_operator>) <no_identifier_factor>
| "(" <no_identifier_expr> ")"
<logical_operator> ::= "&" | "|" | "^" | "<<" | ">>" | "~"
"""
token: Token = self.current_token
if token.type in [TT_PLUS, TT_MINUS, TT_BIN_OR, TT_BIN_AND, TT_BIN_NOT, TT_BIN_XOR, TT_BIN_LSHIFT, TT_BIN_RSHIFT]:
self.advance()
return UnaryOpNode(MathOperatorNode(token), self.no_identifier_factor())
elif token.type == TT_LPAREN:
self.advance()
expr = self.no_identifier_expr()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
return expr
elif token.type == TT_NUM_INT:
self.advance()
return IntNumberNode(token)
elif token.type == TT_NUM_FLOAT:
self.advance()
return FloatNumberNode(token)
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected value")
def no_identifier_expr(self) -> Any:
"""
<no_identifier_expr> ::= <no_identifier_term> (("+" | "-" | <logical_operator>) <no_identifier_term>)*
"""
return self.binary_op(self.no_identifier_term, [TT_PLUS, TT_MINUS, TT_BIN_OR, TT_BIN_AND, TT_BIN_NOT, TT_BIN_XOR, TT_BIN_LSHIFT, TT_BIN_RSHIFT])
def no_identifier_term(self) -> Any:
"""
<no_identifier_term> ::= <no_identifier_factor> (("*" | "/") <no_identifier_factor>)*
"""
return self.binary_op(self.no_identifier_factor, [TT_MULT, TT_DIV])
def factor(self) -> Any:
"""
<factor> ::= <num_int> | <num_float>
| ("+" | "-" | <logical_operator>) <factor>
| "(" <expr> ")"
| <identifier> ("." <identifier>)*
<logical_operator> ::= "&" | "|" | "^" | "<<" | ">>" | "~"
"""
token: Token = self.current_token
if token.type in [TT_PLUS, TT_MINUS, TT_BIN_OR, TT_BIN_AND, TT_BIN_NOT, TT_BIN_XOR, TT_BIN_LSHIFT, TT_BIN_RSHIFT]:
self.advance()
return UnaryOpNode(MathOperatorNode(token), self.factor())
elif token.type == TT_LPAREN:
self.advance()
expr = self.expr()
if self.current_token.type != TT_RPAREN:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected ')'")
self.advance()
return expr
elif token.type == TT_NUM_INT:
self.advance()
return IntNumberNode(token)
elif token.type == TT_NUM_FLOAT:
self.advance()
return FloatNumberNode(token)
elif token.type == TT_IDENTIFIER:
identifier_name: str = self.current_token.value
pos_start: Position = self.current_token.pos_start.get_copy() # for errors
self.advance()
if self.current_token.type == TT_DOT: # handle identifiers with '.' in them
while True: # consume tokens until there is another thing than a valid identifier
self.advance()
if self.current_token.type != TT_IDENTIFIER: # check if it is a valid identifier (avoid having numbers in the middle of the whole identifier for exemple)
raise InvalidSyntaxError(pos_start, self.current_token.pos_end, "invalid identifier")
identifier_name += "." + self.current_token.value
self.advance()
if self.current_token.type != TT_DOT:
break
return IdentifierAccessNode(identifier_name)
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected identifier or value")
def expr(self) -> Any:
"""
<expr> ::= <term> (("+" | "-" | <logical_operator>) <term>)*
"""
return self.binary_op(self.term, [TT_PLUS, TT_MINUS, TT_BIN_OR, TT_BIN_AND, TT_BIN_NOT, TT_BIN_XOR, TT_BIN_LSHIFT, TT_BIN_RSHIFT])
def term(self) -> Any:
"""
<term> ::= <factor> (("*" | "/") <factor>)*
"""
return self.binary_op(self.factor, [TT_MULT, TT_DIV])
def binary_op(self, func, operators) -> BinOpNode:
left_token = func()
if left_token is None:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected value or expression")
while self.current_token.type in operators:
op_token = self.current_token
self.advance()
right_token = func()
if right_token is None:
raise InvalidSyntaxError(self.current_token.pos_start, self.current_token.pos_end, "expected value or expression")
left_token = BinOpNode(left_token, MathOperatorNode(op_token), right_token)
return left_token