Search in sources :

Example 1 with MapLiteral

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

the class I18nFilter method apply.

@Override
public Expression apply(Expression expression, ExpressionContext expressionContext) {
    if (!expression.containsOption(I18N_OPTION) || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_USE || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_TEMPLATE || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_CALL) {
        return expression;
    }
    Map<String, ExpressionNode> options = getFilterOptions(expression, HINT_OPTION, LOCALE_OPTION, BASENAME_OPTION);
    ExpressionNode translation = new RuntimeCall(RuntimeFunction.I18N, expression.getRoot(), new MapLiteral(options));
    expression.removeOption(I18N_OPTION);
    expression.getOptions().put(FormatFilter.FORMAT_LOCALE_OPTION, options.get(LOCALE_OPTION));
    return expression.withNode(translation);
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)

Example 2 with MapLiteral

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

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

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

the class SyntheticMapRemoval method visit.

@Override
public void visit(VariableBinding.Start variableBindingStart) {
    ExpressionNode node = variableBindingStart.getExpression();
    String variable = variableBindingStart.getVariableName();
    ExpressionNode transformed = transform(node);
    if (transformed instanceof MapLiteral) {
        MapLiteral newLiteral = overrideMap(variable, (MapLiteral) transformed);
        tracker.pushVariable(variable, newLiteral);
        transformed = newLiteral;
    } else {
        tracker.pushVariable(variable, null);
    }
    outputStream.write(new VariableBinding.Start(variable, transformed));
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 5 with MapLiteral

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

the class SyntheticMapRemoval method visit.

@Override
public void visit(VariableBinding.End variableBindingEnd) {
    Map.Entry<String, MapLiteral> entry = tracker.peek();
    super.visit(variableBindingEnd);
    MapLiteral literal = entry.getValue();
    if (literal != null) {
        //need to un-bind all the introduced variables
        for (int i = 0; i < literal.getMap().size(); i++) {
            outputStream.write(VariableBinding.END);
        }
    }
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

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