Debian Codegraph @

rhino/src/org/mozilla/javascript/Token.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 /**
10 * This class implements the JavaScript scanner.
11 *
12 * It is based on the C source files jsscan.c and jsscan.h
13 * in the jsref package.
14 *
15 * @see org.mozilla.javascript.Parser
16 *
17 * @author Mike McCabe
18 * @author Brendan Eich
19 */
20
21 public class Token
22 {
23 public static enum CommentType {
24 LINE, BLOCK_COMMENT, JSDOC, HTML
25 }
26
27 // debug flags
28 public static final boolean printTrees = false;
29 static final boolean printICode = false;
30 static final boolean printNames = printTrees || printICode;
31
32 /**
33 * Token types. These values correspond to JSTokenType values in
34 * jsscan.c.
35 */
36
37 public final static int
38 // start enum
39 ERROR = -1, // well-known as the only code < EOF
40 EOF = 0, // end of file token - (not EOF_CHAR)
41 EOL = 1, // end of line
42
43 // Interpreter reuses the following as bytecodes
44 FIRST_BYTECODE_TOKEN = 2,
45
46 ENTERWITH = 2,
47 LEAVEWITH = 3,
48 RETURN = 4,
49 GOTO = 5,
50 IFEQ = 6,
51 IFNE = 7,
52 SETNAME = 8,
53 BITOR = 9,
54 BITXOR = 10,
55 BITAND = 11,
56 EQ = 12,
57 NE = 13,
58 LT = 14,
59 LE = 15,
60 GT = 16,
61 GE = 17,
62 LSH = 18,
63 RSH = 19,
64 URSH = 20,
65 ADD = 21,
66 SUB = 22,
67 MUL = 23,
68 DIV = 24,
69 MOD = 25,
70 NOT = 26,
71 BITNOT = 27,
72 POS = 28,
73 NEG = 29,
74 NEW = 30,
75 DELPROP = 31,
76 TYPEOF = 32,
77 GETPROP = 33,
78 GETPROPNOWARN = 34,
79 SETPROP = 35,
80 GETELEM = 36,
81 SETELEM = 37,
82 CALL = 38,
83 NAME = 39,
84 NUMBER = 40,
85 STRING = 41,
86 NULL = 42,
87 THIS = 43,
88 FALSE = 44,
89 TRUE = 45,
90 SHEQ = 46, // shallow equality (===)
91 SHNE = 47, // shallow inequality (!==)
92 REGEXP = 48,
93 BINDNAME = 49,
94 THROW = 50,
95 RETHROW = 51, // rethrow caught exception: catch (e if ) use it
96 IN = 52,
97 INSTANCEOF = 53,
98 LOCAL_LOAD = 54,
99 GETVAR = 55,
100 SETVAR = 56,
101 CATCH_SCOPE = 57,
102 ENUM_INIT_KEYS = 58,
103 ENUM_INIT_VALUES = 59,
104 ENUM_INIT_ARRAY= 60,
105 ENUM_INIT_VALUES_IN_ORDER = 61,
106 ENUM_NEXT = 62,
107 ENUM_ID = 63,
108 THISFN = 64,
109 RETURN_RESULT = 65, // to return previously stored return result
110 ARRAYLIT = 66, // array literal
111 OBJECTLIT = 67, // object literal
112 GET_REF = 68, // *reference
113 SET_REF = 69, // *reference = something
114 DEL_REF = 70, // delete reference
115 REF_CALL = 71, // f(args) = something or f(args)++
116 REF_SPECIAL = 72, // reference for special properties like __proto
117 YIELD = 73, // JS 1.7 yield pseudo keyword
118 STRICT_SETNAME = 74,
119
120 // For XML support:
121 DEFAULTNAMESPACE = 75, // default xml namespace =
122 ESCXMLATTR = 76,
123 ESCXMLTEXT = 77,
124 REF_MEMBER = 78, // Reference for x.@y, x..y etc.
125 REF_NS_MEMBER = 79, // Reference for x.ns::y, x..ns::y etc.
126 REF_NAME = 80, // Reference for @y, @[y] etc.
127 REF_NS_NAME = 81; // Reference for ns::y, @ns::y@[y] etc.
128
129 // End of interpreter bytecodes
130 public final static int
131 LAST_BYTECODE_TOKEN = REF_NS_NAME,
132
133 TRY = 82,
134 SEMI = 83, // semicolon
135 LB = 84, // left and right brackets
136 RB = 85,
137 LC = 86, // left and right curlies (braces)
138 RC = 87,
139 LP = 88, // left and right parentheses
140 RP = 89,
141 COMMA = 90, // comma operator
142
143 ASSIGN = 91, // simple assignment (=)
144 ASSIGN_BITOR = 92, // |=
145 ASSIGN_BITXOR = 93, // ^=
146 ASSIGN_BITAND = 94, // |=
147 ASSIGN_LSH = 95, // <<=
148 ASSIGN_RSH = 96, // >>=
149 ASSIGN_URSH = 97, // >>>=
150 ASSIGN_ADD = 98, // +=
151 ASSIGN_SUB = 99, // -=
152 ASSIGN_MUL = 100, // *=
153 ASSIGN_DIV = 101, // /=
154 ASSIGN_MOD = 102; // %=
155
156 public final static int
157 FIRST_ASSIGN = ASSIGN,
158 LAST_ASSIGN = ASSIGN_MOD,
159
160 HOOK = 103, // conditional (?:)
161 COLON = 104,
162 OR = 105, // logical or (||)
163 AND = 106, // logical and (&&)
164 INC = 107, // increment/decrement (++ --)
165 DEC = 108,
166 DOT = 109, // member operator (.)
167 FUNCTION = 110, // function keyword
168 EXPORT = 111, // export keyword
169 IMPORT = 112, // import keyword
170 IF = 113, // if keyword
171 ELSE = 114, // else keyword
172 SWITCH = 115, // switch keyword
173 CASE = 116, // case keyword
174 DEFAULT = 117, // default keyword
175 WHILE = 118, // while keyword
176 DO = 119, // do keyword
177 FOR = 120, // for keyword
178 BREAK = 121, // break keyword
179 CONTINUE = 122, // continue keyword
180 VAR = 123, // var keyword
181 WITH = 124, // with keyword
182 CATCH = 125, // catch keyword
183 FINALLY = 126, // finally keyword
184 VOID = 127, // void keyword
185 RESERVED = 128, // reserved keywords
186
187 EMPTY = 129,
188
189 /* types used for the parse tree - these never get returned
190 * by the scanner.
191 */
192
193 BLOCK = 130, // statement block
194 LABEL = 131, // label
195 TARGET = 132,
196 LOOP = 133,
197 EXPR_VOID = 134, // expression statement in functions
198 EXPR_RESULT = 135, // expression statement in scripts
199 JSR = 136,
200 SCRIPT = 137, // top-level node for entire script
201 TYPEOFNAME = 138, // for typeof(simple-name)
202 USE_STACK = 139,
203 SETPROP_OP = 140, // x.y op= something
204 SETELEM_OP = 141, // x[y] op= something
205 LOCAL_BLOCK = 142,
206 SET_REF_OP = 143, // *reference op= something
207
208 // For XML support:
209 DOTDOT = 144, // member operator (..)
210 COLONCOLON = 145, // namespace::name
211 XML = 146, // XML type
212 DOTQUERY = 147, // .() -- e.g., x.emps.emp.(name == "terry")
213 XMLATTR = 148, // @
214 XMLEND = 149,
215
216 // Optimizer-only-tokens
217 TO_OBJECT = 150,
218 TO_DOUBLE = 151,
219
220 GET = 152, // JS 1.5 get pseudo keyword
221 SET = 153, // JS 1.5 set pseudo keyword
222 LET = 154, // JS 1.7 let pseudo keyword
223 CONST = 155,
224 SETCONST = 156,
225 SETCONSTVAR = 157,
226 ARRAYCOMP = 158, // array comprehension
227 LETEXPR = 159,
228 WITHEXPR = 160,
229 DEBUGGER = 161,
230 COMMENT = 162,
231 GENEXPR = 163,
232 METHOD = 164, // ES6 MethodDefinition
233 ARROW = 165, // ES6 ArrowFunction
234 LAST_TOKEN = 166;
235
236 /**
237 * Returns a name for the token. If Rhino is compiled with certain
238 * hardcoded debugging flags in this file, it calls {@code #typeToName};
239 * otherwise it returns a string whose value is the token number.
240 */
241 public static String name(int token)
242 {
243 if (!printNames) {
244 return String.valueOf(token);
245 }
246 return typeToName(token);
247 }
248
249 /**
250 * Always returns a human-readable string for the token name.
251 * For instance, {@link #FINALLY} has the name "FINALLY".
252 * @param token the token code
253 * @return the actual name for the token code
254 */
255 public static String typeToName(int token) {
256 switch (token) {
257 case ERROR: return "ERROR";
258 case EOF: return "EOF";
259 case EOL: return "EOL";
260 case ENTERWITH: return "ENTERWITH";
261 case LEAVEWITH: return "LEAVEWITH";
262 case RETURN: return "RETURN";
263 case GOTO: return "GOTO";
264 case IFEQ: return "IFEQ";
265 case IFNE: return "IFNE";
266 case SETNAME: return "SETNAME";
267 case BITOR: return "BITOR";
268 case BITXOR: return "BITXOR";
269 case BITAND: return "BITAND";
270 case EQ: return "EQ";
271 case NE: return "NE";
272 case LT: return "LT";
273 case LE: return "LE";
274 case GT: return "GT";
275 case GE: return "GE";
276 case LSH: return "LSH";
277 case RSH: return "RSH";
278 case URSH: return "URSH";
279 case ADD: return "ADD";
280 case SUB: return "SUB";
281 case MUL: return "MUL";
282 case DIV: return "DIV";
283 case MOD: return "MOD";
284 case NOT: return "NOT";
285 case BITNOT: return "BITNOT";
286 case POS: return "POS";
287 case NEG: return "NEG";
288 case NEW: return "NEW";
289 case DELPROP: return "DELPROP";
290 case TYPEOF: return "TYPEOF";
291 case GETPROP: return "GETPROP";
292 case GETPROPNOWARN: return "GETPROPNOWARN";
293 case SETPROP: return "SETPROP";
294 case GETELEM: return "GETELEM";
295 case SETELEM: return "SETELEM";
296 case CALL: return "CALL";
297 case NAME: return "NAME";
298 case NUMBER: return "NUMBER";
299 case STRING: return "STRING";
300 case NULL: return "NULL";
301 case THIS: return "THIS";
302 case FALSE: return "FALSE";
303 case TRUE: return "TRUE";
304 case SHEQ: return "SHEQ";
305 case SHNE: return "SHNE";
306 case REGEXP: return "REGEXP";
307 case BINDNAME: return "BINDNAME";
308 case THROW: return "THROW";
309 case RETHROW: return "RETHROW";
310 case IN: return "IN";
311 case INSTANCEOF: return "INSTANCEOF";
312 case LOCAL_LOAD: return "LOCAL_LOAD";
313 case GETVAR: return "GETVAR";
314 case SETVAR: return "SETVAR";
315 case CATCH_SCOPE: return "CATCH_SCOPE";
316 case ENUM_INIT_KEYS: return "ENUM_INIT_KEYS";
317 case ENUM_INIT_VALUES:return "ENUM_INIT_VALUES";
318 case ENUM_INIT_ARRAY: return "ENUM_INIT_ARRAY";
319 case ENUM_INIT_VALUES_IN_ORDER: return "ENUM_INIT_VALUES_IN_ORDER";
320 case ENUM_NEXT: return "ENUM_NEXT";
321 case ENUM_ID: return "ENUM_ID";
322 case THISFN: return "THISFN";
323 case RETURN_RESULT: return "RETURN_RESULT";
324 case ARRAYLIT: return "ARRAYLIT";
325 case OBJECTLIT: return "OBJECTLIT";
326 case GET_REF: return "GET_REF";
327 case SET_REF: return "SET_REF";
328 case DEL_REF: return "DEL_REF";
329 case REF_CALL: return "REF_CALL";
330 case REF_SPECIAL: return "REF_SPECIAL";
331 case DEFAULTNAMESPACE:return "DEFAULTNAMESPACE";
332 case ESCXMLTEXT: return "ESCXMLTEXT";
333 case ESCXMLATTR: return "ESCXMLATTR";
334 case REF_MEMBER: return "REF_MEMBER";
335 case REF_NS_MEMBER: return "REF_NS_MEMBER";
336 case REF_NAME: return "REF_NAME";
337 case REF_NS_NAME: return "REF_NS_NAME";
338 case TRY: return "TRY";
339 case SEMI: return "SEMI";
340 case LB: return "LB";
341 case RB: return "RB";
342 case LC: return "LC";
343 case RC: return "RC";
344 case LP: return "LP";
345 case RP: return "RP";
346 case COMMA: return "COMMA";
347 case ASSIGN: return "ASSIGN";
348 case ASSIGN_BITOR: return "ASSIGN_BITOR";
349 case ASSIGN_BITXOR: return "ASSIGN_BITXOR";
350 case ASSIGN_BITAND: return "ASSIGN_BITAND";
351 case ASSIGN_LSH: return "ASSIGN_LSH";
352 case ASSIGN_RSH: return "ASSIGN_RSH";
353 case ASSIGN_URSH: return "ASSIGN_URSH";
354 case ASSIGN_ADD: return "ASSIGN_ADD";
355 case ASSIGN_SUB: return "ASSIGN_SUB";
356 case ASSIGN_MUL: return "ASSIGN_MUL";
357 case ASSIGN_DIV: return "ASSIGN_DIV";
358 case ASSIGN_MOD: return "ASSIGN_MOD";
359 case HOOK: return "HOOK";
360 case COLON: return "COLON";
361 case OR: return "OR";
362 case AND: return "AND";
363 case INC: return "INC";
364 case DEC: return "DEC";
365 case DOT: return "DOT";
366 case FUNCTION: return "FUNCTION";
367 case EXPORT: return "EXPORT";
368 case IMPORT: return "IMPORT";
369 case IF: return "IF";
370 case ELSE: return "ELSE";
371 case SWITCH: return "SWITCH";
372 case CASE: return "CASE";
373 case DEFAULT: return "DEFAULT";
374 case WHILE: return "WHILE";
375 case DO: return "DO";
376 case FOR: return "FOR";
377 case BREAK: return "BREAK";
378 case CONTINUE: return "CONTINUE";
379 case VAR: return "VAR";
380 case WITH: return "WITH";
381 case CATCH: return "CATCH";
382 case FINALLY: return "FINALLY";
383 case VOID: return "VOID";
384 case RESERVED: return "RESERVED";
385 case EMPTY: return "EMPTY";
386 case BLOCK: return "BLOCK";
387 case LABEL: return "LABEL";
388 case TARGET: return "TARGET";
389 case LOOP: return "LOOP";
390 case EXPR_VOID: return "EXPR_VOID";
391 case EXPR_RESULT: return "EXPR_RESULT";
392 case JSR: return "JSR";
393 case SCRIPT: return "SCRIPT";
394 case TYPEOFNAME: return "TYPEOFNAME";
395 case USE_STACK: return "USE_STACK";
396 case SETPROP_OP: return "SETPROP_OP";
397 case SETELEM_OP: return "SETELEM_OP";
398 case LOCAL_BLOCK: return "LOCAL_BLOCK";
399 case SET_REF_OP: return "SET_REF_OP";
400 case DOTDOT: return "DOTDOT";
401 case COLONCOLON: return "COLONCOLON";
402 case XML: return "XML";
403 case DOTQUERY: return "DOTQUERY";
404 case XMLATTR: return "XMLATTR";
405 case XMLEND: return "XMLEND";
406 case TO_OBJECT: return "TO_OBJECT";
407 case TO_DOUBLE: return "TO_DOUBLE";
408 case GET: return "GET";
409 case SET: return "SET";
410 case LET: return "LET";
411 case YIELD: return "YIELD";
412 case CONST: return "CONST";
413 case SETCONST: return "SETCONST";
414 case ARRAYCOMP: return "ARRAYCOMP";
415 case WITHEXPR: return "WITHEXPR";
416 case LETEXPR: return "LETEXPR";
417 case DEBUGGER: return "DEBUGGER";
418 case COMMENT: return "COMMENT";
419 case GENEXPR: return "GENEXPR";
420 case METHOD: return "METHOD";
421 case ARROW: return "ARROW";
422 }
423
424 // Token without name
425 throw new IllegalStateException(String.valueOf(token));
426 }
427
428 /**
429 * Convert a keyword token to a name string for use with the
430 * {@link Context#FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER} feature.
431 * @param token A token
432 * @return the corresponding name string
433 */
434 public static String keywordToName(int token) {
435 switch (token) {
436 case Token.BREAK: return "break";
437 case Token.CASE: return "case";
438 case Token.CONTINUE: return "continue";
439 case Token.DEFAULT: return "default";
440 case Token.DELPROP: return "delete";
441 case Token.DO: return "do";
442 case Token.ELSE: return "else";
443 case Token.FALSE: return "false";
444 case Token.FOR: return "for";
445 case Token.FUNCTION: return "function";
446 case Token.IF: return "if";
447 case Token.IN: return "in";
448 case Token.LET: return "let";
449 case Token.NEW: return "new";
450 case Token.NULL: return "null";
451 case Token.RETURN: return "return";
452 case Token.SWITCH: return "switch";
453 case Token.THIS: return "this";
454 case Token.TRUE: return "true";
455 case Token.TYPEOF: return "typeof";
456 case Token.VAR: return "var";
457 case Token.VOID: return "void";
458 case Token.WHILE: return "while";
459 case Token.WITH: return "with";
460 case Token.YIELD: return "yield";
461 case Token.CATCH: return "catch";
462 case Token.CONST: return "const";
463 case Token.DEBUGGER: return "debugger";
464 case Token.FINALLY: return "finally";
465 case Token.INSTANCEOF: return "instanceof";
466 case Token.THROW: return "throw";
467 case Token.TRY: return "try";
468 default: return null;
469 }
470 }
471
472 /**
473 * Return true if the passed code is a valid Token constant.
474 * @param code a potential token code
475 * @return true if it's a known token
476 */
477 public static boolean isValidToken(int code) {
478 return code >= ERROR
479 && code <= LAST_TOKEN;
480 }
481 }