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 The ASTVisitor will walk the AST, performing either a preorder or postorder traversal depending on which method is called. methods: preorder(tree, visitor) postorder(tree, visitor) tree: an instance of ast.Node visitor: an instance with visitXXX methods The ASTVisitor is responsible for walking over the tree in the correct order. For each node, it checks the visitor argument for a method named 'visitNodeType' where NodeType is the name of the node's class, e.g. Class. If the method exists, it is called with the node as its sole argument. The visitor method for a particular node type can control how child nodes are visited during a preorder walk. (It can't control the order during a postorder walk, because it is called _after_ the walk has occurred.) The ASTVisitor modifies the visitor argument by adding a visit method to the visitor; this method can be used to visit a child node of arbitrary type. """ CONTINUE = True NO_CONTINUE = False PREORDER = True POSTORDER = False def __init__(self): self._cache = {} self._defaults = [self.default] self._others = [self.default] def register_default(self, function): self._defaults = [function] + self._defaults def unregister_default(self, function): self._defaults.remove(function) def register_other(self, function): self._others = [function] + self._others def unregister_other(self, function): self._others.remove(function) def _iterate_over_defaults(self, node, *args): for function in self._defaults: if function(node, *args) == self.NO_CONTINUE: return def _iterate_over_others(self, node, *args): for function in self._others: if function(node, *args) == self.NO_CONTINUE: return def default(self, node, *args): for child in node.getChildNodes(): self.dispatch(child, *args) def dispatch(self, node, *args): klass = node.__class__ meth = self._get_method_for_className(klass) if self._order == self.PREORDER: if meth != None and meth(node, *args) != self.NO_CONTINUE: self._iterate_over_defaults(node, *args) elif meth == None: self._iterate_over_others(node, *args) elif self._order == self.POSTORDER: if meth == None: self._iterate_over_others(node, *args) if meth != None: self._iterate_over_defaults(node, *args) meth(node, *args) def preorder(self, tree, *args): """Do preorder walk of tree using visitor pattern""" print "in preorder" self._order = self.PREORDER self.dispatch(tree, *args) # XXX *args make sense? def postorder(self, tree, *args): """Do postorder walk of tree using visitor pattern""" self._order = self.POSTORDER self.dispatch(tree, *args) # XXX *args make sense? def _get_method_for_className(self, klass): meth = self._cache.get(klass, None) if meth is None: meth = getattr(self, self._get_visitor_name_for_class(klass), None) self._cache[klass] = meth return meth def _get_visitor_name_for_class(self, klass): return 'visit' + klass.__name__ class ShowUnimplementedVisitor(ASTVisitor): """Prints examples of the nodes that aren't visited This visitor-driver is only useful for development, when it's helpful to develop a visitor incrementally, and get feedback on what you still have to do. """ def __init__(self): ASTVisitor.__init__(self) self.register_other(self.no_visitor) self.visited = {} def visited_p(self, klass): return self.visited.get(klass, False) def set_visited(self, klass): self.visited[klass] = True def no_visitor(self, node, *args): klass = node.__class__ if self.visited_p(klass) == False: print "%s is not yet implemented" % self._get_visitor_name_for_class(klass) self.set_visited(klass) ##This code could be a little braindead all things considered, but needs ##how we want to do all this. _walker = ASTVisitor def walk(tree, visitor, verbose=None): if visitor != None: _walker = visitor walker = _walker() if verbose is not None: walker.VERBOSE = verbose walker.preorder(tree) return walker def dumpNode(node): ##Needed? print node.__class__ for attr in dir(node): if attr[0] != '_': print "\t", "%-10.10s" % attr, getattr(node, attr)