Search in sources :

Example 1 with PushStream

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

the class RepeatPlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
    return new DefaultPluginInvoke() {

        private String listVariable = compilerContext.generateVariable("collectionVar");

        private String collectionSizeVar = compilerContext.generateVariable("size");

        @Override
        public void beforeElement(PushStream stream, String tagName) {
            stream.write(new VariableBinding.Start(listVariable, expression.getRoot()));
            stream.write(new VariableBinding.Start(collectionSizeVar, new UnaryOperation(UnaryOperator.LENGTH, new Identifier(listVariable))));
            stream.write(new Conditional.Start(collectionSizeVar, true));
            String itemVariable = decodeItemVariable();
            String loopStatusVar = Syntax.itemLoopStatusVariable(itemVariable);
            String indexVariable = compilerContext.generateVariable("index");
            stream.write(new Loop.Start(listVariable, itemVariable, indexVariable));
            stream.write(new VariableBinding.Start(loopStatusVar, buildStatusObj(indexVariable, collectionSizeVar)));
        }

        @Override
        public void afterTagClose(PushStream stream, boolean isSelfClosing) {
            stream.write(NEW_LINE);
        }

        @Override
        public void afterElement(PushStream stream) {
            stream.write(VariableBinding.END);
            stream.write(Loop.END);
            stream.write(Conditional.END);
            stream.write(VariableBinding.END);
            stream.write(VariableBinding.END);
        }

        private String decodeItemVariable() {
            String[] args = callInfo.getArguments();
            if (args.length > 0) {
                return args[0];
            }
            return Syntax.DEFAULT_LIST_ITEM_VAR_NAME;
        }

        private MapLiteral buildStatusObj(String indexVar, String sizeVar) {
            HashMap<String, ExpressionNode> obj = new HashMap<>();
            Identifier indexId = new Identifier(indexVar);
            BinaryOperation firstExpr = new BinaryOperation(BinaryOperator.EQ, indexId, NumericConstant.ZERO);
            BinaryOperation lastExpr = new BinaryOperation(BinaryOperator.EQ, indexId, new BinaryOperation(BinaryOperator.SUB, new Identifier(sizeVar), NumericConstant.ONE));
            obj.put(INDEX, indexId);
            obj.put(COUNT, new BinaryOperation(BinaryOperator.ADD, indexId, NumericConstant.ONE));
            obj.put(FIRST, firstExpr);
            obj.put(MIDDLE, new UnaryOperation(UnaryOperator.NOT, new BinaryOperation(BinaryOperator.OR, firstExpr, lastExpr)));
            obj.put(LAST, lastExpr);
            obj.put(ODD, parityCheck(indexId, NumericConstant.ZERO));
            obj.put(EVEN, parityCheck(indexId, NumericConstant.ONE));
            return new MapLiteral(obj);
        }

        private ExpressionNode parityCheck(ExpressionNode numericExpression, NumericConstant expected) {
            return new BinaryOperation(BinaryOperator.EQ, new BinaryOperation(BinaryOperator.REM, numericExpression, NumericConstant.TWO), expected);
        }
    };
}
Also used : Loop(org.apache.sling.scripting.sightly.compiler.commands.Loop) UnaryOperation(org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation) HashMap(java.util.HashMap) BinaryOperation(org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation) Conditional(org.apache.sling.scripting.sightly.compiler.commands.Conditional) MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) NumericConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 2 with PushStream

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

the class UsePlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
    return new DefaultPluginInvoke() {

        @Override
        public void beforeElement(PushStream stream, String tagName) {
            String variableName = decodeVariableName();
            stream.write(new VariableBinding.Global(variableName, new RuntimeCall(RuntimeFunction.USE, expression.getRoot(), new MapLiteral(expression.getOptions()))));
        }

        private String decodeVariableName() {
            String[] arguments = callInfo.getArguments();
            if (arguments.length > 0) {
                return arguments[0];
            }
            return DEFAULT_VARIABLE_NAME;
        }
    };
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 3 with PushStream

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

the class ResourcePlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
    return new DefaultPluginInvoke() {

        private Map<String, ExpressionNode> expressionOptions = new HashMap<>(expression.getOptions());

        @Override
        public void beforeChildren(PushStream stream) {
            String resourceVar = compilerContext.generateVariable("resourceContent");
            stream.write(new VariableBinding.Start(resourceVar, new RuntimeCall(RuntimeFunction.RESOURCE, expression.getRoot(), new MapLiteral(expressionOptions))));
            stream.write(new OutputVariable(resourceVar));
            stream.write(VariableBinding.END);
            Patterns.beginStreamIgnore(stream);
        }

        @Override
        public void afterChildren(PushStream stream) {
            Patterns.endStreamIgnore(stream);
        }
    };
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) Map(java.util.Map) HashMap(java.util.HashMap) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 4 with PushStream

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

the class TextPlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, PluginCallInfo callInfo, final CompilerContext compilerContext) {
    return new DefaultPluginInvoke() {

        @Override
        public void beforeChildren(PushStream stream) {
            String variable = compilerContext.generateVariable("textContent");
            stream.write(new VariableBinding.Start(variable, compilerContext.adjustToContext(expression, MarkupContext.TEXT, ExpressionContext.TEXT).getRoot()));
            stream.write(new OutputVariable(variable));
            stream.write(VariableBinding.END);
            Patterns.beginStreamIgnore(stream);
        }

        @Override
        public void afterChildren(PushStream stream) {
            Patterns.endStreamIgnore(stream);
        }
    };
}
Also used : PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 5 with PushStream

use of org.apache.sling.scripting.sightly.impl.compiler.PushStream 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

PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)10 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)8 MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)6 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)4 HashMap (java.util.HashMap)3 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)3 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)3 Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)3 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)3 Loop (org.apache.sling.scripting.sightly.compiler.commands.Loop)2 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)2 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)2 UnaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation)2 IOException (java.io.IOException)1 Map (java.util.Map)1 SightlyCompilerException (org.apache.sling.scripting.sightly.compiler.SightlyCompilerException)1 CommandStream (org.apache.sling.scripting.sightly.compiler.commands.CommandStream)1 OutText (org.apache.sling.scripting.sightly.compiler.commands.OutText)1 Procedure (org.apache.sling.scripting.sightly.compiler.commands.Procedure)1 CompilationResultImpl (org.apache.sling.scripting.sightly.impl.compiler.CompilationResultImpl)1