use of org.lflang.generator.LFGeneratorContext in project lingua-franca by lf-lang.
the class Main method runGenerator.
/**
* Load the resource, validate it, and, invoke the code generator.
*/
private void runGenerator(List<Path> files, Injector injector) {
Properties properties = this.getProps(cmd);
String pathOption = CLIOption.OUTPUT_PATH.option.getOpt();
Path root = null;
if (cmd.hasOption(pathOption)) {
root = Paths.get(cmd.getOptionValue(pathOption)).normalize();
if (!Files.exists(root)) {
// FIXME: Create it instead?
reporter.printFatalErrorAndExit("Output location '" + root + "' does not exist.");
}
if (!Files.isDirectory(root)) {
reporter.printFatalErrorAndExit("Output location '" + root + "' is not a directory.");
}
}
for (Path path : files) {
if (!Files.exists(path)) {
reporter.printFatalErrorAndExit(path + ": No such file or directory");
}
}
for (Path path : files) {
path = path.toAbsolutePath();
Path pkgRoot = FileConfig.findPackageRoot(path, reporter::printWarning);
String resolved;
if (root != null) {
resolved = root.resolve("src-gen").toString();
} else {
resolved = pkgRoot.resolve("src-gen").toString();
}
this.fileAccess.setOutputPath(resolved);
final Resource resource = getValidatedResource(path);
exitIfCollectedErrors();
LFGeneratorContext context = new MainContext(LFGeneratorContext.Mode.STANDALONE, CancelIndicator.NullImpl, (m, p) -> {
}, properties, false, fileConfig -> injector.getInstance(ErrorReporter.class));
this.generator.generate(resource, this.fileAccess, context);
exitIfCollectedErrors();
// print all other issues (not errors)
issueCollector.getAllIssues().forEach(reporter::printIssue);
System.out.println("Code generation finished.");
}
}
use of org.lflang.generator.LFGeneratorContext in project lingua-franca by lf-lang.
the class TestBase method configure.
/**
* Configure a test by applying the given configurator and return a
* generator context. Also, if the given level is less than
* `TestLevel.BUILD`, add a `no-compile` flag to the generator context. If
* the configurator was not applied successfully, throw an AssertionError.
*
* @param test the test to configure.
* @param configurator The configurator to apply to the test.
* @param level The level of testing in which the generator context will be
* used.
* @return a generator context with a fresh resource, unaffected by any AST
* transformation that may have occured in other tests.
* @throws IOException if there is any file access problem
*/
private LFGeneratorContext configure(LFTest test, Configurator configurator, TestLevel level) throws IOException {
var context = new MainContext(LFGeneratorContext.Mode.STANDALONE, CancelIndicator.NullImpl, (m, p) -> {
}, new Properties(), true, fileConfig -> new DefaultErrorReporter());
var r = resourceSetProvider.get().getResource(URI.createFileURI(test.srcFile.toFile().getAbsolutePath()), true);
if (r.getErrors().size() > 0) {
test.result = Result.PARSE_FAIL;
throw new AssertionError("Test did not parse correctly.");
}
fileAccess.setOutputPath(FileConfig.findPackageRoot(test.srcFile, s -> {
}).resolve(FileConfig.DEFAULT_SRC_GEN_DIR).toString());
test.context = context;
test.fileConfig = new FileConfig(r, FileConfig.getSrcGenRoot(fileAccess), context.useHierarchicalBin());
// Set the no-compile flag the test is not supposed to reach the build stage.
if (level.compareTo(TestLevel.BUILD) < 0) {
context.getArgs().setProperty("no-compile", "");
}
addExtraLfcArgs(context.getArgs());
// Update the test by applying the configuration. E.g., to carry out an AST transformation.
if (configurator != null && !configurator.configure(test)) {
test.result = Result.CONFIG_FAIL;
throw new AssertionError("Test configuration unsuccessful.");
}
return context;
}
Aggregations