use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testPatternMatchesStartRule2.
@Test
public void testPatternMatchesStartRule2() throws Exception {
String grammar = "grammar X2;\n" + "s : ID '=' expr ';' | 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> <ID> ;", m.getParser().getRuleIndex("s"));
} catch (NoViableAltException e) {
failed = true;
}
assertTrue(failed);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testTokenizingPattern.
@Test
public void testTokenizingPattern() throws Exception {
String grammar = "grammar X1;\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("X1.g4", grammar, "X1Parser", "X1Lexer", false);
assertTrue(ok);
ParseTreePatternMatcher m = getPatternMatcher("X1");
List<? extends Token> tokens = m.tokenize("<ID> = <expr> ;");
String results = tokens.toString();
String expected = "[ID:3, [@-1,1:1='=',<1>,1:1], expr:7, [@-1,1:1=';',<2>,1:1]]";
assertEquals(expected, results);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testInvertedTags.
@Test
public void testInvertedTags() throws Exception {
ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
String result = null;
try {
m.split(">expr<");
} catch (IllegalArgumentException iae) {
result = iae.getMessage();
}
String expected = "tag delimiters out of order in pattern: >expr<";
assertEquals(expected, result);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testDelimiters.
@Test
public void testDelimiters() throws Exception {
ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
m.setDelimiters("<<", ">>", "$");
String result = m.split("<<ID>> = <<expr>> ;$<< ick $>>").toString();
assertEquals("[ID, ' = ', expr, ' ;<< ick >>']", result);
}
use of org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher in project antlr4 by antlr.
the class TestParseTreeMatcher method testUnclosedTag.
@Test
public void testUnclosedTag() throws Exception {
ParseTreePatternMatcher m = new ParseTreePatternMatcher(null, null);
String result = null;
try {
m.split("<expr hi mom");
} catch (IllegalArgumentException iae) {
result = iae.getMessage();
}
String expected = "unterminated tag in pattern: <expr hi mom";
assertEquals(expected, result);
}
Aggregations