Commit 4d12bec3 authored by Stefan Behnel's avatar Stefan Behnel

allow restricting the output to certain interesting lines in PrintTree()

parent 45b7c136
...@@ -709,12 +709,17 @@ class PrintTree(TreeVisitor): ...@@ -709,12 +709,17 @@ class PrintTree(TreeVisitor):
"""Prints a representation of the tree to standard output. """Prints a representation of the tree to standard output.
Subclass and override repr_of to provide more information Subclass and override repr_of to provide more information
about nodes. """ about nodes. """
def __init__(self): def __init__(self, start=None, end=None):
TreeVisitor.__init__(self) TreeVisitor.__init__(self)
self._indent = "" self._indent = ""
if start is not None or end is not None:
self._line_range = (start or 0, end or 2**30)
else:
self._line_range = None
def indent(self): def indent(self):
self._indent += " " self._indent += " "
def unindent(self): def unindent(self):
self._indent = self._indent[:-2] self._indent = self._indent[:-2]
...@@ -728,15 +733,17 @@ class PrintTree(TreeVisitor): ...@@ -728,15 +733,17 @@ class PrintTree(TreeVisitor):
# under the parent-node, not displaying the list itself in # under the parent-node, not displaying the list itself in
# the hierarchy. # the hierarchy.
def visit_Node(self, node): def visit_Node(self, node):
if len(self.access_path) == 0: line = node.pos[1]
name = "(root)" if self._line_range is None or self._line_range[0] <= line <= self._line_range[1]:
else: if len(self.access_path) == 0:
parent, attr, idx = self.access_path[-1] name = "(root)"
if idx is not None:
name = "%s[%d]" % (attr, idx)
else: else:
name = attr parent, attr, idx = self.access_path[-1]
print("%s- %s: %s" % (self._indent, name, self.repr_of(node))) if idx is not None:
name = "%s[%d]" % (attr, idx)
else:
name = attr
print("%s- %s: %s" % (self._indent, name, self.repr_of(node)))
self.indent() self.indent()
self.visitchildren(node) self.visitchildren(node)
self.unindent() self.unindent()
......
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