Skip to content

Commit

Permalink
Implemented sub; Fixed bug for insert and sub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhvani Patel committed Jul 14, 2017
1 parent 6c63e68 commit 77f4d9e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 25 deletions.
Binary file modified __pycache__/toCheck.pypy-41.pyc
Binary file not shown.
Binary file modified mutate_token_insert.pyc
Binary file not shown.
34 changes: 31 additions & 3 deletions mutate_token_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import token
from Token import Token
from random import randint
import StringIO

#Declaring Global Constants
YES_TOKEN = 0b00
Expand All @@ -16,6 +17,20 @@
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())
Expand All @@ -34,6 +49,17 @@ def subTokMut(raw_tokens, raw_text):
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 = []
Expand Down Expand Up @@ -153,8 +179,10 @@ def subTokMut(raw_tokens, raw_text):

if toTest == None:
print "Try again..."
subTokMut(raw_tokens_pass, raw_text)
return new_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc, send
#subTokMut(raw_tokens_pass, raw_text)
lenR = 2
lenK = 2
return lenR, raw_tokens_pass, raw_text, lenK, send, insTokS
else:
print toTest[0]
print toTest[0].filename
Expand All @@ -163,7 +191,7 @@ def subTokMut(raw_tokens, raw_text):
print toTest[0].functionname
print toTest[0].text
print toTest[0].errorname
return new_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc, send
return new_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc, send, insTokS

print "-----------FINISHED-------------------"
print chosenLineInd+1
Expand Down
Binary file modified mutate_token_sub.pyc
Binary file not shown.
73 changes: 51 additions & 22 deletions py_mutations_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def perform():
if isinstance(new_i_text, str):
break

new_tokens_ins = all_tokens
new_tokens_ins = all_tokens[:]

if insTok.type == "NL":
insTok.type = "NEWLINE"
Expand Down Expand Up @@ -286,7 +286,7 @@ def perform():

print "NEXT STEP..."

new_tokens_del = allGood
new_tokens_del = allGood[:]

vocab_entry = open_closed_tokens(send)
send.value = vocab_entry
Expand All @@ -308,20 +308,51 @@ def perform():

# SUB
raw_tokens = tokenize.generate_tokens(StringIO.StringIO(all_rows[curr][0]).readline)
global all_tokens
all_tokens = []
#global all_tokens
#all_tokens = []
global indexed_tokens
indexed_tokens = []
print type(raw_tokens)

new_s_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc_s, sendS = subTokMut(raw_tokens, source_code)
new_s_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc_s, sendS, insTokS = subTokMut(raw_tokens, source_code)

while isinstance(new_s_text, int):
new_s_text, YES_TOKEN, SUBSTITUTION, out_tokens_loc_s, sendS, insTokS = subTokMut(YES_TOKEN, SUBSTITUTION)
if isinstance(new_s_text, str):
break

print "NEXT STEP..."
try:
newTokenStream = tokenize.tokenize(StringIO.StringIO(new_s_text).readline, handle_token)
except (tokenize.TokenError) as e:
pass
new_tokens_sub = all_tokens

# SUB DELETE

new_tokens_sub = allGood[:]

vocab_entry = open_closed_tokens(sendS)
sendS.value = vocab_entry

bruhInd = -1
iterInd = 0
for a in allGood:
if a == sendS:
bruhInd = iterInd
iterInd = iterInd + 1
#print bruhInd
#print len(new_tokens_del)
del new_tokens_sub[bruhInd]

# SUB INSERT

if insTokS.type == "NL":
insTokS.type = "NEWLINE"
if insTokS.type == "ENDMARKER":
insTokS.type = "INDENT"

print insTokS.type
print insTokS.value
print "LUNCH"

new_tokens_sub.insert(bruhInd, insTokS)

one_hot_bad_sub = vocabularize_tokens(new_tokens_sub, True)

# MUTATIONS PER CHARACTER
Expand All @@ -339,23 +370,21 @@ def perform():
print source_code
print len(new_i_text)
print len(new_d_text)
print new_i_text
print new_d_text

print len(new_tokens_del)
print len(allGood)


if len(one_hot_bad_del) != len(one_hot_good)-1:
for token in new_tokens_ins:
#print token.type
print token.value
print "<3 <3 <3 GOOD:"
for token in allGood:
#print token.type
print token.value
else:
perform()
return

for token in new_tokens_del:
#print token.type
print token.value
print "<3 <3 <3 GOOD:"
for token in allGood:
#print token.type
print token.value


#one_hot_all = np.concatenate((one_hot_good, one_hot_bad), axis=0)

Expand Down

0 comments on commit 77f4d9e

Please sign in to comment.