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

The following examples show how to use com.google.javascript.rhino.Token#DO . 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: Closure_14_ControlFlowAnalysis_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Computes the destination node of n when we want to fallthrough into the
 * subtree of n. We don't always create a CFG edge into n itself because of
 * DOs and FORs.
 */
static Node computeFallThrough(Node n) {
  switch (n.getType()) {
    case Token.DO:
      return computeFallThrough(n.getFirstChild());
    case Token.FOR:
      if (NodeUtil.isForIn(n)) {
        return n.getFirstChild().getNext();
      }
      return computeFallThrough(n.getFirstChild());
    case Token.LABEL:
      return computeFallThrough(n.getLastChild());
    default:
      return n;
  }
}
 
Example 2
Source File: Cardumen_00200_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: NodeUtil.java    From astor with GNU General Public License v2.0 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 4
Source File: NodeUtil.java    From astor 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 5
Source File: Closure_61_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 6
Source File: Closure_103_ControlFlowAnalysis_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given node can be terminated with a BREAK node.
 */
static boolean isBreakStructure(Node n, boolean labeled) {
  switch (n.getType()) {
    case Token.FOR:
    case Token.DO:
    case Token.WHILE:
    case Token.SWITCH:
      return true;
    case Token.BLOCK:
    case Token.IF:
    case Token.TRY:
      return labeled;
    default:
      return false;
  }
}
 
Example 7
Source File: ControlFlowAnalysis.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the destination node of n when we want to fallthrough into the
 * subtree of n. We don't always create a CFG edge into n itself because of
 * DOs and FORs.
 */
static Node computeFallThrough(Node n) {
  switch (n.getType()) {
    case Token.DO:
      return computeFallThrough(n.getFirstChild());
    case Token.FOR:
      if (NodeUtil.isForIn(n)) {
        return n.getFirstChild().getNext();
      }
      return computeFallThrough(n.getFirstChild());
    case Token.LABEL:
      return computeFallThrough(n.getLastChild());
    default:
      return n;
  }
}
 
Example 8
Source File: Closure_40_NameAnalyzer_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determine if the parent reads the value of a child expression
 * directly.  This is true children used in predicates, RETURN
 * statements and, rhs of variable declarations and assignments.
 *
 * In the case of:
 * if (a) b else c
 *
 * This method returns true for "a", and false for "b" and "c": the
 * IF expression does something special based on "a"'s value.  "b"
 * and "c" are effectivelly outputs.  Same logic applies to FOR,
 * WHILE and DO loop predicates.  AND/OR/HOOK expressions are
 * syntactic sugar for IF statements; therefore this method returns
 * true for the predicate and false otherwise.
 */
private boolean valueConsumedByParent(Node n, Node parent) {
  if (NodeUtil.isAssignmentOp(parent)) {
    return parent.getLastChild() == n;
  }

  switch (parent.getType()) {
    case Token.NAME:
    case Token.RETURN:
      return true;
    case Token.AND:
    case Token.OR:
    case Token.HOOK:
      return parent.getFirstChild() == n;
    case Token.FOR:
      return parent.getFirstChild().getNext() == n;
    case Token.IF:
    case Token.WHILE:
      return parent.getFirstChild() == n;
    case Token.DO:
      return parent.getLastChild() == n;
    default:
      return false;
  }
}
 
Example 9
Source File: Closure_86_NodeUtil_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 10
Source File: Closure_10_NodeUtil_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: Closure_34_CodePrinter_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the a line break should be added after the specified
 * BLOCK.
 */
@Override
boolean breakAfterBlockFor(Node n,  boolean isStatementContext) {
  Preconditions.checkState(n.isBlock());
  Node parent = n.getParent();
  if (parent != null) {
    int type = parent.getType();
    switch (type) {
      case Token.DO:
        // Don't break before 'while' in DO-WHILE statements.
        return false;
      case Token.FUNCTION:
        // FUNCTIONs are handled separately, don't break here.
        return false;
      case Token.TRY:
        // Don't break before catch
        return n != parent.getFirstChild();
      case Token.CATCH:
        // Don't break before finally
        return !NodeUtil.hasFinally(getTryForCatch(parent));
      case Token.IF:
        // Don't break before else
        return n == parent.getLastChild();
    }
  }
  return true;
}
 
