本文整理汇总了Java中com.google.javascript.rhino.Token.NUMBER属性的典型用法代码示例。如果您正苦于以下问题:Java Token.NUMBER属性的具体用法?Java Token.NUMBER怎么用?Java Token.NUMBER使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类com.google.javascript.rhino.Token
的用法示例。
在下文中一共展示了Token.NUMBER属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: constructEdges
@Override
/*
* Block node points to it's first child.
*/
protected void constructEdges() {
this.left = getCFGNodeFor(node.getChildAtIndex(0));
addEdge(left, "numerator", Assumption.NONE);
this.right = getCFGNodeFor(node.getChildAtIndex(1));
left.addEdgeToLeaves(right, "denominator", Assumption.NONE);
// Then, after evaluating the left and right, this division either succeeds and goes to the
// end node, or it's a zero and goes to the end of the function.
right.addEdge(end, "divide", Assumption.NON_ZERO);
// We only add an edge for divide by zero if it's not a number.
if(right.getASTNode().getType() != Token.NUMBER)
right.addEdge(getFunction().getExit(), "divide by zero", Assumption.DIVIDE_BY_ZERO);
}
示例2: isImmutableValue
/**
* Returns true if this is an immutable value.
*/
static boolean isImmutableValue(Node n) {
switch (n.getType()) {
case Token.STRING:
case Token.NUMBER:
case Token.NULL:
case Token.TRUE:
case Token.FALSE:
case Token.VOID:
return true;
case Token.NEG:
return isImmutableValue(n.getFirstChild());
case Token.NAME:
String name = n.getString();
// We assume here that programs don't change the value of the keyword
// undefined to something other than the value undefined.
return "undefined".equals(name)
|| "Infinity".equals(name)
|| "NaN".equals(name);
}
return false;
}
示例3: isCollapsibleValue
/**
* Determines whether we know enough about the given value to be able
* to collapse it into subsequent expressions.
*
* For example, we can collapse booleans and variable names:
* <code>
* x = 3; y = x; // y = x = 3;
* a = true; b = true; // b = a = true;
* <code>
* But we won't try to collapse complex expressions.
*
* @param value The value node.
* @param isLValue Whether it's on the left-hand side of an expr.
*/
private boolean isCollapsibleValue(Node value, boolean isLValue) {
switch (value.getType()) {
case Token.GETPROP:
// Do not collapse GETPROPs on arbitrary objects, because
// they may be implemented setter functions, and oftentimes
// setter functions fail on native objects. This is ok for "THIS"
// objects, because we assume that they are non-native.
return !isLValue || value.getFirstChild().getType() == Token.THIS;
case Token.NAME:
case Token.NUMBER:
case Token.TRUE:
case Token.FALSE:
case Token.NULL:
case Token.STRING:
return true;
}
return false;
}
示例4: determineType
@Override
public TypeInfo determineType() {
if(node.getType() == Token.NUMBER) return new TypeInfo(this, JavaScriptDictionary.NUMBER);
else if(node.getType() == Token.STRING) return new TypeInfo(this, JavaScriptDictionary.STRING);
else if(node.getType() == Token.TRUE || node.getType() == Token.FALSE) return new TypeInfo(this, JavaScriptDictionary.BOOL);
else if(node.getType() == Token.THIS) {
// Is this "this" reference declared in a function declared in an object literal? If so, it's type info
// should include the scope of the object literal, so that functions called with respect to "this" first
// search the object literal for matching functions. Not infallible, but a good guess.
CFGFunction function = getFunction();
ScriptOrFnNode funNode = function.getFunctionNode();
if(funNode.getParent() != null && funNode.getParent().getType() == Token.OBJECTLIT) {
JavaScriptType type = getFunction().getFeedlack().getObjectLiteralType(funNode.getParent());
if(type != null)
return new TypeInfo(this, type);
}
return new TypeInfo(this, JavaScriptDictionary.NONE);
}
else return new TypeInfo(this, JavaScriptDictionary.NONE);
}
示例5: getCodeString
@Override
public String getCodeString() {
if(node.getType() == Token.NUMBER) return "" + node.getDouble();
else if(node.getType() == Token.STRING) return "" + node.getString();
else if(node.getType() == Token.THIS) return "this";
else return "[BasicNode " + Token.name(node.getType()) + "]";
}
示例6: getBooleanValue
/**
* Gets the boolean value of a node that represents a literal. This method
* effectively emulates the <code>Boolean()</code> JavaScript cast function.
*
* @throws IllegalArgumentException If {@code n} is not a literal value
*/
static boolean getBooleanValue(Node n) {
switch (n.getType()) {
case Token.STRING:
return n.getString().length() > 0;
case Token.NUMBER:
return n.getDouble() != 0;
case Token.NULL:
case Token.FALSE:
case Token.VOID:
return false;
case Token.NAME:
String name = n.getString();
if ("undefined".equals(name)
|| "NaN".equals(name)) {
// We assume here that programs don't change the value of the keyword
// undefined to something other than the value undefined.
return false;
} else if ("Infinity".equals(name)) {
return true;
}
break;
case Token.TRUE:
case Token.ARRAYLIT:
case Token.OBJECTLIT:
case Token.REGEXP:
return true;
}
throw new IllegalArgumentException("Non-literal value: " + n);
}
示例7: getStringValue
/**
* Gets the value of a node as a String, or null if it cannot be converted.
* When it returns a non-null String, this method effectively emulates the
* <code>String()</code> JavaScript cast function.
*/
static String getStringValue(Node n) {
// TODO(user): Convert constant array, object, and regex literals as well.
switch (n.getType()) {
case Token.NAME:
case Token.STRING:
return n.getString();
case Token.NUMBER:
double value = n.getDouble();
long longValue = (long) value;
// Return "1" instead of "1.0"
if (longValue == value) {
return Long.toString(longValue);
} else {
return Double.toString(n.getDouble());
}
case Token.FALSE:
case Token.TRUE:
case Token.NULL:
return Node.tokenToName(n.getType());
case Token.VOID:
return "undefined";
}
return null;
}
示例8: isValidDefineValue
/**
* Determines whether the given value may be assigned to a define.
*
* @param val The value being assigned.
* @param defines The list of names of existing defines.
*/
static boolean isValidDefineValue(Node val, Set<String> defines) {
switch (val.getType()) {
case Token.STRING:
case Token.NUMBER:
case Token.TRUE:
case Token.FALSE:
return true;
// Single operators are valid if the child is valid.
case Token.BITAND:
case Token.BITNOT:
case Token.BITOR:
case Token.BITXOR:
case Token.NOT:
case Token.NEG:
return isValidDefineValue(val.getFirstChild(), defines);
// Names are valid if and only if they are defines themselves.
case Token.NAME:
case Token.GETPROP:
if (val.isQualifiedName()) {
return defines.contains(val.getQualifiedName());
}
}
return false;
}
示例9: tryFoldArithmetic
/**
* Try to fold arithmetic binary operators
*/
void tryFoldArithmetic(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.NUMBER &&
right.getType() == Token.NUMBER) {
double result;
double lval = left.getDouble();
double rval = right.getDouble();
switch (n.getType()) {
case Token.ADD:
result = lval + rval;
break;
case Token.SUB:
result = lval - rval;
break;
case Token.MUL:
result = lval * rval;
break;
case Token.DIV:
if (rval == 0) {
error(t, DIVIDE_BY_0_ERROR, right);
return;
}
result = lval / rval;
break;
default:
throw new Error("Unknown arithmetic operator");
}
// length of the left and right value plus 1 byte for the operator.
if (String.valueOf(result).length() <=
String.valueOf(lval).length() + String.valueOf(rval).length() + 1) {
parent.replaceChild(n, Node.newNumber(result));
t.getCompiler().reportCodeChange();
}
}
}
示例10: precedence
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 + ")");
}
}
示例11: tryFoldBitAndOr
/**
* Try to fold arithmetic binary operators
*/
void tryFoldBitAndOr(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.NUMBER &&
right.getType() == Token.NUMBER) {
double result;
double lval = left.getDouble();
double rval = right.getDouble();
// For now, we are being extra conservative, and only folding ints in
// the range MIN_VALUE-MAX_VALUE
if (lval < Integer.MIN_VALUE || lval > Integer.MAX_VALUE ||
rval < Integer.MIN_VALUE || rval > Integer.MAX_VALUE) {
// Fall back through and let the javascript use the larger values
return;
}
// Convert the numbers to ints
int lvalInt = (int) lval;
if (lvalInt != lval) {
return;
}
int rvalInt = (int) rval;
if (rvalInt != rval) {
return;
}
switch (n.getType()) {
case Token.BITAND:
result = lvalInt & rvalInt;
break;
case Token.BITOR:
result = lvalInt | rvalInt;
break;
default:
throw new Error("Unknown bitwise operator");
}
parent.replaceChild(n, Node.newNumber(result));
t.getCompiler().reportCodeChange();
}
}
示例12: tryFoldShift
/**
* Try to fold shift operations
*/
void tryFoldShift(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.NUMBER &&
right.getType() == Token.NUMBER) {
double result;
double lval = left.getDouble();
double rval = right.getDouble();
// check ranges. We do not do anything that would clip the double to
// a 32-bit range, since the user likely does not intend that.
if (!(lval >= Integer.MIN_VALUE && lval <= Integer.MAX_VALUE)) {
error(t, BITWISE_OPERAND_OUT_OF_RANGE, left);
return;
}
// only the lower 5 bits are used when shifting, so don't do anything
// if the shift amount is outside [0,32)
if (!(rval >= 0 && rval < 32)) {
error(t, SHIFT_AMOUNT_OUT_OF_BOUNDS, right);
return;
}
// Convert the numbers to ints
int lvalInt = (int) lval;
if (lvalInt != lval) {
error(t, FRACTIONAL_BITWISE_OPERAND, left);
return;
}
int rvalInt = (int) rval;
if (rvalInt != rval) {
error(t, FRACTIONAL_BITWISE_OPERAND, right);
return;
}
switch (n.getType()) {
case Token.LSH:
result = lvalInt << rvalInt;
break;
case Token.RSH:
result = lvalInt >> rvalInt;
break;
case Token.URSH:
result = lvalInt >>> rvalInt;
break;
default:
throw new AssertionError("Unknown shift operator: " +
Node.tokenToName(n.getType()));
}
parent.replaceChild(n, Node.newNumber(result));
t.getCompiler().reportCodeChange();
}
}
示例13: tryFoldStringIndexOf
/**
* Try to evaluate String.indexOf/lastIndexOf:
* "abcdef".indexOf("bc") -> 1
* "abcdefbc".indexOf("bc", 3) -> 6
*/
void tryFoldStringIndexOf(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (!NodeUtil.isGetProp(left) || !NodeUtil.isImmutableValue(right)) {
return;
}
Node lstringNode = left.getFirstChild();
Node functionName = lstringNode.getNext();
if ((lstringNode.getType() != Token.STRING) ||
(!functionName.getString().equals("indexOf") &&
!functionName.getString().equals("lastIndexOf"))) {
return;
}
String lstring = NodeUtil.getStringValue(lstringNode);
boolean isIndexOf = functionName.getString().equals("indexOf");
Node firstArg = right;
Node secondArg = right.getNext();
String searchValue = NodeUtil.getStringValue(firstArg);
// searchValue must be a valid string.
if (searchValue == null) {
return;
}
int fromIndex = isIndexOf ? 0 : lstring.length();
if (secondArg != null) {
// Third-argument and non-numeric second arg are problematic. Discard.
if ((secondArg.getNext() != null) ||
(secondArg.getType() != Token.NUMBER)) {
return;
} else {
fromIndex = (int) secondArg.getDouble();
}
}
int indexVal = isIndexOf ? lstring.indexOf(searchValue, fromIndex)
: lstring.lastIndexOf(searchValue, fromIndex);
Node newNode = Node.newNumber(indexVal);
parent.replaceChild(n, newNode);
t.getCompiler().reportCodeChange();
}
示例14: tryFoldGetElem
/**
* Try to fold array-element. e.g [1, 2, 3][10];
*/
void tryFoldGetElem(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.ARRAYLIT) {
if (right.getType() != Token.NUMBER) {
// Sometimes people like to use complex expressions to index into
// arrays, or strings to index into array methods.
return;
}
double index = right.getDouble();
int intIndex = (int) index;
if (intIndex != index) {
t.getCompiler().report(JSError.make(t, right,
INVALID_GETELEM_INDEX_ERROR, String.valueOf(index)));
return;
}
if (intIndex < 0) {
t.getCompiler().report(JSError.make(t, n, INDEX_OUT_OF_BOUNDS_ERROR,
String.valueOf(intIndex)));
return;
}
Node elem = left.getFirstChild();
for (int i = 0; elem != null && i < intIndex; i++) {
elem = elem.getNext();
}
if (elem == null) {
t.getCompiler().report(JSError.make(t, n, INDEX_OUT_OF_BOUNDS_ERROR,
String.valueOf(intIndex)));
return;
}
// Replace the entire GETELEM with the value
left.removeChild(elem);
parent.replaceChild(n, elem);
t.getCompiler().reportCodeChange();
}
}
示例15: getName
/**
* Returns a qualified name of the specified node. Dots and brackets
* are changed to the delimiter passed in when constructing the
* NodeNameExtractor object. We also replace ".prototype" with the
* delimiter to keep names short, while still differentiating them
* from static properties. (Prototype properties will end up
* looking like "a$b$$c" if this.delimiter = '$'.)
*/
String getName(Node node) {
switch (node.getType()) {
case Token.FUNCTION:
Node functionNameNode = node.getFirstChild();
return functionNameNode.getString();
case Token.GETPROP:
Node lhsOfDot = node.getFirstChild();
Node rhsOfDot = lhsOfDot.getNext();
String lhsOfDotName = getName(lhsOfDot);
String rhsOfDotName = getName(rhsOfDot);
if ("prototype".equals(rhsOfDotName)) {
return lhsOfDotName + delimiter;
} else {
return lhsOfDotName + delimiter + rhsOfDotName;
}
case Token.GETELEM:
Node outsideBrackets = node.getFirstChild();
Node insideBrackets = outsideBrackets.getNext();
String nameOutsideBrackets = getName(outsideBrackets);
String nameInsideBrackets = getName(insideBrackets);
if ("prototype".equals(nameInsideBrackets)) {
return nameOutsideBrackets + delimiter;
} else {
return nameOutsideBrackets + delimiter + nameInsideBrackets;
}
case Token.NAME:
return node.getString();
case Token.STRING:
return TokenStream.isJSIdentifier(node.getString()) ?
node.getString() : ("__" + nextUniqueInt++);
case Token.NUMBER:
return NodeUtil.getStringValue(node);
case Token.THIS:
return "this";
case Token.CALL:
return getName(node.getFirstChild());
default:
StringBuilder sb = new StringBuilder();
for (Node child = node.getFirstChild(); child != null;
child = child.getNext()) {
if (sb.length() > 0) {
sb.append(delimiter);
}
sb.append(getName(child));
}
return sb.toString();
}
}