Search in sources :

Example 1 with CompilerMessageImpl

use of org.apache.sling.scripting.sightly.impl.compiler.CompilerMessageImpl in project sling by apache.

the class SightlyCompiler method compile.

/**
     * Compiles a {@link CompilationUnit}, passing the processed {@link CommandStream} to the provided {@link BackendCompiler}.
     *
     * @param compilationUnit a compilation unit
     * @param backendCompiler the backend compiler
     * @return the compilation result
     */
public CompilationResult compile(CompilationUnit compilationUnit, BackendCompiler backendCompiler) {
    String scriptName = compilationUnit.getScriptName();
    String scriptSource = null;
    PushStream stream = new PushStream();
    SanityChecker.attachChecker(stream);
    CommandStream optimizedStream = optimizer.transform(stream);
    CompilationResultImpl compilationResult = new CompilationResultImpl(optimizedStream);
    try {
        scriptSource = IOUtils.toString(compilationUnit.getScriptReader());
        //optimizedStream.addHandler(LoggingHandler.INSTANCE);
        if (backendCompiler != null) {
            backendCompiler.handle(optimizedStream);
        }
        frontend.compile(stream, scriptSource);
        for (PushStream.StreamMessage w : stream.getWarnings()) {
            ScriptError warning = getScriptError(scriptSource, w.getCode(), 1, 0, w.getMessage());
            compilationResult.getWarnings().add(new CompilerMessageImpl(scriptName, warning.errorMessage, warning.lineNumber, warning.column));
        }
    } catch (SightlyCompilerException e) {
        ScriptError scriptError = getScriptError(scriptSource, e.getOffendingInput(), e.getLine(), e.getColumn(), e.getMessage());
        compilationResult.getErrors().add(new CompilerMessageImpl(scriptName, scriptError.errorMessage, scriptError.lineNumber, scriptError.column));
    } catch (IOException e) {
        throw new SightlyCompilerException("Unable to read source code from CompilationUnit identifying script " + scriptName, e);
    }
    compilationResult.seal();
    return compilationResult;
}
Also used : CommandStream(org.apache.sling.scripting.sightly.compiler.commands.CommandStream) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) CompilationResultImpl(org.apache.sling.scripting.sightly.impl.compiler.CompilationResultImpl) IOException(java.io.IOException) CompilerMessageImpl(org.apache.sling.scripting.sightly.impl.compiler.CompilerMessageImpl)

Aggregations

IOException (java.io.IOException)1 CommandStream (org.apache.sling.scripting.sightly.compiler.commands.CommandStream)1 CompilationResultImpl (org.apache.sling.scripting.sightly.impl.compiler.CompilationResultImpl)1 CompilerMessageImpl (org.apache.sling.scripting.sightly.impl.compiler.CompilerMessageImpl)1 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)1