use of com.google.template.soy.exprtree.IntegerNode in project closure-templates by google.
the class VeLogInstrumentationVisitor method visitHtmlAttributeNode.
/**
* For HtmlAttributeNode that has a logging function as its value, replace the logging function
* with its place holder, and append a new data attribute that contains all the desired
* information that are used later by the runtime library.
*/
@Override
protected void visitHtmlAttributeNode(HtmlAttributeNode node) {
// Skip attributes that do not have a value.
if (!node.hasValue()) {
return;
}
SourceLocation insertionLocation = node.getSourceLocation();
for (FunctionNode function : SoyTreeUtils.getAllNodesOfType(node, FunctionNode.class)) {
if (!(function.getSoyFunction() instanceof LoggingFunction)) {
continue;
}
FunctionNode funcNode = new FunctionNode(VeLogJsSrcLoggingFunction.INSTANCE, insertionLocation);
funcNode.addChild(new StringNode(function.getFunctionName(), QuoteStyle.SINGLE, insertionLocation));
funcNode.addChild(new ListLiteralNode(function.getChildren(), insertionLocation));
StandaloneNode attributeName = node.getChild(0);
if (attributeName instanceof RawTextNode) {
// If attribute name is a plain text, directly pass it as a function argument.
funcNode.addChild(new StringNode(((RawTextNode) attributeName).getRawText(), QuoteStyle.SINGLE, insertionLocation));
} else {
// Otherwise wrap the print node or call node into a let block, and use the let variable
// as a function argument.
String varName = "soy_logging_function_attribute_" + counter;
LetContentNode letNode = LetContentNode.forVariable(nodeIdGen.genId(), attributeName.getSourceLocation(), varName, null);
// Adds a let var which references to the original attribute name, and move the name to
// the let block.
node.replaceChild(attributeName, new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
true, /* expr= */
new VarRefNode(varName, insertionLocation, false, letNode.getVar()), /* attributes= */
ImmutableList.of(), ErrorReporter.exploding()));
letNode.addChild(attributeName);
node.getParent().addChild(node.getParent().getChildIndex(node), letNode);
funcNode.addChild(new VarRefNode(varName, insertionLocation, false, letNode.getVar()));
}
funcNode.addChild(new IntegerNode(counter++, insertionLocation));
PrintNode loggingFunctionAttribute = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
true, /* expr= */
funcNode, /* attributes= */
ImmutableList.of(), ErrorReporter.exploding());
// Append the logging function attribute to its parent
int appendIndex = node.getParent().getChildIndex(node) + 1;
node.getParent().addChild(appendIndex, loggingFunctionAttribute);
// Replace the original attribute value to the placeholder.
HtmlAttributeValueNode placeHolder = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, Quotes.DOUBLE);
placeHolder.addChild(new RawTextNode(nodeIdGen.genId(), ((LoggingFunction) function.getSoyFunction()).getPlaceholder(), insertionLocation));
node.replaceChild(node.getChild(1), placeHolder);
// logging function in a html attribute value.
break;
}
visitChildren(node);
}
use of com.google.template.soy.exprtree.IntegerNode in project closure-templates by google.
the class InsertMsgsVisitor method replaceIsPrimaryMsgInUseFunction.
private void replaceIsPrimaryMsgInUseFunction(FunctionNode node) {
boolean isPrimaryMsgInUse;
if (msgBundle == null) {
isPrimaryMsgInUse = true;
} else {
// if the primary message id is available or the fallback message is not available, then we
// are using the primary message.
long primaryMsgId = ((IntegerNode) node.getChild(1)).getValue();
long fallbackMsgId = ((IntegerNode) node.getChild(2)).getValue();
isPrimaryMsgInUse = !msgBundle.getMsgParts(primaryMsgId).isEmpty() || msgBundle.getMsgParts(fallbackMsgId).isEmpty();
}
node.getParent().replaceChild(node, new BooleanNode(isPrimaryMsgInUse, node.getSourceLocation()));
}
use of com.google.template.soy.exprtree.IntegerNode in project closure-templates by google.
the class InternalValueUtilsTest method testConvertPrimitiveExprToData.
@Test
public void testConvertPrimitiveExprToData() {
assertTrue(InternalValueUtils.convertPrimitiveExprToData(new NullNode(SourceLocation.UNKNOWN)) instanceof NullData);
assertTrue(InternalValueUtils.convertPrimitiveExprToData(new BooleanNode(true, SourceLocation.UNKNOWN)).booleanValue());
assertEquals(-1, InternalValueUtils.convertPrimitiveExprToData(new IntegerNode(-1, SourceLocation.UNKNOWN)).integerValue());
assertEquals(6.02e23, InternalValueUtils.convertPrimitiveExprToData(new FloatNode(6.02e23, SourceLocation.UNKNOWN)).floatValue(), 0.0);
assertEquals("foo", InternalValueUtils.convertPrimitiveExprToData(new StringNode("foo", QuoteStyle.SINGLE, SourceLocation.UNKNOWN)).stringValue());
}
use of com.google.template.soy.exprtree.IntegerNode in project closure-templates by google.
the class ParseExpressionTest method testParseFunctionCall.
@Test
public void testParseFunctionCall() throws Exception {
ExprNode expr = assertThatExpression("isFirst($x)").isValidExpression();
FunctionNode isFirstFn = (FunctionNode) expr;
assertThat(isFirstFn.getFunctionName()).isEqualTo("isFirst");
assertThat(isFirstFn.numChildren()).isEqualTo(1);
assertThat(isFirstFn.getChild(0).toSourceString()).isEqualTo("$x");
expr = assertThatExpression("round(3.14159, 2)").isValidExpression();
FunctionNode roundFn = (FunctionNode) expr;
assertThat(roundFn.getFunctionName()).isEqualTo("round");
assertThat(roundFn.numChildren()).isEqualTo(2);
assertThat(((FloatNode) roundFn.getChild(0)).getValue()).isEqualTo(3.14159);
assertThat(((IntegerNode) roundFn.getChild(1)).getValue()).isEqualTo(2);
}
use of com.google.template.soy.exprtree.IntegerNode in project closure-templates by google.
the class ParseExpressionTest method testParsePrimitives.
@Test
public void testParsePrimitives() throws Exception {
ExprNode expr = assertThatExpression("null").isValidExpression();
assertThat(expr).isInstanceOf(NullNode.class);
expr = assertThatExpression("true").isValidExpression();
assertThat(((BooleanNode) expr).getValue()).isTrue();
expr = assertThatExpression("false").isValidExpression();
assertThat(((BooleanNode) expr).getValue()).isFalse();
expr = assertThatExpression("26").isValidExpression();
assertThat(((IntegerNode) expr).getValue()).isEqualTo(26);
expr = assertThatExpression("0xCAFE").isValidExpression();
assertThat(((IntegerNode) expr).getValue()).isEqualTo(0xCAFE);
expr = assertThatExpression("3.14").isValidExpression();
assertThat(((FloatNode) expr).getValue()).isEqualTo(3.14);
expr = assertThatExpression("3e-3").isValidExpression();
assertThat(((FloatNode) expr).getValue()).isEqualTo(3e-3);
expr = assertThatExpression("'Aa`! \\n \\r \\t \\\\ \\\' \"'").isValidExpression();
assertThat(((StringNode) expr).getValue()).isEqualTo("Aa`! \n \r \t \\ \' \"");
expr = assertThatExpression("'\\u2222 \\uEEEE \\u9EC4 \\u607A'").isValidExpression();
assertThat(((StringNode) expr).getValue()).isEqualTo("\u2222 \uEEEE \u9EC4 \u607A");
expr = assertThatExpression("'\u2222 \uEEEE \u9EC4 \u607A'").isValidExpression();
assertThat(((StringNode) expr).getValue()).isEqualTo("\u2222 \uEEEE \u9EC4 \u607A");
}
Aggregations