Search in sources :

Example 6 with ExprNode

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

the class ResolveExpressionTypesVisitor method addTypeSubstitutions.

// Given a map of type subsitutions, add all the entries to the current set of
// active substitutions.
private void addTypeSubstitutions(Map<Wrapper<ExprNode>, SoyType> substitutionsToAdd) {
    for (Map.Entry<Wrapper<ExprNode>, SoyType> entry : substitutionsToAdd.entrySet()) {
        ExprNode expr = entry.getKey().get();
        // Get the existing type
        SoyType previousType = expr.getType();
        for (TypeSubstitution subst = substitutions; subst != null; subst = subst.parent) {
            if (ExprEquivalence.get().equivalent(subst.expression, expr)) {
                previousType = subst.type;
                break;
            }
        }
        // If the new type is different than the current type, then add a new type substitution.
        if (!entry.getValue().equals(previousType)) {
            substitutions = new TypeSubstitution(substitutions, expr, entry.getValue());
        }
    }
}
Also used : AbstractParentExprNode(com.google.template.soy.exprtree.AbstractParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) Wrapper(com.google.common.base.Equivalence.Wrapper) SoyType(com.google.template.soy.types.SoyType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 7 with ExprNode

use of com.google.template.soy.exprtree.ExprNode 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 8 with ExprNode

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

the class SimplifyExprVisitor method visitExprNode.

// -----------------------------------------------------------------------------------------------
// Fallback implementation.
@Override
protected void visitExprNode(ExprNode node) {
    if (!(node instanceof ParentExprNode)) {
        return;
    }
    ParentExprNode nodeAsParent = (ParentExprNode) node;
    // Recurse.
    visitChildren(nodeAsParent);
    // constant.
    for (ExprNode child : nodeAsParent.getChildren()) {
        if (!isConstant(child)) {
            // cannot preevaluate
            return;
        }
    }
    attemptPreeval(nodeAsParent);
}
Also used : ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode)

Example 9 with ExprNode

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

the class XidPass method run.

@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
    for (FunctionNode fn : SoyTreeUtils.getAllNodesOfType(file, FunctionNode.class)) {
        if (fn.getSoyFunction() == BuiltinFunction.XID) {
            if (fn.numChildren() != 1) {
                // if it isn't == 1, then an error has already been reported, move along.
                continue;
            }
            ExprNode child = fn.getChild(0);
            switch(child.getKind()) {
                case GLOBAL_NODE:
                    GlobalNode global = (GlobalNode) child;
                    if (global.isResolved()) {
                        // This doesn't have to be an error. but it is confusing if it is is since it is
                        // unclear if the user intended to xid the identifier or the value.
                        reporter.report(global.getSourceLocation(), GLOBAL_XID_ARG_IS_RESOLVED, global.getType().toString(), global.getValue().toSourceString());
                    }
                    fn.replaceChild(0, new StringNode(global.getName(), QuoteStyle.SINGLE, global.getSourceLocation()));
                    break;
                case STRING_NODE:
                    break;
                default:
                    reporter.report(child.getSourceLocation(), STRING_OR_GLOBAL_REQUIRED);
            }
        }
    }
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Example 10 with ExprNode

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

the class SoyTreeUtils method visitAllNodes.

/**
 * Runs the visitor on all nodes (including {@link ExprNode expr nodes}) reachable from the given
 * node. The order of visiting is breadth first.
 *
 * <p>If the visitor return {@code false} from {@link NodeVisitor#exec(Node)} we will short
 * circuit visiting.
 */
public static void visitAllNodes(Node node, NodeVisitor<? super Node, VisitDirective> visitor) {
    ArrayDeque<Node> queue = new ArrayDeque<>();
    queue.add(node);
    Node current;
    while ((current = queue.poll()) != null) {
        switch(visitor.exec(current)) {
            case ABORT:
                return;
            case CONTINUE:
                if (current instanceof ParentNode<?>) {
                    queue.addAll(((ParentNode<?>) current).getChildren());
                }
                if (current instanceof ExprHolderNode) {
                    queue.addAll(((ExprHolderNode) current).getExprList());
                }
                continue;
            case SKIP_CHILDREN:
                continue;
            default:
                throw new AssertionError();
        }
    }
}
Also used : ParentNode(com.google.template.soy.basetree.ParentNode) Node(com.google.template.soy.basetree.Node) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) ExprHolderNode(com.google.template.soy.soytree.SoyNode.ExprHolderNode) ParentNode(com.google.template.soy.basetree.ParentNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ExprHolderNode(com.google.template.soy.soytree.SoyNode.ExprHolderNode) ArrayDeque(java.util.ArrayDeque)

Aggregations

ExprNode (com.google.template.soy.exprtree.ExprNode)38 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)7 IntegerNode (com.google.template.soy.exprtree.IntegerNode)7 StringNode (com.google.template.soy.exprtree.StringNode)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)5 Test (org.junit.Test)5 ImmutableList (com.google.common.collect.ImmutableList)4 SoyValue (com.google.template.soy.data.SoyValue)4 GlobalNode (com.google.template.soy.exprtree.GlobalNode)4 VarRefNode (com.google.template.soy.exprtree.VarRefNode)4 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 SoyType (com.google.template.soy.types.SoyType)4 LinkedHashMap (java.util.LinkedHashMap)4 AbstractParentExprNode (com.google.template.soy.exprtree.AbstractParentExprNode)3 FloatNode (com.google.template.soy.exprtree.FloatNode)3 SourceLocation (com.google.template.soy.base.SourceLocation)2 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 BooleanNode (com.google.template.soy.exprtree.BooleanNode)2 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2