Search in sources :

Example 1 with MemorySource

use of org.snt.inmemantlr.memobjects.MemorySource in project inmemantlr by julianthome.

the class GenericParser method addUtilityJavaFile.

/**
 * add utility Java class on which the antlr grammar depends on
 *
 * @param f Java file
 * @throws FileNotFoundException Java file does not exist
 */
private void addUtilityJavaFile(File f) throws FileNotFoundException {
    if (!f.exists()) {
        throw new FileNotFoundException("File " + f.getName() + " does " + "not exist");
    }
    String name = FilenameUtils.removeExtension(f.getName());
    String content = FileUtils.loadFileContent(f);
    LOGGER.debug("add utility {}", name);
    fp.addFiles(new MemorySource(name, content));
}
Also used : MemorySource(org.snt.inmemantlr.memobjects.MemorySource)

Example 2 with MemorySource

use of org.snt.inmemantlr.memobjects.MemorySource in project inmemantlr by julianthome.

the class GenericParser method compile.

/**
 * compile generic parser
 *
 * @throws CompilationErrorException an error during the Java compilation
 * occurs
 * @throws RedundantCompilationException objects are already compiled
 */
public void compile() throws CompilationException {
    LOGGER.debug("compile");
    // the antlr objects are already compiled
    if (antrlObjectsAvailable())
        throw new RedundantCompilationException("Antlr objects are already " + "available");
    Set<StringCodeGenPipeline> pip = antlr.getPipelines();
    if (pip.isEmpty())
        throw new CompilationException("No string code pipeline available");
    for (StringCodeGenPipeline p : pip) {
        for (MemorySource ms : p.getItems()) {
            LOGGER.debug(ms.getName() + " " + ms.toString());
        }
    }
    // process all grammar objects
    Tuple<String, String> parserLexer = antlr.process();
    parserName = parserLexer.getFirst();
    lexerName = parserLexer.getSecond();
    if (lexerName.isEmpty())
        throw new IllegalArgumentException("lexerName must not be empty");
    if (parserName.isEmpty())
        throw new IllegalArgumentException("parserName must not be empty");
    Set<CunitProvider> cu = new LinkedHashSet<>();
    if (fp.hasItems())
        cu.add(fp);
    cu.addAll(antlr.getCompilationUnits());
    sc.compile(cu, oprov);
}
Also used : MemorySource(org.snt.inmemantlr.memobjects.MemorySource)

Example 3 with MemorySource

use of org.snt.inmemantlr.memobjects.MemorySource 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

MemorySource (org.snt.inmemantlr.memobjects.MemorySource)3 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