Search in sources :

Example 6 with IntegerNode

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);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ListLiteralNode(com.google.template.soy.exprtree.ListLiteralNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) StringNode(com.google.template.soy.exprtree.StringNode) LoggingFunction(com.google.template.soy.logging.LoggingFunction) PrintNode(com.google.template.soy.soytree.PrintNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 7 with IntegerNode

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()));
}
Also used : IntegerNode(com.google.template.soy.exprtree.IntegerNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode)

Example 8 with IntegerNode

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());
}
Also used : NullData(com.google.template.soy.data.restricted.NullData) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FloatNode(com.google.template.soy.exprtree.FloatNode) StringNode(com.google.template.soy.exprtree.StringNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode) NullNode(com.google.template.soy.exprtree.NullNode) Test(org.junit.Test)

Example 9 with IntegerNode

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);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) FloatNode(com.google.template.soy.exprtree.FloatNode) Test(org.junit.Test)

Example 10 with IntegerNode

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");
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FloatNode(com.google.template.soy.exprtree.FloatNode) StringNode(com.google.template.soy.exprtree.StringNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode) Test(org.junit.Test)

Aggregations

IntegerNode (com.google.template.soy.exprtree.IntegerNode)15 Test (org.junit.Test)8 ExprNode (com.google.template.soy.exprtree.ExprNode)7 StringNode (com.google.template.soy.exprtree.StringNode)6 BooleanNode (com.google.template.soy.exprtree.BooleanNode)4 FloatNode (com.google.template.soy.exprtree.FloatNode)4 FunctionNode (com.google.template.soy.exprtree.FunctionNode)4 GlobalNode (com.google.template.soy.exprtree.GlobalNode)4 VarRefNode (com.google.template.soy.exprtree.VarRefNode)4 SourceLocation (com.google.template.soy.base.SourceLocation)3 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)3 ItemAccessNode (com.google.template.soy.exprtree.ItemAccessNode)2 NullNode (com.google.template.soy.exprtree.NullNode)2 ConditionalOpNode (com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode)2 PrintNode (com.google.template.soy.soytree.PrintNode)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 CopyState (com.google.template.soy.basetree.CopyState)1 NullData (com.google.template.soy.data.restricted.NullData)1