use of org.antlr.v4.tool.Grammar 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.tool.Grammar in project antlr4 by antlr.
the class TestParseTreeMatcher method testIDNodeWithLabelMatches.
@Test
public void testIDNodeWithLabelMatches() throws Exception {
String grammar = "grammar X8;\n" + "s : ID ';' ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
String input = "x ;";
String pattern = "<id:ID>;";
ParseTreeMatch m = checkPatternMatch(grammar, "s", input, pattern, "X8");
assertEquals("{ID=[x], id=[x]}", m.getLabels().toString());
assertNotNull(m.get("id"));
assertNotNull(m.get("ID"));
assertEquals("x", m.get("id").getText());
assertEquals("x", m.get("ID").getText());
assertEquals("[x]", m.getAll("id").toString());
assertEquals("[x]", m.getAll("ID").toString());
assertNull(m.get("undefined"));
assertEquals("[]", m.getAll("undefined").toString());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestParseTreeMatcher method testLabelGetsLastIDNode.
@Test
public void testLabelGetsLastIDNode() throws Exception {
String grammar = "grammar X9;\n" + "s : ID ID ';' ;\n" + "ID : [a-z]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
String input = "x y;";
String pattern = "<id:ID> <id:ID>;";
ParseTreeMatch m = checkPatternMatch(grammar, "s", input, pattern, "X9");
assertEquals("{ID=[x, y], id=[x, y]}", m.getLabels().toString());
assertNotNull(m.get("id"));
assertNotNull(m.get("ID"));
assertEquals("y", m.get("id").getText());
assertEquals("y", m.get("ID").getText());
assertEquals("[x, y]", m.getAll("id").toString());
assertEquals("[x, y]", m.getAll("ID").toString());
assertNull(m.get("undefined"));
assertEquals("[]", m.getAll("undefined").toString());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestParseTreeMatcher method checkPatternMatch.
public ParseTreeMatch checkPatternMatch(String grammar, String startRule, String input, String pattern, String grammarName, boolean invertMatch) throws Exception {
String grammarFileName = grammarName + ".g4";
String parserName = grammarName + "Parser";
String lexerName = grammarName + "Lexer";
boolean ok = rawGenerateAndBuildRecognizer(grammarFileName, grammar, parserName, lexerName, false);
assertTrue(ok);
ParseTree result = execParser(startRule, input, parserName, lexerName);
ParseTreePattern p = getPattern(grammarName, pattern, startRule);
ParseTreeMatch match = p.match(result);
boolean matched = match.succeeded();
if (invertMatch)
assertFalse(matched);
else
assertTrue(matched);
return match;
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testImportFileLocationInSubdir.
@Test
public void testImportFileLocationInSubdir() throws Exception {
String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
BaseRuntimeTest.mkdir(tmpdir);
String subdir = tmpdir + "/sub";
BaseRuntimeTest.mkdir(subdir);
writeFile(subdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + // defines B from inherited token space
"B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", subdir);
assertEquals(0, equeue.size());
}
Aggregations