forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutate_token_sub.py
206 lines (161 loc) · 4.95 KB
/
mutate_token_sub.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
# 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
# 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))
# 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 subTokMut(raw_tokens, raw_text):
with open('vocabulary_mutate.json') as data_file:
data = json.load(data_file)
#pprint(data)
#print "HI"
chosenInd = randint(0,84)
chosenToken = data["indexes_m"][chosenInd]
#print chosenToken
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]
insTokS = insEdTok
raw_tokens_pass = []
out_tokens_loc = []
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] != 0:
if token[0] != 53:
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])
change = temp.strip()
check = temp.index(change)
#print "WHAT"
#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"
if len(after) > 0:
if after[0] == ' ':
new_text = before + chosenToken.encode() + after
else:
new_text = before + chosenToken.encode() + after
else:
new_text = before + chosenToken.encode() + after
else:
if 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:
#print "WAS HERE"
new_text = before + chosenToken.encode() + after
#print actual_target_ind
#print '-------------------------------'
#print new_text
toTest = checkPyPySyntax(new_text)
if toTest == None:
#print "Try again..."
#subTokMut(raw_tokens_pass, raw_text)
#print new_text
lenR = 2
lenK = 2
return lenR, raw_tokens_pass, raw_text, lenK, send, insTokS
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
return new_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc, send, insTokS
#print "-----------FINISHED-------------------"
#print chosenLineInd+1
#print out_tokens_loc
#print len(raw_tokens_pass)
#print len(out_tokens_loc)
#print lenD