-
Notifications
You must be signed in to change notification settings - Fork 7
/
pico_parse.py
1042 lines (854 loc) · 33 KB
/
pico_parse.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
989
990
991
992
993
994
995
996
997
998
999
1000
from utils import *
from pico_defs import Language
from pico_tokenize import TokenNodeBase, Token, TokenType
from pico_tokenize import is_identifier, k_identifier_split_re
k_nested = object()
class VarKind(Enum):
local = global_ = member = label = ...
class VarBase():
"""A variable (a Local, Global, table Member key, or Label)"""
def __init__(m, kind, name):
m.kind = kind
m.name = name
m.is_const = None
m.implicit = False # has implicit *access*?
m.reassigned = 0
keys_kind = None
rename = None
constval = None
def __repr__(m):
return f"{typename(m)}({m.name})"
class Local(VarBase):
def __init__(m, name, scope, builtin=False):
super().__init__(VarKind.local, name)
m.scope = scope
m.builtin = builtin
m.captured = False
class Global(VarBase):
def __init__(m, name):
super().__init__(VarKind.global_, name)
class Member(VarBase):
def __init__(m, name):
super().__init__(VarKind.member, name)
class Label(VarBase):
def __init__(m, name, scope):
super().__init__(VarKind.label, name)
m.scope = scope
class Scope:
"""A scope that defines new Locals. Note that in lua, every 'local' statement creates a new Scope"""
def __init__(m, parent=None, depth=0, funcdepth=0):
m.parent = parent
m.depth = depth
m.funcdepth = funcdepth
m.items = {}
def add(m, var):
m.items[var.name] = var
def find(m, name):
var = m.items.get(name)
if var:
return var
elif m.parent:
return m.parent.find(name)
def chain(m):
curr = m
while curr:
yield curr
curr = curr.parent
@lazy_property
def used_locals(m): # set of Local or Label objs
return set()
@lazy_property
def used_globals(m): # set of names, not of Global objs
return set()
@property
def has_used_globals(m):
return lazy_property.is_set(m, "used_globals")
@lazy_property
def used_members(m): # set of names, not of Member objs
# note - only for members used as if they were globals,
# NOT for normally used members! (very rare)
return set()
@property
def has_used_members(m):
return lazy_property.is_set(m, "used_members")
class LabelScope:
"""A scope that defines the labels in a block"""
def __init__(m, parent=None, funcdepth=0):
m.parent = parent
m.funcdepth = funcdepth
m.labels = None
def add(m, var):
if m.labels is None:
m.labels = {}
m.labels[var.name] = var
def find(m, name, crossfunc=False):
var = m.labels.get(name) if m.labels else None
if var:
return var
elif m.parent and (crossfunc or m.parent.funcdepth == m.funcdepth):
return m.parent.find(name)
def chain(m, crossfunc=False):
curr = m
while curr:
yield curr
curr = curr.parent if (crossfunc or curr.parent.funcdepth == curr.funcdepth) else None
# for implementation reuse with locals, we stored used labels in used_locals
@lazy_property
def used_locals(m):
return set()
@property
def has_used_locals(m):
return lazy_property.is_set(m, "used_locals")
@property
def has_used_globals(m):
return False
@property
def has_used_members(m):
return False
class NodeType(Enum):
var = index = member = const = group = unary_op = binary_op = call = ...
table = table_index = table_member = varargs = assign = op_assign = ...
local = function = if_ = elseif = else_ = while_ = repeat = until = ...
for_ = for_in = return_ = break_ = goto = label = block = do = ...
sublang = super_root = ... # special
class Node(TokenNodeBase):
"""A pico8 syntax tree node, spanning 'source'.text['idx':'endidx']. Its 'type' is a NodeType
Its 'children' is a list of the Tokens and Nodes that comprise it (for traversal purposes),
while each node type has additional properties specifying the relevant children (search for the NodeType below to find out which)."""
def __init__(m, type, children, **kwargs):
super().__init__()
m.type, m.children = type, children
m.__dict__.update(kwargs)
for child in children:
child.parent = m
def get_tokens(m): # (not including erased tokens, whereas traverse includes them)
tokens = []
def on_token(token):
if token.value != None:
tokens.append(token)
m.traverse_tokens(on_token)
return tokens
short = False # default property
assignment = False # default proprerty
value = None # default proprty (TODO: bad, for consistency with Token)
@property
def source(m):
return m.first_token().source
@property
def idx(m):
return m.first_token().idx
@property
def endidx(m):
return m.last_token().endidx
@property
def lang(m):
return m.first_token().lang
def _create_for_insert(m, i, type, value, near_next):
if near_next:
if i < len(m.children):
return Token.synthetic(type, value, m.children[i], prepend=True)
else:
return Token.synthetic(type, value, m.next_token(), prepend=True)
else:
if i > 0:
return Token.synthetic(type, value, m.children[i - 1], append=True)
else:
return Token.synthetic(type, value, m.prev_token(), append=True)
def insert_token(m, i, type, value, near_next=False):
m.children.insert(i, m._create_for_insert(i, type, value, near_next))
m.children[i].parent = m
def append_token(m, type, value, near_next=False):
m.children.append(m._create_for_insert(len(m.children), type, value, near_next))
m.children[-1].parent = m
def modify_token(m, i, value, expected=None):
m.children[i].modify(value, expected)
def erase_token(m, i, expected=None):
m.children[i].erase(expected)
def remove_token(m, i, expected=None):
if expected != None:
m.children[i].check(expected)
del m.children[i]
def insert_existing(m, i, existing, near_next=False):
src = m._create_for_insert(i, None, None, near_next)
def reset_location(token):
token.idx, token.endidx = src.idx, src.endidx
token.vline, token.modified = None, True
existing.traverse_tokens(reset_location)
m.children.insert(i, existing)
m.children[i].parent = m
def append_existing(m, existing, near_next=False):
m.insert_existing(len(m.children), existing, near_next)
def remove_child(m, i):
del m.children[i]
def replace_with(m, target): # target must not reference m, but may reference m.copy() or m.move()
old_parent = m.parent
m.__dict__ = target.__dict__
m.parent = old_parent
for child in m.children:
child.parent = m
def erase(m):
m.replace_with(Node(None, []))
class ParseError(Exception):
pass
k_unary_ops = {
"-", "~", "not", "#", "@", "%", "$", "*",
}
k_unary_ops_prec = 11
k_unary_op_langs = {
"*": Language.picotron
}
k_binary_op_precs = {
"or": 1, "and": 2,
"!=": 3, "~=": 3, "==": 3, "<": 3, "<=": 3, ">": 3, ">=": 3,
"|": 4, "^^": 5, "~": 5, "&": 6,
"<<": 7, ">>": 7, ">>>": 7, ">><": 7, "<<>": 7,
"..": 8,
"+": 9, "-": 9,
"*": 10, "/": 10, "//": 10, "\\": 10, "%": 10,
"^": 12,
}
k_right_binary_ops = {
"^", ".."
}
k_binary_op_langs = {
">>>": Language.pico8,
">><": Language.pico8,
"<<>": Language.pico8,
"//": Language.picotron,
}
k_block_ends = ("end", "else", "elseif", "until")
k_prefix_types = (NodeType.var, NodeType.member, NodeType.index, NodeType.call, NodeType.group)
def parse(source, tokens, ctxt=None, super_root=None, lang=None, for_expr=False):
idx = 0
depth = -1 # incremented in parse_block
funcdepth = -1 # incremented in parse_root
scope = Scope(None, depth, funcdepth)
labelscope = LabelScope(None, funcdepth)
unresolved_labels = None
errors = []
if lang is None:
lang = ctxt.lang if ctxt else Language.pico8
is_pico8 = lang == Language.pico8
is_picotron = lang == Language.picotron
if super_root and super_root.children:
globals, members, has_env = super_root.globals, super_root.members, super_root.has_env
else:
globals = LazyDict(lambda key: Global(key))
members = LazyDict(lambda key: Member(key))
has_env = False
scope.add(Local("_ENV", scope))
if ctxt and ctxt.local_builtins:
for local in ctxt.local_builtins:
scope.add(Local(local, scope, builtin=True))
def peek(off=0):
i = idx + off
return tokens[i] if 0 <= i < len(tokens) else Token.dummy(source)
def take():
nonlocal idx
token = peek()
idx += 1
return token
def accept(value, tokens=None):
nonlocal idx
if peek().value == value:
if e(tokens):
tokens.append(peek())
idx += 1
return True
return False
def add_error_at(msg, token, fail=False):
errors.append(Error(msg, token))
if fail:
raise ParseError()
def add_error(msg, off=0, fail=False):
add_error_at(msg, peek(off), fail)
def require(value, tokens=None):
if not accept(value, tokens):
add_error(f"expected '{value}'", fail=True)
return peek(-1)
def require_ident(tokens=None):
nonlocal idx
if peek().type == TokenType.ident:
if e(tokens):
tokens.append(peek())
idx += 1
return peek(-1)
add_error("identifier expected", fail=True)
def parse_var(token=None, new=None, member=False, label=False, implicit=False, const=None, with_attr=False):
token = token or require_ident()
tokens = [token]
name = token.value
var = None
search_scope = None
attr = None
if member:
kind = VarKind.member
var = members[name]
elif label:
kind = VarKind.label
search_scope = labelscope
if new:
var = Label(name, labelscope)
# else, can't resolve until func ends
else:
kind = VarKind.local
search_scope = new or scope
if new:
assert isinstance(new, Scope)
var = Local(name, new)
else:
var = scope.find(name)
if var and var.scope.funcdepth != scope.funcdepth:
var.captured = True
if not var:
kind = VarKind.global_
var = globals[name]
if ctxt and ctxt.consts and name in ctxt.consts:
var.is_const = True
var.constval = ctxt.consts[name]
if name == "_ENV" and not implicit:
nonlocal has_env
has_env = True
if implicit and var:
var.implicit = True
var_kind = getattr(token, "var_kind", None)
if var:
if hasattr(token, "keys_kind"):
var.keys_kind = token.keys_kind
if hasattr(token, "rename"):
var.rename = token.rename
const = getattr(token, "is_const", const)
if const != None:
var.is_const = const
if with_attr and is_picotron and accept("<", tokens):
attr = take()
if attr.value not in ("const", "close"):
add_error(f"unknown attribute '{attr.value}'")
tokens.append(attr)
require(">", tokens)
node = Node(NodeType.var, tokens, name=name, kind=kind, var_kind=var_kind, var=var, new=bool(new), scope=search_scope, attr=attr)
if label and not new:
nonlocal unresolved_labels
if unresolved_labels is None:
unresolved_labels = []
unresolved_labels.append(node)
if kind == VarKind.global_:
env_token = Token.synthetic(TokenType.ident, "_ENV", token)
env_node = parse_var(token=env_token, implicit=True)
node.add_extra_child(env_node)
node.env_var = env_node.var
return node
def parse_function(stmt=False, local=False):
nonlocal scope, funcdepth, unresolved_labels
if local:
tokens = [peek(-2), peek(-1)]
else:
tokens = [peek(-1)]
self_param = None
func_kind = getattr(tokens[0], "func_kind", None)
if local:
scope = Scope(scope, depth, funcdepth)
target, name = None, None
funcscope = Scope(scope, depth + 1, funcdepth + 1)
params = []
if stmt:
if local:
target = parse_var(new=scope)
scope.add(target.var)
name = target.name
else:
target = parse_var()
name = target.name
while accept("."):
token = peek(-1)
key = parse_var(member=True)
target = Node(NodeType.member, [target, token, key], key=key, child=target, method=False)
name += "." + key.name
if accept(":"):
token = peek(-1)
key = parse_var(member=True)
target = Node(NodeType.member, [target, token, key], key=key, child=target, method=True)
name += ":" + key.name
self_token = Token.synthetic(TokenType.ident, "self", target, append=True)
self_param = parse_var(token=self_token, new=funcscope, implicit=True)
params.append(self_param)
mark_reassign(target)
tokens.append(target)
require("(", tokens)
if not accept(")", tokens):
while True:
if accept("..."):
params.append(Node(NodeType.varargs, [peek(-1)]))
else:
params.append(parse_var(new=funcscope))
tokens.append(params[-1])
if accept(")", tokens):
break
require(",", tokens)
for param in params:
if param.type == NodeType.var:
funcscope.add(param.var)
old_unresolved = unresolved_labels
unresolved_labels = None
funcdepth += 1
scope = funcscope
body = parse_block()
tokens.append(body)
require("end", tokens)
scope = scope.parent
funcdepth -= 1
late_resolve_labels()
unresolved_labels = old_unresolved
funcnode = Node(NodeType.function, tokens, target=target, params=params, body=body, name=name, kind=func_kind)
if self_param:
funcnode.add_extra_child(self_param)
return funcnode
def parse_table():
tokens = [peek(-1)]
keys_kind = getattr(tokens[0], "keys_kind", None)
items = []
while not accept("}", tokens):
if accept("["):
open = peek(-1)
key = parse_expr()
close = require("]")
eq = require("=")
value = parse_expr()
items.append(Node(NodeType.table_index, [open, key, close, eq, value], key=key, value=value))
elif peek(1).value == "=":
key = parse_var(member=True)
eq = require("=")
value = parse_expr()
items.append(Node(NodeType.table_member, [key, eq, value], key=key, value=value))
else:
items.append(parse_expr())
tokens.append(items[-1])
if accept("}", tokens):
break
if not accept(";", tokens):
require(",", tokens)
return Node(NodeType.table, tokens, items=items, keys_kind=keys_kind)
def parse_call(expr, extra_arg=None):
tokens = [expr, peek(-1)]
args = []
if extra_arg:
args.append(extra_arg) # not a direct token
if not accept(")", tokens):
while True:
args.append(parse_expr())
tokens.append(args[-1])
if accept(")", tokens):
break
require(",", tokens)
return Node(NodeType.call, tokens, func=expr, args=args)
def parse_const(token):
node = Node(NodeType.const, [token], token=token)
if getattr(token, "var_kind", None):
node.extra_names = k_identifier_split_re.split(token.parsed_value)
for i, value in enumerate(node.extra_names):
if is_identifier(value, lang):
subtoken = Token.synthetic(TokenType.ident, value, token)
subtoken.var_kind = token.var_kind
node.add_extra_child(parse_var(token=subtoken, member=True))
else:
subtoken = Token.synthetic(TokenType.string, value, token)
node.add_extra_child(parse_const(subtoken))
if hasattr(token, "sublang"):
sublang_token = Token.synthetic(TokenType.string, "", token)
node.add_extra_child(Node(NodeType.sublang, (sublang_token,), name=token.sublang_name, sublang=token.sublang))
return node
def parse_core_expr():
token = take()
value = token.value
if value == None:
add_error("unexpected end of input", fail=True)
elif value in ("nil", "true", "false") or token.type in (TokenType.number, TokenType.string):
return parse_const(token)
elif value == "{":
return parse_table()
elif value == "(":
expr = parse_expr()
close = require(")")
return Node(NodeType.group, [token, expr, close], child=expr)
elif value in k_unary_ops and k_unary_op_langs.get(value, lang) == lang:
expr = parse_expr(k_unary_ops_prec)
return Node(NodeType.unary_op, [token, expr], child=expr, op=value)
elif value == "?":
return parse_print()
elif value == "function":
return parse_function()
elif value == "...":
return Node(NodeType.varargs, [token])
elif token.type == TokenType.ident:
return parse_var(token=token)
else:
add_error("unknown expression", fail=True)
def compare_prec(op, prec):
return (prec == None) or ((prec <= k_binary_op_precs[op]) if op in k_right_binary_ops else (prec < k_binary_op_precs[op]))
def parse_expr(prec=None):
expr = parse_core_expr()
while True:
nonlocal idx
token = take()
value = token.value
if value == "." and expr.type in k_prefix_types:
var = parse_var(member=True)
expr = Node(NodeType.member, [expr, token, var], key=var, child=expr, method=False)
elif value == "[" and expr.type in k_prefix_types:
index = parse_expr()
close = require("]")
expr = Node(NodeType.index, [expr, token, index, close], key=index, child=expr)
elif value == "(" and expr.type in k_prefix_types:
expr = parse_call(expr)
elif (value == "{" or peek(-1).type == TokenType.string) and expr.type in k_prefix_types:
idx -= 1
arg = parse_core_expr()
expr = Node(NodeType.call, [expr, arg], func=expr, args=[arg])
elif value == ":" and expr.type in k_prefix_types:
var = parse_var(member=True)
expr = Node(NodeType.member, [expr, token, var], key=var, child=expr, method=True)
if peek().value == "{" or peek().type == TokenType.string:
arg = parse_core_expr()
expr = Node(NodeType.call, [expr, arg], func=expr, args=[var, arg])
else:
require("(")
expr = parse_call(expr, extra_arg=var)
elif value in k_binary_op_precs and k_binary_op_langs.get(value, lang) == lang and compare_prec(value, prec):
other = parse_expr(k_binary_op_precs[value])
expr = Node(NodeType.binary_op, [expr, token, other], left=expr, right=other, op=value)
else:
idx -= 1
return expr
def parse_list(tokens, func):
list = [func()]
tokens.append(list[-1])
while accept(",", tokens):
list.append(func())
tokens.append(list[-1])
return list
def short_state(vline):
return k_nested if peek().vline == vline else True
def parse_if(type=NodeType.if_):
tokens = [peek(-1)]
cond = parse_expr()
tokens.append(cond)
else_ = None
short = False
if accept("then", tokens) or (is_pico8 and accept("do", tokens)):
then = parse_block()
tokens.append(then)
if accept("else"):
else_tokens = [peek(-1)]
else_body = parse_block()
else_tokens.append(else_body)
require("end", else_tokens)
else_ = Node(NodeType.else_, else_tokens, body=else_body, short=False)
tokens.append(else_)
elif accept("elseif"):
else_ = parse_if(NodeType.elseif)
tokens.append(else_)
else:
require("end", tokens)
elif peek(-1).value == ")":
vline = peek(-1).vline
then = parse_block(vline=vline)
tokens.append(then)
if peek().vline == vline and accept("else"):
else_tokens = [peek(-1)]
else_body = parse_block(vline=vline)
else_tokens.append(else_body)
else_ = Node(NodeType.else_, else_tokens, body=else_body, short=short_state(vline))
tokens.append(else_)
short = short_state(vline)
else:
add_error("then or shorthand required", fail=True)
return Node(type, tokens, cond=cond, then=then, else_=else_, short=short)
def parse_while():
tokens = [peek(-1)]
cond = parse_expr()
tokens.append(cond)
short = False
if accept("do", tokens):
body = parse_block()
tokens.append(body)
require("end", tokens)
elif peek(-1).value == ")":
vline = peek(-1).vline
body = parse_block(vline=vline)
tokens.append(body)
short = short_state(vline)
else:
add_error("do or shorthand required", fail=True)
return Node(NodeType.while_, tokens, cond=cond, body=body, short=short)
def parse_repeat():
tokens = [peek(-1)]
body, until = parse_block(with_until=True)
tokens.append(body)
tokens.append(until)
repeat = Node(NodeType.repeat, tokens, body=body, until=until)
return repeat
def parse_until():
tokens = []
require("until", tokens)
cond = parse_expr()
tokens.append(cond)
return Node(NodeType.until, tokens, cond=cond)
def parse_for():
nonlocal scope
tokens = [peek(-1)]
if peek(1).value == "=":
newscope = Scope(scope, depth + 1, funcdepth)
target = parse_var(new=newscope)
tokens.append(target)
require("=", tokens)
min = parse_expr()
tokens.append(min)
require(",", tokens)
max = parse_expr()
tokens.append(max)
step = None
if accept(",", tokens):
step = parse_expr()
tokens.append(step)
require("do", tokens)
newscope.add(target.var)
scope = newscope
body = parse_block()
tokens.append(body)
require("end", tokens)
scope = scope.parent
return Node(NodeType.for_, tokens, target=target, min=min, max=max, step=step, body=body)
else:
newscope = Scope(scope, depth + 1, funcdepth)
targets = parse_list(tokens, lambda: parse_var(new=newscope))
require("in", tokens)
sources = parse_list(tokens, parse_expr)
require("do", tokens)
for target in targets:
newscope.add(target.var)
scope = newscope
body = parse_block()
tokens.append(body)
require("end", tokens)
scope = scope.parent
return Node(NodeType.for_in, tokens, targets=targets, sources=sources, body=body)
def parse_return(vline):
tokens = [peek(-1)]
if peek().value in k_block_ends + (";",) or (e(vline) and peek().vline > vline):
return Node(NodeType.return_, tokens, items=[])
else:
rets = parse_list(tokens, parse_expr)
return Node(NodeType.return_, tokens, items=rets)
def parse_print():
tokens = [peek(-1)]
args = parse_list(tokens, parse_expr)
# explicitly represent the implicit use of print
print_token = Token.synthetic(TokenType.ident, "print", tokens[0])
func = parse_var(token=print_token, implicit=True)
node = Node(NodeType.call, tokens, func=func, args=args, short=True)
node.add_extra_child(func)
return node
def parse_local():
nonlocal scope
tokens = [peek(-1)]
newscope = Scope(scope, depth, funcdepth)
const = getattr(tokens[0], "is_const", None)
targets = parse_list(tokens, lambda: parse_var(new=newscope, const=const, with_attr=True))
sources = []
if accept("=", tokens):
sources = parse_list(tokens, parse_expr)
for target in targets:
newscope.add(target.var)
scope = newscope
return Node(NodeType.local, tokens, targets=targets, sources=sources)
def mark_reassign(target):
target.assignment = True
if target.type == NodeType.var and target.var:
target.var.reassigned += 1
def parse_assign(first):
tokens = [first]
if accept(",", tokens):
targets = [first] + parse_list(tokens, parse_expr)
else:
targets = [first]
require("=", tokens)
sources = parse_list(tokens, parse_expr)
for target in targets:
mark_reassign(target)
return Node(NodeType.assign, tokens, targets=targets, sources=sources)
def parse_opassign(first):
nonlocal idx
token = peek()
op = token.value[:-1]
idx += 1
source = parse_expr()
mark_reassign(first)
return Node(NodeType.op_assign, [first, token, source], target=first, source=source, op=op)
def parse_misc_stmt():
nonlocal idx
idx -= 1
first = parse_expr()
if peek().value in (",", "="):
return parse_assign(first)
elif peek().value and peek().value.endswith("="):
return parse_opassign(first)
elif first.type == NodeType.call:
return first
else:
add_error("expression has no side-effect")
def parse_stmt(vline):
token = take()
value = token.value
if value == ";":
return None
elif value == "do":
body = parse_block()
end = require("end")
return Node(NodeType.do, [token, body, end], body=body)
elif value == "if":
return parse_if()
elif value == "while":
return parse_while()
elif value == "repeat":
return parse_repeat()
elif value == "for":
return parse_for()
elif value == "break":
return Node(NodeType.break_, [token])
elif value == "return":
return parse_return(vline)
elif value == "local":
if accept("function"):
return parse_function(stmt=True, local=True)
else:
return parse_local()
elif value == "goto":
label = parse_var(label=True)
return Node(NodeType.goto, [token, label], label=label)
elif value == "::":
label = parse_var(label=True, new=True)
labelscope.add(label.var)
end = require("::")
return Node(NodeType.label, [token, label, end], label=label)
elif value == "function":
return parse_function(stmt=True)
else:
return parse_misc_stmt()
def parse_block(vline=None, with_until=False):
nonlocal scope, labelscope, depth
oldscope = scope
labelscope = LabelScope(labelscope, funcdepth)
depth += 1
stmts = []
tokens = []
while e(peek().type):
if e(vline) and peek().vline > vline:
break
if peek().value in k_block_ends:
break
stmt = parse_stmt(vline)
if stmt:
stmts.append(stmt)
tokens.append(stmt)
else:
tokens.append(peek(-1))
if with_until:
until = parse_until()
depth -= 1
labelscope = labelscope.parent
while scope != oldscope:
scope = scope.parent
node = Node(NodeType.block, tokens, stmts=stmts)
if with_until:
return node, until
else:
return node
def late_resolve_labels():
if unresolved_labels:
for node in unresolved_labels:
node.var = node.scope.find(node.name)
if not node.var:
add_error_at(f"Unknown label {node.name}", node.children[0])
def parse_root():
nonlocal funcdepth
funcdepth += 1
root = parse_block()
funcdepth -= 1
late_resolve_labels()
if peek().type != None:
add_error("Expected end of input")
if idx < len(tokens):
root.children.append(take()) # extra comments/etc
assert scope.parent is None
#verify_parse(root) # DEBUG
return root
def verify_parse(root):
def visitor(token):
nonlocal idx
assert token == tokens[idx]
idx += 1
nonlocal idx
idx = 0
root.traverse_tokens(visitor)
assert idx == len(tokens)
try:
if for_expr:
root = parse_expr()
else:
root = parse_root()
except ParseError:
root = None
if root and super_root:
root.parent = super_root
super_root.children.append(root)
super_root.roots[source.fspath] = root
root = super_root
if root:
root.globals, root.members, root.has_env = globals, members, has_env
return root, errors
def create_super_root():
return Node(NodeType.super_root, [], roots={})
def get_sub_root(root, source):
if root.type != NodeType.super_root:
return root
return root.roots[source.fspath]
# node utils
def is_assign_target(node):
return node.parent.type == NodeType.assign and node in node.parent.targets
def is_op_assign_target(node):