-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_renderer.py
38 lines (34 loc) · 1.23 KB
/
ast_renderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import operator
from functools import reduce
from tatsu.walkers import NodeWalker
class AstRenderer(NodeWalker):
def walk_object(self, node):
print("START WALK" + "--------------" * 10)
graph = (
reduce(operator.add, (self.walk(n) for n in node._children), "")
if node._children is not None
else ""
)
label = type(node).__name__ + ":\\n"
members = [
attr
for attr in dir(node)
if not callable(getattr(node, attr))
and not attr.startswith("_")
and not attr == "ast"
]
for member in members:
if getattr(node, member) is None:
continue
print(f"{member} = {getattr(node, member)}")
label += f"{member} = {getattr(node, member)}, \\n"
label = label.replace('"', "'")
label = label.replace("\n", "\\n")
assert '"' not in label
assert "\n" not in label
graph = graph + f'{id(node)} [label="{label}"];\n'
if node._children is not None:
for n in node._children:
graph = graph + f"{id(node)} -> {id(n)};\n"
print("END WALK" + "--------------" * 10)
return graph