-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawTree.py
More file actions
40 lines (31 loc) · 1.39 KB
/
DrawTree.py
File metadata and controls
40 lines (31 loc) · 1.39 KB
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
from antlr4 import CommonTokenStream, InputStream
from output.PlSqlLexer import PlSqlLexer
from output.PlSqlParser import PlSqlParser
import json
class DrawTree:
def __init__(self):
pass
@staticmethod
def parse_tree_to_dict(tree, parser):
if tree.getChildCount() == 0:
return {"name": tree.getText()} if tree.getText().strip() else None
rule_name = parser.ruleNames[tree.getRuleIndex()] if hasattr(tree, "getRuleIndex") else "UNKNOWN"
children = [DrawTree.parse_tree_to_dict(child, parser) for child in tree.getChildren()]
children = list(filter(None, children))
return {
"name": rule_name,
"children": children if children else []
}
@staticmethod
def generate_parse_tree_json(sql, output_file="static/tree.json"):
lexer = PlSqlLexer(InputStream(sql))
stream = CommonTokenStream(lexer)
parser = PlSqlParser(stream)
tree = parser.sql_script()
tree_dict = DrawTree.parse_tree_to_dict(tree, parser)
# print("tree dict: " + json.dumps(tree_dict, indent=4, ensure_ascii=False))
if tree_dict:
with open(output_file, "w", encoding="utf-8") as f:
json.dump(tree_dict, f, indent=4, ensure_ascii=False)
print(f"✅ Parse tree JSON đã được tạo: {output_file}")
return tree_dict