blob: 9e39d3664937fbb3227e78deff752f3017be4fd1 [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
Jeremy Hylton16af5572002-04-18 16:26:40 +000029 be used to visit a child node of arbitrary type.
Jeremy Hyltoned958612000-03-06 18:49:31 +000030 """
31
32 VERBOSE = 0
33
34 def __init__(self):
35 self.node = None
Thomas Wouters46cc7c02000-08-12 20:32:46 +000036 self._cache = {}
Jeremy Hyltoned958612000-03-06 18:49:31 +000037
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000038 def default(self, node, *args):
Jeremy Hylton5a9ac972001-08-29 18:17:22 +000039 for child in node.getChildNodes():
40 self.dispatch(child, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000041
42 def dispatch(self, node, *args):
43 self.node = node
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000044 klass = node.__class__
45 meth = self._cache.get(klass, None)
Thomas Wouters46cc7c02000-08-12 20:32:46 +000046 if meth is None:
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000047 className = klass.__name__
Thomas Wouters46cc7c02000-08-12 20:32:46 +000048 meth = getattr(self.visitor, 'visit' + className, self.default)
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000049 self._cache[klass] = meth
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000050## if self.VERBOSE > 0:
51## className = klass.__name__
52## if self.VERBOSE == 1:
53## if meth == 0:
54## print "dispatch", className
55## else:
56## print "dispatch", className, (meth and meth.__name__ or '')
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000057 return meth(node, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000058
Jeremy Hyltond91bbba2001-04-11 16:26:05 +000059 def preorder(self, tree, visitor, *args):
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000060 """Do preorder walk of tree using visitor"""
61 self.visitor = visitor
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000062 visitor.visit = self.dispatch
63 self.dispatch(tree, *args) # XXX *args make sense?
Jeremy Hylton66d2c1f2000-10-25 18:02:02 +000064
Jeremy Hyltoned958612000-03-06 18:49:31 +000065class ExampleASTVisitor(ASTVisitor):
66 """Prints examples of the nodes that aren't visited
67
68 This visitor-driver is only useful for development, when it's
Andrew M. Kuchlingb2f89ee2003-08-11 16:20:39 +000069 helpful to develop a visitor incrementally, and get feedback on what
Jeremy Hyltoned958612000-03-06 18:49:31 +000070 you still have to do.
71 """
72 examples = {}
Tim Peterse0c446b2001-10-18 21:57:37 +000073
Jeremy Hyltoned958612000-03-06 18:49:31 +000074 def dispatch(self, node, *args):
75 self.node = node
Thomas Wouters46cc7c02000-08-12 20:32:46 +000076 meth = self._cache.get(node.__class__, None)
77 className = node.__class__.__name__
78 if meth is None:
79 meth = getattr(self.visitor, 'visit' + className, 0)
80 self._cache[node.__class__] = meth
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000081 if self.VERBOSE > 1:
82 print "dispatch", className, (meth and meth.__name__ or '')
Jeremy Hyltoned958612000-03-06 18:49:31 +000083 if meth:
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000084 meth(node, *args)
Jeremy Hyltoned958612000-03-06 18:49:31 +000085 elif self.VERBOSE > 0:
86 klass = node.__class__
Jeremy Hyltonf635abe2000-03-16 20:04:16 +000087 if not self.examples.has_key(klass):
Thomas Wouters46cc7c02000-08-12 20:32:46 +000088 self.examples[klass] = klass
89 print
90 print self.visitor
91 print klass
92 for attr in dir(node):
93 if attr[0] != '_':
94 print "\t", "%-12.12s" % attr, getattr(node, attr)
95 print
Jeremy Hylton058a5ad2001-08-27 20:47:08 +000096 return self.default(node, *args)
97
98# XXX this is an API change
Jeremy Hyltoned958612000-03-06 18:49:31 +000099
100_walker = ASTVisitor
Jeremy Hylton058a5ad2001-08-27 20:47:08 +0000101def walk(tree, visitor, walker=None, verbose=None):
102 if walker is None:
103 walker = _walker()
Jeremy Hyltoned958612000-03-06 18:49:31 +0000104 if verbose is not None:
Jeremy Hylton058a5ad2001-08-27 20:47:08 +0000105 walker.VERBOSE = verbose
106 walker.preorder(tree, visitor)
107 return walker.visitor
Jeremy Hyltoned958612000-03-06 18:49:31 +0000108
109def dumpNode(node):
110 print node.__class__
111 for attr in dir(node):
112 if attr[0] != '_':
113 print "\t", "%-10.10s" % attr, getattr(node, attr)