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

The following examples show how to use com.google.javascript.rhino.Token#LP . 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_61_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether this node is used as an L-value. Notice that sometimes
 * names are used as both L-values and R-values.
 *
 * We treat "var x;" as a pseudo-L-value, which kind of makes sense if you
 * treat it as "assignment to 'undefined' at the top of the scope". But if
 * we're honest with ourselves, it doesn't make sense, and we only do this
 * because it makes sense to treat this as synactically similar to
 * "var x = 0;".
 *
 * @param node The node
 * @return True if n is an L-value.
 */
static boolean isLValue(Node node) {
  int nType = node.getType();
  Preconditions.checkArgument(nType == Token.NAME || nType == Token.GETPROP ||
      nType == Token.GETELEM);
  Node parent = node.getParent();
  return (NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == node)
      || (NodeUtil.isForIn(parent) && parent.getFirstChild() == node)
      || NodeUtil.isVar(parent)
      || (parent.getType() == Token.FUNCTION &&
          parent.getFirstChild() == node)
      || parent.getType() == Token.DEC
      || parent.getType() == Token.INC
      || parent.getType() == Token.LP
      || parent.getType() == Token.CATCH;
}
 
Example 2
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 3
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 4
Source File: Closure_70_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visit a node in a local scope, and add any local variables or catch
 * parameters into the local symbol table.
 *
 * @param t The node traversal.
 * @param n The node being visited.
 * @param parent The parent of n
 */
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  if (n == scope.getRootNode()) return;

  if (n.getType() == Token.LP && parent == scope.getRootNode()) {
    handleFunctionInputs(parent);
    return;
  }

  super.visit(t, n, parent);
}
 
Example 5
Source File: ScopedAliases.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void findAliases(NodeTraversal t) {
  Scope scope = t.getScope();
  for (Var v : scope.getVarIterable()) {
    Node n = v.getNode();
    int type = n.getType();
    Node parent = n.getParent();
    if (parent.isVar() &&
        n.hasChildren() && n.getFirstChild().isQualifiedName()) {
      String name = n.getString();
      Var aliasVar = scope.getVar(name);
      aliases.put(name, aliasVar);

      String qualifiedName =
          aliasVar.getInitialValue().getQualifiedName();
      transformation.addAlias(name, qualifiedName);

      int rootIndex = qualifiedName.indexOf(".");
      if (rootIndex != -1) {
        String qNameRoot = qualifiedName.substring(0, rootIndex);
        if (!aliases.containsKey(qNameRoot)) {
          forbiddenLocals.add(qNameRoot);
        }
      }
    } else if (v.isBleedingFunction()) {
      // Bleeding functions already get a BAD_PARAMETERS error, so just
      // do nothing.
    } else if (parent.getType() == Token.LP) {
      // Parameters of the scope function also get a BAD_PARAMETERS
      // error.
    } else {
      // TODO(robbyw): Support using locals for private variables.
      report(t, n, GOOG_SCOPE_NON_ALIAS_LOCAL, n.getString());
    }
  }
}
 
Example 6
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visit a node in a local scope, and add any local variables or catch
 * parameters into the local symbol table.
 *
 * @param t The node traversal.
 * @param n The node being visited.
 * @param parent The parent of n
 */
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  if (n == scope.getRootNode()) return;

  if (n.getType() == Token.LP && parent == scope.getRootNode()) {
    handleFunctionInputs(parent);
    return;
  }

  super.visit(t, n, parent);
}
 
Example 7
Source File: Closure_54_FunctionType_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates an instance for a function that is an interface. */
private FunctionType(JSTypeRegistry registry, String name, Node source) {
  super(registry, name,
      registry.getNativeObjectType(JSTypeNative.FUNCTION_INSTANCE_TYPE));
  Preconditions.checkArgument(source == null ||
      Token.FUNCTION == source.getType());
  Preconditions.checkArgument(name != null);
  this.source = source;
  this.call = new ArrowType(registry, new Node(Token.LP), null);
  this.kind = Kind.INTERFACE;
  this.typeOfThis = new InstanceObjectType(registry, this);
}
 
Example 8
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 9
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 10
Source File: Closure_90_FunctionTypeBuilder_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Infer the parameter types from the doc info alone.
 */
FunctionTypeBuilder inferParameterTypes(JSDocInfo info) {
  // Create a fake args parent.
  Node lp = new Node(Token.LP);
  for (String name : info.getParameterNames()) {
    lp.addChildToBack(Node.newString(Token.NAME, name));
  }

  return inferParameterTypes(lp, info);
}
 
