blob: 3f2ae668e20fd17854d288b81853b7f2c236eec2 [file] [log] [blame]
Jeremy Hylton9812e7b2000-03-06 18:54:30 +00001from compiler import ast
Jeremy Hyltoned958612000-03-06 18:49:31 +00002
Jeremy Hylton058a5ad2001-08-27 20:47:08 +00003# XXX should probably rename ASTVisitor to ASTWalker
4# XXX can it be made even more generic?
5
Jeremy Hyltoned958612000-03-06 18:49:31 +00006class ASTVisitor:
7 """Performs a depth-first walk of the AST
8
9 The ASTVisitor will walk the AST, performing either a preorder or
10 postorder traversal depending on which method is called.
11
12 methods:
13 preorder(tree, visitor)
14 postorder(tree, visitor)
15 tree: an instance of ast.Node
16 visitor: an instance with visitXXX methods
17
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.
23
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
31 nodes.
32
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.
38 """
39
40 VERBOSE = 0
41
42 def __init__(self):
43 self.node = None
Thomas Wouters46cc7c02000-08-12 20:32:46 +000044 self._cache = {}
Jeremy Hyltoned958612000-03-06 18:49:31 +000045
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000046 def default(self, node, *args):
Jeremy Hyltoned958612000-03-06 18:49:31 +000047 for child in node.getChildren():
48 if isinstance(child, ast.Node):
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000049 self.dispatch(child, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000050
51 def dispatch(self, node, *args):
52 self.node = node
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000053 klass = node.__class__
54 meth = self._cache.get(klass, None)
Thomas Wouters46cc7c02000-08-12 20:32:46 +000055 if meth is None:
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000056 className = klass.__name__
Thomas Wouters46cc7c02000-08-12 20:32:46 +000057 meth = getattr(self.visitor, 'visit' + className, self.default)
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000058 self._cache[klass] = meth
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000059## if self.VERBOSE > 0:
60## className = klass.__name__
61## if self.VERBOSE == 1:
62## if meth == 0:
63## print "dispatch", className
64## else:
65## print "dispatch", className, (meth and meth.__name__ or '')
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000066 return meth(node, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000067
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000068 def preorder(self, tree, visitor, *args):
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000069 """Do preorder walk of tree using visitor"""
70 self.visitor = visitor
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000071 visitor.visit = self.dispatch
72 self.dispatch(tree, *args) # XXX *args make sense?
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000073
Jeremy Hyltoned958612000-03-06 18:49:31 +000074class ExampleASTVisitor(ASTVisitor):
75 """Prints examples of the nodes that aren't visited
76
77 This visitor-driver is only useful for development, when it's
78 helpful to develop a visitor incremently, and get feedback on what
79 you still have to do.
80 """
81 examples = {}
82
83 def dispatch(self, node, *args):
84 self.node = node
Thomas Wouters46cc7c02000-08-12 20:32:46 +000085 meth = self._cache.get(node.__class__, None)
86 className = node.__class__.__name__
87 if meth is None:
88 meth = getattr(self.visitor, 'visit' + className, 0)
89 self._cache[node.__class__] = meth
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000090 if self.VERBOSE > 1:
91 print "dispatch", className, (meth and meth.__name__ or '')
Jeremy Hyltoned958612000-03-06 18:49:31 +000092 if meth:
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000093 meth(node, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000094 elif self.VERBOSE > 0:
95 klass = node.__class__
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000096 if not self.examples.has_key(klass):
Thomas Wouters46cc7c02000-08-12 20:32:46 +000097 self.examples[klass] = klass
98 print
99 print self.visitor
100 print klass
101 for attr in dir(node):
102 if attr[0] != '_':
103 print "\t", "%-12.12s" % attr, getattr(node, attr)
104 print
Jeremy Hylton058a5ad2001-08-27 20:47:08 +0000105 return self.default(node, *args)
106
107# XXX this is an API change
Jeremy Hyltoned958612000-03-06 18:49:31 +0000108
109_walker = ASTVisitor
Jeremy Hylton058a5ad2001-08-27 20:47:08 +0000110def walk(tree, visitor, walker=None, verbose=None):
111 if walker is None:
112 walker = _walker()
Jeremy Hyltoned958612000-03-06 18:49:31 +0000113 if verbose is not None:
Jeremy Hylton058a5ad2001-08-27 20:47:08 +0000114 walker.VERBOSE = verbose
115 walker.preorder(tree, visitor)
116 return walker.visitor
Jeremy Hyltoned958612000-03-06 18:49:31 +0000117
118def dumpNode(node):
119 print node.__class__
120 for attr in dir(node):
121 if attr[0] != '_':
122 print "\t", "%-10.10s" % attr, getattr(node, attr)