use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher 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.ParseTreePatternMatcher 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.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testPatternMatchesStartRule.
@Test
public void testPatternMatchesStartRule() 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]+ -> skip ;\n";
boolean ok = rawGenerateAndBuildRecognizer("X2.g4", grammar, "X2Parser", "X2Lexer", false);
assertTrue(ok);
ParseTreePatternMatcher m = getPatternMatcher("X2");
boolean failed = false;
try {
m.compile("<ID> ;", m.getParser().getRuleIndex("s"));
} catch (InputMismatchException e) {
failed = true;
}
assertTrue(failed);
}
Aggregations