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