Chromium Code Reviews| 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 for i, pattern in enumerate(patterns): | |
| 432 if pattern not in line: | |
| 433 continue | |
| 434 if pattern_founds[i]: | |
| 435 line = '' | |
|
peria
2016/09/09 02:15:24
Do we have to add an empty line?
Yuki
2016/09/14 07:10:59
Done.
| |
| 436 else: | |
| 437 pattern_founds[i] = True | |
| 438 output.append(line) | |
| 439 | |
| 440 # Let |'\n'.join| emit the last newline. | |
| 441 if output: | |
| 442 output.append('') | |
| 443 | |
| 444 return '\n'.join(output) | |
| 445 | |
| 446 | |
| 410 def format_blink_cpp_source_code(text): | 447 def format_blink_cpp_source_code(text): |
| 411 """Formats C++ source code. | 448 """Formats C++ source code. |
| 412 | 449 |
| 413 Supported modifications are: | 450 Supported modifications are: |
| 414 - Reduces successive empty lines into a single empty line. | 451 - Reduces successive empty lines into a single empty line. |
| 415 - Removes empty lines just after an open brace or before closing brace. | 452 - Removes empty lines just after an open brace or before closing brace. |
| 416 This rule does not apply to namespaces. | 453 This rule does not apply to namespaces. |
| 417 | 454 |
| 418 Designed to be used as a filter function for Jinja2. | 455 Designed to be used as a filter function for Jinja2. |
| 419 | 456 |
| 420 Args: | 457 Args: |
| 421 text: A str of C++ source code. | 458 text: A str of C++ source code. |
| 422 | 459 |
| 423 Returns: | 460 Returns: |
| 424 A formatted str of the source code. | 461 A formatted str of the source code. |
| 425 """ | 462 """ |
| 463 re_empty_line = re.compile(r'^\s*$') | |
| 426 re_first_brace = re.compile(r'(?P<first>[{}])') | 464 re_first_brace = re.compile(r'(?P<first>[{}])') |
| 427 re_last_brace = re.compile(r'.*(?P<last>[{}]).*?$') | 465 re_last_brace = re.compile(r'.*(?P<last>[{}]).*?$') |
| 428 was_open_brace = True # Trick to remove the empty lines at the beginning. | 466 was_open_brace = True # Trick to remove the empty lines at the beginning. |
| 429 was_empty_line = False | 467 was_empty_line = False |
| 430 output = [] | 468 output = [] |
| 431 for line in text.split('\n'): | 469 for line in text.split('\n'): |
| 432 # Skip empty lines. | 470 # Skip empty lines. |
| 433 if not line: # empty line | 471 if re_empty_line.match(line): |
| 434 was_empty_line = True | 472 was_empty_line = True |
| 435 continue | 473 continue |
| 436 | 474 |
| 437 # Emit a single empty line if needed. | 475 # Emit a single empty line if needed. |
| 438 if was_empty_line: | 476 if was_empty_line: |
| 439 was_empty_line = False | 477 was_empty_line = False |
| 440 match = re_first_brace.search(line) | 478 match = re_first_brace.search(line) |
| 441 if was_open_brace: | 479 if was_open_brace: |
| 442 # No empty line just after an open brace. | 480 # No empty line just after an open brace. |
| 443 pass | 481 pass |
| 444 elif match and match.group('first') == '}' and 'namespace' not in li ne: | 482 elif match and match.group('first') == '}' and 'namespace' not in li ne: |
| 445 # No empty line just before a closing brace. | 483 # No empty line just before a closing brace. |
| 446 pass | 484 pass |
| 447 else: | 485 else: |
| 448 # Preserve a single empty line. | 486 # Preserve a single empty line. |
| 449 output.append('') | 487 output.append('') |
| 450 | 488 |
| 451 # Emit the line itself. | 489 # Emit the line itself. |
| 452 output.append(line) | 490 output.append(line) |
| 453 | 491 |
| 454 # Remember an open brace. | 492 # Remember an open brace. |
| 455 match = re_last_brace.search(line) | 493 match = re_last_brace.search(line) |
| 456 was_open_brace = (match and match.group('last') == '{' and 'namespace' n ot in line) | 494 was_open_brace = (match and match.group('last') == '{' and 'namespace' n ot in line) |
| 457 | 495 |
| 458 # Let |'\n'.join| emit the last newline. | 496 # Let |'\n'.join| emit the last newline. |
| 459 if output: | 497 if output: |
| 460 output.append('') | 498 output.append('') |
| 461 | 499 |
| 462 return '\n'.join(output) | 500 return '\n'.join(output) |
| OLD | NEW |