use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testBigTreeOfImports.
@Test
public void testBigTreeOfImports() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar T;\n" + "tokens{T}\n" + "x : T ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "T.g4", slave);
slave = "parser grammar S;\n" + "import T;\n" + "tokens{S}\n" + "y : S ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
slave = "parser grammar C;\n" + "tokens{C}\n" + "i : C ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "C.g4", slave);
slave = "parser grammar B;\n" + "tokens{B}\n" + "j : B ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "B.g4", slave);
slave = "parser grammar A;\n" + "import B,C;\n" + "tokens{A}\n" + "k : A ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "A.g4", slave);
String master = "grammar M;\n" + "import S,A;\n" + "tokens{M}\n" + "a : M ;\n";
writeFile(tmpdir, "M.g4", master);
Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
assertEquals("[]", equeue.errors.toString());
assertEquals("[]", equeue.warnings.toString());
String expectedTokenIDToTypeMap = "{EOF=-1, M=1, S=2, T=3, A=4, B=5, C=6}";
String expectedStringLiteralToTypeMap = "{}";
String expectedTypeToTokenList = "[M, S, T, A, B, C]";
assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
boolean ok = rawGenerateAndBuildRecognizer("M.g4", master, "MParser", 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 testHeadersPropogatedCorrectlyToImportedGrammars.
@Test
public void testHeadersPropogatedCorrectlyToImportedGrammars() throws Exception {
String slave = "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "@header{package mypackage;}\n" + "s : a ;\n" + // defines B from inherited token space
"B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", master, false);
// should be ok
int expecting = 0;
assertEquals(expecting, equeue.errors.size());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testImportedTokenVocabIgnoredWithWarning.
@Test
public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar S;\n" + "options {tokenVocab=whatever;}\n" + "tokens { A }\n" + "x : A {System.out.println(\"S.x\");} ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(tmpdir, "M.g4", master);
Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
Object expectedArg = "S";
ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE;
GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g.fileName, null, expectedArg);
checkGrammarSemanticsWarning(equeue, expectedMessage);
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
assertEquals("unexpected warnings: " + equeue, 1, equeue.warnings.size());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testRulesVisibleThroughMultilevelImport.
@Test
public void testRulesVisibleThroughMultilevelImport() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar T;\n" + "x : T ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "T.g4", slave);
String slave2 = // A, B, C token type order
"parser grammar S;\n" + "import T;\n" + "a : S ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave2);
String master = "grammar M;\n" + "import S;\n" + // x MUST BE VISIBLE TO M
"a : M x ;\n";
writeFile(tmpdir, "M.g4", master);
Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
String expectedTokenIDToTypeMap = "{EOF=-1, M=1, T=2}";
String expectedStringLiteralToTypeMap = "{}";
String expectedTypeToTokenList = "[M, T]";
assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestCompositeGrammars method testTokensFileInOutputDirAndImportFileInSubdir.
@Test
public void testTokensFileInOutputDirAndImportFileInSubdir() 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 parser = "parser grammar MParser;\n" + "import S;\n" + "options {tokenVocab=MLexer;}\n" + "s : a ;\n";
writeFile(tmpdir, "MParser.g4", parser);
String lexer = "lexer grammar MLexer;\n" + // defines B from inherited token space
"B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(tmpdir, "MLexer.g4", lexer);
String outdir = tmpdir + "/out";
BaseRuntimeTest.mkdir(outdir);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MLexer.g4", false, "-o", outdir);
assertEquals(0, equeue.size());
equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MParser.g4", false, "-o", outdir, "-lib", subdir);
assertEquals(0, equeue.size());
}
Aggregations