Search in sources :

Example 1 with ConditionalOpNode

use of com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode in project closure-templates by google.

the class MsgIdFunctionPass method handleMsgIdCall.

/**
 * Rewrites calls to msgId($msgVar) to either a static constant message id or a conditional if
 * there is a fallback.
 */
private void handleMsgIdCall(FunctionNode fn, MsgFallbackGroupNode msgNode) {
    ExprNode replacement;
    long primaryMsgId = MsgUtils.computeMsgIdForDualFormat(msgNode.getChild(0));
    if (msgNode.numChildren() == 1) {
        // easy peasy
        replacement = createMsgIdNode(primaryMsgId, fn.getSourceLocation());
    } else {
        long fallbackMsgId = MsgUtils.computeMsgIdForDualFormat(msgNode.getChild(1));
        ConditionalOpNode condOpNode = new ConditionalOpNode(fn.getSourceLocation());
        FunctionNode isPrimaryMsgInUse = new FunctionNode(BuiltinFunction.IS_PRIMARY_MSG_IN_USE, fn.getSourceLocation());
        // We add the varRef, and the 2 message ids to the funcitonnode as arguments so they are
        // trivial to access in the backends.  This is a little hacky however since we never generate
        // code for these things.
        // We could formalize the hack by providing a way to stash arbitrary data in the FunctionNode
        // and then just pack this up in a non-AST datastructure.
        isPrimaryMsgInUse.addChild(fn.getChild(0));
        isPrimaryMsgInUse.addChild(new IntegerNode(primaryMsgId, fn.getSourceLocation()));
        isPrimaryMsgInUse.addChild(new IntegerNode(fallbackMsgId, fn.getSourceLocation()));
        condOpNode.addChild(isPrimaryMsgInUse);
        condOpNode.addChild(createMsgIdNode(primaryMsgId, fn.getSourceLocation()));
        condOpNode.addChild(createMsgIdNode(fallbackMsgId, fn.getSourceLocation()));
        replacement = condOpNode;
    }
    fn.getParent().replaceChild(fn, replacement);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ConditionalOpNode(com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode)

Example 2 with ConditionalOpNode

use of com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode in project closure-templates by google.

the class AbstractOperatorNodeTest method testToSourceString2.

@Test
public void testToSourceString2() {
    // Test expression: not $x ? $x != $x : $x * $x
    // 
    // The expression tree looks like this:
    // [ConditionalOpNode] n0
    // [NotOpNode] n1
    // [VarRefNode] $x
    // [NotEqualOpNode] n2
    // [VarRefNode] $x
    // [VarRefNode] $x
    // [TimesOpNode] n3
    // [VarRefNode] $x
    // [VarRefNode] $x
    // Root n0.
    ConditionalOpNode n0 = new ConditionalOpNode(X);
    // Children of n0.
    NotOpNode n1 = new NotOpNode(X);
    NotEqualOpNode n2 = new NotEqualOpNode(X);
    TimesOpNode n3 = new TimesOpNode(X);
    n0.addChild(n1);
    n0.addChild(n2);
    n0.addChild(n3);
    // Child of n1.
    n1.addChild(x);
    // Children of n2.
    n2.addChild(x.copy(new CopyState()));
    n2.addChild(x.copy(new CopyState()));
    // Children of n3.
    n3.addChild(x.copy(new CopyState()));
    n3.addChild(x.copy(new CopyState()));
    assertThat(n0.toSourceString()).isEqualTo("not $x ? $x != $x : $x * $x");
}
Also used : ConditionalOpNode(com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode) TimesOpNode(com.google.template.soy.exprtree.OperatorNodes.TimesOpNode) NotOpNode(com.google.template.soy.exprtree.OperatorNodes.NotOpNode) CopyState(com.google.template.soy.basetree.CopyState) NotEqualOpNode(com.google.template.soy.exprtree.OperatorNodes.NotEqualOpNode) Test(org.junit.Test)

Example 3 with ConditionalOpNode

use of com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode in project closure-templates by google.

the class ParseExpressionTest method testParseOperators.

@Test
public void testParseOperators() throws Exception {
    ExprNode expr = assertThatExpression("-11").isValidExpression();
    IntegerNode negInt = (IntegerNode) expr;
    assertThat(negInt.getValue()).isEqualTo(-11);
    expr = assertThatExpression("not false").isValidExpression();
    NotOpNode notOp = (NotOpNode) expr;
    assertThat(((BooleanNode) notOp.getChild(0)).getValue()).isFalse();
    expr = assertThatExpression("90 -14.75").isValidExpression();
    MinusOpNode minusOp = (MinusOpNode) expr;
    assertThat(((IntegerNode) minusOp.getChild(0)).getValue()).isEqualTo(90);
    assertThat(((FloatNode) minusOp.getChild(1)).getValue()).isEqualTo(14.75);
    expr = assertThatExpression("$a or true").isValidExpression();
    OrOpNode orOp = (OrOpNode) expr;
    assertThat(orOp.getChild(0).toSourceString()).isEqualTo("$a");
    assertThat(((BooleanNode) orOp.getChild(1)).getValue()).isTrue();
    expr = assertThatExpression("$a ?: $b ?: $c").isValidExpression();
    NullCoalescingOpNode nullCoalOp0 = (NullCoalescingOpNode) expr;
    assertThat(nullCoalOp0.getChild(0).toSourceString()).isEqualTo("$a");
    NullCoalescingOpNode nullCoalOp1 = (NullCoalescingOpNode) nullCoalOp0.getChild(1);
    assertThat(nullCoalOp1.getChild(0).toSourceString()).isEqualTo("$b");
    assertThat(nullCoalOp1.getChild(1).toSourceString()).isEqualTo("$c");
    expr = assertThatExpression("$a?:$b==null?0*1:0x1").isValidExpression();
    NullCoalescingOpNode nullCoalOp = (NullCoalescingOpNode) expr;
    assertThat(nullCoalOp.getChild(0).toSourceString()).isEqualTo("$a");
    ConditionalOpNode condOp = (ConditionalOpNode) nullCoalOp.getChild(1);
    assertThat(condOp.getChild(0)).isInstanceOf(EqualOpNode.class);
    assertThat(condOp.getChild(1)).isInstanceOf(TimesOpNode.class);
    assertThat(condOp.getChild(2)).isInstanceOf(IntegerNode.class);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ConditionalOpNode(com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode) NullCoalescingOpNode(com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) MinusOpNode(com.google.template.soy.exprtree.OperatorNodes.MinusOpNode) OrOpNode(com.google.template.soy.exprtree.OperatorNodes.OrOpNode) FloatNode(com.google.template.soy.exprtree.FloatNode) NotOpNode(com.google.template.soy.exprtree.OperatorNodes.NotOpNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode) Test(org.junit.Test)

Aggregations

ConditionalOpNode (com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode)3 ExprNode (com.google.template.soy.exprtree.ExprNode)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 NotOpNode (com.google.template.soy.exprtree.OperatorNodes.NotOpNode)2 Test (org.junit.Test)2 CopyState (com.google.template.soy.basetree.CopyState)1 BooleanNode (com.google.template.soy.exprtree.BooleanNode)1 FloatNode (com.google.template.soy.exprtree.FloatNode)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 MinusOpNode (com.google.template.soy.exprtree.OperatorNodes.MinusOpNode)1 NotEqualOpNode (com.google.template.soy.exprtree.OperatorNodes.NotEqualOpNode)1 NullCoalescingOpNode (com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode)1 OrOpNode (com.google.template.soy.exprtree.OperatorNodes.OrOpNode)1 TimesOpNode (com.google.template.soy.exprtree.OperatorNodes.TimesOpNode)1