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

The following examples show how to use com.google.javascript.rhino.Token#LABEL . 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_60_NodeUtil_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:
      return true;
    default:
      Preconditions.checkState(isControlStructure(parent));
      return false;
  }
}
 
Example 2
Source File: Closure_102_Normalize_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Do normalizations that introduce new siblings or parents.
 */
private void doStatementNormalizations(NodeTraversal t, Node n, Node parent) {
  if (n.getType() == Token.LABEL) {
    normalizeLabels(n);
  }

  // Only inspect the children of SCRIPTs, BLOCKs and LABELs, as all these
  // are the only legal place for VARs and FOR statements.
  if (NodeUtil.isStatementBlock(n) || n.getType() == Token.LABEL) {
    extractForInitializer(n, null, null);
  }

  // Only inspect the children of SCRIPTs, BLOCKs, as all these
  // are the only legal place for VARs.
  if (NodeUtil.isStatementBlock(n)) {
    splitVarDeclarations(n);
  }

  if (n.getType() == Token.FUNCTION) {
    moveNamedFunctions(n.getLastChild());
  }
}
 
Example 3
Source File: Closure_77_CodeGenerator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return Whether the Node is a DO or FUNCTION (with or without
 * labels).
 */
private boolean isOneExactlyFunctionOrDo(Node n) {
  if (n.getType() == Token.LABEL) {
    Node labeledStatement = n.getLastChild();
    if (labeledStatement.getType() != Token.BLOCK) {
      return isOneExactlyFunctionOrDo(labeledStatement);
    } else {
      // For labels with block children, we need to ensure that a
      // labeled FUNCTION or DO isn't generated when extraneous BLOCKs
      // are skipped.
      if (getNonEmptyChildCount(n, 2) == 1) {
        return isOneExactlyFunctionOrDo(getFirstNonEmptyChild(n));
      } else {
        // Either a empty statement or an block with more than one child,
        // way it isn't a FUNCTION or DO.
        return false;
      }
    }
  } else {
    return (n.getType() == Token.FUNCTION || n.getType() == Token.DO);
  }
}
 
Example 4
Source File: Normalize.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Limit the number of special cases where LABELs need to be handled. Only
 * BLOCK and loops are allowed to be labeled.  Loop labels must remain in
 * place as the named continues are not allowed for labeled blocks.
 */
private void normalizeLabels(Node n) {
  Preconditions.checkArgument(n.isLabel());

  Node last = n.getLastChild();
  switch (last.getType()) {
    case Token.LABEL:
    case Token.BLOCK:
    case Token.FOR:
    case Token.WHILE:
    case Token.DO:
      return;
    default:
      Node block = IR.block();
      block.copyInformationFrom(last);
      n.replaceChild(last, block);
      block.addChildToFront(last);
      reportCodeChange("LABEL normalization");
      return;
  }
}
 
Example 5
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 6
Source File: Closure_79_Normalize_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Do normalizations that introduce new siblings or parents.
 */
private void doStatementNormalizations(
    NodeTraversal t, Node n, Node parent) {
  if (n.getType() == Token.LABEL) {
    normalizeLabels(n);
  }

  // Only inspect the children of SCRIPTs, BLOCKs and LABELs, as all these
  // are the only legal place for VARs and FOR statements.
  if (NodeUtil.isStatementBlock(n) || n.getType() == Token.LABEL) {
    extractForInitializer(n, null, null);
  }

  // Only inspect the children of SCRIPTs, BLOCKs, as all these
  // are the only legal place for VARs.
  if (NodeUtil.isStatementBlock(n)) {
    splitVarDeclarations(n);
  }

  if (n.getType() == Token.FUNCTION) {
    moveNamedFunctions(n.getLastChild());
  }
}
 
Example 7
Source File: Closure_72_RenameLabels_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * shouldTraverse is call when descending into the Node tree, so it is used
 * here to build the context for label renames.
 *
 * {@inheritDoc}
 */
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node,
    Node parent) {
  if (node.getType() == Token.LABEL) {
    // Determine the new name for this label.
    LabelNamespace current = namespaceStack.peek();
    int currentDepth = current.renameMap.size() + 1;
    String name = node.getFirstChild().getString();

    // Store the context for this label name.
    LabelInfo li = new LabelInfo(currentDepth);
    Preconditions.checkState(!current.renameMap.containsKey(name));
    current.renameMap.put(name, li);

    // Create a new name, if needed, for this depth.
    if (names.size() < currentDepth) {
      names.add(nameSupplier.get());
    }

    String newName = getNameForId(currentDepth);
    compiler.addToDebugLog("label renamed: " + name + " => " + newName);
  }

  return true;
}
 
Example 8
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 9
Source File: Closure_60_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 10
Source File: jKali_003_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 11
Source File: Closure_94_NodeUtil_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:
      return true;
    default:
      Preconditions.checkState(isControlStructure(parent));
      return false;
  }
}
 
Example 12
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return Whether the node is used as a statement.
 */
static boolean isStatement(Node n) {
  Node parent = n.getParent();
  // It is not possible to determine definitely if a node is a statement
  // or not if it is not part of the AST.  A FUNCTION node can be
  // either part of an expression or a statement.
  Preconditions.checkState(parent != null);
  switch (parent.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.LABEL:
      return true;
    default:
      return false;
  }
}
 
Example 13
Source File: jMutRepair_003_s.java    From coming with MIT License 5 votes vote down vote up
static boolean isStatementParent(Node parent) {
  // It is not possible to determine definitely if a node is a statement
  // or not if it is not part of the AST.  A FUNCTION node can be
  // either part of an expression or a statement.
  Preconditions.checkState(parent != null);
  switch (parent.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.LABEL:
      return true;
    default:
      return false;
  }
}
 
