-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformatas.py
executable file
·324 lines (257 loc) · 7.33 KB
/
reformatas.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
#!/usr/bin/python
import sys
import codecs
import re
import shutil
import os
import_stmt = re.compile(r"(\s*)import\s+([A-Za-z$_][A-Za-z0-9$_.]*(?:\.\*)?);")
def order_imports_like_fdt(a, b):
if a.startswith("flash."):
if b.startswith("flash."):
return cmp(a, b)
else:
return 1
elif b.startswith("flash."):
return -1
else:
return cmp(a, b)
class OrganizeImports(object):
def __init__(self, output):
self.output = output
self.import_group = None
self.current_indent = None
def flush_group(self):
last_tld = None
if self.import_group is not None and len(self.import_group) > 0:
for i in sorted(self.import_group, order_imports_like_fdt):
tld = i.split(".", 1)[0]
if last_tld is not None and tld != last_tld:
self.output.write("")
last_tld = tld
self.output.write("{0}import {1};".format(
self.current_indent, i))
self.output.write("")
self.output.write("")
self.import_group = None
self.current_indent = None
def write(self, line):
import_match = import_stmt.match(line)
if import_match is not None:
line_indent = import_match.group(1)
line_import = import_match.group(2)
if line_indent != self.current_indent:
self.flush_group()
if self.import_group is None:
self.import_group = set()
self.current_indent = line_indent
self.import_group.add(line_import)
elif len(line) == 0:
if self.current_indent is None:
self.output.write("")
else:
self.flush_group()
self.output.write(line)
def close(self):
self.flush_group()
self.output.close()
package_stmt = re.compile(r"package(\s+[A-Za-z$_][A-Za-z0-9$_.]*)?\s*{?")
PO_PASS_THROUGH = 0
PO_EXPECT_PACKAGE = 1
PO_EXPECT_OBRACE = 2
PO_IN_PACKAGE = 3
class PackageOutdenter(object):
def __init__(self, output):
self.output = output
self.state = PO_EXPECT_PACKAGE
def write(self, line):
suppress_line = False
if len(line) == 0 or self.state == PO_PASS_THROUGH:
pass
elif self.state == PO_EXPECT_PACKAGE:
package_match = package_stmt.match(line)
if package_match is not None:
if line.endswith("{"):
self.state = PO_IN_PACKAGE
else:
self.state = PO_EXPECT_OBRACE
else:
self.state = PO_PASS_THROUGH
elif self.state == PO_EXPECT_OBRACE:
if line == "{":
self.state = PO_IN_PACKAGE
else:
self.state = PO_PASS_THROUGH
elif self.state == PO_IN_PACKAGE:
if line.startswith("\t"):
suppress_line = True
self.output.write(line[1:])
elif line.startswith(" "):
suppress_line = True
self.output.write(line[4:])
else:
self.state = PO_PASS_THROUGH
if not suppress_line:
self.output.write(line)
def close(self):
self.output.close()
doc_open = re.compile(r"(\s*)/\*\*$")
doc_line = re.compile(r"(\s*) \*(/?)(.*)")
DG_PASS_THROUGH = 0
DG_EXPECT_OPEN = 1
DG_IN_DOC_COMMENT = 2
class DocGobbler(object):
def __init__(self, output):
self.output = output
self.state = DG_EXPECT_OPEN
self.current_indent = None
self.comment_opener = None
self.comment_lines = None
self.comment_content = False
self.comment_closer = None
def write_if_not_none(self, line):
if line is not None:
self.output.write(line)
def flush_comment(self, unconditional):
if (unconditional or self.comment_content):
self.write_if_not_none(self.comment_opener)
for line in self.comment_lines:
self.write_if_not_none(line)
self.write_if_not_none(self.comment_closer)
self.current_indent = None
self.comment_opener = None
self.comment_lines = None
self.comment_content = False
self.comment_closer = None
def emit_comment_line(self, trailer):
return trailer.find("@author") == -1 and trailer != " ..."
def write(self, line):
if self.state == DG_EXPECT_OPEN:
open_match = doc_open.match(line)
if open_match is not None:
self.current_indent = open_match.group(1)
self.comment_opener = line
self.comment_lines = list()
self.state = DG_IN_DOC_COMMENT
else:
self.output.write(line)
elif self.state == DG_IN_DOC_COMMENT:
line_match = doc_line.match(line)
if line_match is not None:
line_indent = line_match.group(1)
close_slash = line_match.group(2)
trailer = line_match.group(3).rstrip()
if line_indent != self.current_indent:
self.flush_comment(True)
self.output.write(line)
self.state = DG_EXPECT_OPEN
elif close_slash == "/":
self.comment_closer = line
self.flush_comment(False)
self.state = DG_EXPECT_OPEN
else:
if self.emit_comment_line(trailer):
self.comment_lines.append(line)
self.comment_content = self.comment_content or len(trailer) > 0
else:
self.output.write(line)
def close(self):
if self.state == DG_IN_DOC_COMMENT:
self.flush_comment(True)
self.output.close()
decorator_order = {
'public': 10,
'protected': 20,
'private': 30,
'internal': 40,
'override': 50,
'final': 60,
'native': 70,
'static': 80,
}
member_decl = set(["const", "function", "var"])
class Redecorator(object):
def __init__(self, output):
self.output = output
def write(self, line):
tokens = filter(lambda t: len(t) > 0, re.split(r"(\s+)", line))
if len(tokens) > 0:
line_dec = list()
if tokens[0].isspace():
indent = tokens.pop(0)
else:
indent = ""
while len(tokens) > 0:
t = tokens.pop(0)
if t.isspace():
pass
elif t in decorator_order:
# bundle the keyword with its trailing whitespace to
# preserve line length
line_dec.append((t, tokens.pop(0)))
elif t in member_decl:
line_dec.sort(key=lambda m: decorator_order[m[0]])
line = indent + \
''.join(map(lambda m: m[0] + m[1], line_dec)) + \
t + ''.join(tokens)
self.output.write(line)
return
else:
break
self.output.write(line)
def close(self):
self.output.close()
class NewLiner(object):
def __init__(self, output):
self.output = output
def write(self, line):
self.output.write(line + "\n")
def close(self):
self.output.close()
class TabExpander(object):
def __init__(self, output):
self.output = output
def write(self, line):
self.output.write(line.replace("\t", " "))
def close(self):
self.output.close()
class Cleaner(object):
def __init__(self, output):
self.output = output
def write(self, line):
self.output.write(line.lstrip(u'\ufeff').rstrip())
def close(self):
self.output.close()
def reformat(in_stream, out_stream):
filter_chain = NewLiner(out_stream)
filter_chain = TabExpander(filter_chain)
filter_chain = Redecorator(filter_chain)
filter_chain = OrganizeImports(filter_chain)
filter_chain = PackageOutdenter(filter_chain)
filter_chain = DocGobbler(filter_chain)
filter_chain = Cleaner(filter_chain)
for line in in_stream:
filter_chain.write(line)
filter_chain.close()
def reformat_source(in_file, backup_suffix = None):
delete_backup = False
if backup_suffix is None:
backup_suffix = ".bak"
delete_backup = True
backup_file = in_file + backup_suffix
shutil.copy2(in_file, backup_file)
in_stream = codecs.open(backup_file, 'r', 'utf_8')
out_stream = codecs.open(in_file, 'w', 'utf_8')
reformat(in_stream, out_stream)
out_stream.close()
in_stream.close()
if delete_backup:
os.remove(backup_file)
def main():
if len(sys.argv) < 2:
print("Usage: {0} file.as ...".format(sys.argv[0]))
sys.exit(2)
else:
for in_file in sys.argv[1:]:
reformat_source(in_file, "~")
if __name__ == '__main__':
main()