-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeSit_Java
More file actions
181 lines (132 loc) · 4.41 KB
/
treeSit_Java
File metadata and controls
181 lines (132 loc) · 4.41 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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from tree_sitter import Language, Parser
import sh
def instantiate_language(name, url):
# Download language definition
# try:
# sh.git.clone(url, name)
# except sh.ErrorReturnCode as e:
# print(e.stderr.decode())
# compile language definition
Language.build_library(f"{name}.so", ["/student/mjr175/method-level-comment-generation/"+name])
# load language
return Language(f"/student/mjr175/method-level-comment-generation/java/{name}.so", name)
# instantiate language
JAVA = instantiate_language("java", "https://github.com/tree-sitter/tree-sitter-java")
# instantiate parser
parser = Parser()
parser.set_language(JAVA)
source = b"""
public static int sum(int a, int b) {
return a + b;
}
"""
tree = parser.parse(source)
root = tree.root_node
# print(root)
# print(root.text)
# print(root.type)
# print(root.children)
# print(root.sexp())
# basic checking ends here
def print_captures(captures):
for capture, tag in captures:
#print(capture)
#print(tag)
#print(f"@{tag} [{capture.type}] ({capture.start_byte}:{capture.end_byte})")
print(quote(text(capture)), "\n")
def quote(s):
return '\n'.join(f"> {line}" for line in s.splitlines())
def text(node):
return node.text.decode()
# Queries
with open("/student/mjr175/method-level-comment-generation/StoreUtil.java", "rb") as f:
tree = parser.parse(f.read())
root = tree.root_node
# print(text(root))
# Get method_declaration
query = JAVA.query("""
;; Match any `method_declaration` node and capture it.
(method_declaration) @method
""")
# Get method names
query = JAVA.query("""
;; Match any `method_declaration` node where at-least one child is an `identifier`.
;; Capture the identifier.
(method_invocation (identifier) @method.name)
""")
# Get method calls
query = JAVA.query("""
;; Match any `method calls`.
;; Capture the calls.
(method_invocation) @method.calls
""")
captures = query.captures(root)
#print_captures(captures)
# for child in root.children:
# print(child)
# for leaf in child.children:
# print(leaf)
def traverse_old(node, depth=0):
if node is None:
return
# Print node information
print(node.type)
if(node.type=="method_invocation"):
arg_string = ""
for child in node.children:
if(child.type=="argument_list"):
for args in child.children:
arg_string+=args.type
arg_string = arg_string.strip('()')
arg_list = [item.strip() for item in arg_string.split(',')]
print(arg_list)
print(len(arg_list))
# Recursively traverse child nodes
for child in node.children:
traverse(child)
def traverse(node, depth=0):
global isDoctring
global blockComment
global declaredMethod
if node is None:
return
# Print node information
#print(node.type)
if(node.type=="method_declaration"):
declaredMethod = node.text.decode('utf-8')
if(isDoctring==True):
print('-------------------------------')
print('DocString: ',blockComment)
print('---------------------------------')
print('Method: ',declaredMethod)
print('-----------------------------------------')
isDoctring = False
if(node.type=="block_comment"):
blockComment = node.text.decode('utf-8')
isDoctring = True
if(node.type=="method_invocation"):
node_text = node.text.decode('utf-8')
#####print('Called_Method: ',node_text)
functionName = []
cnt = 0
arg_string=''
arg_list=[]
for child in node.children:
if(child.type=="argument_list"):
for args in child.children:
arg_string+=args.type
arg_string = arg_string.strip('()')
arg_list = [item.strip() for item in arg_string.split(',')]
cnt = len(arg_list)
if(child.type=="identifier"):
#print('identifier: ',child.text.decode('utf-8'))
functionName.append(child.text.decode('utf-8'))
#####print('function name: ',functionName.pop())
#####print('argument count: ',cnt)
# Recursively traverse child nodes
for child in node.children:
traverse(child)
blockComment = ''
declaredMethod = ''
isDoctring = False
traverse(root)