Search in sources :

Example 16 with ExpressionNode

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

the class ExpressionReducer method evaluate.

@Override
public EvalResult evaluate(RuntimeCall runtimeCall) {
    List<ExpressionNode> nodes = new ArrayList<>();
    for (ExpressionNode node : runtimeCall.getArguments()) {
        EvalResult result = eval(node);
        nodes.add(result.getNode());
    }
    return EvalResult.nonConstant(new RuntimeCall(runtimeCall.getFunctionName(), nodes));
}
Also used : ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) ArrayList(java.util.ArrayList) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)

Example 17 with ExpressionNode

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

the class ExpressionReducer method evaluate.

@Override
public EvalResult evaluate(MapLiteral mapLiteral) {
    HashMap<String, EvalResult> results = new HashMap<>();
    boolean isConstant = true;
    for (Map.Entry<String, ExpressionNode> entry : mapLiteral.getMap().entrySet()) {
        EvalResult result = eval(entry.getValue());
        results.put(entry.getKey(), result);
        isConstant = isConstant && result.isConstant();
    }
    if (isConstant) {
        HashMap<String, Object> map = new HashMap<>();
        for (Map.Entry<String, EvalResult> entry : results.entrySet()) {
            map.put(entry.getKey(), entry.getValue().getValue());
        }
        return EvalResult.constant(map);
    } else {
        HashMap<String, ExpressionNode> literal = new HashMap<>();
        for (Map.Entry<String, EvalResult> entry : results.entrySet()) {
            literal.put(entry.getKey(), entry.getValue().getNode());
        }
        return EvalResult.nonConstant(new MapLiteral(literal));
    }
}
Also used : HashMap(java.util.HashMap) MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ExpressionNode

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

the class MarkupHandler method emitSingleFragment.

private void emitSingleFragment(String name, Interpolation interpolation, char quoteChar, PluginInvoke invoke) {
    //raw expression
    Expression valueExpression = expressionWrapper.transform(interpolation, null, ExpressionContext.ATTRIBUTE);
    //holds the raw attribute value
    String attrValue = symbolGenerator.next("attrValue");
    //holds the escaped attribute value
    String attrContent = symbolGenerator.next("attrContent");
    // holds the comparison (attrValue == true)
    String isTrueVar = symbolGenerator.next("isTrueAttr");
    String shouldDisplayAttr = symbolGenerator.next("shouldDisplayAttr");
    MarkupContext markupContext = getAttributeMarkupContext(name);
    boolean alreadyEscaped = false;
    if (valueExpression.getRoot() instanceof RuntimeCall) {
        RuntimeCall rc = (RuntimeCall) valueExpression.getRoot();
        if (RuntimeFunction.XSS.equals(rc.getFunctionName())) {
            alreadyEscaped = true;
        }
    }
    ExpressionNode node = valueExpression.getRoot();
    //attrContent = <expr>
    stream.write(new VariableBinding.Start(attrValue, node));
    if (!alreadyEscaped) {
        Expression contentExpression = valueExpression.withNode(new Identifier(attrValue));
        stream.write(new VariableBinding.Start(attrContent, adjustContext(compilerContext, contentExpression, markupContext, ExpressionContext.ATTRIBUTE).getRoot()));
        stream.write(new VariableBinding.Start(shouldDisplayAttr, new BinaryOperation(BinaryOperator.OR, new Identifier(attrContent), new BinaryOperation(BinaryOperator.EQ, new StringConstant("false"), new Identifier(attrValue)))));
    } else {
        stream.write(new VariableBinding.Start(shouldDisplayAttr, new BinaryOperation(BinaryOperator.OR, new Identifier(attrValue), new BinaryOperation(BinaryOperator.EQ, new StringConstant("false"), new Identifier(attrValue)))));
    }
    // if (attrContent)
    stream.write(new Conditional.Start(shouldDisplayAttr, true));
    //write("attrName");
    emitAttributeStart(name);
    invoke.beforeAttributeValue(stream, name, node);
    stream.write(new //isTrueAttr = (attrValue == true)
    VariableBinding.Start(//isTrueAttr = (attrValue == true)
    isTrueVar, new BinaryOperation(BinaryOperator.EQ, new Identifier(attrValue), BooleanConstant.TRUE)));
    //if (!isTrueAttr)
    stream.write(new Conditional.Start(isTrueVar, false));
    // write("='");
    emitAttributeValueStart(quoteChar);
    if (!alreadyEscaped) {
        //write(attrContent)
        stream.write(new OutputVariable(attrContent));
    } else {
        // write(attrValue)
        stream.write(new OutputVariable(attrValue));
    }
    //write("'");
    emitAttributeEnd(quoteChar);
    //end if isTrueAttr
    stream.write(Conditional.END);
    //end scope for isTrueAttr
    stream.write(VariableBinding.END);
    invoke.afterAttributeValue(stream, name);
    //end if attrContent
    stream.write(Conditional.END);
    //end scope for attrContent
    stream.write(VariableBinding.END);
    if (!alreadyEscaped) {
        stream.write(VariableBinding.END);
    }
    //end scope for attrValue
    stream.write(VariableBinding.END);
}
Also used : BinaryOperation(org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) Conditional(org.apache.sling.scripting.sightly.compiler.commands.Conditional) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) MarkupContext(org.apache.sling.scripting.sightly.compiler.expression.MarkupContext) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 19 with ExpressionNode

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

the class ExpressionTranslator method visit.

@Override
public void visit(ArrayLiteral arrayLiteral) {
    source.startExpression().startArray();
    boolean needsComma = false;
    for (ExpressionNode node : arrayLiteral.getItems()) {
        if (needsComma) {
            source.separateArgument();
        }
        visit(node);
        needsComma = true;
    }
    source.endArray().endExpression();
}
Also used : ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)

Example 20 with ExpressionNode

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

Aggregations

ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)21 HashMap (java.util.HashMap)9 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)7 MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)7 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)7 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)7 ArrayList (java.util.ArrayList)6 Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)6 Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)5 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)5 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)4 ArrayLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)4 ExpressionWrapper (org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper)4 Interpolation (org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation)4 Test (org.junit.Test)4 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)3 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)3 Map (java.util.Map)2 Loop (org.apache.sling.scripting.sightly.compiler.commands.Loop)2 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)2