Search in sources :

Example 1 with InternalBackendException

use of org.abs_models.backend.common.InternalBackendException in project abstools by abstools.

the class JavaBackend method compile.

private int compile() throws Exception {
    final Model model = parse(arguments.files);
    if (model.hasParserErrors() || model.hasErrors() || model.hasTypeErrors()) {
        printErrorMessage();
        return 1;
    }
    if (!arguments.destDir.mkdirs() && !arguments.destDir.isDirectory()) {
        throw new IOException("Could not create directory " + arguments.destDir.toString());
    }
    if (!arguments.destDir.canWrite()) {
        throw new InternalBackendException("Destination directory " + arguments.destDir.getAbsolutePath() + " cannot be written to!");
    }
    compile(model, arguments.destDir);
    return 0;
}
Also used : InternalBackendException(org.abs_models.backend.common.InternalBackendException) Model(org.abs_models.frontend.ast.Model) IOException(java.io.IOException)

Example 2 with InternalBackendException

use of org.abs_models.backend.common.InternalBackendException in project abstools by abstools.

the class ABSTest method assertParseFilesOk.

protected Model assertParseFilesOk(Set<String> fileNames, Config... config) throws IOException, InternalBackendException, DeltaModellingException, WrongProgramArgumentException {
    Main main = new Main();
    java.util.List<File> files = fileNames.stream().map(f -> new File(resolveFileName(f))).collect(Collectors.toList());
    Model m = main.parse(files);
    return assertParseModelOk(m, config);
}
Also used : FilenameFilter(java.io.FilenameFilter) Arrays(java.util.Arrays) SemanticCondition(org.abs_models.frontend.analyser.SemanticCondition) Set(java.util.Set) IOException(java.io.IOException) EXPECT_TYPE_ERROR(org.abs_models.ABSTest.Config.EXPECT_TYPE_ERROR) Collectors(java.util.stream.Collectors) EXPECT_PARSE_ERROR(org.abs_models.ABSTest.Config.EXPECT_PARSE_ERROR) WITHOUT_DESUGARING_AFTER_TYPECHECK(org.abs_models.ABSTest.Config.WITHOUT_DESUGARING_AFTER_TYPECHECK) File(java.io.File) LocationTypeExtension(org.abs_models.frontend.typechecker.locationtypes.LocationTypeExtension) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) TYPE_CHECK(org.abs_models.ABSTest.Config.TYPE_CHECK) Main(org.abs_models.frontend.parser.Main) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) StringReader(java.io.StringReader) Model(org.abs_models.frontend.ast.Model) WITH_LOC_INF(org.abs_models.ABSTest.Config.WITH_LOC_INF) Assert.fail(org.junit.Assert.fail) InternalBackendException(org.abs_models.backend.common.InternalBackendException) WITHOUT_MODULE_NAME(org.abs_models.ABSTest.Config.WITHOUT_MODULE_NAME) DeltaModellingException(org.abs_models.frontend.delta.DeltaModellingException) Model(org.abs_models.frontend.ast.Model) Main(org.abs_models.frontend.parser.Main) File(java.io.File)

Example 3 with InternalBackendException

use of org.abs_models.backend.common.InternalBackendException in project abstools by abstools.

the class Main method mainMethod.

