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 particular child node. If the visitor method
30 returns a true value, the ASTVisitor will not traverse the child
33 XXX The interface for controlling the preorder walk needs to be
34 re-considered. The current interface is convenient for visitors
35 that mostly let the ASTVisitor do everything. For something like
36 a code generator, where you want to walk to occur in a specific
37 order, it's a pain to add "return 1" to the end of each method.
46 def default(self
, node
, *args
):
47 for child
in node
.getChildNodes():
48 self
.dispatch(child
, *args
)
50 def dispatch(self
, node
, *args
):
52 klass
= node
.__class
__
53 meth
= self
._cache
.get(klass
, None)
55 className
= klass
.__name
__
56 meth
= getattr(self
.visitor
, 'visit' + className
, self
.default
)
57 self
._cache
[klass
] = meth
58 ## if self.VERBOSE > 0:
59 ## className = klass.__name__
60 ## if self.VERBOSE == 1:
62 ## print "dispatch", className
64 ## print "dispatch", className, (meth and meth.__name__ or '')
65 return meth(node
, *args
)
67 def preorder(self
, tree
, visitor
, *args
):
68 """Do preorder walk of tree using visitor"""
69 self
.visitor
= visitor
70 visitor
.visit
= self
.dispatch
71 self
.dispatch(tree
, *args
) # XXX *args make sense?
73 class ExampleASTVisitor(ASTVisitor
):
74 """Prints examples of the nodes that aren't visited
76 This visitor-driver is only useful for development, when it's
77 helpful to develop a visitor incremently, and get feedback on what
82 def dispatch(self
, node
, *args
):
84 meth
= self
._cache
.get(node
.__class
__, None)
85 className
= node
.__class
__.__name
__
87 meth
= getattr(self
.visitor
, 'visit' + className
, 0)
88 self
._cache
[node
.__class
__] = meth
90 print "dispatch", className
, (meth
and meth
.__name
__ or '')
93 elif self
.VERBOSE
> 0:
94 klass
= node
.__class
__
95 if not self
.examples
.has_key(klass
):
96 self
.examples
[klass
] = klass
100 for attr
in dir(node
):
102 print "\t", "%-12.12s" % attr
, getattr(node
, attr
)
104 return self
.default(node
, *args
)
106 # XXX this is an API change
109 def walk(tree
, visitor
, walker
=None, verbose
=None):
112 if verbose
is not None:
113 walker
.VERBOSE
= verbose
114 walker
.preorder(tree
, visitor
)
115 return walker
.visitor
119 for attr
in dir(node
):
121 print "\t", "%-10.10s" % attr
, getattr(node
, attr
)