use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseNodeTest method checkGrammarSemanticsError.
protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception {
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;
}
}
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 antlr.
the class BaseNodeTest 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 = antlrOnString(getTmpDir(), "JavaScript", grammarFileName, grammarStr, defaultListener, extraOptions);
if (!equeue.errors.isEmpty()) {
return false;
}
List<String> files = new ArrayList<String>();
if (lexerName != null) {
files.add(lexerName + ".js");
}
if (parserName != null) {
files.add(parserName + ".js");
Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
if (!optionsSet.contains("-no-listener")) {
files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.')) + "Listener.js");
}
if (optionsSet.contains("-visitor")) {
files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.')) + "Visitor.js");
}
}
// allIsWell: no compile
return true;
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseSwiftTest method rawGenerateRecognizer.
/**
* Return true if all is well
*/
private boolean rawGenerateRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
ErrorQueue equeue = antlrOnString(getTmpDir(), "Swift", grammarFileName, grammarStr, defaultListener, extraOptions);
if (!equeue.errors.isEmpty()) {
return false;
}
List<String> files = new ArrayList<String>();
if (lexerName != null) {
files.add(lexerName + ".swift");
files.add(lexerName + "ATN.swift");
}
if (parserName != null) {
files.add(parserName + ".swift");
files.add(parserName + "ATN.swift");
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.swift");
files.add(grammarName + "BaseListener.swift");
}
if (optionsSet.contains("-visitor")) {
files.add(grammarName + "Visitor.swift");
files.add(grammarName + "BaseVisitor.swift");
}
}
addSourceFiles(files.toArray(new String[files.size()]));
return true;
}
use of org.antlr.v4.test.runtime.ErrorQueue 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.ErrorQueue 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