Search in sources :

Example 21 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class GenPyCallExprVisitor method visitCallDelegateNode.

/**
 * Visits a delegate call node and builds the call expression to retrieve the function and execute
 * it. The get_delegate_fn returns the function directly, so its output can be called directly.
 *
 * @param node The delegate call node.
 * @return The call Python expression.
 */
@Override
protected PyExpr visitCallDelegateNode(CallDelegateNode node) {
    ExprRootNode variantSoyExpr = node.getDelCalleeVariantExpr();
    PyExpr variantPyExpr;
    if (variantSoyExpr == null) {
        // Case 1: Delegate call with empty variant.
        variantPyExpr = new PyStringExpr("''");
    } else {
        // Case 2: Delegate call with variant expression.
        TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarStack, errorReporter);
        variantPyExpr = translator.exec(variantSoyExpr);
    }
    String calleeExprText = new PyFunctionExprBuilder("runtime.get_delegate_fn").addArg(node.getDelCalleeName()).addArg(variantPyExpr).addArg(node.allowEmptyDefault()).build();
    String callExprText = calleeExprText + "(" + genObjToPass(node) + ", ijData)";
    return escapeCall(callExprText, node.getEscapingDirectives());
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 22 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class GenPyExprsVisitor method visitPrintNode.

/**
 * Visiting a print node accomplishes 3 basic tasks. It loads data, it performs any operations
 * needed, and it executes the appropriate print directives.
 *
 * <p>TODO(dcphillips): Add support for local variables once LetNode are supported.
 *
 * <p>Example:
 *
 * <pre>
 *   {$boo |changeNewlineToBr}
 *   {$goo + 5}
 * </pre>
 *
 * might generate
 *
 * <pre>
 *   sanitize.change_newline_to_br(data.get('boo'))
 *   data.get('goo') + 5
 * </pre>
 */
@Override
protected void visitPrintNode(PrintNode node) {
    TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarExprs, errorReporter);
    PyExpr pyExpr = translator.exec(node.getExpr());
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Get directive.
        SoyPrintDirective directive = directiveNode.getPrintDirective();
        if (!(directive instanceof SoyPySrcPrintDirective)) {
            errorReporter.report(directiveNode.getSourceLocation(), UNKNOWN_SOY_PY_SRC_PRINT_DIRECTIVE, directiveNode.getName());
            continue;
        }
        // Get directive args.
        List<ExprRootNode> args = directiveNode.getArgs();
        // Translate directive args.
        List<PyExpr> argsPyExprs = new ArrayList<>(args.size());
        for (ExprRootNode arg : args) {
            argsPyExprs.add(translator.exec(arg));
        }
        // Apply directive.
        pyExpr = ((SoyPySrcPrintDirective) directive).applyForPySrc(pyExpr, argsPyExprs);
    }
    pyExprs.add(pyExpr);
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyPySrcPrintDirective(com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) ArrayList(java.util.ArrayList) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 23 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class TranslateToPyExprVisitor method visitNullCoalescingOpNode.

@Override
protected PyExpr visitNullCoalescingOpNode(NullCoalescingOpNode node) {
    List<PyExpr> children = visitChildren(node);
    PyExpr conditionalExpr = PyExprUtils.genPyNotNullCheck(children.get(0));
    PyExpr trueExpr = children.get(0);
    PyExpr falseExpr = children.get(1);
    // lambda x=<left hand side> : <right hand side> if x is None else x
    return genTernaryConditional(conditionalExpr, trueExpr, falseExpr);
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr)

Example 24 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class TranslateToPyExprVisitor method visitLegacyObjectMapLiteralOrMapLiteralNode.

private PyExpr visitLegacyObjectMapLiteralOrMapLiteralNode(AbstractParentExprNode node) {
    Preconditions.checkState(node.getKind() == ExprNode.Kind.LEGACY_OBJECT_MAP_LITERAL_NODE || node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE);
    Preconditions.checkArgument(node.numChildren() % 2 == 0);
    Map<PyExpr, PyExpr> dict = new LinkedHashMap<>();
    boolean needsRuntimeNullCheck = node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE;
    for (int i = 0, n = node.numChildren(); i < n; i += 2) {
        ExprNode keyNode = node.getChild(i);
        PyExpr key = visit(keyNode);
        if (needsRuntimeNullCheck) {
            key = new PyFunctionExprBuilder("runtime.check_not_null").addArg(key).asPyExpr();
        }
        ExprNode valueNode = node.getChild(i + 1);
        dict.put(key, visit(valueNode));
    }
    // to index into the map with the wrong convention.
    return PyExprUtils.convertMapToOrderedDict(dict);
}
Also used : AbstractParentExprNode(com.google.template.soy.exprtree.AbstractParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 25 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class TranslateToPyExprVisitor method visitConditionalOpNode.

@Override
protected PyExpr visitConditionalOpNode(ConditionalOpNode node) {
    // Retrieve the operands.
    Operator op = Operator.CONDITIONAL;
    List<SyntaxElement> syntax = op.getSyntax();
    List<PyExpr> operandExprs = visitChildren(node);
    Operand conditionalOperand = ((Operand) syntax.get(0));
    PyExpr conditionalExpr = operandExprs.get(conditionalOperand.getIndex());
    Operand trueOperand = ((Operand) syntax.get(4));
    PyExpr trueExpr = operandExprs.get(trueOperand.getIndex());
    Operand falseOperand = ((Operand) syntax.get(8));
    PyExpr falseExpr = operandExprs.get(falseOperand.getIndex());
    return genTernaryConditional(conditionalExpr, trueExpr, falseExpr);
}
Also used : Operator(com.google.template.soy.exprtree.Operator) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Operand(com.google.template.soy.exprtree.Operator.Operand) SyntaxElement(com.google.template.soy.exprtree.Operator.SyntaxElement)

Aggregations

PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)82 Test (org.junit.Test)58 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)21 PyFunctionExprBuilder (com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)6 LinkedHashMap (java.util.LinkedHashMap)4 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 SoyNode (com.google.template.soy.soytree.SoyNode)3 ExprNode (com.google.template.soy.exprtree.ExprNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 PyListExpr (com.google.template.soy.pysrc.restricted.PyListExpr)2 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 PrintNode (com.google.template.soy.soytree.PrintNode)2 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)2 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1