Commit f49378c5 authored by Dag Sverre Seljebotn's avatar Dag Sverre Seljebotn

Added ReadonlyVisitor.

There was an option in VisitorTransform for this but it was way too obscure,
better to have a seperate class.
parent 3797aa88
...@@ -84,14 +84,12 @@ class VisitorTransform(Transform): ...@@ -84,14 +84,12 @@ class VisitorTransform(Transform):
# Note: If needed, this can be replaced with a more efficient metaclass # Note: If needed, this can be replaced with a more efficient metaclass
# approach, resolving the jump table at module load time. # approach, resolving the jump table at module load time.
def __init__(self, readonly=False, **kw): def __init__(self, **kw):
"""readonly - If this is set to True, the results of process_node """readonly - If this is set to True, the results of process_node
will be discarded (so that one can return None without changing will be discarded (so that one can return None without changing
the tree).""" the tree)."""
super(VisitorTransform, self).__init__(**kw) super(VisitorTransform, self).__init__(**kw)
self.visitmethods = {'process_' : {}, 'pre_' : {}, 'post_' : {}} self.visitmethods = {'process_' : {}, 'pre_' : {}, 'post_' : {}}
self.attrname = ""
self.readonly = readonly
def get_visitfunc(self, prefix, cls): def get_visitfunc(self, prefix, cls):
mname = prefix + cls.__name__ mname = prefix + cls.__name__
...@@ -106,16 +104,12 @@ class VisitorTransform(Transform): ...@@ -106,16 +104,12 @@ class VisitorTransform(Transform):
self.visitmethods[prefix][mname] = m self.visitmethods[prefix][mname] = m
return m return m
def process_node(self, node, name="_"): def process_node(self, node):
# Pass on to calls registered in self.visitmethods # Pass on to calls registered in self.visitmethods
self.attrname = name
if node is None: if node is None:
return None return None
result = self.get_visitfunc("process_", node.__class__)(node) result = self.get_visitfunc("process_", node.__class__)(node)
if self.readonly: return node
return node
else:
return result
def process_Node(self, node): def process_Node(self, node):
descend = self.get_visitfunc("pre_", node.__class__)(node) descend = self.get_visitfunc("pre_", node.__class__)(node)
...@@ -130,6 +124,15 @@ class VisitorTransform(Transform): ...@@ -130,6 +124,15 @@ class VisitorTransform(Transform):
def post_Node(self, node): def post_Node(self, node):
pass pass
class ReadonlyVisitor(VisitorTransform):
"""
Like VisitorTransform, however process_X methods do not have to return
the result node -- the result of process_X is always discarded and the
structure of the original tree is not changed.
"""
def process_node(self, node):
super(ReadonlyVisitor, self).process_node(node) # discard result
return node
# Utils # Utils
def ensure_statlist(node): def ensure_statlist(node):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment