OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 # fixcc -- indent and space lily's c++ code | 3 # fixcc -- indent and space lily's c++ code |
4 | 4 |
5 # This file is part of LilyPond, the GNU music typesetter. | 5 # This file is part of LilyPond, the GNU music typesetter. |
6 # | 6 # |
7 # LilyPond is free software: you can redistribute it and/or modify | 7 # LilyPond is free software: you can redistribute it and/or modify |
8 # it under the terms of the GNU General Public License as published by | 8 # it under the terms of the GNU General Public License as published by |
9 # the Free Software Foundation, either version 3 of the License, or | 9 # the Free Software Foundation, either version 3 of the License, or |
10 # (at your option) any later version. | 10 # (at your option) any later version. |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 self.options = [] | 194 self.options = [] |
195 self.format = format | 195 self.format = format |
196 | 196 |
197 def replacement_text (self): | 197 def replacement_text (self): |
198 return self.match.group ('match') | 198 return self.match.group ('match') |
199 | 199 |
200 def substring (self, s): | 200 def substring (self, s): |
201 return self.match.group (s) | 201 return self.match.group (s) |
202 | 202 |
203 def __repr__ (self): | 203 def __repr__ (self): |
204 return `self.__class__` + ' type = ' + self.type | 204 return repr(self.__class__) + ' type = ' + self.type |
205 | 205 |
206 class Multiline_comment (Snippet): | 206 class Multiline_comment (Snippet): |
207 def __init__ (self, source, match, format): | 207 def __init__ (self, source, match, format): |
208 self.type = type | 208 self.type = type |
209 self.match = match | 209 self.match = match |
210 self.hash = 0 | 210 self.hash = 0 |
211 self.options = [] | 211 self.options = [] |
212 self.format = format | 212 self.format = format |
213 | 213 |
214 def replacement_text (self): | 214 def replacement_text (self): |
(...skipping 19 matching lines...) Expand all Loading... |
234 res = {} | 234 res = {} |
235 for i in types: | 235 for i in types: |
236 res[i] = re.compile (snippet_res[format][i]) | 236 res[i] = re.compile (snippet_res[format][i]) |
237 | 237 |
238 snippets = [] | 238 snippets = [] |
239 index = 0 | 239 index = 0 |
240 ## found = dict (map (lambda x: (x, None), | 240 ## found = dict (map (lambda x: (x, None), |
241 ## types)) | 241 ## types)) |
242 ## urg python2.1 | 242 ## urg python2.1 |
243 found = {} | 243 found = {} |
244 map (lambda x, f = found: f.setdefault (x, None), | 244 list(map (lambda x, f = found: f.setdefault (x, None), |
245 types) | 245 types)) |
246 | 246 |
247 # We want to search for multiple regexes, without searching | 247 # We want to search for multiple regexes, without searching |
248 # the string multiple times for one regex. | 248 # the string multiple times for one regex. |
249 # Hence, we use earlier results to limit the string portion | 249 # Hence, we use earlier results to limit the string portion |
250 # where we search. | 250 # where we search. |
251 # Since every part of the string is traversed at most once for | 251 # Since every part of the string is traversed at most once for |
252 # every type of snippet, this is linear. | 252 # every type of snippet, this is linear. |
253 | 253 |
254 while 1: | 254 while 1: |
255 if verbose_p: | 255 if verbose_p: |
256 sys.stderr.write ('.') | 256 sys.stderr.write ('.') |
257 first = None | 257 first = None |
258 endex = 1 << 30 | 258 endex = 1 << 30 |
259 for type in types: | 259 for type in types: |
260 if not found[type] or found[type][0] < index: | 260 if not found[type] or found[type][0] < index: |
261 found[type] = None | 261 found[type] = None |
262 m = res[type].search (s[index:endex]) | 262 m = res[type].search (s[index:endex]) |
263 if not m: | 263 if not m: |
264 continue | 264 continue |
265 | 265 |
266 cl = Snippet | 266 cl = Snippet |
267 if snippet_type_to_class.has_key (type): | 267 if type in snippet_type_to_class: |
268 cl = snippet_type_to_class[type] | 268 cl = snippet_type_to_class[type] |
269 snip = cl (type, m, format) | 269 snip = cl (type, m, format) |
270 start = index + m.start ('match') | 270 start = index + m.start ('match') |
271 found[type] = (start, snip) | 271 found[type] = (start, snip) |
272 | 272 |
273 if found[type] \ | 273 if found[type] \ |
274 and (not first \ | 274 and (not first \ |
275 or found[type][0] < found[first][0]): | 275 or found[type][0] < found[first][0]): |
276 first = type | 276 first = type |
277 | 277 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 'singleline_comment', | 315 'singleline_comment', |
316 'string', | 316 'string', |
317 # 'char', | 317 # 'char', |
318 'include', | 318 'include', |
319 ) | 319 ) |
320 | 320 |
321 chunks = find_toplevel_snippets (t, snippet_types) | 321 chunks = find_toplevel_snippets (t, snippet_types) |
322 #code = filter (lambda x: is_derived_class (x.__class__, Substring), | 322 #code = filter (lambda x: is_derived_class (x.__class__, Substring), |
323 # chunks) | 323 # chunks) |
324 | 324 |
325 t = ''.join (map (lambda x: x.filter_text (), chunks)) | 325 t = ''.join ([x.filter_text () for x in chunks]) |
326 fixt = file | 326 fixt = file |
327 if s != t: | 327 if s != t: |
328 if not outdir: | 328 if not outdir: |
329 os.system ('mv %s %s~' % (file, file)) | 329 os.system ('mv %s %s~' % (file, file)) |
330 else:· | 330 else:· |
331 fixt = os.path.join (outdir, | 331 fixt = os.path.join (outdir, |
332 os.path.basename (file)) | 332 os.path.basename (file)) |
333 h = open (fixt, "w") | 333 h = open (fixt, "w") |
334 h.write (t) | 334 h.write (t) |
335 h.close () | 335 h.close () |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 411 |
412 | 412 |
413 outdir = 0 | 413 outdir = 0 |
414 format = CXX | 414 format = CXX |
415 socketdir = '/tmp/fixcc' | 415 socketdir = '/tmp/fixcc' |
416 socketname = 'fixcc%d' % os.getpid () | 416 socketname = 'fixcc%d' % os.getpid () |
417 | 417 |
418 def main (): | 418 def main (): |
419 files = do_options () | 419 files = do_options () |
420 if not check_astyle_version(): | 420 if not check_astyle_version(): |
421 print "Warning: try to use %s." % PREFERRED_ASTYLE_VERSION | 421 print("Warning: try to use %s." % PREFERRED_ASTYLE_VERSION) |
422 print "Please limit use of this version to files with changed code." | 422 print("Please limit use of this version to files with changed code.") |
423 if len(files) > 4: | 423 if len(files) > 4: |
424 print "Too many files with this version. See `astyle --help`" | 424 print("Too many files with this version. See `astyle --help`") |
425 sys.exit(1) | 425 sys.exit(1) |
426 if outdir and not os.path.isdir (outdir): | 426 if outdir and not os.path.isdir (outdir): |
427 os.makedirs (outdir) | 427 os.makedirs (outdir) |
428 for i in files: | 428 for i in files: |
429 sys.stderr.write ('%s...\n' % i) | 429 sys.stderr.write ('%s...\n' % i) |
430 nitpick_file (outdir, i) | 430 nitpick_file (outdir, i) |
431 | 431 |
432 | 432 |
433 ## TODO: make this compilable and check with g++ | 433 ## TODO: make this compilable and check with g++ |
434 TEST = ''' | 434 TEST = ''' |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 | 542 |
543 def test (): | 543 def test (): |
544 test_file = 'fixcc.cc' | 544 test_file = 'fixcc.cc' |
545 open (test_file, 'w').write (TEST) | 545 open (test_file, 'w').write (TEST) |
546 nitpick_file (outdir, test_file) | 546 nitpick_file (outdir, test_file) |
547 sys.stdout.write (open (test_file).read ()) | 547 sys.stdout.write (open (test_file).read ()) |
548 | 548 |
549 if __name__ == '__main__': | 549 if __name__ == '__main__': |
550 main () | 550 main () |
551 | 551 |
OLD | NEW |