Search in sources :

Example 1 with ParentExprNode

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

the class ExpressionCompilerTest method createTemplateBody.

private String createTemplateBody(String soyExpr) {
    // collect all varrefs and apply them as template parameters.  This way all varrefs have a valid
    // vardef
    // TODO(lukes): this logic would be useful in a lot of tests and potentially unblock efforts to
    // eliminate UNDECLARED vars
    ExprNode expr = SoyFileParser.parseExpression(soyExpr, PluginResolver.nullResolver(Mode.ALLOW_UNDEFINED, ErrorReporter.exploding()), ErrorReporter.exploding());
    final StringBuilder templateBody = new StringBuilder();
    new AbstractExprNodeVisitor<Void>() {

        final Set<String> names = new HashSet<>();

        @Override
        protected void visitVarRefNode(VarRefNode node) {
            if (names.add(node.getName())) {
                SoyType type = variables.get(node.getName()).soyType();
                templateBody.append("{@param ").append(node.getName()).append(": ").append(type).append("}\n");
            }
        }

        @Override
        protected void visitExprNode(ExprNode node) {
            if (node instanceof ParentExprNode) {
                visitChildren((ParentExprNode) node);
            }
        }
    }.exec(expr);
    templateBody.append("{" + soyExpr + "}\n");
    return templateBody.toString();
}
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) VarRefNode(com.google.template.soy.exprtree.VarRefNode) SoyType(com.google.template.soy.types.SoyType) HashSet(java.util.HashSet)

Example 2 with ParentExprNode

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

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

the class SharedTestUtils method createTemplateBodyForExpression.

/**
 * Returns a template body for the given soy expression. With type specializations.
 */
public static String createTemplateBodyForExpression(String soyExpr, final Map<String, SoyType> typeMap) {
    ExprNode expr = SoyFileParser.parseExpression(soyExpr, PluginResolver.nullResolver(Mode.ALLOW_UNDEFINED, ErrorReporter.exploding()), ErrorReporter.exploding());
    final Set<String> loopVarNames = new HashSet<>();
    final Set<String> names = new HashSet<>();
    new AbstractExprNodeVisitor<Void>() {

        @Override
        protected void visitVarRefNode(VarRefNode node) {
            if (!node.isDollarSignIjParameter()) {
                names.add(node.getName());
            }
        }

        @Override
        protected void visitFunctionNode(FunctionNode node) {
            switch(node.getFunctionName()) {
                case "index":
                case "isFirst":
                case "isLast":
                    loopVarNames.add(((VarRefNode) node.getChild(0)).getName());
                    break;
                // fall out
                default:
            }
            visitChildren(node);
        }

        @Override
        protected void visitExprNode(ExprNode node) {
            if (node instanceof ParentExprNode) {
                visitChildren((ParentExprNode) node);
            }
        }
    }.exec(expr);
    final StringBuilder templateBody = new StringBuilder();
    for (String varName : Sets.difference(names, loopVarNames)) {
        SoyType type = typeMap.get(varName);
        if (type == null) {
            type = UnknownType.getInstance();
        }
        templateBody.append("{@param " + varName + ": " + type + "}\n");
    }
    String contents = "{" + soyExpr + "}\n";
    for (String loopVar : loopVarNames) {
        contents = "{for $" + loopVar + " in [null]}\n" + contents + "\n{/for}";
    }
    templateBody.append(contents);
    return templateBody.toString();
}
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) VarRefNode(com.google.template.soy.exprtree.VarRefNode) SoyType(com.google.template.soy.types.SoyType) FunctionNode(com.google.template.soy.exprtree.FunctionNode) HashSet(java.util.HashSet)

Aggregations

ExprNode (com.google.template.soy.exprtree.ExprNode)3 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 SoyType (com.google.template.soy.types.SoyType)2 HashSet (java.util.HashSet)2 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1