use of org.apache.sling.scripting.sightly.compiler.commands.CommandStream 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;
}
use of org.apache.sling.scripting.sightly.compiler.commands.CommandStream in project sling by apache.
the class DeadCodeRemoval method transformer.
// this could be merged with constant folding for better accuracy
public static StreamTransformer transformer() {
return new StreamTransformer() {
@Override
public CommandStream transform(CommandStream inStream) {
StatefulVisitor visitor = new StatefulVisitor();
DeadCodeRemoval dcr = new DeadCodeRemoval(visitor.getControl());
visitor.initializeWith(dcr);
Streams.connect(inStream, dcr.getOutputStream(), visitor);
return dcr.getOutputStream();
}
};
}
Aggregations