Skip to content

Commit

Permalink
prettify parsetree
Browse files Browse the repository at this point in the history
helper method to actually look at parsetrees containing CompValues, dicts and lists
  • Loading branch information
joernhees authored and gromgull committed Jan 18, 2017
1 parent c28d0de commit 87414fd
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rdflib/plugins/sparql/parserutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,31 @@ def setEvalFn(self, evalfn):
return self


def prettify_parsetree(t, indent='', depth=0):
out = []
if isinstance(t, ParseResults):
for e in t.asList():
out.append(prettify_parsetree(e, indent, depth + 1))
for k, v in sorted(t.items()):
out.append("%s%s- %s:\n" % (indent, ' ' * depth, k))
out.append(prettify_parsetree(v, indent, depth + 1))
elif isinstance(t, CompValue):
out.append("%s%s> %s:\n" % (indent, ' ' * depth, t.name))
for k, v in t.items():
out.append("%s%s- %s:\n" % (indent, ' ' * (depth + 1), k))
out.append(prettify_parsetree(v, indent, depth + 2))
elif isinstance(t, dict):
for k, v in t.items():
out.append("%s%s- %s:\n" % (indent, ' ' * (depth + 1), k))
out.append(prettify_parsetree(v, indent, depth + 2))
elif isinstance(t, list):
for e in t:
out.append(prettify_parsetree(e, indent, depth + 1))
else:
out.append("%s%s- %r\n" % (indent, ' ' * depth, t))
return "".join(out)


if __name__ == '__main__':
from pyparsing import Word, nums
import sys
Expand Down

0 comments on commit 87414fd

Please sign in to comment.