Java Code Examples for com.google.javascript.rhino.Token#WHILE

The following examples show how to use com.google.javascript.rhino.Token#WHILE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cardumen_0087_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
 */
static boolean isControlStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
    case Token.WITH:
    case Token.IF:
    case Token.LABEL:
    case Token.TRY:
    case Token.CATCH:
    case Token.SWITCH:
    case Token.CASE:
    case Token.DEFAULT_CASE:
      return true;
    default:
      return false;
  }
}
 
Example 2
Source File: Cardumen_0014_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
 */
static boolean isControlStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
    case Token.WITH:
    case Token.IF:
    case Token.LABEL:
    case Token.TRY:
    case Token.CATCH:
    case Token.SWITCH:
    case Token.CASE:
    case Token.DEFAULT_CASE:
      return true;
    default:
      return false;
  }
}
 
Example 3
Source File: Cardumen_00149_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is code node for FOR, DO,
 * WHILE, WITH, or IF node.
 */
static boolean isControlStructureCodeBlock(Node parent, Node n) {
  switch (parent.getType()) {
    case Token.FOR:
    case Token.WHILE:
    case Token.LABEL:
    case Token.WITH:
      return parent.getLastChild() == n;
    case Token.DO:
      return parent.getFirstChild() == n;
    case Token.IF:
      return parent.getFirstChild() != n;
    case Token.TRY:
      return parent.getFirstChild() == n || parent.getLastChild() == n;
    case Token.CATCH:
      return parent.getLastChild() == n;
    case Token.SWITCH:
    case Token.CASE:
      return parent.getFirstChild() != n;
    case Token.DEFAULT_CASE:
      return true;
    default:
      Preconditions.checkState(isControlStructure(parent));
      return false;
  }
}
 
Example 4
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
 */
static boolean isControlStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
    case Token.WITH:
    case Token.IF:
    case Token.LABEL:
    case Token.TRY:
    case Token.CATCH:
    case Token.SWITCH:
    case Token.CASE:
    case Token.DEFAULT:
      return true;
    default:
      return false;
  }
}
 
Example 5
Source File: Cardumen_00149_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
 */
static boolean isControlStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
    case Token.WITH:
    case Token.IF:
    case Token.LABEL:
    case Token.TRY:
    case Token.CATCH:
    case Token.SWITCH:
    case Token.CASE:
    case Token.DEFAULT_CASE:
      return true;
    default:
      return false;
  }
}
 
Example 6
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets the condition of an ON_TRUE / ON_FALSE CFG edge.
 * @param n a node with an outgoing conditional CFG edge
 * @return the condition node or null if the condition is not obviously a node
 */
static Node getConditionExpression(Node n) {
  switch (n.getType()) {
    case Token.IF:
    case Token.WHILE:
      return n.getFirstChild();
    case Token.DO:
      return n.getLastChild();
    case Token.FOR:
      switch (n.getChildCount()) {
        case 3:
          return null;
        case 4:
          return n.getFirstChild().getNext();
      }
      throw new IllegalArgumentException("malformed 'for' statement " + n);
    case Token.CASE:
      return null;
  }
  throw new IllegalArgumentException(n + " does not have a condition.");
}
 
Example 7
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the condition of an ON_TRUE / ON_FALSE CFG edge.
 * @param n a node with an outgoing conditional CFG edge
 * @return the condition node or null if the condition is not obviously a node
 */
static Node getConditionExpression(Node n) {
  switch (n.getType()) {
    case Token.IF:
    case Token.WHILE:
      return n.getFirstChild();
    case Token.DO:
      return n.getLastChild();
    case Token.FOR:
      switch (n.getChildCount()) {
        case 3:
          return null;
        case 4:
          return n.getFirstChild().getNext();
      }
      throw new IllegalArgumentException("malformed 'for' statement " + n);
    case Token.CASE:
      return null;
  }
  throw new IllegalArgumentException(n + " does not have a condition.");
}
 
Example 8
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is code node for FOR, DO,
 * WHILE, WITH, or IF node.
 */
