Index: tools/telemetry/catapult_base/refactor/annotated_symbol/__init__.py |
diff --git a/tools/telemetry/catapult_base/refactor/annotated_symbol/__init__.py b/tools/telemetry/catapult_base/refactor/annotated_symbol/__init__.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..32858b523d5275aecb0abd21efe11e568c9a87be |
--- /dev/null |
+++ b/tools/telemetry/catapult_base/refactor/annotated_symbol/__init__.py |
@@ -0,0 +1,71 @@ |
+# Copyright 2015 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+# pylint: disable=wildcard-import |
+from catapult_base.refactor.annotated_symbol.class_definition import * |
+from catapult_base.refactor.annotated_symbol.function_definition import * |
+from catapult_base.refactor.annotated_symbol.import_statement import * |
+from catapult_base.refactor.annotated_symbol.reference import * |
+from catapult_base.refactor import snippet |
+ |
+ |
+__all__ = [ |
+ 'Annotate', |
+ |
+ 'Class', |
+ 'Function', |
+ 'Import', |
+ 'Reference', |
+] |
+ |
+ |
+# Specific symbol types with extra methods for manipulating them. |
+# Python's full grammar is here: |
+# https://docs.python.org/2/reference/grammar.html |
+ |
+# Annotated Symbols have an Annotate classmethod that takes a symbol type and |
+# list of children, and returns an instance of that annotated Symbol. |
+ |
+ANNOTATED_SYMBOLS = ( |
+ AsName, |
+ Class, |
+ DottedName, |
+ ImportFrom, |
+ ImportName, |
+ Function, |
+) |
+ |
+ |
+# Unfortunately, some logical groupings are not represented by a node in the |
+# parse tree. To work around this, some annotated Symbols have an Annotate |
+# classmethod that takes and returns a list of Snippets instead. |
+ |
+ANNOTATED_GROUPINGS = ( |
+ Reference, |
+) |
+ |
+ |
+def Annotate(f): |
+ """Return the syntax tree of the given file.""" |
+ return _AnnotateNode(snippet.Snippetize(f)) |
+ |
+ |
+def _AnnotateNode(node): |
+ if not isinstance(node, snippet.Symbol): |
+ return node |
+ |
+ children = map(_AnnotateNode, node.children) |
+ |
+ for symbol_type in ANNOTATED_GROUPINGS: |
+ annotated_grouping = symbol_type.Annotate(children) |
+ if annotated_grouping: |
+ children = annotated_grouping |
+ break |
+ |
+ for symbol_type in ANNOTATED_SYMBOLS: |
+ annotated_symbol = symbol_type.Annotate(node.type, children) |
+ if annotated_symbol: |
+ return annotated_symbol |
+ |
+ return snippet.Symbol(node.type, children) |