Search in sources :

Example 11 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class GenJsExprsVisitor method visitPrintNode.

/**
 * Example:
 * <pre>
 *   {$boo.foo}
 *   {$goo.moo + 5}
 * </pre>
 * might generate
 * <pre>
 *   opt_data.boo.foo
 *   gooData4.moo + 5
 * </pre>
 */
@Override
protected void visitPrintNode(PrintNode node) {
    CodeChunk.WithValue expr = translateExpr(node.getExpr());
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Get directive.
        SoyPrintDirective directive = directiveNode.getPrintDirective();
        if (!(directive instanceof SoyJsSrcPrintDirective)) {
            errorReporter.report(node.getSourceLocation(), UNKNOWN_SOY_JS_SRC_PRINT_DIRECTIVE, directiveNode.getName());
            return;
        }
        // Get directive args.
        List<ExprRootNode> argNodes = directiveNode.getArgs();
        // Convert args to CodeChunks.
        List<CodeChunk.WithValue> argChunks = new ArrayList<>(argNodes.size());
        for (ExprRootNode argNode : argNodes) {
            argChunks.add(translateExpr(argNode));
        }
        // Apply directive.
        expr = SoyJsPluginUtils.applyDirective(translationContext.codeGenerator(), expr, (SoyJsSrcPrintDirective) directive, argChunks);
    }
    chunks.add(expr);
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) ArrayList(java.util.ArrayList) SoyJsSrcPrintDirective(com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 12 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class RewriteGenderMsgsVisitor method visitMsgNode.

// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
@Override
protected void visitMsgNode(MsgNode msg) {
    List<ExprRootNode> genderExprs = msg.getAndRemoveGenderExprs();
    if (genderExprs == null) {
        // not a msg that this pass should rewrite
        return;
    }
    // ------ Do the rewrite. ------
    // Note: We process the genders in reverse order so that the first listed gender will end up
    // being the outermost 'select' level.
    genderExprs = Lists.reverse(genderExprs);
    Checkpoint checkpoint = errorReporter.checkpoint();
    List<String> baseSelectVarNames = MsgSubstUnitBaseVarNameUtils.genNoncollidingBaseNamesForExprs(ExprRootNode.unwrap(genderExprs), FALLBACK_BASE_SELECT_VAR_NAME, errorReporter);
    if (errorReporter.errorsSince(checkpoint)) {
        // To prevent an IndexOutOfBoundsException below.
        return;
    }
    for (int i = 0; i < genderExprs.size(); i++) {
        ExprRootNode genderExpr = genderExprs.get(i);
        String baseSelectVarName = baseSelectVarNames.get(i);
        // and the new algorithm). If so, then there's no need to specify the baseSelectVarName.
        if (MsgSubstUnitBaseVarNameUtils.genNaiveBaseNameForExpr(genderExpr.getRoot(), FALLBACK_BASE_SELECT_VAR_NAME).equals(baseSelectVarName) && MsgSubstUnitBaseVarNameUtils.genShortestBaseNameForExpr(genderExpr.getRoot(), FALLBACK_BASE_SELECT_VAR_NAME).equals(baseSelectVarName)) {
            baseSelectVarName = null;
        }
        splitMsgForGender(msg, genderExpr, baseSelectVarName);
    }
    // ------ Verify from the re-written msg that gender restrictions are followed. ------
    checkExceedsMaxGenders((MsgSelectNode) msg.getChild(0), 1);
}
Also used : Checkpoint(com.google.template.soy.error.ErrorReporter.Checkpoint) Checkpoint(com.google.template.soy.error.ErrorReporter.Checkpoint) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 13 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class TemplateDelegateNodeBuilder method setCommandValues.

@Override
public TemplateNodeBuilder setCommandValues(Identifier templateName, List<CommandTagAttribute> attrs) {
    this.cmdText = templateName.identifier() + " " + Joiner.on(' ').join(attrs);
    setCommonCommandValues(attrs);
    this.delTemplateName = templateName.identifier();
    for (CommandTagAttribute attribute : attrs) {
        Identifier name = attribute.getName();
        if (COMMON_ATTRIBUTE_NAMES.contains(name.identifier())) {
            continue;
        }
        switch(name.identifier()) {
            case "variant":
                // need to get variant parsing out of this.  maybe we can expose some sort of limited
                // primitiveOrGlobal parsing solution?
                ExprNode variantExpr = attribute.valueAsExpr(errorReporter);
                validateVariantExpression(variantExpr, errorReporter);
                this.delTemplateVariantExpr = new ExprRootNode(variantExpr);
                break;
            default:
                errorReporter.report(name.location(), CommandTagAttribute.UNSUPPORTED_ATTRIBUTE_KEY, name.identifier(), "deltemplate", ImmutableList.builder().addAll(COMMON_ATTRIBUTE_NAMES).add("variant").build());
        }
    }
    this.delPriority = soyFileHeaderInfo.priority;
    genInternalTemplateNameHelper();
    return this;
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) Identifier(com.google.template.soy.base.internal.Identifier) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 14 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class RenderVisitor method visitPrintNode.

@Override
protected void visitPrintNode(PrintNode node) {
    SoyValue result = eval(node.getExpr(), node);
    if (result instanceof UndefinedData) {
        throw RenderException.createWithSource("In 'print' tag, expression \"" + node.getExpr().toSourceString() + "\" evaluates to undefined.", node);
    }
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Evaluate directive args.
        List<ExprRootNode> argsExprs = directiveNode.getArgs();
        List<SoyValue> argsSoyDatas = Lists.newArrayListWithCapacity(argsExprs.size());
        for (ExprRootNode argExpr : argsExprs) {
            argsSoyDatas.add(eval(argExpr, directiveNode));
        }
        // Apply directive.
        result = applyDirective(directiveNode.getPrintDirective(), result, argsSoyDatas, node);
    }
    append(currOutputBuf, result, node);
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) UndefinedData(com.google.template.soy.data.restricted.UndefinedData) SoyValue(com.google.template.soy.data.SoyValue) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 15 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class TemplateNodeTest method testValidVariant.

