-
Notifications
You must be signed in to change notification settings - Fork 21
/
tree_sitter_parser.py
65 lines (47 loc) · 1.38 KB
/
tree_sitter_parser.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
60
61
62
63
64
65
from tree_sitter import Language, Parser
Language.build_library(
'build/lang.so',
[
'./tree-sitter-python'
]
)
LANGUAGE = Language('build/lang.so', 'python')
QUERY = LANGUAGE.query("""
(function_definition name: (identifier) @fn-name)
""")
global_parser = Parser()
global_parser.set_language(LANGUAGE)
def get_fn_name(code, parser=global_parser):
src = bytes(code, "utf8")
tree = parser.parse(src)
node = tree.root_node
for cap, typ in QUERY.captures(node):
if typ == "fn-name":
return node_to_string(src, cap)
return None
def node_to_string(src: bytes, node):
return src[node.start_byte:node.end_byte].decode("utf8")
def make_parser():
_parser = Parser()
_parser.set_language(LANGUAGE)
return _parser
RETURN_QUERY = LANGUAGE.query("""
(return_statement) @return
""")
def does_have_return(src, parser=global_parser):
tree = parser.parse(bytes(src, "utf8"))
root = tree.root_node
captures = RETURN_QUERY.captures(root)
for node, _ in captures:
# if it doesn't have an argument, it's not a return with a value
if len(node.children) <= 1: # includes "return" itself
continue
else:
return True
return False
if __name__ == "__main__":
code = """
import ble
from a import b
"""
print(global_parser.parse(bytes(code, "utf8")).root_node.sexp())