Lichen

compiler/ast.py

609:3a80900f08ff
2017-02-21 Paul Boddie Only copy changed template files and only generate updated program files, removing the "make clean" invocation and permitting incremental builds. Changed constant labelling to employ content digests so that constant names remain stable and do not confuse already-generated code. method-wrapper-for-context
     1 """Python abstract syntax node definitions     2      3 This file was originally generated by Tools/compiler/astgen.py     4 """     5 from compiler.consts import CO_VARARGS, CO_VARKEYWORDS     6      7 def flatten(seq):     8     l = []     9     for elt in seq:    10         if isinstance(elt, (tuple, list)):    11             for elt2 in flatten(elt):    12                 l.append(elt2)    13         else:    14             l.append(elt)    15     return l    16     17 def flatten_nodes(seq):    18     return [n for n in flatten(seq) if isinstance(n, Node)]    19     20 def flatten_statement(seq):    21     l = []    22     for elt in seq:    23         if isinstance(elt, Stmt):    24             l += flatten_statement(elt)    25         else:    26             l.append(elt)    27     return l    28     29 def flatten_assignment(node):    30     l = []    31     if isinstance(node, (AssList, AssTuple)):    32         for n in node.nodes:    33             l += flatten_assignment(n)    34     else:    35         l.append(node)    36     return l    37     38 def is_deletion(node):    39     return isinstance(node, (AssAttr, AssName)) and node.flags == "OP_DELETE"    40     41 def docstring(s):    42     if s.find("\n") != -1:    43         if s.find("'''") != -1:    44             return '"""%s"""' % s.replace('"""', '\\"\\"\\"')    45         else:    46             return "'''%s'''" % s.replace("'''", "\\'\\'\\'")    47     else:    48         return repr(s)    49     50 def indent(s):    51     return s.replace("\n", "\n\t")    52     53 def get_defaults(node):    54     55     "Return a list of (argument name, default) tuples for the 'node'."    56     57     star = (node.flags & 4 != 0) and 1 or 0    58     dstar = (node.flags & 8 != 0) and 1 or 0    59     argnames = node.argnames[:]    60     61     # Add stars to star and dstar parameters.    62     63     if star:    64         argnames[-dstar-star] = "*%s" % argnames[-dstar-star]    65     if dstar:    66         argnames[-dstar] = "**%s" % argnames[-dstar]    67     68     # Map defaults to parameters.    69     70     defaults = [None] * (len(node.argnames) - star - dstar - len(node.defaults)) + list(node.defaults) + [None] * (star + dstar)    71     return zip(argnames, defaults)    72     73 def decode_function(node):    74     return [(default and "%s=%s" % (argname, default) or argname) for (argname, default) in get_defaults(node)]    75     76 nodes = {}    77     78 class OperatorUser:    79     80     "Operator-related node."    81     82     pass    83     84 class Operator(OperatorUser):    85     86     "Operator node."    87     88     pass    89     90 class Node:    91     92     "Abstract base class for ast nodes."    93     94     def getChildren(self):    95         pass # implemented by subclasses    96     97     def __iter__(self):    98         for n in self.getChildren():    99             yield n   100    101     def getChildNodes(self):   102         pass # implemented by subclasses   103    104 class EmptyNode(Node):   105     pass   106    107 class Expression(Node):   108     # Expression is an artificial node class to support "eval"   109     nodes["expression"] = "Expression"   110     def __init__(self, node):   111         self.node = node   112    113     def getChildren(self):   114         return self.node,   115    116     def getChildNodes(self):   117         return self.node,   118    119     def __repr__(self):   120         return "Expression(%r)" % (self.node,)   121    122     def __str__(self):   123         return str(self.node)   124    125 class Add(Node, Operator):   126     def __init__(self, leftright, lineno=None):   127         self.left = leftright[0]   128         self.right = leftright[1]   129         self.lineno = lineno   130    131     def getChildren(self):   132         return self.left, self.right   133    134     def getChildNodes(self):   135         return self.left, self.right   136    137     def __repr__(self):   138         return "Add((%r, %r))" % (self.left, self.right)   139    140     def __str__(self):   141         return "(%s + %s)" % (self.left, self.right)   142    143 class And(Node):   144     def __init__(self, nodes, lineno=None):   145         self.nodes = nodes   146         self.lineno = lineno   147    148     def getChildren(self):   149         return tuple(flatten(self.nodes))   150    151     def getChildNodes(self):   152         nodelist = []   153         nodelist.extend(flatten_nodes(self.nodes))   154         return tuple(nodelist)   155    156     def __repr__(self):   157         return "And(%r)" % (self.nodes,)   158    159     def __str__(self):   160         return "(%s)" % " and ".join(map(str, self.nodes))   161    162 class AssAttr(Node):   163     def __init__(self, expr, attrname, flags, lineno=None):   164         self.expr = expr   165         self.attrname = attrname   166         self.flags = flags   167         self.lineno = lineno   168    169     def getChildren(self):   170         return self.expr, self.attrname, self.flags   171    172     def getChildNodes(self):   173         return self.expr,   174    175     def __repr__(self):   176         return "AssAttr(%r, %r, %r)" % (self.expr, self.attrname, self.flags)   177    178     def __str__(self):   179         return "%s%s.%s" % (self.flags == "OP_DELETE" and "del " or "", self.expr, self.attrname)   180    181 class AssList(Node):   182     def __init__(self, nodes, lineno=None):   183         self.nodes = nodes   184         self.lineno = lineno   185    186     def getChildren(self):   187         return tuple(flatten(self.nodes))   188    189     def getChildNodes(self):   190         nodelist = []   191         nodelist.extend(flatten_nodes(self.nodes))   192         return tuple(nodelist)   193    194     def __repr__(self):   195         return "AssList(%r)" % (self.nodes,)   196    197     def __str__(self):   198         nodes = flatten_assignment(self)   199         if nodes and is_deletion(nodes[0]):   200             return "; ".join(map(str, self.nodes))   201         else:   202             return "[%s]" % ", ".join(map(str, self.nodes))   203    204 class AssName(Node):   205     def __init__(self, name, flags, lineno=None):   206         self.name = name   207         self.flags = flags   208         self.lineno = lineno   209    210     def getChildren(self):   211         return self.name, self.flags   212    213     def getChildNodes(self):   214         return ()   215    216     def __repr__(self):   217         return "AssName(%r, %r)" % (self.name, self.flags)   218    219     def __str__(self):   220         return "%s%s" % (self.flags == "OP_DELETE" and "del " or "", self.name)   221    222 class AssTuple(Node):   223     def __init__(self, nodes, lineno=None):   224         self.nodes = nodes   225         self.lineno = lineno   226    227     def getChildren(self):   228         return tuple(flatten(self.nodes))   229    230     def getChildNodes(self):   231         nodelist = []   232         nodelist.extend(flatten_nodes(self.nodes))   233         return tuple(nodelist)   234    235     def __repr__(self):   236         return "AssTuple(%r)" % (self.nodes,)   237    238     def __str__(self):   239         nodes = flatten_assignment(self)   240         if nodes and is_deletion(nodes[0]):   241             return "; ".join(map(str, self.nodes))   242         else:   243             return "(%s)" % ", ".join(map(str, self.nodes))   244    245 class Assert(Node):   246     def __init__(self, test, fail, lineno=None):   247         self.test = test   248         self.fail = fail   249         self.lineno = lineno   250    251     def getChildren(self):   252         children = []   253         children.append(self.test)   254         children.append(self.fail)   255         return tuple(children)   256    257     def getChildNodes(self):   258         nodelist = []   259         nodelist.append(self.test)   260         if self.fail is not None:   261             nodelist.append(self.fail)   262         return tuple(nodelist)   263    264     def __repr__(self):   265         return "Assert(%r, %r)" % (self.test, self.fail)   266    267     def __str__(self):   268         return "assert %s%s" % (self.test, self.fail and ", %s" % self.fail or "")   269    270 class Assign(Node):   271     def __init__(self, nodes, expr, lineno=None):   272         self.nodes = nodes   273         self.expr = expr   274         self.lineno = lineno   275    276     def getChildren(self):   277         children = []   278         children.extend(flatten(self.nodes))   279         children.append(self.expr)   280         return tuple(children)   281    282     def getChildNodes(self):   283         nodelist = []   284         nodelist.extend(flatten_nodes(self.nodes))   285         nodelist.append(self.expr)   286         return tuple(nodelist)   287    288     def __repr__(self):   289         return "Assign(%r, %r)" % (self.nodes, self.expr)   290    291     def __str__(self):   292         return "%s = %s" % (", ".join(map(str, self.nodes)), self.expr)   293    294 class AugAssign(Node, OperatorUser):   295     def __init__(self, node, op, expr, lineno=None):   296         self.node = node   297         self.op = op   298         self.expr = expr   299         self.lineno = lineno   300    301     def getChildren(self):   302         return self.node, self.op, self.expr   303    304     def getChildNodes(self):   305         return self.node, self.expr   306    307     def __repr__(self):   308         return "AugAssign(%r, %r, %r)" % (self.node, self.op, self.expr)   309    310     def __str__(self):   311         return "%s %s %s" % (self.node, self.op, self.expr)   312    313 class Backquote(Node):   314     def __init__(self, expr, lineno=None):   315         self.expr = expr   316         self.lineno = lineno   317    318     def getChildren(self):   319         return self.expr,   320    321     def getChildNodes(self):   322         return self.expr,   323    324     def __repr__(self):   325         return "Backquote(%r)" % (self.expr,)   326    327     def __str__(self):   328         return "`%s`" % self.expr   329    330 class Bitand(Node, Operator):   331     def __init__(self, nodes, lineno=None):   332         self.nodes = nodes   333         self.lineno = lineno   334    335     def getChildren(self):   336         return tuple(flatten(self.nodes))   337    338     def getChildNodes(self):   339         nodelist = []   340         nodelist.extend(flatten_nodes(self.nodes))   341         return tuple(nodelist)   342    343     def __repr__(self):   344         return "Bitand(%r)" % (self.nodes,)   345    346     def __str__(self):   347         return "(%s)" % " & ".join(map(str, self.nodes))   348    349 class Bitor(Node, Operator):   350     def __init__(self, nodes, lineno=None):   351         self.nodes = nodes   352         self.lineno = lineno   353    354     def getChildren(self):   355         return tuple(flatten(self.nodes))   356    357     def getChildNodes(self):   358         nodelist = []   359         nodelist.extend(flatten_nodes(self.nodes))   360         return tuple(nodelist)   361    362     def __repr__(self):   363         return "Bitor(%r)" % (self.nodes,)   364    365     def __str__(self):   366         return "(%s)" % " | ".join(map(str, self.nodes))   367    368 class Bitxor(Node, Operator):   369     def __init__(self, nodes, lineno=None):   370         self.nodes = nodes   371         self.lineno = lineno   372    373     def getChildren(self):   374         return tuple(flatten(self.nodes))   375    376     def getChildNodes(self):   377         nodelist = []   378         nodelist.extend(flatten_nodes(self.nodes))   379         return tuple(nodelist)   380    381     def __repr__(self):   382         return "Bitxor(%r)" % (self.nodes,)   383    384     def __str__(self):   385         return "(%s)" % " ^ ".join(map(str, self.nodes))   386    387 class Break(Node):   388     def __init__(self, lineno=None):   389         self.lineno = lineno   390    391     def getChildren(self):   392         return ()   393    394     def getChildNodes(self):   395         return ()   396    397     def __repr__(self):   398         return "Break()"   399    400     def __str__(self):   401         return "break"   402    403 class CallFunc(Node):   404     def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None):   405         self.node = node   406         self.args = args   407         self.star_args = star_args   408         self.dstar_args = dstar_args   409         self.lineno = lineno   410    411     def getChildren(self):   412         children = []   413         children.append(self.node)   414         children.extend(flatten(self.args))   415         children.append(self.star_args)   416         children.append(self.dstar_args)   417         return tuple(children)   418    419     def getChildNodes(self):   420         nodelist = []   421         nodelist.append(self.node)   422         nodelist.extend(flatten_nodes(self.args))   423         if self.star_args is not None:   424             nodelist.append(self.star_args)   425         if self.dstar_args is not None:   426             nodelist.append(self.dstar_args)   427         return tuple(nodelist)   428    429     def __repr__(self):   430         args = []   431         if self.dstar_args:   432             args.insert(0, repr(self.dstar_args))   433         if args or self.star_args:   434             args.insert(0, repr(self.star_args))   435         return "CallFunc(%r, %r%s)" % (self.node, self.args, args and (", %s" % ", ".join(args)) or "")   436    437     def __str__(self):   438         star_args = self.star_args and ["*%s" % self.star_args] or []   439         dstar_args = self.dstar_args and ["**%s" % self.dstar_args] or []   440         return "%s(%s)" % (self.node, ", ".join(map(str, tuple(self.args) + tuple(star_args) + tuple(dstar_args))))   441    442 class Class(Node):   443     def __init__(self, name, bases, doc, code, decorators = None, lineno=None):   444         self.name = name   445         self.bases = bases   446         self.doc = doc   447         self.code = code   448         self.decorators = decorators   449         self.lineno = lineno   450    451     def getChildren(self):   452         children = []   453         children.append(self.name)   454         children.extend(flatten(self.bases))   455         children.append(self.doc)   456         children.append(self.code)   457         children.append(self.decorators)   458         return tuple(children)   459    460     def getChildNodes(self):   461         nodelist = []   462         nodelist.extend(flatten_nodes(self.bases))   463         nodelist.append(self.code)   464         if self.decorators is not None:   465             nodelist.append(self.decorators)   466         return tuple(nodelist)   467    468     def __repr__(self):   469         return "Class(%r, %r, %r, %r, %r)" % (self.name, self.bases, self.doc, self.code, self.decorators)   470    471     def __str__(self):   472         return "%sclass %s%s:%s%s\n" % (   473             self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "",   474             self.name,   475             self.bases and "(%s)" % ", ".join(map(str, self.bases)) or "",   476             self.doc and "\n\t" + docstring(self.doc) or "",   477             indent("\n%s" % self.code)   478             )   479    480 class Compare(Node, OperatorUser):   481     def __init__(self, expr, ops, lineno=None):   482         self.expr = expr   483         self.ops = ops   484         self.lineno = lineno   485    486     def getChildren(self):   487         children = []   488         children.append(self.expr)   489         children.extend(flatten(self.ops))   490         return tuple(children)   491    492     def getChildNodes(self):   493         nodelist = []   494         nodelist.append(self.expr)   495         nodelist.extend(flatten_nodes(self.ops))   496         return tuple(nodelist)   497    498     def __repr__(self):   499         return "Compare(%r, %r)" % (self.expr, self.ops)   500    501     def __str__(self):   502         return "%s %s" % (self.expr, " ".join([("%s %s" % op) for op in self.ops]))   503    504 class Const(Node):   505     def __init__(self, value, literals=None, lineno=None):   506         self.value = value   507         self.literals = literals   508         self.lineno = lineno   509    510     def getChildren(self):   511         return self.value,   512    513     def getChildNodes(self):   514         return ()   515    516     def __repr__(self):   517         return "Const(%r, %r)" % (self.value, self.literals)   518    519     def __str__(self):   520         return repr(self.value)   521    522 class Continue(Node):   523     def __init__(self, lineno=None):   524         self.lineno = lineno   525    526     def getChildren(self):   527         return ()   528    529     def getChildNodes(self):   530         return ()   531    532     def __repr__(self):   533         return "Continue()"   534    535     def __str__(self):   536         return "continue"   537    538 class Decorators(Node):   539     def __init__(self, nodes, lineno=None):   540         self.nodes = nodes   541         self.lineno = lineno   542    543     def getChildren(self):   544         return tuple(flatten(self.nodes))   545    546     def getChildNodes(self):   547         nodelist = []   548         nodelist.extend(flatten_nodes(self.nodes))   549         return tuple(nodelist)   550    551     def __repr__(self):   552         return "Decorators(%r)" % (self.nodes,)   553    554     def __str__(self):   555         return "\n".join([("@%s" % node) for node in self.nodes])   556    557 class Dict(Node):   558     def __init__(self, items, lineno=None):   559         self.items = items   560         self.lineno = lineno   561    562     def getChildren(self):   563         return tuple(flatten(self.items))   564    565     def getChildNodes(self):   566         nodelist = []   567         nodelist.extend(flatten_nodes(self.items))   568         return tuple(nodelist)   569    570     def __repr__(self):   571         return "Dict(%r)" % (self.items,)   572    573     def __str__(self):   574         return "{%s}" % ", ".join([("%s : %s" % (key, value)) for (key, value) in self.items])   575    576 class Discard(Node):   577     def __init__(self, expr, lineno=None):   578         self.expr = expr   579         self.lineno = lineno   580    581     def getChildren(self):   582         return self.expr,   583    584     def getChildNodes(self):   585         return self.expr,   586    587     def __repr__(self):   588         return "Discard(%r)" % (self.expr,)   589    590     def __str__(self):   591         return str(self.expr)   592    593 class Div(Node, Operator):   594     def __init__(self, leftright, lineno=None):   595         self.left = leftright[0]   596         self.right = leftright[1]   597         self.lineno = lineno   598    599     def getChildren(self):   600         return self.left, self.right   601    602     def getChildNodes(self):   603         return self.left, self.right   604    605     def __repr__(self):   606         return "Div((%r, %r))" % (self.left, self.right)   607    608     def __str__(self):   609         return "(%s / %s)" % (self.left, self.right)   610    611 class Ellipsis(Node):   612     def __init__(self, lineno=None):   613         self.lineno = lineno   614    615     def getChildren(self):   616         return ()   617    618     def getChildNodes(self):   619         return ()   620    621     def __repr__(self):   622         return "Ellipsis()"   623    624     def __str__(self):   625         return "..."   626    627 class Exec(Node):   628     def __init__(self, expr, locals, globals, lineno=None):   629         self.expr = expr   630         self.locals = locals   631         self.globals = globals   632         self.lineno = lineno   633    634     def getChildren(self):   635         children = []   636         children.append(self.expr)   637         children.append(self.locals)   638         children.append(self.globals)   639         return tuple(children)   640    641     def getChildNodes(self):   642         nodelist = []   643         nodelist.append(self.expr)   644         if self.locals is not None:   645             nodelist.append(self.locals)   646         if self.globals is not None:   647             nodelist.append(self.globals)   648         return tuple(nodelist)   649    650     def __repr__(self):   651         return "Exec(%r, %r, %r)" % (self.expr, self.locals, self.globals)   652    653     def __str__(self):   654         return "exec %s%s%s" % (self.expr, self.locals and "in %s" % self.locals or "",   655             self.globals and ", %s" % self.globals or "")   656    657 class FloorDiv(Node, Operator):   658     def __init__(self, leftright, lineno=None):   659         self.left = leftright[0]   660         self.right = leftright[1]   661         self.lineno = lineno   662    663     def getChildren(self):   664         return self.left, self.right   665    666     def getChildNodes(self):   667         return self.left, self.right   668    669     def __repr__(self):   670         return "FloorDiv((%r, %r))" % (self.left, self.right)   671    672     def __str__(self):   673         return "(%s // %s)" % (self.left, self.right)   674    675 class For(Node):   676     def __init__(self, assign, list, body, else_, lineno=None):   677         self.assign = assign   678         self.list = list   679         self.body = body   680         self.else_ = else_   681         self.lineno = lineno   682    683     def getChildren(self):   684         children = []   685         children.append(self.assign)   686         children.append(self.list)   687         children.append(self.body)   688         children.append(self.else_)   689         return tuple(children)   690    691     def getChildNodes(self):   692         nodelist = []   693         nodelist.append(self.assign)   694         nodelist.append(self.list)   695         nodelist.append(self.body)   696         if self.else_ is not None:   697             nodelist.append(self.else_)   698         return tuple(nodelist)   699    700     def __repr__(self):   701         return "For(%r, %r, %r, %r)" % (self.assign, self.list, self.body, self.else_)   702    703     def __str__(self):   704         return "for %s in %s:%s%s" % (   705             self.assign, self.list,   706             indent("\n%s" % self.body),   707             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""   708             )   709    710 class From(Node):   711     def __init__(self, modname, names, level, lineno=None):   712         self.modname = modname   713         self.names = names   714         self.level = level   715         self.lineno = lineno   716    717     def getChildren(self):   718         return self.modname, self.names, self.level   719    720     def getChildNodes(self):   721         return ()   722    723     def __repr__(self):   724         return "From(%r, %r, %r)" % (self.modname, self.names, self.level)   725    726     def __str__(self):   727         return "from %s import %s" % (self.modname,   728             ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names]))   729    730 class Function(Node):   731     def __init__(self, decorators, name, argnames, defaults, flags, doc, code, lineno=None):   732         self.decorators = decorators   733         self.name = name   734         self.argnames = argnames   735         self.defaults = defaults   736         self.flags = flags   737         self.doc = doc   738         self.code = code   739         self.lineno = lineno   740         self.varargs = self.kwargs = None   741         if flags & CO_VARARGS:   742             self.varargs = 1   743         if flags & CO_VARKEYWORDS:   744             self.kwargs = 1   745    746     def getChildren(self):   747         children = []   748         children.append(self.decorators)   749         children.append(self.name)   750         children.append(self.argnames)   751         children.extend(flatten(self.defaults))   752         children.append(self.flags)   753         children.append(self.doc)   754         children.append(self.code)   755         return tuple(children)   756    757     def getChildNodes(self):   758         nodelist = []   759         if self.decorators is not None:   760             nodelist.append(self.decorators)   761         nodelist.extend(flatten_nodes(self.defaults))   762         nodelist.append(self.code)   763         return tuple(nodelist)   764    765     def __repr__(self):   766         return "Function(%r, %r, %r, %r, %r, %r, %r)" % (self.decorators, self.name, self.argnames, self.defaults, self.flags, self.doc, self.code)   767    768     def __str__(self):   769         parameters = decode_function(self)   770    771         return "%sdef %s(%s):%s%s\n" % (   772             self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "",   773             self.name,   774             ", ".join(parameters),   775             self.doc and "\n\n\t%s\n" % docstring(self.doc) or "",   776             indent("\n%s" % self.code)   777             )   778    779 class GenExpr(Node):   780     def __init__(self, code, lineno=None):   781         self.code = code   782         self.lineno = lineno   783         self.argnames = ['.0']   784         self.varargs = self.kwargs = None   785    786     def getChildren(self):   787         return self.code,   788    789     def getChildNodes(self):   790         return self.code,   791    792     def __repr__(self):   793         return "GenExpr(%r)" % (self.code,)   794    795     def __str__(self):   796         return str(self.code)   797    798 class GenExprFor(Node):   799     def __init__(self, assign, iter, ifs, lineno=None):   800         self.assign = assign   801         self.iter = iter   802         self.ifs = ifs   803         self.lineno = lineno   804         self.is_outmost = False   805    806     def getChildren(self):   807         children = []   808         children.append(self.assign)   809         children.append(self.iter)   810         children.extend(flatten(self.ifs))   811         return tuple(children)   812    813     def getChildNodes(self):   814         nodelist = []   815         nodelist.append(self.assign)   816         nodelist.append(self.iter)   817         nodelist.extend(flatten_nodes(self.ifs))   818         return tuple(nodelist)   819    820     def __repr__(self):   821         return "GenExprFor(%r, %r, %r)" % (self.assign, self.iter, self.ifs)   822    823     def __str__(self):   824         return "for %s in %s%s" % (   825             self.assign, self.iter,   826             self.ifs and " ".join(map(str, self.ifs)) or ""   827             )   828    829 class GenExprIf(Node):   830     def __init__(self, test, lineno=None):   831         self.test = test   832         self.lineno = lineno   833    834     def getChildren(self):   835         return self.test,   836    837     def getChildNodes(self):   838         return self.test,   839    840     def __repr__(self):   841         return "GenExprIf(%r)" % (self.test,)   842    843     def __str__(self):   844         return "if %s" % self.test   845    846 class GenExprInner(Node):   847     def __init__(self, expr, quals, lineno=None):   848         self.expr = expr   849         self.quals = quals   850         self.lineno = lineno   851    852     def getChildren(self):   853         children = []   854         children.append(self.expr)   855         children.extend(flatten(self.quals))   856         return tuple(children)   857    858     def getChildNodes(self):   859         nodelist = []   860         nodelist.append(self.expr)   861         nodelist.extend(flatten_nodes(self.quals))   862         return tuple(nodelist)   863    864     def __repr__(self):   865         return "GenExprInner(%r, %r)" % (self.expr, self.quals)   866    867     def __str__(self):   868         return "%s %s" % (self.expr, " ".join(map(str, self.quals)))   869    870 class Getattr(Node):   871     def __init__(self, expr, attrname, lineno=None):   872         self.expr = expr   873         self.attrname = attrname   874         self.lineno = lineno   875    876     def getChildren(self):   877         return self.expr, self.attrname   878    879     def getChildNodes(self):   880         return self.expr,   881    882     def __repr__(self):   883         return "Getattr(%r, %r)" % (self.expr, self.attrname)   884    885     def __str__(self):   886         return "%s.%s" % (self.expr, self.attrname)   887    888 class Global(Node):   889     def __init__(self, names, lineno=None):   890         self.names = names   891         self.lineno = lineno   892    893     def getChildren(self):   894         return self.names,   895    896     def getChildNodes(self):   897         return ()   898    899     def __repr__(self):   900         return "Global(%r)" % (self.names,)   901    902     def __str__(self):   903         return "global %s" % ", ".join(map(str, self.names))   904    905 class If(Node):   906     def __init__(self, tests, else_, lineno=None):   907         self.tests = tests   908         self.else_ = else_   909         self.lineno = lineno   910    911     def getChildren(self):   912         children = []   913         children.extend(flatten(self.tests))   914         children.append(self.else_)   915         return tuple(children)   916    917     def getChildNodes(self):   918         nodelist = []   919         nodelist.extend(flatten_nodes(self.tests))   920         if self.else_ is not None:   921             nodelist.append(self.else_)   922         return tuple(nodelist)   923    924     def __repr__(self):   925         return "If(%r, %r)" % (self.tests, self.else_)   926    927     def __str__(self):   928         tests = [("%sif %s:%s" % (i > 0 and "el" or "", test, indent("\n%s" % body))) for (i, (test, body)) in enumerate(self.tests)]   929         return "%s%s" % (   930             "\n".join(tests),   931             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""   932             )   933    934 class Import(Node):   935     def __init__(self, names, lineno=None):   936         self.names = names   937         self.lineno = lineno   938    939     def getChildren(self):   940         return self.names,   941    942     def getChildNodes(self):   943         return ()   944    945     def __repr__(self):   946         return "Import(%r)" % (self.names,)   947    948     def __str__(self):   949         return "import %s" % (   950             ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names]))   951    952 class Invert(Node, Operator):   953     def __init__(self, expr, lineno=None):   954         self.expr = expr   955         self.lineno = lineno   956    957     def getChildren(self):   958         return self.expr,   959    960     def getChildNodes(self):   961         return self.expr,   962    963     def __repr__(self):   964         return "Invert(%r)" % (self.expr,)   965    966     def __str__(self):   967         return "~%s" % self.expr   968    969 class Keyword(Node):   970     def __init__(self, name, expr, lineno=None):   971         self.name = name   972         self.expr = expr   973         self.lineno = lineno   974    975     def getChildren(self):   976         return self.name, self.expr   977    978     def getChildNodes(self):   979         return self.expr,   980    981     def __repr__(self):   982         return "Keyword(%r, %r)" % (self.name, self.expr)   983    984     def __str__(self):   985         return "%s=%s" % (self.name, self.expr)   986    987 class Lambda(Node):   988     def __init__(self, argnames, defaults, flags, code, lineno=None):   989         self.argnames = argnames   990         self.defaults = defaults   991         self.flags = flags   992         self.code = code   993         self.lineno = lineno   994         self.varargs = self.kwargs = None   995         if flags & CO_VARARGS:   996             self.varargs = 1   997         if flags & CO_VARKEYWORDS:   998             self.kwargs = 1   999   1000     def getChildren(self):  1001         children = []  1002         children.append(self.argnames)  1003         children.extend(flatten(self.defaults))  1004         children.append(self.flags)  1005         children.append(self.code)  1006         return tuple(children)  1007   1008     def getChildNodes(self):  1009         nodelist = []  1010         nodelist.extend(flatten_nodes(self.defaults))  1011         nodelist.append(self.code)  1012         return tuple(nodelist)  1013   1014     def __repr__(self):  1015         return "Lambda(%r, %r, %r, %r)" % (self.argnames, self.defaults, self.flags, self.code)  1016   1017     def __str__(self):  1018         parameters = decode_function(self)  1019         return "lambda %s: %s" % (", ".join(parameters), self.code)  1020   1021 class LeftShift(Node, Operator):  1022     def __init__(self, leftright, lineno=None):  1023         self.left = leftright[0]  1024         self.right = leftright[1]  1025         self.lineno = lineno  1026   1027     def getChildren(self):  1028         return self.left, self.right  1029   1030     def getChildNodes(self):  1031         return self.left, self.right  1032   1033     def __repr__(self):  1034         return "LeftShift((%r, %r))" % (self.left, self.right)  1035   1036     def __str__(self):  1037         return "(%s << %s)" % (self.left, self.right)  1038   1039 class List(Node):  1040     def __init__(self, nodes, lineno=None):  1041         self.nodes = nodes  1042         self.lineno = lineno  1043   1044     def getChildren(self):  1045         return tuple(flatten(self.nodes))  1046   1047     def getChildNodes(self):  1048         nodelist = []  1049         nodelist.extend(flatten_nodes(self.nodes))  1050         return tuple(nodelist)  1051   1052     def __repr__(self):  1053         return "List(%r)" % (self.nodes,)  1054   1055     def __str__(self):  1056         return "[%s]" % ", ".join(map(str, self.nodes))  1057   1058 class Mod(Node, Operator):  1059     def __init__(self, leftright, lineno=None):  1060         self.left = leftright[0]  1061         self.right = leftright[1]  1062         self.lineno = lineno  1063   1064     def getChildren(self):  1065         return self.left, self.right  1066   1067     def getChildNodes(self):  1068         return self.left, self.right  1069   1070     def __repr__(self):  1071         return "Mod((%r, %r))" % (self.left, self.right)  1072   1073     def __str__(self):  1074         return "(%s %% %s)" % (self.left, self.right)  1075   1076 class Module(Node):  1077     def __init__(self, doc, node, lineno=None):  1078         self.doc = doc  1079         self.node = node  1080         self.lineno = lineno  1081   1082     def getChildren(self):  1083         return self.doc, self.node  1084   1085     def getChildNodes(self):  1086         return self.node,  1087   1088     def __repr__(self):  1089         return "Module(%r, %r)" % (self.doc, self.node)  1090   1091     def __str__(self):  1092         return "%s%s" % (self.doc and "%s\n\n" % docstring(self.doc) or "", self.node)  1093   1094 class Mul(Node, Operator):  1095     def __init__(self, leftright, lineno=None):  1096         self.left = leftright[0]  1097         self.right = leftright[1]  1098         self.lineno = lineno  1099   1100     def getChildren(self):  1101         return self.left, self.right  1102   1103     def getChildNodes(self):  1104         return self.left, self.right  1105   1106     def __repr__(self):  1107         return "Mul((%r, %r))" % (self.left, self.right)  1108   1109     def __str__(self):  1110         return "(%s * %s)" % (self.left, self.right)  1111   1112 class Name(Node):  1113     def __init__(self, name, lineno=None):  1114         self.name = name  1115         self.lineno = lineno  1116   1117     def getChildren(self):  1118         return self.name,  1119   1120     def getChildNodes(self):  1121         return ()  1122   1123     def __repr__(self):  1124         return "Name(%r)" % (self.name,)  1125   1126     def __str__(self):  1127         return str(self.name)  1128   1129 class Not(Node):  1130     def __init__(self, expr, lineno=None):  1131         self.expr = expr  1132         self.lineno = lineno  1133   1134     def getChildren(self):  1135         return self.expr,  1136   1137     def getChildNodes(self):  1138         return self.expr,  1139   1140     def __repr__(self):  1141         return "Not(%r)" % (self.expr,)  1142   1143     def __str__(self):  1144         return "not %s" % self.expr  1145   1146 class Or(Node):  1147     def __init__(self, nodes, lineno=None):  1148         self.nodes = nodes  1149         self.lineno = lineno  1150   1151     def getChildren(self):  1152         return tuple(flatten(self.nodes))  1153   1154     def getChildNodes(self):  1155         nodelist = []  1156         nodelist.extend(flatten_nodes(self.nodes))  1157         return tuple(nodelist)  1158   1159     def __repr__(self):  1160         return "Or(%r)" % (self.nodes,)  1161   1162     def __str__(self):  1163         return "(%s)" % " or ".join(map(str, self.nodes))  1164   1165 class Pass(Node):  1166     def __init__(self, lineno=None):  1167         self.lineno = lineno  1168   1169     def getChildren(self):  1170         return ()  1171   1172     def getChildNodes(self):  1173         return ()  1174   1175     def __repr__(self):  1176         return "Pass()"  1177   1178     def __str__(self):  1179         return "pass"  1180   1181 class Power(Node, Operator):  1182     def __init__(self, leftright, lineno=None):  1183         self.left = leftright[0]  1184         self.right = leftright[1]  1185         self.lineno = lineno  1186   1187     def getChildren(self):  1188         return self.left, self.right  1189   1190     def getChildNodes(self):  1191         return self.left, self.right  1192   1193     def __repr__(self):  1194         return "Power((%r, %r))" % (self.left, self.right)  1195   1196     def __str__(self):  1197         return "(%s ** %s)" % (self.left, self.right)  1198   1199 class Print(Node):  1200     def __init__(self, nodes, dest, lineno=None):  1201         self.nodes = nodes  1202         self.dest = dest  1203         self.lineno = lineno  1204   1205     def getChildren(self):  1206         children = []  1207         children.extend(flatten(self.nodes))  1208         children.append(self.dest)  1209         return tuple(children)  1210   1211     def getChildNodes(self):  1212         nodelist = []  1213         nodelist.extend(flatten_nodes(self.nodes))  1214         if self.dest is not None:  1215             nodelist.append(self.dest)  1216         return tuple(nodelist)  1217   1218     def __repr__(self):  1219         return "Print(%r, %r)" % (self.nodes, self.dest)  1220   1221     def __str__(self):  1222         dest = self.dest and [">>%s" % self.dest] or []  1223         return "print %s," % ", ".join(map(str, dest + self.nodes))  1224   1225 class Printnl(Node):  1226     def __init__(self, nodes, dest, lineno=None):  1227         self.nodes = nodes  1228         self.dest = dest  1229         self.lineno = lineno  1230   1231     def getChildren(self):  1232         children = []  1233         children.extend(flatten(self.nodes))  1234         children.append(self.dest)  1235         return tuple(children)  1236   1237     def getChildNodes(self):  1238         nodelist = []  1239         nodelist.extend(flatten_nodes(self.nodes))  1240         if self.dest is not None:  1241             nodelist.append(self.dest)  1242         return tuple(nodelist)  1243   1244     def __repr__(self):  1245         return "Printnl(%r, %r)" % (self.nodes, self.dest)  1246   1247     def __str__(self):  1248         dest = self.dest and [">>%s" % self.dest] or []  1249         return "print %s" % ", ".join(map(str, dest + self.nodes))  1250   1251 class Raise(Node):  1252     def __init__(self, expr1, expr2, expr3, lineno=None):  1253         self.expr1 = expr1  1254         self.expr2 = expr2  1255         self.expr3 = expr3  1256         self.lineno = lineno  1257   1258     def getChildren(self):  1259         children = []  1260         children.append(self.expr1)  1261         children.append(self.expr2)  1262         children.append(self.expr3)  1263         return tuple(children)  1264   1265     def getChildNodes(self):  1266         nodelist = []  1267         if self.expr1 is not None:  1268             nodelist.append(self.expr1)  1269         if self.expr2 is not None:  1270             nodelist.append(self.expr2)  1271         if self.expr3 is not None:  1272             nodelist.append(self.expr3)  1273         return tuple(nodelist)  1274   1275     def __repr__(self):  1276         return "Raise(%r, %r, %r)" % (self.expr1, self.expr2, self.expr3)  1277   1278     def __str__(self):  1279         args = self.expr1 and [self.expr1] or []  1280         args += self.expr2 and [self.expr2] or []  1281         args += self.expr3 and [self.expr3] or []  1282         return "raise %s" % ", ".join(map(str, args))  1283   1284 class Return(Node):  1285     def __init__(self, value, lineno=None):  1286         self.value = value  1287         self.lineno = lineno  1288   1289     def getChildren(self):  1290         return self.value,  1291   1292     def getChildNodes(self):  1293         return self.value,  1294   1295     def __repr__(self):  1296         return "Return(%r)" % (self.value,)  1297   1298     def __str__(self):  1299         return "return %s" % self.value  1300   1301 class RightShift(Node, Operator):  1302     def __init__(self, leftright, lineno=None):  1303         self.left = leftright[0]  1304         self.right = leftright[1]  1305         self.lineno = lineno  1306   1307     def getChildren(self):  1308         return self.left, self.right  1309   1310     def getChildNodes(self):  1311         return self.left, self.right  1312   1313     def __repr__(self):  1314         return "RightShift((%r, %r))" % (self.left, self.right)  1315   1316     def __str__(self):  1317         return "(%s >> %s)" % (self.left, self.right)  1318   1319 class Set(Node):  1320     def __init__(self, nodes, lineno=None):  1321         self.nodes = nodes  1322         self.lineno = lineno  1323   1324     def getChildren(self):  1325         return tuple(flatten(self.nodes))  1326   1327     def getChildNodes(self):  1328         nodelist = []  1329         nodelist.extend(flatten_nodes(self.nodes))  1330         return tuple(nodelist)  1331   1332     def __repr__(self):  1333         return "Set(%r)" % (self.nodes,)  1334   1335     def __str__(self):  1336         return "{%s}" % ", ".join(map(str, self.nodes))  1337   1338 class Slice(Node, OperatorUser):  1339     def __init__(self, expr, flags, lower, upper, lineno=None):  1340         self.expr = expr  1341         self.flags = flags  1342         self.lower = lower  1343         self.upper = upper  1344         self.lineno = lineno  1345   1346     def getChildren(self):  1347         children = []  1348         children.append(self.expr)  1349         children.append(self.flags)  1350         children.append(self.lower)  1351         children.append(self.upper)  1352         return tuple(children)  1353   1354     def getChildNodes(self):  1355         nodelist = []  1356         nodelist.append(self.expr)  1357         if self.lower is not None:  1358             nodelist.append(self.lower)  1359         if self.upper is not None:  1360             nodelist.append(self.upper)  1361         return tuple(nodelist)  1362   1363     def __repr__(self):  1364         return "Slice(%r, %r, %r, %r)" % (self.expr, self.flags, self.lower, self.upper)  1365   1366     def __str__(self):  1367         args = [self.lower or "", self.upper or ""]  1368         return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ":".join(map(str, args)))  1369   1370 class Sliceobj(Node):  1371     def __init__(self, nodes, lineno=None):  1372         self.nodes = nodes  1373         self.lineno = lineno  1374   1375     def getChildren(self):  1376         return tuple(flatten(self.nodes))  1377   1378     def getChildNodes(self):  1379         nodelist = []  1380         nodelist.extend(flatten_nodes(self.nodes))  1381         return tuple(nodelist)  1382   1383     def __repr__(self):  1384         return "Sliceobj(%r)" % (self.nodes,)  1385   1386     def __str__(self):  1387         return ":".join(map(str, self.nodes))  1388   1389 class Stmt(Node):  1390     def __init__(self, nodes, lineno=None):  1391         self.nodes = nodes  1392         self.lineno = lineno  1393   1394     def getChildren(self):  1395         return tuple(flatten(self.nodes))  1396   1397     def getChildNodes(self):  1398         nodelist = []  1399         nodelist.extend(flatten_nodes(self.nodes))  1400         return tuple(nodelist)  1401   1402     def __repr__(self):  1403         return "Stmt(%r)" % (self.nodes,)  1404   1405     def __str__(self):  1406         return "\n".join(map(str, flatten_statement(self.nodes)))  1407   1408 class Sub(Node, Operator):  1409     def __init__(self, leftright, lineno=None):  1410         self.left = leftright[0]  1411         self.right = leftright[1]  1412         self.lineno = lineno  1413   1414     def getChildren(self):  1415         return self.left, self.right  1416   1417     def getChildNodes(self):  1418         return self.left, self.right  1419   1420     def __repr__(self):  1421         return "Sub((%r, %r))" % (self.left, self.right)  1422   1423     def __str__(self):  1424         return "(%s - %s)" % (self.left, self.right)  1425   1426 class Subscript(Node, OperatorUser):  1427     def __init__(self, expr, flags, subs, lineno=None):  1428         self.expr = expr  1429         self.flags = flags  1430         self.subs = subs  1431         self.lineno = lineno  1432   1433     def getChildren(self):  1434         children = []  1435         children.append(self.expr)  1436         children.append(self.flags)  1437         children.extend(flatten(self.subs))  1438         return tuple(children)  1439   1440     def getChildNodes(self):  1441         nodelist = []  1442         nodelist.append(self.expr)  1443         nodelist.extend(flatten_nodes(self.subs))  1444         return tuple(nodelist)  1445   1446     def __repr__(self):  1447         return "Subscript(%r, %r, %r)" % (self.expr, self.flags, self.subs)  1448   1449     def __str__(self):  1450         return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ",".join(map(str, self.subs)))  1451   1452 class TryExcept(Node):  1453     def __init__(self, body, handlers, else_, lineno=None):  1454         self.body = body  1455         self.handlers = handlers  1456         self.else_ = else_  1457         self.lineno = lineno  1458   1459     def getChildren(self):  1460         children = []  1461         children.append(self.body)  1462         children.extend(flatten(self.handlers))  1463         children.append(self.else_)  1464         return tuple(children)  1465   1466     def getChildNodes(self):  1467         nodelist = []  1468         nodelist.append(self.body)  1469         nodelist.extend(flatten_nodes(self.handlers))  1470         if self.else_ is not None:  1471             nodelist.append(self.else_)  1472         return tuple(nodelist)  1473   1474     def __repr__(self):  1475         return "TryExcept(%r, %r, %r)" % (self.body, self.handlers, self.else_)  1476   1477     def __str__(self):  1478         handlers = [  1479             ("\nexcept%s%s:%s" % (spec and " %s" % spec or "", assign and ", %s" % assign or "", indent("\n%s" % statement)))  1480             for (spec, assign, statement) in self.handlers  1481             ]  1482   1483         return "try:%s%s%s" % (  1484             indent("\n%s" % self.body),  1485             "".join(handlers),  1486             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""  1487             )  1488   1489 class TryFinally(Node):  1490     def __init__(self, body, final, lineno=None):  1491         self.body = body  1492         self.final = final  1493         self.lineno = lineno  1494   1495     def getChildren(self):  1496         return self.body, self.final  1497   1498     def getChildNodes(self):  1499         return self.body, self.final  1500   1501     def __repr__(self):  1502         return "TryFinally(%r, %r)" % (self.body, self.final)  1503   1504     def __str__(self):  1505         return "try:%s\nfinally:%s" % (  1506             indent("\n%s" % self.body),  1507             indent("\n%s" % self.final)  1508             )  1509   1510 class Tuple(Node):  1511     def __init__(self, nodes, lineno=None):  1512         self.nodes = nodes  1513         self.lineno = lineno  1514   1515     def getChildren(self):  1516         return tuple(flatten(self.nodes))  1517   1518     def getChildNodes(self):  1519         nodelist = []  1520         nodelist.extend(flatten_nodes(self.nodes))  1521         return tuple(nodelist)  1522   1523     def __repr__(self):  1524         return "Tuple(%r)" % (self.nodes,)  1525   1526     def __str__(self):  1527         return "(%s)" % ", ".join(map(str, self.nodes))  1528   1529 class UnaryAdd(Node, Operator):  1530     def __init__(self, expr, lineno=None):  1531         self.expr = expr  1532         self.lineno = lineno  1533   1534     def getChildren(self):  1535         return self.expr,  1536   1537     def getChildNodes(self):  1538         return self.expr,  1539   1540     def __repr__(self):  1541         return "UnaryAdd(%r)" % (self.expr,)  1542   1543     def __str__(self):  1544         return "+%s" % self.expr  1545   1546 class UnarySub(Node, Operator):  1547     def __init__(self, expr, lineno=None):  1548         self.expr = expr  1549         self.lineno = lineno  1550   1551     def getChildren(self):  1552         return self.expr,  1553   1554     def getChildNodes(self):  1555         return self.expr,  1556   1557     def __repr__(self):  1558         return "UnarySub(%r)" % (self.expr,)  1559   1560     def __str__(self):  1561         return "-%s" % self.expr  1562   1563 class While(Node):  1564     def __init__(self, test, body, else_, lineno=None):  1565         self.test = test  1566         self.body = body  1567         self.else_ = else_  1568         self.lineno = lineno  1569   1570     def getChildren(self):  1571         children = []  1572         children.append(self.test)  1573         children.append(self.body)  1574         children.append(self.else_)  1575         return tuple(children)  1576   1577     def getChildNodes(self):  1578         nodelist = []  1579         nodelist.append(self.test)  1580         nodelist.append(self.body)  1581         if self.else_ is not None:  1582             nodelist.append(self.else_)  1583         return tuple(nodelist)  1584   1585     def __repr__(self):  1586         return "While(%r, %r, %r)" % (self.test, self.body, self.else_)  1587   1588     def __str__(self):  1589         return "while %s:%s%s" % (  1590             self.test,  1591             indent("\n%s" % self.body),  1592             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""  1593             )  1594   1595 class With(Node):  1596     def __init__(self, expr, vars, body, lineno=None):  1597         self.expr = expr  1598         self.vars = vars  1599         self.body = body  1600         self.lineno = lineno  1601   1602     def getChildren(self):  1603         children = []  1604         children.append(self.expr)  1605         children.append(self.vars)  1606         children.append(self.body)  1607         return tuple(children)  1608   1609     def getChildNodes(self):  1610         nodelist = []  1611         nodelist.append(self.expr)  1612         if self.vars is not None:  1613             nodelist.append(self.vars)  1614         nodelist.append(self.body)  1615         return tuple(nodelist)  1616   1617     def __repr__(self):  1618         return "With(%r, %r, %r)" % (self.expr, self.vars, self.body)  1619   1620     def __str__(self):  1621         return "with %s%s:%s" % (  1622             self.expr,  1623             self.vars and " as %s" % ", ".join(map(str, self.vars)),  1624             indent("\n%s" % self.body),  1625             )  1626   1627 for name, obj in globals().items():  1628     if isinstance(obj, type) and issubclass(obj, Node):  1629         nodes[name.lower()] = obj