Skip to content

Commit

Permalink
Modified show function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dob-The-Duilder committed Sep 4, 2023
1 parent a3d03a5 commit c545d95
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions pycparser/_ast_gen.py
Expand Up @@ -224,16 +224,13 @@ def children(self):
"""
pass
def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showcoord=False, _my_node_name=None):
def show(self, buf=sys.stdout, attrnames=False, nodenames=False, showcoord=False, _my_node_name=None, indent='', islast = True):
""" Pretty print the Node and all its attributes and
children (recursively) to a buffer.
buf:
Open IO buffer into which the Node is printed.
offset:
Initial offset (amount of leading spaces)
attrnames:
True if you want to see the attribute names in
name=value pairs. False to only see the values.
Expand All @@ -245,8 +242,16 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
showcoord:
Do you want the coordinates of each Node to be
displayed.
indent:
Spaces and lines to be displayed behind node.
islast:
Denotes if node is final element in its tree.
"""
lead = ' ' * offset
marker = "└─" if islast else "├─"
lead = indent + marker
if nodenames and _my_node_name is not None:
buf.write(lead + self.__class__.__name__+ ' <' + _my_node_name + '>: ')
else:
Expand All @@ -265,14 +270,23 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
buf.write(' (at %s)' % self.coord)
buf.write('\n')
indent += " " if islast else "│ "
lastChild = None
children = self.children()
if len(children) > 0:
_, lastChild = children[-1]
for (child_name, child) in self.children():
child.show(
buf,
offset=offset + 2,
attrnames=attrnames,
nodenames=nodenames,
showcoord=showcoord,
_my_node_name=child_name)
_my_node_name=child_name,
indent=indent,
islast=(lastChild == child))
class NodeVisitor(object):
Expand Down

0 comments on commit c545d95

Please sign in to comment.