static boolean isControlStructureCodeBlock(Node parent, Node n) {
  switch (parent.getType()) {
    case Token.FOR:
    case Token.WHILE:
    case Token.LABEL:
    case Token.WITH:
      return parent.getLastChild() == n;
    case Token.DO:
      return parent.getFirstChild() == n;
    case Token.IF:
      return parent.getFirstChild() != n;
    case Token.TRY:
      return parent.getFirstChild() == n || parent.getLastChild() == n;
    case Token.CATCH:
      return parent.getLastChild() == n;
    case Token.SWITCH:
    case Token.CASE:
      return parent.getFirstChild() != n;
    case Token.DEFAULT:
      return true;
    default:
      Preconditions.checkState(isControlStructure(parent));
      return false;
  }
}
 
Example 9
Source File: Cardumen_00149_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, WHILE, WITH, or IF node.
 */
static boolean isControlStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
    case Token.WITH:
    case Token.IF:
    case Token.LABEL:
    case Token.TRY:
    case Token.CATCH:
    case Token.SWITCH:
    case Token.CASE:
    case Token.DEFAULT_CASE:
      return true;
    default:
      return false;
  }
}
 
Example 10
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, or WHILE node.
 */
static boolean isLoopStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
      return true;
    default:
      return false;
  }
}
 
Example 11
Source File: jMutRepair_003_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether the given node is a FOR, DO, or WHILE node.
 */
static boolean isLoopStructure(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
      return true;
    default:
      return false;
  }
}
 
Example 12
Source File: Cardumen_0087_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node to inspect.
 * @return If the node, is a FOR, WHILE, or DO, it returns the node for
 * the code BLOCK, null otherwise.
 */
static Node getLoopCodeBlock(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.WHILE:
      return n.getLastChild();
    case Token.DO:
      return n.getFirstChild();
    default:
      return null;
  }
}
 
Example 13
Source File: Cardumen_00200_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node to inspect.
 * @return If the node, is a FOR, WHILE, or DO, it returns the node for
 * the code BLOCK, null otherwise.
 */
static Node getLoopCodeBlock(Node n) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.WHILE:
      return n.getLastChild();
    case Token.DO:
      return n.getFirstChild();
    default:
      return null;
  }
}
 
Example 14
Source File: Nopol2017_0051_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether this node is testing for the existence of a property.
 * If true, we will not emit warnings about a missing property.
 *
 * @param getProp The GETPROP being tested.
 */
private boolean isPropertyTest(Node getProp) {
  Node parent = getProp.getParent();
  switch (parent.getType()) {
    case Token.CALL:
      return parent.getFirstChild() != getProp &&
          compiler.getCodingConvention().isPropertyTestFunction(parent);

    case Token.IF:
    case Token.WHILE:
    case Token.DO:
    case Token.FOR:
      return NodeUtil.getConditionExpression(parent) == getProp;

    case Token.INSTANCEOF:
    case Token.TYPEOF:
      return true;

    case Token.AND:
    case Token.HOOK:
      return parent.getFirstChild() == getProp;

    case Token.NOT:
      return parent.getParent().getType() == Token.OR &&
          parent.getParent().getFirstChild() == parent;
  }
  return false;
}
 
Example 15
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether this node is testing for the existence of a property.
 * If true, we will not emit warnings about a missing property.
 *
 * @param getProp The GETPROP being tested.
 */
private boolean isPropertyTest(Node getProp) {
  Node parent = getProp.getParent();
  switch (parent.getType()) {
    case Token.CALL:
      return parent.getFirstChild() != getProp &&
          compiler.getCodingConvention().isPropertyTestFunction(parent);

    case Token.IF:
    case Token.WHILE:
    case Token.DO:
    case Token.FOR:
      return NodeUtil.getConditionExpression(parent) == getProp;

    case Token.INSTANCEOF:
    case Token.TYPEOF:
      return true;

    case Token.AND:
    case Token.HOOK:
      return parent.getFirstChild() == getProp;

    case Token.NOT:
      return parent.getParent().getType() == Token.OR &&
          parent.getParent().getFirstChild() == parent;
  }
  return false;
}
 
Example 16
Source File: Cardumen_0087_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param n The expression to check.
 * @return Whether the expression is unconditionally executed only once in the
 *     containing execution scope.
 */
