# XXX should probably rename ASTVisitor to ASTWalker
# XXX can it be made even more generic?
"""Performs a depth-first walk of the AST
The ASTVisitor will walk the AST, performing either a preorder or
postorder traversal depending on which method is called.
tree: an instance of ast.Node
visitor: an instance with visitXXX methods
The ASTVisitor is responsible for walking over the tree in the
correct order. For each node, it checks the visitor argument for
a method named 'visitNodeType' where NodeType is the name of the
node's class, e.g. Class. If the method exists, it is called
with the node as its sole argument.
The visitor method for a particular node type can control how
child nodes are visited during a preorder walk. (It can't control
the order during a postorder walk, because it is called _after_
the walk has occurred.) The ASTVisitor modifies the visitor
argument by adding a visit method to the visitor; this method can
be used to visit a child node of arbitrary type.
def default(self, node, *args):
for child in node.getChildNodes():
self.dispatch(child, *args)
def dispatch(self, node, *args):
meth = self._cache.get(klass, None)
className = klass.__name__
meth = getattr(self.visitor, 'visit' + className, self.default)
self._cache[klass] = meth
## className = klass.__name__
## print "dispatch", className
## print "dispatch", className, (meth and meth.__name__ or '')
def preorder(self, tree, visitor, *args):
"""Do preorder walk of tree using visitor"""
visitor.visit = self.dispatch
self.dispatch(tree, *args) # XXX *args make sense?
class ExampleASTVisitor(ASTVisitor):
"""Prints examples of the nodes that aren't visited
This visitor-driver is only useful for development, when it's
helpful to develop a visitor incrementally, and get feedback on what
def dispatch(self, node, *args):
meth = self._cache.get(node.__class__, None)
className = node.__class__.__name__
meth = getattr(self.visitor, 'visit' + className, 0)
self._cache[node.__class__] = meth
print "dispatch", className, (meth and meth.__name__ or '')
if klass not in self.examples:
self.examples[klass] = klass
print "\t", "%-12.12s" % attr, getattr(node, attr)
return self.default(node, *args)
# XXX this is an API change
def walk(tree, visitor, walker=None, verbose=None):
walker.preorder(tree, visitor)
print "\t", "%-10.10s" % attr, getattr(node, attr)