Example 14
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
static boolean isStatementParent(Node parent) {
  // It is not possible to determine definitely if a node is a statement
  // or not if it is not part of the AST.  A FUNCTION node can be
  // either part of an expression or a statement.
  Preconditions.checkState(parent != null);
  switch (parent.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.LABEL:
      return true;
    default:
      return false;
  }
}
 
Example 15
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
static boolean isStatementParent(Node parent) {
  // It is not possible to determine definitely if a node is a statement
  // or not if it is not part of the AST.  A FUNCTION node can be
  // either part of an expression or a statement.
  Preconditions.checkState(parent != null);
  switch (parent.getType()) {
    case Token.SCRIPT:
    case Token.BLOCK:
    case Token.LABEL:
      return true;
    default:
      return false;
  }
}
 
Example 16
Source File: Closure_102_Normalize_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Bring the initializers out of FOR loops.  These need to be placed
 * before any associated LABEL nodes. This needs to be done from the top
 * level label first so this is called as a pre-order callback (from
 * shouldTraverse).
 *
 * @param n The node to inspect.
 * @param before The node to insert the initializer before.
 * @param beforeParent The parent of the node before which the initializer
 *     will be inserted.
 */
private void extractForInitializer(
    Node n, Node before, Node beforeParent) {

  for (Node next, c = n.getFirstChild(); c != null; c = next) {
    next = c.getNext();
    Node insertBefore = (before == null) ? c : before;
    Node insertBeforeParent = (before == null) ? n : beforeParent;
    switch (c.getType()) {
      case Token.LABEL:
        extractForInitializer(c, insertBefore, insertBeforeParent);
        break;
      case Token.FOR:
        if (!NodeUtil.isForIn(c)
            && c.getFirstChild().getType() != Token.EMPTY) {
          Node init = c.getFirstChild();
          c.replaceChild(init, new Node(Token.EMPTY));

          Node newStatement;
          // Only VAR statements, and expressions are allowed,
          // but are handled differently.
          if (init.getType() == Token.VAR) {
            newStatement = init;
          } else {
            newStatement = NodeUtil.newExpr(init);
          }

          insertBeforeParent.addChildBefore(newStatement, insertBefore);
          reportCodeChange("FOR initializer");
        }
        break;
    }
  }
}
 
Example 17
Source File: Cardumen_00151_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Simplify a toplevel expression, while preserving program
 * behavior.
 */
private void replaceTopLevelExpressionWithRhs(Node parent, Node n) {
  // validate inputs
  switch (parent.getType()) {
    case Token.BLOCK:
    case Token.SCRIPT:
    case Token.FOR:
    case Token.LABEL:
      break;
    default:
      throw new IllegalArgumentException(
          "Unsupported parent node type in replaceWithRhs " +
          Token.name(parent.getType()));
  }

  switch (n.getType()) {
    case Token.EXPR_RESULT:
    case Token.FUNCTION:
    case Token.VAR:
      break;
    case Token.ASSIGN:
      Preconditions.checkArgument(parent.isFor(),
          "Unsupported assignment in replaceWithRhs. parent: %s", Token.name(parent.getType()));
      break;
    default:
      throw new IllegalArgumentException(
          "Unsupported node type in replaceWithRhs " +
          Token.name(n.getType()));
  }

  // gather replacements
  List<Node> replacements = Lists.newArrayList();
  for (Node rhs : getRhsSubexpressions(n)) {
    replacements.addAll(getSideEffectNodes(rhs));
  }

  if (parent.isFor()) {
    // tweak replacements array s.t. it is a single expression node.
    if (replacements.isEmpty()) {
      replacements.add(IR.empty());
    } else {
      Node expr = collapseReplacements(replacements);
      replacements.clear();
      replacements.add(expr);
    }
  }

  changeProxy.replaceWith(parent, n, replacements);
}
 
Example 18
Source File: Closure_103_ControlFlowAnalysis_s.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;
}
 
Example 19
Source File: Cardumen_00202_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Simplify a toplevel expression, while preserving program
 * behavior.
 */
private void replaceTopLevelExpressionWithRhs(Node parent, Node n) {
  // validate inputs
  switch (parent.getType()) {
    case Token.BLOCK:
    case Token.SCRIPT:
    case Token.FOR:
    case Token.LABEL:
      break;
    default:
      throw new IllegalArgumentException(
          "Unsupported parent node type in replaceWithRhs " +
          Token.name(parent.getType()));
  }

  switch (n.getType()) {
    case Token.EXPR_RESULT:
    case Token.FUNCTION:
    case Token.VAR:
      break;
    case Token.ASSIGN:
      Preconditions.checkArgument(parent.isFor(),
          "Unsupported assignment in replaceWithRhs. parent: %s", Token.name(parent.getType()));
      break;
    default:
      throw new IllegalArgumentException(
          "Unsupported node type in replaceWithRhs " +
          Token.name(n.getType()));
  }

  // gather replacements
  List<Node> replacements = Lists.newArrayList();
  for (Node rhs : getRhsSubexpressions(n)) {
    replacements.addAll(getSideEffectNodes(rhs));
  }

  if (parent.isFor()) {
    // tweak replacements array s.t. it is a single expression node.
    if (replacements.isEmpty()) {
      replacements.add(IR.empty());
    } else {
      Node expr = collapseReplacements(replacements);
      replacements.clear();
      replacements.add(expr);
    }
  }

  changeProxy.replaceWith(parent, n, replacements);
}
 
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;
}