use of org.antlr.v4.runtime.tree.pattern.ParseTreePattern in project antlr4 by antlr.
the class TestParseTreeMatcher method testCompilingMultipleTokens.
@Test
public void testCompilingMultipleTokens() throws Exception {
String grammar = "grammar X2;\n" + "s : ID '=' ID ';' ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
boolean ok = rawGenerateAndBuildRecognizer("X2.g4", grammar, "X2Parser", "X2Lexer", false);
assertTrue(ok);
ParseTreePatternMatcher m = getPatternMatcher("X2");
ParseTreePattern t = m.compile("<ID> = <ID> ;", m.getParser().getRuleIndex("s"));
String results = t.getPatternTree().toStringTree(m.getParser());
String expected = "(s <ID> = <ID> ;)";
assertEquals(expected, results);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePattern in project antlr4 by antlr.
the class TestParseTreeMatcher method testHiddenTokensNotSeenByTreePatternParser.
@Test
public void testHiddenTokensNotSeenByTreePatternParser() throws Exception {
String grammar = "grammar X2;\n" + "s : ID '=' expr ';' ;\n" + "expr : ID | INT ;\n" + "ID : [a-z]+ ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\r\\n\\t]+ -> channel(HIDDEN) ;\n";
boolean ok = rawGenerateAndBuildRecognizer("X2.g4", grammar, "X2Parser", "X2Lexer", false);
assertTrue(ok);
ParseTreePatternMatcher m = getPatternMatcher("X2");
ParseTreePattern t = m.compile("<ID> = <expr> ;", m.getParser().getRuleIndex("s"));
String results = t.getPatternTree().toStringTree(m.getParser());
String expected = "(s <ID> = (expr <expr>) ;)";
assertEquals(expected, results);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePattern in project antlr4 by antlr.
the class TestParseTreeMatcher method getPattern.
public ParseTreePattern getPattern(String grammarName, String pattern, String ruleName) throws Exception {
Class<? extends Lexer> lexerClass = loadLexerClassFromTempDir(grammarName + "Lexer");
Constructor<? extends Lexer> ctor = lexerClass.getConstructor(CharStream.class);
Lexer lexer = ctor.newInstance((CharStream) null);
Class<? extends Parser> parserClass = loadParserClassFromTempDir(grammarName + "Parser");
Constructor<? extends Parser> pctor = parserClass.getConstructor(TokenStream.class);
Parser parser = pctor.newInstance(new CommonTokenStream(lexer));
return parser.compileParseTreePattern(pattern, parser.getRuleIndex(ruleName));
}
Aggregations