1 from compiler
import ast
3 # XXX should probably rename ASTVisitor to ASTWalker
4 # XXX can it be made even more generic?
7 """Performs a depth-first walk of the AST
9 The ASTVisitor will walk the AST, performing either a preorder or
10 postorder traversal depending on which method is called.
13 preorder(tree, visitor)
14 postorder(tree, visitor)
15 tree: an instance of ast.Node
16 visitor: an instance with visitXXX methods
18 The ASTVisitor is responsible for walking over the tree in the
19 correct order. For each node, it checks the visitor argument for
20 a method named 'visitNodeType' where NodeType is the name of the
21 node's class, e.g. Class. If the method exists, it is called
22 with the node as its sole argument.
24 The visitor method for a particular node type can control how
25 child nodes are visited during a preorder walk. (It can't control
26 the order during a postorder walk, because it is called _after_
27 the walk has occurred.) The ASTVisitor modifies the visitor
28 argument by adding a visit method to the visitor; this method can
29 be used to visit a child node of arbitrary type.
38 def default(self
, node
, *args
):
39 for child
in node
.getChildNodes():
40 self
.dispatch(child
, *args
)
42 def dispatch(self
, node
, *args
):
44 klass
= node
.__class
__
45 meth
= self
._cache
.get(klass
, None)
47 className
= klass
.__name
__
48 meth
= getattr(self
.visitor
, 'visit' + className
, self
.default
)
49 self
._cache
[klass
] = meth
50 ## if self.VERBOSE > 0:
51 ## className = klass.__name__
52 ## if self.VERBOSE == 1:
54 ## print "dispatch", className
56 ## print "dispatch", className, (meth and meth.__name__ or '')
57 return meth(node
, *args
)
59 def preorder(self
, tree
, visitor
, *args
):
60 """Do preorder walk of tree using visitor"""
61 self
.visitor
= visitor
62 visitor
.visit
= self
.dispatch
63 self
.dispatch(tree
, *args
) # XXX *args make sense?
65 class ExampleASTVisitor(ASTVisitor
):
66 """Prints examples of the nodes that aren't visited
68 This visitor-driver is only useful for development, when it's
69 helpful to develop a visitor incrementally, and get feedback on what
74 def dispatch(self
, node
, *args
):
76 meth
= self
._cache
.get(node
.__class
__, None)
77 className
= node
.__class
__.__name
__
79 meth
= getattr(self
.visitor
, 'visit' + className
, 0)
80 self
._cache
[node
.__class
__] = meth
82 print "dispatch", className
, (meth
and meth
.__name
__ or '')
85 elif self
.VERBOSE
> 0:
86 klass
= node
.__class
__
87 if not self
.examples
.has_key(klass
):
88 self
.examples
[klass
] = klass
92 for attr
in dir(node
):
94 print "\t", "%-12.12s" % attr
, getattr(node
, attr
)
96 return self
.default(node
, *args
)
98 # XXX this is an API change
101 def walk(tree
, visitor
, walker
=None, verbose
=None):
104 if verbose
is not None:
105 walker
.VERBOSE
= verbose
106 walker
.preorder(tree
, visitor
)
107 return walker
.visitor
111 for attr
in dir(node
):
113 print "\t", "%-10.10s" % attr
, getattr(node
, attr
)