Search in sources :

Example 1 with Identifier

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier 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 Identifier

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier in project sling by apache.

the class GenHelper method listCoercion.

public static void listCoercion(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
    ExpressionNode node = typedNode.getNode();
    if (node instanceof Identifier) {
        //using list coercion caching optimization
        VariableDescriptor descriptor = visitor.getAnalyzer().descriptor(((Identifier) node).getName());
        String listCoercionVar = descriptor.requireListCoercion();
        source.startExpression().append(listCoercionVar).equality().nullLiteral().conditional().startExpression().append(listCoercionVar).assign().objectModel().startCall(SourceGenConstants.ROM_TO_COLLECTION, true);
        node.accept(visitor);
        source.endCall().endExpression().conditionalBranchSep().append(listCoercionVar).endExpression();
    } else {
        source.objectModel().startCall(SourceGenConstants.ROM_TO_COLLECTION, true);
        typedNode.getNode().accept(visitor);
        source.endCall();
    }
}
Also used : Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)

Example 3 with Identifier

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier in project sling by apache.

the class MarkupHandler method emitMultipleFragment.

private void emitMultipleFragment(String name, Interpolation interpolation, char quoteChar, PluginInvoke invoke) {
    // Simplified algorithm for attribute output, which works when the interpolation is not of size 1. In this
    // case we are certain that the attribute value cannot be the boolean value true, so we can skip this test
    // altogether
    Expression expression = expressionWrapper.transform(interpolation, getAttributeMarkupContext(name), ExpressionContext.ATTRIBUTE);
    String attrContent = symbolGenerator.next("attrContent");
    String shouldDisplayAttr = symbolGenerator.next("shouldDisplayAttr");
    stream.write(new VariableBinding.Start(attrContent, expression.getRoot()));
    stream.write(new VariableBinding.Start(shouldDisplayAttr, new BinaryOperation(BinaryOperator.OR, new Identifier(attrContent), new BinaryOperation(BinaryOperator.EQ, new StringConstant("false"), new Identifier(attrContent)))));
    stream.write(new Conditional.Start(shouldDisplayAttr, true));
    emitAttributeStart(name);
    invoke.beforeAttributeValue(stream, name, expression.getRoot());
    emitAttributeValueStart(quoteChar);
    stream.write(new OutputVariable(attrContent));
    emitAttributeEnd(quoteChar);
    invoke.afterAttributeValue(stream, name);
    stream.write(Conditional.END);
    stream.write(VariableBinding.END);
    stream.write(VariableBinding.END);
}
Also used : Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) BinaryOperation(org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation) Conditional(org.apache.sling.scripting.sightly.compiler.commands.Conditional) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 4 with Identifier

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier in project sling by apache.

the class IncludePlugin method invoke.

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

        @Override
        public void beforeChildren(PushStream stream) {
            String includedContentVar = compilerContext.generateVariable("includedResult");
            String pathVar = compilerContext.generateVariable("includePath");
            stream.write(new VariableBinding.Start(pathVar, expression.getRoot()));
            stream.write(new VariableBinding.Start(includedContentVar, new RuntimeCall(RuntimeFunction.INCLUDE, new Identifier(pathVar), new MapLiteral(expression.getOptions()))));
            stream.write(new OutputVariable(includedContentVar));
            //end includedContentVar
            stream.write(VariableBinding.END);
            //end pathVar
            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) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) 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) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 5 with Identifier

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier in project sling by apache.

the class ListPlugin 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));
        }

        @Override
        public void beforeChildren(PushStream stream) {
            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 afterChildren(PushStream stream) {
            stream.write(VariableBinding.END);
            stream.write(Loop.END);
        }

        @Override
        public void afterElement(PushStream stream) {
            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)

Aggregations

Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)7 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)6 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)5 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)4 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)4 MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)4 HashMap (java.util.HashMap)3 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)3 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)3 Loop (org.apache.sling.scripting.sightly.compiler.commands.Loop)2 Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)2 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)2 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)2 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)2 UnaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation)2 Map (java.util.Map)1 MarkupContext (org.apache.sling.scripting.sightly.compiler.expression.MarkupContext)1