-
Notifications
You must be signed in to change notification settings - Fork 3
/
delombok.py
294 lines (239 loc) · 6.72 KB
/
delombok.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
import sys
import re
import difflib
from pprint import pprint
import subprocess
import re
from os.path import realpath, dirname, join
INSERT_COST = 1
DELETE_COST = INSERT_COST
NONE = 0
INSERT = 1
DELETE = 2
MATCH = 3
lombokjar = join(dirname(realpath(__file__)), 'lombok.jar')
def lineending(s):
if s[-2:] == '\r\n':
return '\r\n'
elif s[-1:] == '\n':
return '\n'
else:
return ''
def comp(el1, el2):
if el1 == el2:
return 0
elif el1.strip() == el2.strip():
return 0.1
else:
return 2.1
def match(s1, s2):
m = [[(0, NONE) for i in range(len(s2) + 1)] for j in range(len(s1) + 1)]
for x in range(len(s1) + 1):
for y in range(len(s2) + 1):
if x == 0:
if y == 0:
m[x][y] = (0, NONE)
else:
m[x][y] = (m[x][y-1][0] + INSERT_COST, INSERT)
else:
if y == 0:
m[x][y] = (m[x-1][y][0] + DELETE_COST, DELETE)
else:
m[x][y] = min(
(m[x-1][y][0] + DELETE_COST, DELETE),
(m[x][y-1][0] + INSERT_COST, INSERT),
(m[x-1][y-1][0] + comp(s1[x-1], s2[y-1]), MATCH)
)
x, y = len(s1), len(s2)
outbuf = ''
mergebuf = ''
while x > 0 or y > 0:
mod = m[x][y][1]
if mod == DELETE:
outbuf = merge(lineending(s1[x-1]), mergebuf) + outbuf
mergebuf = ''
x = x - 1
elif mod == INSERT:
mergebuf = merge(s2[y-1], mergebuf)
y = y - 1
elif mod == MATCH:
outbuf = merge(s1[x-1], mergebuf) + outbuf
mergebuf = ''
x = x - 1
y = y - 1
else:
raise Exception('Internal Error!')
if len(mergebuf) > 0:
outbuf = mergebuf + outbuf
return outbuf
class Element:
def __init__(self, code, schar, echar):
self.code = code
self.schar = schar
self.echar = echar
def getLines(self):
return re.split('\r?\n', self.code)
def getText(self):
return self.code[self.schar:self.echar + 1]
def __str__(self):
return self.__class__.__name__ + '(\n' + self.getText() + '\n)'
class LineComment(Element):
def __init__(self, code, schar, echar):
super().__init__(code, schar, echar)
def getFilteredText(self):
return '/*' + super().getText()[2:].replace('*', '/') + '*/'
class BlockComment(Element):
def __init__(self, code, schar, echar):
super().__init__(code, schar, echar)
def getFilteredText(self):
return super().getText()
class StringLiteral(Element):
def __init__(self, code, schar, echar):
super().__init__(code, schar, echar)
def getFilteredText(self):
return super().getText()
class MultiLineStringLiteral(Element):
def __init__(self, code, schar, echar):
super().__init__(code, schar, echar)
def getFilteredText(self):
return '"" /*' + super().getText()[3:-3].replace('*', '/') + '*/'
def parse(code):
elements = []
idx = 0
while idx < len(code):
sidx = idx
idx = parse_line_comment(code, idx, elements)
if idx != sidx:
continue
idx = parse_block_comment(code, idx, elements)
if idx != sidx:
continue
idx = parse_multiline_string_literal(code, idx, elements)
if idx != sidx:
continue
idx = parse_string_literal(code, '"', idx, elements)
if idx != sidx:
continue
idx = parse_string_literal(code, "'", idx, elements)
if idx != sidx:
continue
if idx == sidx:
idx = idx + 1
return elements
def parse_line_comment(code, idx, elements):
offset = idx + 2
if code[idx:offset] != '//':
return idx
for ch in code[offset:]:
if ch == "\n":
break
offset = offset + 1
if offset >= len(code):
offset = len(code) - 1
elif code[offset] == '\n':
offset = offset - 1
if code[offset] == '\r':
offset = offset - 1
elements.append(LineComment(code, idx, offset))
return offset + 1
def parse_block_comment(code, idx, elements):
offset = idx + 2
if code[idx:offset] != '/*':
return idx
for i in range(offset, len(code)):
if code[i:i+2] == '*/':
elements.append(BlockComment(code, idx, i + 1))
return i + 2
raise Exception("Block comment not closed!: " + code[idx:len(code)])
def parse_string_literal(code, character, idx, elements):
if code[idx] != character:
return idx
offset = idx + 1
escaping = False
for i in range(offset, len(code)):
if code[i] == '\\':
escaping = not escaping
elif code[i] == character and not escaping:
elements.append(StringLiteral(code, idx, i))
return i + 1
else:
escaping = False
raise Exception("String Literal not closed!: " + code[idx:len(code)])
def parse_multiline_string_literal(code, idx, elements):
offset = idx + 3
if code[idx:offset] != '"""' :
return idx
for ch in code[offset:]:
offset = offset + 1
if ch == '\n':
break
elif ch in ' \t\r':
continue
else:
raise Exception('Multiline string literal, expected whitespace or linebreak!')
escaping = False
for i in range(offset, len(code)):
if code[i] == '\\':
escaping = not escaping
elif code[i:i+3] == '"""' and not escaping:
elements.append(MultiLineStringLiteral(code, idx, i + 2))
return i + 3
else:
escaping = False
raise Exception("Multiline string literal not closed!: " + code[idx, len(code)])
def normalize(code, elements):
end = 0
result = ''
for e in elements:
result = result + code[end:e.schar] + e.getFilteredText()
end = e.echar + 1
result = result + code[end:len(code)]
return result
def merge(l1, l2):
if len(l2) == 0:
return l1
return l1[0:len(l1)-len(lineending(l1))] + ' ' + l2
def writeFile(path, contents):
with open(path, 'w') as f:
print(
contents,
end='',
file=f
)
def main():
inputFile = sys.argv[1]
outputFile = sys.argv[2]
with open(inputFile, 'r') as f:
original = f.read()
normalizedOriginal = normalize(original, parse(original))
if not re.compile('^\s*import(\s+static)?\s+lombok(\.|\s|;|$)', re.MULTILINE).search(original):
print(inputFile + ' does not contain lombok code!', file=sys.stderr)
writeFile(outputFile, original)
return
delombokArgs = [
'java',
'-jar', lombokjar,
'delombok',
'-f', 'suppressWarnings:skip',
'-f', 'generated:skip',
'-f', 'generateDelombokComment:skip',
'--print',
inputFile
]
print(' '.join(delombokArgs), file=sys.stderr)
delomboked = subprocess.run(
delombokArgs,
capture_output=True,
check=True
).stdout.decode()
if delomboked.strip() == '':
print('WARNING: Delombok returned an empty string for ' + inputFile + ' !', file=sys.stderr)
normalizedDelomboked = normalize(delomboked, parse(delomboked))
writeFile(
outputFile,
match(
normalizedOriginal.splitlines(keepends=True),
normalizedDelomboked.splitlines(keepends=True)
)
)
main()