@Test
public void testValidVariant() {
    // Variant is a string literal: There's no expression and the value is already resolved.
    TemplateDelegateNode node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"'abc'\"}", "{/deltemplate}"));
    assertEquals("namespace.boo", node.getDelTemplateName());
    assertEquals("abc", node.getDelTemplateVariant());
    assertEquals("abc", node.getDelTemplateKey().variant());
    // Variant is a global, that was not yet resolved.
    node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"test.GLOBAL_CONSTANT\"}", "{/deltemplate}"));
    assertEquals("namespace.boo", node.getDelTemplateName());
    assertEquals("test.GLOBAL_CONSTANT", node.getDelTemplateVariant());
    assertEquals("test.GLOBAL_CONSTANT", node.getDelTemplateKey().variant());
    // Verify the global expression.
    List<ExprRootNode> exprs = node.getExprList();
    assertEquals(1, exprs.size());
    ExprRootNode expr = exprs.get(0);
    assertEquals("test.GLOBAL_CONSTANT", expr.toSourceString());
    assertEquals(1, expr.numChildren());
    assertTrue(expr.getRoot() instanceof GlobalNode);
    // Substitute the global expression.
    expr.replaceChild(0, new IntegerNode(123, expr.getRoot().getSourceLocation()));
    // Check the new values.
    assertEquals("123", node.getDelTemplateVariant());
    assertEquals("123", node.getDelTemplateKey().variant());
    // Resolve a global to a string.
    node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"test.GLOBAL_CONSTANT\"}", "{/deltemplate}"));
    node.getExprList().get(0).replaceChild(0, new StringNode("variant", QuoteStyle.SINGLE, node.getSourceLocation()));
    assertEquals("variant", node.getDelTemplateVariant());
    assertEquals("variant", node.getDelTemplateKey().variant());
}
Also used : IntegerNode(com.google.template.soy.exprtree.IntegerNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode) Test(org.junit.Test)

Aggregations

ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)16 SoyDataException (com.google.template.soy.data.SoyDataException)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 PrintDirectiveNode (com.google.template.soy.soytree.PrintDirectiveNode)3 ArrayList (java.util.ArrayList)3 SoyValue (com.google.template.soy.data.SoyValue)2 StringNode (com.google.template.soy.exprtree.StringNode)2 Expression (com.google.template.soy.jbcsrc.restricted.Expression)2 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 Statement (com.google.template.soy.jbcsrc.restricted.Statement)2 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)2 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)2 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)2 CaseOrDefaultNode (com.google.template.soy.soytree.CaseOrDefaultNode)2 Label (org.objectweb.asm.Label)2 Identifier (com.google.template.soy.base.internal.Identifier)1 IntegerData (com.google.template.soy.data.restricted.IntegerData)1 UndefinedData (com.google.template.soy.data.restricted.UndefinedData)1 Checkpoint (com.google.template.soy.error.ErrorReporter.Checkpoint)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1