001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Roger Lawrence
026: * Mike McCabe
027: * Igor Bukanov
028: * Bob Jervis
029: * Milen Nankov
030: *
031: * Alternatively, the contents of this file may be used under the terms of
032: * the GNU General Public License Version 2 or later (the "GPL"), in which
033: * case the provisions of the GPL are applicable instead of those above. If
034: * you wish to allow use of your version of this file only under the terms of
035: * the GPL and not to allow others to use your version of this file under the
036: * MPL, indicate your decision by deleting the provisions above and replacing
037: * them with the notice and other provisions required by the GPL. If you do
038: * not delete the provisions above, a recipient may use your version of this
039: * file under either the MPL or the GPL.
040: *
041: * ***** END LICENSE BLOCK ***** */
042:
043: package org.mozilla.javascript;
044:
045: /**
046: * This class implements the JavaScript scanner.
047: *
048: * It is based on the C source files jsscan.c and jsscan.h
049: * in the jsref package.
050: *
051: * @see org.mozilla.javascript.Parser
052: *
053: * @author Mike McCabe
054: * @author Brendan Eich
055: */
056:
057: public class Token {
058:
059: // debug flags
060: public static final boolean printTrees = false;
061: static final boolean printICode = false;
062: static final boolean printNames = printTrees || printICode;
063:
064: /**
065: * Token types. These values correspond to JSTokenType values in
066: * jsscan.c.
067: */
068:
069: public final static int
070: // start enum
071: ERROR = -1, // well-known as the only code < EOF
072: EOF = 0, // end of file token - (not EOF_CHAR)
073: EOL = 1, // end of line
074:
075: // Interpreter reuses the following as bytecodes
076: FIRST_BYTECODE_TOKEN = 2,
077:
078: ENTERWITH = 2, LEAVEWITH = 3,
079: RETURN = 4,
080: GOTO = 5,
081: IFEQ = 6, IFNE = 7, SETNAME = 8,
082: BITOR = 9,
083: BITXOR = 10,
084: BITAND = 11, EQ = 12, NE = 13, LT = 14,
085: LE = 15,
086: GT = 16,
087: GE = 17, LSH = 18, RSH = 19, URSH = 20,
088: ADD = 21,
089: SUB = 22,
090: MUL = 23, DIV = 24,
091: MOD = 25,
092: NOT = 26,
093: BITNOT = 27,
094: POS = 28,
095: NEG = 29,
096: NEW = 30,
097: DELPROP = 31,
098: TYPEOF = 32,
099: GETPROP = 33,
100: GETPROPNOWARN = 34,
101: SETPROP = 35,
102: GETELEM = 36,
103: SETELEM = 37,
104: CALL = 38,
105: NAME = 39,
106: NUMBER = 40,
107: STRING = 41,
108: NULL = 42,
109: THIS = 43,
110: FALSE = 44,
111: TRUE = 45,
112: SHEQ = 46, // shallow equality (===)
113: SHNE = 47, // shallow inequality (!==)
114: REGEXP = 48,
115: BINDNAME = 49,
116: THROW = 50,
117: RETHROW = 51, // rethrow caught exception: catch (e if ) use it
118: IN = 52,
119: INSTANCEOF = 53,
120: LOCAL_LOAD = 54,
121: GETVAR = 55,
122: SETVAR = 56,
123: CATCH_SCOPE = 57,
124: ENUM_INIT_KEYS = 58,
125: ENUM_INIT_VALUES = 59,
126: ENUM_INIT_ARRAY = 60,
127: ENUM_NEXT = 61,
128: ENUM_ID = 62,
129: THISFN = 63,
130: RETURN_RESULT = 64, // to return previously stored return result
131: ARRAYLIT = 65, // array literal
132: OBJECTLIT = 66, // object literal
133: GET_REF = 67, // *reference
134: SET_REF = 68, // *reference = something
135: DEL_REF = 69, // delete reference
136: REF_CALL = 70, // f(args) = something or f(args)++
137: REF_SPECIAL = 71, // reference for special properties like __proto
138: YIELD = 72, // JS 1.7 yield pseudo keyword
139:
140: // For XML support:
141: DEFAULTNAMESPACE = 73, // default xml namespace =
142: ESCXMLATTR = 74, ESCXMLTEXT = 75, REF_MEMBER = 76, // Reference for x.@y, x..y etc.
143: REF_NS_MEMBER = 77, // Reference for x.ns::y, x..ns::y etc.
144: REF_NAME = 78, // Reference for @y, @[y] etc.
145: REF_NS_NAME = 79; // Reference for ns::y, @ns::y@[y] etc.
146:
147: // End of interpreter bytecodes
148: public final static int LAST_BYTECODE_TOKEN = REF_NS_NAME,
149:
150: TRY = 80, SEMI = 81, // semicolon
151: LB = 82, // left and right brackets
152: RB = 83, LC = 84, // left and right curlies (braces)
153: RC = 85, LP = 86, // left and right parentheses
154: RP = 87, COMMA = 88, // comma operator
155:
156: ASSIGN = 89, // simple assignment (=)
157: ASSIGN_BITOR = 90, // |=
158: ASSIGN_BITXOR = 91, // ^=
159: ASSIGN_BITAND = 92, // |=
160: ASSIGN_LSH = 93, // <<=
161: ASSIGN_RSH = 94, // >>=
162: ASSIGN_URSH = 95, // >>>=
163: ASSIGN_ADD = 96, // +=
164: ASSIGN_SUB = 97, // -=
165: ASSIGN_MUL = 98, // *=
166: ASSIGN_DIV = 99, // /=
167: ASSIGN_MOD = 100; // %=
168:
169: public final static int FIRST_ASSIGN = ASSIGN,
170: LAST_ASSIGN = ASSIGN_MOD,
171:
172: HOOK = 101, // conditional (?:)
173: COLON = 102,
174: OR = 103, // logical or (||)
175: AND = 104, // logical and (&&)
176: INC = 105, // increment/decrement (++ --)
177: DEC = 106,
178: DOT = 107, // member operator (.)
179: FUNCTION = 108, // function keyword
180: EXPORT = 109, // export keyword
181: IMPORT = 110, // import keyword
182: IF = 111, // if keyword
183: ELSE = 112, // else keyword
184: SWITCH = 113, // switch keyword
185: CASE = 114, // case keyword
186: DEFAULT = 115, // default keyword
187: WHILE = 116, // while keyword
188: DO = 117, // do keyword
189: FOR = 118, // for keyword
190: BREAK = 119, // break keyword
191: CONTINUE = 120, // continue keyword
192: VAR = 121, // var keyword
193: WITH = 122, // with keyword
194: CATCH = 123, // catch keyword
195: FINALLY = 124, // finally keyword
196: VOID = 125, // void keyword
197: RESERVED = 126, // reserved keywords
198:
199: EMPTY = 127,
200:
201: /* types used for the parse tree - these never get returned
202: * by the scanner.
203: */
204:
205: BLOCK = 128, // statement block
206: LABEL = 129, // label
207: TARGET = 130,
208: LOOP = 131,
209: EXPR_VOID = 132, // expression statement in functions
210: EXPR_RESULT = 133, // expression statement in scripts
211: JSR = 134,
212: SCRIPT = 135, // top-level node for entire script
213: TYPEOFNAME = 136, // for typeof(simple-name)
214: USE_STACK = 137,
215: SETPROP_OP = 138, // x.y op= something
216: SETELEM_OP = 139, // x[y] op= something
217: LOCAL_BLOCK = 140,
218: SET_REF_OP = 141, // *reference op= something
219:
220: // For XML support:
221: DOTDOT = 142, // member operator (..)
222: COLONCOLON = 143, // namespace::name
223: XML = 144, // XML type
224: DOTQUERY = 145, // .() -- e.g., x.emps.emp.(name == "terry")
225: XMLATTR = 146, // @
226: XMLEND = 147,
227:
228: // Optimizer-only-tokens
229: TO_OBJECT = 148,
230: TO_DOUBLE = 149,
231:
232: GET = 150, // JS 1.5 get pseudo keyword
233: SET = 151, // JS 1.5 set pseudo keyword
234: LET = 152, // JS 1.7 let pseudo keyword
235: CONST = 153, SETCONST = 154,
236: SETCONSTVAR = 155,
237: ARRAYCOMP = 156, // array comprehension
238: LETEXPR = 157, WITHEXPR = 158, DEBUGGER = 159,
239: LAST_TOKEN = 159;
240:
241: public static String name(int token) {
242: if (!printNames) {
243: return String.valueOf(token);
244: }
245: switch (token) {
246: case ERROR:
247: return "ERROR";
248: case EOF:
249: return "EOF";
250: case EOL:
251: return "EOL";
252: case ENTERWITH:
253: return "ENTERWITH";
254: case LEAVEWITH:
255: return "LEAVEWITH";
256: case RETURN:
257: return "RETURN";
258: case GOTO:
259: return "GOTO";
260: case IFEQ:
261: return "IFEQ";
262: case IFNE:
263: return "IFNE";
264: case SETNAME:
265: return "SETNAME";
266: case BITOR:
267: return "BITOR";
268: case BITXOR:
269: return "BITXOR";
270: case BITAND:
271: return "BITAND";
272: case EQ:
273: return "EQ";
274: case NE:
275: return "NE";
276: case LT:
277: return "LT";
278: case LE:
279: return "LE";
280: case GT:
281: return "GT";
282: case GE:
283: return "GE";
284: case LSH:
285: return "LSH";
286: case RSH:
287: return "RSH";
288: case URSH:
289: return "URSH";
290: case ADD:
291: return "ADD";
292: case SUB:
293: return "SUB";
294: case MUL:
295: return "MUL";
296: case DIV:
297: return "DIV";
298: case MOD:
299: return "MOD";
300: case NOT:
301: return "NOT";
302: case BITNOT:
303: return "BITNOT";
304: case POS:
305: return "POS";
306: case NEG:
307: return "NEG";
308: case NEW:
309: return "NEW";
310: case DELPROP:
311: return "DELPROP";
312: case TYPEOF:
313: return "TYPEOF";
314: case GETPROP:
315: return "GETPROP";
316: case GETPROPNOWARN:
317: return "GETPROPNOWARN";
318: case SETPROP:
319: return "SETPROP";
320: case GETELEM:
321: return "GETELEM";
322: case SETELEM:
323: return "SETELEM";
324: case CALL:
325: return "CALL";
326: case NAME:
327: return "NAME";
328: case NUMBER:
329: return "NUMBER";
330: case STRING:
331: return "STRING";
332: case NULL:
333: return "NULL";
334: case THIS:
335: return "THIS";
336: case FALSE:
337: return "FALSE";
338: case TRUE:
339: return "TRUE";
340: case SHEQ:
341: return "SHEQ";
342: case SHNE:
343: return "SHNE";
344: case REGEXP:
345: return "OBJECT";
346: case BINDNAME:
347: return "BINDNAME";
348: case THROW:
349: return "THROW";
350: case RETHROW:
351: return "RETHROW";
352: case IN:
353: return "IN";
354: case INSTANCEOF:
355: return "INSTANCEOF";
356: case LOCAL_LOAD:
357: return "LOCAL_LOAD";
358: case GETVAR:
359: return "GETVAR";
360: case SETVAR:
361: return "SETVAR";
362: case CATCH_SCOPE:
363: return "CATCH_SCOPE";
364: case ENUM_INIT_KEYS:
365: return "ENUM_INIT_KEYS";
366: case ENUM_INIT_VALUES:
367: return "ENUM_INIT_VALUES";
368: case ENUM_INIT_ARRAY:
369: return "ENUM_INIT_ARRAY";
370: case ENUM_NEXT:
371: return "ENUM_NEXT";
372: case ENUM_ID:
373: return "ENUM_ID";
374: case THISFN:
375: return "THISFN";
376: case RETURN_RESULT:
377: return "RETURN_RESULT";
378: case ARRAYLIT:
379: return "ARRAYLIT";
380: case OBJECTLIT:
381: return "OBJECTLIT";
382: case GET_REF:
383: return "GET_REF";
384: case SET_REF:
385: return "SET_REF";
386: case DEL_REF:
387: return "DEL_REF";
388: case REF_CALL:
389: return "REF_CALL";
390: case REF_SPECIAL:
391: return "REF_SPECIAL";
392: case DEFAULTNAMESPACE:
393: return "DEFAULTNAMESPACE";
394: case ESCXMLTEXT:
395: return "ESCXMLTEXT";
396: case ESCXMLATTR:
397: return "ESCXMLATTR";
398: case REF_MEMBER:
399: return "REF_MEMBER";
400: case REF_NS_MEMBER:
401: return "REF_NS_MEMBER";
402: case REF_NAME:
403: return "REF_NAME";
404: case REF_NS_NAME:
405: return "REF_NS_NAME";
406: case TRY:
407: return "TRY";
408: case SEMI:
409: return "SEMI";
410: case LB:
411: return "LB";
412: case RB:
413: return "RB";
414: case LC:
415: return "LC";
416: case RC:
417: return "RC";
418: case LP:
419: return "LP";
420: case RP:
421: return "RP";
422: case COMMA:
423: return "COMMA";
424: case ASSIGN:
425: return "ASSIGN";
426: case ASSIGN_BITOR:
427: return "ASSIGN_BITOR";
428: case ASSIGN_BITXOR:
429: return "ASSIGN_BITXOR";
430: case ASSIGN_BITAND:
431: return "ASSIGN_BITAND";
432: case ASSIGN_LSH:
433: return "ASSIGN_LSH";
434: case ASSIGN_RSH:
435: return "ASSIGN_RSH";
436: case ASSIGN_URSH:
437: return "ASSIGN_URSH";
438: case ASSIGN_ADD:
439: return "ASSIGN_ADD";
440: case ASSIGN_SUB:
441: return "ASSIGN_SUB";
442: case ASSIGN_MUL:
443: return "ASSIGN_MUL";
444: case ASSIGN_DIV:
445: return "ASSIGN_DIV";
446: case ASSIGN_MOD:
447: return "ASSIGN_MOD";
448: case HOOK:
449: return "HOOK";
450: case COLON:
451: return "COLON";
452: case OR:
453: return "OR";
454: case AND:
455: return "AND";
456: case INC:
457: return "INC";
458: case DEC:
459: return "DEC";
460: case DOT:
461: return "DOT";
462: case FUNCTION:
463: return "FUNCTION";
464: case EXPORT:
465: return "EXPORT";
466: case IMPORT:
467: return "IMPORT";
468: case IF:
469: return "IF";
470: case ELSE:
471: return "ELSE";
472: case SWITCH:
473: return "SWITCH";
474: case CASE:
475: return "CASE";
476: case DEFAULT:
477: return "DEFAULT";
478: case WHILE:
479: return "WHILE";
480: case DO:
481: return "DO";
482: case FOR:
483: return "FOR";
484: case BREAK:
485: return "BREAK";
486: case CONTINUE:
487: return "CONTINUE";
488: case VAR:
489: return "VAR";
490: case WITH:
491: return "WITH";
492: case CATCH:
493: return "CATCH";
494: case FINALLY:
495: return "FINALLY";
496: case VOID:
497: return "VOID";
498: case RESERVED:
499: return "RESERVED";
500: case EMPTY:
501: return "EMPTY";
502: case BLOCK:
503: return "BLOCK";
504: case LABEL:
505: return "LABEL";
506: case TARGET:
507: return "TARGET";
508: case LOOP:
509: return "LOOP";
510: case EXPR_VOID:
511: return "EXPR_VOID";
512: case EXPR_RESULT:
513: return "EXPR_RESULT";
514: case JSR:
515: return "JSR";
516: case SCRIPT:
517: return "SCRIPT";
518: case TYPEOFNAME:
519: return "TYPEOFNAME";
520: case USE_STACK:
521: return "USE_STACK";
522: case SETPROP_OP:
523: return "SETPROP_OP";
524: case SETELEM_OP:
525: return "SETELEM_OP";
526: case LOCAL_BLOCK:
527: return "LOCAL_BLOCK";
528: case SET_REF_OP:
529: return "SET_REF_OP";
530: case DOTDOT:
531: return "DOTDOT";
532: case COLONCOLON:
533: return "COLONCOLON";
534: case XML:
535: return "XML";
536: case DOTQUERY:
537: return "DOTQUERY";
538: case XMLATTR:
539: return "XMLATTR";
540: case XMLEND:
541: return "XMLEND";
542: case TO_OBJECT:
543: return "TO_OBJECT";
544: case TO_DOUBLE:
545: return "TO_DOUBLE";
546: case GET:
547: return "GET";
548: case SET:
549: return "SET";
550: case LET:
551: return "LET";
552: case YIELD:
553: return "YIELD";
554: case CONST:
555: return "CONST";
556: case SETCONST:
557: return "SETCONST";
558: case ARRAYCOMP:
559: return "ARRAYCOMP";
560: case WITHEXPR:
561: return "WITHEXPR";
562: case LETEXPR:
563: return "LETEXPR";
564: case DEBUGGER:
565: return "DEBUGGER";
566: }
567:
568: // Token without name
569: throw new IllegalStateException(String.valueOf(token));
570: }
571: }
|