-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepair.py
431 lines (339 loc) · 11.3 KB
/
repair.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
import token
import tokenize
from PythonVoiceCodingPlugin.third_party.asttokens import asttokens
from PythonVoiceCodingPlugin.library import previous_token,next_token
from PythonVoiceCodingPlugin.library.BracketMatcher import BracketMatcher
from PythonVoiceCodingPlugin.library.modification import ModificationHandler
from PythonVoiceCodingPlugin.library.lexical import LineInfo,expand_to_line_or_statement
def get_dummy(atok):
dummy_name = "the_dummy_name_is_really_dummy"
forbidden = {x.string for x in atok.tokens if x.type == token.NAME}
while dummy_name in forbidden:
dummy_name = dummy_name + "even_more"
return dummy_name
def neighbors(atok,t):
x = next_token(atok,t)
x = x if x and x.type != 0 else None
y = next_token(atok,x) if x else None
z = previous_token(atok,t)
w = previous_token(atok,z) if z else None
return [t,x,y,w,z]
#######################################################################################
#######################################################################################
''' keywords taken from 3.7.4 '''
KEYWORDS = {
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'
}
OPERATORS = {
':', ',', ';', '+', '-', '*', '/', '|', '&', '<', '>', '=', '%', '~', '^', '@',
'==','!=','<=','>=','<<','>>','**','+=','-=','*=','/=','%=','&=','|=',
'^=', '<<=', '>>=', '**=', '//', '//=',
}
###############################
UNARY = {
"assert","del","elif","for","global","if","import","nonlocal","raise",
"while","with","yield","else",
'+', '-', '~', '@', 'not', ('not','in') ,
"is","def","class"
}
STARTING_UNARY = {
'+', '-', '~', '@', 'not', ('not','in') , "*", "**", ("else",":"),"is",("yield","from"),
}
FORCE_NAME_UNARY = {
"def","class",
}
BOTH_SIDES = {
'/', '|', '&', '<', '>', '=', '%', '^',
'==','!=','<=','>=','<<','>>','+=','-=','*=','/=','%=','&=','|=',
'^=', '<<=', '>>=', '**=', '//', '//=',
'and', 'as','or', 'in',
}
#######################################################################################
#######################################################################################
def start_atom(t):
return (
(
t.type in [token.NAME, token.NUMBER, token.STRING] and
(t.string not in KEYWORDS or t.string in ["True","False","None","lambda"])
) or
t.string in ["(","[","{","..."]
)
def finish_atom(t):
return (
(
t.type in [token.NAME, token.NUMBER, token.STRING] and
(t.string not in KEYWORDS or t.string in ["True","False","None"])
) or
t.string in [")","]","}","..."]
)
def before_star(t):
# of these does not check all cases
return t[-1] is None or not (
finish_atom(t[-1]) or t[-1].string in ["(","[","{",",","import","for","lambda"] or t[-1].type in [token.INDENT]
)
def after_star(t):
# of these does not check all cases
return t[1] is None or not (
start_atom(t[1]) or t[-1] is not None and (
# (t[-1].string,t[1].string) in [('(',','),(',',","),(",",")")] or
t[-1].string in ["import"]
)
)
'''
def after_star(t):
# of these does not check all cases
return not (
t[-1] is None
finish_atom(t[-1]) or t[-1].string in ["(","[","{","import"] or t[-1].type in [token.INDENT]
or (
t[1] is not None and
(t[-1].string,t[1].string) in [('(',','),(',',","),(",",")")]
)
)'''
def after_both_sides(t):
return t[1] is None or not(
start_atom(t[1]) or
t[1].string in STARTING_UNARY or
(t[2] is not None and (t[1].string,t[2].string) in STARTING_UNARY)
)
def before_both_sides(t):
return t[-1] is None or not(
finish_atom(t[-1]) or
(t[-2] is not None and finish_atom(t[-2]) and (t[-1].string,t[0].string) in STARTING_UNARY)
)
def before_unary(t):
return False
def after_unary(t):
if t[0].string in FORCE_NAME_UNARY:
return t[1] is None or not(
t[1].type == token.NAME
)
return t[1] is None or not(
start_atom(t[1]) or
t[1].string in STARTING_UNARY or
(t[0].string,t[1].string) in STARTING_UNARY
)
def after_comma(t):
return t[1] is None or t[1].string ==","
def before_comma(t):
return t[ -1] is None or t[-1].string in ["(","[","{","lambda"]
def after_bracket(t):
return t[1] is None or t[1].string in ["for","if","while","with"] or (
t[0].string in ["{","[",] and t[-1] is not None and
finish_atom(t[-1]) and t[1].string in ["]","}"]
)
def before_bracket(t):
return t[-1] is None or t[-1].string in [":"]
# not sure but seems to work
def after_double_dot(t):
return t[1] is None or not(
start_atom(t[1]) or
t[1].string.isspace() or
t[1].string in [
"]","assert","raise"
"break","continue","pass",
"return","yield","await","del",
"global","nonlocal",
"import","from"
]
)
def before_double_dot(t):
return t[-1] is None or not(
finish_atom(t[-1]) or
t[-1].string.isspace() or
t[-1].string=="[" or
t[-1].string in KEYWORDS
)
def before_dot(t):
return t[ - 1] is None or not (
t[-1].string in ["from",".","import"] or
finish_atom(t[-1])
)
return False
def after_dot(t):
return t[1] is None or not (
start_atom(t[1]) or (
t[1].string in ["."] and
t[-1] is not None and
t[-1].string in ["from",".","import"]
)
)
return False
def after_return(t):
return t[1] is not None and not(
start_atom(t[1]) or t[1].string.isspace()
)
'''
def after_else(t):
if 60<t[0].start[0]<80:
print(t,"\n",start_atom(t[1]),t[1].string in [":"],start_atom(t[1]) or t[1].string in [":"])
return t[1] is None or not(
start_atom(t[1]) or t[1].string in [":"]
)
'''
def handle_empty_compound(atok ,t,l,b,dummy):
n = neighbors(atok, t)
left,right = expand_to_line_or_statement(atok,t, l, b)
if token.DEDENT==left.type:
left = next_token(atok,left)
if t.string=="elif" and left.string!="elif":
left = next_token(atok,left)
if left is t and right.string == ":" :
rh = next_token(atok,right)
while rh and (rh.line.isspace() or rh.start[0]==right.start[0]):
rh = next_token(atok, rh)
temporary = left.line
ls = temporary[:len(temporary) - len(temporary.lstrip())]
temporary = rh.line if rh else ""
rs = temporary[:len(temporary) - len(temporary.lstrip())]
if len(ls)>=len(rs):
return True , right.endpos," pass ", False
else:
return (False,)
# these needs to be updated
elif t.string=="if" and right is t:
return True , right.endpos, " " + dummy + " else " + dummy,True
else:
return (False,)
def process_token(atok,t,l,b ):
n = neighbors(atok, t)
s = t.string
p = t.type
if s in ["*","**"]:
before = before_star(n)
before_space = False
after = after_star(n)
after_space = False
elif s in [","]:
before = before_comma(n)
before_space = False
after = after_comma(n)
after_space = False
elif s in ["(","[","{"]:
before =False
before_space = False
after = after_bracket(n)
after_space = False
elif s in [":"]:
before = before_double_dot(n)
before_space = False
after = after_double_dot(n)
after_space = False
elif s in ["."]:
before = before_dot(n)
before_space = False
after = after_dot(n)
after_space = False
elif s in BOTH_SIDES:
before = before_both_sides(n)
before_space = True
after = after_both_sides(n)
after_space = True
elif s in UNARY:
before = before_unary(n)
before_space = True
after = after_unary(n)
after_space = True
elif s in ["return"]:
before = False
before_space = True
after = after_return(n)
after_space = True
else:
before = False
before_space = False
after = False
after_space = False
return ((before,before_space),(after,after_space))
#######################################################################################
#######################################################################################
#######################################################################################
class RepairMissing():
def __init__(self, atok, m = None, timestamp = None):
self.m = m if m else ModificationHandler(atok.text)
self.start_time = timestamp if timestamp else m.get_timestamp()
self.after = set()
self.before = set()
self.d = get_dummy(atok)
self.atok = atok
def insert_before(self, token, space):
index = token.index
value = self.d if not space else self.d + " "
if not index in self.before and not index-1 in self.after:
self.before.add(index)
# print("inserting before",[token])
self.m.modify_from(self.start_time,(token.startpos,token.startpos),value)
def insert_after(self, token, space):
index = token.index
value = self.d if not space else " " + self.d
if not index+1 in self.before and not index in self.after:
self.after.add(index)
# print("inserting off their",[token])
self.m.modify_from(self.start_time,(token.endpos,token.endpos),value)
def correct_start_of_line(self,atok,token,before_information,after_information):
if before_information[0] and previous_token(atok,token).type==token.INDENT:
p = previous_token(atok, token)
n = next_token(atok,token)
def handle_error(self,atok, token):
if token in self.already_checked:
return True
final_error = token
return False
# this is wrong
while final_error.type==tokenize.ERRORTOKEN:
self.already_checked.add(final_error)
final_error = next_token(atok,final_error)
final_error = previous_token(atok, token)
def handled_consecutive_names(self,atok,token):
if token in self.already_checked:
return True
if token.type != tokenize.NAME or token.string in KEYWORDS:
return False
final_token = next_token(atok,token)
while final_token.type in [tokenize.NAME,tokenize.ERRORTOKEN] and final_token.string not in KEYWORDS:
final_token = next_token(atok,final_token)
final_token = previous_token(atok,final_token)
if final_token != token:
for t in atok.token_range(token, final_token):
self.already_checked.add(t)
self.m.modify_from(self.start_time,(token.startpos,final_token.endpos),self.d)
return True
return False
def work(self):
l = LineInfo(self.atok)
b = BracketMatcher(self.atok)
k = 0
self.already_checked = set()
for t in self.atok.tokens:
if t.string in ["if","for","while","with","def","elif","else","try","except","finally","class"]:
if t.string == "elif":
k = k + 1
z = handle_empty_compound(self.atok,t,l,b ,self.d)
if z[0]:
self.m.modify_from(self.start_time,(z[1],z[1]),z[2])
if z[3]:
continue
if t.type==tokenize.NAME:
if self.handled_consecutive_names(self.atok, t):
continue
x,y = process_token(self.atok,t,l,b)
if x[0]:
self.insert_before(t,x[1])
if y[0]:
self.insert_after(t,y[1])
print("\nk",k)
def repair_operator(atok,m,d= None,timestamp = 0):
correct_right = [token.NAME, token.NUMBER, token.STRING]
# operators=["=","==","!=","<","<=",">",">=","+","-","*","**","/","//","%","@","+=","-=","*=","**=",]
operators = set(tokenize.EXACT_TOKEN_TYPES.keys()) - {"(",")","[","]","{","}",".",",",":",";"}
d = d if d is not None else get_dummy(atok)
for x in atok.tokens :
if x.type == token.OP and x.string in operators:
y = next_token(atok,x)
if y and not (y.type in correct_right or y.string in ["(","[","{","*","**"]):
m.modify_from(timestamp,(x.endpos,x.endpos),d)
return m
# print(("not","in") in STARTING_UNARY)