-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_template.py
230 lines (174 loc) · 6.28 KB
/
simple_template.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import json
import javalang
import javalang.tokenizer
import regex as re
def match_conditional_expression(code):
ret = []
if re.match(r"if\s?\(.+\)\s?{$", code):
s_code = code.split(")")
pre_code = ")".join(s_code[:-1])
post_code = ")" + s_code[-1]
ret.append((pre_code + ' &&', post_code))
ret.append((pre_code + ' ||', post_code))
return ret
def match_calling_function(code, tokenizer):
ret = []
matches = re.finditer(r"[^)(\s]+\([^)(]+\)", code)
for match in matches:
matched_code = match.group()
sc = code.split(matched_code)
if len(sc) != 2:
continue
matched_code.split("(")
ret.append((sc[0] + "<mask>" + "(" + "".join(matched_code.split("(")[1:]) + sc[1],
tokenizer(matched_code.split("(")[0], return_tensors='pt')['input_ids'].size()[1] - 2))
return ret
def match_function_api_call(code, tokenizer): # lowest level
ret = []
matches = re.finditer(r"\([^)(]+\)", code)
for match in matches: # Match single function api print(abc)
matched_code = match.group()
sc = code.split(matched_code)
new_code = "(<mask>)".join(sc)
if new_code not in [v[0] for v in ret]:
ret.append((new_code, tokenizer(matched_code[1:-1], return_tensors='pt')['input_ids'].size()[1] - 2))
return ret
def _match_function_multi_input_api_call_generate_template(matched_code, tokenizer):
ret = []
parameters = matched_code.split(",")
max = 0
for parameter in parameters:
size = tokenizer(parameter, return_tensors='pt')['input_ids'].size()[1] - 2
if size > max:
max = size
ret.append(("(<mask>, " + matched_code + ")", max)) # add variable in beginning
ret.append(("(" + matched_code + ",<mask>" + ")", max)) # add variable in end
for index, parameter in enumerate(parameters):
new_code = "("
for jindex in range(len(parameters)):
add_code = "<mask>"
if index != jindex:
add_code = parameters[jindex]
if jindex != 0:
new_code += "," + add_code
else:
new_code += add_code
new_code += ")"
ret.append((new_code, max))
return ret
def match_function_multi_input_api_call(code, tokenizer): # lowest level
ret = []
matches = re.finditer(r"\([^)(]+,[^)(]+\)", code)
for match in matches: # Match single function api print(abc)
matched_code = match.group()
sc = code.split(matched_code)
if len(sc) != 2:
continue
matched_code = matched_code[1:-1]
for p_code in _match_function_multi_input_api_call_generate_template(matched_code, tokenizer):
ret.append((sc[0] + p_code[0] + sc[1], p_code[1]))
return ret
def match_simple_operator(code, tokenizer):
u_operator = ['!=', '>=', '<=', '==', '<', '>' '%=', '^=', '|=', '&=', '/=', '*=', '-=', '+=']
ret = [""]
try:
tokens = list(javalang.tokenizer.tokenize(code))
except:
return []
current_index = 1
for token in tokens:
while current_index < token.position[1]:
current_index += 1
ret = [v + " " for v in ret]
if token.__class__.__name__ == 'Operator':
for index in range(1, len(ret)):
ret[index] = ret[index] + token.value
ret.append(ret[0] + "2mask2")
ret[0] = ret[0] + token.value
current_index += len(token.value)
if not token.__class__.__name__ == 'Operator':
ret = [v + token.value for v in ret]
ret = [v.replace(" 2mask2", "<mask>") for v in ret]
ret = [v.replace("2mask2", "<mask>") for v in ret]
ret = ret[1:]
print("Simple Operator Templates:")
return ret
def generate_match_template(code, tokenizer):
ret = []
# Not very smart templates
ret.extend(match_function_api_call(code, tokenizer))
ret.extend(match_function_multi_input_api_call(code, tokenizer))
ret.extend(match_calling_function(code, tokenizer))
print("Match Templates:")
return ret
def parse_code_file(code):
# print(code)
tokens = javalang.tokenizer.tokenize(code)
parser = javalang.parser.Parser(tokens)
tree = parser.parse()
for path, node in tree:
print(node)
print(node.position)
def parse_code_begin(code):
ret_list = []
string_builder = ""
try:
tokens = list(javalang.tokenizer.tokenize(code))
except:
return ret_list
current_index = 1
for token in tokens:
while current_index < token.position[1]:
current_index += 1
string_builder += " "
if string_builder != "":
ret_list.append(string_builder)
string_builder += token.value
current_index += len(token.value)
return ret_list
def parse_code_end(code):
ret_list = []
string_builder = ""
try:
tokens = list(javalang.tokenizer.tokenize(code))
except:
return ret_list
tokens.reverse()
current_index = len(code) + 1
for token in tokens:
current_index -= len(token.value)
if string_builder != "":
ret_list.append(string_builder)
while current_index > token.position[1]:
current_index -= 1
string_builder = " " + string_builder
string_builder = token.value + string_builder
return ret_list
def remove_redudant(ret):
real_ret = []
past_code = set()
for c in ret:
patch_code = c[0].replace(" ", "").replace("#", "").replace("\n", "").replace("\t", "")
if patch_code in past_code:
continue
past_code.add(patch_code)
real_ret.append(c)
return real_ret
def generate_template(code):
ret = []
for pre_code in parse_code_begin(code):
ret.append((pre_code, ""))
for post_code in parse_code_end(code):
ret.append(("", post_code))
ret.extend(match_conditional_expression(code))
print("Front and Back Templates:")
print(ret)
return ret
def generate_middle_template(code):
ret_list = []
tokens = list(javalang.tokenizer.tokenize(code))
for token in tokens:
if token.__class__.__name__ != "Separator":
ret_list.append(token.value)
print(ret_list)
return ret_list