Patterns are strings of source input text with special tags representing token or rule references such as:
<ID> = <expr>;
Given a pattern start rule such as statement
, this object constructs a Antlr4.Runtime.Tree.IParseTree with placeholders for the ID
and expr
subtree. Then the Match(Antlr4.Runtime.Tree.IParseTree, ParseTreePattern) routines can compare an actual Antlr4.Runtime.Tree.IParseTree from a parse with this pattern. Tag <ID>
matches any ID
token and tag <expr>
references the result of the expr
rule (generally an instance of ExprContext
.
Pattern 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 0
.
The Matches(Antlr4.Runtime.Tree.IParseTree, ParseTreePattern) routines return
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: ParseTreePattern.FindAll(Antlr4.Runtime.Tree.IParseTree, string) and ParseTreePattern.Match(Antlr4.Runtime.Tree.IParseTree) 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 <ID> = <expr>;
into a sequence of four tokens (assuming lexer throws out whitespace or puts it on a hidden channel). Be aware that the input stream is reset for the lexer (but not the parser; a Antlr4.Runtime.ParserInterpreter is created to parse the input.). Any user-defined fields you have put into the lexer might get changed when this mechanism asks it to scan the pattern string.
Normally a parser does not accept token <expr>
as a valid expr
but, from the parser passed in, we create a special version of the underlying grammar representation (an Antlr4.Runtime.Atn.ATN ) that allows imaginary tokens representing rules ( <expr>
) to match entire rules. We call these bypass alternatives.
Delimiters are <
and >
, with \
as the escape string by default, but you can set them to whatever you want using SetDelimiters(string, string, string) . You must escape both start and stop strings \<
and \>
.