Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beautify Node.show() for parse tree printing #518

Closed
wants to merge 9 commits into from
22 changes: 17 additions & 5 deletions pycparser/_ast_gen.py
Expand Up @@ -224,7 +224,7 @@ 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, offset=0, 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.

Expand All @@ -245,8 +245,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 vertical lines to be displayed in front of node.

islast:
Denotes if node is final element in its tree.
"""
lead = ' ' * offset
marker = "└─" if islast else "├─"
lead = ' ' * offset + 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 +273,18 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
buf.write(' (at %s)' % self.coord)
buf.write('\n')

for (child_name, child) in self.children():
indent += " " if islast else "│ "

for idx, (child_name, child) in enumerate(self.children()):
child.show(
buf,
offset=offset + 2,
offset=offset,
attrnames=attrnames,
nodenames=nodenames,
showcoord=showcoord,
_my_node_name=child_name)
_my_node_name=child_name,
indent=indent,
islast=(idx == len(self.children())-1))


class NodeVisitor(object):
Expand Down
2 changes: 1 addition & 1 deletion pycparser/_build_tables.py
Expand Up @@ -20,7 +20,7 @@
# Generate c_ast.py
from _ast_gen import ASTCodeGenerator
ast_gen = ASTCodeGenerator('_c_ast.cfg')
ast_gen.generate(open('c_ast.py', 'w'))
ast_gen.generate(open('c_ast.py', 'w', encoding='UTF-8')))
Dob-The-Duilder marked this conversation as resolved.
Show resolved Hide resolved

from pycparser import c_parser

Expand Down
22 changes: 17 additions & 5 deletions pycparser/c_ast.py
Expand Up @@ -55,7 +55,7 @@ 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, offset=0, 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.

Expand All @@ -76,8 +76,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 vertical lines to be displayed in front of node.

islast:
Denotes if node is final element in its tree.
"""
lead = ' ' * offset
marker = "└─" if islast else "├─"
lead = ' ' * offset + indent + marker

if nodenames and _my_node_name is not None:
buf.write(lead + self.__class__.__name__+ ' <' + _my_node_name + '>: ')
else:
Expand All @@ -96,14 +104,18 @@ def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showc
buf.write(' (at %s)' % self.coord)
buf.write('\n')

for (child_name, child) in self.children():
indent += " " if islast else "│ "

for idx, (child_name, child) in enumerate(self.children()):
child.show(
buf,
offset=offset + 2,
offset=offset,
attrnames=attrnames,
nodenames=nodenames,
showcoord=showcoord,
_my_node_name=child_name)
_my_node_name=child_name,
indent=indent,
islast=(idx == len(self.children())-1))


class NodeVisitor(object):
Expand Down