use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testNestedComposite.
@Test
public void testNestedComposite() throws Exception {
// Wasn't compiling. http://www.antlr.org/jira/browse/ANTLR-438
ErrorQueue equeue = new ErrorQueue();
String gstr = "lexer grammar L;\n" + "T1: '1';\n" + "T2: '2';\n" + "T3: '3';\n" + "T4: '4';\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "L.g4", gstr);
gstr = "parser grammar G1;\n" + "s: a | b;\n" + "a: T1;\n" + "b: T2;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "G1.g4", gstr);
gstr = "parser grammar G2;\n" + "import G1;\n" + "a: T3;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "G2.g4", gstr);
String G3str = "grammar G3;\n" + "import G2;\n" + "b: T4;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "G3.g4", G3str);
Grammar g = new Grammar(tmpdir + "/G3.g4", G3str, equeue);
String expectedTokenIDToTypeMap = "{EOF=-1, T4=1, T3=2}";
String expectedStringLiteralToTypeMap = "{}";
String expectedTypeToTokenList = "[T4, T3]";
assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
boolean ok = rawGenerateAndBuildRecognizer("G3.g4", G3str, "G3Parser", null);
// should be ok
boolean expecting = true;
assertEquals(expecting, ok);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testErrorInImportedGetsRightFilename.
@Test
public void testErrorInImportedGetsRightFilename() throws Exception {
String slave = "parser grammar S;\n" + "a : 'a' | c;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir);
ANTLRMessage msg = equeue.errors.get(0);
assertEquals(ErrorType.UNDEFINED_RULE_REF, msg.getErrorType());
assertEquals("c", msg.getArgs()[0]);
assertEquals(2, msg.line);
assertEquals(10, msg.charPosition);
assertEquals("S.g4", new File(msg.fileName).getName());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testOutputDirShouldNotEffectImports.
@Test
public void testOutputDirShouldNotEffectImports() 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);
String outdir = tmpdir + "/out";
BaseRuntimeTest.mkdir(outdir);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir, "-lib", subdir);
assertEquals(0, equeue.size());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestGrammarParserInterpreter method testOneAlt.
@Test
public void testOneAlt() throws Exception {
LexerGrammar lg = new LexerGrammar(lexerText);
Grammar g = new Grammar("parser grammar T;\n" + "s : ID\n" + " ;\n", lg);
testInterp(lg, g, "s", "a", "(s:1 a)");
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestGrammarParserInterpreter method testAltsWithLabels.
@Test
public void testAltsWithLabels() throws Exception {
LexerGrammar lg = new LexerGrammar(lexerText);
Grammar g = new Grammar("parser grammar T;\n" + "s : ID # foo\n" + " | INT # bar\n" + " ;\n", lg);
// it won't show the labels here because my simple node text provider above just shows the alternative
testInterp(lg, g, "s", "a", "(s:1 a)");
testInterp(lg, g, "s", "3", "(s:2 3)");
}
Aggregations