blob: dfeda3232add9501a060c9aaea2b1a82eb7f7925 [file] [log] [blame]
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +00001import sys
Jeremy Hylton9812e7b2000-03-06 18:54:30 +00002from compiler import ast
Jeremy Hyltoned958612000-03-06 18:49:31 +00003
4class ASTVisitor:
5 """Performs a depth-first walk of the AST
6
7 The ASTVisitor will walk the AST, performing either a preorder or
8 postorder traversal depending on which method is called.
9
10 methods:
11 preorder(tree, visitor)
12 postorder(tree, visitor)
13 tree: an instance of ast.Node
14 visitor: an instance with visitXXX methods
15
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.
21
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
29 nodes.
30
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.
36 """
37
38 VERBOSE = 0
39
40 def __init__(self):
41 self.node = None
Thomas Wouters46cc7c02000-08-12 20:32:46 +000042 self._cache = {}
Jeremy Hyltoned958612000-03-06 18:49:31 +000043
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000044 def default(self, node, *args):
Jeremy Hyltoned958612000-03-06 18:49:31 +000045 for child in node.getChildren():
46 if isinstance(child, ast.Node):
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000047 apply(self._preorder, (child,) + args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000048
49 def dispatch(self, node, *args):
50 self.node = node
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000051 klass = node.__class__
52 meth = self._cache.get(klass, None)
Thomas Wouters46cc7c02000-08-12 20:32:46 +000053 if meth is None:
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000054 className = klass.__name__
Thomas Wouters46cc7c02000-08-12 20:32:46 +000055 meth = getattr(self.visitor, 'visit' + className, self.default)
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000056 self._cache[klass] = meth
Jeremy Hyltoned958612000-03-06 18:49:31 +000057 if self.VERBOSE > 0:
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000058 className = klass.__name__
Jeremy Hyltoned958612000-03-06 18:49:31 +000059 if self.VERBOSE == 1:
60 if meth == 0:
61 print "dispatch", className
62 else:
63 print "dispatch", className, (meth and meth.__name__ or '')
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000064 return meth(node, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000065
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000066 def preorder(self, tree, visitor, *args):
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000067 """Do preorder walk of tree using visitor"""
68 self.visitor = visitor
69 visitor.visit = self._preorder
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000070 self._preorder(tree, *args) # XXX *args make sense?
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000071
72 _preorder = dispatch
73
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:
93 return apply(meth, (node,) + args)
94 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
105 return apply(self.default, (node,) + args)
Jeremy Hyltoned958612000-03-06 18:49:31 +0000106
107_walker = ASTVisitor
108def walk(tree, visitor, verbose=None):
109 w = _walker()
110 if verbose is not None:
111 w.VERBOSE = verbose
112 w.preorder(tree, visitor)
113 return w.visitor
114
115def dumpNode(node):
116 print node.__class__
117 for attr in dir(node):
118 if attr[0] != '_':
119 print "\t", "%-10.10s" % attr, getattr(node, attr)