forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutate_deletion.py
95 lines (66 loc) · 2.19 KB
/
mutate_deletion.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
# Copyright 2017 Dhvani Patel
import tokenize
from check_pypy_syntax import checkPyPySyntax
from compile_error import CompileError
import token
from Token import Token
from random import randint
# 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 deleteMut(raw_text):
first_strip = ''
while len(first_strip) == 0:
raw_text = str(raw_text)
num_lines = raw_text.count('\n')+1
chosenLineInd = randint(1,num_lines)
print chosenLineInd
if chosenLineInd == 1:
first_strip = raw_text[:find_nth(raw_text, "\n", chosenLineInd)]
else:
first_strip = raw_text[find_nth(raw_text, "\n", chosenLineInd-1)+1:find_nth(raw_text, "\n", chosenLineInd)]
print len(first_strip)
print first_strip
chosenColInd = randint(1,len(first_strip)+2)
first_col_strip = first_strip[:chosenColInd-1]
last_col_strip = first_strip[chosenColInd:]
new_line = first_col_strip + last_col_strip
if chosenLineInd ==1 :
print "F"
last_text = raw_text[find_nth(raw_text, "\n", chosenLineInd)+1:]
print "L"
print last_text
final_code_text = new_line + "\n" + last_text
elif chosenLineInd == num_lines:
first_text = raw_text[:find_nth(raw_text, "\n", chosenLineInd-1)]
print "F"
print first_text
final_code_text = first_text + "\n" + new_line
else:
first_text = raw_text[:find_nth(raw_text, "\n", chosenLineInd-1)]
print "F"
print first_text
last_text = raw_text[find_nth(raw_text, "\n", chosenLineInd)+1:]
print "L"
print last_text
final_code_text = first_text + new_line.encode() + "\n" + last_text
print '------------------------------------'
print final_code_text
print num_lines
toTest = checkPyPySyntax(final_code_text)
if toTest == None:
print "Try again..."
deleteMut(raw_text)
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-------------------"