2 from compiler
import ast
5 """Performs a depth-first walk of the AST
7 The ASTVisitor will walk the AST, performing either a preorder or
8 postorder traversal depending on which method is called.
11 preorder(tree, visitor)
12 postorder(tree, visitor)
13 tree: an instance of ast.Node
14 visitor: an instance with visitXXX methods
16 The ASTVisitor is responsible for walking over the tree in the
17 correct order. For each node, it checks the visitor argument for
18 a method named 'visitNodeType' where NodeType is the name of the
19 node's class, e.g. Class. If the method exists, it is called
20 with the node as its sole argument.
22 The visitor method for a particular node type can control how
23 child nodes are visited during a preorder walk. (It can't control
24 the order during a postorder walk, because it is called _after_
25 the walk has occurred.) The ASTVisitor modifies the visitor
26 argument by adding a visit method to the visitor; this method can
27 be used to visit a particular child node. If the visitor method
28 returns a true value, the ASTVisitor will not traverse the child
31 XXX The interface for controlling the preorder walk needs to be
32 re-considered. The current interface is convenient for visitors
33 that mostly let the ASTVisitor do everything. For something like
34 a code generator, where you want to walk to occur in a specific
35 order, it's a pain to add "return 1" to the end of each method.
44 def default(self
, node
, *args
):
45 for child
in node
.getChildren():
46 if isinstance(child
, ast
.Node
):
47 apply(self
._preorder
, (child
,) + args
)
49 def dispatch(self
, node
, *args
):
51 klass
= node
.__class
__
52 meth
= self
._cache
.get(klass
, None)
54 className
= klass
.__name
__
55 meth
= getattr(self
.visitor
, 'visit' + className
, self
.default
)
56 self
._cache
[klass
] = meth
58 className
= klass
.__name
__
61 print "dispatch", className
63 print "dispatch", className
, (meth
and meth
.__name
__ or '')
64 return apply(meth
, (node
,) + args
)
66 def preorder(self
, tree
, visitor
):
67 """Do preorder walk of tree using visitor"""
68 self
.visitor
= visitor
69 visitor
.visit
= self
._preorder
74 class ExampleASTVisitor(ASTVisitor
):
75 """Prints examples of the nodes that aren't visited
77 This visitor-driver is only useful for development, when it's
78 helpful to develop a visitor incremently, and get feedback on what
83 def dispatch(self
, node
, *args
):
85 meth
= self
._cache
.get(node
.__class
__, None)
86 className
= node
.__class
__.__name
__
88 meth
= getattr(self
.visitor
, 'visit' + className
, 0)
89 self
._cache
[node
.__class
__] = meth
91 print "dispatch", className
, (meth
and meth
.__name
__ or '')
93 return apply(meth
, (node
,) + args
)
94 elif self
.VERBOSE
> 0:
95 klass
= node
.__class
__
96 if not self
.examples
.has_key(klass
):
97 self
.examples
[klass
] = klass
101 for attr
in dir(node
):
103 print "\t", "%-12.12s" % attr
, getattr(node
, attr
)
105 return apply(self
.default
, (node
,) + args
)
108 def walk(tree
, visitor
, verbose
=None):
110 if verbose
is not None:
112 w
.preorder(tree
, visitor
)
117 for attr
in dir(node
):
119 print "\t", "%-10.10s" % attr
, getattr(node
, attr
)