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.
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
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
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.
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.
44 ANNOTATED_GROUPINGS
= (
50 """Return the syntax tree of the given file."""
51 return _AnnotateNode(snippet
.Snippetize(f
))
54 def _AnnotateNode(node
):
55 if not isinstance(node
, snippet
.Symbol
):
58 children
= map(_AnnotateNode
, node
.children
)
60 for symbol_type
in ANNOTATED_GROUPINGS
:
61 annotated_grouping
= symbol_type
.Annotate(children
)
62 if annotated_grouping
:
63 children
= annotated_grouping
66 for symbol_type
in ANNOTATED_SYMBOLS
:
67 annotated_symbol
= symbol_type
.Annotate(node
.type, children
)
69 return annotated_symbol
71 return snippet
.Symbol(node
.type, children
)