OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. | 5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. |
6 | 6 |
7 Design doc: http://www.chromium.org/developers/design-documents/idl-build | 7 Design doc: http://www.chromium.org/developers/design-documents/idl-build |
8 """ | 8 """ |
9 | 9 |
10 import os | 10 import os |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 idl_type = union_type | 400 idl_type = union_type |
401 if union_type.is_nullable: | 401 if union_type.is_nullable: |
402 idl_type = union_type.inner_type | 402 idl_type = union_type.inner_type |
403 name = idl_type.cpp_type or idl_type.name | 403 name = idl_type.cpp_type or idl_type.name |
404 alias = aliases.get(name) | 404 alias = aliases.get(name) |
405 if alias: | 405 if alias: |
406 return alias | 406 return alias |
407 return name | 407 return name |
408 | 408 |
409 | 409 |
| 410 def format_remove_duplicates(text, patterns): |
| 411 """Removes duplicated line-basis patterns. |
| 412 |
| 413 Based on simple pattern matching, removes duplicated lines in a block |
| 414 of lines. Lines that match with a same pattern are considered as |
| 415 duplicates. |
| 416 |
| 417 Designed to be used as a filter function for Jinja2. |
| 418 |
| 419 Args: |
| 420 text: A str of multi-line text. |
| 421 patterns: A list of str where each str represents a simple |
| 422 pattern. The patterns are not considered as regexp, and |
| 423 exact match is applied. |
| 424 |
| 425 Returns: |
| 426 A formatted str with duplicates removed. |
| 427 """ |
| 428 pattern_founds = [False] * len(patterns) |
| 429 output = [] |
| 430 for line in text.split('\n'): |
| 431 to_be_removed = False |
| 432 for i, pattern in enumerate(patterns): |
| 433 if pattern not in line: |
| 434 continue |
| 435 if pattern_founds[i]: |
| 436 to_be_removed = True |
| 437 else: |
| 438 pattern_founds[i] = True |
| 439 if to_be_removed: |
| 440 continue |
| 441 output.append(line) |
| 442 |
| 443 # Let |'\n'.join| emit the last newline. |
| 444 if output: |
| 445 output.append('') |
| 446 |
| 447 return '\n'.join(output) |
| 448 |
| 449 |
410 def format_blink_cpp_source_code(text): | 450 def format_blink_cpp_source_code(text): |
411 """Formats C++ source code. | 451 """Formats C++ source code. |
412 | 452 |
413 Supported modifications are: | 453 Supported modifications are: |
414 - Reduces successive empty lines into a single empty line. | 454 - Reduces successive empty lines into a single empty line. |
415 - Removes empty lines just after an open brace or before closing brace. | 455 - Removes empty lines just after an open brace or before closing brace. |
416 This rule does not apply to namespaces. | 456 This rule does not apply to namespaces. |
417 | 457 |
418 Designed to be used as a filter function for Jinja2. | 458 Designed to be used as a filter function for Jinja2. |
419 | 459 |
420 Args: | 460 Args: |
421 text: A str of C++ source code. | 461 text: A str of C++ source code. |
422 | 462 |
423 Returns: | 463 Returns: |
424 A formatted str of the source code. | 464 A formatted str of the source code. |
425 """ | 465 """ |
| 466 re_empty_line = re.compile(r'^\s*$') |
426 re_first_brace = re.compile(r'(?P<first>[{}])') | 467 re_first_brace = re.compile(r'(?P<first>[{}])') |
427 re_last_brace = re.compile(r'.*(?P<last>[{}]).*?$') | 468 re_last_brace = re.compile(r'.*(?P<last>[{}]).*?$') |
428 was_open_brace = True # Trick to remove the empty lines at the beginning. | 469 was_open_brace = True # Trick to remove the empty lines at the beginning. |
429 was_empty_line = False | 470 was_empty_line = False |
430 output = [] | 471 output = [] |
431 for line in text.split('\n'): | 472 for line in text.split('\n'): |
432 # Skip empty lines. | 473 # Skip empty lines. |
433 if not line: # empty line | 474 if re_empty_line.match(line): |
434 was_empty_line = True | 475 was_empty_line = True |
435 continue | 476 continue |
436 | 477 |
437 # Emit a single empty line if needed. | 478 # Emit a single empty line if needed. |
438 if was_empty_line: | 479 if was_empty_line: |
439 was_empty_line = False | 480 was_empty_line = False |
440 match = re_first_brace.search(line) | 481 match = re_first_brace.search(line) |
441 if was_open_brace: | 482 if was_open_brace: |
442 # No empty line just after an open brace. | 483 # No empty line just after an open brace. |
443 pass | 484 pass |
444 elif match and match.group('first') == '}' and 'namespace' not in li
ne: | 485 elif match and match.group('first') == '}' and 'namespace' not in li
ne: |
445 # No empty line just before a closing brace. | 486 # No empty line just before a closing brace. |
446 pass | 487 pass |
447 else: | 488 else: |
448 # Preserve a single empty line. | 489 # Preserve a single empty line. |
449 output.append('') | 490 output.append('') |
450 | 491 |
451 # Emit the line itself. | 492 # Emit the line itself. |
452 output.append(line) | 493 output.append(line) |
453 | 494 |
454 # Remember an open brace. | 495 # Remember an open brace. |
455 match = re_last_brace.search(line) | 496 match = re_last_brace.search(line) |
456 was_open_brace = (match and match.group('last') == '{' and 'namespace' n
ot in line) | 497 was_open_brace = (match and match.group('last') == '{' and 'namespace' n
ot in line) |
457 | 498 |
458 # Let |'\n'.join| emit the last newline. | 499 # Let |'\n'.join| emit the last newline. |
459 if output: | 500 if output: |
460 output.append('') | 501 output.append('') |
461 | 502 |
462 return '\n'.join(output) | 503 return '\n'.join(output) |
OLD | NEW |