Search in sources :

Example 6 with SoyNode

use of com.google.template.soy.soytree.SoyNode in project closure-templates by google.

the class ExtractMsgsVisitor method execOnMultipleNodes.

/**
 * Returns a SoyMsgBundle containing all messages extracted from the given nodes (locale string is
 * null).
 */
public SoyMsgBundle execOnMultipleNodes(Iterable<? extends SoyNode> nodes) {
    msgs = Lists.newArrayList();
    for (SoyNode node : nodes) {
        visit(node);
    }
    Collections.sort(msgs, SOURCE_LOCATION_ORDERING);
    return new SoyMsgBundleImpl(null, msgs);
}
Also used : SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)

Example 7 with SoyNode

use of com.google.template.soy.soytree.SoyNode in project closure-templates by google.

the class SoyNodeCompiler method visitSwitchNode.

@Override
protected Statement visitSwitchNode(SwitchNode node) {
    // A few special cases:
    // 1. only a {default} block.  In this case we can skip all the switch logic and temporaries
    // 2. no children.  Just return the empty statement
    // Note that in both of these cases we do not evalutate (or generate code) for the switch
    // expression.
    List<BlockNode> children = node.getChildren();
    if (children.isEmpty()) {
        return Statement.NULL_STATEMENT;
    }
    if (children.size() == 1 && children.get(0) instanceof SwitchDefaultNode) {
        return visitChildrenInNewScope(children.get(0));
    }
    // otherwise we need to evaluate the switch variable and generate dispatching logic.
    SoyExpression switchVar = exprCompiler.compile(node.getExpr());
    Scope scope = variables.enterScope();
    Variable variable = scope.createSynthetic(SyntheticVarName.forSwitch(node), switchVar, STORE);
    Statement initializer = variable.initializer();
    switchVar = switchVar.withSource(variable.local());
    List<IfBlock> cases = new ArrayList<>();
    Optional<Statement> defaultBlock = Optional.absent();
    for (SoyNode child : children) {
        if (child instanceof SwitchCaseNode) {
            SwitchCaseNode caseNode = (SwitchCaseNode) child;
            Label reattachPoint = new Label();
            List<Expression> comparisons = new ArrayList<>();
            for (ExprRootNode caseExpr : caseNode.getExprList()) {
                comparisons.add(compareSoyEquals(switchVar, exprCompiler.compile(caseExpr, reattachPoint)));
            }
            Expression condition = BytecodeUtils.logicalOr(comparisons).labelStart(reattachPoint);
            Statement block = visitChildrenInNewScope(caseNode);
            cases.add(IfBlock.create(condition, block));
        } else {
            SwitchDefaultNode defaultNode = (SwitchDefaultNode) child;
            defaultBlock = Optional.of(visitChildrenInNewScope(defaultNode));
        }
    }
    Statement exitScope = scope.exitScope();
    // generation that we could maybe use
    return Statement.concat(initializer, ControlFlow.ifElseChain(cases, defaultBlock), exitScope);
}
Also used : BlockNode(com.google.template.soy.soytree.SoyNode.BlockNode) SoyNode(com.google.template.soy.soytree.SoyNode) Variable(com.google.template.soy.jbcsrc.TemplateVariableManager.Variable) Statement(com.google.template.soy.jbcsrc.restricted.Statement) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Scope(com.google.template.soy.jbcsrc.TemplateVariableManager.Scope) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SwitchCaseNode(com.google.template.soy.soytree.SwitchCaseNode) IfBlock(com.google.template.soy.jbcsrc.ControlFlow.IfBlock) SwitchDefaultNode(com.google.template.soy.soytree.SwitchDefaultNode)

Example 8 with SoyNode

use of com.google.template.soy.soytree.SoyNode in project closure-templates by google.

the class ResolveExpressionTypesVisitor method visitIfNode.

@Override
protected void visitIfNode(IfNode node) {
    // TODO(user): Also support switch / case.
    TypeSubstitution savedSubstitutionState = substitutions;
    for (SoyNode child : node.getChildren()) {
        if (child instanceof IfCondNode) {
            IfCondNode icn = (IfCondNode) child;
            visitExpressions(icn);
            // Visit the conditional expression to compute which types can be narrowed.
            TypeNarrowingConditionVisitor visitor = new TypeNarrowingConditionVisitor();
            visitor.exec(icn.getExpr());
            // Save the state of substitutions from the previous if block.
            TypeSubstitution previousSubstitutionState = substitutions;
            // Modify the current set of type substitutions for the 'true' branch
            // of the if statement.
            addTypeSubstitutions(visitor.positiveTypeConstraints);
            visitChildren(icn);
            // Rewind the substitutions back to the state before the if-condition.
            // Add in the negative substitutions, which will affect subsequent blocks
            // of the if statement.
            // So for example if we have a variable whose type is (A|B|C) and the
            // first if-block tests whether that variable is type A, then in the
            // 'else' block it must be of type (B|C); If a subsequent 'elseif'
            // statement tests whether it's type B, then in the following else block
            // it can only be of type C.
            substitutions = previousSubstitutionState;
            addTypeSubstitutions(visitor.negativeTypeConstraints);
        } else if (child instanceof IfElseNode) {
            // For the else node, we simply inherit the previous set of subsitutions.
            IfElseNode ien = (IfElseNode) child;
            visitChildren(ien);
        }
    }
    substitutions = savedSubstitutionState;
}
Also used : IfElseNode(com.google.template.soy.soytree.IfElseNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) SoyNode(com.google.template.soy.soytree.SoyNode) IfCondNode(com.google.template.soy.soytree.IfCondNode)

