-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Applier.py
189 lines (189 loc) · 6.2 KB
/
_Applier.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
import tqdm
#import PatchProcess
import argparse
import os
def getCode(pat):
#uncheck for len of code
return pat[pat.find('$')+1:]
def preprocess(pat):
#patch for simplified grammar
if pat.startswith('insert-before$'):
return pat.replace('insert-before$', 'insert-before:0$', 1)
elif pat.startswith('insert-after$'):
return pat.replace('insert-after$', 'insert-after:0$', 1)
elif pat.startswith('replace$'):
return pat.replace('replace$', 'replace:0,1$', 1)
elif pat.startswith('wrap$'):
return pat.replace('wrap$', 'wrap:0,1$', 1)
elif pat == 'delete':
return 'delete:0,1'
else:
test_tks = pat.split(':')
test_tks[1] = pat[pat.find(':')+1:]
if test_tks[0] not in ['delete', 'replace', 'wrap']:
return pat
elif test_tks[0] == 'delete' and ',' not in test_tks[1]:
return test_tks[0] + ':0,' + test_tks[1]
elif ',' not in test_tks[1][:test_tks[1].find('$')]:
return test_tks[0] + ':0,' +test_tks[1]
else:
return pat
def tokenize(pat):
pat = preprocess(pat)
#uncheck the error grammar
oper = pat.split(':')[0]
if oper not in ['delete', 'move-before', 'move-after']:
code = getCode(pat)
pos = []
if len(code) == 0:
oper = 'skip'
return (oper)
_pos = pat[pat.find(':')+1:pat.find('$')].split(',')
for i in _pos:
pos.append(int(i))
#tuple (str:operation, [int:positions..], str:code]
return (oper, pos, code)
else:
pos = []
_pos = pat.split(':')[1]
_pos = _pos.split(',')
for i in _pos:
pos.append(int(i))
#tuple (str:operation, [int:positions..])
return (oper, pos)
def getTargetLine(line, pat):
return line + tokenize(pat)[1][0]
def toSrcList(src):
res = []
for code in src:
if type(code) == tuple:
res.append(code[0]+code[1]+code[2])
else:
res.append(code)
return res
def toSrc(src):
return ''.join(toSrcList(src))
def toFixedLineSrc(src):
len0 = len(src)
srclist = toSrcList(src)
srclist = list(map(lambda x : x.replace('\n', ''), srclist))
srclist = list(map(lambda x : '{}\n'.format(x), srclist))
assert len(srclist) == len0
return ''.join(srclist)
def ins_bf(src, line, bias, code):
cp = line - 1 + bias[0]
if type(src[cp]) == tuple:
src[cp] = (src[cp][0]+' '+code, src[cp][1], src[cp][2])
else:
src[cp] = (code, src[cp], '')
return src
def ins_af(src, line, bias, code):
cp = line - 1 + bias[0]
if type(src[cp]) == tuple:
src[cp] = (src[cp][0], src[cp][1], code+' '+src[cp][2])
else:
src[cp] = ('', src[cp], code)
return src
def ins_ba(src, line, bias, code1, code2):
return ins_af(ins_bf(src, line, [bias[0]], code1), line, [bias[1]], code2)
def del_(src, line, bias):
i = line-1+bias[0]
while i != line+bias[0]+bias[1]-1:
if type(src[i]) == tuple:
src[i] = (src[i][0], '', src[i][2])
else:
src[i] = ('', '', '')
i+=1
return src
def rep(src, line, bias, code):
i = line-1+bias[0]
if type(src[i]) == tuple:
src[i] = (src[i][0], code, src[i][2])
else:
src[i] = ('', code, '')
i+=1
while i != line+bias[0]+bias[1]-1:
if type(src[i]) == tuple:
src[i] = (src[i][0], '', src[i][2])
else:
src[i] = ('', '', '')
i+=1
return src
def wrap(src, line, bias, code):
src = ins_bf(src, line, [bias[0]], code)
src = ins_af(src, line, [bias[0]+bias[1]-1], '}')
return src
def mv_b(src, line, bias):
code = ''.join(toSrcList(src[line-1+bias[0]:line-1+bias[0]+bias[1]]))
i=line - 1 + bias[0]
while i != line - 1 + bias[0] + bias[1]:
src[i] = ''
i += 1
return ins_bf(src, line, [bias[2]], code)
def mv_a(src, line, bias):
code = ''.join(toSrcList(src[line-1+bias[0]:line-1+bias[0]+bias[1]]))
i=line - 1 + bias[0]
while i != line - 1 + bias[0] + bias[1]:
src[i] = ''
i += 1
return ins_af(src, line, [bias[2]], code)
def applyPatch(src, pat, line):
assert type(src) == list
tp = tokenize(pat)
op = tp[0]
if op == 'insert-before':
return ins_bf(src, line, tp[1], tp[2])
elif op == 'insert-after':
return ins_af(src, line, tp[1], tp[2])
elif op == 'insert-before-after':
code = tp[2]
code = code.split('<#>')
return ins_ba(src, line, tp[1], code[0], code[1])
elif op == 'delete':
return del_(src, line, tp[1])
elif op == 'replace':
return rep(src, line, tp[1], tp[2])
elif op == 'wrap':
return wrap(src, line, tp[1], tp[2])
elif op == 'move-before':
return mv_b(src, line, tp[1])
elif op == 'move-after':
return mv_a(src, line, tp[1])
else:
print('{}, un change'.format(op))
return src
'''def make(src, pat, line, out, ori):
PP = PatchProcess.PatchProcessor()
if not os.path.exists(out):
os.makedirs(out)
with open(src, 'r') as s:
src = s.readlines()
src_str = ''.join(src)
with open(ori, 'r') as o:
ori = o.readlines()
ori_s = ''.join(ori)
with open(pat, 'r') as p:
pat = p.readlines()
ct = 0
for patch in tqdm.tqdm(pat):
srcp = src[0:]
try:
sb,pb = PP.extractMethodForSrc(ori_s, ''.join(apply_(srcp, patch, line)))
except Exception:
continue
with open(out+'/'+str(ct)+'_sb', 'w') as w:
w.write(sb)
with open(out+'/'+str(ct)+'_pb', 'w') as wp:
wp.write(pb)
ct += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'pa')
parser.add_argument('-m', '--mode', required=False, default='ap')
parser.add_argument('-s', '--src', required=False)
parser.add_argument('-p', '--pat', required=False)
parser.add_argument('-l', '--line', required=False, type=int)
parser.add_argument('-o', '--out', required=False)
parser.add_argument('-i', '--ori', required=False)
args = parser.parse_args()
if args.mode == 'ap':
make(args.src, args.pat, args.line, args.out, args.ori)'''