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