1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
205 private static final TokenType[] values;
206
207 TokenType(final TokenKind kind, final String name) {
208 next = null;
209 this.kind = kind;
210 this.name = name;
211 precedence = 0;
212 isLeftAssociative = false;
213 }
214
215 TokenType(final TokenKind kind, final String name, final int precedence, final boolean isLeftAssociative) {
216 next = null;
217 this.kind = kind;
218 this.name = name;
219 this.precedence = precedence;
220 this.isLeftAssociative = isLeftAssociative;
221 }
222
223 /**
224 * Determines if the token has greater precedence than other.
225 * @param other Compare token.
226 * @param isLeft Is to the left of the other.
227 * @return True if greater precedence.
228 */
229 public boolean needsParens(final TokenType other, final boolean isLeft) {
230 return other.precedence != 0 &&
231 (precedence > other.precedence ||
232 precedence == other.precedence && isLeftAssociative && !isLeft);
233 }
234
235 /**
236 * Determines if the type is a valid operator.
237 * @param noIn TRUE if IN operator should be ignored.
238 * @return TRUE if valid operator.
239 */
240 public boolean isOperator(final boolean noIn) {
241 return kind == BINARY && (!noIn || this != IN) && precedence != 0;
242 }
243
244 /**
245 * Accessors.
246 */
247 public int getLength() {
248 assert name != null : "Token name not set";
249 return name.length();
250 }
251
252 public String getName() {
253 return name;
254 }
255
256 public String getNameOrType() {
257 return name == null ? super.name().toLowerCase(Locale.ENGLISH) : name;
258 }
259
260 public TokenType getNext() {
261 return next;
262 }
263
264 public void setNext(final TokenType next) {
265 this.next = next;
266 }
|
1 /*
2 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
205 private static final TokenType[] values;
206
207 TokenType(final TokenKind kind, final String name) {
208 next = null;
209 this.kind = kind;
210 this.name = name;
211 precedence = 0;
212 isLeftAssociative = false;
213 }
214
215 TokenType(final TokenKind kind, final String name, final int precedence, final boolean isLeftAssociative) {
216 next = null;
217 this.kind = kind;
218 this.name = name;
219 this.precedence = precedence;
220 this.isLeftAssociative = isLeftAssociative;
221 }
222
223 /**
224 * Determines if the token has greater precedence than other.
225 *
226 * @param other Compare token.
227 * @param isLeft Is to the left of the other.
228 *
229 * @return {@code true} if greater precedence.
230 */
231 public boolean needsParens(final TokenType other, final boolean isLeft) {
232 return other.precedence != 0 &&
233 (precedence > other.precedence ||
234 precedence == other.precedence && isLeftAssociative && !isLeft);
235 }
236
237 /**
238 * Determines if the type is a valid operator.
239 *
240 * @param noIn {@code true} if IN operator should be ignored.
241 *
242 * @return {@code true} if valid operator.
243 */
244 public boolean isOperator(final boolean noIn) {
245 return kind == BINARY && (!noIn || this != IN) && precedence != 0;
246 }
247
248
249 public int getLength() {
250 assert name != null : "Token name not set";
251 return name.length();
252 }
253
254 public String getName() {
255 return name;
256 }
257
258 public String getNameOrType() {
259 return name == null ? super.name().toLowerCase(Locale.ENGLISH) : name;
260 }
261
262 public TokenType getNext() {
263 return next;
264 }
265
266 public void setNext(final TokenType next) {
267 this.next = next;
268 }
|