summaryrefslogtreecommitdiffstats
path: root/Tools
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2001-08-27 20:47:08 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2001-08-27 20:47:08 (GMT)
commit058a5adad02f5ae4652d1cd1d0e5ad4ece04fa85 (patch)
treedc3173956549497667d9c151cb3d38faa8a97f2d /Tools
parentcccc478577e1a709efcb02c8a588a7f62a8ee455 (diff)
downloadcpython-058a5adad02f5ae4652d1cd1d0e5ad4ece04fa85.zip
cpython-058a5adad02f5ae4652d1cd1d0e5ad4ece04fa85.tar.gz
cpython-058a5adad02f5ae4652d1cd1d0e5ad4ece04fa85.tar.bz2
Two changes to visitor API:
Remove _preorder as alias for dispatch and call dispatch directly. Add an extra optional argument to walk() XXX Also comment out some code that does debugging prints.
Diffstat (limited to 'Tools')
-rw-r--r--Tools/compiler/compiler/visitor.py43
1 files changed, 23 insertions, 20 deletions
diff --git a/Tools/compiler/compiler/visitor.py b/Tools/compiler/compiler/visitor.py
index dfeda32..3f2ae66 100644
--- a/Tools/compiler/compiler/visitor.py
+++ b/Tools/compiler/compiler/visitor.py
@@ -1,6 +1,8 @@
-import sys
from compiler import ast
+# XXX should probably rename ASTVisitor to ASTWalker
+# XXX can it be made even more generic?
+
class ASTVisitor:
"""Performs a depth-first walk of the AST
@@ -44,7 +46,7 @@ class ASTVisitor:
def default(self, node, *args):
for child in node.getChildren():
if isinstance(child, ast.Node):
- apply(self._preorder, (child,) + args)
+ self.dispatch(child, *args)
def dispatch(self, node, *args):
self.node = node
@@ -54,22 +56,20 @@ class ASTVisitor:
className = klass.__name__
meth = getattr(self.visitor, 'visit' + className, self.default)
self._cache[klass] = meth
- if self.VERBOSE > 0:
- className = klass.__name__
- if self.VERBOSE == 1:
- if meth == 0:
- print "dispatch", className
- else:
- print "dispatch", className, (meth and meth.__name__ or '')
+## if self.VERBOSE > 0:
+## className = klass.__name__
+## if self.VERBOSE == 1:
+## if meth == 0:
+## print "dispatch", className
+## else:
+## print "dispatch", className, (meth and meth.__name__ or '')
return meth(node, *args)
def preorder(self, tree, visitor, *args):
"""Do preorder walk of tree using visitor"""
self.visitor = visitor
- visitor.visit = self._preorder
- self._preorder(tree, *args) # XXX *args make sense?
-
- _preorder = dispatch
+ visitor.visit = self.dispatch
+ self.dispatch(tree, *args) # XXX *args make sense?
class ExampleASTVisitor(ASTVisitor):
"""Prints examples of the nodes that aren't visited
@@ -90,7 +90,7 @@ class ExampleASTVisitor(ASTVisitor):
if self.VERBOSE > 1:
print "dispatch", className, (meth and meth.__name__ or '')
if meth:
- return apply(meth, (node,) + args)
+ meth(node, *args)
elif self.VERBOSE > 0:
klass = node.__class__
if not self.examples.has_key(klass):
@@ -102,15 +102,18 @@ class ExampleASTVisitor(ASTVisitor):
if attr[0] != '_':
print "\t", "%-12.12s" % attr, getattr(node, attr)
print
- return apply(self.default, (node,) + args)
+ return self.default(node, *args)
+
+# XXX this is an API change
_walker = ASTVisitor
-def walk(tree, visitor, verbose=None):
- w = _walker()
+def walk(tree, visitor, walker=None, verbose=None):
+ if walker is None:
+ walker = _walker()
if verbose is not None:
- w.VERBOSE = verbose
- w.preorder(tree, visitor)
- return w.visitor
+ walker.VERBOSE = verbose
+ walker.preorder(tree, visitor)
+ return walker.visitor
def dumpNode(node):
print node.__class__