001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.js.ast;
017:
018: /**
019: * Represents the operator in a JavaScript binary operation.
020: */
021: public final class JsBinaryOperator extends JsOperator {
022:
023: // Precedence indices from "JavaScript - The Definitive Guide" 4th Edition
024: // (page 57)
025: //
026:
027: // Precendence 15 is for really important things that have their own AST
028: // classes.
029:
030: // Precendence 14 is for unary operators.
031:
032: private static final int LEFT_INFIX = LEFT | INFIX;
033: public static final JsBinaryOperator MUL = create("*", 13,
034: LEFT_INFIX);
035: public static final JsBinaryOperator DIV = create("/", 13,
036: LEFT_INFIX);
037: public static final JsBinaryOperator MOD = create("%", 13,
038: LEFT_INFIX);
039:
040: public static final JsBinaryOperator ADD = create("+", 12,
041: LEFT_INFIX);
042: public static final JsBinaryOperator SUB = create("-", 12,
043: LEFT_INFIX);
044:
045: public static final JsBinaryOperator SHL = create("<<", 11,
046: LEFT_INFIX);
047: public static final JsBinaryOperator SHR = create(">>", 11,
048: LEFT_INFIX);
049: public static final JsBinaryOperator SHRU = create(">>>", 11,
050: LEFT_INFIX);
051:
052: public static final JsBinaryOperator LT = create("<", 10,
053: LEFT_INFIX);
054: public static final JsBinaryOperator LTE = create("<=", 10,
055: LEFT_INFIX);
056: public static final JsBinaryOperator GT = create(">", 10,
057: LEFT_INFIX);
058: public static final JsBinaryOperator GTE = create(">=", 10,
059: LEFT_INFIX);
060: public static final JsBinaryOperator INSTANCEOF = create(
061: "instanceof", 10, LEFT_INFIX);
062: public static final JsBinaryOperator INOP = create("in", 10,
063: LEFT_INFIX);
064:
065: public static final JsBinaryOperator EQ = create("==", 9,
066: LEFT_INFIX);
067: public static final JsBinaryOperator NEQ = create("!=", 9,
068: LEFT_INFIX);
069: public static final JsBinaryOperator REF_EQ = create("===", 9,
070: LEFT_INFIX);
071: public static final JsBinaryOperator REF_NEQ = create("!==", 9,
072: LEFT_INFIX);
073:
074: public static final JsBinaryOperator BIT_AND = create("&", 8,
075: LEFT_INFIX);
076:
077: public static final JsBinaryOperator BIT_XOR = create("^", 7,
078: LEFT_INFIX);
079:
080: public static final JsBinaryOperator BIT_OR = create("|", 6,
081: LEFT_INFIX);
082:
083: public static final JsBinaryOperator AND = create("&&", 5,
084: LEFT_INFIX);
085:
086: public static final JsBinaryOperator OR = create("||", 4,
087: LEFT_INFIX);
088:
089: // Precendence 3 is for the condition operator.
090:
091: // These assignment operators are right-associatve.
092: public static final JsBinaryOperator ASG = create("=", 2, INFIX);
093: public static final JsBinaryOperator ASG_ADD = create("+=", 2,
094: INFIX);
095: public static final JsBinaryOperator ASG_SUB = create("-=", 2,
096: INFIX);
097: public static final JsBinaryOperator ASG_MUL = create("*=", 2,
098: INFIX);
099: public static final JsBinaryOperator ASG_DIV = create("/=", 2,
100: INFIX);
101: public static final JsBinaryOperator ASG_MOD = create("%=", 2,
102: INFIX);
103: public static final JsBinaryOperator ASG_SHL = create("<<=", 2,
104: INFIX);
105: public static final JsBinaryOperator ASG_SHR = create(">>=", 2,
106: INFIX);
107: public static final JsBinaryOperator ASG_SHRU = create(">>>=", 2,
108: INFIX);
109: public static final JsBinaryOperator ASG_BIT_AND = create("&=", 2,
110: INFIX);
111: public static final JsBinaryOperator ASG_BIT_OR = create("|=", 2,
112: INFIX);
113: public static final JsBinaryOperator ASG_BIT_XOR = create("^=", 2,
114: INFIX);
115:
116: public static final JsBinaryOperator COMMA = create(",", 1,
117: LEFT_INFIX);
118:
119: private static JsBinaryOperator create(String symbol,
120: int precedence, int mask) {
121: JsBinaryOperator op = new JsBinaryOperator(symbol, precedence,
122: mask);
123: return op;
124: }
125:
126: private JsBinaryOperator(String symbol, int precedence, int mask) {
127: super (symbol, precedence, mask);
128: }
129:
130: public boolean isAssignment() {
131: /*
132: * Beware, flaky! Maybe I should have added Yet Another Field to
133: * BinaryOperator?
134: */
135: return (getPrecedence() == ASG.getPrecedence());
136: }
137:
138: @Override
139: public boolean isKeyword() {
140: return this == INSTANCEOF || this == INOP;
141: }
142:
143: }
|