Example 12
Source File: Closure_66_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 13
Source File: Closure_76_DeadAssignmentsElimination_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t,
    ControlFlowGraph<Node> cfg) {
  Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();

  for (DiGraphNode<Node, Branch> cfgNode : nodes) {
    FlowState<LiveVariableLattice> state =
        cfgNode.getAnnotation();
    Node n = cfgNode.getValue();
    if (n == null) {
      continue;
    }
    switch (n.getType()) {
      case Token.IF:
      case Token.WHILE:
      case Token.DO:
        tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
        continue;
      case Token.FOR:
        if (!NodeUtil.isForIn(n)) {
          tryRemoveAssignment(
              t, NodeUtil.getConditionExpression(n), state);
        }
        continue;
      case Token.SWITCH:
      case Token.CASE:
      case Token.RETURN:
        if (n.hasChildren()) {
          tryRemoveAssignment(t, n.getFirstChild(), state);
        }
        continue;
      // TODO(user): case Token.VAR: Remove var a=1;a=2;.....
    }

    tryRemoveAssignment(t, n, state);
  }
}
 
Example 14
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 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 15
Source File: Closure_86_NodeUtil_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 16
Source File: Closure_85_UnreachableCodeElimination_s.java    From coming with MIT License 5 votes vote down vote up
private void removeDeadExprStatementSafely(Node n) {
  Node parent = n.getParent();
  if (n.getType() == Token.EMPTY ||
      (n.getType() == Token.BLOCK && !n.hasChildren())) {
    // Not always trivial to remove, let FoldContants work its magic later.
    return;
  }

  switch (n.getType()) {
    // Removing an unreachable DO node is messy because it means we still have
    // to execute one iteration. If the DO's body has breaks in the middle, it
    // can get even more trickier and code size might actually increase.
    case Token.DO:
      return;

    case Token.BLOCK:
      // BLOCKs are used in several ways including wrapping CATCH blocks in TRYs
      if (parent.getType() == Token.TRY) {
        if (NodeUtil.isTryCatchNodeContainer(n)) {
          return;
        }
      }
      break;

    case Token.CATCH:
      Node tryNode = parent.getParent();
      NodeUtil.maybeAddFinally(tryNode);
      break;
  }

  NodeUtil.redeclareVarsInsideBranch(n);
  compiler.reportCodeChange();
  if (logger.isLoggable(Level.FINE)) {
    logger.fine("Removing " + n.toString());
  }
  NodeUtil.removeChild(n.getParent(), n);
}
 
Example 17
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 18
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 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 19
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 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 20
Source File: Closure_30_MustBeReachingVariableDef_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param n The node in question.
 * @param cfgNode The node to add
 * @param conditional true if the definition is not always executed.
 */
private void computeMustDef(
    Node n, Node cfgNode, MustDef output, boolean conditional) {
  switch (n.getType()) {

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

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

    case Token.FOR:
      if (!NodeUtil.isForIn(n)) {
        computeMustDef(
            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()) {
          addToDefIfLocal(lhs.getString(), cfgNode, rhs, output);
        }
      }
      return;

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

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

    case Token.VAR:
      for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
        if (c.hasChildren()) {
          computeMustDef(c.getFirstChild(), cfgNode, output, conditional);
          addToDefIfLocal(c.getString(), conditional ? null : cfgNode,
              c.getFirstChild(), output);
        }
      }
      return;

    default:
      if (NodeUtil.isAssignmentOp(n)) {
        if (n.getFirstChild().isName()) {
          Node name = n.getFirstChild();
          computeMustDef(name.getNext(), cfgNode, output, conditional);
          addToDefIfLocal(name.getString(), conditional ? null : cfgNode,
            n.getLastChild(), output);
          return;
        } else if (NodeUtil.isGet(n.getFirstChild())) {
          // Treat all assignments to arguments as redefining the
          // parameters itself.
          Node obj = n.getFirstChild().getFirstChild();
          if (obj.isName() && "arguments".equals(obj.getString())) {
            // TODO(user): More accuracy can be introduced
            // ie: We know exactly what arguments[x] is if x is a constant
            // number.
            escapeParameters(output);
          }
        }
      }

      if (n.isName() && "arguments".equals(n.getString())) {
        escapeParameters(output);
      }

      // DEC and INC actually defines the variable.
      if (n.isDec() || n.isInc()) {
        Node target = n.getFirstChild();
        if (target.isName()) {
          addToDefIfLocal(target.getString(),
              conditional ? null : cfgNode, null, output);
          return;
        }
      }

      for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
        computeMustDef(c, cfgNode, output, conditional);
      }
  }
}