forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutate_token_delete.py
317 lines (243 loc) · 7.24 KB
/
mutate_token_delete.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2017 Dhvani Patel
import json
from pprint import pprint
import tokenize
from check_pypy_syntax import checkPyPySyntax
from compile_error import CompileError
import token
from Token import Token
from random import randint
#Declaring Global Constants
YES_TOKEN = 0b10
NO_TOKEN = 0b01
INSERTION = 0b001
DELETION = 0b010
SUBSTITUTION = 0b100
# Method for finding index of certain characters in a string, n being the n'th occurence of the character/string
def find_nth(haystack, needle, n):
start = haystack.find(needle.encode())
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
def deleteTokMutS(raw_tokens, all_tokens, raw_text):
#out_tokens_loc = []
raw_tokens_pass = []
orig = []
actual_token_len = []
for token in all_tokens:
token_use = token
#orig.append(token_use)
actual_token_len.append(token_use)
for token in raw_tokens:
token_use = token
orig.append(token_use)
raw_tokens_pass.append(token_use)
#print token
#print "OKAY"
num_lines = len(actual_token_len)
num_encode = len(orig)
if (num_lines % 10 == 0):
numTokensNeeded = int((num_lines / 10))
else:
numTokensNeeded = int((num_lines / 10))
fixToks = []
chosens = []
haha = -1
radOut = 0
inds = []
for i in actual_token_len:
if i.type != 'COMMENT':
if i.type != 'INDENT':
if i.type != 'DEDENT':
if i.type != 'NEWLINE':
if i.type != 'NL':
if i.type != 'ENDMARKER':
inds.append(actual_token_len.index(i))
#print inds
#print len(inds)
#print len(actual_token_len)
#print type(radha)
allInds = []
for nah in range(numTokensNeeded+1):
temp = []
#print nah
for nahHoi in range(len(inds)):
if nah != 0:
flag = nah * 10
pastFlag = (nah-1)*10
#print "inds"
#print inds[nahHoi]
#print "indsSSS"
if pastFlag < inds[nahHoi] <= flag:
temp.append(inds[nahHoi])
if len(temp) != 0:
allInds.append(temp)
#print allInds
#print type(radha)
#print len(allInds)
#print numTokensNeeded
curr = 0
new_text = ''
while radOut < len(allInds):
if radOut == (numTokensNeeded-1):
param_start = haha
param_end = num_lines-1
else:
param_start = radOut * 10
param_end = param_start + 9
haha = param_end
toChooseArr = allInds[radOut]
chosenLineIndTemp = randint(0, len(toChooseArr)-1) #num_lines-1
chosenLineInd = toChooseArr[chosenLineIndTemp]
#print "ok"
#print chosenLineInd
chosens.append(chosenLineInd)
#print radOut
source_code = raw_text
send = actual_token_len[chosenLineInd]
#send = Token(tokenize.tok_name[raw_tokens_pass[chosenLineInd][0]], raw_tokens_pass[chosenLineInd][1], raw_tokens_pass[chosenLineInd][2][0], raw_tokens_pass[chosenLineInd][2][1], raw_tokens_pass[chosenLineInd][3][0], raw_tokens_pass[chosenLineInd][3][1], raw_tokens_pass[chosenLineInd][4])
fixToks.append(send)
indexToRemove = source_code.index(actual_token_len[chosenLineInd].line)
#print actual_token_len[chosenLineInd].line
temp = source_code[indexToRemove:indexToRemove+len(actual_token_len[chosenLineInd].line)+1]
#print "temp"
#print temp
change = temp.strip()
check = change.find(raw_tokens_pass[chosenLineInd][1])
#print actual_token_len[chosenLineInd].value
#print raw_tokens_pass[chosenLineInd][1]
shotInd = temp.index(raw_tokens_pass[chosenLineInd][1])
#print shotInd
#print change
#print "TEMP"
#print temp
#print shotInd
actual_target_ind = indexToRemove + shotInd
#print raw_tokens_pass[chosenLineInd][1]
#print len(raw_tokens_pass[chosenLineInd][1])
#print len(change)
if check == 0 and len(raw_tokens_pass[chosenLineInd][1]) == len(change):
before = source_code[:indexToRemove]
else:
before = source_code[:actual_target_ind]
#print "B"
#print before
after = source_code[actual_target_ind+len(raw_tokens_pass[chosenLineInd][1]):]
#print "A"
#print after
if check == 0:
#print "GOT EM"
new_text = before + after[1:]
else:
new_text = before + after
#print actual_target_ind
#print '-------------------------------'
#print new_text
toTest = checkPyPySyntax(new_text)
if toTest == None:
#print radOut
#if radOut != 0:
# radOut = radOut-1
#else:
# radOut = 0
#print radOut
curr = curr + 1
if curr > 10:
radOut = radOut + 1
else:
radOut = radOut
fixToks.remove(send)
chosens.remove(chosenLineInd)
#print "test_t"
else:
curr = 0
radOut = radOut + 1
return new_text, YES_TOKEN, DELETION, chosens, fixToks
def deleteTokMut(raw_tokens, raw_text):
out_tokens_loc = []
raw_tokens_pass = []
orig = []
for token in raw_tokens:
token_use = token
orig.append(token_use)
if token[0] != 5:
if token[0] != 6:
if token[0] != 4:
if token[0] != 54:
if token[0] != 53:
if token[0] != 0:
raw_tokens_pass.append(token_use)
#print token
#print "OKAY"
num_lines = len(raw_tokens_pass)
num_encode = len(orig)
chosenLineInd = randint(0, num_lines-1) #num_lines-1
chosenTrueLineInd = -1
indI = 0
for x in orig:
if raw_tokens_pass[chosenLineInd] == x:
#print "<3"
chosenTrueLineInd = indI
break
indI = indI + 1
#print chosenTrueLineInd
toIter = num_encode + (num_encode+1)
for _ in range(toIter):
out_tokens_loc.extend('0')
lenD = len(out_tokens_loc)
for indI in range(toIter):
indLook = ((chosenTrueLineInd) * 2) + 1
if indI == indLook:
out_tokens_loc[indI] = ('1')
source_code = raw_text
send = Token(tokenize.tok_name[raw_tokens_pass[chosenLineInd][0]], raw_tokens_pass[chosenLineInd][1], raw_tokens_pass[chosenLineInd][2][0], raw_tokens_pass[chosenLineInd][2][1], raw_tokens_pass[chosenLineInd][3][0], raw_tokens_pass[chosenLineInd][3][1], raw_tokens_pass[chosenLineInd][4])
indexToRemove = source_code.index(raw_tokens_pass[chosenLineInd][4])
temp = source_code[indexToRemove:indexToRemove+len(raw_tokens_pass[chosenLineInd][4])+1]
change = temp.strip()
check = change.find(raw_tokens_pass[chosenLineInd][1])
shotInd = temp.index(raw_tokens_pass[chosenLineInd][1])
#print change
#print "TEMP"
#print temp
#print shotInd
actual_target_ind = indexToRemove + shotInd
#print raw_tokens_pass[chosenLineInd][1]
#print len(raw_tokens_pass[chosenLineInd][1])
#print len(change)
if check == 0 and len(raw_tokens_pass[chosenLineInd][1]) == len(change):
before = source_code[:indexToRemove]
else:
before = source_code[:actual_target_ind]
#print "B"
#print before
after = source_code[actual_target_ind+len(raw_tokens_pass[chosenLineInd][1]):]
#print "A"
#print after
if check == 0:
#print "GOT EM"
new_text = before + after[1:]
else:
new_text = before + after
#print actual_target_ind
#print '-------------------------------'
#print new_text
toTest = checkPyPySyntax(new_text)
if toTest == None:
#print "Try again..."
#deleteTokMut(raw_tokens_pass, raw_text)
lenR = 2
lenK = 2
return lenR, raw_tokens_pass, raw_text, lenK, send
else:
#print toTest[0]
#print toTest[0].filename
#print toTest[0].line
#print toTest[0].column
#print toTest[0].functionname
#print toTest[0].text
#print toTest[0].errorname
#print "-----------FINISHED-------------------"
#print chosenLineInd+1
#print out_tokens_loc
return new_text, YES_TOKEN, DELETION, out_tokens_loc, send