• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Insert "continuation" nodes into lib2to3 tree.
15
16The "backslash-newline" continuation marker is shoved into the node's prefix.
17Pull them out and make it into nodes of their own.
18
19  SpliceContinuations(): the main function exported by this module.
20"""
21
22from lib2to3 import pytree
23
24from yapf.yapflib import format_token
25
26
27def SpliceContinuations(tree):
28  """Given a pytree, splice the continuation marker into nodes.
29
30  Arguments:
31    tree: (pytree.Node) The tree to work on. The tree is modified by this
32      function.
33  """
34
35  def RecSplicer(node):
36    """Inserts a continuation marker into the node."""
37    if isinstance(node, pytree.Leaf):
38      if node.prefix.lstrip().startswith('\\\n'):
39        new_lineno = node.lineno - node.prefix.count('\n')
40        return pytree.Leaf(
41            type=format_token.CONTINUATION,
42            value=node.prefix,
43            context=('', (new_lineno, 0)))
44      return None
45    num_inserted = 0
46    for index, child in enumerate(node.children[:]):
47      continuation_node = RecSplicer(child)
48      if continuation_node:
49        node.children.insert(index + num_inserted, continuation_node)
50        num_inserted += 1
51
52  RecSplicer(tree)
53