-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.py
37 lines (28 loc) · 1.06 KB
/
tree.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
# First run
#sed -ik -r '/#.*/d ; /^\s*$/d' shell_input.c
#sed -ik -r '/#.*/d ; /^\s*$/d' shell_output.c
from tree_sitter import Language, Parser
def p(t, i, f):
if t.type in ['char_literal', 'string_literal', 'concatenated_string', ',', 'comment', 'ERROR', 'identifier']:
return
f.write("".join(' ' for _ in range(i)) + "(" + t.type + "\n")
for c in t.children:
p(c, i+2, f)
f.write("".join(' ' for _ in range(i)) + ")" + "\n")
Language.build_library(
# Store the library in the `build` directory
"build/my-languages.so",
# Include one or more languages
["/Users/abhinavmenon/tree-sitter-c"])
C_LANGUAGE = Language("build/my-languages.so", "c")
parser = Parser()
parser.set_language(C_LANGUAGE)
file = 'sqlite'
with open(f'{file}_input.c', 'r') as f:
tree = parser.parse(bytes(f.read(), 'utf-8'))
with open(f'{file}_input_sexp.txt', 'w') as g:
p(tree.root_node, 0, g)
with open(f'{file}_output.c', 'r') as f:
tree = parser.parse(bytes(f.read(), 'utf-8'))
with open(f'{file}_output_sexp.txt', 'w') as g:
p(tree.root_node, 0, g)