pyparser_utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2022 Bill Wendling, 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. """PyParser-related utilities.
  15. This module collects various utilities related to the parse trees produced by
  16. the pyparser.
  17. GetLogicalLine: produces a list of tokens from the logical lines within a
  18. range.
  19. GetTokensInSubRange: produces a sublist of tokens from a current token list
  20. within a range.
  21. GetTokenIndex: Get the index of a token.
  22. GetNextTokenIndex: Get the index of the next token after a given position.
  23. GetPrevTokenIndex: Get the index of the previous token before a given
  24. position.
  25. TokenStart: Convenience function to return the token's start as a tuple.
  26. TokenEnd: Convenience function to return the token's end as a tuple.
  27. """
  28. def GetLogicalLine(logical_lines, node):
  29. """Get a list of tokens within the node's range from the logical lines."""
  30. start = TokenStart(node)
  31. end = TokenEnd(node)
  32. tokens = []
  33. for line in logical_lines:
  34. if line.start > end:
  35. break
  36. if line.start <= start or line.end >= end:
  37. tokens.extend(GetTokensInSubRange(line.tokens, node))
  38. return tokens
  39. def GetTokensInSubRange(tokens, node):
  40. """Get a subset of tokens representing the node."""
  41. start = TokenStart(node)
  42. end = TokenEnd(node)
  43. tokens_in_range = []
  44. for tok in tokens:
  45. tok_range = (tok.lineno, tok.column)
  46. if tok_range >= start and tok_range < end:
  47. tokens_in_range.append(tok)
  48. return tokens_in_range
  49. def GetTokenIndex(tokens, pos):
  50. """Get the index of the token at pos."""
  51. for index, token in enumerate(tokens):
  52. if (token.lineno, token.column) == pos:
  53. return index
  54. return None
  55. def GetNextTokenIndex(tokens, pos):
  56. """Get the index of the next token after pos."""
  57. for index, token in enumerate(tokens):
  58. if (token.lineno, token.column) >= pos:
  59. return index
  60. return None
  61. def GetPrevTokenIndex(tokens, pos):
  62. """Get the index of the previous token before pos."""
  63. for index, token in enumerate(tokens):
  64. if index > 0 and (token.lineno, token.column) >= pos:
  65. return index - 1
  66. return None
  67. def TokenStart(node):
  68. return (node.lineno, node.col_offset)
  69. def TokenEnd(node):
  70. return (node.end_lineno, node.end_col_offset)
  71. #############################################################################
  72. # Code for debugging #
  73. #############################################################################
  74. def AstDump(node):
  75. import ast
  76. print(ast.dump(node, include_attributes=True, indent=4))