Lichen

compiler/ast.py

934:2989aab1b4f7
13 months ago Paul Boddie Renamed the utf8string class to unicode, eliminating the unicode function. This means that the simple case of merely returning an object if it is already a Unicode object no longer occurs when using the unicode callable, but such behaviour might be better supported with more general customised instantiation functionality.
     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, privileged=False):   872         self.expr = expr   873         self.attrname = attrname   874         self.lineno = lineno   875    876         # Support privileged internal accesses.   877    878         self.privileged = privileged   879    880     def getChildren(self):   881         return self.expr, self.attrname   882    883     def getChildNodes(self):   884         return self.expr,   885    886     def __repr__(self):   887         return "Getattr(%r, %r)" % (self.expr, self.attrname)   888    889     def __str__(self):   890         return "%s.%s" % (self.expr, self.attrname)   891    892 class Global(Node):   893     def __init__(self, names, lineno=None):   894         self.names = names   895         self.lineno = lineno   896    897     def getChildren(self):   898         return self.names,   899    900     def getChildNodes(self):   901         return ()   902    903     def __repr__(self):   904         return "Global(%r)" % (self.names,)   905    906     def __str__(self):   907         return "global %s" % ", ".join(map(str, self.names))   908    909 class If(Node):   910     def __init__(self, tests, else_, lineno=None):   911         self.tests = tests   912         self.else_ = else_   913         self.lineno = lineno   914    915     def getChildren(self):   916         children = []   917         children.extend(flatten(self.tests))   918         children.append(self.else_)   919         return tuple(children)   920    921     def getChildNodes(self):   922         nodelist = []   923         nodelist.extend(flatten_nodes(self.tests))   924         if self.else_ is not None:   925             nodelist.append(self.else_)   926         return tuple(nodelist)   927    928     def __repr__(self):   929         return "If(%r, %r)" % (self.tests, self.else_)   930    931     def __str__(self):   932         tests = [("%sif %s:%s" % (i > 0 and "el" or "", test, indent("\n%s" % body))) for (i, (test, body)) in enumerate(self.tests)]   933         return "%s%s" % (   934             "\n".join(tests),   935             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""   936             )   937    938 class Import(Node):   939     def __init__(self, names, lineno=None):   940         self.names = names   941         self.lineno = lineno   942    943     def getChildren(self):   944         return self.names,   945    946     def getChildNodes(self):   947         return ()   948    949     def __repr__(self):   950         return "Import(%r)" % (self.names,)   951    952     def __str__(self):   953         return "import %s" % (   954             ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names]))   955    956 class Invert(Node, Operator):   957     def __init__(self, expr, lineno=None):   958         self.expr = expr   959         self.lineno = lineno   960    961     def getChildren(self):   962         return self.expr,   963    964     def getChildNodes(self):   965         return self.expr,   966    967     def __repr__(self):   968         return "Invert(%r)" % (self.expr,)   969    970     def __str__(self):   971         return "~%s" % self.expr   972    973 class Keyword(Node):   974     def __init__(self, name, expr, lineno=None):   975         self.name = name   976         self.expr = expr   977         self.lineno = lineno   978    979     def getChildren(self):   980         return self.name, self.expr   981    982     def getChildNodes(self):   983         return self.expr,   984    985     def __repr__(self):   986         return "Keyword(%r, %r)" % (self.name, self.expr)   987    988     def __str__(self):   989         return "%s=%s" % (self.name, self.expr)   990    991 class Lambda(Node):   992     def __init__(self, argnames, defaults, flags, code, lineno=None):   993         self.argnames = argnames   994         self.defaults = defaults   995         self.flags = flags   996         self.code = code   997         self.lineno = lineno   998         self.varargs = self.kwargs = None   999         if flags & CO_VARARGS:  1000             self.varargs = 1  1001         if flags & CO_VARKEYWORDS:  1002             self.kwargs = 1  1003   1004     def getChildren(self):  1005         children = []  1006         children.append(self.argnames)  1007         children.extend(flatten(self.defaults))  1008         children.append(self.flags)  1009         children.append(self.code)  1010         return tuple(children)  1011   1012     def getChildNodes(self):  1013         nodelist = []  1014         nodelist.extend(flatten_nodes(self.defaults))  1015         nodelist.append(self.code)  1016         return tuple(nodelist)  1017   1018     def __repr__(self):  1019         return "Lambda(%r, %r, %r, %r)" % (self.argnames, self.defaults, self.flags, self.code)  1020   1021     def __str__(self):  1022         parameters = decode_function(self)  1023         return "lambda %s: %s" % (", ".join(parameters), self.code)  1024   1025 class LeftShift(Node, Operator):  1026     def __init__(self, leftright, lineno=None):  1027         self.left = leftright[0]  1028         self.right = leftright[1]  1029         self.lineno = lineno  1030   1031     def getChildren(self):  1032         return self.left, self.right  1033   1034     def getChildNodes(self):  1035         return self.left, self.right  1036   1037     def __repr__(self):  1038         return "LeftShift((%r, %r))" % (self.left, self.right)  1039   1040     def __str__(self):  1041         return "(%s << %s)" % (self.left, self.right)  1042   1043 class List(Node):  1044     def __init__(self, nodes, lineno=None):  1045         self.nodes = nodes  1046         self.lineno = lineno  1047   1048     def getChildren(self):  1049         return tuple(flatten(self.nodes))  1050   1051     def getChildNodes(self):  1052         nodelist = []  1053         nodelist.extend(flatten_nodes(self.nodes))  1054         return tuple(nodelist)  1055   1056     def __repr__(self):  1057         return "List(%r)" % (self.nodes,)  1058   1059     def __str__(self):  1060         return "[%s]" % ", ".join(map(str, self.nodes))  1061   1062 class Mod(Node, Operator):  1063     def __init__(self, leftright, lineno=None):  1064         self.left = leftright[0]  1065         self.right = leftright[1]  1066         self.lineno = lineno  1067   1068     def getChildren(self):  1069         return self.left, self.right  1070   1071     def getChildNodes(self):  1072         return self.left, self.right  1073   1074     def __repr__(self):  1075         return "Mod((%r, %r))" % (self.left, self.right)  1076   1077     def __str__(self):  1078         return "(%s %% %s)" % (self.left, self.right)  1079   1080 class Module(Node):  1081     def __init__(self, doc, node, lineno=None):  1082         self.doc = doc  1083         self.node = node  1084         self.lineno = lineno  1085   1086     def getChildren(self):  1087         return self.doc, self.node  1088   1089     def getChildNodes(self):  1090         return self.node,  1091   1092     def __repr__(self):  1093         return "Module(%r, %r)" % (self.doc, self.node)  1094   1095     def __str__(self):  1096         return "%s%s" % (self.doc and "%s\n\n" % docstring(self.doc) or "", self.node)  1097   1098 class Mul(Node, Operator):  1099     def __init__(self, leftright, lineno=None):  1100         self.left = leftright[0]  1101         self.right = leftright[1]  1102         self.lineno = lineno  1103   1104     def getChildren(self):  1105         return self.left, self.right  1106   1107     def getChildNodes(self):  1108         return self.left, self.right  1109   1110     def __repr__(self):  1111         return "Mul((%r, %r))" % (self.left, self.right)  1112   1113     def __str__(self):  1114         return "(%s * %s)" % (self.left, self.right)  1115   1116 class Name(Node):  1117     def __init__(self, name, lineno=None):  1118         self.name = name  1119         self.lineno = lineno  1120   1121     def getChildren(self):  1122         return self.name,  1123   1124     def getChildNodes(self):  1125         return ()  1126   1127     def __repr__(self):  1128         return "Name(%r)" % (self.name,)  1129   1130     def __str__(self):  1131         return str(self.name)  1132   1133 class Not(Node):  1134     def __init__(self, expr, lineno=None):  1135         self.expr = expr  1136         self.lineno = lineno  1137   1138     def getChildren(self):  1139         return self.expr,  1140   1141     def getChildNodes(self):  1142         return self.expr,  1143   1144     def __repr__(self):  1145         return "Not(%r)" % (self.expr,)  1146   1147     def __str__(self):  1148         return "not %s" % self.expr  1149   1150 class Or(Node):  1151     def __init__(self, nodes, lineno=None):  1152         self.nodes = nodes  1153         self.lineno = lineno  1154   1155     def getChildren(self):  1156         return tuple(flatten(self.nodes))  1157   1158     def getChildNodes(self):  1159         nodelist = []  1160         nodelist.extend(flatten_nodes(self.nodes))  1161         return tuple(nodelist)  1162   1163     def __repr__(self):  1164         return "Or(%r)" % (self.nodes,)  1165   1166     def __str__(self):  1167         return "(%s)" % " or ".join(map(str, self.nodes))  1168   1169 class Pass(Node):  1170     def __init__(self, lineno=None):  1171         self.lineno = lineno  1172   1173     def getChildren(self):  1174         return ()  1175   1176     def getChildNodes(self):  1177         return ()  1178   1179     def __repr__(self):  1180         return "Pass()"  1181   1182     def __str__(self):  1183         return "pass"  1184   1185 class Power(Node, Operator):  1186     def __init__(self, leftright, lineno=None):  1187         self.left = leftright[0]  1188         self.right = leftright[1]  1189         self.lineno = lineno  1190   1191     def getChildren(self):  1192         return self.left, self.right  1193   1194     def getChildNodes(self):  1195         return self.left, self.right  1196   1197     def __repr__(self):  1198         return "Power((%r, %r))" % (self.left, self.right)  1199   1200     def __str__(self):  1201         return "(%s ** %s)" % (self.left, self.right)  1202   1203 class Print(Node):  1204     def __init__(self, nodes, dest, lineno=None):  1205         self.nodes = nodes  1206         self.dest = dest  1207         self.lineno = lineno  1208   1209     def getChildren(self):  1210         children = []  1211         children.extend(flatten(self.nodes))  1212         children.append(self.dest)  1213         return tuple(children)  1214   1215     def getChildNodes(self):  1216         nodelist = []  1217         nodelist.extend(flatten_nodes(self.nodes))  1218         if self.dest is not None:  1219             nodelist.append(self.dest)  1220         return tuple(nodelist)  1221   1222     def __repr__(self):  1223         return "Print(%r, %r)" % (self.nodes, self.dest)  1224   1225     def __str__(self):  1226         dest = self.dest and [">>%s" % self.dest] or []  1227         return "print %s," % ", ".join(map(str, dest + self.nodes))  1228   1229 class Printnl(Node):  1230     def __init__(self, nodes, dest, lineno=None):  1231         self.nodes = nodes  1232         self.dest = dest  1233         self.lineno = lineno  1234   1235     def getChildren(self):  1236         children = []  1237         children.extend(flatten(self.nodes))  1238         children.append(self.dest)  1239         return tuple(children)  1240   1241     def getChildNodes(self):  1242         nodelist = []  1243         nodelist.extend(flatten_nodes(self.nodes))  1244         if self.dest is not None:  1245             nodelist.append(self.dest)  1246         return tuple(nodelist)  1247   1248     def __repr__(self):  1249         return "Printnl(%r, %r)" % (self.nodes, self.dest)  1250   1251     def __str__(self):  1252         dest = self.dest and [">>%s" % self.dest] or []  1253         return "print %s" % ", ".join(map(str, dest + self.nodes))  1254   1255 class Raise(Node):  1256     def __init__(self, expr1, expr2=None, expr3=None, lineno=None):  1257         self.expr1 = expr1  1258         self.expr2 = expr2  1259         self.expr3 = expr3  1260         self.lineno = lineno  1261   1262     def getChildren(self):  1263         children = []  1264         children.append(self.expr1)  1265         children.append(self.expr2)  1266         children.append(self.expr3)  1267         return tuple(children)  1268   1269     def getChildNodes(self):  1270         nodelist = []  1271         if self.expr1 is not None:  1272             nodelist.append(self.expr1)  1273         if self.expr2 is not None:  1274             nodelist.append(self.expr2)  1275         if self.expr3 is not None:  1276             nodelist.append(self.expr3)  1277         return tuple(nodelist)  1278   1279     def __repr__(self):  1280         return "Raise(%r, %r, %r)" % (self.expr1, self.expr2, self.expr3)  1281   1282     def __str__(self):  1283         args = self.expr1 and [self.expr1] or []  1284         args += self.expr2 and [self.expr2] or []  1285         args += self.expr3 and [self.expr3] or []  1286         return "raise %s" % ", ".join(map(str, args))  1287   1288 class Return(Node):  1289     def __init__(self, value, lineno=None):  1290         self.value = value  1291         self.lineno = lineno  1292   1293     def getChildren(self):  1294         return self.value,  1295   1296     def getChildNodes(self):  1297         return self.value,  1298   1299     def __repr__(self):  1300         return "Return(%r)" % (self.value,)  1301   1302     def __str__(self):  1303         return "return %s" % self.value  1304   1305 class RightShift(Node, Operator):  1306     def __init__(self, leftright, lineno=None):  1307         self.left = leftright[0]  1308         self.right = leftright[1]  1309         self.lineno = lineno  1310   1311     def getChildren(self):  1312         return self.left, self.right  1313   1314     def getChildNodes(self):  1315         return self.left, self.right  1316   1317     def __repr__(self):  1318         return "RightShift((%r, %r))" % (self.left, self.right)  1319   1320     def __str__(self):  1321         return "(%s >> %s)" % (self.left, self.right)  1322   1323 class Set(Node):  1324     def __init__(self, nodes, lineno=None):  1325         self.nodes = nodes  1326         self.lineno = lineno  1327   1328     def getChildren(self):  1329         return tuple(flatten(self.nodes))  1330   1331     def getChildNodes(self):  1332         nodelist = []  1333         nodelist.extend(flatten_nodes(self.nodes))  1334         return tuple(nodelist)  1335   1336     def __repr__(self):  1337         return "Set(%r)" % (self.nodes,)  1338   1339     def __str__(self):  1340         return "{%s}" % ", ".join(map(str, self.nodes))  1341   1342 class Slice(Node, OperatorUser):  1343     def __init__(self, expr, flags, lower, upper, lineno=None):  1344         self.expr = expr  1345         self.flags = flags  1346         self.lower = lower  1347         self.upper = upper  1348         self.lineno = lineno  1349   1350     def getChildren(self):  1351         children = []  1352         children.append(self.expr)  1353         children.append(self.flags)  1354         children.append(self.lower)  1355         children.append(self.upper)  1356         return tuple(children)  1357   1358     def getChildNodes(self):  1359         nodelist = []  1360         nodelist.append(self.expr)  1361         if self.lower is not None:  1362             nodelist.append(self.lower)  1363         if self.upper is not None:  1364             nodelist.append(self.upper)  1365         return tuple(nodelist)  1366   1367     def __repr__(self):  1368         return "Slice(%r, %r, %r, %r)" % (self.expr, self.flags, self.lower, self.upper)  1369   1370     def __str__(self):  1371         args = [self.lower or "", self.upper or ""]  1372         return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ":".join(map(str, args)))  1373   1374 class Sliceobj(Node):  1375     def __init__(self, nodes, lineno=None):  1376         self.nodes = nodes  1377         self.lineno = lineno  1378   1379     def getChildren(self):  1380         return tuple(flatten(self.nodes))  1381   1382     def getChildNodes(self):  1383         nodelist = []  1384         nodelist.extend(flatten_nodes(self.nodes))  1385         return tuple(nodelist)  1386   1387     def __repr__(self):  1388         return "Sliceobj(%r)" % (self.nodes,)  1389   1390     def __str__(self):  1391         return ":".join(map(str, self.nodes))  1392   1393 class Stmt(Node):  1394     def __init__(self, nodes, lineno=None):  1395         self.nodes = nodes  1396         self.lineno = lineno  1397   1398     def getChildren(self):  1399         return tuple(flatten(self.nodes))  1400   1401     def getChildNodes(self):  1402         nodelist = []  1403         nodelist.extend(flatten_nodes(self.nodes))  1404         return tuple(nodelist)  1405   1406     def __repr__(self):  1407         return "Stmt(%r)" % (self.nodes,)  1408   1409     def __str__(self):  1410         return "\n".join(map(str, flatten_statement(self.nodes)))  1411   1412 class Sub(Node, Operator):  1413     def __init__(self, leftright, lineno=None):  1414         self.left = leftright[0]  1415         self.right = leftright[1]  1416         self.lineno = lineno  1417   1418     def getChildren(self):  1419         return self.left, self.right  1420   1421     def getChildNodes(self):  1422         return self.left, self.right  1423   1424     def __repr__(self):  1425         return "Sub((%r, %r))" % (self.left, self.right)  1426   1427     def __str__(self):  1428         return "(%s - %s)" % (self.left, self.right)  1429   1430 class Subscript(Node, OperatorUser):  1431     def __init__(self, expr, flags, subs, lineno=None):  1432         self.expr = expr  1433         self.flags = flags  1434         self.subs = subs  1435         self.lineno = lineno  1436   1437     def getChildren(self):  1438         children = []  1439         children.append(self.expr)  1440         children.append(self.flags)  1441         children.extend(flatten(self.subs))  1442         return tuple(children)  1443   1444     def getChildNodes(self):  1445         nodelist = []  1446         nodelist.append(self.expr)  1447         nodelist.extend(flatten_nodes(self.subs))  1448         return tuple(nodelist)  1449   1450     def __repr__(self):  1451         return "Subscript(%r, %r, %r)" % (self.expr, self.flags, self.subs)  1452   1453     def __str__(self):  1454         return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ",".join(map(str, self.subs)))  1455   1456 class TryExcept(Node):  1457     def __init__(self, body, handlers, else_, lineno=None):  1458         self.body = body  1459         self.handlers = handlers  1460         self.else_ = else_  1461         self.lineno = lineno  1462   1463     def getChildren(self):  1464         children = []  1465         children.append(self.body)  1466         children.extend(flatten(self.handlers))  1467         children.append(self.else_)  1468         return tuple(children)  1469   1470     def getChildNodes(self):  1471         nodelist = []  1472         nodelist.append(self.body)  1473         nodelist.extend(flatten_nodes(self.handlers))  1474         if self.else_ is not None:  1475             nodelist.append(self.else_)  1476         return tuple(nodelist)  1477   1478     def __repr__(self):  1479         return "TryExcept(%r, %r, %r)" % (self.body, self.handlers, self.else_)  1480   1481     def __str__(self):  1482         handlers = [  1483             ("\nexcept%s%s:%s" % (spec and " %s" % spec or "", assign and ", %s" % assign or "", indent("\n%s" % statement)))  1484             for (spec, assign, statement) in self.handlers  1485             ]  1486   1487         return "try:%s%s%s" % (  1488             indent("\n%s" % self.body),  1489             "".join(handlers),  1490             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""  1491             )  1492   1493 class TryFinally(Node):  1494     def __init__(self, body, final, lineno=None):  1495         self.body = body  1496         self.final = final  1497         self.lineno = lineno  1498   1499     def getChildren(self):  1500         return self.body, self.final  1501   1502     def getChildNodes(self):  1503         return self.body, self.final  1504   1505     def __repr__(self):  1506         return "TryFinally(%r, %r)" % (self.body, self.final)  1507   1508     def __str__(self):  1509         return "try:%s\nfinally:%s" % (  1510             indent("\n%s" % self.body),  1511             indent("\n%s" % self.final)  1512             )  1513   1514 class Tuple(Node):  1515     def __init__(self, nodes, lineno=None):  1516         self.nodes = nodes  1517         self.lineno = lineno  1518   1519     def getChildren(self):  1520         return tuple(flatten(self.nodes))  1521   1522     def getChildNodes(self):  1523         nodelist = []  1524         nodelist.extend(flatten_nodes(self.nodes))  1525         return tuple(nodelist)  1526   1527     def __repr__(self):  1528         return "Tuple(%r)" % (self.nodes,)  1529   1530     def __str__(self):  1531         return "(%s)" % ", ".join(map(str, self.nodes))  1532   1533 class UnaryAdd(Node, Operator):  1534     def __init__(self, expr, lineno=None):  1535         self.expr = expr  1536         self.lineno = lineno  1537   1538     def getChildren(self):  1539         return self.expr,  1540   1541     def getChildNodes(self):  1542         return self.expr,  1543   1544     def __repr__(self):  1545         return "UnaryAdd(%r)" % (self.expr,)  1546   1547     def __str__(self):  1548         return "+%s" % self.expr  1549   1550 class UnarySub(Node, Operator):  1551     def __init__(self, expr, lineno=None):  1552         self.expr = expr  1553         self.lineno = lineno  1554   1555     def getChildren(self):  1556         return self.expr,  1557   1558     def getChildNodes(self):  1559         return self.expr,  1560   1561     def __repr__(self):  1562         return "UnarySub(%r)" % (self.expr,)  1563   1564     def __str__(self):  1565         return "-%s" % self.expr  1566   1567 class While(Node):  1568     def __init__(self, test, body, else_, lineno=None):  1569         self.test = test  1570         self.body = body  1571         self.else_ = else_  1572         self.lineno = lineno  1573   1574     def getChildren(self):  1575         children = []  1576         children.append(self.test)  1577         children.append(self.body)  1578         children.append(self.else_)  1579         return tuple(children)  1580   1581     def getChildNodes(self):  1582         nodelist = []  1583         nodelist.append(self.test)  1584         nodelist.append(self.body)  1585         if self.else_ is not None:  1586             nodelist.append(self.else_)  1587         return tuple(nodelist)  1588   1589     def __repr__(self):  1590         return "While(%r, %r, %r)" % (self.test, self.body, self.else_)  1591   1592     def __str__(self):  1593         return "while %s:%s%s" % (  1594             self.test,  1595             indent("\n%s" % self.body),  1596             self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or ""  1597             )  1598   1599 for name, obj in globals().items():  1600     if isinstance(obj, type) and issubclass(obj, Node):  1601         nodes[name.lower()] = obj