ParseTreeMatch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. # Represents the result of matching a {@link ParseTree} against a tree pattern.
  8. #
  9. from io import StringIO
  10. from antlr4.tree.ParseTreePattern import ParseTreePattern
  11. from antlr4.tree.Tree import ParseTree
  12. class ParseTreeMatch(object):
  13. __slots__ = ('tree', 'pattern', 'labels', 'mismatchedNode')
  14. #
  15. # Constructs a new instance of {@link ParseTreeMatch} from the specified
  16. # parse tree and pattern.
  17. #
  18. # @param tree The parse tree to match against the pattern.
  19. # @param pattern The parse tree pattern.
  20. # @param labels A mapping from label names to collections of
  21. # {@link ParseTree} objects located by the tree pattern matching process.
  22. # @param mismatchedNode The first node which failed to match the tree
  23. # pattern during the matching process.
  24. #
  25. # @exception IllegalArgumentException if {@code tree} is {@code null}
  26. # @exception IllegalArgumentException if {@code pattern} is {@code null}
  27. # @exception IllegalArgumentException if {@code labels} is {@code null}
  28. #
  29. def __init__(self, tree:ParseTree, pattern:ParseTreePattern, labels:dict, mismatchedNode:ParseTree):
  30. if tree is None:
  31. raise Exception("tree cannot be null")
  32. if pattern is None:
  33. raise Exception("pattern cannot be null")
  34. if labels is None:
  35. raise Exception("labels cannot be null")
  36. self.tree = tree
  37. self.pattern = pattern
  38. self.labels = labels
  39. self.mismatchedNode = mismatchedNode
  40. #
  41. # Get the last node associated with a specific {@code label}.
  42. #
  43. # <p>For example, for pattern {@code <id:ID>}, {@code get("id")} returns the
  44. # node matched for that {@code ID}. If more than one node
  45. # matched the specified label, only the last is returned. If there is
  46. # no node associated with the label, this returns {@code null}.</p>
  47. #
  48. # <p>Pattern tags like {@code <ID>} and {@code <expr>} without labels are
  49. # considered to be labeled with {@code ID} and {@code expr}, respectively.</p>
  50. #
  51. # @param label The label to check.
  52. #
  53. # @return The last {@link ParseTree} to match a tag with the specified
  54. # label, or {@code null} if no parse tree matched a tag with the label.
  55. #
  56. def get(self, label:str):
  57. parseTrees = self.labels.get(label, None)
  58. if parseTrees is None or len(parseTrees)==0:
  59. return None
  60. else:
  61. return parseTrees[len(parseTrees)-1]
  62. #
  63. # Return all nodes matching a rule or token tag with the specified label.
  64. #
  65. # <p>If the {@code label} is the name of a parser rule or token in the
  66. # grammar, the resulting list will contain both the parse trees matching
  67. # rule or tags explicitly labeled with the label and the complete set of
  68. # parse trees matching the labeled and unlabeled tags in the pattern for
  69. # the parser rule or token. For example, if {@code label} is {@code "foo"},
  70. # the result will contain <em>all</em> of the following.</p>
  71. #
  72. # <ul>
  73. # <li>Parse tree nodes matching tags of the form {@code <foo:anyRuleName>} and
  74. # {@code <foo:AnyTokenName>}.</li>
  75. # <li>Parse tree nodes matching tags of the form {@code <anyLabel:foo>}.</li>
  76. # <li>Parse tree nodes matching tags of the form {@code <foo>}.</li>
  77. # </ul>
  78. #
  79. # @param label The label.
  80. #
  81. # @return A collection of all {@link ParseTree} nodes matching tags with
  82. # the specified {@code label}. If no nodes matched the label, an empty list
  83. # is returned.
  84. #
  85. def getAll(self, label:str):
  86. nodes = self.labels.get(label, None)
  87. if nodes is None:
  88. return list()
  89. else:
  90. return nodes
  91. #
  92. # Gets a value indicating whether the match operation succeeded.
  93. #
  94. # @return {@code true} if the match operation succeeded; otherwise,
  95. # {@code false}.
  96. #
  97. def succeeded(self):
  98. return self.mismatchedNode is None
  99. #
  100. # {@inheritDoc}
  101. #
  102. def __str__(self):
  103. with StringIO() as buf:
  104. buf.write("Match ")
  105. buf.write("succeeded" if self.succeeded() else "failed")
  106. buf.write("; found ")
  107. buf.write(str(len(self.labels)))
  108. buf.write(" labels")
  109. return buf.getvalue()