public int mainMethod(Absc arguments) {
    int result = 0;
    boolean done = false;
    this.arguments = arguments;
    try {
        if (arguments.backend != null) {
            if (arguments.backend.maude) {
                result = Math.max(result, MaudeCompiler.doMain(arguments));
                done = true;
            }
            if (arguments.backend.java) {
                result = Math.max(result, JavaBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.erlang) {
                result = Math.max(result, ErlangBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.prolog) {
                result = Math.max(result, PrologBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.coreabs) {
                result = Math.max(result, CoreAbsBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.json) {
                result = Math.max(result, Tester.doMain(arguments));
                done = true;
            }
            if (arguments.backend.prettyprint) {
                result = Math.max(result, PrettyPrinterBackEnd.doMain(arguments));
                done = true;
            }
            if (arguments.backend.outline) {
                result = Math.max(result, OutlinePrinterBackEnd.doMain(arguments));
                done = true;
            }
            if (arguments.backend.dumpProducts) {
                Model m = parse(arguments.files);
                if (m.hasParserErrors()) {
                    // parse should have already printed errors
                    result = Math.max(result, 1);
                } else {
                    Iterator<ProductDecl> pi = m.getProductDecls().iterator();
                    while (pi.hasNext()) {
                        System.out.print(pi.next().getName());
                        if (pi.hasNext())
                            System.out.print(' ');
                    }
                }
                done = true;
            }
        }
        if (!done) {
            // no backend selected, just do type-checking
            Model m = parse(arguments.files);
            if (m.hasParserErrors() || m.hasErrors() || m.hasTypeErrors()) {
                printErrorMessage();
                result = 1;
            }
        }
    } catch (InternalBackendException e) {
        // don't print stack trace here
        printError(e.getMessage());
        result = 1;
    } catch (Exception e) {
        if (e.getMessage() == null) {
            e.printStackTrace();
        }
        assert e.getMessage() != null : e.toString();
        printError(e.getMessage());
        result = 1;
    }
    return result;
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) InternalBackendException(org.abs_models.backend.common.InternalBackendException) Model(org.abs_models.frontend.ast.Model) InternalBackendException(org.abs_models.backend.common.InternalBackendException) FileNotFoundException(java.io.FileNotFoundException) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) IOException(java.io.IOException) DeltaModellingException(org.abs_models.frontend.delta.DeltaModellingException)

Example 4 with InternalBackendException

use of org.abs_models.backend.common.InternalBackendException in project abstools by abstools.

the class MaudeCompiler method compile.

/**
 * @throws Exception
 */
public int compile() throws Exception {
    if (arguments.verbose)
        System.out.println("Generating Maude code...");
    module = arguments.maude_timed ? SIMULATOR.EQ_TIMED : SIMULATOR.RL;
    final Model model = parse(arguments.files);
    // Maude has build-in AwaitAsyncCall support
    model.doAACrewrite = false;
    if (model.hasParserErrors() || model.hasErrors() || model.hasTypeErrors()) {
        printErrorMessage();
        return 1;
    }
    PrintStream stream = System.out;
    if (arguments.outputfile != null) {
        stream = new PrintStream(arguments.outputfile);
    }
    // Are we running in the source directory?
    InputStream is = ClassLoader.getSystemResourceAsStream(RUNTIME_INTERPRETER_PATH);
    if (is == null) {
        // Are we running within absfrontend.jar?
        is = ClassLoader.getSystemResourceAsStream("maude/abs-interpreter.maude");
    }
    if (is == null) {
        throw new InternalBackendException("Could not locate abs-interpreter.maude");
    }
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    stream.println("*** Generated " + dateFormat.format(new Date()));
    ByteStreams.copy(is, stream);
    model.generateMaude(stream, module, arguments.maude_mainBlock, arguments.maude_clocklimit, arguments.maude_defaultResources);
    if (arguments.verbose && arguments.outputfile != null) {
        System.out.println("Finished.  Start `maude " + arguments.outputfile.toString() + "' to run the model.");
    }
    return 0;
}
Also used : PrintStream(java.io.PrintStream) InputStream(java.io.InputStream) InternalBackendException(org.abs_models.backend.common.InternalBackendException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Model(org.abs_models.frontend.ast.Model) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with InternalBackendException

use of org.abs_models.backend.common.InternalBackendException in project abstools by abstools.

the class ErlangBackend method compile.

public void compile(Model m, File destDir, EnumSet<CompileOptions> options) throws IOException, InterruptedException, InternalBackendException {
    int version = getErlangVersion();
    if (version < minErlangVersion) {
        String message = "ABS requires at least erlang version " + Integer.toString(minErlangVersion) + ", installed version is " + Integer.toString(version);
        throw new InternalBackendException(message);
    }
    String ebin_dir = "/absmodel/_build/default/lib/absmodel/ebin/";
    ErlApp erlApp = new ErlApp(destDir, arguments.http_index_file, arguments.http_static_dir);
    m.generateErlangCode(erlApp, options);
    erlApp.close();
    List<String> compile_command = new ArrayList<String>();
    // We used to call "rebar compile" here but calling erlc directly
    // removes 1.5s from the compile time
    compile_command.add("erlc");
    if (options.contains(CompileOptions.DEBUG)) {
        compile_command.add("+debug_info");
    }
    compile_command.add("-I");
    compile_command.add(destDir + "/absmodel/include");
    compile_command.add("-o");
    compile_command.add(destDir + ebin_dir);
    // Set<String> compiled_basenames =
    // Arrays.stream(new File(destDir, ebin_dir).listFiles())
    // .map((File f) -> FilenameUtils.removeExtension(f.getName()))
    // .collect(Collectors.toSet());
    Arrays.stream(new File(destDir, "absmodel/src/").listFiles(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            return name.endsWith(".erl");
        }
    })).forEach((File f) -> compile_command.add(f.toString()));
    if (options.contains(CompileOptions.VERBOSE)) {
        System.out.println("Compiling erlang files with command: " + String.join(" ", compile_command));
    }
    Process p = Runtime.getRuntime().exec(compile_command.toArray(new String[0]));
    if (options.contains(CompileOptions.VERBOSE))
        IOUtils.copy(p.getInputStream(), System.out);
    else
        IOUtils.copy(p.getInputStream(), new NullOutputStream());
    p.waitFor();
    if (p.exitValue() != 0) {
        String message = "Compilation of generated erlang code failed with exit value " + p.exitValue();
        if (!options.contains(CompileOptions.VERBOSE))
            message = message + "\n  (use -v for detailed compiler output)";
        throw new InternalBackendException(message);
    // TODO: consider removing the generated code here.  For now,
    // let's leave it in place for diagnosis.
    } else {
        if (options.contains(CompileOptions.VERBOSE)) {
            System.out.println();
            System.out.println("Finished.  \"gen/erl/run\" to start the model.");
            System.out.println("          (\"gen/erl/run --help\" for more options)");
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) InternalBackendException(org.abs_models.backend.common.InternalBackendException) ArrayList(java.util.ArrayList) File(java.io.File) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Aggregations

InternalBackendException (org.abs_models.backend.common.InternalBackendException)5 Model (org.abs_models.frontend.ast.Model)4 IOException (java.io.IOException)3 File (java.io.File)2 FilenameFilter (java.io.FilenameFilter)2 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)2 DeltaModellingException (org.abs_models.frontend.delta.DeltaModellingException)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 StringReader (java.io.StringReader)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 EXPECT_PARSE_ERROR (org.abs_models.ABSTest.Config.EXPECT_PARSE_ERROR)1 EXPECT_TYPE_ERROR (org.abs_models.ABSTest.Config.EXPECT_TYPE_ERROR)1