Java Code Examples for com.google.javascript.rhino.Token#name()

The following examples show how to use com.google.javascript.rhino.Token#name() . 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: FunctionRewriter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getGetPropertyName(node);
  if (propName != null) {
    if (!propName.isString()) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName, node);
  } else {
    return node;
  }
}
 
Example 2
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (!callNode.isNew()) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.isName() &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 3
Source File: Closure_55_FunctionRewriter_s.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getGetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 4
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (callNode.getType() != Token.NEW) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.getType() == Token.NAME &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 5
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (callNode.getType() != Token.NEW) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.getType() == Token.NAME &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 6
Source File: Closure_55_FunctionRewriter_s.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getSetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 7
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
static boolean constructorCallHasSideEffects(
    Node callNode, AbstractCompiler compiler) {
  if (callNode.getType() != Token.NEW) {
    throw new IllegalStateException(
        "Expected NEW node, got " + Token.name(callNode.getType()));
  }

  if (callNode.isNoSideEffectsCall()) {
    return false;
  }

  Node nameNode = callNode.getFirstChild();
  if (nameNode.getType() == Token.NAME &&
      CONSTRUCTORS_WITHOUT_SIDE_EFFECTS.contains(nameNode.getString())) {
    return false;
  }

  return true;
}
 
Example 8
Source File: Nopol2017_0047_t.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getGetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 9
Source File: Nopol2017_0047_s.java    From coming with MIT License 6 votes vote down vote up
@Override
public Node reduce(Node node) {
  if (!isReduceableFunctionExpression(node)) {
    return node;
  }

  Node propName = getSetPropertyName(node);
  if (propName != null) {
    if (propName.getType() != Token.STRING) {
      throw new IllegalStateException(
          "Expected STRING, got " + Token.name(propName.getType()));
    }

    return buildCallNode(FACTORY_METHOD_NAME, propName,
                         node.getLineno(), node.getCharno());
  } else {
    return node;
  }
}
 
Example 10
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation or fails.
 *
 * @param operator the operator's token value to convert
 * @return the string representation
 * @throws Error if the token value is not an operator
 */
static String opToStrNoFail(int operator) {
  String res = opToStr(operator);
  if (res == null) {
    throw new Error("Unknown op " + operator + ": " +
                    Token.name(operator));
  }
  return res;
}
 
Example 11
Source File: Cardumen_00149_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation or fails.
 *
 * @param operator the operator's token value to convert
 * @return the string representation
 * @throws Error if the token value is not an operator
 */
static String opToStrNoFail(int operator) {
  String res = opToStr(operator);
  if (res == null) {
    throw new Error("Unknown op " + operator + ": " +
                    Token.name(operator));
  }
  return res;
}
 
Example 12
Source File: Cardumen_0087_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation or fails.
 *
 * @param operator the operator's token value to convert
 * @return the string representation
 * @throws Error if the token value is not an operator
 */
static String opToStrNoFail(int operator) {
  String res = opToStr(operator);
  if (res == null) {
    throw new Error("Unknown op " + operator + ": " +
                    Token.name(operator));
  }
  return res;
}
 
Example 13
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation or fails.
 *
 * @param operator the operator's token value to convert
 * @return the string representation
 * @throws Error if the token value is not an operator
 */
static String opToStrNoFail(int operator) {
  String res = opToStr(operator);
  if (res == null) {
    throw new Error("Unknown op " + operator + ": " +
                    Token.name(operator));
  }
  return res;
}
 
Example 14
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 15
Source File: Cardumen_0092_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 16
Source File: Cardumen_00151_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 17
Source File: Closure_40_NameAnalyzer_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 18
Source File: JsMessageVisitor.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns human-readable name of the given node's type.
 */
private static String getReadableTokenName(Node node) {
  return Token.name(node.getType());
}
 
Example 19
Source File: Cardumen_00149_s.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 20
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);
}