# # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # # # Represents the result of matching a {@link ParseTree} against a tree pattern. # from io import StringIO from antlr4.tree.ParseTreePattern import ParseTreePattern from antlr4.tree.Tree import ParseTree class ParseTreeMatch(object): # # Constructs a new instance of {@link ParseTreeMatch} from the specified # parse tree and pattern. # # @param tree The parse tree to match against the pattern. # @param pattern The parse tree pattern. # @param labels A mapping from label names to collections of # {@link ParseTree} objects located by the tree pattern matching process. # @param mismatchedNode The first node which failed to match the tree # pattern during the matching process. # # @exception IllegalArgumentException if {@code tree} is {@code null} # @exception IllegalArgumentException if {@code pattern} is {@code null} # @exception IllegalArgumentException if {@code labels} is {@code null} # def __init__(self, tree:ParseTree, pattern:ParseTreePattern, labels:dict, mismatchedNode:ParseTree): if tree is None: raise Exception("tree cannot be null") if pattern is None: raise Exception("pattern cannot be null") if labels is None: raise Exception("labels cannot be null") self.tree = tree self.pattern = pattern self.labels = labels self.mismatchedNode = mismatchedNode # # Get the last node associated with a specific {@code label}. # #
For example, for pattern {@code
Pattern tags like {@code
If the {@code label} is the name of a parser rule or token in the # grammar, the resulting list will contain both the parse trees matching # rule or tags explicitly labeled with the label and the complete set of # parse trees matching the labeled and unlabeled tags in the pattern for # the parser rule or token. For example, if {@code label} is {@code "foo"}, # the result will contain all of the following.
# #