ParseTreePattern.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #
  2. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  3. # Use of this file is governed by the BSD 3-clause license that
  4. # can be found in the LICENSE.txt file in the project root.
  5. #
  6. #
  7. # A pattern like {@code <ID> = <expr>;} converted to a {@link ParseTree} by
  8. # {@link ParseTreePatternMatcher#compile(String, int)}.
  9. #
  10. from antlr4.tree.ParseTreePatternMatcher import ParseTreePatternMatcher
  11. from antlr4.tree.Tree import ParseTree
  12. from antlr4.xpath.XPath import XPath
  13. class ParseTreePattern(object):
  14. __slots__ = ('matcher', 'patternRuleIndex', 'pattern', 'patternTree')
  15. # Construct a new instance of the {@link ParseTreePattern} class.
  16. #
  17. # @param matcher The {@link ParseTreePatternMatcher} which created this
  18. # tree pattern.
  19. # @param pattern The tree pattern in concrete syntax form.
  20. # @param patternRuleIndex The parser rule which serves as the root of the
  21. # tree pattern.
  22. # @param patternTree The tree pattern in {@link ParseTree} form.
  23. #
  24. def __init__(self, matcher:ParseTreePatternMatcher, pattern:str, patternRuleIndex:int , patternTree:ParseTree):
  25. self.matcher = matcher
  26. self.patternRuleIndex = patternRuleIndex
  27. self.pattern = pattern
  28. self.patternTree = patternTree
  29. #
  30. # Match a specific parse tree against this tree pattern.
  31. #
  32. # @param tree The parse tree to match against this tree pattern.
  33. # @return A {@link ParseTreeMatch} object describing the result of the
  34. # match operation. The {@link ParseTreeMatch#succeeded()} method can be
  35. # used to determine whether or not the match was successful.
  36. #
  37. def match(self, tree:ParseTree):
  38. return self.matcher.match(tree, self)
  39. #
  40. # Determine whether or not a parse tree matches this tree pattern.
  41. #
  42. # @param tree The parse tree to match against this tree pattern.
  43. # @return {@code true} if {@code tree} is a match for the current tree
  44. # pattern; otherwise, {@code false}.
  45. #
  46. def matches(self, tree:ParseTree):
  47. return self.matcher.match(tree, self).succeeded()
  48. # Find all nodes using XPath and then try to match those subtrees against
  49. # this tree pattern.
  50. #
  51. # @param tree The {@link ParseTree} to match against this pattern.
  52. # @param xpath An expression matching the nodes
  53. #
  54. # @return A collection of {@link ParseTreeMatch} objects describing the
  55. # successful matches. Unsuccessful matches are omitted from the result,
  56. # regardless of the reason for the failure.
  57. #
  58. def findAll(self, tree:ParseTree, xpath:str):
  59. subtrees = XPath.findAll(tree, xpath, self.matcher.parser)
  60. matches = list()
  61. for t in subtrees:
  62. match = self.match(t)
  63. if match.succeeded():
  64. matches.append(match)
  65. return matches