summaryrefslogtreecommitdiffstats
path: root/Lib/compiler/ast.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2004-09-07 15:28:01 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2004-09-07 15:28:01 (GMT)
commit566d934745378df72fbbf7d3f834e7d08f7e42d1 (patch)
tree495591bd3b0dfcc198f36c5a904dae9cac617036 /Lib/compiler/ast.py
parent2ad68e69b975b1714d3da1cd5e419c8294a5f2c1 (diff)
downloadcpython-566d934745378df72fbbf7d3f834e7d08f7e42d1.zip
cpython-566d934745378df72fbbf7d3f834e7d08f7e42d1.tar.gz
cpython-566d934745378df72fbbf7d3f834e7d08f7e42d1.tar.bz2
compiler.transformer: correct lineno attribute when possible
SF patch #1015989 The basic idea of this patch is to compute lineno attributes for all AST nodes. The actual implementation lead to a lot of restructing and code cleanup. The generated AST nodes now have an optional lineno argument to constructor. Remove the top-level asList(), since it didn't seem to serve any purpose. Add an __iter__ to ast nodes. Use isinstance() instead of explicit type tests. Change transformer to use the new lineno attribute, which replaces three lines of code with one. Use universal newlines so that we can get rid of special-case code for line endings. Use lookup_node() in a few more frequently called, but simple com_xxx methods(). Change string exception to class exception.
Diffstat (limited to 'Lib/compiler/ast.py')
-rw-r--r--Lib/compiler/ast.py434
1 files changed, 205 insertions, 229 deletions
diff --git a/Lib/compiler/ast.py b/Lib/compiler/ast.py
index 6f585b7..cfc2a51 100644
--- a/Lib/compiler/ast.py
+++ b/Lib/compiler/ast.py
@@ -1,15 +1,14 @@
"""Python abstract syntax node definitions
-This file is automatically generated.
+This file is automatically generated by Tools/compiler/astgen.py
"""
-from types import TupleType, ListType
from consts import CO_VARARGS, CO_VARKEYWORDS
def flatten(list):
l = []
for elt in list:
t = type(elt)
- if t is TupleType or t is ListType:
+ if t is tuple or t is list:
for elt2 in flatten(elt):
l.append(elt2)
else:
@@ -19,29 +18,17 @@ def flatten(list):
def flatten_nodes(list):
return [n for n in flatten(list) if isinstance(n, Node)]
-def asList(nodearg):
- l = []
- for item in nodearg:
- if hasattr(item, "asList"):
- l.append(item.asList())
- else:
- t = type(item)
- if t is TupleType or t is ListType:
- l.append(tuple(asList(item)))
- else:
- l.append(item)
- return l
-
nodes = {}
-class Node: # an abstract base class
- lineno = None # provide a lineno for nodes that don't have one
- def getType(self):
- pass # implemented by subclass
+class Node:
+ """Abstract base class for ast nodes."""
def getChildren(self):
pass # implemented by subclasses
- def asList(self):
- return tuple(asList(self.getChildren()))
+ def __iter__(self):
+ for n in self.getChildren():
+ yield n
+ def asList(self): # for backwards compatibility
+ return self.getChildren()
def getChildNodes(self):
pass # implemented by subclasses
@@ -64,10 +51,10 @@ class Expression(Node):
return "Expression(%s)" % (repr(self.node))
class Add(Node):
- nodes["add"] = "Add"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -79,14 +66,12 @@ class Add(Node):
return "Add((%s, %s))" % (repr(self.left), repr(self.right))
class And(Node):
- nodes["and"] = "And"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -97,11 +82,11 @@ class And(Node):
return "And(%s)" % (repr(self.nodes),)
class AssAttr(Node):
- nodes["assattr"] = "AssAttr"
- def __init__(self, expr, attrname, flags):
+ def __init__(self, expr, attrname, flags, lineno=None):
self.expr = expr
self.attrname = attrname
self.flags = flags
+ self.lineno = lineno
def getChildren(self):
return self.expr, self.attrname, self.flags
@@ -113,14 +98,12 @@ class AssAttr(Node):
return "AssAttr(%s, %s, %s)" % (repr(self.expr), repr(self.attrname), repr(self.flags))
class AssList(Node):
- nodes["asslist"] = "AssList"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -131,10 +114,10 @@ class AssList(Node):
return "AssList(%s)" % (repr(self.nodes),)
class AssName(Node):
- nodes["assname"] = "AssName"
- def __init__(self, name, flags):
+ def __init__(self, name, flags, lineno=None):
self.name = name
self.flags = flags
+ self.lineno = lineno
def getChildren(self):
return self.name, self.flags
@@ -146,14 +129,12 @@ class AssName(Node):
return "AssName(%s, %s)" % (repr(self.name), repr(self.flags))
class AssTuple(Node):
- nodes["asstuple"] = "AssTuple"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -164,10 +145,10 @@ class AssTuple(Node):
return "AssTuple(%s)" % (repr(self.nodes),)
class Assert(Node):
- nodes["assert"] = "Assert"
- def __init__(self, test, fail):
+ def __init__(self, test, fail, lineno=None):
self.test = test
self.fail = fail
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -178,17 +159,18 @@ class Assert(Node):
def getChildNodes(self):
nodelist = []
nodelist.append(self.test)
- if self.fail is not None: nodelist.append(self.fail)
+ if self.fail is not None:
+ nodelist.append(self.fail)
return tuple(nodelist)
def __repr__(self):
return "Assert(%s, %s)" % (repr(self.test), repr(self.fail))
class Assign(Node):
- nodes["assign"] = "Assign"
- def __init__(self, nodes, expr):
+ def __init__(self, nodes, expr, lineno=None):
self.nodes = nodes
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -206,11 +188,11 @@ class Assign(Node):
return "Assign(%s, %s)" % (repr(self.nodes), repr(self.expr))
class AugAssign(Node):
- nodes["augassign"] = "AugAssign"
- def __init__(self, node, op, expr):
+ def __init__(self, node, op, expr, lineno=None):
self.node = node
self.op = op
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.node, self.op, self.expr
@@ -222,9 +204,9 @@ class AugAssign(Node):
return "AugAssign(%s, %s, %s)" % (repr(self.node), repr(self.op), repr(self.expr))
class Backquote(Node):
- nodes["backquote"] = "Backquote"
- def __init__(self, expr):
+ def __init__(self, expr, lineno=None):
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.expr,
@@ -236,14 +218,12 @@ class Backquote(Node):
return "Backquote(%s)" % (repr(self.expr),)
class Bitand(Node):
- nodes["bitand"] = "Bitand"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -254,14 +234,12 @@ class Bitand(Node):
return "Bitand(%s)" % (repr(self.nodes),)
class Bitor(Node):
- nodes["bitor"] = "Bitor"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -272,14 +250,12 @@ class Bitor(Node):
return "Bitor(%s)" % (repr(self.nodes),)
class Bitxor(Node):
- nodes["bitxor"] = "Bitxor"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -290,9 +266,8 @@ class Bitxor(Node):
return "Bitxor(%s)" % (repr(self.nodes),)
class Break(Node):
- nodes["break"] = "Break"
- def __init__(self, ):
- pass
+ def __init__(self, lineno=None):
+ self.lineno = lineno
def getChildren(self):
return ()
@@ -304,12 +279,12 @@ class Break(Node):
return "Break()"
class CallFunc(Node):
- nodes["callfunc"] = "CallFunc"
- def __init__(self, node, args, star_args = None, dstar_args = None):
+ def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None):
self.node = node
self.args = args
self.star_args = star_args
self.dstar_args = dstar_args
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -323,20 +298,22 @@ class CallFunc(Node):
nodelist = []
nodelist.append(self.node)
nodelist.extend(flatten_nodes(self.args))
- if self.star_args is not None: nodelist.append(self.star_args)
- if self.dstar_args is not None: nodelist.append(self.dstar_args)
+ if self.star_args is not None:
+ nodelist.append(self.star_args)
+ if self.dstar_args is not None:
+ nodelist.append(self.dstar_args)
return tuple(nodelist)
def __repr__(self):
return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
class Class(Node):
- nodes["class"] = "Class"
- def __init__(self, name, bases, doc, code):
+ def __init__(self, name, bases, doc, code, lineno=None):
self.name = name
self.bases = bases
self.doc = doc
self.code = code
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -356,10 +333,10 @@ class Class(Node):
return "Class(%s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code))
class Compare(Node):
- nodes["compare"] = "Compare"
- def __init__(self, expr, ops):
+ def __init__(self, expr, ops, lineno=None):
self.expr = expr
self.ops = ops
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -377,9 +354,9 @@ class Compare(Node):
return "Compare(%s, %s)" % (repr(self.expr), repr(self.ops))
class Const(Node):
- nodes["const"] = "Const"
- def __init__(self, value):
+ def __init__(self, value, lineno=None):
self.value = value
+ self.lineno = lineno
def getChildren(self):
return self.value,
@@ -391,9 +368,8 @@ class Const(Node):
return "Const(%s)" % (repr(self.value),)
class Continue(Node):
- nodes["continue"] = "Continue"
- def __init__(self, ):
- pass
+ def __init__(self, lineno=None):
+ self.lineno = lineno
def getChildren(self):
return ()
@@ -405,28 +381,28 @@ class Continue(Node):
return "Continue()"
class Decorators(Node):
- nodes["decorators"] = "Decorators"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
return tuple(flatten(self.nodes))
def getChildNodes(self):
- return flatten_nodes(self.nodes)
+ nodelist = []
+ nodelist.extend(flatten_nodes(self.nodes))
+ return tuple(nodelist)
def __repr__(self):
return "Decorators(%s)" % (repr(self.nodes),)
class Dict(Node):
- nodes["dict"] = "Dict"
- def __init__(self, items):
+ def __init__(self, items, lineno=None):
self.items = items
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.items))
- return tuple(children)
+ return tuple(flatten(self.items))
def getChildNodes(self):
nodelist = []
@@ -437,9 +413,9 @@ class Dict(Node):
return "Dict(%s)" % (repr(self.items),)
class Discard(Node):
- nodes["discard"] = "Discard"
- def __init__(self, expr):
+ def __init__(self, expr, lineno=None):
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.expr,
@@ -451,10 +427,10 @@ class Discard(Node):
return "Discard(%s)" % (repr(self.expr),)
class Div(Node):
- nodes["div"] = "Div"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -466,9 +442,8 @@ class Div(Node):
return "Div((%s, %s))" % (repr(self.left), repr(self.right))
class Ellipsis(Node):
- nodes["ellipsis"] = "Ellipsis"
- def __init__(self, ):
- pass
+ def __init__(self, lineno=None):
+ self.lineno = lineno
def getChildren(self):
return ()
@@ -480,11 +455,11 @@ class Ellipsis(Node):
return "Ellipsis()"
class Exec(Node):
- nodes["exec"] = "Exec"
- def __init__(self, expr, locals, globals):
+ def __init__(self, expr, locals, globals, lineno=None):
self.expr = expr
self.locals = locals
self.globals = globals
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -496,18 +471,20 @@ class Exec(Node):
def getChildNodes(self):
nodelist = []
nodelist.append(self.expr)
- if self.locals is not None: nodelist.append(self.locals)
- if self.globals is not None: nodelist.append(self.globals)
+ if self.locals is not None:
+ nodelist.append(self.locals)
+ if self.globals is not None:
+ nodelist.append(self.globals)
return tuple(nodelist)
def __repr__(self):
return "Exec(%s, %s, %s)" % (repr(self.expr), repr(self.locals), repr(self.globals))
class FloorDiv(Node):
- nodes["floordiv"] = "FloorDiv"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -519,12 +496,12 @@ class FloorDiv(Node):
return "FloorDiv((%s, %s))" % (repr(self.left), repr(self.right))
class For(Node):
- nodes["for"] = "For"
- def __init__(self, assign, list, body, else_):
+ def __init__(self, assign, list, body, else_, lineno=None):
self.assign = assign
self.list = list
self.body = body
self.else_ = else_
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -539,17 +516,18 @@ class For(Node):
nodelist.append(self.assign)
nodelist.append(self.list)
nodelist.append(self.body)
- if self.else_ is not None: nodelist.append(self.else_)
+ if self.else_ is not None:
+ nodelist.append(self.else_)
return tuple(nodelist)
def __repr__(self):
return "For(%s, %s, %s, %s)" % (repr(self.assign), repr(self.list), repr(self.body), repr(self.else_))
class From(Node):
- nodes["from"] = "From"
- def __init__(self, modname, names):
+ def __init__(self, modname, names, lineno=None):
self.modname = modname
self.names = names
+ self.lineno = lineno
def getChildren(self):
return self.modname, self.names
@@ -561,8 +539,7 @@ class From(Node):
return "From(%s, %s)" % (repr(self.modname), repr(self.names))
class Function(Node):
- nodes["function"] = "Function"
- def __init__(self, decorators, name, argnames, defaults, flags, doc, code):
+ def __init__(self, decorators, name, argnames, defaults, flags, doc, code, lineno=None):
self.decorators = decorators
self.name = name
self.argnames = argnames
@@ -570,18 +547,18 @@ class Function(Node):
self.flags = flags
self.doc = doc
self.code = code
+ self.lineno = lineno
self.varargs = self.kwargs = None
if flags & CO_VARARGS:
self.varargs = 1
if flags & CO_VARKEYWORDS:
self.kwargs = 1
-
+
def getChildren(self):
children = []
- if self.decorators:
- children.append(flatten(self.decorators.nodes))
+ children.append(self.decorators)
children.append(self.name)
children.append(self.argnames)
children.extend(flatten(self.defaults))
@@ -592,8 +569,8 @@ class Function(Node):
def getChildNodes(self):
nodelist = []
- if self.decorators:
- nodelist.extend(flatten_nodes(self.decorators.nodes))
+ if self.decorators is not None:
+ nodelist.append(self.decorators)
nodelist.extend(flatten_nodes(self.defaults))
nodelist.append(self.code)
return tuple(nodelist)
@@ -602,12 +579,12 @@ class Function(Node):
return "Function(%s, %s, %s, %s, %s, %s, %s)" % (repr(self.decorators), repr(self.name), repr(self.argnames), repr(self.defaults), repr(self.flags), repr(self.doc), repr(self.code))
class GenExpr(Node):
- nodes["genexpr"] = "GenExpr"
- def __init__(self, code):
+ def __init__(self, code, lineno=None):
self.code = code
+ self.lineno = lineno
self.argnames = ['[outmost-iterable]']
self.varargs = self.kwargs = None
-
+
def getChildren(self):
@@ -620,11 +597,11 @@ class GenExpr(Node):
return "GenExpr(%s)" % (repr(self.code),)
class GenExprFor(Node):
- nodes["genexprfor"] = "GenExprFor"
- def __init__(self, assign, iter, ifs):
+ def __init__(self, assign, iter, ifs, lineno=None):
self.assign = assign
self.iter = iter
self.ifs = ifs
+ self.lineno = lineno
self.is_outmost = False
@@ -646,9 +623,9 @@ class GenExprFor(Node):
return "GenExprFor(%s, %s, %s)" % (repr(self.assign), repr(self.iter), repr(self.ifs))
class GenExprIf(Node):
- nodes["genexprif"] = "GenExprIf"
- def __init__(self, test):
+ def __init__(self, test, lineno=None):
self.test = test
+ self.lineno = lineno
def getChildren(self):
return self.test,
@@ -660,10 +637,10 @@ class GenExprIf(Node):
return "GenExprIf(%s)" % (repr(self.test),)
class GenExprInner(Node):
- nodes["genexprinner"] = "GenExprInner"
- def __init__(self, expr, quals):
+ def __init__(self, expr, quals, lineno=None):
self.expr = expr
self.quals = quals
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -681,10 +658,10 @@ class GenExprInner(Node):
return "GenExprInner(%s, %s)" % (repr(self.expr), repr(self.quals))
class Getattr(Node):
- nodes["getattr"] = "Getattr"
- def __init__(self, expr, attrname):
+ def __init__(self, expr, attrname, lineno=None):
self.expr = expr
self.attrname = attrname
+ self.lineno = lineno
def getChildren(self):
return self.expr, self.attrname
@@ -696,9 +673,9 @@ class Getattr(Node):
return "Getattr(%s, %s)" % (repr(self.expr), repr(self.attrname))
class Global(Node):
- nodes["global"] = "Global"
- def __init__(self, names):
+ def __init__(self, names, lineno=None):
self.names = names
+ self.lineno = lineno
def getChildren(self):
return self.names,
@@ -710,10 +687,10 @@ class Global(Node):
return "Global(%s)" % (repr(self.names),)
class If(Node):
- nodes["if"] = "If"
- def __init__(self, tests, else_):
+ def __init__(self, tests, else_, lineno=None):
self.tests = tests
self.else_ = else_
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -724,16 +701,17 @@ class If(Node):
def getChildNodes(self):
nodelist = []
nodelist.extend(flatten_nodes(self.tests))
- if self.else_ is not None: nodelist.append(self.else_)
+ if self.else_ is not None:
+ nodelist.append(self.else_)
return tuple(nodelist)
def __repr__(self):
return "If(%s, %s)" % (repr(self.tests), repr(self.else_))
class Import(Node):
- nodes["import"] = "Import"
- def __init__(self, names):
+ def __init__(self, names, lineno=None):
self.names = names
+ self.lineno = lineno
def getChildren(self):
return self.names,
@@ -745,9 +723,9 @@ class Import(Node):
return "Import(%s)" % (repr(self.names),)
class Invert(Node):
- nodes["invert"] = "Invert"
- def __init__(self, expr):
+ def __init__(self, expr, lineno=None):
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.expr,
@@ -759,10 +737,10 @@ class Invert(Node):
return "Invert(%s)" % (repr(self.expr),)
class Keyword(Node):
- nodes["keyword"] = "Keyword"
- def __init__(self, name, expr):
+ def __init__(self, name, expr, lineno=None):
self.name = name
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.name, self.expr
@@ -774,18 +752,18 @@ class Keyword(Node):
return "Keyword(%s, %s)" % (repr(self.name), repr(self.expr))
class Lambda(Node):
- nodes["lambda"] = "Lambda"
- def __init__(self, argnames, defaults, flags, code):
+ def __init__(self, argnames, defaults, flags, code, lineno=None):
self.argnames = argnames
self.defaults = defaults
self.flags = flags
self.code = code
+ self.lineno = lineno
self.varargs = self.kwargs = None
if flags & CO_VARARGS:
self.varargs = 1
if flags & CO_VARKEYWORDS:
self.kwargs = 1
-
+
def getChildren(self):
@@ -806,10 +784,10 @@ class Lambda(Node):
return "Lambda(%s, %s, %s, %s)" % (repr(self.argnames), repr(self.defaults), repr(self.flags), repr(self.code))
class LeftShift(Node):
- nodes["leftshift"] = "LeftShift"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -821,14 +799,12 @@ class LeftShift(Node):
return "LeftShift((%s, %s))" % (repr(self.left), repr(self.right))
class List(Node):
- nodes["list"] = "List"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -839,10 +815,10 @@ class List(Node):
return "List(%s)" % (repr(self.nodes),)
class ListComp(Node):
- nodes["listcomp"] = "ListComp"
- def __init__(self, expr, quals):
+ def __init__(self, expr, quals, lineno=None):
self.expr = expr
self.quals = quals
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -860,11 +836,11 @@ class ListComp(Node):
return "ListComp(%s, %s)" % (repr(self.expr), repr(self.quals))
class ListCompFor(Node):
- nodes["listcompfor"] = "ListCompFor"
- def __init__(self, assign, list, ifs):
+ def __init__(self, assign, list, ifs, lineno=None):
self.assign = assign
self.list = list
self.ifs = ifs
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -884,9 +860,9 @@ class ListCompFor(Node):
return "ListCompFor(%s, %s, %s)" % (repr(self.assign), repr(self.list), repr(self.ifs))
class ListCompIf(Node):
- nodes["listcompif"] = "ListCompIf"
- def __init__(self, test):
+ def __init__(self, test, lineno=None):
self.test = test
+ self.lineno = lineno
def getChildren(self):
return self.test,
@@ -898,10 +874,10 @@ class ListCompIf(Node):
return "ListCompIf(%s)" % (repr(self.test),)
class Mod(Node):
- nodes["mod"] = "Mod"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -913,10 +889,10 @@ class Mod(Node):
return "Mod((%s, %s))" % (repr(self.left), repr(self.right))
class Module(Node):
- nodes["module"] = "Module"
- def __init__(self, doc, node):
+ def __init__(self, doc, node, lineno=None):
self.doc = doc
self.node = node
+ self.lineno = lineno
def getChildren(self):
return self.doc, self.node
@@ -928,10 +904,10 @@ class Module(Node):
return "Module(%s, %s)" % (repr(self.doc), repr(self.node))
class Mul(Node):
- nodes["mul"] = "Mul"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -943,9 +919,9 @@ class Mul(Node):
return "Mul((%s, %s))" % (repr(self.left), repr(self.right))
class Name(Node):
- nodes["name"] = "Name"
- def __init__(self, name):
+ def __init__(self, name, lineno=None):
self.name = name
+ self.lineno = lineno
def getChildren(self):
return self.name,
@@ -957,9 +933,9 @@ class Name(Node):
return "Name(%s)" % (repr(self.name),)
class Not(Node):
- nodes["not"] = "Not"
- def __init__(self, expr):
+ def __init__(self, expr, lineno=None):
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.expr,
@@ -971,14 +947,12 @@ class Not(Node):
return "Not(%s)" % (repr(self.expr),)
class Or(Node):
- nodes["or"] = "Or"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -989,9 +963,8 @@ class Or(Node):
return "Or(%s)" % (repr(self.nodes),)
class Pass(Node):
- nodes["pass"] = "Pass"
- def __init__(self, ):
- pass
+ def __init__(self, lineno=None):
+ self.lineno = lineno
def getChildren(self):
return ()
@@ -1003,10 +976,10 @@ class Pass(Node):
return "Pass()"
class Power(Node):
- nodes["power"] = "Power"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -1018,10 +991,10 @@ class Power(Node):
return "Power((%s, %s))" % (repr(self.left), repr(self.right))
class Print(Node):
- nodes["print"] = "Print"
- def __init__(self, nodes, dest):
+ def __init__(self, nodes, dest, lineno=None):
self.nodes = nodes
self.dest = dest
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1032,17 +1005,18 @@ class Print(Node):
def getChildNodes(self):
nodelist = []
nodelist.extend(flatten_nodes(self.nodes))
- if self.dest is not None: nodelist.append(self.dest)
+ if self.dest is not None:
+ nodelist.append(self.dest)
return tuple(nodelist)
def __repr__(self):
return "Print(%s, %s)" % (repr(self.nodes), repr(self.dest))
class Printnl(Node):
- nodes["printnl"] = "Printnl"
- def __init__(self, nodes, dest):
+ def __init__(self, nodes, dest, lineno=None):
self.nodes = nodes
self.dest = dest
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1053,18 +1027,19 @@ class Printnl(Node):
def getChildNodes(self):
nodelist = []
nodelist.extend(flatten_nodes(self.nodes))
- if self.dest is not None: nodelist.append(self.dest)
+ if self.dest is not None:
+ nodelist.append(self.dest)
return tuple(nodelist)
def __repr__(self):
return "Printnl(%s, %s)" % (repr(self.nodes), repr(self.dest))
class Raise(Node):
- nodes["raise"] = "Raise"
- def __init__(self, expr1, expr2, expr3):
+ def __init__(self, expr1, expr2, expr3, lineno=None):
self.expr1 = expr1
self.expr2 = expr2
self.expr3 = expr3
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1075,18 +1050,21 @@ class Raise(Node):
def getChildNodes(self):
nodelist = []
- if self.expr1 is not None: nodelist.append(self.expr1)
- if self.expr2 is not None: nodelist.append(self.expr2)
- if self.expr3 is not None: nodelist.append(self.expr3)
+ if self.expr1 is not None:
+ nodelist.append(self.expr1)
+ if self.expr2 is not None:
+ nodelist.append(self.expr2)
+ if self.expr3 is not None:
+ nodelist.append(self.expr3)
return tuple(nodelist)
def __repr__(self):
return "Raise(%s, %s, %s)" % (repr(self.expr1), repr(self.expr2), repr(self.expr3))
class Return(Node):
- nodes["return"] = "Return"
- def __init__(self, value):
+ def __init__(self, value, lineno=None):
self.value = value
+ self.lineno = lineno
def getChildren(self):
return self.value,
@@ -1098,10 +1076,10 @@ class Return(Node):
return "Return(%s)" % (repr(self.value),)
class RightShift(Node):
- nodes["rightshift"] = "RightShift"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -1113,12 +1091,12 @@ class RightShift(Node):
return "RightShift((%s, %s))" % (repr(self.left), repr(self.right))
class Slice(Node):
- nodes["slice"] = "Slice"
- def __init__(self, expr, flags, lower, upper):
+ def __init__(self, expr, flags, lower, upper, lineno=None):
self.expr = expr
self.flags = flags
self.lower = lower
self.upper = upper
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1131,22 +1109,22 @@ class Slice(Node):
def getChildNodes(self):
nodelist = []
nodelist.append(self.expr)
- if self.lower is not None: nodelist.append(self.lower)
- if self.upper is not None: nodelist.append(self.upper)
+ if self.lower is not None:
+ nodelist.append(self.lower)
+ if self.upper is not None:
+ nodelist.append(self.upper)
return tuple(nodelist)
def __repr__(self):
return "Slice(%s, %s, %s, %s)" % (repr(self.expr), repr(self.flags), repr(self.lower), repr(self.upper))
class Sliceobj(Node):
- nodes["sliceobj"] = "Sliceobj"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -1157,14 +1135,12 @@ class Sliceobj(Node):
return "Sliceobj(%s)" % (repr(self.nodes),)
class Stmt(Node):
- nodes["stmt"] = "Stmt"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -1175,10 +1151,10 @@ class Stmt(Node):
return "Stmt(%s)" % (repr(self.nodes),)
class Sub(Node):
- nodes["sub"] = "Sub"
- def __init__(self, (left, right)):
+ def __init__(self, (left, right), lineno=None):
self.left = left
self.right = right
+ self.lineno = lineno
def getChildren(self):
return self.left, self.right
@@ -1190,11 +1166,11 @@ class Sub(Node):
return "Sub((%s, %s))" % (repr(self.left), repr(self.right))
class Subscript(Node):
- nodes["subscript"] = "Subscript"
- def __init__(self, expr, flags, subs):
+ def __init__(self, expr, flags, subs, lineno=None):
self.expr = expr
self.flags = flags
self.subs = subs
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1213,11 +1189,11 @@ class Subscript(Node):
return "Subscript(%s, %s, %s)" % (repr(self.expr), repr(self.flags), repr(self.subs))
class TryExcept(Node):
- nodes["tryexcept"] = "TryExcept"
- def __init__(self, body, handlers, else_):
+ def __init__(self, body, handlers, else_, lineno=None):
self.body = body
self.handlers = handlers
self.else_ = else_
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1230,17 +1206,18 @@ class TryExcept(Node):
nodelist = []
nodelist.append(self.body)
nodelist.extend(flatten_nodes(self.handlers))
- if self.else_ is not None: nodelist.append(self.else_)
+ if self.else_ is not None:
+ nodelist.append(self.else_)
return tuple(nodelist)
def __repr__(self):
return "TryExcept(%s, %s, %s)" % (repr(self.body), repr(self.handlers), repr(self.else_))
class TryFinally(Node):
- nodes["tryfinally"] = "TryFinally"
- def __init__(self, body, final):
+ def __init__(self, body, final, lineno=None):
self.body = body
self.final = final
+ self.lineno = lineno
def getChildren(self):
return self.body, self.final
@@ -1252,14 +1229,12 @@ class TryFinally(Node):
return "TryFinally(%s, %s)" % (repr(self.body), repr(self.final))
class Tuple(Node):
- nodes["tuple"] = "Tuple"
- def __init__(self, nodes):
+ def __init__(self, nodes, lineno=None):
self.nodes = nodes
+ self.lineno = lineno
def getChildren(self):
- children = []
- children.extend(flatten(self.nodes))
- return tuple(children)
+ return tuple(flatten(self.nodes))
def getChildNodes(self):
nodelist = []
@@ -1270,9 +1245,9 @@ class Tuple(Node):
return "Tuple(%s)" % (repr(self.nodes),)
class UnaryAdd(Node):
- nodes["unaryadd"] = "UnaryAdd"
- def __init__(self, expr):
+ def __init__(self, expr, lineno=None):
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.expr,
@@ -1284,9 +1259,9 @@ class UnaryAdd(Node):
return "UnaryAdd(%s)" % (repr(self.expr),)
class UnarySub(Node):
- nodes["unarysub"] = "UnarySub"
- def __init__(self, expr):
+ def __init__(self, expr, lineno=None):
self.expr = expr
+ self.lineno = lineno
def getChildren(self):
return self.expr,
@@ -1298,11 +1273,11 @@ class UnarySub(Node):
return "UnarySub(%s)" % (repr(self.expr),)
class While(Node):
- nodes["while"] = "While"
- def __init__(self, test, body, else_):
+ def __init__(self, test, body, else_, lineno=None):
self.test = test
self.body = body
self.else_ = else_
+ self.lineno = lineno
def getChildren(self):
children = []
@@ -1315,16 +1290,17 @@ class While(Node):
nodelist = []
nodelist.append(self.test)
nodelist.append(self.body)
- if self.else_ is not None: nodelist.append(self.else_)
+ if self.else_ is not None:
+ nodelist.append(self.else_)
return tuple(nodelist)
def __repr__(self):
return "While(%s, %s, %s)" % (repr(self.test), repr(self.body), repr(self.else_))
class Yield(Node):
- nodes["yield"] = "Yield"
- def __init__(self, value):
+ def __init__(self, value, lineno=None):
self.value = value
+ self.lineno = lineno
def getChildren(self):
return self.value,
@@ -1335,6 +1311,6 @@ class Yield(Node):
def __repr__(self):
return "Yield(%s)" % (repr(self.value),)
-klasses = globals()
-for k in nodes.keys():
- nodes[k] = klasses[nodes[k]]
+for name, obj in globals().items():
+ if isinstance(obj, type) and issubclass(obj, Node):
+ nodes[name.lower()] = obj