continuation_splicer.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. The "backslash-newline" continuation marker is shoved into the node's prefix.
  16. Pull them out and make it into nodes of their own.
  17. SpliceContinuations(): the main function exported by this module.
  18. """
  19. from yapf_third_party._ylib2to3 import pytree
  20. from yapf.yapflib import format_token
  21. def SpliceContinuations(tree):
  22. """Given a pytree, splice the continuation marker into nodes.
  23. Arguments:
  24. tree: (pytree.Node) The tree to work on. The tree is modified by this
  25. function.
  26. """
  27. def RecSplicer(node):
  28. """Inserts a continuation marker into the node."""
  29. if isinstance(node, pytree.Leaf):
  30. if node.prefix.lstrip().startswith('\\\n'):
  31. new_lineno = node.lineno - node.prefix.count('\n')
  32. return pytree.Leaf(
  33. type=format_token.CONTINUATION,
  34. value=node.prefix,
  35. context=('', (new_lineno, 0)))
  36. return None
  37. num_inserted = 0
  38. for index, child in enumerate(node.children[:]):
  39. continuation_node = RecSplicer(child)
  40. if continuation_node:
  41. node.children.insert(index + num_inserted, continuation_node)
  42. num_inserted += 1
  43. RecSplicer(tree)