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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations