Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../compiler
File: visitor.py
from compiler import ast
[0] Fix | Delete
[1] Fix | Delete
# XXX should probably rename ASTVisitor to ASTWalker
[2] Fix | Delete
# XXX can it be made even more generic?
[3] Fix | Delete
[4] Fix | Delete
class ASTVisitor:
[5] Fix | Delete
"""Performs a depth-first walk of the AST
[6] Fix | Delete
[7] Fix | Delete
The ASTVisitor will walk the AST, performing either a preorder or
[8] Fix | Delete
postorder traversal depending on which method is called.
[9] Fix | Delete
[10] Fix | Delete
methods:
[11] Fix | Delete
preorder(tree, visitor)
[12] Fix | Delete
postorder(tree, visitor)
[13] Fix | Delete
tree: an instance of ast.Node
[14] Fix | Delete
visitor: an instance with visitXXX methods
[15] Fix | Delete
[16] Fix | Delete
The ASTVisitor is responsible for walking over the tree in the
[17] Fix | Delete
correct order. For each node, it checks the visitor argument for
[18] Fix | Delete
a method named 'visitNodeType' where NodeType is the name of the
[19] Fix | Delete
node's class, e.g. Class. If the method exists, it is called
[20] Fix | Delete
with the node as its sole argument.
[21] Fix | Delete
[22] Fix | Delete
The visitor method for a particular node type can control how
[23] Fix | Delete
child nodes are visited during a preorder walk. (It can't control
[24] Fix | Delete
the order during a postorder walk, because it is called _after_
[25] Fix | Delete
the walk has occurred.) The ASTVisitor modifies the visitor
[26] Fix | Delete
argument by adding a visit method to the visitor; this method can
[27] Fix | Delete
be used to visit a child node of arbitrary type.
[28] Fix | Delete
"""
[29] Fix | Delete
[30] Fix | Delete
VERBOSE = 0
[31] Fix | Delete
[32] Fix | Delete
def __init__(self):
[33] Fix | Delete
self.node = None
[34] Fix | Delete
self._cache = {}
[35] Fix | Delete
[36] Fix | Delete
def default(self, node, *args):
[37] Fix | Delete
for child in node.getChildNodes():
[38] Fix | Delete
self.dispatch(child, *args)
[39] Fix | Delete
[40] Fix | Delete
def dispatch(self, node, *args):
[41] Fix | Delete
self.node = node
[42] Fix | Delete
klass = node.__class__
[43] Fix | Delete
meth = self._cache.get(klass, None)
[44] Fix | Delete
if meth is None:
[45] Fix | Delete
className = klass.__name__
[46] Fix | Delete
meth = getattr(self.visitor, 'visit' + className, self.default)
[47] Fix | Delete
self._cache[klass] = meth
[48] Fix | Delete
## if self.VERBOSE > 0:
[49] Fix | Delete
## className = klass.__name__
[50] Fix | Delete
## if self.VERBOSE == 1:
[51] Fix | Delete
## if meth == 0:
[52] Fix | Delete
## print "dispatch", className
[53] Fix | Delete
## else:
[54] Fix | Delete
## print "dispatch", className, (meth and meth.__name__ or '')
[55] Fix | Delete
return meth(node, *args)
[56] Fix | Delete
[57] Fix | Delete
def preorder(self, tree, visitor, *args):
[58] Fix | Delete
"""Do preorder walk of tree using visitor"""
[59] Fix | Delete
self.visitor = visitor
[60] Fix | Delete
visitor.visit = self.dispatch
[61] Fix | Delete
self.dispatch(tree, *args) # XXX *args make sense?
[62] Fix | Delete
[63] Fix | Delete
class ExampleASTVisitor(ASTVisitor):
[64] Fix | Delete
"""Prints examples of the nodes that aren't visited
[65] Fix | Delete
[66] Fix | Delete
This visitor-driver is only useful for development, when it's
[67] Fix | Delete
helpful to develop a visitor incrementally, and get feedback on what
[68] Fix | Delete
you still have to do.
[69] Fix | Delete
"""
[70] Fix | Delete
examples = {}
[71] Fix | Delete
[72] Fix | Delete
def dispatch(self, node, *args):
[73] Fix | Delete
self.node = node
[74] Fix | Delete
meth = self._cache.get(node.__class__, None)
[75] Fix | Delete
className = node.__class__.__name__
[76] Fix | Delete
if meth is None:
[77] Fix | Delete
meth = getattr(self.visitor, 'visit' + className, 0)
[78] Fix | Delete
self._cache[node.__class__] = meth
[79] Fix | Delete
if self.VERBOSE > 1:
[80] Fix | Delete
print "dispatch", className, (meth and meth.__name__ or '')
[81] Fix | Delete
if meth:
[82] Fix | Delete
meth(node, *args)
[83] Fix | Delete
elif self.VERBOSE > 0:
[84] Fix | Delete
klass = node.__class__
[85] Fix | Delete
if klass not in self.examples:
[86] Fix | Delete
self.examples[klass] = klass
[87] Fix | Delete
print
[88] Fix | Delete
print self.visitor
[89] Fix | Delete
print klass
[90] Fix | Delete
for attr in dir(node):
[91] Fix | Delete
if attr[0] != '_':
[92] Fix | Delete
print "\t", "%-12.12s" % attr, getattr(node, attr)
[93] Fix | Delete
print
[94] Fix | Delete
return self.default(node, *args)
[95] Fix | Delete
[96] Fix | Delete
# XXX this is an API change
[97] Fix | Delete
[98] Fix | Delete
_walker = ASTVisitor
[99] Fix | Delete
def walk(tree, visitor, walker=None, verbose=None):
[100] Fix | Delete
if walker is None:
[101] Fix | Delete
walker = _walker()
[102] Fix | Delete
if verbose is not None:
[103] Fix | Delete
walker.VERBOSE = verbose
[104] Fix | Delete
walker.preorder(tree, visitor)
[105] Fix | Delete
return walker.visitor
[106] Fix | Delete
[107] Fix | Delete
def dumpNode(node):
[108] Fix | Delete
print node.__class__
[109] Fix | Delete
for attr in dir(node):
[110] Fix | Delete
if attr[0] != '_':
[111] Fix | Delete
print "\t", "%-10.10s" % attr, getattr(node, attr)
[112] Fix | Delete
[113] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function