-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
988 lines (736 loc) · 32.1 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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
from lexer import LexerToken, TokenType, Keywords, Lexer
from error import ErrorList, Error, ErrorType
from parse.source_location import SourceLocation
from parse.node import *
class Parser():
def __init__(self, tokens, source_location):
self.tokens = tokens
self.token_index = 0
self._current_token = self.next_token()
self.error_list = ErrorList()
self.source_location = source_location
self.keyword_methods = {
'let': self.parse_variable_declaration,
'if': self.parse_if_statement,
'func': self.parse_func_declaration,
'import': self.parse_import,
'return': self.parse_return,
'while': self.parse_while,
'for': self.parse_for,
'macro': self.parse_macro,
'mixin': self.parse_mixin,
'try': self.parse_try,
}
@property
def filename(self):
return self.source_location.filename
@property
def current_token(self):
if self._current_token is None:
return LexerToken.NONE
return self._current_token
def expect_token(self, token_type, offset=0, token=None):
# if no token passed in, peek from offset
if token == None:
token = self.peek_token(offset)
if token.type == token_type:
return token
else:
self.error('expected {0} but recieved {1}'.format(token_type.name, token.type.name))
return None
def next_token(self):
# check if next index is past list boundaries
if self.token_index+1 > len(self.tokens):
return None
# return selected token, increment index
self._current_token = self.tokens[self.token_index]
self.token_index += 1
return self.current_token
# return token at token.index + offset
def peek_token(self, offset=1, expected_type=None):
# check bounds
if self.token_index+offset-1 > len(self.tokens):
return None
token = self.tokens[self.token_index+offset-1]
# check type if expected_type != None
if expected_type != None and token.type != expected_type:
return None
return token
def error(self, message):
# tokens have locations attached from the lexer, pass to self.error
# if an error occurs
location = self.current_token.location
self.error_list.push_error(Error(ErrorType.Syntax, location, message, self.filename, "Syntax Error"))
# read next token and error if token.type != passed in token type
def eat(self, token_type=None):
if token_type is not None:
if self.expect_token(token_type) is None:
return None
self._current_token = self.next_token()
return self.current_token
# parse reference to a variable
def parse_variable(self):
# create variable node and eat identifier
variable_node = NodeVariable(self.current_token)
self.eat(TokenType.Identifier)
return variable_node
def parse_member_expression(self, lhs):
if self.eat(TokenType.Dot) is None:
return None
token = self.current_token
# expect identifier for right hand side
rhs_name = self.peek_token(0, expected_type=TokenType.Identifier)
if rhs_name is None:
self.error('invalid member access: must be in format <expression>.<identifier>')
return None
self.eat(TokenType.Identifier)
return NodeMemberExpression(lhs, rhs_name, token)
def parse_array_access_expression(self, lhs):
if self.eat(TokenType.LBracket) is None:
return None
token = self.current_token
# get internal expr
access_expr = self.parse_expression()
if access_expr is None:
self.error('invalid array access expression')
return None
if self.eat(TokenType.RBracket) is None:
return None
return NodeArrayAccessExpression(lhs, access_expr, token)
def import_file(self, filename, filename_token=None):
try:
fp = open(filename, 'r')
if not filename.endswith(".para") and not filename.endswith(".paracode"):
self.error('source file \'{}\'s file extension (\'{}\') is not a valid ParaCode extension'.format(filename, "." + filename.split(".")[-1]))
fp.close()
return None
except FileNotFoundError:
self.error('source file \'{}\' does not exist'.format(filename))
return None
data = fp.read()
# lex loaded file data
source_location = SourceLocation(filename)
lexer = Lexer(data, source_location)
tokens = lexer.lex()
parser = Parser(tokens, source_location)
# an import node acts similar to a block and holds all variables and functions
# in a tree. A parser is passed for getting various information in the interpreter
if filename_token == None:
filename_token = LexerToken(f'"{filename}"')
node = NodeImport(filename_token, source_location)
node.children = parser.get_statements()
for error in parser.error_list.errors:
self.error_list.push_error(error)
return node
def parse_while(self):
# eat while keyword
token = self.current_token
if not self.eat(TokenType.Keyword):
return None
expression = self.parse_expression()
if expression == None:
return None
block = self.parse_block_statement()
if block == None:
return None
return NodeWhile(expression, block, token)
def parse_for(self):
# eat for keyword
token = self.current_token
if not self.eat(TokenType.Keyword):
return None
# get var name of iter
var_token = self.current_token
self.eat(TokenType.Identifier)
if var_token is None:
return None
# eat in keyword
in_keyword = self.current_token
self.eat(TokenType.Keyword)
if in_keyword is None or in_keyword.value != 'in':
self.error('for loop expects syntax `for <var> in <expr> { ... }`')
return None
expression = self.parse_expression()
if expression == None:
return None
block = self.parse_block_statement()
if block == None:
return None
return NodeFor(var_token, expression, block, token)
def parse_macro(self):
token = self.current_token
self.eat(TokenType.Keyword)
# eat macro name
name = self.current_token
if self.eat(TokenType.Identifier) is None:
return None
argument_list = None
# eat optional arguments
if self.peek_token(0, TokenType.LParen):
argument_list = self.parse_argument_list()
if argument_list is None:
return None
# parse block
block = self.parse_block_statement()
if block is None:
return None
# add self argument
macro_self_argument = NodeDeclare(None, LexerToken("__macro_self", TokenType.Identifier), NodeNone(token))
if argument_list is None:
argument_list = NodeArgumentList(
[macro_self_argument],
token
)
else:
argument_list.arguments = [macro_self_argument, *argument_list.arguments]
fun_expr = NodeFunctionExpression(argument_list, block)
#macro_expr = NodeMacro(fun_expr, token)
macro_var = NodeVariable(LexerToken('Macro', TokenType.Identifier))
member_access_call_node = NodeCall(
NodeMemberExpression(
macro_var,
LexerToken('new', TokenType.Identifier),
macro_var.token
),
NodeArgumentList(
[fun_expr],
macro_var.token
)
)
# parse assignment, parenthesis, etc.
val_node = NodeAssign(NodeVariable(name), NodeMacro(member_access_call_node, token))
type_node = NodeVariable(LexerToken('Macro', TokenType.Identifier))
node = NodeDeclare(type_node, name, val_node)
return node
def parse_mixin(self):
# TODO error if not in macro
# eat keyword
token = self.current_token
self.eat(TokenType.Keyword)
# eat tokens in block
if self.eat(TokenType.LBrace) is None:
return None
tokens = []
brace_level = 1
while brace_level > 0 and self.current_token != LexerToken.NONE:
if self.current_token.type == TokenType.LBrace:
brace_level += 1
elif self.current_token.type == TokenType.RBrace:
brace_level -= 1
tokens.append(self.current_token)
self.eat()
return NodeMixin(tokens, token)
def parse_import(self):
self.eat(TokenType.Keyword)
filename = None
filename_token = self.current_token
# check for path after import
self.expect_token(TokenType.String, token=filename_token)
# trim off ""
filename = self.current_token.value[1:-1]
import os
if not os.path.exists(filename):
if os.path.exists("pkg_data/" + filename):
filename = "pkg_data/" + filename
self.eat(TokenType.String)
return self.import_file(filename, filename_token)
def parse_return(self):
self.eat(TokenType.Keyword)
value_node = self.parse_expression()
return NodeFunctionReturn(value_node, self.current_token)
def parse_assignment_statement(self, node, require_operator=True):
# operator would be '=' or '+=', '-=', etc.
if require_operator:
self.eat()
value = self.parse_expression()
if value is None:
self.error('Invalid assignment')
return None
node = NodeAssign(node, value)
return node
def parse_argument_list(self):
# eat open parenthesis
if self.eat(TokenType.LParen) is None:
return None
argument_list = None
if self.peek_token(0, expected_type=TokenType.RParen):
argument_list = NodeArgumentList([], self.current_token)
else:
arguments = []
has_vargs = False
first_arg = True
any_default = False
while True:
if has_vargs:
self.error('Arguments provided after variadic arguments')
break
is_vargs = False
if self.peek_token(0, TokenType.Multiply):
is_vargs = True
has_vargs = True
argument = None
if is_vargs:
# eat *
if self.eat(TokenType.Multiply) is None:
return None
token = self.current_token
var = self.parse_variable()
if var is None:
return None
argument = NodeSplatArgument(var, token)
else:
if self.expect_token(TokenType.Identifier) is None:
self.error('invalid argument format')
break
# parse declaration(vname:type) without let keyword
argument = self.parse_variable_declaration(require_keyword=False)
if argument is None:
self.error('invalid argument')
break
if not isinstance(argument, NodeSplatArgument) and hasattr(argument.value, 'value'):
any_default = True
else:
if any_default:
self.error('non-default argument follows default argument')
break
arguments.append(argument)
if self.peek_token(0, expected_type=TokenType.Comma):
# eat comma and continue on with argument list
self.eat(TokenType.Comma)
first_arg = False
elif not first_arg and not self.peek_token(0, expected_type=TokenType.RParen) and not self.peek_token(1, expected_type=TokenType.LBrace):
# not first arg and no comma, rollback?
break
else:
break
argument_list = NodeArgumentList(arguments, self.current_token)
if argument_list is None:
self.error('invalid argument list')
return None
# eat closing parenthesis
self.eat(TokenType.RParen)
return argument_list
def parse_parenthesis(self):
# eat open parenthesis
self.eat(TokenType.LParen)
expr = self.parse_expression()
self.eat(TokenType.RParen)
return expr
def parse_statement(self):
token = self.current_token
if token.type == TokenType.NoneToken:
raise Exception('none')
# empty statement, eat semicolon and try again
if token.type == TokenType.Semicolon:
self.eat(TokenType.Semicolon)
return NodeNone(token)
if token.type == TokenType.Keyword:
node = self.parse_keyword()
if node is None:
return None
# check if node is function block, exempt from semicolon
if node.type == NodeType.Declare and (node.value is not None and node.value.type == NodeType.Assign):
rhs = node.value.value
if rhs.type in (NodeType.FunctionExpression, NodeType.Macro):
return node
if node.type in (NodeType.IfStatement, NodeType.Try, NodeType.While, NodeType.For, NodeType.Mixin):
return node
else:
node = self.parse_expression()
if node is None:
self.error('Unknown token {} in statement'.format(token.type))
node = None
if self.current_token.type != TokenType.Semicolon:
self.error('Missing semicolon (found {})'.format(self.current_token.type.name))
else:
# eat semicolon at end of statement
self.eat(TokenType.Semicolon)
return node
def get_statements(self):
statements = []
# read until no statements left
while self.current_token != LexerToken.NONE:
# We hit last statement in block, break
if self.current_token.type == TokenType.RBrace:
break
statement = self.parse_statement()
# parse statement and skip to next semicolon
statements.append(statement)
return statements
def parse_keyword(self):
keyword = self.expect_token(TokenType.Keyword)
method = None
if keyword is not None:
method = self.keyword_methods[keyword.value]
if method is None:
self.error('{0} is not a valid keyword'.format(keyword))
return method()
def parse_block_statement(self):
self.eat(TokenType.LBrace)
block = NodeBlock(self.current_token)
block.children = self.get_statements()
self.eat(TokenType.RBrace)
return block
def parse_array_expression(self):
is_dictionary = False
members = []
# eat left bracket
if self.eat(TokenType.LBracket) is None:
return None
token = self.current_token
at_dictionary_value = False
if self.peek_token(0, expected_type=TokenType.Colon) and self.peek_token(1, expected_type=TokenType.RBracket):
is_dictionary = True
members = [[], []]
self.eat(TokenType.Colon)
token = self.current_token
while token.type != TokenType.RBracket:
# parse expr
item_expr = self.parse_expression()
if item_expr is None:
self.error('invalid array member item {}'.format(self.current_token))
return None
if not is_dictionary and members == [] and self.peek_token(0, expected_type=TokenType.Colon):
is_dictionary = True
members = [[], []]
if is_dictionary:
if not at_dictionary_value:
members[0].append(item_expr)
else:
members[1].append(item_expr)
else:
members.append(item_expr)
if self.current_token.type == TokenType.Comma:
self.eat(TokenType.Comma)
if is_dictionary and at_dictionary_value:
at_dictionary_value = False
elif self.current_token.type == TokenType.Colon and is_dictionary and not at_dictionary_value:
self.eat(TokenType.Colon)
at_dictionary_value = True
else:
break
if is_dictionary:
members[0] = NodeArrayExpression(members[0], token)
members[1] = NodeArrayExpression(members[1], token)
if self.eat(TokenType.RBracket) is None:
return None
return NodeArrayExpression(members, token, is_dictionary)
def parse_object_expression(self):
members = [] # array of var declarations
# eat left brace
if self.eat(TokenType.LBrace) is None:
return None
token = self.current_token
# find all lines in block
while token.type != TokenType.RBrace:
# parse variable declaration
var_decl = self.parse_variable_declaration(False)
if var_decl is None:
self.error('invalid object member declaration')
return None
members.append(var_decl)
token = self.current_token
if self.eat(TokenType.RBrace) is None:
return None
return NodeObjectExpression(members)
def parse_type(self):
node = NodeVarType(self.current_token)
self.eat()
return node
def parse_function_call(self, node):
self.eat(TokenType.LParen)
argnames = []
last = self.current_token
if self.current_token.type is not TokenType.RParen:
# skip until RParen
while self.current_token.type != TokenType.RParen:
# append argument to ArgumentList node
if self.peek_token(0, expected_type=TokenType.Multiply):
# splat args
vargs_token = self.current_token
self.eat(TokenType.Multiply)
splat_expr = self.parse_expression()
if splat_expr is None:
return None
argnames.append(NodeSplatArgument(splat_expr, vargs_token))
else:
argnames.append(self.parse_expression())
if self.current_token.type == TokenType.RParen:
break
self.eat()
if self.current_token.type == TokenType.NoneToken:
return NodeNone(last)
# eat closing paren
self.eat(TokenType.RParen)
args = NodeArgumentList(argnames, self.current_token)
return NodeCall(node, args)
def parse_function_expression(self):
token = self.current_token
if self.eat(TokenType.Keyword) is None:
return None
argument_list = self.parse_argument_list()
if argument_list is None:
return None
block = self.parse_block_statement()
if block is None:
return None
return NodeFunctionExpression(argument_list, block)
def parse_arrow_function(self, node):
token = self.current_token
arguments = []
if isinstance(node, NodeVariable):
print(type(node.token))
arguments.append(NodeDeclare(None, node.token, NodeNone(token)))
if self.eat(TokenType.Arrow) is None:
return None
expr = self.parse_expression()
return_node = NodeFunctionReturn(expr, token)
block = NodeBlock(token)
block.children = [return_node]
fun_expr = NodeFunctionExpression(
NodeArgumentList(
arguments,
token
),
block
)
return fun_expr
def parse_func_declaration(self):
# func NAME(...) { ... }
# eat func keyword
type = self.current_token
self.eat(TokenType.Keyword)
# eat function name
name = self.current_token
if self.eat(TokenType.Identifier) is None:
return None
argument_list = self.parse_argument_list()
if argument_list is None:
return None
block = self.parse_block_statement()
if block is None:
return None
fun_expr = NodeFunctionExpression(argument_list, block)
# parse assignment, parenthesis, etc.
val_node = NodeAssign(NodeVariable(name), fun_expr)
type_node = NodeVariable(LexerToken('Func', TokenType.Identifier))
node = NodeDeclare(type_node, name, val_node)
return node
def parse_variable_declaration(self, require_keyword=True):
# let VARNAME:TYPE parse_assignment_statement
if require_keyword:
# eat let keyword
if self.eat(TokenType.Keyword) is None:
return None
elif self.peek_token(0, expected_type=TokenType.Keyword) and self.peek_token(0).value == 'func':
return self.parse_func_declaration()
name = self.current_token
if self.eat(TokenType.Identifier) is None:
return None
type_node = None
allow_casting = False
# manual type set
if self.current_token.type == TokenType.Colon:
self.eat(TokenType.Colon)
type_node_token = self.current_token
type_node = self.parse_factor()
if type_node is None or (not isinstance(type_node, NodeVariable) and not isinstance(type_node, NodeMemberExpression)):
self.error('Declaration type should either be an identifier or member access, got {}'.format(type_node_token))
return None
if self.current_token.type == TokenType.Question:
self.eat(TokenType.Question)
allow_casting = True
if self.current_token.type == TokenType.Equals:
val_node = self.parse_assignment_statement(NodeVariable(name, allow_casting))
else:
val_node = NodeNone(name)
vnodes = NodeDeclare(type_node, name, val_node, allow_casting)
return vnodes
def parse_if_statement(self):
# eat `if`
if_token = self.current_token
if self.eat(TokenType.Keyword) is None:
return None
expr = self.parse_expression()
if expr is None:
return None
block = self.parse_block_statement()
if block is None:
return None
else_block = None
token = self.current_token
if token.type == TokenType.Keyword:
if Keywords(token.value) == Keywords.Else:
# eat else
self.eat(TokenType.Keyword)
else_block = self.parse_block_statement()
elif Keywords(token.value) == Keywords.Elif:
else_block = self.parse_if_statement()
return NodeIfStatement(expr, block, else_block, if_token)
def parse_try(self):
# eat `try`
try_token = self.current_token
if self.eat(TokenType.Keyword) is None:
return None
block = self.parse_block_statement()
if block is None:
return None
catch_block = []
token = self.current_token
expr = []
variable = []
else_block = None
finally_block = None
while token.type == TokenType.Keyword and Keywords(token.value) == Keywords.Catch:
# eat catch
self.eat(TokenType.Keyword)
if self.current_token.type == TokenType.LBracket:
values = []
while self.current_token.type != TokenType.RBracket:
if self.current_token.type != TokenType.Comma and self.current_token.type != TokenType.LBracket:
values.append(NodeString(self.current_token))
self.eat(self.current_token.type)
self.eat(self.current_token.type)
expr.append(values)
if self.current_token.type == TokenType.Identifier:
e = self.parse_expression()
print(e)
elif self.current_token.type == TokenType.LBrace:
expr.append(NodeString(LexerToken("Exception")))
elif self.current_token.type == TokenType.Identifier:
expr.append(self.parse_expression())
if self.current_token.type == TokenType.Identifier:
e = self.parse_expression()
val_node = NodeAssign(NodeVariable(e.token), expr[-1])
type_node = NodeVariable(LexerToken("Exception", TokenType.Identifier))
# type_node_token = self.current_token
# type_node = self.parse_factor()
n = NodeDeclare(type_node, e.token, val_node)
variable.append(n)
elif self.current_token.type == TokenType.LBrace:
variable.append(None)
catch_block.append(self.parse_block_statement())
token = self.current_token
if token.type == TokenType.Keyword and Keywords(token.value) == Keywords.Else:
# eat else
self.eat(TokenType.Keyword)
else_block = self.parse_block_statement()
token = self.current_token
if token.type == TokenType.Keyword and Keywords(token.value) == Keywords.Finally:
# eat finally
self.eat(TokenType.Keyword)
finally_block = self.parse_block_statement()
token = self.current_token
return NodeTryCatch(block, catch_block, expr, else_block, finally_block, try_token, variable)
def parse_factor(self):
# handles value or (x ± x)
token = self.current_token
node = None
# handle +, -
if token.type in (TokenType.Plus, TokenType.Minus):
self.eat(token.type)
node = NodeUnaryOp(token, self.parse_factor())
# handle '!'
elif token.type == TokenType.Not:
self.eat(TokenType.Not)
node = NodeUnaryOp(token, self.parse_factor())
# handle '~'
elif token.type == TokenType.BitwiseNot:
self.eat(TokenType.BitwiseNot)
node = NodeUnaryOp(token, self.parse_factor())
elif token.type == TokenType.Number:
self.eat(TokenType.Number)
node = NodeNumber(token)
elif token.type == TokenType.String:
self.eat(TokenType.String)
node = NodeString(token)
elif token.type == TokenType.LParen:
self.eat(TokenType.LParen)
node = self.parse_expression()
self.eat(TokenType.RParen)
elif token.type == TokenType.LBracket:
node = self.parse_array_expression()
elif token.type == TokenType.LBrace:
node = self.parse_object_expression()
elif token.type == TokenType.Identifier:
node = self.parse_variable()
if self.peek_token(0, expected_type=TokenType.Arrow):
node = self.parse_arrow_function(node)
elif token.type == TokenType.Keyword:
if Keywords(token.value) == Keywords.Func:
node = self.parse_function_expression()
else:
self.error('Invalid usage of keyword {} in expression'.format(token.value))
return None
if node is None:
self.error('Unexpected token: {}'.format(self.current_token))
self.eat()
return None
while self.current_token.type in (TokenType.Dot, TokenType.LParen, TokenType.LBracket):
if self.peek_token(0, expected_type=TokenType.Dot):
node = self.parse_member_expression(node)
elif self.peek_token(0, expected_type=TokenType.LBracket):
node = self.parse_array_access_expression(node)
elif self.peek_token(0, expected_type=TokenType.LParen):
node = self.parse_function_call(node)
return node
def parse_term(self):
# handles multiply, division, expressions
node = self.parse_factor()
while self.current_token.type in (TokenType.Multiply, TokenType.Divide):
token = self.current_token
if token.type == TokenType.Multiply:
self.eat(TokenType.Multiply)
elif token.type == TokenType.Divide:
self.eat(TokenType.Divide)
node = NodeBinOp(left=node, token=token, right=self.parse_factor())
return node
def parse_expression(self):
node = self.parse_term()
multiop_types = (
TokenType.PlusEquals, TokenType.MinusEquals,
TokenType.MultiplyEquals, TokenType.DivideEquals,
TokenType.ModulusEquals,
TokenType.BitwiseOrEquals, TokenType.BitwiseAndEquals,
TokenType.BitwiseXorEquals,
TokenType.BitwiseLShiftEquals, TokenType.BitwiseRShiftEquals
)
expected_types = (
TokenType.Equals,
TokenType.Plus, TokenType.Minus, TokenType.Modulus,
TokenType.Compare, TokenType.NotCompare,
TokenType.Spaceship,
TokenType.Arrow,
TokenType.LessThan, TokenType.GreaterThan,
TokenType.LessThanEqual, TokenType.GreaterThanEqual,
TokenType.BitwiseOr, TokenType.BitwiseAnd, TokenType.BitwiseXor,
TokenType.And, TokenType.Or,
TokenType.BitwiseLShift, TokenType.BitwiseRShift,
TokenType.Exponentiation
) + multiop_types
while self.current_token.type in expected_types:
token = self.current_token
if self.peek_token(0, expected_type=TokenType.Equals):
node = self.parse_assignment_statement(node)
continue
if self.current_token.type in multiop_types:
# parse (lhs [operator] rhs) and return assign node
assign_node = self.parse_assignment_statement(node)
# this is slightly sketchy, but also will be able to handle
# operations like <<= and any other multichar operation
operation = LexerToken(token.value.strip('='))
# make value (lhs [operator] rhs)
value_node = NodeBinOp(left=node, token=operation, right=assign_node.value)
# final node should be (lhs [=] lhs [operator] rhs)
assign_node.value = value_node
node = assign_node
continue
if token.type in expected_types:
self.eat()
if (token.type == TokenType.Or or token.type == TokenType.And) and self.peek_token().type in expected_types:
node = NodeBinOp(left=node, token=token, right=self.parse_expression())
continue
node = NodeBinOp(left=node, token=token, right=self.parse_term())
return node
def parse(self):
return self.get_statements()