Top Description Inners Fields Constructors Methods
org.antlr.v4.runtime.tree.pattern

public Class ParseTreePatternMatcher

extends Object
Class Inheritance
Imports
org.antlr.v4.runtime.ANTLRInputStream, .BailErrorStrategy, .CommonTokenStream, .Lexer, .ListTokenSource, .Parser, .ParserInterpreter, .ParserRuleContext, .RecognitionException, .Token, org.antlr.v4.runtime.atn.ATN, org.antlr.v4.runtime.misc.MultiMap, .ParseCancellationException, org.antlr.v4.runtime.tree.ParseTree, .RuleNode, .TerminalNode, java.util.ArrayList, .Arrays, .List

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:

<ID> = <expr>;

Given a pattern start rule such as statement, this object constructs a ParseTree with placeholders for the ID and expr subtree. Then the match routines can compare an actual ParseTree 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 routines return true or false based upon a match for the tree rooted at the parameter sent in. The 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 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: ParseTreePattern#findAll and 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 <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 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 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. You must escape both start and stop strings \< and \>.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class

Field Summary

Modifier and TypeField and Description
protected String
private final Lexer
lexer

This is the backing field for getLexer().

private final Parser
parser

This is the backing field for getParser().

protected String
protected String

Constructor Summary

AccessConstructor and Description
public
ParseTreePatternMatcher(Lexer lexer, Parser parser)

Constructs a ParseTreePatternMatcher or from a Lexer and Parser object.

Method Summary

Modifier and TypeMethod and Description
public ParseTreePattern
compile(String pattern, int patternRuleIndex)

For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.

public Lexer
getLexer()

Used to convert the tree pattern string into a series of tokens.

public Parser
getParser()

Used to collect to the grammar file name, token names, rule names for used to parse the pattern into a parse tree.

protected RuleTagToken
getRuleTagToken(ParseTree t)

Is t (expr <expr>) subtree?

public ParseTreeMatch
match(ParseTree tree, String pattern, int patternRuleIndex)

Compare pattern matched as rule patternRuleIndex against tree and return a ParseTreeMatch object that contains the matched elements, or the node at which the match failed.

public ParseTreeMatch
match(ParseTree tree, ParseTreePattern pattern)

Compare pattern matched against tree and return a ParseTreeMatch object that contains the matched elements, or the node at which the match failed.

public boolean
matches(ParseTree tree, String pattern, int patternRuleIndex)

Does pattern matched as rule patternRuleIndex match tree?

public boolean
matches(ParseTree tree, ParseTreePattern pattern)

Does pattern matched as rule patternRuleIndex match tree?

protected ParseTree

Returns:

the first node encountered in tree which does not match a corresponding node in patternTree, or null if the match was successful. The specific node returned depends on the matching algorithm used by the implementation, and may be overridden.
matchImpl
(ParseTree tree, ParseTree patternTree, MultiMap<String, ParseTree> labels)

Recursively walk tree against patternTree, filling match.

public void
setDelimiters(String
The start delimiter.
start
,
String
The stop delimiter.
stop
,
String
The escape sequence to use for escaping a start or stop delimiter.
escapeLeft
)

Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.

public List<Chunk>
split(String pattern)

Split <ID> = <e:expr> ; into 4 chunks for tokenizing by tokenize.

public List<? extends Token>
tokenize(String pattern)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

escapeback to summary
protected String escape
lexerback to summary
private final Lexer lexer

This is the backing field for getLexer().

parserback to summary
private final Parser parser

This is the backing field for getParser().

startback to summary
protected String start
stopback to summary
protected String stop

Constructor Detail

ParseTreePatternMatcherback to summary
public 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.

Method Detail

compileback to summary
public ParseTreePattern compile(String pattern, int patternRuleIndex)

For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.

getLexerback to summary
public Lexer getLexer()

Used to convert the tree pattern string into a series of tokens. The input stream is reset.

getParserback to summary
public Parser getParser()

Used to collect to the grammar file name, token names, rule names for used to parse the pattern into a parse tree.

getRuleTagTokenback to summary
protected RuleTagToken getRuleTagToken(ParseTree t)

Is t (expr <expr>) subtree?

matchback to summary
public ParseTreeMatch match(ParseTree tree, String pattern, int patternRuleIndex)

Compare pattern matched as rule patternRuleIndex against tree and return a ParseTreeMatch object that contains the matched elements, or the node at which the match failed.

matchback to summary
public ParseTreeMatch match(ParseTree tree, ParseTreePattern pattern)

Compare pattern matched against tree 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.

matchesback to summary
public boolean matches(ParseTree tree, String pattern, int patternRuleIndex)

Does pattern matched as rule patternRuleIndex match tree?

matchesback to summary
public boolean matches(ParseTree tree, ParseTreePattern pattern)

Does pattern matched as rule patternRuleIndex match tree? Pass in a compiled pattern instead of a string representation of a tree pattern.

matchImplback to summary
protected ParseTree matchImpl(ParseTree tree, ParseTree patternTree, MultiMap<String, ParseTree> labels)

Recursively walk tree against patternTree, filling match.labels.

Returns:ParseTree

the first node encountered in tree which does not match a corresponding node in patternTree, or null if the match was successful. The specific node returned depends on the matching algorithm used by the implementation, and may be overridden.

setDelimitersback to summary
public void setDelimiters(String start, String stop, String escapeLeft)

Set the delimiters used for marking rule and token tags within concrete syntax used by the tree pattern parser.

Parameters
start:String

The start delimiter.

stop:String

The stop delimiter.

escapeLeft:String

The escape sequence to use for escaping a start or stop delimiter.

Exceptions
IllegalArgumentException:
  • if start is null or empty.
  • if stop is null or empty.
splitback to summary
public List<Chunk> split(String pattern)

Split <ID> = <e:expr> ; into 4 chunks for tokenizing by tokenize.

tokenizeback to summary
public List<? extends Token> tokenize(String pattern)
org.antlr.v4.runtime.tree.pattern back to summary

public Class ParseTreePatternMatcher.CannotInvokeStartRule

extends RuntimeException
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

CannotInvokeStartRuleback to summary
public CannotInvokeStartRule(Throwable e)
org.antlr.v4.runtime.tree.pattern back to summary

public Class ParseTreePatternMatcher.StartRuleDoesNotConsumeFullPattern

extends RuntimeException
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

StartRuleDoesNotConsumeFullPatternback to summary
public StartRuleDoesNotConsumeFullPattern()