use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseCSharpTest method rawGenerateRecognizer.
/** Return true if all is well */
protected boolean rawGenerateRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
ErrorQueue equeue = antlrOnString(getTmpDir(), "CSharp", grammarFileName, grammarStr, defaultListener, extraOptions);
if (!equeue.errors.isEmpty()) {
return false;
}
List<String> files = new ArrayList<String>();
if (lexerName != null) {
files.add(lexerName + ".cs");
}
if (parserName != null) {
files.add(parserName + ".cs");
Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
String grammarName = grammarFileName.substring(0, grammarFileName.lastIndexOf('.'));
if (!optionsSet.contains("-no-listener")) {
files.add(grammarName + "Listener.cs");
files.add(grammarName + "BaseListener.cs");
}
if (optionsSet.contains("-visitor")) {
files.add(grammarName + "Visitor.cs");
files.add(grammarName + "BaseVisitor.cs");
}
}
addSourceFiles(files.toArray(new String[files.size()]));
return true;
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testDelegatesSeeSameTokenType.
@Test
public void testDelegatesSeeSameTokenType() throws Exception {
String slaveS = "parser grammar S;\n" + "tokens { A, B, C }\n" + "x : A ;\n";
String slaveT = "parser grammar T;\n" + "tokens { C, B, A } // reverse order\n" + "y : A ;\n";
BaseRuntimeTest.mkdir(tmpdir);
writeFile(tmpdir, "S.g4", slaveS);
writeFile(tmpdir, "T.g4", slaveT);
String master = "// The lexer will create rules to match letters a, b, c.\n" + "// The associated token types A, B, C must have the same value\n" + "// and all import'd parsers. Since ANTLR regenerates all imports\n" + "// for use with the delegator M, it can generate the same token type\n" + "// mapping in each parser:\n" + "// public static final int C=6;\n" + "// public static final int EOF=-1;\n" + "// public static final int B=5;\n" + "// public static final int WS=7;\n" + "// public static final int A=4;\n" + "grammar M;\n" + "import S,T;\n" + "s : x y ; // matches AA, which should be 'aa'\n" + "B : 'b' ; // another order: B, A, C\n" + "A : 'a' ;\n" + "C : 'c' ;\n" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
String expectedTokenIDToTypeMap = "{EOF=-1, B=1, A=2, C=3, WS=4}";
String expectedStringLiteralToTypeMap = "{'a'=2, 'b'=1, 'c'=3}";
String expectedTypeToTokenList = "[B, A, C, WS]";
assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
assertEquals(expectedStringLiteralToTypeMap, sort(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 TestCompositeGrammars method testSyntaxErrorsInImportsNotThrownOut.
@Test
public void testSyntaxErrorsInImportsNotThrownOut() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar S;\n" + "options {toke\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);
assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testImportSelfLoop.
// Test for https://github.com/antlr/antlr4/issues/1317
@Test
public void testImportSelfLoop() throws Exception {
BaseRuntimeTest.mkdir(tmpdir);
String master = "grammar M;\n" + "import M;\n" + "s : 'a' ;\n";
writeFile(tmpdir, "M.g4", master);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir);
assertEquals(0, equeue.size());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseBrowserTest 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();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
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