1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is Rhino code, released
19
* The Initial Developer of the Original Code is
20
* Netscape Communications Corporation.
21
* Portions created by the Initial Developer are Copyright (C) 1997-1999
22
* the Initial Developer. All Rights Reserved.
31
* Alternatively, the contents of this file may be used under the terms of
32
* the GNU General Public License Version 2 or later (the "GPL"), in which
33
* case the provisions of the GPL are applicable instead of those above. If
34
* you wish to allow use of your version of this file only under the terms of
35
* the GPL and not to allow others to use your version of this file under the
36
* MPL, indicate your decision by deleting the provisions above and replacing
37
* them with the notice and other provisions required by the GPL. If you do
38
* not delete the provisions above, a recipient may use your version of this
39
* file under either the MPL or the GPL.
41
* ***** END LICENSE BLOCK ***** */
43
package org.mozilla.javascript;
46
* This class implements the JavaScript scanner.
48
* It is based on the C source files jsscan.c and jsscan.h
49
* in the jsref package.
51
* @see org.mozilla.javascript.Parser
54
* @author Brendan Eich
61
public static final boolean printTrees = false;
62
static final boolean printICode = false;
63
static final boolean printNames = printTrees || printICode;
66
* Token types. These values correspond to JSTokenType values in
70
public final static int
72
ERROR = -1, // well-known as the only code < EOF
73
EOF = 0, // end of file token - (not EOF_CHAR)
74
EOL = 1, // end of line
76
// Interpreter reuses the following as bytecodes
77
FIRST_BYTECODE_TOKEN = 2,
123
SHEQ = 46, // shallow equality (===)
124
SHNE = 47, // shallow inequality (!==)
128
RETHROW = 51, // rethrow caught exception: catch (e if ) use it
136
ENUM_INIT_VALUES = 59,
141
RETURN_RESULT = 64, // to return previously stored return result
142
ARRAYLIT = 65, // array literal
143
OBJECTLIT = 66, // object literal
144
GET_REF = 67, // *reference
145
SET_REF = 68, // *reference = something
146
DEL_REF = 69, // delete reference
147
REF_CALL = 70, // f(args) = something or f(args)++
148
REF_SPECIAL = 71, // reference for special properties like __proto
149
YIELD = 72, // JS 1.7 yield pseudo keyword
152
DEFAULTNAMESPACE = 73, // default xml namespace =
155
REF_MEMBER = 76, // Reference for x.@y, x..y etc.
156
REF_NS_MEMBER = 77, // Reference for x.ns::y, x..ns::y etc.
157
REF_NAME = 78, // Reference for @y, @[y] etc.
158
REF_NS_NAME = 79; // Reference for ns::y, @ns::y@[y] etc.
160
// End of interpreter bytecodes
161
public final static int
162
LAST_BYTECODE_TOKEN = REF_NS_NAME,
165
SEMI = 81, // semicolon
166
LB = 82, // left and right brackets
168
LC = 84, // left and right curlies (braces)
170
LP = 86, // left and right parentheses
172
COMMA = 88, // comma operator
174
ASSIGN = 89, // simple assignment (=)
175
ASSIGN_BITOR = 90, // |=
176
ASSIGN_BITXOR = 91, // ^=
177
ASSIGN_BITAND = 92, // |=
178
ASSIGN_LSH = 93, // <<=
179
ASSIGN_RSH = 94, // >>=
180
ASSIGN_URSH = 95, // >>>=
181
ASSIGN_ADD = 96, // +=
182
ASSIGN_SUB = 97, // -=
183
ASSIGN_MUL = 98, // *=
184
ASSIGN_DIV = 99, // /=
185
ASSIGN_MOD = 100; // %=
187
public final static int
188
FIRST_ASSIGN = ASSIGN,
189
LAST_ASSIGN = ASSIGN_MOD,
191
HOOK = 101, // conditional (?:)
193
OR = 103, // logical or (||)
194
AND = 104, // logical and (&&)
195
INC = 105, // increment/decrement (++ --)
197
DOT = 107, // member operator (.)
198
FUNCTION = 108, // function keyword
199
EXPORT = 109, // export keyword
200
IMPORT = 110, // import keyword
201
IF = 111, // if keyword
202
ELSE = 112, // else keyword
203
SWITCH = 113, // switch keyword
204
CASE = 114, // case keyword
205
DEFAULT = 115, // default keyword
206
WHILE = 116, // while keyword
207
DO = 117, // do keyword
208
FOR = 118, // for keyword
209
BREAK = 119, // break keyword
210
CONTINUE = 120, // continue keyword
211
VAR = 121, // var keyword
212
WITH = 122, // with keyword
213
CATCH = 123, // catch keyword
214
FINALLY = 124, // finally keyword
215
VOID = 125, // void keyword
216
RESERVED = 126, // reserved keywords
220
/* types used for the parse tree - these never get returned
224
BLOCK = 128, // statement block
225
LABEL = 129, // label
228
EXPR_VOID = 132, // expression statement in functions
229
EXPR_RESULT = 133, // expression statement in scripts
231
SCRIPT = 135, // top-level node for entire script
232
TYPEOFNAME = 136, // for typeof(simple-name)
234
SETPROP_OP = 138, // x.y op= something
235
SETELEM_OP = 139, // x[y] op= something
237
SET_REF_OP = 141, // *reference op= something
240
DOTDOT = 142, // member operator (..)
241
COLONCOLON = 143, // namespace::name
242
XML = 144, // XML type
243
DOTQUERY = 145, // .() -- e.g., x.emps.emp.(name == "terry")
247
// Optimizer-only-tokens
251
GET = 150, // JS 1.5 get pseudo keyword
252
SET = 151, // JS 1.5 set pseudo keyword
253
LET = 152, // JS 1.7 let pseudo keyword
257
ARRAYCOMP = 156, // array comprehension
263
public static String name(int token)
266
return String.valueOf(token);
269
case ERROR: return "ERROR";
270
case EOF: return "EOF";
271
case EOL: return "EOL";
272
case ENTERWITH: return "ENTERWITH";
273
case LEAVEWITH: return "LEAVEWITH";
274
case RETURN: return "RETURN";
275
case GOTO: return "GOTO";
276
case IFEQ: return "IFEQ";
277
case IFNE: return "IFNE";
278
case SETNAME: return "SETNAME";
279
case BITOR: return "BITOR";
280
case BITXOR: return "BITXOR";
281
case BITAND: return "BITAND";
282
case EQ: return "EQ";
283
case NE: return "NE";
284
case LT: return "LT";
285
case LE: return "LE";
286
case GT: return "GT";
287
case GE: return "GE";
288
case LSH: return "LSH";
289
case RSH: return "RSH";
290
case URSH: return "URSH";
291
case ADD: return "ADD";
292
case SUB: return "SUB";
293
case MUL: return "MUL";
294
case DIV: return "DIV";
295
case MOD: return "MOD";
296
case NOT: return "NOT";
297
case BITNOT: return "BITNOT";
298
case POS: return "POS";
299
case NEG: return "NEG";
300
case NEW: return "NEW";
301
case DELPROP: return "DELPROP";
302
case TYPEOF: return "TYPEOF";
303
case GETPROP: return "GETPROP";
304
case GETPROPNOWARN: return "GETPROPNOWARN";
305
case SETPROP: return "SETPROP";
306
case GETELEM: return "GETELEM";
307
case SETELEM: return "SETELEM";
308
case CALL: return "CALL";
309
case NAME: return "NAME";
310
case NUMBER: return "NUMBER";
311
case STRING: return "STRING";
312
case NULL: return "NULL";
313
case THIS: return "THIS";
314
case FALSE: return "FALSE";
315
case TRUE: return "TRUE";
316
case SHEQ: return "SHEQ";
317
case SHNE: return "SHNE";
318
case REGEXP: return "OBJECT";
319
case BINDNAME: return "BINDNAME";
320
case THROW: return "THROW";
321
case RETHROW: return "RETHROW";
322
case IN: return "IN";
323
case INSTANCEOF: return "INSTANCEOF";
324
case LOCAL_LOAD: return "LOCAL_LOAD";
325
case GETVAR: return "GETVAR";
326
case SETVAR: return "SETVAR";
327
case CATCH_SCOPE: return "CATCH_SCOPE";
328
case ENUM_INIT_KEYS: return "ENUM_INIT_KEYS";
329
case ENUM_INIT_VALUES:return "ENUM_INIT_VALUES";
330
case ENUM_INIT_ARRAY: return "ENUM_INIT_ARRAY";
331
case ENUM_NEXT: return "ENUM_NEXT";
332
case ENUM_ID: return "ENUM_ID";
333
case THISFN: return "THISFN";
334
case RETURN_RESULT: return "RETURN_RESULT";
335
case ARRAYLIT: return "ARRAYLIT";
336
case OBJECTLIT: return "OBJECTLIT";
337
case GET_REF: return "GET_REF";
338
case SET_REF: return "SET_REF";
339
case DEL_REF: return "DEL_REF";
340
case REF_CALL: return "REF_CALL";
341
case REF_SPECIAL: return "REF_SPECIAL";
342
case DEFAULTNAMESPACE:return "DEFAULTNAMESPACE";
343
case ESCXMLTEXT: return "ESCXMLTEXT";
344
case ESCXMLATTR: return "ESCXMLATTR";
345
case REF_MEMBER: return "REF_MEMBER";
346
case REF_NS_MEMBER: return "REF_NS_MEMBER";
347
case REF_NAME: return "REF_NAME";
348
case REF_NS_NAME: return "REF_NS_NAME";
349
case TRY: return "TRY";
350
case SEMI: return "SEMI";
351
case LB: return "LB";
352
case RB: return "RB";
353
case LC: return "LC";
354
case RC: return "RC";
355
case LP: return "LP";
356
case RP: return "RP";
357
case COMMA: return "COMMA";
358
case ASSIGN: return "ASSIGN";
359
case ASSIGN_BITOR: return "ASSIGN_BITOR";
360
case ASSIGN_BITXOR: return "ASSIGN_BITXOR";
361
case ASSIGN_BITAND: return "ASSIGN_BITAND";
362
case ASSIGN_LSH: return "ASSIGN_LSH";
363
case ASSIGN_RSH: return "ASSIGN_RSH";
364
case ASSIGN_URSH: return "ASSIGN_URSH";
365
case ASSIGN_ADD: return "ASSIGN_ADD";
366
case ASSIGN_SUB: return "ASSIGN_SUB";
367
case ASSIGN_MUL: return "ASSIGN_MUL";
368
case ASSIGN_DIV: return "ASSIGN_DIV";
369
case ASSIGN_MOD: return "ASSIGN_MOD";
370
case HOOK: return "HOOK";
371
case COLON: return "COLON";
372
case OR: return "OR";
373
case AND: return "AND";
374
case INC: return "INC";
375
case DEC: return "DEC";
376
case DOT: return "DOT";
377
case FUNCTION: return "FUNCTION";
378
case EXPORT: return "EXPORT";
379
case IMPORT: return "IMPORT";
380
case IF: return "IF";
381
case ELSE: return "ELSE";
382
case SWITCH: return "SWITCH";
383
case CASE: return "CASE";
384
case DEFAULT: return "DEFAULT";
385
case WHILE: return "WHILE";
386
case DO: return "DO";
387
case FOR: return "FOR";
388
case BREAK: return "BREAK";
389
case CONTINUE: return "CONTINUE";
390
case VAR: return "VAR";
391
case WITH: return "WITH";
392
case CATCH: return "CATCH";
393
case FINALLY: return "FINALLY";
394
case VOID: return "VOID";
395
case RESERVED: return "RESERVED";
396
case EMPTY: return "EMPTY";
397
case BLOCK: return "BLOCK";
398
case LABEL: return "LABEL";
399
case TARGET: return "TARGET";
400
case LOOP: return "LOOP";
401
case EXPR_VOID: return "EXPR_VOID";
402
case EXPR_RESULT: return "EXPR_RESULT";
403
case JSR: return "JSR";
404
case SCRIPT: return "SCRIPT";
405
case TYPEOFNAME: return "TYPEOFNAME";
406
case USE_STACK: return "USE_STACK";
407
case SETPROP_OP: return "SETPROP_OP";
408
case SETELEM_OP: return "SETELEM_OP";
409
case LOCAL_BLOCK: return "LOCAL_BLOCK";
410
case SET_REF_OP: return "SET_REF_OP";
411
case DOTDOT: return "DOTDOT";
412
case COLONCOLON: return "COLONCOLON";
413
case XML: return "XML";
414
case DOTQUERY: return "DOTQUERY";
415
case XMLATTR: return "XMLATTR";
416
case XMLEND: return "XMLEND";
417
case TO_OBJECT: return "TO_OBJECT";
418
case TO_DOUBLE: return "TO_DOUBLE";
419
case GET: return "GET";
420
case SET: return "SET";
421
case LET: return "LET";
422
case YIELD: return "YIELD";
423
case CONST: return "CONST";
424
case SETCONST: return "SETCONST";
425
case ARRAYCOMP: return "ARRAYCOMP";
426
case WITHEXPR: return "WITHEXPR";
427
case LETEXPR: return "LETEXPR";
428
case DEBUGGER: return "DEBUGGER";
431
// Token without name
432
throw new IllegalStateException(String.valueOf(token));