Example 9 with SoyNode

use of com.google.template.soy.soytree.SoyNode in project closure-templates by google.

the class MsgFuncGenerator method collectVarNameListAndToPyExprMap.

/**
 * Private helper to process and collect all variables used within this msg node for code
 * generation.
 *
 * @return A Map populated with all the variables used with in this message node, using {@link
 *     MsgPlaceholderInitialNode#genBasePhName}.
 */
private Map<PyExpr, PyExpr> collectVarNameListAndToPyExprMap() {
    Map<PyExpr, PyExpr> nodePyVarToPyExprMap = new LinkedHashMap<>();
    for (Map.Entry<String, MsgSubstUnitNode> entry : msgNode.getVarNameToRepNodeMap().entrySet()) {
        MsgSubstUnitNode substUnitNode = entry.getValue();
        PyExpr substPyExpr = null;
        if (substUnitNode instanceof MsgPlaceholderNode) {
            SoyNode phInitialNode = ((AbstractParentSoyNode<?>) substUnitNode).getChild(0);
            if (phInitialNode instanceof PrintNode || phInitialNode instanceof CallNode || phInitialNode instanceof RawTextNode) {
                substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(phInitialNode)).toPyString();
            }
            // when the placeholder is generated by HTML tags
            if (phInitialNode instanceof MsgHtmlTagNode) {
                substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.execOnChildren((ParentSoyNode<?>) phInitialNode)).toPyString();
            }
        } else if (substUnitNode instanceof MsgPluralNode) {
            // Translates {@link MsgPluralNode#pluralExpr} into a Python lookup expression.
            // Note that {@code pluralExpr} represents the soy expression of the {@code plural} attr,
            // i.e. the {@code $numDrafts} in {@code {plural $numDrafts}...{/plural}}.
            substPyExpr = translateToPyExprVisitor.exec(((MsgPluralNode) substUnitNode).getExpr());
        } else if (substUnitNode instanceof MsgSelectNode) {
            substPyExpr = translateToPyExprVisitor.exec(((MsgSelectNode) substUnitNode).getExpr());
        }
        if (substPyExpr != null) {
            nodePyVarToPyExprMap.put(new PyStringExpr("'" + entry.getKey() + "'"), substPyExpr);
        }
    }
    return nodePyVarToPyExprMap;
}
Also used : AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) MsgSubstUnitNode(com.google.template.soy.soytree.SoyNode.MsgSubstUnitNode) CallNode(com.google.template.soy.soytree.CallNode) LinkedHashMap(java.util.LinkedHashMap) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode)

Example 10 with SoyNode

use of com.google.template.soy.soytree.SoyNode in project closure-templates by google.

the class RewriteGlobalsPassTest method testResolveAlias.

@Test
public void testResolveAlias() {
    String template = "" + "{namespace ns}\n" + "\n" + "{alias foo.bar.baz as global}\n" + "{alias global.with.sugar}\n" + "\n" + "{template .t}\n" + "  {global}\n" + "  {global.with.field}\n" + "  {sugar}\n" + "  {sugar.with.field}\n" + "  {unregistered}\n" + "{/template}";
    SoyFileSetNode soytree = SoyFileSetParserBuilder.forFileContents(template).allowUnboundGlobals(true).parse().fileSet();
    ImmutableList.Builder<String> actual = ImmutableList.builder();
    for (SoyNode child : soytree.getChild(0).getChild(0).getChildren()) {
        actual.add(child.toSourceString());
    }
    assertThat(actual.build()).containsExactly("{foo.bar.baz}", "{foo.bar.baz.with.field}", "{global.with.sugar}", "{global.with.sugar.with.field}", "{unregistered}").inOrder();
}
Also used : SoyNode(com.google.template.soy.soytree.SoyNode) ImmutableList(com.google.common.collect.ImmutableList) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Aggregations

SoyNode (com.google.template.soy.soytree.SoyNode)19 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)9 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)6 IfCondNode (com.google.template.soy.soytree.IfCondNode)5 IfElseNode (com.google.template.soy.soytree.IfElseNode)5 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 ArrayList (java.util.ArrayList)4 ErrorReporter (com.google.template.soy.error.ErrorReporter)3 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)3 ImmutableList (com.google.common.collect.ImmutableList)2 UniqueNameGenerator (com.google.template.soy.base.internal.UniqueNameGenerator)2 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)2 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 Statement (com.google.template.soy.jbcsrc.restricted.Statement)2 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 SwitchCaseNode (com.google.template.soy.soytree.SwitchCaseNode)2 SwitchDefaultNode (com.google.template.soy.soytree.SwitchDefaultNode)2 Supplier (com.google.common.base.Supplier)1 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1