-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathast.py
More file actions
974 lines (644 loc) · 19.9 KB
/
ast.py
File metadata and controls
974 lines (644 loc) · 19.9 KB
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
'''
Abstract syntax tree classes.
This is designed to replace the stdlib ast module.
Unlike the stdlib module, it is version independent.
The classes in this file are based on the corresponding types in the cpython interpreter, copyright PSF.
'''
class AstBase(object):
__slots__ = "lineno", "col_offset", "_end",
def __repr__(self):
args = ",".join(repr(getattr(self, field, None)) for field in self.__slots__)
return "%s(%s)" % (self.__class__.__name__, args)
class Class(AstBase):
'AST node representing a class definition'
__slots__ = "name", "body",
def __init__(self, name, body):
self.name = name
self.body = body
class Function(AstBase):
'AST node representing a function definition'
__slots__ = "is_async", "name", "type_parameters", "args", "vararg", "kwonlyargs", "kwarg", "body",
def __init__(self, name, type_parameters, args, vararg, kwonlyargs, kwarg, body, is_async=False):
self.name = name
self.type_parameters = type_parameters
self.args = args
self.vararg = vararg
self.kwonlyargs = kwonlyargs
self.kwarg = kwarg
self.body = body
self.is_async = is_async
class Module(AstBase):
def __init__(self, body):
self.body = body
class StringPart(AstBase):
'''Implicitly concatenated part of string literal'''
__slots__ = "prefix", "text", "s",
def __init__(self, prefix, text, s):
self.prefix = prefix
self.text = text
self.s = s
class TemplateStringPart(AstBase):
'''A string constituent of a template string literal'''
__slots__ = "text", "s",
def __init__(self, text, s):
self.text = text
self.s = s
class alias(AstBase):
__slots__ = "value", "asname",
def __init__(self, value, asname):
self.value = value
self.asname = asname
class arguments(AstBase):
__slots__ = "defaults", "kw_defaults", "annotations", "varargannotation", "kwargannotation", "kw_annotations",
def __init__(self, defaults, kw_defaults, annotations, varargannotation, kwargannotation, kw_annotations):
if len(defaults) != len(annotations):
raise AssertionError('len(defaults) != len(annotations)')
if len(kw_defaults) != len(kw_annotations):
raise AssertionError('len(kw_defaults) != len(kw_annotations)')
self.kw_defaults = kw_defaults
self.defaults = defaults
self.annotations = annotations
self.varargannotation = varargannotation
self.kwargannotation = kwargannotation
self.kw_annotations = kw_annotations
class boolop(AstBase):
pass
class cmpop(AstBase):
pass
class comprehension(AstBase):
__slots__ = "is_async", "target", "iter", "ifs",
def __init__(self, target, iter, ifs, is_async=False):
self.target = target
self.iter = iter
self.ifs = ifs
self.is_async = is_async
class dict_item(AstBase):
pass
class type_parameter(AstBase):
pass
class expr(AstBase):
__slots__ = "parenthesised",
class expr_context(AstBase):
pass
class operator(AstBase):
pass
class stmt(AstBase):
pass
class unaryop(AstBase):
pass
class pattern(AstBase):
__slots__ = "parenthesised",
class And(boolop):
pass
class Or(boolop):
pass
class Eq(cmpop):
pass
class Gt(cmpop):
pass
class GtE(cmpop):
pass
class In(cmpop):
pass
class Is(cmpop):
pass
class IsNot(cmpop):
pass
class Lt(cmpop):
pass
class LtE(cmpop):
pass
class NotEq(cmpop):
pass
class NotIn(cmpop):
pass
class DictUnpacking(dict_item):
__slots__ = "value",
def __init__(self, value):
self.value = value
class KeyValuePair(dict_item):
__slots__ = "key", "value",
def __init__(self, key, value):
self.key = key
self.value = value
class keyword(dict_item):
__slots__ = "arg", "value",
def __init__(self, arg, value):
self.arg = arg
self.value = value
class AssignExpr(expr):
__slots__ = "target", "value",
def __init__(self, value, target):
self.value = value
self.target = target
class Attribute(expr):
__slots__ = "value", "attr", "ctx",
def __init__(self, value, attr, ctx):
self.value = value
self.attr = attr
self.ctx = ctx
class Await(expr):
__slots__ = "value",
def __init__(self, value):
self.value = value
class BinOp(expr):
__slots__ = "left", "op", "right",
def __init__(self, left, op, right):
self.left = left
self.op = op
self.right = right
class BoolOp(expr):
__slots__ = "op", "values",
def __init__(self, op, values):
self.op = op
self.values = values
class Bytes(expr):
__slots__ = "s", "prefix", "implicitly_concatenated_parts",
def __init__(self, s, prefix, implicitly_concatenated_parts):
self.s = s
self.prefix = prefix
self.implicitly_concatenated_parts = implicitly_concatenated_parts
class Call(expr):
__slots__ = "func", "positional_args", "named_args",
def __init__(self, func, positional_args, named_args):
self.func = func
self.positional_args = positional_args
self.named_args = named_args
class ClassExpr(expr):
'AST node representing class creation'
__slots__ = "name", "type_parameters", "bases", "keywords", "inner_scope",
def __init__(self, name, type_parameters, bases, keywords, inner_scope):
self.name = name
self.type_parameters = type_parameters
self.bases = bases
self.keywords = keywords
self.inner_scope = inner_scope
class Compare(expr):
__slots__ = "left", "ops", "comparators",
def __init__(self, left, ops, comparators):
self.left = left
self.ops = ops
self.comparators = comparators
class Dict(expr):
__slots__ = "items",
def __init__(self, items):
self.items = items
class DictComp(expr):
__slots__ = "key", "value", "generators", "function", "iterable",
def __init__(self, key, value, generators):
self.key = key
self.value = value
self.generators = generators
class Ellipsis(expr):
pass
class Filter(expr):
'''Filtered expression in a template'''
__slots__ = "value", "filter",
def __init__(self, value, filter):
self.value = value
self.filter = filter
class FormattedValue(expr):
__slots__ = "value", "conversion", "format_spec",
def __init__(self, value, conversion, format_spec):
self.value = value
self.conversion = conversion
self.format_spec = format_spec
class FunctionExpr(expr):
'AST node representing function creation'
__slots__ = "name", "args", "returns", "inner_scope",
def __init__(self, name, args, returns, inner_scope):
self.name = name
self.args = args
self.returns = returns
self.inner_scope = inner_scope
class GeneratorExp(expr):
__slots__ = "elt", "generators", "function", "iterable",
def __init__(self, elt, generators):
self.elt = elt
self.generators = generators
class IfExp(expr):
__slots__ = "test", "body", "orelse",
def __init__(self, test, body, orelse):
self.test = test
self.body = body
self.orelse = orelse
class ImportExpr(expr):
'''AST node representing module import
(roughly equivalent to the runtime call to __import__)'''
__slots__ = "level", "name", "top",
def __init__(self, level, name, top):
self.level = level
self.name = name
self.top = top
class ImportMember(expr):
'''AST node representing 'from import'. Similar to Attribute access,
but during import'''
__slots__ = "module", "name",
def __init__(self, module, name):
self.module = module
self.name = name
class JoinedStr(expr):
__slots__ = "values",
def __init__(self, values):
self.values = values
class TemplateString(expr):
__slots__ = "prefix", "values",
def __init__(self, prefix, values):
self.prefix = prefix
self.values = values
class JoinedTemplateString(expr):
__slots__ = "strings",
def __init__(self, strings):
self.strings = strings
class Lambda(expr):
__slots__ = "args", "inner_scope",
def __init__(self, args, inner_scope):
self.args = args
self.inner_scope = inner_scope
class List(expr):
__slots__ = "elts", "ctx",
def __init__(self, elts, ctx):
self.elts = elts
self.ctx = ctx
class ListComp(expr):
__slots__ = "elt", "generators", "function", "iterable",
def __init__(self, elt, generators):
self.elt = elt
self.generators = generators
class Match(stmt):
__slots__ = "subject", "cases",
def __init__(self, subject, cases):
self.subject = subject
self.cases = cases
class Case(stmt):
__slots__ = "pattern", "guard", "body",
def __init__(self, pattern, guard, body):
self.pattern = pattern
self.guard = guard
self.body = body
class Guard(expr):
__slots__ = "test",
def __init__(self, test):
self.test = test
class MatchAsPattern(pattern):
__slots__ = "pattern", "alias",
def __init__(self, pattern, alias):
self.pattern = pattern
self.alias = alias
class MatchOrPattern(pattern):
__slots__ = "patterns",
def __init__(self, patterns):
self.patterns = patterns
class MatchLiteralPattern(pattern):
__slots__ = "literal",
def __init__(self, literal):
self.literal = literal
class MatchCapturePattern(pattern):
__slots__ = "variable",
def __init__(self, variable):
self.variable = variable
class MatchWildcardPattern(pattern):
__slots__ = []
class MatchValuePattern(pattern):
__slots__ = "value",
def __init__(self, value):
self.value = value
class MatchSequencePattern(pattern):
__slots__ = "patterns",
def __init__(self, patterns):
self.patterns = patterns
class MatchStarPattern(pattern):
__slots__ = "target",
def __init__(self, target):
self.target = target
class MatchMappingPattern(pattern):
__slots__ = "mappings",
def __init__(self, mappings):
self.mappings = mappings
class MatchDoubleStarPattern(pattern):
__slots__ = "target",
def __init__(self, target):
self.target = target
class MatchKeyValuePattern(pattern):
__slots__ = "key", "value",
def __init__(self, key, value):
self.key = key
self.value = value
class MatchClassPattern(pattern):
__slots__ = "class_name", "positional", "keyword",
def __init__(self, class_name, positional, keyword):
self.class_name = class_name
self.positional = positional
self.keyword = keyword
class MatchKeywordPattern(pattern):
__slots__ = "attribute", "value",
def __init__(self, attribute, value):
self.attribute = attribute
self.value = value
class Name(expr):
__slots__ = "variable", "ctx",
def __init__(self, variable, ctx):
self.variable = variable
self.ctx = ctx
@property
def id(self):
return self.variable.id
class Num(expr):
__slots__ = "n", "text",
def __init__(self, n, text):
self.n = n
self.text = text
class ParamSpec(type_parameter):
__slots__ = "name", "default",
def __init__(self, name, default):
self.name = name
self.default = default
class PlaceHolder(expr):
'''PlaceHolder variable in template ($name)'''
__slots__ = "variable", "ctx",
def __init__(self, variable, ctx):
self.variable = variable
self.ctx = ctx
@property
def id(self):
return self.variable.id
class Repr(expr):
__slots__ = "value",
def __init__(self, value):
self.value = value
class Set(expr):
__slots__ = "elts",
def __init__(self, elts):
self.elts = elts
class SetComp(expr):
__slots__ = "elt", "generators", "function", "iterable",
def __init__(self, elt, generators):
self.elt = elt
self.generators = generators
class Slice(expr):
'''AST node for a slice as a subclass of expr to simplify Subscripts'''
__slots__ = "start", "stop", "step",
def __init__(self, start, stop, step):
self.start = start
self.stop = stop
self.step = step
class Starred(expr):
__slots__ = "value", "ctx",
def __init__(self, value, ctx):
self.value = value
self.ctx = ctx
class Str(expr):
__slots__ = "s", "prefix", "implicitly_concatenated_parts",
def __init__(self, s, prefix, implicitly_concatenated_parts):
self.s = s
self.prefix = prefix
self.implicitly_concatenated_parts = implicitly_concatenated_parts
class Subscript(expr):
__slots__ = "value", "index", "ctx",
def __init__(self, value, index, ctx):
self.value = value
self.index = index
self.ctx = ctx
class TemplateDottedNotation(expr):
'''Unified dot notation expression in a template'''
__slots__ = "value", "attr", "ctx",
def __init__(self, value, attr, ctx):
self.value = value
self.attr = attr
self.ctx = ctx
class Tuple(expr):
__slots__ = "elts", "ctx",
def __init__(self, elts, ctx):
self.elts = elts
self.ctx = ctx
class TypeAlias(stmt):
__slots__ = "name", "type_parameters", "value",
def __init__(self, name, type_parameters, value):
self.name = name
self.type_parameters = type_parameters
self.value = value
class TypeVar(type_parameter):
__slots__ = "name", "bound", "default"
def __init__(self, name, bound, default):
self.name = name
self.bound = bound
self.default = default
class TypeVarTuple(type_parameter):
__slots__ = "name", "default",
def __init__(self, name, default):
self.name = name
self.default = default
class UnaryOp(expr):
__slots__ = "op", "operand",
def __init__(self, op, operand):
self.op = op
self.operand = operand
class Yield(expr):
__slots__ = "value",
def __init__(self, value):
self.value = value
class YieldFrom(expr):
__slots__ = "value",
def __init__(self, value):
self.value = value
class SpecialOperation(expr):
__slots__ = "name", "arguments"
def __init__(self, name, arguments):
self.name = name
self.arguments = arguments
class AugLoad(expr_context):
pass
class AugStore(expr_context):
pass
class Del(expr_context):
pass
class Load(expr_context):
pass
class Param(expr_context):
pass
class Store(expr_context):
pass
class Add(operator):
pass
class BitAnd(operator):
pass
class BitOr(operator):
pass
class BitXor(operator):
pass
class Div(operator):
pass
class FloorDiv(operator):
pass
class LShift(operator):
pass
class MatMult(operator):
pass
class Mod(operator):
pass
class Mult(operator):
pass
class Pow(operator):
pass
class RShift(operator):
pass
class Sub(operator):
pass
class AnnAssign(stmt):
__slots__ = "value", "annotation", "target",
def __init__(self, value, annotation, target):
self.value = value
self.annotation = annotation
self.target = target
class Assert(stmt):
__slots__ = "test", "msg",
def __init__(self, test, msg):
self.test = test
self.msg = msg
class Assign(stmt):
__slots__ = "targets", "value",
def __init__(self, value, targets):
self.value = value
assert isinstance(targets, list)
self.targets = targets
class AugAssign(stmt):
__slots__ = "operation",
def __init__(self, operation):
self.operation = operation
class Break(stmt):
pass
class Continue(stmt):
pass
class Delete(stmt):
__slots__ = "targets",
def __init__(self, targets):
self.targets = targets
class ExceptStmt(stmt):
'''AST node for except handler, as a subclass of stmt in order
to better support location and flow control'''
__slots__ = "type", "name", "body",
def __init__(self, type, name, body):
self.type = type
self.name = name
self.body = body
class ExceptGroupStmt(stmt):
'''AST node for except* handler, as a subclass of stmt in order
to better support location and flow control'''
__slots__ = "type", "name", "body",
def __init__(self, type, name, body):
self.type = type
self.name = name
self.body = body
class Exec(stmt):
__slots__ = "body", "globals", "locals",
def __init__(self, body, globals, locals):
self.body = body
self.globals = globals
self.locals = locals
class Expr(stmt):
__slots__ = "value",
def __init__(self, value):
self.value = value
class For(stmt):
__slots__ = "is_async", "target", "iter", "body", "orelse",
def __init__(self, target, iter, body, orelse, is_async=False):
self.target = target
self.iter = iter
self.body = body
self.orelse = orelse
self.is_async = is_async
class Global(stmt):
__slots__ = "names",
def __init__(self, names):
self.names = names
class If(stmt):
__slots__ = "test", "body", "orelse",
def __init__(self, test, body, orelse):
self.test = test
self.body = body
self.orelse = orelse
class Import(stmt):
__slots__ = "names",
def __init__(self, names):
self.names = names
class ImportFrom(stmt):
__slots__ = "module",
def __init__(self, module):
self.module = module
class Nonlocal(stmt):
__slots__ = "names",
def __init__(self, names):
self.names = names
class Pass(stmt):
pass
class Print(stmt):
__slots__ = "dest", "values", "nl",
def __init__(self, dest, values, nl):
self.dest = dest
self.values = values
self.nl = nl
class Raise(stmt):
__slots__ = "exc", "cause", "type", "inst", "tback",
class Return(stmt):
__slots__ = "value",
def __init__(self, value):
self.value = value
class TemplateWrite(stmt):
'''Template text'''
__slots__ = "value",
def __init__(self, value):
self.value = value
class Try(stmt):
__slots__ = "body", "orelse", "handlers", "finalbody",
def __init__(self, body, orelse, handlers, finalbody):
self.body = body
self.orelse = orelse
self.handlers = handlers
self.finalbody = finalbody
class While(stmt):
__slots__ = "test", "body", "orelse",
def __init__(self, test, body, orelse):
self.test = test
self.body = body
self.orelse = orelse
class With(stmt):
__slots__ = "is_async", "context_expr", "optional_vars", "body",
def __init__(self, context_expr, optional_vars, body, is_async=False):
self.context_expr = context_expr
self.optional_vars = optional_vars
self.body = body
self.is_async = is_async
class Invert(unaryop):
pass
class Not(unaryop):
pass
class UAdd(unaryop):
pass
class USub(unaryop):
pass
class Variable(object):
'A variable'
def __init__(self, var_id, scope = None):
assert isinstance(var_id, str), type(var_id)
self.id = var_id
self.scope = scope
def __repr__(self):
return 'Variable(%r, %r)' % (self.id, self.scope)
def __eq__(self, other):
if type(other) is not Variable:
return False
if self.scope is None or other.scope is None:
raise TypeError("Scope not set")
return self.scope == other.scope and self.id == other.id
def __ne__(self, other):
return not self == other
def __hash__(self):
if self.scope is None:
raise TypeError("Scope not set")
return 391246 ^ hash(self.id) ^ hash(self.scope)
def is_global(self):
return isinstance(self.scope, Module)
def iter_fields(node):
for name in node.__slots__:
if hasattr(node, name):
yield name, getattr(node, name)