use of org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString 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.test.runtime.BaseRuntimeTest.antlrOnString in project antlr4 by antlr.
the class BaseJavaTest method rawGenerateAndBuildRecognizer.
/** Return true if all is well */
protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTmpDir(), "Java", grammarFileName, grammarStr, defaultListener, extraOptions);
if (!equeue.errors.isEmpty()) {
return false;
}
List<String> files = new ArrayList<String>();
if (lexerName != null) {
files.add(lexerName + ".java");
}
if (parserName != null) {
files.add(parserName + ".java");
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.java");
files.add(grammarName + "BaseListener.java");
}
if (optionsSet.contains("-visitor")) {
files.add(grammarName + "Visitor.java");
files.add(grammarName + "BaseVisitor.java");
}
}
boolean allIsWell = compile(files.toArray(new String[files.size()]));
return allIsWell;
}
use of org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString 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.BaseRuntimeTest.antlrOnString in project antlr4 by antlr.
the class BaseJavaToolTest method testErrors.
public void testErrors(String[] pairs, boolean printTree) {
for (int i = 0; i < pairs.length; i += 2) {
String grammarStr = pairs[i];
String expect = pairs[i + 1];
String[] lines = grammarStr.split("\n");
String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
// use default language target in case test overrides
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, null, fileName, grammarStr, false);
String actual = equeue.toString(true);
actual = actual.replace(tmpdir + File.separator, "");
// System.err.println(actual);
String msg = grammarStr;
msg = msg.replace("\n", "\\n");
msg = msg.replace("\r", "\\r");
msg = msg.replace("\t", "\\t");
assertEquals("error in: " + msg, expect, actual);
}
}
use of org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString in project antlr4 by antlr.
the class TestPerformance method testExponentialInclude.
@Test(timeout = 20000)
public void testExponentialInclude() {
String grammarFormat = "parser grammar Level_%d_%d;\n" + "\n" + "%s import Level_%d_1, Level_%d_2;\n" + "\n" + "rule_%d_%d : EOF;\n";
BaseRuntimeTest.mkdir(tmpdir);
long startTime = System.nanoTime();
int levels = 20;
for (int level = 0; level < levels; level++) {
String leafPrefix = level == levels - 1 ? "//" : "";
String grammar1 = String.format(grammarFormat, level, 1, leafPrefix, level + 1, level + 1, level, 1);
writeFile(tmpdir, "Level_" + level + "_1.g4", grammar1);
if (level > 0) {
String grammar2 = String.format(grammarFormat, level, 2, leafPrefix, level + 1, level + 1, level, 1);
writeFile(tmpdir, "Level_" + level + "_2.g4", grammar2);
}
}
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "Level_0_1.g4", false);
Assert.assertTrue(equeue.errors.isEmpty());
long endTime = System.nanoTime();
System.out.format("%s milliseconds.%n", (endTime - startTime) / 1000000.0);
}
Aggregations