use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseDartTest 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(getTempDirPath(), "Dart", grammarFileName, grammarStr, defaultListener, extraOptions);
if (!equeue.errors.isEmpty()) {
return false;
}
List<String> files = new ArrayList<String>();
if (lexerName != null) {
files.add(lexerName + ".dart");
}
if (parserName != null) {
files.add(parserName + ".dart");
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.dart");
files.add(grammarName + "BaseListener.dart");
}
if (optionsSet.contains("-visitor")) {
files.add(grammarName + "Visitor.dart");
files.add(grammarName + "BaseVisitor.dart");
}
}
String runtime = locateRuntime();
writeFile(getTempDirPath(), "pubspec.yaml", "name: \"test\"\n" + "dependencies:\n" + " antlr4:\n" + " path: " + runtime + "\n" + "environment:\n" + " sdk: \">=2.12.0 <3.0.0\"\n");
final File dartToolDir = new File(getTempDirPath(), ".dart_tool");
if (cacheDartPackages == null) {
try {
final Process process = Runtime.getRuntime().exec(new String[] { locateDart(), "pub", "get" }, null, getTempTestDir());
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
stderrVacuum.start();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
process.destroy();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}, 30_000);
process.waitFor();
timer.cancel();
stderrVacuum.join();
String stderrDuringPubGet = stderrVacuum.toString();
if (!stderrDuringPubGet.isEmpty()) {
System.out.println("Pub Get error: " + stderrVacuum.toString());
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;
}
cacheDartPackages = readFile(getTempDirPath(), ".packages");
cacheDartPackageConfig = readFile(dartToolDir.getAbsolutePath(), "package_config.json");
} else {
writeFile(getTempDirPath(), ".packages", cacheDartPackages);
// noinspection ResultOfMethodCallIgnored
dartToolDir.mkdir();
writeFile(dartToolDir.getAbsolutePath(), "package_config.json", cacheDartPackageConfig);
}
// allIsWell: no compile
return true;
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
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 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";
RuntimeTestUtils.mkdir(getTempDirPath());
String subdir = getTempDirPath() + "/sub";
RuntimeTestUtils.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(getTempDirPath(), "M.g4", master);
String outdir = getTempDirPath() + "/out";
RuntimeTestUtils.mkdir(outdir);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-o", outdir, "-lib", subdir);
assertEquals(0, equeue.size());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testImportedTokenVocabIgnoredWithWarning.
@Test
public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar S;\n" + "options {tokenVocab=whatever;}\n" + "tokens { A }\n" + "x : A {System.out.println(\"S.x\");} ;\n";
RuntimeTestUtils.mkdir(getTempDirPath());
writeFile(getTempDirPath(), "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(getTempDirPath(), "M.g4", master);
Grammar g = new Grammar(getTempDirPath() + "/M.g4", master, equeue);
Object expectedArg = "S";
ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE;
GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g.fileName, null, expectedArg);
checkGrammarSemanticsWarning(equeue, expectedMessage);
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
assertEquals("unexpected warnings: " + equeue, 1, equeue.warnings.size());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testHeadersPropogatedCorrectlyToImportedGrammars.
@Test
public void testHeadersPropogatedCorrectlyToImportedGrammars() throws Exception {
String slave = "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n";
RuntimeTestUtils.mkdir(getTempDirPath());
writeFile(getTempDirPath(), "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "@header{package mypackage;}\n" + "s : a ;\n" + // defines B from inherited token space
"B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", master, false);
// should be ok
int expecting = 0;
assertEquals(expecting, equeue.errors.size());
}
Aggregations