-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_parser.py
667 lines (618 loc) · 19.6 KB
/
my_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
from collections import OrderedDict
from my_ast import *
from my_grammar import *
class Parser(object):
def __init__(self, lexer):
self.lexer = lexer
self.file_name = lexer.file_name
self.current_token = None
self.indent_level = 0
self.next_token()
self.user_types = []
self.in_class = False
@property
def line_num(self):
return self.current_token.line_num
def next_token(self):
token = self.current_token
self.current_token = self.lexer.get_next_token()
# print(self.current_token)
return token
def eat_type(self, *token_type):
if self.current_token.type in token_type:
self.next_token()
else:
raise SyntaxError('Line {}'.format(self.line_num))
def eat_value(self, *token_value):
if self.current_token.value in token_value:
self.next_token()
else:
raise SyntaxError
def preview(self, num=1):
return self.lexer.preview_token(num)
def program(self):
root = Compound()
while self.current_token.type != EOF:
comp = self.compound_statement()
root.children.extend(comp.children)
return Program(root)
def struct_declaration(self):
self.eat_value(STRUCT)
name = self.next_token()
self.user_types.append(name.value)
self.eat_type(NEWLINE)
self.indent_level += 1
fields = OrderedDict()
while self.current_token.indent_level > name.indent_level:
field_type = self.type_spec()
field = self.next_token().value
fields[field] = field_type
self.eat_type(NEWLINE)
self.indent_level -= 1
return StructDeclaration(name.value, fields, self.line_num)
def class_declaration(self):
base = None
constructor = None
methods = None
class_fields = None
instance_fields = None
self.in_class = True
self.next_token()
class_name = self.current_token
self.eat_type(NAME)
if self.current_token.value == LPAREN:
pass # TODO impliment multiple inheritance
self.eat_type(NEWLINE)
self.indent_level += 1
while self.current_token.indent_level == self.indent_level:
if self.current_token.value == NEW:
constructor = self.constructor_declaration(class_name)
self.indent_level -= 1
self.in_class = False
return ClassDeclaration(class_name.value, base=base, constructor=constructor, methods=methods, class_fields=class_fields, instance_fields=instance_fields)
def variable_declaration(self):
type_node = self.type_spec()
var_node = Var(self.current_token.value, self.line_num)
self.eat_type(NAME)
var = VarDecl(var_node, type_node, self.line_num)
if self.current_token.value == ASSIGN:
var = self.variable_declaration_assignment(var)
return var
def variable_declaration_assignment(self, declaration):
return Assign(declaration, self.next_token().value, self.expr(), self.line_num)
def alias_declaration(self):
self.eat_value(ALIAS)
name = self.next_token()
self.user_types.append(name.value)
self.eat_value(ASSIGN)
return AliasDeclaration(name.value, (self.type_spec(),), self.line_num)
def function_declaration(self):
self.eat_value(DEF)
if self.current_token.value == LPAREN:
name = ANON
else:
name = self.next_token()
self.eat_value(LPAREN)
params = OrderedDict()
param_defaults = {}
vararg = None
while self.current_token.value != RPAREN:
if self.current_token.type == NAME:
param_type = self.variable(self.current_token)
self.eat_type(NAME)
else:
param_type = self.type_spec()
params[self.current_token.value] = param_type
param_name = self.current_token.value
self.eat_type(NAME)
if self.current_token.value != RPAREN:
if self.current_token.value == ASSIGN:
self.eat_value(ASSIGN)
param_defaults[param_name] = self.expr()
if self.current_token.value == ELLIPSIS:
key, value = params.popitem()
if not vararg:
vararg = []
vararg.append(key)
vararg.append(value)
self.eat_value(ELLIPSIS)
break
if self.current_token.value != RPAREN:
self.eat_value(COMMA)
self.eat_value(RPAREN)
self.eat_value(ARROW)
if self.current_token.value == VOID:
return_type = Void()
self.next_token()
else:
return_type = self.type_spec()
self.eat_type(NEWLINE)
self.indent_level += 1
stmts = self.compound_statement()
self.indent_level -= 1
if name == ANON:
return AnonymousFunc(return_type, params, stmts, self.line_num, param_defaults, vararg)
else:
return FuncDecl(name.value, return_type, params, stmts, self.line_num, param_defaults, vararg)
def constructor_declaration(self, class_name):
self.eat_value(NEW)
self.eat_value(LPAREN)
params = OrderedDict()
param_defaults = {}
vararg = None
while self.current_token.value != RPAREN:
if self.current_token.type == NAME:
param_type = self.variable(self.current_token)
self.eat_type(NAME)
else:
param_type = self.type_spec()
params[self.current_token.value] = param_type
param_name = self.current_token.value
self.eat_type(NAME)
if self.current_token.value != RPAREN:
if self.current_token.value == ASSIGN:
self.eat_value(ASSIGN)
param_defaults[param_name] = self.expr()
if self.current_token.value == ELLIPSIS:
key, value = params.popitem()
if not vararg:
vararg = []
vararg.append(key)
vararg.append(value)
self.eat_value(ELLIPSIS)
break
if self.current_token.value != RPAREN:
self.eat_value(COMMA)
self.eat_value(RPAREN)
self.eat_type(NEWLINE)
self.indent_level += 1
stmts = self.compound_statement()
self.indent_level -= 1
return FuncDecl('{}.constructor'.format(class_name), Void(), params, stmts, self.line_num, param_defaults, vararg)
def bracket_literal(self):
token = self.next_token()
if token.value == LCURLYBRACKET:
return self.curly_bracket_expression(token)
elif token.value == LPAREN:
return self.list_expression(token)
else:
return self.square_bracket_expression(token)
def function_call(self, token):
if token.value == INPUT:
return Input(self.expr(), self.line_num)
self.eat_value(LPAREN)
args = []
named_args = {}
while self.current_token.value != RPAREN:
while self.current_token.type == NEWLINE:
self.eat_type(NEWLINE)
if self.current_token.value in (LPAREN, LSQUAREBRACKET, LCURLYBRACKET):
args.append(self.bracket_literal())
elif self.preview().value == ASSIGN:
name = self.expr().value
self.eat_value(ASSIGN)
named_args[name] = self.expr()
else:
args.append(self.expr())
while self.current_token.type == NEWLINE:
self.eat_type(NEWLINE)
if self.current_token.value != RPAREN:
self.eat_value(COMMA)
func = FuncCall(token.value, args, self.line_num, named_args)
self.next_token()
return func
def type_spec(self):
token = self.current_token
if token.value in self.user_types:
self.eat_type(NAME)
return Type(token.value, self.line_num)
self.eat_type(TYPE)
type_spec = Type(token.value, self.line_num)
func_ret_type = None
if self.current_token.value == LSQUAREBRACKET and token.value == FUNC:
self.next_token()
func_ret_type = self.type_spec()
self.eat_value(RSQUAREBRACKET)
if func_ret_type:
type_spec.func_ret_type = func_ret_type
return type_spec
def compound_statement(self):
nodes = self.statement_list()
root = Compound()
for node in nodes:
root.children.append(node)
return root
def statement_list(self):
node = self.statement()
if self.current_token.type == NEWLINE:
self.next_token()
if isinstance(node, Return):
return [node]
results = [node]
while self.current_token.indent_level == self.indent_level:
results.append(self.statement())
if self.current_token.type == NEWLINE:
self.next_token()
elif self.current_token.type == EOF:
results = [x for x in results if x is not None]
break
return results
def statement(self):
if self.current_token.value == IF:
node = self.if_statement()
elif self.current_token.value == WHILE:
node = self.while_statement()
elif self.current_token.value == FOR:
node = self.for_statement()
elif self.current_token.value == BREAK:
self.next_token()
node = Break(self.line_num)
elif self.current_token.value == CONTINUE:
self.next_token()
node = Continue(self.line_num)
elif self.current_token.value == PASS:
self.next_token()
node = Pass(self.line_num)
elif self.current_token.value == CONST:
node = self.assignment_statement(self.current_token)
elif self.current_token.value == SWITCH:
self.next_token()
node = self.switch_statement()
elif self.current_token.value == RETURN:
node = self.return_statement()
elif self.current_token.value in self.user_types:
node = self.variable_declaration()
elif self.current_token.type == NAME:
if self.preview().value == DOT:
node = self.property_or_method(self.next_token())
else:
node = self.name_statement()
elif self.current_token.value == DEF:
node = self.function_declaration()
elif self.current_token.value == ALIAS:
node = self.alias_declaration()
elif self.current_token.type == TYPE:
if self.current_token.value == STRUCT:
node = self.struct_declaration()
else:
node = self.variable_declaration()
elif self.current_token.value == CLASS:
node = self.class_declaration()
elif self.current_token.value == EOF:
return
else:
self.next_token()
node = self.statement()
return node
def square_bracket_expression(self, token):
if token.value == LSQUAREBRACKET:
items = []
while self.current_token.value != RSQUAREBRACKET:
items.append(self.expr())
if self.current_token.value == COMMA:
self.next_token()
else:
break
self.eat_value(RSQUAREBRACKET)
return Collection(ARRAY, self.line_num, False, *items)
elif self.current_token.type == TYPE:
type_token = self.next_token()
if self.current_token.value == COMMA:
return self.dictionary_assignment(token)
elif self.current_token.value == RSQUAREBRACKET:
self.next_token()
return self.collection_expression(token, type_token)
elif self.current_token.type == NUMBER:
tok = self.expr()
if self.current_token.value == COMMA:
return self.slice_expression(tok)
else:
self.eat_value(RSQUAREBRACKET)
access = self.access_collection(token, tok)
if self.current_token.value in ASSIGNMENT_OP:
op = self.current_token
self.next_token()
right = self.expr()
if op.value == ASSIGN:
return Assign(access, op.value, right, self.line_num)
else:
return OpAssign(access, op.value, right, self.line_num)
return access
elif token.type == NAME:
self.eat_value(LSQUAREBRACKET)
tok = self.expr()
if self.current_token.value == COMMA:
return self.slice_expression(tok)
else:
self.eat_value(RSQUAREBRACKET)
return self.access_collection(token, tok)
else:
raise SyntaxError
def slice_expression(self, token):
pass
def curly_bracket_expression(self, token):
hash_or_struct = None
if token.value == LCURLYBRACKET:
pairs = OrderedDict()
while self.current_token.value != RCURLYBRACKET:
key = self.expr()
if self.current_token.value == COLON:
hash_or_struct = 'hash'
self.eat_value(COLON)
else:
hash_or_struct = 'struct'
self.eat_value(ASSIGN)
pairs[key.value] = self.expr()
if self.current_token.value == COMMA:
self.next_token()
else:
break
self.eat_value(RCURLYBRACKET)
if hash_or_struct == 'hash':
return HashMap(pairs, self.line_num)
elif hash_or_struct == 'struct':
return StructLiteral(pairs, self.line_num)
else:
raise SyntaxError('Wait... what?')
def list_expression(self, token):
if token.value == LPAREN:
items = []
while self.current_token.value != RPAREN:
items.append(self.expr())
if self.current_token.value == COMMA:
self.next_token()
else:
break
self.eat_value(RPAREN)
return Collection(LIST, self.line_num, False, *items)
def collection_expression(self, token, type_token):
if self.current_token.value == ASSIGN:
return self.array_of_type_assignment(token, type_token)
else:
raise NotImplementedError
def access_collection(self, collection, key):
return CollectionAccess(collection, key, self.line_num)
def array_of_type_assignment(self, token, type_token):
raise NotImplementedError
def dot_access(self, token):
self.eat_value(DOT)
field = self.current_token.value
self.next_token()
return DotAccess(token.value, field, self.line_num)
def name_statement(self):
token = self.next_token()
if token.value == PRINT:
node = Print(self.expr(), self.line_num)
elif token.value == INPUT:
node = Input(self.expr(), self.line_num)
elif self.current_token.value == LPAREN:
node = self.function_call(token)
elif self.current_token.value == LSQUAREBRACKET:
self.next_token()
node = self.square_bracket_expression(token)
elif self.current_token.value in ASSIGNMENT_OP:
node = self.assignment_statement(token)
else:
raise SyntaxError('Line {}'.format(self.line_num))
return node
def property_or_method(self, token):
self.eat_value(DOT)
field = self.current_token.value
self.next_token()
left = DotAccess(token.value, field, self.line_num)
token = self.next_token()
if token.value in ASSIGNMENT_OP:
return self.field_assignment(token, left)
else:
return self.method_call(token, left)
def method_call(self, token, left):
args = []
named_args = {}
while self.current_token.value != RPAREN:
while self.current_token.type == NEWLINE:
self.eat_type(NEWLINE)
if self.current_token.value in (LPAREN, LSQUAREBRACKET, LCURLYBRACKET):
args.append(self.bracket_literal())
elif self.preview().value == ASSIGN:
name = self.expr().value
self.eat_value(ASSIGN)
named_args[name] = self.expr()
else:
args.append(self.expr())
while self.current_token.type == NEWLINE:
self.eat_type(NEWLINE)
if self.current_token.value != RPAREN:
self.eat_value(COMMA)
method = MethodCall(left.obj, left.field, args, self.line_num, named_args)
self.next_token()
return method
def field_assignment(self, token, left):
if token.value == ASSIGN:
right = self.expr()
node = Assign(left, token.value, right, self.line_num)
elif token.value in ARITHMETIC_ASSIGNMENT_OP:
right = self.expr()
node = OpAssign(left, token.value, right, self.line_num)
else:
raise SyntaxError('Unknown assignment operator: {}'.format(token.value))
return node
def dictionary_assignment(self, token):
raise NotImplementedError
def return_statement(self):
self.next_token()
return Return(self.expr(), self.line_num)
def if_statement(self):
self.indent_level += 1
token = self.next_token()
comp = If(token.value, [self.expr()], [self.compound_statement()], token.indent_level, self.line_num)
if self.current_token.indent_level < comp.indent_level:
self.indent_level -= 1
return comp
while self.current_token.value == ELSE_IF:
self.next_token()
comp.comps.append(self.expr())
comp.blocks.append(self.compound_statement())
if self.current_token.value == ELSE:
self.next_token()
comp.comps.append(Else())
comp.blocks.append(self.compound_statement())
self.indent_level -= 1
return comp
def while_statement(self):
self.indent_level += 1
token = self.next_token()
comp = While(token.value, self.expr(), self.loop_block(), self.line_num)
self.indent_level -= 1
return comp
def for_statement(self):
self.indent_level += 1
self.next_token()
elements = []
while self.current_token.value != IN:
elements.append(self.expr())
if self.current_token.value == COMMA:
self.eat_value(COMMA)
self.eat_value(IN)
iterator = self.expr()
if self.current_token.value == NEWLINE:
self.eat_type(NEWLINE)
block = self.loop_block()
loop = For(iterator, block, elements, self.line_num)
self.indent_level -= 1
return loop
def switch_statement(self):
self.indent_level += 1
value = self.expr()
switch = Switch(value, [], self.line_num)
if self.current_token.type == NEWLINE:
self.next_token()
while self.current_token.indent_level == self.indent_level:
switch.cases.append(self.case_statement())
if self.current_token.type == NEWLINE:
self.next_token()
elif self.current_token.type == EOF:
return switch
self.indent_level -= 1
return switch
def case_statement(self):
self.indent_level += 1
if self.current_token.value == CASE:
self.next_token()
value = self.expr()
elif self.current_token.value == DEFAULT:
self.next_token()
value = DEFAULT
else:
raise SyntaxError
block = self.compound_statement()
self.indent_level -= 1
return Case(value, block, self.line_num)
def loop_block(self):
nodes = self.statement_list()
root = LoopBlock()
for node in nodes:
root.children.append(node)
return root
def assignment_statement(self, token):
if token.value == CONST:
read_only = True
self.next_token()
token = self.current_token
self.next_token()
else:
read_only = False
left = self.variable(token, read_only)
token = self.next_token()
if token.value == ASSIGN:
right = self.expr()
node = Assign(left, token.value, right, self.line_num)
elif token.value in ARITHMETIC_ASSIGNMENT_OP:
right = self.expr()
node = OpAssign(left, token.value, right, self.line_num)
else:
raise SyntaxError('Unknown assignment operator: {}'.format(token.value))
return node
def variable(self, token, read_only=False):
return Var(token.value, self.line_num, read_only)
def constant(self, token):
return Constant(token.value, self.line_num)
def factor(self):
token = self.current_token
preview = self.preview()
if preview.value == DOT:
self.next_token()
return self.dot_access(token)
elif token.value in (PLUS, MINUS):
self.next_token()
return UnaryOp(token.value, self.factor(), self.line_num)
elif token.value == NOT:
self.next_token()
return UnaryOp(token.value, self.expr(), self.line_num)
elif token.type == NUMBER:
self.next_token()
return Num(token.value, token.value_type, self.line_num)
elif token.type == STRING:
self.next_token()
return Str(token.value, self.line_num)
elif token.value == DEF:
return self.function_declaration()
elif token.type == TYPE:
return self.type_spec()
elif token.value == LPAREN:
if preview.value == RPAREN:
return []
else:
self.next_token()
node = self.expr()
self.eat_value(RPAREN)
return node
elif preview.value == LPAREN:
self.next_token()
return self.function_call(token)
elif preview.value == LSQUAREBRACKET:
self.next_token()
return self.square_bracket_expression(token)
elif token.value == LSQUAREBRACKET:
self.next_token()
return self.square_bracket_expression(token)
elif token.value == LCURLYBRACKET:
self.next_token()
return self.curly_bracket_expression(token)
elif token.type == NAME:
self.next_token()
return self.variable(token)
elif token.type == CONSTANT:
self.next_token()
return self.constant(token)
else:
raise SyntaxError
def term(self):
node = self.factor()
ops = (MUL, DIV, FLOORDIV, MOD, POWER, CAST, RANGE) + COMPARISON_OP + LOGICAL_OP + BINARY_OP
while self.current_token.value in ops:
token = self.next_token()
if token.value in COMPARISON_OP or token.value in LOGICAL_OP or token.value in BINARY_OP:
node = BinOp(node, token.value, self.expr(), self.line_num)
elif token.value == RANGE:
node = Range(node, self.expr(), self.line_num)
else:
node = BinOp(node, token.value, self.factor(), self.line_num)
return node
def expr(self):
node = self.term()
while self.current_token.value in (PLUS, MINUS):
token = self.next_token()
node = BinOp(node, token.value, self.term(), self.line_num)
return node
def parse(self):
node = self.program()
if self.current_token.type != EOF:
raise SyntaxError('Unexpected end of program')
return node
if __name__ == '__main__':
from my_lexer import Lexer
file = 'test.my'
l = Lexer(open(file).read(), file)
parser = Parser(l)
tree = parser.parse()
print(tree)