use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class TestCompositeGrammars method testCombinedImportsCombined.
@Test
public void testCombinedImportsCombined() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = // A, B, C token type order
"grammar S;\n" + "tokens { A, B, C }\n" + "x : 'x' INT {System.out.println(\"S.x\");} ;\n" + "INT : '0'..'9'+ ;\n" + "WS : (' '|'\\n') -> skip ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "s : x INT ;\n";
writeFile(tmpdir, "M.g4", master);
@SuppressWarnings("unused") Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
String found = execParser("M.g4", master, "MParser", "MLexer", "s", "x 34 9", debug);
assertEquals("S.x\n", found);
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class TestCompositeGrammars method test3LevelImport.
// Make sure that M can import S that imports T.
@Test
public void test3LevelImport() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar T;\n" + "a : T ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "T.g4", slave);
String slave2 = "parser grammar S;\n" + "import T;\n" + "a : S ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave2);
String master = "grammar M;\n" + "import S;\n" + "a : M ;\n";
writeFile(tmpdir, "M.g4", master);
Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
// S and T aren't imported; overridden
String expectedTokenIDToTypeMap = "{EOF=-1, M=1}";
String expectedStringLiteralToTypeMap = "{}";
String expectedTypeToTokenList = "[M]";
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("M.g4", master, "MParser", null);
// should be ok
boolean expecting = true;
assertEquals(expecting, ok);
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class TestCompositeGrammars method testErrorInImportedGetsRightFilename.
@Test
public void testErrorInImportedGetsRightFilename() throws Exception {
String slave = "parser grammar S;\n" + "a : 'a' | c;\n";
mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
String master = "grammar M;\n" + "import S;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = antlr("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.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class TestCompositeGrammars method testRulesVisibleThroughMultilevelImport.
@Test
public void testRulesVisibleThroughMultilevelImport() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar T;\n" + "x : T ;\n";
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";
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.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseJavaTest method testActions.
public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
int lp = templates.indexOf('(');
String name = templates.substring(0, lp);
STGroup group = new STGroupString(templates);
ST st = group.getInstanceOf(name);
st.add(actionName, action);
String grammar = st.render();
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammar, equeue);
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
AnalysisPipeline anal = new AnalysisPipeline(g);
anal.process();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser(false);
String output = outputFileST.render();
// System.out.println(output);
String b = "#" + actionName + "#";
int start = output.indexOf(b);
String e = "#end-" + actionName + "#";
int end = output.indexOf(e);
String snippet = output.substring(start + b.length(), end);
assertEquals(expected, snippet);
}
if (equeue.size() > 0) {
// System.err.println(equeue.toString());
}
}
Aggregations