Search in sources :

Example 1 with CompilationErrorException

use of org.snt.inmemantlr.exceptions.CompilationErrorException in project inmemantlr by julianthome.

the class StringCompiler method compile.

/**
 * do the compilation for the antlr artifacts
 * @param units string code generation pipeline
 * @param oprov compiler option provider
 * @throws CompilationErrorException if the compilation was not successful
 */
public void compile(Set<CunitProvider> units, CompilerOptionsProvider oprov) throws CompilationErrorException {
    JavaCompiler javac = new EclipseCompiler();
    StandardJavaFileManager sjfm = javac.getStandardFileManager(null, null, null);
    SpecialJavaFileManager fileManager = new SpecialJavaFileManager(sjfm, cl);
    List<MemorySource> cunit = new ArrayList<>();
    Set<MemorySource> mset = new HashSet<>();
    for (CunitProvider sc : units) {
        cunit.addAll(sc.getItems());
        for (MemorySource ms : sc.getItems()) {
            LOGGER.debug(ms.toString());
        }
    }
    mset.addAll(cunit);
    DiagnosticListener<? super JavaFileObject> dlistener = null;
    Iterable<String> classes = null;
    Writer out = new StringWriter();
    List<String> optionList = new ArrayList<>();
    optionList.addAll(oprov.getOptions());
    JavaCompiler.CompilationTask compile = javac.getTask(out, fileManager, dlistener, optionList, classes, cunit);
    boolean ret = compile.call();
    if (!ret) {
        throw new CompilationErrorException(out.toString());
    }
    // the corresponding byte code
    for (MemorySource ms : mset) {
        Set<MemoryByteCode> mb = fileManager.getByteCodeFromClass(ms.getClassName());
        if (mb.size() == 0)
            throw new IllegalArgumentException("MemoryByteCode must not be empty");
        // book keeping of source-bytecode tuples
        mt.addMemoryTuple(ms, mb);
    }
}
Also used : CompilationErrorException(org.snt.inmemantlr.exceptions.CompilationErrorException) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) StringWriter(java.io.StringWriter) MemoryByteCode(org.snt.inmemantlr.memobjects.MemoryByteCode) MemorySource(org.snt.inmemantlr.memobjects.MemorySource) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 EclipseCompiler (org.eclipse.jdt.internal.compiler.tool.EclipseCompiler)1 CompilationErrorException (org.snt.inmemantlr.exceptions.CompilationErrorException)1 MemoryByteCode (org.snt.inmemantlr.memobjects.MemoryByteCode)1 MemorySource (org.snt.inmemantlr.memobjects.MemorySource)1