| OLD | NEW |
| (Empty) |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 # pylint: disable=wildcard-import | |
| 6 from catapult_base.refactor.annotated_symbol.class_definition import * | |
| 7 from catapult_base.refactor.annotated_symbol.function_definition import * | |
| 8 from catapult_base.refactor.annotated_symbol.import_statement import * | |
| 9 from catapult_base.refactor.annotated_symbol.reference import * | |
| 10 from catapult_base.refactor import snippet | |
| 11 | |
| 12 | |
| 13 __all__ = [ | |
| 14 'Annotate', | |
| 15 | |
| 16 'Class', | |
| 17 'Function', | |
| 18 'Import', | |
| 19 'Reference', | |
| 20 ] | |
| 21 | |
| 22 | |
| 23 # Specific symbol types with extra methods for manipulating them. | |
| 24 # Python's full grammar is here: | |
| 25 # https://docs.python.org/2/reference/grammar.html | |
| 26 | |
| 27 # Annotated Symbols have an Annotate classmethod that takes a symbol type and | |
| 28 # list of children, and returns an instance of that annotated Symbol. | |
| 29 | |
| 30 ANNOTATED_SYMBOLS = ( | |
| 31 AsName, | |
| 32 Class, | |
| 33 DottedName, | |
| 34 ImportFrom, | |
| 35 ImportName, | |
| 36 Function, | |
| 37 ) | |
| 38 | |
| 39 | |
| 40 # Unfortunately, some logical groupings are not represented by a node in the | |
| 41 # parse tree. To work around this, some annotated Symbols have an Annotate | |
| 42 # classmethod that takes and returns a list of Snippets instead. | |
| 43 | |
| 44 ANNOTATED_GROUPINGS = ( | |
| 45 Reference, | |
| 46 ) | |
| 47 | |
| 48 | |
| 49 def Annotate(f): | |
| 50 """Return the syntax tree of the given file.""" | |
| 51 return _AnnotateNode(snippet.Snippetize(f)) | |
| 52 | |
| 53 | |
| 54 def _AnnotateNode(node): | |
| 55 if not isinstance(node, snippet.Symbol): | |
| 56 return node | |
| 57 | |
| 58 children = map(_AnnotateNode, node.children) | |
| 59 | |
| 60 for symbol_type in ANNOTATED_GROUPINGS: | |
| 61 annotated_grouping = symbol_type.Annotate(children) | |
| 62 if annotated_grouping: | |
| 63 children = annotated_grouping | |
| 64 break | |
| 65 | |
| 66 for symbol_type in ANNOTATED_SYMBOLS: | |
| 67 annotated_symbol = symbol_type.Annotate(node.type, children) | |
| 68 if annotated_symbol: | |
| 69 return annotated_symbol | |
| 70 | |
| 71 return snippet.Symbol(node.type, children) | |
| OLD | NEW |