compile method
For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.
Implementation
ParseTreePattern compile(String pattern, int patternRuleIndex) {
final tokenList = tokenize(pattern);
final tokenSrc = ListTokenSource(tokenList);
final tokens = CommonTokenStream(tokenSrc);
final parserInterp = ParserInterpreter(
parser.grammarFileName,
parser.vocabulary,
parser.ruleNames,
parser.ATNWithBypassAlts,
tokens,
);
ParseTree tree;
try {
parserInterp.errorHandler = BailErrorStrategy();
tree = parserInterp.parse(patternRuleIndex);
// System.out.println("pattern tree = "+tree.toStringTree(parserInterp));
} on ParseCancellationException {
rethrow;
} on RecognitionException {
rethrow;
} catch (e) {
throw CannotInvokeStartRule(e.toString());
}
// Make sure tree pattern compilation checks for a complete parse
if (tokens.LA(1) != Token.EOF) {
throw StartRuleDoesNotConsumeFullPattern();
}
return ParseTreePattern(this, pattern, patternRuleIndex, tree);
}