Class ParseTreeMatch
- java.lang.Object
-
- org.antlr.v4.runtime.tree.pattern.ParseTreeMatch
-
-
Constructor Summary
Constructors Constructor Description ParseTreeMatch(ParseTree tree, ParseTreePattern pattern, MultiMap<String,ParseTree> labels, ParseTree mismatchedNode)Constructs a new instance ofParseTreeMatchfrom the specified parse tree and pattern.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ParseTreeget(String label)Get the last node associated with a specificlabel.List<ParseTree>getAll(String label)Return all nodes matching a rule or token tag with the specified label.MultiMap<String,ParseTree>getLabels()Return a mapping from label → [list of nodes].ParseTreegetMismatchedNode()Get the node at which we first detected a mismatch.ParseTreePatterngetPattern()Get the tree pattern we are matching against.ParseTreegetTree()Get the parse tree we are trying to match to a pattern.booleansucceeded()Gets a value indicating whether the match operation succeeded.StringtoString()
-
-
-
Constructor Detail
-
ParseTreeMatch
public ParseTreeMatch(ParseTree tree, ParseTreePattern pattern, MultiMap<String,ParseTree> labels, ParseTree mismatchedNode)
Constructs a new instance ofParseTreeMatchfrom the specified parse tree and pattern.- Parameters:
tree- The parse tree to match against the pattern.pattern- The parse tree pattern.labels- A mapping from label names to collections ofParseTreeobjects located by the tree pattern matching process.mismatchedNode- The first node which failed to match the tree pattern during the matching process.- Throws:
IllegalArgumentException- iftreeisnullIllegalArgumentException- ifpatternisnullIllegalArgumentException- iflabelsisnull
-
-
Method Detail
-
get
public ParseTree get(String label)
Get the last node associated with a specificlabel.For example, for pattern
<id:ID>,get("id")returns the node matched for thatID. If more than one node matched the specified label, only the last is returned. If there is no node associated with the label, this returnsnull.Pattern tags like
<ID>and<expr>without labels are considered to be labeled withIDandexpr, respectively.- Parameters:
label- The label to check.- Returns:
- The last
ParseTreeto match a tag with the specified label, ornullif no parse tree matched a tag with the label.
-
getAll
public List<ParseTree> getAll(String label)
Return all nodes matching a rule or token tag with the specified label.If the
labelis 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, iflabelis"foo", the result will contain all of the following.- Parse tree nodes matching tags of the form
<foo:anyRuleName>and<foo:AnyTokenName>. - Parse tree nodes matching tags of the form
<anyLabel:foo>. - Parse tree nodes matching tags of the form
<foo>.
- Parameters:
label- The label.- Returns:
- A collection of all
ParseTreenodes matching tags with the specifiedlabel. If no nodes matched the label, an empty list is returned.
- Parse tree nodes matching tags of the form
-
getLabels
public MultiMap<String,ParseTree> getLabels()
Return a mapping from label → [list of nodes].The map includes special entries corresponding to the names of rules and tokens referenced in tags in the original pattern. For additional information, see the description of
getAll(String).- Returns:
- A mapping from labels to parse tree nodes. If the parse tree pattern did not contain any rule or token tags, this map will be empty.
-
getMismatchedNode
public ParseTree getMismatchedNode()
Get the node at which we first detected a mismatch.- Returns:
- the node at which we first detected a mismatch, or
nullif the match was successful.
-
succeeded
public boolean succeeded()
Gets a value indicating whether the match operation succeeded.- Returns:
trueif the match operation succeeded; otherwise,false.
-
getPattern
public ParseTreePattern getPattern()
Get the tree pattern we are matching against.- Returns:
- The tree pattern we are matching against.
-
getTree
public ParseTree getTree()
Get the parse tree we are trying to match to a pattern.- Returns:
- The
ParseTreewe are trying to match to a pattern.
-
-