Example 11
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.LP ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 12
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.LP ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 13
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visit a node in a local scope, and add any local variables or catch
 * parameters into the local symbol table.
 *
 * @param t The node traversal.
 * @param n The node being visited.
 * @param parent The parent of n
 */
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  if (n == scope.getRootNode()) return;

  if (n.getType() == Token.LP && parent == scope.getRootNode()) {
    handleFunctionInputs(parent);
    return;
  }

  super.visit(t, n, parent);
}
 
Example 14
Source File: Nopol2017_0051_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.LP ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 15
Source File: Closure_75_NodeUtil_t.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.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 16
Source File: Closure_86_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.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 17
Source File: Closure_110_ScopedAliases_t.java    From coming with MIT License 4 votes vote down vote up
private void findAliases(NodeTraversal t) {
  Scope scope = t.getScope();
  for (Var v : scope.getVarIterable()) {
    Node n = v.getNode();
    Node parent = n.getParent();
    boolean isVar = parent.isVar();
    boolean isFunctionDecl = NodeUtil.isFunctionDeclaration(parent);
    if (isVar && n.getFirstChild() != null && n.getFirstChild().isQualifiedName()) {
      recordAlias(v);
    } else if (v.isBleedingFunction()) {
      // Bleeding functions already get a BAD_PARAMETERS error, so just
      // do nothing.
    } else if (parent.getType() == Token.LP) {
      // Parameters of the scope function also get a BAD_PARAMETERS
      // error.
    } else if (isVar || isFunctionDecl) {
      boolean isHoisted = NodeUtil.isHoistedFunctionDeclaration(parent);
      Node grandparent = parent.getParent();
      Node value = v.getInitialValue() != null ?
          v.getInitialValue() :
          null;
      Node varNode = null;

      String name = n.getString();
      int nameCount = scopedAliasNames.count(name);
      scopedAliasNames.add(name);
      String globalName =
          "$jscomp.scope." + name + (nameCount == 0 ? "" : ("$" + nameCount));

      compiler.ensureLibraryInjected("base");

      // First, we need to free up the function expression (EXPR)
      // to be used in another expression.
      if (isFunctionDecl) {
        // Replace "function NAME() { ... }" with "var NAME;".
        Node existingName = v.getNameNode();

        // We can't keep the local name on the function expression,
        // because IE is buggy and will leak the name into the global
        // scope. This is covered in more detail here:
        // http://wiki.ecmascript.org/lib/exe/fetch.php?id=resources:resources&cache=cache&media=resources:jscriptdeviationsfromes3.pdf
        //
        // This will only cause problems if this is a hoisted, recursive
        // function, and the programmer is using the hoisting.
        Node newName = IR.name("").useSourceInfoFrom(existingName);
        value.replaceChild(existingName, newName);

        varNode = IR.var(existingName).useSourceInfoFrom(existingName);
        grandparent.replaceChild(parent, varNode);
      } else {
        if (value != null) {
          // If this is a VAR, we can just detach the expression and
          // the tree will still be valid.
          value.detachFromParent();
        }
        varNode = parent;
      }

      // Add $jscomp.scope.name = EXPR;
      // Make sure we copy over all the jsdoc and debug info.
      if (value != null || v.getJSDocInfo() != null) {
        Node newDecl = NodeUtil.newQualifiedNameNodeDeclaration(
            compiler.getCodingConvention(),
            globalName,
            value,
            v.getJSDocInfo())
            .useSourceInfoIfMissingFromForTree(n);
        NodeUtil.setDebugInformation(
            newDecl.getFirstChild().getFirstChild(), n, name);

        if (isHoisted) {
          grandparent.addChildToFront(newDecl);
        } else {
          grandparent.addChildBefore(newDecl, varNode);
        }
      }

      // Rewrite "var name = EXPR;" to "var name = $jscomp.scope.name;"
      v.getNameNode().addChildToFront(
          NodeUtil.newQualifiedNameNode(
              compiler.getCodingConvention(), globalName, n, name));

      recordAlias(v);
    } else {
      // Do not other kinds of local symbols, like catch params.
      report(t, n, GOOG_SCOPE_NON_ALIAS_LOCAL, n.getString());
    }
  }
}
 
Example 18
Source File: Closure_80_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.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 19
Source File: Closure_75_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.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 20
Source File: Closure_80_NodeUtil_t.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.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}