-
Notifications
You must be signed in to change notification settings - Fork 38
/
builder.py
1572 lines (1415 loc) · 49.2 KB
/
builder.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
import ast
import re
from antlr4 import InputStream, CommonTokenStream
from luaparser.astnodes import *
from luaparser.parser.LuaLexer import LuaLexer
from typing import List, Tuple, Literal
from antlr4.Token import Token
class SyntaxException(Exception):
def __init__(self, user_msg, token=None):
if token:
message = (
"(" + str(token.line) + "," + str(token.start) + "): Error: " + user_msg
)
else:
message = "Error: " + user_msg
super().__init__(message)
class Expr(Enum):
OR = 1
AND = 2
REL = 3
CONCAT = 4
ADD = 5
MULT = 6
BITWISE = 7
UNARY = 8
POW = 9
ATOM = 10
class Tokens:
AND = 1
BREAK = 2
DO = 3
ELSETOK = 4
ELSEIF = 5
END = 6
FALSE = 7
FOR = 8
FUNCTION = 9
GOTO = 10
IFTOK = 11
IN = 12
LOCAL = 13
NIL = 14
NOT = 15
OR = 16
REPEAT = 17
RETURN = 18
THEN = 19
TRUE = 20
UNTIL = 21
WHILE = 22
ADD = 23
MINUS = 24
MULT = 25
DIV = 26
FLOOR = 27
MOD = 28
POW = 29
LENGTH = 30
EQ = 31
NEQ = 32
LTEQ = 33
GTEQ = 34
LT = 35
GT = 36
ASSIGN = 37
BITAND = 38
BITOR = 39
BITNOT = 40
BITRSHIFT = 41
BITRLEFT = 42
OPAR = 43
CPAR = 44
OBRACE = 45
CBRACE = 46
OBRACK = 47
CBRACK = 48
COLCOL = 49
COL = 50
COMMA = 51
VARARGS = 52
CONCAT = 53
DOT = 54
SEMCOL = 55
NAME = 56
NUMBER = 57
STRING = 58
COMMENT = 59
LINE_COMMENT = 60
SPACE = 61
NEWLINE = 62
SHEBANG = 63
LongBracket = 64
LITERAL_NAMES = [
"<INVALID>",
"'and'",
"'break'",
"'do'",
"'else'",
"'elseif'",
"'end'",
"'false'",
"'for'",
"'function'",
"'goto'",
"'if'",
"'in'",
"'local'",
"'nil'",
"'not'",
"'or'",
"'repeat'",
"'return'",
"'then'",
"'true'",
"'until'",
"'while'",
"'+'",
"'-'",
"'*'",
"'/'",
"'//'",
"'%'",
"'^'",
"'#'",
"'=='",
"'~='",
"'<='",
"'>='",
"'<'",
"'>'",
"'='",
"'&'",
"'|'",
"'~'",
"'>>'",
"'<<'",
"'('",
"')'",
"'{'",
"'}'",
"'['",
"']'",
"'::'",
"':'",
"','",
"'...'",
"'..'",
"'.'",
"';'",
"NAME",
"NUMBER",
"STRING",
"COMMENT",
"LINE_COMMENT",
"SPACE",
"NEWLINE",
"SHEBANG",
"LONG_BRACKET",
]
def _listify(obj):
if not isinstance(obj, list):
return [obj]
else:
return obj
class Builder:
CLOSING_TOKEN = [Tokens.END, Tokens.CBRACE, Tokens.CPAR]
HIDDEN_TOKEN = [
Tokens.SHEBANG,
Tokens.LINE_COMMENT,
Tokens.COMMENT,
Tokens.NEWLINE,
Tokens.SPACE,
-2,
]
REL_OPERATORS = [
Tokens.LT,
Tokens.GT,
Tokens.LTEQ,
Tokens.GTEQ,
Tokens.NEQ,
Tokens.EQ,
]
def __init__(self, source):
self._stream = CommonTokenStream(LuaLexer(InputStream(source)))
# contains a list of CommonTokens
self._line_count: int = 0
self._right_index: int = 0
self._last_expr_type: Optional[int] = None
# following stack are used to backup values
self._index_stack: List[int] = []
self._right_index_stack: List[int] = []
self.text: str = "" # last token text
self.type: int = -1 # last token type
# contains expected token in case of invalid input code
self._expected = []
# comments waiting to be inserted into ast nodes
self._comments_index_stack: List[int] = []
self.comments: List[Comment] = []
self._hidden_handled: bool = False
self._hidden_handled_stack: List[bool] = []
@property
def _LT(self) -> CommonToken:
"""Last token that was consumed in next_i*_* method."""
return self._stream.LT(-1)
def process(self) -> Chunk:
node = self.parse_chunk()
if not node:
raise SyntaxException("Expecting a chunk")
return node
def save(self):
# logging.debug('trying ' + inspect.stack()[1][3])
self._index_stack.append(self._stream.index)
self._right_index_stack.append(self._right_index)
self._comments_index_stack.append(len(self.comments))
self._hidden_handled_stack.append(self._hidden_handled)
def success(self):
self._index_stack.pop()
self._right_index_stack.pop()
self._comments_index_stack.pop()
self._hidden_handled_stack.pop()
return True
def failure(self):
self._stream.seek(self._index_stack.pop())
self._right_index = self._right_index_stack.pop()
self._hidden_handled = self._hidden_handled_stack.pop()
n_elem_to_delete = len(self.comments) - self._comments_index_stack.pop()
if n_elem_to_delete >= 1:
del self.comments[-n_elem_to_delete:]
return False
def failure_save(self):
self._stream.seek(self._index_stack.pop())
self._right_index = self._right_index_stack.pop()
self._hidden_handled = self._hidden_handled_stack.pop()
n_elem_to_delete = len(self.comments) - self._comments_index_stack.pop()
if n_elem_to_delete >= 1:
del self.comments[-n_elem_to_delete:]
self._index_stack.append(self._stream.index)
self._right_index_stack.append(self._right_index)
self._comments_index_stack.append(len(self.comments))
self._hidden_handled_stack.append(self._hidden_handled)
def next_is_rc(
self, type_to_seek: int, hidden_right: bool = True
) -> Optional[Token]:
token = self._stream.LT(1)
tok_type: int = token.type
self._right_index = self._stream.index
if tok_type == type_to_seek:
self.text = token.text
self.type = tok_type
self._stream.consume()
self._hidden_handled = False
if hidden_right:
self.handle_hidden_right()
return token
self._expected.append(type_to_seek)
return None
def next_is_c(self, type_to_seek: int, hidden_right: bool = True) -> bool:
token = self._stream.LT(1)
tok_type: int = token.type
self._right_index = self._stream.index
if tok_type == type_to_seek:
self._stream.consume()
self._hidden_handled = False
if hidden_right:
self.handle_hidden_right()
return True
self._expected.append(type_to_seek)
return False
def next_is(self, type_to_seek) -> bool:
if self._stream.LT(1).type == type_to_seek:
return True
else:
self._expected.append(type_to_seek)
return False
def prev_is(self, type_to_seek) -> bool:
return self._stream.LT(-1).type == type_to_seek
def next_in_rc(self, types: List[int], hidden_right: bool = True) -> bool:
token = self._stream.LT(1)
tok_type: int = token.type
self._right_index = self._stream.index
if tok_type in types:
self.type = tok_type
self._stream.consume()
self._hidden_handled = False
if hidden_right:
self.handle_hidden_right()
return True
self._expected.extend(types)
return False
def next_in(self, types: List[int]) -> bool:
if self._stream.LT(1).type in types:
return True
else:
self._expected.extend(types)
return False
def handle_hidden_left(self) -> None:
if self._hidden_handled:
return
tokens = self._stream.getHiddenTokensToLeft(self._stream.index)
if tokens:
for t in tokens:
if t.type == Tokens.LINE_COMMENT:
self.comments.append(
Comment(
t.text,
first_token=t,
last_token=t,
)
)
elif t.type == Tokens.COMMENT:
self.comments.append(
Comment(
t.text,
True,
first_token=t,
last_token=t,
)
)
elif t.type == Tokens.NEWLINE:
# append n time a None value (indicate newline)
self.comments += t.text.count("\n") * [None]
self._hidden_handled = True
def handle_hidden_right(self) -> None:
if self._hidden_handled:
return
tokens = self._stream.getHiddenTokensToRight(self._right_index)
if tokens:
for t in tokens:
if t.type == Tokens.LINE_COMMENT:
self.comments.append(
Comment(
t.text,
first_token=t,
last_token=t,
)
)
elif t.type == Tokens.COMMENT:
self.comments.append(
Comment(
t.text,
True,
first_token=t,
last_token=t,
)
)
elif t.type == Tokens.NEWLINE:
# append n time a None value (indicate newline)
self.comments += t.text.count("\n") * [None]
self._hidden_handled = True
def get_comments(self) -> Comments:
comments = [c for c in self.comments if c is not None]
self.comments = []
return comments
def get_comments_followed_by_blank_line(self) -> Comments:
"""Returns comments followed by a blank line."""
if not self.comments:
return []
idx = 0
comments: List[Comment] = []
# search first comment
while idx < len(self.comments) and self.comments[idx] is None:
idx += 1
# get comments starting from this index
while idx < len(self.comments) and self.comments[idx] is not None:
comments.append(self.comments[idx])
idx += 1
# check if followed by a blank line
if idx + 1 < len(self.comments):
if self.comments[idx] is None and self.comments[idx + 1] is None:
# clean list
self.comments = self.comments[idx + 2:]
return comments
return []
def get_inline_comment(self) -> Comment or None:
if self.comments:
c = self.comments.pop(0)
if c is None:
return None
else:
return c
return None
def has_newline_before(self) -> bool:
return None in self.comments
def abort(self) -> None:
types_str = []
token = self._stream.LT(2)
expected = set(self._expected)
for type_to_seek in expected:
types_str.append(LITERAL_NAMES[type_to_seek])
raise SyntaxException(
"Expecting one of "
+ ", ".join(types_str)
+ " at line "
+ str(token.line)
+ ", column "
+ str(token.column)
)
def parse_chunk(self) -> Chunk or None:
first_token: Token = self._stream.LT(1)
self.handle_hidden_left()
comments = self.get_comments_followed_by_blank_line()
block = self.parse_block()
if block:
token = self._stream.LT(1)
if token.type == -1:
# do not consume EOF
return Chunk(
block,
comments=comments,
first_token=first_token,
last_token=self._LT,
)
return False
def parse_block(self) -> Block:
t: Token = self._stream.LT(1)
statements = []
while True:
stat = self.parse_stat()
if not stat:
break
statements.append(stat)
# optional ret stat
stat = self.parse_ret_stat()
if stat:
statements.append(stat)
# force handle trailing hidden tokens after block
self._hidden_handled = False
self.handle_hidden_right()
return Block(
statements,
first_token=t,
last_token=statements[-1].last_token if statements else None,
comments=self.get_comments(),
)
def parse_stat(self) -> Statement or None:
comments = self.get_comments()
stat = (
self.parse_assignment()
or self.parse_var(is_statement=True)
or self.parse_while_stat()
or self.parse_repeat_stat()
or self.parse_local()
or self.parse_goto_stat()
or self.parse_if_stat()
or self.parse_for_stat()
or self.parse_function()
or self.parse_label()
)
if stat:
stat.comments = comments
return stat
stat = self.parse_do_block()
if stat:
self.handle_hidden_right()
return Do(stat)
if self.next_is(Tokens.BREAK) and self.next_is_rc(Tokens.BREAK):
self.handle_hidden_right()
return Break()
if self.next_is(Tokens.SEMCOL) and self.next_is_rc(Tokens.SEMCOL):
self.handle_hidden_right()
return SemiColon()
return None
def parse_ret_stat(self) -> Return or bool:
self.save()
if self.next_is_rc(Tokens.RETURN):
t: Token = self._LT
expr_list = self.parse_expr_list() # optional
# consume optional token
if self.next_is(Tokens.SEMCOL):
self.next_is_rc(Tokens.SEMCOL)
self.success()
return Return(expr_list, first_token=t, last_token=self._LT)
return self.failure()
def parse_assignment(self) -> Assign or bool:
self.save()
t: Token = self._stream.LT(1)
targets = self.parse_var_list()
if targets:
if self.next_is_rc(Tokens.ASSIGN):
values = self.parse_expr_list()
if values:
self.success()
return Assign(
targets,
values,
first_token=t,
last_token=self._LT,
)
else:
self.abort()
return self.failure()
def parse_var_list(self) -> List[Expression] or bool:
lua_vars = []
self.save()
var = self.parse_var()
if var:
lua_vars.append(var)
while True:
self.save()
if self.next_is_rc(Tokens.COMMA):
var = self.parse_var()
if var:
lua_vars.append(var)
self.success()
else:
self.failure()
return self.failure()
else:
self.failure()
break
self.success()
return lua_vars
return self.failure()
# When is_statement is true, root must be a Statement.
def parse_var(self, is_statement=False) -> Node or bool:
self.save()
root = self.parse_callee()
if root:
tail = self.parse_tail()
while tail:
tail.first_token = root.first_token
if isinstance(tail, Call):
tail.func = root
elif isinstance(tail, Index):
tail.value = root
elif isinstance(tail, Invoke):
tail.source = root
else:
args = _listify(tail)
tail = Call(
root,
args,
first_token=root.first_token,
last_token=args[-1].last_token if args else None,
)
root = tail
tail = self.parse_tail()
if tail:
self.handle_hidden_right()
if is_statement and not isinstance(root, Statement):
return self.failure()
self.handle_hidden_right()
self.success()
return root
return self.failure()
def parse_tail(self) -> Node or bool:
# do not render last hidden
self.save()
if self.next_is_rc(Tokens.DOT) and self.next_is_rc(Tokens.NAME, False):
self.success()
return Index(
Name(
self.text,
first_token=self._LT,
last_token=self._LT,
),
# value must be set in parent
Name(""),
last_token=self._LT,
)
self.failure_save()
if self.next_is_rc(Tokens.OBRACK):
expr = self.parse_expr()
if expr and self.next_is_rc(Tokens.CBRACK, False):
self.success()
return Index(
expr,
Name(""),
notation=IndexNotation.SQUARE,
# value must be set in parent
)
self.failure_save()
if self.next_is_rc(Tokens.COL) and self.next_is_rc(Tokens.NAME):
name = Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
if self.next_is_rc(Tokens.OPAR):
expr_list = self.parse_expr_list() or []
if self.next_is_rc(Tokens.CPAR, False):
self.success()
# noinspection PyTypeChecker
return Invoke(None, name, expr_list)
self.failure_save()
if self.next_is_rc(Tokens.COL) and self.next_is_rc(Tokens.NAME):
name = Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
table = self.parse_table_constructor(False)
if table:
self.success()
# noinspection PyTypeChecker
return Invoke(None, name, [table])
self.failure_save()
if self.next_is_rc(Tokens.COL) and self.next_is_rc(Tokens.NAME):
name = Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
if self.next_is_rc(Tokens.STRING, False):
string = self.parse_lua_str(self.text, self._LT)
self.success()
# noinspection PyTypeChecker
return Invoke(None, name, [string])
self.failure_save()
if self.next_is(Tokens.OPAR):
# handle the ambiguous syntax
# http://lua-users.org/lists/lua-l/2009-08/msg00543.html
# example:
# a = b + c;
# (print or io.write)('foo')
# check if a newline is present before OPAR
tokens = self._stream.getHiddenTokensToLeft(self._stream.index)
if tokens:
for t in tokens:
if t.type == Tokens.NEWLINE and not self.prev_is(Tokens.SEMCOL):
raise SyntaxException(
"Ambiguous syntax detected", self._stream.LT(-1)
)
if self.next_is_rc(Tokens.OPAR, False):
self.handle_hidden_right()
expr_list = self.parse_expr_list() or []
if self.next_is_rc(Tokens.CPAR, False):
self.success()
# noinspection PyTypeChecker
return Call(None, expr_list, last_token=self._LT)
self.failure_save()
table = self.parse_table_constructor(False)
if table:
self.success()
return table
self.failure_save()
if self.next_is_rc(Tokens.STRING, False):
string = self.parse_lua_str(self.text, self._LT)
self.success()
return string
return self.failure()
def parse_expr_list(self) -> List[Expression] or bool:
expr_list: List[Expression] = []
self.save()
expr = self.parse_expr()
if expr:
expr_list.append(expr)
while True:
self.save()
if self.next_is_rc(Tokens.COMMA):
self._expected = []
expr = self.parse_expr()
if expr:
expr_list.append(expr)
self.success()
else:
# a comma is alone at the end
self.failure()
self.failure()
self.abort()
else:
self.failure()
break
self.success()
return expr_list
return self.failure()
def parse_do_block(self) -> Block or bool:
self.save()
if self.next_is_rc(Tokens.DO, False):
self.handle_hidden_right()
block = self.parse_block()
if block:
if self.next_is_rc(Tokens.END):
self.success()
return block
return self.failure()
def parse_while_stat(self) -> While or bool:
self.save()
if self.next_is_rc(Tokens.WHILE):
self._expected = []
expr = self.parse_expr()
if expr:
self._expected = []
body = self.parse_do_block()
if body:
self.success()
return While(expr, body)
self.abort()
return self.failure()
def parse_repeat_stat(self) -> Repeat or bool:
self.save()
if self.next_is_rc(Tokens.REPEAT, False):
self.handle_hidden_right()
body = self.parse_block()
if body:
if self.next_is_rc(Tokens.UNTIL):
expr = self.parse_expr()
if expr:
self.success()
return Repeat(body, expr)
return self.failure()
def parse_local(self) -> Node or bool:
self.save()
self._expected = []
start_token = self.next_is_rc(Tokens.LOCAL)
if start_token:
targets = self.parse_name_list()
if targets:
values = []
self.save()
if self.next_is_rc(Tokens.ASSIGN):
values = self.parse_expr_list()
if values:
self.success()
else:
self.failure()
self.failure()
self.abort()
else:
self.failure()
self.success()
return LocalAssign(
targets,
values,
first_token=start_token,
last_token=values[-1].last_token if values else None,
)
self.save()
if self.next_is_rc(Tokens.FUNCTION) and self.next_is_rc(Tokens.NAME):
name = Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
body = self.parse_func_body()
if body:
self.success()
self.success()
node = LocalFunction(name, body[0], body[1])
self.handle_hidden_right()
node.first_token = start_token
node.last_token = body[1].last_token
return node
self.failure()
self.abort()
return self.failure()
def parse_goto_stat(self) -> Goto or bool:
self.save()
if self.next_is_rc(Tokens.GOTO) and self.next_is_rc(Tokens.NAME):
self.success()
return Goto(
Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
)
return self.failure()
def parse_if_stat(self) -> If or bool:
self.save()
if self.next_is_rc(Tokens.IFTOK):
self._expected = []
test = self.parse_expr()
if test:
if self.next_is_rc(Tokens.THEN, False):
self.handle_hidden_right()
body = self.parse_block()
if body:
main = If(test, body, None)
root = main
while True: # zero or more
orelse = self.parse_elseif_stat()
if not orelse:
break
else:
root.orelse = orelse
root = orelse
else_exp = self.parse_else_stat() # optional
if else_exp:
root.orelse = else_exp
if self.next_is_rc(Tokens.END):
self.success()
return main
self.abort()
return self.failure()
def parse_elseif_stat(self) -> ElseIf or bool:
self.save()
if self.next_is_rc(Tokens.ELSEIF):
test = self.parse_expr()
if test:
if self.next_is_rc(Tokens.THEN, False):
self.handle_hidden_right()
body = self.parse_block()
if body:
self.success()
return ElseIf(test, body, None) # orelse will be set in parent
return self.failure()
def parse_else_stat(self) -> Block or bool:
self.save()
if self.next_is(Tokens.ELSETOK):
if self.next_is_rc(Tokens.ELSETOK, False):
self.handle_hidden_right()
body = self.parse_block()
if body:
self.success()
return body
return self.failure()
def parse_for_stat(self) -> Fornum or Forin or bool:
self.save()
if self.next_is_rc(Tokens.FOR):
self.save()
if self.next_is_rc(Tokens.NAME):
target = Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
if self.next_is_rc(Tokens.ASSIGN):
start = self.parse_expr()
if start and self.next_is_rc(Tokens.COMMA):
stop = self.parse_expr()
if stop:
step = 1
# optional step
if self.next_is(Tokens.COMMA) and self.next_is_rc(
Tokens.COMMA
):
step = self.parse_expr()
body = self.parse_do_block()
if not body:
self.failure()
return self.failure()
self.success()
self.success()
return Fornum(target, start, stop, step, body)
self.failure_save()
target = self.parse_name_list()
if target and self.next_is_rc(Tokens.IN):
iter_expr = self.parse_expr_list()
if iter_expr:
body = self.parse_do_block()
if body:
self.success()
self.success()
return Forin(body, iter_expr, target)
self.failure()
return self.failure()
def parse_function(self) -> Method or Function or bool:
self.save()
self._expected = []
start_token = self.next_is_rc(Tokens.FUNCTION)
if start_token:
names = self.parse_names()
if names:
self.save()
if self.next_is_rc(Tokens.COL) and self.next_is_rc(Tokens.NAME):
name = Name(
self.text,
first_token=self._LT,
last_token=self._LT,
)
func_body = self.parse_func_body()
if func_body:
self.success()
self.success()
node = Method(
names,
name,
func_body[0],
func_body[1],
first_token=start_token,
last_token=func_body[1].last_token,
)
self.handle_hidden_right()
return node
self.failure()
func_body = self.parse_func_body()
if func_body:
self.success()
node = Function(
names,
func_body[0],
func_body[1],
first_token=start_token,
last_token=func_body[1].last_token,
)
self.handle_hidden_right()
return node
self.abort()
return self.failure()
def parse_names(self) -> Name or Index or bool:
self.save()
if self.next_is_rc(Tokens.NAME):
root = Name(
self.text,