ParseTreePatternMatcher class
A tree pattern matching mechanism for ANTLR ParseTrees.
Patterns are strings of source input text with special tags representing token or rule references such as:
{@code
Given a pattern start rule such as [statement], this object constructs a [ParseTree] with placeholders for the [ID] and [expr] subtree. Then the {@link #match} routines can compare an actual [ParseTree] from a parse with this pattern. Tag {@code
Pattern {@code x = 0;} is a similar pattern that matches the same pattern except that it requires the identifier to be [x] and the expression to be {@code 0}.
The {@link #matches} routines return [true] or [false] based upon a match for the tree rooted at the parameter sent in. The {@link #match} routines return a [ParseTreeMatch] object that contains the parse tree, the parse tree pattern, and a map from tag name to matched nodes (more below). A subtree that fails to match, returns with {@link ParseTreeMatch#mismatchedNode} set to the first tree node that did not match.
For efficiency, you can compile a tree pattern in string form to a [ParseTreePattern] object.
See [TestParseTreeMatcher] for lots of examples. [ParseTreePattern] has two static helper methods: {@link ParseTreePattern#findAll} and {@link ParseTreePattern#match} that are easy to use but not super efficient because they create new [ParseTreePatternMatcher] objects each time and have to compile the pattern in string form before using it.
The lexer and parser that you pass into the [ParseTreePatternMatcher] constructor are used to parse the pattern in string form. The lexer converts the {@code
Normally a parser does not accept token {@code
Delimiters are {@code <} and {@code >}, with {@code \} as the escape string by default, but you can set them to whatever you want using {@link #setDelimiters}. You must escape both start and stop strings {@code \<} and {@code \>}.
Constructors
- ParseTreePatternMatcher(Lexer lexer, Parser parser)
- Constructs a ParseTreePatternMatcher or from a Lexer and Parser object. The lexer input stream is altered for tokenizing the tree patterns. The parser is used as a convenient mechanism to get the grammar name, plus token, rule names.
Properties
- escape ↔ String
-
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- lexer → Lexer
-
Used to convert the tree pattern string into a series of tokens. The
input stream is reset.
final
- parser → Parser
-
Used to collect to the grammar file name, token names, rule names for
used to parse the pattern into a parse tree.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- start ↔ String
-
getter/setter pair
- stop ↔ String
-
getter/setter pair
Methods
-
compile(
String pattern, int patternRuleIndex) → ParseTreePattern - For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.
-
getRuleTagToken(
ParseTree t) → RuleTagToken? -
Is
t
{@code (expr -
match(
ParseTree tree, {ParseTreePattern? pattern, String? patternStr, int? patternRuleIndex}) → ParseTreeMatch -
Compare
pattern
matched againsttree
and return a ParseTreeMatch object that contains the matched elements, or the node at which the match failed. Pass in a compiled pattern instead of a string representation of a tree pattern. -
matches(
ParseTree tree, {ParseTreePattern? pattern, String? patternStr, int? patternRuleIndex}) → bool -
Does
pattern
matched as rule patternRuleIndex match tree? Pass in a compiled pattern instead of a string representation of a tree pattern. -
matchImpl(
ParseTree tree, ParseTree patternTree, MultiMap< String, ParseTree> labels) → ParseTree? -
Recursively walk
tree
againstpatternTree
, filling {@code match.}{@link ParseTreeMatch#labels labels}. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setDelimiters(
String start, String stop, String escapeLeft) → void - Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.
-
split(
String pattern) → List< Chunk> - Split {@code
-
tokenize(
String pattern) → List< Token> -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited