forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutate_token_insert.py
462 lines (380 loc) · 11.2 KB
/
mutate_token_insert.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# 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
import StringIO
#Declaring Global Constants
YES_TOKEN = 0b10
NO_TOKEN = 0b01
INSERTION = 0b001
DELETION = 0b010
SUBSTITUTION = 0b100
global new_token
# 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
# Create list of tokens
def handle_token(type, token, (srow, scol), (erow, ecol), line):
if repr(token)[:2] == 'u\'':
val = repr(token)[2:len(repr(token))-1]
else:
val = repr(token)[1:len(repr(token))-1]
send = Token(tokenize.tok_name[type], val, srow, scol, erow, ecol, line)
global new_token
new_token.append(send)
#print "%d,%d-%d,%d:\t%s\t%s" % \
# (srow, scol, erow, ecol, tokenize.tok_name[type], repr(token))
def insertTokMutS(raw_tokens, all_tokens, raw_text):
new_text = raw_text
with open('vocabulary_mutate.json') as data_file:
data = json.load(data_file)
#pprint(data)
#print "HI"
#print len(data["indexes_m"])
#print chosenToken
#print tokenize.tok_name
#insTok = Token(
out_tokens_loc = []
raw_tokens_pass = []
actual_token_len = []
orig = []
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)
#from py_mutations_hub import getRid
#print "OKAY"
#print len(raw_tokens_pass)
#test = getRid(raw_tokens_pass, True)
#print len(test)
#print len(actual_token_len)
#print type(radha)
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))
insToks = []
chosens = []
#print numTokensNeeded
#print "import num"
haha = -1
radOut = 0
curr = 0
while radOut < numTokensNeeded:
#chosen = raw_tokens_pass[chosenLineInd]
chosenInd = randint(0,84)
chosenToken = data["indexes_m"][chosenInd]
#print "RAD S"
#print radOut
#print len(chosens)
#print len(insToks)
#print "RAD O"
global new_token
new_token = []
try:
toksG = tokenize.tokenize(StringIO.StringIO(chosenToken).readline, handle_token)
except tokenize.TokenError:
pass
#print type(toksG)
#print len(new_token)
insEdTok = new_token[0]
insTok = insEdTok
insToks.append(insTok)
#print num_lines
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
chosenLineInd = randint(param_start, param_end) #num_lines-1
#print "inds"
#print chosenLineInd
#print "stop"
#chosen = raw_tokens_pass[chosenLineInd]
chosen = actual_token_len[chosenLineInd]
#chosen = 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])
chosens.append(chosen)
source_code = raw_text
#print len(source_code)
#print raw_tokens_pass[chosenLineInd][0]
#print all_tokens[chosenLineInd].value
#print num_lines - 1
#print len(raw_tokens_pass)
#print len(actual_token_len)
#print raw_tokens_pass[50][4]
#print actual_token_len[chosenLineInd].line
#print raw_text
toAddBeforeInd = source_code.index(actual_token_len[chosenLineInd].line)
#print toAddBeforeInd
temp = source_code[toAddBeforeInd:toAddBeforeInd+len(actual_token_len[chosenLineInd].line)]
#print temp
#print actual_token_len[chosenLineInd].value
#print "kobe"
#print raw_tokens_pass[chosenLineInd][1]
shotInd = temp.index(raw_tokens_pass[chosenLineInd][1])
change = temp.strip()
check = temp.index(change)
#print check
#print len(temp)
#print shotInd
if shotInd+1 == len(temp):
shotInd = shotInd-1
actual_target_ind = toAddBeforeInd + shotInd
before = source_code[:actual_target_ind+len(raw_tokens_pass[chosenLineInd][1])]
#print "B"
#print before
after = source_code[actual_target_ind+len(raw_tokens_pass[chosenLineInd][1]):]
#print "A"
#print after
#print raw_tokens_pass[chosenLineInd][0]
if raw_tokens_pass[chosenLineInd][0] == 53:
chosenToken = '\n' + chosenToken
if shotInd == 0:
if raw_tokens_pass[chosenLineInd][0] == 4:
new_text = before + chosenToken.encode() + after
else:
new_text = before + ' ' + chosenToken.encode() + ' ' + after
else:
if raw_tokens_pass[chosenLineInd][0] == 54:
new_text = before + chosenToken.encode() + after
elif chosenInd == data["indexes_m"].index('\n'):
#print "shiz"
if after[0] == ' ':
space = ' ' * (check-1)
else:
space = ' ' * (check)
new_text = before + chosenToken.encode() + space + after
else:
new_text = before + ' ' + chosenToken.encode() + ' ' + after
toTest = checkPyPySyntax(new_text)
print radOut
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
insToks.remove(insTok)
chosens.remove(chosen)
#print "test_t"
else:
curr = 0
radOut = radOut + 1
'''
print "Overthink"
print len(orig)
print numTokensNeeded
print len(insToks)
print insToks[0].value
print len(chosens)
print chosens[0].value
print "relax"
'''
#print NAH
#print "NAH"
#print ".___."
#print len(chosens)
#print len(insToks)
#print "NAH"
return new_text, NO_TOKEN, INSERTION, out_tokens_loc, chosens, insToks
#print "-----------FINISHED-------------------"
#print chosenLineInd+1
#print out_tokens_loc
#print len(raw_tokens_pass)
#print len(out_tokens_loc)
#print lenD
#print chosenTrueLineInd
def insertTokMut(raw_tokens, raw_text):
with open('vocabulary_mutate.json') as data_file:
data = json.load(data_file)
#pprint(data)
#print "HI"
#print len(data["indexes_m"])
#print chosenToken
#print tokenize.tok_name
#insTok = Token(
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] != 0:
raw_tokens_pass.append(token_use)
#print token
#print "OKAY"
num_lines = len(raw_tokens_pass)
num_encode = len(orig)
numTokensNeeded = int((num_lines / 10)) + 1
insToks = []
chosens = []
#print numTokensNeeded
#print "import num"
for rad in range(numTokensNeeded):
#chosen = raw_tokens_pass[chosenLineInd]
chosenInd = randint(0,84)
chosenToken = data["indexes_m"][chosenInd]
global new_token
new_token = []
try:
toksG = tokenize.tokenize(StringIO.StringIO(chosenToken).readline, handle_token)
except tokenize.TokenError:
pass
#print type(toksG)
#print len(new_token)
insEdTok = new_token[0]
insTok = insEdTok
insToks.append(insTok)
#print num_lines
if rad == (numTokensNeeded-1):
param_start = param_end
param_end = num_lines-1
else:
param_start = rad * 10
param_end = param_start + 9
chosenLineInd = randint(param_start, param_end) #num_lines-1
#print "inds"
#print chosenLineInd
#print "stop"
chosen = 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])
chosens.append(chosen)
'''
print "Overthink"
print len(orig)
print numTokensNeeded
print len(insToks)
print insToks[0].value
print len(chosens)
print chosens[0].value
print "relax"
'''
#print NAH
#print "NAH"
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+1:
out_tokens_loc[indI] = ('1')
source_code = raw_text
#print len(source_code)
#print raw_tokens_pass[chosenLineInd][0]
#print raw_tokens_pass[chosenLineInd][4]
#print raw_text
toAddBeforeInd = source_code.index(raw_tokens_pass[chosenLineInd][4])
temp = source_code[toAddBeforeInd:toAddBeforeInd+len(raw_tokens_pass[chosenLineInd][4])]
#print temp
#print "kobe"
#print raw_tokens_pass[chosenLineInd][1]
shotInd = temp.index(raw_tokens_pass[chosenLineInd][1])
change = temp.strip()
check = temp.index(change)
#print check
#print len(temp)
#print shotInd
if shotInd+1 == len(temp):
shotInd = shotInd-1
actual_target_ind = toAddBeforeInd + shotInd
before = source_code[:actual_target_ind+len(raw_tokens_pass[chosenLineInd][1])]
#print "B"
#print before
after = source_code[actual_target_ind+len(raw_tokens_pass[chosenLineInd][1]):]
#print "A"
#print after
#print raw_tokens_pass[chosenLineInd][0]
if shotInd == 0:
if raw_tokens_pass[chosenLineInd][0] == 4:
new_text = before + chosenToken.encode() + after
else:
new_text = before + ' ' + chosenToken.encode() + ' ' + after
else:
if raw_tokens_pass[chosenLineInd][0] == 54:
new_text = before + chosenToken.encode() + after
elif chosenInd == data["indexes_m"].index('\n'):
#print "shiz"
if after[0] == ' ':
space = ' ' * (check-1)
else:
space = ' ' * (check)
new_text = before + chosenToken.encode() + space + after
else:
new_text = before + ' ' + chosenToken.encode() + ' ' + after
toTest = checkPyPySyntax(new_text)
if toTest == None:
print rad
rad = rad-1
print rad
insToks.remove(insTok)
chosens.remove(chosen)
print "test_t"
#print '------------------------------------'
#print new_text
toTest = checkPyPySyntax(new_text)
if toTest == None:
#print "Try again..."
#print "-----------FINISHED-------------------"
#insertTokMut(raw_tokens_pass, raw_text)
#print "-----------FINISHED-------------------"
#print "shit man"
lenR = 2
lenK = 2
return lenR, raw_tokens_pass, raw_text, actual_token_len, chosens, insToks
else:
#print "-----------FINISHED-------------------"
#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 type(out_tokens_loc)
#print len(new_text)
#print NO_TOKEN
#print INSERTION
#print len(out_tokens_loc)
return new_text, NO_TOKEN, INSERTION, out_tokens_loc, chosens, insToks
#print "-----------FINISHED-------------------"
#print chosenLineInd+1
#print out_tokens_loc
#print len(raw_tokens_pass)
#print len(out_tokens_loc)
#print lenD
#print chosenTrueLineInd