-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
59 lines (47 loc) · 1.47 KB
/
visualize.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import graphviz
from parse import *
def main():
if len(sys.argv) != 2:
print("Usage: python3 visualize.py <input_filename>")
print("You must also have Graphviz installed.")
exit(1)
text = read_file(sys.argv[1])
lexer = Lexer(text, sys.argv[1])
tokens, err = lexer.lex()
if err: return CstNode.empty(), err, None, None
parser = Parser(tokens)
cst, err, astt, _ = parser.parse()
if err: return cst, err, astt, _
if err is not None:
return print(err)
visualize(cst, "images/graphviz_cst")
visualize(astt, "images/graphviz_ast")
def visualize(node: Union[AstNode, CstNode], filename: str):
dot = graphviz.Digraph()
nid = 0
node_to_id = {}
def label_nodes(node):
nonlocal nid
nid += 1
node_to_id[node] = str(nid)
for c in node.children:
label_nodes(c)
def visualize_helper(node):
nonlocal node_to_id
if node.value is not None and node.value.value is not None:
s = f"{node.name}\n{node.value.value}"
else:
s = f"{node.name}"
dot.node(node_to_id[node], s)
for c in node.children:
dot.edge(node_to_id[node], node_to_id[c])
visualize_helper(c)
label_nodes(node)
visualize_helper(node)
dot.render(filename, format="png")
def read_file(filename: str) -> str:
with open(filename) as f:
text = f.read()
return text
if __name__ == "__main__":
main()