static boolean isExecutedExactlyOnce(Node n) {
  inspect: do {
    Node parent = n.getParent();
    switch (parent.getType()) {
      case Token.IF:
      case Token.HOOK:
      case Token.AND:
      case Token.OR:
        if (parent.getFirstChild() != n) {
          return false;
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.FOR:
        if (NodeUtil.isForIn(parent)) {
          if (parent.getChildAtIndex(1) != n) {
            return false;
          }
        } else {
          if (parent.getFirstChild() != n) {
            return false;
          }
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.WHILE:
      case Token.DO:
        return false;
      case Token.TRY:
        // Consider all code under a try/catch to be conditionally executed.
        if (!hasFinally(parent) || parent.getLastChild() != n) {
          return false;
        }
        continue inspect;
      case Token.CASE:
      case Token.DEFAULT_CASE:
        return false;
      case Token.SCRIPT:
      case Token.FUNCTION:
        // Done, we've reached the scope root.
        break inspect;
    }
  } while ((n = n.getParent()) != null);
  return true;
}
 
Example 17
Source File: Closure_12_MaybeReachingVariableUse_s.java    From coming with MIT License 4 votes vote down vote up
private void computeMayUse(
    Node n, Node cfgNode, ReachingUses output, boolean conditional) {
  switch (n.getType()) {

    case Token.BLOCK:
    case Token.FUNCTION:
      return;

    case Token.NAME:
      addToUseIfLocal(n.getString(), cfgNode, output);
      return;

    case Token.WHILE:
    case Token.DO:
    case Token.IF:
      computeMayUse(
          NodeUtil.getConditionExpression(n), cfgNode, output, conditional);
      return;

    case Token.FOR:
      if (!NodeUtil.isForIn(n)) {
        computeMayUse(
            NodeUtil.getConditionExpression(n), cfgNode, output, conditional);
      } else {
        // for(x in y) {...}
        Node lhs = n.getFirstChild();
        Node rhs = lhs.getNext();
        if (lhs.isVar()) {
          lhs = lhs.getLastChild(); // for(var x in y) {...}
        }
        if (lhs.isName() && !conditional) {
          removeFromUseIfLocal(lhs.getString(), output);
        }
        computeMayUse(rhs, cfgNode, output, conditional);
      }
      return;

    case Token.AND:
    case Token.OR:
      computeMayUse(n.getLastChild(), cfgNode, output, true);
      computeMayUse(n.getFirstChild(), cfgNode, output, conditional);
      return;

    case Token.HOOK:
      computeMayUse(n.getLastChild(), cfgNode, output, true);
      computeMayUse(n.getFirstChild().getNext(), cfgNode, output, true);
      computeMayUse(n.getFirstChild(), cfgNode, output, conditional);
      return;

    case Token.VAR:
      Node varName = n.getFirstChild();
      Preconditions.checkState(n.hasChildren(), "AST should be normalized");

      if (varName.hasChildren()) {
        computeMayUse(varName.getFirstChild(), cfgNode, output, conditional);
        if (!conditional) {
          removeFromUseIfLocal(varName.getString(), output);
        }
      }
      return;

    default:
      if (NodeUtil.isAssignmentOp(n) && n.getFirstChild().isName()) {
        Node name = n.getFirstChild();
        if (!conditional) {
          removeFromUseIfLocal(name.getString(), output);
        }

        // In case of a += "Hello". There is a read of a.
        if (!n.isAssign()) {
          addToUseIfLocal(name.getString(), cfgNode, output);
        }

        computeMayUse(name.getNext(), cfgNode, output, conditional);
      } else {
        /*
         * We want to traverse in reverse order because we want the LAST
         * definition in the sub-tree....
         * But we have no better way to traverse in reverse other :'(
         */
        for (Node c = n.getLastChild(); c != null; c = n.getChildBefore(c)) {
          computeMayUse(c, cfgNode, output, conditional);
        }
      }
  }
}
 
Example 18
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param n The expression to check.
 * @return Whether the expression is unconditionally executed only once in the
 *     containing execution scope.
 */
static boolean isExecutedExactlyOnce(Node n) {
  inspect: do {
    Node parent = n.getParent();
    switch (parent.getType()) {
      case Token.IF:
      case Token.HOOK:
      case Token.AND:
      case Token.OR:
        if (parent.getFirstChild() != n) {
          return false;
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.FOR:
        if (NodeUtil.isForIn(parent)) {
          if (parent.getChildAtIndex(1) != n) {
            return false;
          }
        } else {
          if (parent.getFirstChild() != n) {
            return false;
          }
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.WHILE:
      case Token.DO:
        return false;
      case Token.TRY:
        // Consider all code under a try/catch to be conditionally executed.
        if (!hasFinally(parent) || parent.getLastChild() != n) {
          return false;
        }
        continue inspect;
      case Token.CASE:
      case Token.DEFAULT_CASE:
        return false;
      case Token.SCRIPT:
      case Token.FUNCTION:
        // Done, we've reached the scope root.
        break inspect;
    }
  } while ((n = n.getParent()) != null);
  return true;
}
 
Example 19
Source File: ControlFlowAnalysis.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.IF:
      handleIf(n);
      return;
    case Token.WHILE:
      handleWhile(n);
      return;
    case Token.DO:
      handleDo(n);
      return;
    case Token.FOR:
      handleFor(n);
      return;
    case Token.SWITCH:
      handleSwitch(n);
      return;
    case Token.CASE:
      handleCase(n);
      return;
    case Token.DEFAULT_CASE:
      handleDefault(n);
      return;
    case Token.BLOCK:
    case Token.SCRIPT:
      handleStmtList(n);
      return;
    case Token.FUNCTION:
      handleFunction(n);
      return;
    case Token.EXPR_RESULT:
      handleExpr(n);
      return;
    case Token.THROW:
      handleThrow(n);
      return;
    case Token.TRY:
      handleTry(n);
      return;
    case Token.CATCH:
      handleCatch(n);
      return;
    case Token.BREAK:
      handleBreak(n);
      return;
    case Token.CONTINUE:
      handleContinue(n);
      return;
    case Token.RETURN:
      handleReturn(n);
      return;
    case Token.WITH:
      handleWith(n);
      return;
    case Token.LABEL:
      return;
    default:
      handleStmt(n);
      return;
  }
}
 
Example 20
Source File: Closure_14_ControlFlowAnalysis_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public boolean shouldTraverse(
    NodeTraversal nodeTraversal, Node n, Node parent) {
  astPosition.put(n, astPositionCounter++);

  switch (n.getType()) {
    case Token.FUNCTION:
      if (shouldTraverseFunctions || n == cfg.getEntry().getValue()) {
        exceptionHandler.push(n);
        return true;
      }
      return false;
    case Token.TRY:
      exceptionHandler.push(n);
      return true;
  }

  /*
   * We are going to stop the traversal depending on what the node's parent
   * is.
   *
   * We are only interested in adding edges between nodes that change control
   * flow. The most obvious ones are loops and IF-ELSE's. A statement
   * transfers control to its next sibling.
   *
   * In case of an expression tree, there is no control flow within the tree
   * even when there are short circuited operators and conditionals. When we
   * are doing data flow analysis, we will simply synthesize lattices up the
   * expression tree by finding the meet at each expression node.
   *
   * For example: within a Token.SWITCH, the expression in question does not
   * change the control flow and need not to be considered.
   */
  if (parent != null) {
    switch (parent.getType()) {
      case Token.FOR:
        // Only traverse the body of the for loop.
        return n == parent.getLastChild();

      // Skip the conditions.
      case Token.IF:
      case Token.WHILE:
      case Token.WITH:
        return n != parent.getFirstChild();
      case Token.DO:
        return n != parent.getFirstChild().getNext();
      // Only traverse the body of the cases
      case Token.SWITCH:
      case Token.CASE:
      case Token.CATCH:
      case Token.LABEL:
        return n != parent.getFirstChild();
      case Token.FUNCTION:
        return n == parent.getFirstChild().getNext().getNext();
      case Token.CONTINUE:
      case Token.BREAK:
      case Token.EXPR_RESULT:
      case Token.VAR:
      case Token.RETURN:
      case Token.THROW:
        return false;
      case Token.TRY:
        /* Just before we are about to visit the second child of the TRY node,
         * we know that we will be visiting either the CATCH or the FINALLY.
         * In other words, we know that the post order traversal of the TRY
         * block has been finished, no more exceptions can be caught by the
         * handler at this TRY block and should be taken out of the stack.
         */
        if (n == parent.getFirstChild().getNext()) {
          Preconditions.checkState(exceptionHandler.peek() == parent);
          exceptionHandler.pop();
        }
    }
  }
  return true;
}