use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testChunking.
@Test
public void testChunking() throws Exception {
ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
assertEquals("[ID, ' = ', expr, ' ;']", m.split("<ID> = <expr> ;").toString());
assertEquals("[' ', ID, ' = ', expr]", m.split(" <ID> = <expr>").toString());
assertEquals("[ID, ' = ', expr]", m.split("<ID> = <expr>").toString());
assertEquals("[expr]", m.split("<expr>").toString());
assertEquals("['<x> foo']", m.split("\\<x\\> foo").toString());
assertEquals("['foo <x> bar ', tag]", m.split("foo \\<x\\> bar <tag>").toString());
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testCompilingPattern.
@Test
public void testCompilingPattern() 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");
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 getPatternMatcher.
public ParseTreePatternMatcher getPatternMatcher(String grammarName) 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 new ParseTreePatternMatcher(lexer, parser);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testCompilingPatternConsumesAllTokens.
@Test
public void testCompilingPatternConsumesAllTokens() 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> = <expr> ; extra", m.getParser().getRuleIndex("s"));
} catch (ParseTreePatternMatcher.StartRuleDoesNotConsumeFullPattern e) {
failed = true;
}
assertTrue(failed);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testExtraClose.
@Test
public void testExtraClose() throws Exception {
ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
String result = null;
try {
m.split("<expr> >");
} catch (IllegalArgumentException iae) {
result = iae.getMessage();
}
String expected = "missing start tag in pattern: <expr> >";
assertEquals(expected, result);
}
Aggregations