use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class BaseTest method checkGrammarSemanticsWarning.
protected void checkGrammarSemanticsWarning(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception {
ANTLRMessage foundMsg = null;
for (int i = 0; i < equeue.warnings.size(); i++) {
ANTLRMessage m = equeue.warnings.get(i);
if (m.getErrorType() == expectedMessage.getErrorType()) {
foundMsg = m;
}
}
assertNotNull("no error; " + expectedMessage.getErrorType() + " expected", foundMsg);
assertTrue("error is not a GrammarSemanticsMessage", foundMsg instanceof GrammarSemanticsMessage);
assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs()));
if (equeue.size() != 1) {
System.err.println(equeue);
}
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class BaseTest method checkError.
protected void checkError(ErrorQueue equeue, ANTLRMessage expectedMessage) throws Exception {
// System.out.println("errors="+equeue);
ANTLRMessage foundMsg = null;
for (int i = 0; i < equeue.errors.size(); i++) {
ANTLRMessage m = equeue.errors.get(i);
if (m.getErrorType() == expectedMessage.getErrorType()) {
foundMsg = m;
}
}
assertTrue("no error; " + expectedMessage.getErrorType() + " expected", !equeue.errors.isEmpty());
assertTrue("too many errors; " + equeue.errors, equeue.errors.size() <= 1);
assertNotNull("couldn't find expected error: " + expectedMessage.getErrorType(), foundMsg);
/*
assertTrue("error is not a GrammarSemanticsMessage",
foundMsg instanceof GrammarSemanticsMessage);
*/
assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class BaseTest 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());
}
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
the class TestCodeGeneration method getEvalInfoForString.
public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammarString);
List<String> evals = new ArrayList<String>();
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();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
// STViz viz = outputFileST.inspect();
// try {
// viz.waitForClose();
// }
// catch (Exception e) {
// e.printStackTrace();
// }
boolean debug = false;
DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
InstanceScope scope = new InstanceScope(null, outputFileST);
StringWriter sw = new StringWriter();
AutoIndentWriter out = new AutoIndentWriter(sw);
interp.exec(out, scope);
for (String e : interp.evals) {
if (e.contains(pattern)) {
evals.add(e);
}
}
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
return evals;
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by tunnelvisionlabs.
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";
mkdir(tmpdir);
writeFile(tmpdir, "T.g4", slave);
slave = "parser grammar S;\n" + "import T;\n" + "tokens{S}\n" + "y : S ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slave);
slave = "parser grammar C;\n" + "tokens{C}\n" + "i : C ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "C.g4", slave);
slave = "parser grammar B;\n" + "tokens{B}\n" + "j : B ;\n";
mkdir(tmpdir);
writeFile(tmpdir, "B.g4", slave);
slave = "parser grammar A;\n" + "import B,C;\n" + "tokens{A}\n" + "k : A ;\n";
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);
}
Aggregations