Commit 2ab4744f authored by Stefan Behnel's avatar Stefan Behnel

Minor code simplifications in visitor implementation.

parent 743df03d
...@@ -12,10 +12,12 @@ cdef class TreeVisitor: ...@@ -12,10 +12,12 @@ cdef class TreeVisitor:
cdef _visitchild(self, child, parent, attrname, idx) cdef _visitchild(self, child, parent, attrname, idx)
cdef dict _visitchildren(self, parent, attrs) cdef dict _visitchildren(self, parent, attrs)
cpdef visitchildren(self, parent, attrs=*) cpdef visitchildren(self, parent, attrs=*)
cdef _raise_compiler_error(self, child, e)
cdef class VisitorTransform(TreeVisitor): cdef class VisitorTransform(TreeVisitor):
cdef dict _process_children(self, parent, attrs=*) cdef dict _process_children(self, parent, attrs=*)
cpdef visitchildren(self, parent, attrs=*) cpdef visitchildren(self, parent, attrs=*)
cdef list _flatten_list(self, list orig_list)
cdef class CythonTransform(VisitorTransform): cdef class CythonTransform(VisitorTransform):
cdef public context cdef public context
......
...@@ -78,7 +78,7 @@ class TreeVisitor(object): ...@@ -78,7 +78,7 @@ class TreeVisitor(object):
self.dispatch_table = {} self.dispatch_table = {}
self.access_path = [] self.access_path = []
def dump_node(self, node, indent=0): def dump_node(self, node):
ignored = list(node.child_attrs or []) + [ ignored = list(node.child_attrs or []) + [
u'child_attrs', u'pos', u'gil_message', u'cpp_message', u'subexprs'] u'child_attrs', u'pos', u'gil_message', u'cpp_message', u'subexprs']
values = [] values = []
...@@ -90,7 +90,6 @@ class TreeVisitor(object): ...@@ -90,7 +90,6 @@ class TreeVisitor(object):
source = os.path.basename(source.get_description()) source = os.path.basename(source.get_description())
values.append(u'%s:%s:%s' % (source, pos[1], pos[2])) values.append(u'%s:%s:%s' % (source, pos[1], pos[2]))
attribute_names = dir(node) attribute_names = dir(node)
attribute_names.sort()
for attr in attribute_names: for attr in attribute_names:
if attr in ignored: if attr in ignored:
continue continue
...@@ -156,7 +155,6 @@ class TreeVisitor(object): ...@@ -156,7 +155,6 @@ class TreeVisitor(object):
cls = type(obj) cls = type(obj)
pattern = "visit_%s" pattern = "visit_%s"
mro = inspect.getmro(cls) mro = inspect.getmro(cls)
handler_method = None
for mro_cls in mro: for mro_cls in mro:
handler_method = getattr(self, pattern % mro_cls.__name__, None) handler_method = getattr(self, pattern % mro_cls.__name__, None)
if handler_method is not None: if handler_method is not None:
...@@ -255,20 +253,23 @@ class VisitorTransform(TreeVisitor): ...@@ -255,20 +253,23 @@ class VisitorTransform(TreeVisitor):
# fast cdef entry point for calls from Cython subclasses # fast cdef entry point for calls from Cython subclasses
result = self._visitchildren(parent, attrs) result = self._visitchildren(parent, attrs)
for attr, newnode in result.items(): for attr, newnode in result.items():
if type(newnode) is not list: if type(newnode) is list:
setattr(parent, attr, newnode) newnode = self._flatten_list(newnode)
else: setattr(parent, attr, newnode)
# Flatten the list one level and remove any None
newlist = []
for x in newnode:
if x is not None:
if type(x) is list:
newlist += x
else:
newlist.append(x)
setattr(parent, attr, newlist)
return result return result
@cython.final
def _flatten_list(self, orig_list):
# Flatten the list one level and remove any None
newlist = []
for x in orig_list:
if x is not None:
if type(x) is list:
newlist.extend(x)
else:
newlist.append(x)
return newlist
def recurse_to_children(self, node): def recurse_to_children(self, node):
self._process_children(node) self._process_children(node)
return node return node
...@@ -276,6 +277,7 @@ class VisitorTransform(TreeVisitor): ...@@ -276,6 +277,7 @@ class VisitorTransform(TreeVisitor):
def __call__(self, root): def __call__(self, root):
return self._visit(root) return self._visit(root)
class CythonTransform(VisitorTransform): class CythonTransform(VisitorTransform):
""" """
Certain common conventions and utilities for Cython transforms. Certain common conventions and utilities for Cython transforms.
......
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