Package com.google.template.soy.soytree

Examples of com.google.template.soy.soytree.SwitchCaseNode


    boolean isFirstCase = true;
    for (SoyNode child : node.getChildren()) {

      if (child instanceof SwitchCaseNode) {
        SwitchCaseNode scn = (SwitchCaseNode) child;

        StringBuilder conditionExprText = new StringBuilder();
        boolean isFirstCaseValue = true;
        for (ExprNode caseExpr : scn.getExprList()) {
          JavaExpr caseJavaExpr = ttjev.exec(caseExpr);
          if (isFirstCaseValue) {
            isFirstCaseValue = false;
          } else {
            conditionExprText.append(" || ");
View Full Code Here


    SwitchNode sn = (SwitchNode) nodes.get(0);
    assertEquals("$boo", sn.getExpr().toSourceString());
    assertTrue(sn.getExpr().getChild(0) instanceof DataRefNode);
    assertEquals(4, sn.numChildren());

    SwitchCaseNode scn0 = (SwitchCaseNode) sn.getChild(0);
    assertEquals("0", scn0.getExprListText());
    assertEquals(1, scn0.getExprList().size());
    assertTrue(scn0.getExprList().get(0).getChild(0) instanceof IntegerNode);

    SwitchCaseNode scn1 = (SwitchCaseNode) sn.getChild(1);
    assertEquals("$foo.goo", scn1.getExprListText());
    assertEquals(1, scn1.getExprList().size());
    assertTrue(scn1.getExprList().get(0).getChild(0) instanceof DataRefNode);

    SwitchCaseNode scn2 = (SwitchCaseNode) sn.getChild(2);
    assertEquals("-1, 1, $moo", scn2.getExprListText());
    assertEquals(3, scn2.getExprList().size());
    assertTrue(scn2.getExprList().get(0).getChild(0) instanceof NegativeOpNode);
    assertTrue(scn2.getExprList().get(1).getChild(0) instanceof IntegerNode);
    assertTrue(scn2.getExprList().get(2).getChild(0) instanceof DataRefNode);
    assertEquals("Bluh", ((RawTextNode) scn2.getChild(0)).getRawText());

    assertEquals(
        "Bloh", ((RawTextNode) ((SwitchDefaultNode) sn.getChild(3)).getChild(0)).getRawText());

    assertEquals(
View Full Code Here

    //     and remove all children after it, if any. Can stop processing after doing this, because
    //     the new SwitchDefaultNode is now the last child.
    // (b) If the case has all constant exprs and none match: Remove the child.
    for (SoyNode child : Lists.newArrayList(node.getChildren()) /*copy*/) {
      if (child instanceof SwitchCaseNode) {
        SwitchCaseNode caseNode = (SwitchCaseNode) child;

        boolean hasMatchingConstant = false;
        boolean hasAllNonmatchingConstants = true;
        for (ExprRootNode<?> caseExpr : caseNode.getExprList()) {
          SoyData caseExprValue = getConstantOrNull(caseExpr);
          if (caseExprValue == null) {
            hasAllNonmatchingConstants = false;
          } else if (caseExprValue.equals(switchExprValue)) {
            hasMatchingConstant = true;
            hasAllNonmatchingConstants = false;
            break;
          }
        }

        if (hasMatchingConstant) {
          // ------ Has a constant expr that matches. ------
          // Remove all children after this child.
          int caseIndex = node.getChildIndex(caseNode);
          for (int i = node.numChildren() - 1; i > caseIndex; i--) {
            node.removeChild(i);
          }
          // Replace this child with a new SwitchDefaultNode.
          SwitchDefaultNode newDefaultNode = new SwitchDefaultNode(nodeIdGen.genId());
          newDefaultNode.addChildren(caseNode.getChildren());
          node.replaceChild(caseIndex, newDefaultNode);
          // Stop processing.
          break;

        } else if (hasAllNonmatchingConstants) {
View Full Code Here

    SoyData switchValue = eval(node.getExpr());

    for (SoyNode child : node.getChildren()) {

      if (child instanceof SwitchCaseNode) {
        SwitchCaseNode scn = (SwitchCaseNode) child;
        for (ExprNode caseExpr : scn.getExprList()) {
          if (switchValue.equals(eval(caseExpr))) {
            visit(scn);
            return;
          }
        }
View Full Code Here

    jsCodeBuilder.increaseIndent();

    for (SoyNode child : node.getChildren()) {

      if (child instanceof SwitchCaseNode) {
        SwitchCaseNode scn = (SwitchCaseNode) child;

        for (ExprNode caseExpr : scn.getExprList()) {
          JsExpr caseJsExpr =
              jsExprTranslator.translateToJsExpr(caseExpr, null, localVarTranslations);
          jsCodeBuilder.appendLine("case ", caseJsExpr.getText(), ":");
        }
View Full Code Here

TOP

Related Classes of com.google.template.soy.soytree.SwitchCaseNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.