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, MOD = 25,
091: NOT = 26,
092: BITNOT = 27,
093: POS = 28, NEG = 29, NEW = 30,
094: DELPROP = 31,
095: TYPEOF = 32,
096: GETPROP = 33, SETPROP = 34,
097: GETELEM = 35,
098: SETELEM = 36,
099: CALL = 37,
100: NAME = 38,
101: NUMBER = 39,
102: STRING = 40,
103: NULL = 41,
104: THIS = 42,
105: FALSE = 43,
106: TRUE = 44,
107: SHEQ = 45, // shallow equality (===)
108: SHNE = 46, // shallow inequality (!==)
109: REGEXP = 47,
110: BINDNAME = 48,
111: THROW = 49,
112: RETHROW = 50, // rethrow caught execetion: catch (e if ) use it
113: IN = 51, INSTANCEOF = 52,
114: LOCAL_LOAD = 53,
115: GETVAR = 54,
116: SETVAR = 55,
117: CATCH_SCOPE = 56,
118: ENUM_INIT_KEYS = 57,
119: ENUM_INIT_VALUES = 58,
120: ENUM_NEXT = 59,
121: ENUM_ID = 60,
122: THISFN = 61, RETURN_RESULT = 62, // to return prevoisly stored return result
123: ARRAYLIT = 63, // array literal
124: OBJECTLIT = 64, // object literal
125: GET_REF = 65, // *reference
126: SET_REF = 66, // *reference = something
127: DEL_REF = 67, // delete reference
128: REF_CALL = 68, // f(args) = something or f(args)++
129: REF_SPECIAL = 69, // reference for special properties like __proto
130:
131: // For XML support:
132: DEFAULTNAMESPACE = 70, // default xml namespace =
133: ESCXMLATTR = 71, ESCXMLTEXT = 72, REF_MEMBER = 73, // Reference for x.@y, x..y etc.
134: REF_NS_MEMBER = 74, // Reference for x.ns::y, x..ns::y etc.
135: REF_NAME = 75, // Reference for @y, @[y] etc.
136: REF_NS_NAME = 76; // Reference for ns::y, @ns::y@[y] etc.
137:
138: // End of interpreter bytecodes
139: public final static int LAST_BYTECODE_TOKEN = REF_NS_NAME,
140:
141: TRY = 77, SEMI = 78, // semicolon
142: LB = 79, // left and right brackets
143: RB = 80, LC = 81, // left and right curlies (braces)
144: RC = 82, LP = 83, // left and right parentheses
145: RP = 84, COMMA = 85, // comma operator
146:
147: ASSIGN = 86, // simple assignment (=)
148: ASSIGN_BITOR = 87, // |=
149: ASSIGN_BITXOR = 88, // ^=
150: ASSIGN_BITAND = 89, // |=
151: ASSIGN_LSH = 90, // <<=
152: ASSIGN_RSH = 91, // >>=
153: ASSIGN_URSH = 92, // >>>=
154: ASSIGN_ADD = 93, // +=
155: ASSIGN_SUB = 94, // -=
156: ASSIGN_MUL = 95, // *=
157: ASSIGN_DIV = 96, // /=
158: ASSIGN_MOD = 97; // %=
159:
160: public final static int FIRST_ASSIGN = ASSIGN,
161: LAST_ASSIGN = ASSIGN_MOD,
162:
163: HOOK = 98, // conditional (?:)
164: COLON = 99,
165: OR = 100, // logical or (||)
166: AND = 101, // logical and (&&)
167: INC = 102, // increment/decrement (++ --)
168: DEC = 103,
169: DOT = 104, // member operator (.)
170: FUNCTION = 105, // function keyword
171: EXPORT = 106, // export keyword
172: IMPORT = 107, // import keyword
173: IF = 108, // if keyword
174: ELSE = 109, // else keyword
175: SWITCH = 110, // switch keyword
176: CASE = 111, // case keyword
177: DEFAULT = 112, // default keyword
178: WHILE = 113, // while keyword
179: DO = 114, // do keyword
180: FOR = 115, // for keyword
181: BREAK = 116, // break keyword
182: CONTINUE = 117, // continue keyword
183: VAR = 118, // var keyword
184: WITH = 119, // with keyword
185: CATCH = 120, // catch keyword
186: FINALLY = 121, // finally keyword
187: VOID = 122, // void keyword
188: RESERVED = 123, // reserved keywords
189:
190: EMPTY = 124,
191:
192: /* types used for the parse tree - these never get returned
193: * by the scanner.
194: */
195:
196: BLOCK = 125, // statement block
197: LABEL = 126, // label
198: TARGET = 127, LOOP = 128,
199: EXPR_VOID = 129, // expression statement in functions
200: EXPR_RESULT = 130, // expression statement in scripts
201: JSR = 131,
202: SCRIPT = 132, // top-level node for entire script
203: TYPEOFNAME = 133, // for typeof(simple-name)
204: USE_STACK = 134,
205: SETPROP_OP = 135, // x.y op= something
206: SETELEM_OP = 136, // x[y] op= something
207: LOCAL_BLOCK = 137,
208: SET_REF_OP = 138, // *reference op= something
209:
210: // For XML support:
211: DOTDOT = 139, // member operator (..)
212: COLONCOLON = 140, // namespace::name
213: XML = 141, // XML type
214: DOTQUERY = 142, // .() -- e.g., x.emps.emp.(name == "terry")
215: XMLATTR = 143, // @
216: XMLEND = 144,
217:
218: // Optimizer-only-tokens
219: TO_OBJECT = 145, TO_DOUBLE = 146,
220:
221: GET = 147, // JS 1.5 get pseudo keyword
222: SET = 148, // JS 1.5 set pseudo keyword
223: CONST = 149,
224: SETCONST = 150,
225: SETCONSTVAR = 151,
226:
227: // <netbeans>
228: //LAST_TOKEN = 152;
229:
230: // In syntax highlighting comments and whitespace need their own tokens
231: LINE_COMMENT = 152,
232: BLOCK_COMMENT = 153,
233: WHITESPACE = 154,
234:
235: // In the parse tree, add nodes for the parameters such that I have offsets
236: PARAMETER = 155,
237:
238: // In the parse tree, create AST nodes for the function name portion
239: // such that I have accurate offsets
240: FUNCNAME = 156,
241:
242: // In object literals, add a node for the name portion
243: OBJLITNAME = 157,
244:
245: // I need to treat the string/regexp delimiters as separate from the string itself
246: // such that bracket matching logic etc. can be more intelligent
247: STRING_BEGIN = 158, STRING_END = 159, STRING_ERROR = 160,
248: REGEXP_BEGIN = 161, REGEXP_END = 162, REGEXP_ERROR = 163,
249:
250: // For syntax highlighting purposes, "undefined" should be treated as a keyword
251: UNDEFINED = 164,
252:
253: LAST_TOKEN = 165;
254:
255: // </netbeans>
256:
257: public static String name(int token) {
258: if (!printNames) {
259: return String.valueOf(token);
260: }
261: switch (token) {
262: // <netbeans>
263: case 65535: // I SEEM TO GET UNSIGNED ISSUES - WHERE IS THE TRUNCATION?
264: // </netbeans>
265: case ERROR:
266: return "ERROR";
267: case EOF:
268: return "EOF";
269: case EOL:
270: return "EOL";
271: case ENTERWITH:
272: return "ENTERWITH";
273: case LEAVEWITH:
274: return "LEAVEWITH";
275: case RETURN:
276: return "RETURN";
277: case GOTO:
278: return "GOTO";
279: case IFEQ:
280: return "IFEQ";
281: case IFNE:
282: return "IFNE";
283: case SETNAME:
284: return "SETNAME";
285: case BITOR:
286: return "BITOR";
287: case BITXOR:
288: return "BITXOR";
289: case BITAND:
290: return "BITAND";
291: case EQ:
292: return "EQ";
293: case NE:
294: return "NE";
295: case LT:
296: return "LT";
297: case LE:
298: return "LE";
299: case GT:
300: return "GT";
301: case GE:
302: return "GE";
303: case LSH:
304: return "LSH";
305: case RSH:
306: return "RSH";
307: case URSH:
308: return "URSH";
309: case ADD:
310: return "ADD";
311: case SUB:
312: return "SUB";
313: case MUL:
314: return "MUL";
315: case DIV:
316: return "DIV";
317: case MOD:
318: return "MOD";
319: case NOT:
320: return "NOT";
321: case BITNOT:
322: return "BITNOT";
323: case POS:
324: return "POS";
325: case NEG:
326: return "NEG";
327: case NEW:
328: return "NEW";
329: case DELPROP:
330: return "DELPROP";
331: case TYPEOF:
332: return "TYPEOF";
333: case GETPROP:
334: return "GETPROP";
335: case SETPROP:
336: return "SETPROP";
337: case GETELEM:
338: return "GETELEM";
339: case SETELEM:
340: return "SETELEM";
341: case CALL:
342: return "CALL";
343: case NAME:
344: return "NAME";
345: case NUMBER:
346: return "NUMBER";
347: case STRING:
348: return "STRING";
349: case NULL:
350: return "NULL";
351: case THIS:
352: return "THIS";
353: case FALSE:
354: return "FALSE";
355: case TRUE:
356: return "TRUE";
357: case SHEQ:
358: return "SHEQ";
359: case SHNE:
360: return "SHNE";
361: // <netbeans>
362: // This was a bug, right?
363: //case REGEXP: return "OBJECT";
364: case REGEXP:
365: return "REGEXP";
366: // </netbeans>
367: case BINDNAME:
368: return "BINDNAME";
369: case THROW:
370: return "THROW";
371: case RETHROW:
372: return "RETHROW";
373: case IN:
374: return "IN";
375: case INSTANCEOF:
376: return "INSTANCEOF";
377: case LOCAL_LOAD:
378: return "LOCAL_LOAD";
379: case GETVAR:
380: return "GETVAR";
381: case SETVAR:
382: return "SETVAR";
383: case CATCH_SCOPE:
384: return "CATCH_SCOPE";
385: case ENUM_INIT_KEYS:
386: return "ENUM_INIT_KEYS";
387: case ENUM_INIT_VALUES:
388: return "ENUM_INIT_VALUES";
389: case ENUM_NEXT:
390: return "ENUM_NEXT";
391: case ENUM_ID:
392: return "ENUM_ID";
393: case THISFN:
394: return "THISFN";
395: case RETURN_RESULT:
396: return "RETURN_RESULT";
397: case ARRAYLIT:
398: return "ARRAYLIT";
399: case OBJECTLIT:
400: return "OBJECTLIT";
401: case GET_REF:
402: return "GET_REF";
403: case SET_REF:
404: return "SET_REF";
405: case DEL_REF:
406: return "DEL_REF";
407: case REF_CALL:
408: return "REF_CALL";
409: case REF_SPECIAL:
410: return "REF_SPECIAL";
411: case DEFAULTNAMESPACE:
412: return "DEFAULTNAMESPACE";
413: case ESCXMLTEXT:
414: return "ESCXMLTEXT";
415: case ESCXMLATTR:
416: return "ESCXMLATTR";
417: case REF_MEMBER:
418: return "REF_MEMBER";
419: case REF_NS_MEMBER:
420: return "REF_NS_MEMBER";
421: case REF_NAME:
422: return "REF_NAME";
423: case REF_NS_NAME:
424: return "REF_NS_NAME";
425: case TRY:
426: return "TRY";
427: case SEMI:
428: return "SEMI";
429: case LB:
430: return "LB";
431: case RB:
432: return "RB";
433: case LC:
434: return "LC";
435: case RC:
436: return "RC";
437: case LP:
438: return "LP";
439: case RP:
440: return "RP";
441: case COMMA:
442: return "COMMA";
443: case ASSIGN:
444: return "ASSIGN";
445: case ASSIGN_BITOR:
446: return "ASSIGN_BITOR";
447: case ASSIGN_BITXOR:
448: return "ASSIGN_BITXOR";
449: case ASSIGN_BITAND:
450: return "ASSIGN_BITAND";
451: case ASSIGN_LSH:
452: return "ASSIGN_LSH";
453: case ASSIGN_RSH:
454: return "ASSIGN_RSH";
455: case ASSIGN_URSH:
456: return "ASSIGN_URSH";
457: case ASSIGN_ADD:
458: return "ASSIGN_ADD";
459: case ASSIGN_SUB:
460: return "ASSIGN_SUB";
461: case ASSIGN_MUL:
462: return "ASSIGN_MUL";
463: case ASSIGN_DIV:
464: return "ASSIGN_DIV";
465: case ASSIGN_MOD:
466: return "ASSIGN_MOD";
467: case HOOK:
468: return "HOOK";
469: case COLON:
470: return "COLON";
471: case OR:
472: return "OR";
473: case AND:
474: return "AND";
475: case INC:
476: return "INC";
477: case DEC:
478: return "DEC";
479: case DOT:
480: return "DOT";
481: case FUNCTION:
482: return "FUNCTION";
483: case EXPORT:
484: return "EXPORT";
485: case IMPORT:
486: return "IMPORT";
487: case IF:
488: return "IF";
489: case ELSE:
490: return "ELSE";
491: case SWITCH:
492: return "SWITCH";
493: case CASE:
494: return "CASE";
495: case DEFAULT:
496: return "DEFAULT";
497: case WHILE:
498: return "WHILE";
499: case DO:
500: return "DO";
501: case FOR:
502: return "FOR";
503: case BREAK:
504: return "BREAK";
505: case CONTINUE:
506: return "CONTINUE";
507: case VAR:
508: return "VAR";
509: case WITH:
510: return "WITH";
511: case CATCH:
512: return "CATCH";
513: case FINALLY:
514: return "FINALLY";
515: case RESERVED:
516: return "RESERVED";
517: case EMPTY:
518: return "EMPTY";
519: case BLOCK:
520: return "BLOCK";
521: case LABEL:
522: return "LABEL";
523: case TARGET:
524: return "TARGET";
525: case LOOP:
526: return "LOOP";
527: case EXPR_VOID:
528: return "EXPR_VOID";
529: case EXPR_RESULT:
530: return "EXPR_RESULT";
531: case JSR:
532: return "JSR";
533: case SCRIPT:
534: return "SCRIPT";
535: case TYPEOFNAME:
536: return "TYPEOFNAME";
537: case USE_STACK:
538: return "USE_STACK";
539: case SETPROP_OP:
540: return "SETPROP_OP";
541: case SETELEM_OP:
542: return "SETELEM_OP";
543: case LOCAL_BLOCK:
544: return "LOCAL_BLOCK";
545: case SET_REF_OP:
546: return "SET_REF_OP";
547: case DOTDOT:
548: return "DOTDOT";
549: case COLONCOLON:
550: return "COLONCOLON";
551: case XML:
552: return "XML";
553: case DOTQUERY:
554: return "DOTQUERY";
555: case XMLATTR:
556: return "XMLATTR";
557: case XMLEND:
558: return "XMLEND";
559: case TO_OBJECT:
560: return "TO_OBJECT";
561: case TO_DOUBLE:
562: return "TO_DOUBLE";
563: case GET:
564: return "GET";
565: case SET:
566: return "SET";
567: case CONST:
568: return "CONST";
569: case SETCONST:
570: return "SETCONST";
571: // <netbeans>
572: case LINE_COMMENT:
573: return "LINE_COMMENT";
574: case BLOCK_COMMENT:
575: return "BLOCK_COMMENT";
576: case WHITESPACE:
577: return "WHITESPACE";
578: case PARAMETER:
579: return "PARAMETER";
580: case FUNCNAME:
581: return "FUNCNAME";
582: case OBJLITNAME:
583: return "OBJLITNAME";
584: case STRING_BEGIN:
585: return "STRING_BEGIN";
586: case STRING_END:
587: return "STRING_END";
588: case STRING_ERROR:
589: return "STRING_ERROR";
590: case REGEXP_BEGIN:
591: return "REGEXP_BEGIN";
592: case REGEXP_END:
593: return "REGEXP_END";
594: case REGEXP_ERROR:
595: return "REGEXP_ERROR";
596: case UNDEFINED:
597: return "UNDEFINED";
598: // </netbeans>
599: }
600:
601: // Token without name
602: throw new IllegalStateException(String.valueOf(token));
603: }
604:
605: // <netbeans>
606: public static String fullName(int token) {
607: boolean oldPrintNames = printNames;
608: printNames = true;
609: String result = name(token);
610: printNames = oldPrintNames;
611:
612: return result;
613: }
614: // </netbeans>
615: }
|