Commit ec5dbe46 authored by Stefan Behnel's avatar Stefan Behnel

stream-lined Node.end_pos()

parent 860d2932
...@@ -153,24 +153,24 @@ class Node(object): ...@@ -153,24 +153,24 @@ class Node(object):
self.body.annotate(code) self.body.annotate(code)
def end_pos(self): def end_pos(self):
if not self.child_attrs:
return self.pos
try: try:
return self._end_pos return self._end_pos
except AttributeError: except AttributeError:
flat = [] pos = self.pos
for attr in self.child_attrs: for attr in self.child_attrs:
child = getattr(self, attr) child = getattr(self, attr)
# Sometimes lists, sometimes nodes # Sometimes lists, sometimes nodes
if child is None: if child is None:
pass pass
elif isinstance(child, list): elif isinstance(child, list):
flat += child for c in child:
pos = max(pos, c.end_pos())
else: else:
flat.append(child) pos = max(pos, child.end_pos())
if len(flat) == 0: self._end_pos = pos
self._end_pos = self.pos return pos
else:
self._end_pos = max([child.end_pos() for child in flat])
return self._end_pos
class BlockNode: class BlockNode:
......
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