1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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 Mozilla Communicator client 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) 1998
22
* the Initial Developer. All Rights Reserved.
26
* Alternatively, the contents of this file may be used under the terms of
27
* either of the GNU General Public License Version 2 or later (the "GPL"),
28
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
44
#include <stdio.h> /* first to avoid trouble on some systems */
55
#include "jsarena.h" /* Added by JSIFY */
56
#include "jsutil.h" /* Added by JSIFY */
70
#define RESERVE_JAVA_KEYWORDS
71
#define RESERVE_ECMA_KEYWORDS
73
static struct keyword {
75
JSTokenType tokentype; /* JSTokenType */
77
JSVersion version; /* JSVersion */
79
{"break", TOK_BREAK, JSOP_NOP, JSVERSION_DEFAULT},
80
{"case", TOK_CASE, JSOP_NOP, JSVERSION_DEFAULT},
81
{"continue", TOK_CONTINUE, JSOP_NOP, JSVERSION_DEFAULT},
82
{"default", TOK_DEFAULT, JSOP_NOP, JSVERSION_DEFAULT},
83
{js_delete_str, TOK_DELETE, JSOP_NOP, JSVERSION_DEFAULT},
84
{"do", TOK_DO, JSOP_NOP, JSVERSION_DEFAULT},
85
{"else", TOK_ELSE, JSOP_NOP, JSVERSION_DEFAULT},
86
{"export", TOK_EXPORT, JSOP_NOP, JSVERSION_1_2},
87
{js_false_str, TOK_PRIMARY, JSOP_FALSE, JSVERSION_DEFAULT},
88
{"for", TOK_FOR, JSOP_NOP, JSVERSION_DEFAULT},
89
{js_function_str, TOK_FUNCTION, JSOP_NOP, JSVERSION_DEFAULT},
90
{"if", TOK_IF, JSOP_NOP, JSVERSION_DEFAULT},
91
{js_in_str, TOK_IN, JSOP_IN, JSVERSION_DEFAULT},
92
{js_new_str, TOK_NEW, JSOP_NEW, JSVERSION_DEFAULT},
93
{js_null_str, TOK_PRIMARY, JSOP_NULL, JSVERSION_DEFAULT},
94
{"return", TOK_RETURN, JSOP_NOP, JSVERSION_DEFAULT},
95
{"switch", TOK_SWITCH, JSOP_NOP, JSVERSION_DEFAULT},
96
{js_this_str, TOK_PRIMARY, JSOP_THIS, JSVERSION_DEFAULT},
97
{js_true_str, TOK_PRIMARY, JSOP_TRUE, JSVERSION_DEFAULT},
98
{js_typeof_str, TOK_UNARYOP, JSOP_TYPEOF,JSVERSION_DEFAULT},
99
{"var", TOK_VAR, JSOP_DEFVAR,JSVERSION_DEFAULT},
100
{js_void_str, TOK_UNARYOP, JSOP_VOID, JSVERSION_DEFAULT},
101
{"while", TOK_WHILE, JSOP_NOP, JSVERSION_DEFAULT},
102
{"with", TOK_WITH, JSOP_NOP, JSVERSION_DEFAULT},
105
{js_const_str, TOK_VAR, JSOP_DEFCONST,JSVERSION_DEFAULT},
107
{js_const_str, TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
110
#if JS_HAS_EXCEPTIONS
111
{"try", TOK_TRY, JSOP_NOP, JSVERSION_DEFAULT},
112
{"catch", TOK_CATCH, JSOP_NOP, JSVERSION_DEFAULT},
113
{"finally", TOK_FINALLY, JSOP_NOP, JSVERSION_DEFAULT},
114
{"throw", TOK_THROW, JSOP_NOP, JSVERSION_DEFAULT},
116
{"try", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
117
{"catch", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
118
{"finally", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
119
{"throw", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
122
#if JS_HAS_INSTANCEOF
123
{js_instanceof_str, TOK_INSTANCEOF, JSOP_INSTANCEOF,JSVERSION_1_4},
125
{js_instanceof_str, TOK_RESERVED, JSOP_NOP, JSVERSION_1_4},
128
#ifdef RESERVE_JAVA_KEYWORDS
129
{"abstract", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
130
{"boolean", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
131
{"byte", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
132
{"char", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
133
{"class", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
134
{"double", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
135
{"extends", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
136
{"final", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
137
{"float", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
138
{"goto", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
139
{"implements", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
140
{"import", TOK_IMPORT, JSOP_NOP, JSVERSION_DEFAULT},
141
{"int", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
142
{"interface", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
143
{"long", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
144
{"native", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
145
{"package", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
146
{"private", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
147
{"protected", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
148
{"public", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
149
{"short", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
150
{"static", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
151
{"super", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
152
{"synchronized", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
153
{"throws", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
154
{"transient", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
155
{"volatile", TOK_RESERVED, JSOP_NOP, JSVERSION_DEFAULT},
158
#ifdef RESERVE_ECMA_KEYWORDS
159
{"enum", TOK_RESERVED, JSOP_NOP, JSVERSION_1_3},
162
#if JS_HAS_DEBUGGER_KEYWORD
163
{"debugger", TOK_DEBUGGER, JSOP_NOP, JSVERSION_1_3},
164
#elif defined(RESERVE_ECMA_KEYWORDS)
165
{"debugger", TOK_RESERVED, JSOP_NOP, JSVERSION_1_3},
167
{0, TOK_EOF, JSOP_NOP, JSVERSION_DEFAULT}
171
js_InitScanner(JSContext *cx)
176
for (kw = keywords; kw->name; kw++) {
177
atom = js_Atomize(cx, kw->name, strlen(kw->name), ATOM_PINNED);
180
ATOM_SET_KEYWORD(atom, kw);
186
js_MapKeywords(void (*mapfun)(const char *))
190
for (kw = keywords; kw->name; kw++)
195
js_NewTokenStream(JSContext *cx, const jschar *base, size_t length,
196
const char *filename, uintN lineno,
197
JSPrincipals *principals)
201
ts = js_NewBufferTokenStream(cx, base, length);
204
ts->filename = filename;
207
JSPRINCIPALS_HOLD(cx, principals);
208
ts->principals = principals;
212
JS_FRIEND_API(JSTokenStream *)
213
js_NewBufferTokenStream(JSContext *cx, const jschar *base, size_t length)
218
nb = sizeof(JSTokenStream) + JS_LINE_LIMIT * sizeof(jschar);
219
JS_ARENA_ALLOCATE_CAST(ts, JSTokenStream *, &cx->tempPool, nb);
221
JS_ReportOutOfMemory(cx);
226
ts->linebuf.base = ts->linebuf.limit = ts->linebuf.ptr = (jschar *)(ts + 1);
227
ts->userbuf.base = (jschar *)base;
228
ts->userbuf.limit = (jschar *)base + length;
229
ts->userbuf.ptr = (jschar *)base;
230
ts->listener = cx->runtime->sourceHandler;
231
ts->listenerData = cx->runtime->sourceHandlerData;
235
JS_FRIEND_API(JSTokenStream *)
236
js_NewFileTokenStream(JSContext *cx, const char *filename, FILE *defaultfp)
242
JS_ARENA_ALLOCATE_CAST(base, jschar *, &cx->tempPool,
243
JS_LINE_LIMIT * sizeof(jschar));
246
ts = js_NewBufferTokenStream(cx, base, JS_LINE_LIMIT);
249
if (!filename || strcmp(filename, "-") == 0) {
252
file = fopen(filename, "r");
254
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_OPEN,
255
filename, "No such file or directory");
259
ts->userbuf.ptr = ts->userbuf.limit;
261
ts->filename = filename;
265
JS_FRIEND_API(JSBool)
266
js_CloseTokenStream(JSContext *cx, JSTokenStream *ts)
269
JSPRINCIPALS_DROP(cx, ts->principals);
270
return !ts->file || fclose(ts->file) == 0;
274
my_fgets(char *buf, int size, FILE *file)
284
for (i = 0; i < n && (c = getc(file)) != EOF; i++) {
286
if (c == '\n') { /* any \n ends a line */
287
i++; /* keep the \n; we know there is room for \0 */
290
if (crflag) { /* \r not followed by \n ends line at the \r */
292
break; /* and overwrite c in buf with \0 */
294
crflag = (c == '\r');
302
GetChar(JSTokenStream *ts)
305
ptrdiff_t i, j, len, olen;
307
char cbuf[JS_LINE_LIMIT];
310
if (ts->ungetpos != 0) {
311
c = ts->ungetbuf[--ts->ungetpos];
314
if (ts->linebuf.ptr == ts->linebuf.limit) {
315
len = PTRDIFF(ts->userbuf.limit, ts->userbuf.ptr, jschar);
318
ts->flags |= TSF_EOF;
322
/* Fill ts->userbuf so that \r and \r\n convert to \n. */
323
crflag = (ts->flags & TSF_CRFLAG) != 0;
324
len = my_fgets(cbuf, JS_LINE_LIMIT - crflag, ts->file);
326
ts->flags |= TSF_EOF;
330
ubuf = ts->userbuf.base;
333
ts->flags &= ~TSF_CRFLAG;
334
if (cbuf[0] != '\n') {
340
for (j = 0; i < len; i++, j++)
341
ubuf[i] = (jschar) (unsigned char) cbuf[j];
342
ts->userbuf.limit = ubuf + len;
343
ts->userbuf.ptr = ubuf;
346
ts->listener(ts->filename, ts->lineno, ts->userbuf.ptr, len,
347
&ts->listenerTSData, ts->listenerData);
351
* Any one of \n, \r, or \r\n ends a line (longest match wins).
352
* Also allow the Unicode line and paragraph separators.
354
for (nl = ts->userbuf.ptr; nl < ts->userbuf.limit; nl++) {
356
* Try to prevent value-testing on most characters by
357
* filtering out characters that aren't 000x or 202x.
359
if ((*nl & 0xDFD0) == 0) {
363
if (nl + 1 < ts->userbuf.limit && nl[1] == '\n')
367
if (*nl == LINE_SEPARATOR || *nl == PARA_SEPARATOR)
373
* If there was a line terminator, copy thru it into linebuf.
374
* Else copy JS_LINE_LIMIT-1 bytes into linebuf.
376
if (nl < ts->userbuf.limit)
377
len = PTRDIFF(nl, ts->userbuf.ptr, jschar) + 1;
378
if (len >= JS_LINE_LIMIT)
379
len = JS_LINE_LIMIT - 1;
380
js_strncpy(ts->linebuf.base, ts->userbuf.ptr, len);
381
ts->userbuf.ptr += len;
385
* Make sure linebuf contains \n for EOL (don't do this in
386
* userbuf because the user's string might be readonly).
388
if (nl < ts->userbuf.limit) {
390
if (ts->linebuf.base[len-1] == '\r') {
392
* Does the line segment end in \r? We must check
393
* for a \n at the front of the next segment before
394
* storing a \n into linebuf. This case matters
395
* only when we're reading from a file.
397
if (nl + 1 == ts->userbuf.limit && ts->file) {
399
ts->flags |= TSF_CRFLAG; /* clear NLFLAG? */
402
* This can happen when a segment ends in
403
* \r\r. Start over. ptr == limit in this
404
* case, so we'll fall into buffer-filling
410
ts->linebuf.base[len-1] = '\n';
413
} else if (*nl == '\n') {
414
if (nl > ts->userbuf.base &&
416
ts->linebuf.base[len-2] == '\r') {
418
JS_ASSERT(ts->linebuf.base[len] == '\n');
419
ts->linebuf.base[len-1] = '\n';
421
} else if (*nl == LINE_SEPARATOR || *nl == PARA_SEPARATOR) {
422
ts->linebuf.base[len-1] = '\n';
426
/* Reset linebuf based on adjusted segment length. */
427
ts->linebuf.limit = ts->linebuf.base + len;
428
ts->linebuf.ptr = ts->linebuf.base;
430
/* Update position of linebuf within physical userbuf line. */
431
if (!(ts->flags & TSF_NLFLAG))
432
ts->linepos += ts->linelen;
435
if (ts->linebuf.limit[-1] == '\n')
436
ts->flags |= TSF_NLFLAG;
438
ts->flags &= ~TSF_NLFLAG;
440
/* Update linelen from original segment length. */
443
c = *ts->linebuf.ptr++;
444
} while (JS_ISFORMAT(c));
452
UngetChar(JSTokenStream *ts, int32 c)
456
JS_ASSERT(ts->ungetpos < sizeof ts->ungetbuf / sizeof ts->ungetbuf[0]);
459
ts->ungetbuf[ts->ungetpos++] = (jschar)c;
463
PeekChar(JSTokenStream *ts)
473
PeekChars(JSTokenStream *ts, intN n, jschar *cp)
478
for (i = 0; i < n; i++) {
484
for (j = i - 1; j >= 0; j--)
485
UngetChar(ts, cp[j]);
490
SkipChars(JSTokenStream *ts, intN n)
497
MatchChar(JSTokenStream *ts, int32 expect)
509
js_ReportCompileErrorNumber(JSContext *cx, JSTokenStream *ts,
510
JSCodeGenerator *cg, uintN flags,
511
const uintN errorNumber, ...)
514
JSErrorReporter onError;
515
JSErrorReport report;
517
JSString *linestr = NULL;
521
if ((flags & JSREPORT_STRICT) && !JS_HAS_STRICT_OPTION(cx))
524
memset(&report, 0, sizeof (struct JSErrorReport));
525
report.flags = flags;
526
report.errorNumber = errorNumber;
529
va_start(ap, errorNumber);
530
if (!js_ExpandErrorArguments(cx, js_GetErrorMessage, NULL,
531
errorNumber, &message, &report, &warning,
537
js_AddRoot(cx, &linestr, "error line buffer");
539
JS_ASSERT(!ts || ts->linebuf.limit < ts->linebuf.base + JS_LINE_LIMIT);
540
onError = cx->errorReporter;
543
* We are typically called with non-null ts and null cg from jsparse.c.
544
* We can be called with null ts from the regexp compilation functions.
545
* The code generator (jsemit.c) may pass null ts and non-null cg.
548
report.filename = ts->filename;
549
report.lineno = ts->lineno;
550
linestr = js_NewStringCopyN(cx, ts->linebuf.base,
551
ts->linebuf.limit - ts->linebuf.base,
553
report.linebuf = linestr
554
? JS_GetStringBytes(linestr)
557
ts->tokens[(ts->cursor + ts->lookahead) & NTOKENS_MASK].ptr;
558
report.tokenptr = linestr
559
? report.linebuf + (tokenptr - ts->linebuf.base)
561
report.uclinebuf = linestr
562
? JS_GetStringChars(linestr)
564
report.uctokenptr = linestr
565
? report.uclinebuf + (tokenptr - ts->linebuf.base)
568
report.filename = cg->filename;
569
report.lineno = CG_CURRENT_LINE(cg);
572
#if JS_HAS_ERROR_EXCEPTIONS
574
* If there's a runtime exception type associated with this error
575
* number, set that as the pending exception. For errors occuring at
576
* compile time, this is very likely to be a JSEXN_SYNTAXERR.
578
* If an exception is thrown but not caught, the JSREPORT_EXCEPTION
579
* flag will be set in report.flags. Proper behavior for an error
580
* reporter is to ignore a report with this flag for all but top-level
581
* compilation errors. The exception will remain pending, and so long
582
* as the non-top-level "load", "eval", or "compile" native function
583
* returns false, the top-level reporter will eventually receive the
584
* uncaught exception report.
586
* XXX it'd probably be best if there was only one call to this
587
* function, but there seem to be two error reporter call points.
591
* Only try to raise an exception if there isn't one already set -
592
* otherwise the exception will describe only the last compile error,
593
* which is likely spurious.
595
if (!(ts && (ts->flags & TSF_ERROR)))
596
if (js_ErrorToException(cx, message, &report))
600
* Suppress any compiletime errors that don't occur at the top level.
601
* This may still fail, as interplevel may be zero in contexts where we
602
* don't really want to call the error reporter, as when js is called
603
* by other code which could catch the error.
605
if (cx->interpLevel != 0)
608
if (cx->runtime->debugErrorHook && onError) {
609
JSDebugErrorHook hook = cx->runtime->debugErrorHook;
610
/* test local in case debugErrorHook changed on another thread */
611
if (hook && !hook(cx, message, &report,
612
cx->runtime->debugErrorHookData)) {
617
(*onError)(cx, message, &report);
620
JS_free(cx, message);
621
if (report.messageArgs) {
623
while (report.messageArgs[i])
624
JS_free(cx, (void *)report.messageArgs[i++]);
625
JS_free(cx, (void *)report.messageArgs);
627
if (report.ucmessage)
628
JS_free(cx, (void *)report.ucmessage);
630
js_RemoveRoot(cx->runtime, &linestr);
632
if (ts && !JSREPORT_IS_WARNING(flags)) {
633
/* Set the error flag to suppress spurious reports. */
634
ts->flags |= TSF_ERROR;
640
js_PeekToken(JSContext *cx, JSTokenStream *ts)
644
if (ts->lookahead != 0) {
645
tt = ts->tokens[(ts->cursor + ts->lookahead) & NTOKENS_MASK].type;
647
tt = js_GetToken(cx, ts);
654
js_PeekTokenSameLine(JSContext *cx, JSTokenStream *ts)
658
JS_ASSERT(ts->lookahead == 0 ||
659
ON_CURRENT_LINE(ts, CURRENT_TOKEN(ts).pos));
660
ts->flags |= TSF_NEWLINES;
661
tt = js_PeekToken(cx, ts);
662
ts->flags &= ~TSF_NEWLINES;
669
GrowTokenBuf(JSContext *cx, JSTokenBuf *tb)
672
ptrdiff_t offset, length;
677
offset = PTRDIFF(tb->ptr, base, jschar);
678
pool = &cx->tempPool;
680
tbsize = TBMIN * sizeof(jschar);
682
JS_ARENA_ALLOCATE_CAST(base, jschar *, pool, tbsize);
684
length = PTRDIFF(tb->limit, base, jschar);
685
tbsize = length * sizeof(jschar);
687
JS_ARENA_GROW_CAST(base, jschar *, pool, tbsize, tbsize);
690
JS_ReportOutOfMemory(cx);
694
tb->limit = base + length;
695
tb->ptr = base + offset;
700
AddToTokenBuf(JSContext *cx, JSTokenBuf *tb, jschar c)
702
if (tb->ptr == tb->limit && !GrowTokenBuf(cx, tb))
709
* We have encountered a '\': check for a Unicode escape sequence after it,
710
* returning the character code value if we found a Unicode escape sequence.
711
* Otherwise, non-destructively return the original '\'.
714
GetUnicodeEscape(JSTokenStream *ts)
719
if (PeekChars(ts, 5, cp) && cp[0] == 'u' &&
720
JS7_ISHEX(cp[1]) && JS7_ISHEX(cp[2]) &&
721
JS7_ISHEX(cp[3]) && JS7_ISHEX(cp[4]))
723
c = (((((JS7_UNHEX(cp[1]) << 4)
724
+ JS7_UNHEX(cp[2])) << 4)
725
+ JS7_UNHEX(cp[3])) << 4)
734
js_GetToken(JSContext *cx, JSTokenStream *ts)
740
JSBool hadUnicodeEscape;
742
#define INIT_TOKENBUF(tb) ((tb)->ptr = (tb)->base)
743
#define FINISH_TOKENBUF(tb) if (!AddToTokenBuf(cx, tb, 0)) RETURN(TOK_ERROR)
744
#define TOKEN_LENGTH(tb) ((tb)->ptr - (tb)->base - 1)
745
#define RETURN(tt) { if (tt == TOK_ERROR) ts->flags |= TSF_ERROR; \
746
tp->pos.end.index = ts->linepos + \
747
(ts->linebuf.ptr - ts->linebuf.base) - \
749
return (tp->type = tt); }
751
/* If there was a fatal error, keep returning TOK_ERROR. */
752
if (ts->flags & TSF_ERROR)
755
/* Check for a pushed-back token resulting from mismatching lookahead. */
756
while (ts->lookahead != 0) {
758
ts->cursor = (ts->cursor + 1) & NTOKENS_MASK;
759
tt = CURRENT_TOKEN(ts).type;
760
if (tt != TOK_EOL || (ts->flags & TSF_NEWLINES))
768
ts->flags &= ~TSF_DIRTYLINE;
769
if (ts->flags & TSF_NEWLINES)
772
} while (JS_ISSPACE(c));
774
ts->cursor = (ts->cursor + 1) & NTOKENS_MASK;
775
tp = &CURRENT_TOKEN(ts);
776
tp->ptr = ts->linebuf.ptr - 1;
777
tp->pos.begin.index = ts->linepos + (tp->ptr - ts->linebuf.base);
778
tp->pos.begin.lineno = tp->pos.end.lineno = (uint16)ts->lineno;
782
if (c != '-' && c != '\n')
783
ts->flags |= TSF_DIRTYLINE;
785
hadUnicodeEscape = JS_FALSE;
786
if (JS_ISIDENT_START(c) ||
788
(c = GetUnicodeEscape(ts),
789
hadUnicodeEscape = JS_ISIDENT_START(c)))) {
790
INIT_TOKENBUF(&ts->tokenbuf);
792
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
796
c = GetUnicodeEscape(ts);
799
hadUnicodeEscape = JS_TRUE;
806
FINISH_TOKENBUF(&ts->tokenbuf);
808
atom = js_AtomizeChars(cx,
810
TOKEN_LENGTH(&ts->tokenbuf),
814
if (!hadUnicodeEscape && ATOM_KEYWORD(atom)) {
815
struct keyword *kw = ATOM_KEYWORD(atom);
817
if (JSVERSION_IS_ECMA(cx->version) || kw->version <= cx->version) {
818
tp->t_op = (JSOp) kw->op;
819
RETURN(kw->tokentype);
822
tp->t_op = JSOP_NAME;
827
if (JS7_ISDEC(c) || (c == '.' && JS7_ISDEC(PeekChar(ts)))) {
829
const jschar *endptr;
833
INIT_TOKENBUF(&ts->tokenbuf);
836
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
839
if (JS_TOLOWER(c) == 'x') {
840
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
844
} else if (JS7_ISDEC(c)) {
849
while (JS7_ISHEX(c)) {
855
* We permit 08 and 09 as decimal numbers, which makes our
856
* behaviour a superset of the ECMA numeric grammar. We might
857
* not always be so permissive, so we warn about it.
859
if (radix == 8 && c >= '8') {
860
if (!js_ReportCompileErrorNumber(cx, ts, NULL,
863
c == '8' ? "08" : "09")) {
869
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
874
if (radix == 10 && (c == '.' || JS_TOLOWER(c) == 'e')) {
877
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
880
} while (JS7_ISDEC(c));
882
if (JS_TOLOWER(c) == 'e') {
883
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
886
if (c == '+' || c == '-') {
887
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
892
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
893
JSMSG_MISSING_EXPONENT);
897
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
900
} while (JS7_ISDEC(c));
905
FINISH_TOKENBUF(&ts->tokenbuf);
908
if (!js_strtod(cx, ts->tokenbuf.base, &endptr, &dval)) {
909
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
910
JSMSG_OUT_OF_MEMORY);
914
if (!js_strtointeger(cx, ts->tokenbuf.base, &endptr, radix, &dval)) {
915
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
916
JSMSG_OUT_OF_MEMORY);
924
if (c == '"' || c == '\'') {
927
INIT_TOKENBUF(&ts->tokenbuf);
928
while ((c = GetChar(ts)) != qc) {
929
if (c == '\n' || c == EOF) {
931
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
932
JSMSG_UNTERMINATED_STRING);
936
switch (c = GetChar(ts)) {
937
case 'b': c = '\b'; break;
938
case 'f': c = '\f'; break;
939
case 'n': c = '\n'; break;
940
case 'r': c = '\r'; break;
941
case 't': c = '\t'; break;
942
case 'v': c = '\v'; break;
945
if ('0' <= c && c < '8') {
948
if ('0' <= c && c < '8') {
949
val = 8 * val + JS7_UNDEC(c);
952
if ('0' <= c && c < '8') {
954
val = 8 * val + JS7_UNDEC(c);
962
} else if (c == 'u') {
964
if (PeekChars(ts, 4, cp) &&
965
JS7_ISHEX(cp[0]) && JS7_ISHEX(cp[1]) &&
966
JS7_ISHEX(cp[2]) && JS7_ISHEX(cp[3])) {
967
c = (((((JS7_UNHEX(cp[0]) << 4)
968
+ JS7_UNHEX(cp[1])) << 4)
969
+ JS7_UNHEX(cp[2])) << 4)
973
} else if (c == 'x') {
975
if (PeekChars(ts, 2, cp) &&
976
JS7_ISHEX(cp[0]) && JS7_ISHEX(cp[1])) {
977
c = (JS7_UNHEX(cp[0]) << 4) + JS7_UNHEX(cp[1]);
980
} else if (c == '\n' && JSVERSION_IS_ECMA(cx->version)) {
981
/* ECMA follows C by removing escaped newlines. */
987
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
990
FINISH_TOKENBUF(&ts->tokenbuf);
991
atom = js_AtomizeChars(cx,
993
TOKEN_LENGTH(&ts->tokenbuf),
997
tp->pos.end.lineno = (uint16)ts->lineno;
998
tp->t_op = JSOP_STRING;
1008
case ';': c = TOK_SEMI; break;
1009
case '.': c = TOK_DOT; break;
1010
case '[': c = TOK_LB; break;
1011
case ']': c = TOK_RB; break;
1012
case '{': c = TOK_LC; break;
1013
case '}': c = TOK_RC; break;
1014
case '(': c = TOK_LP; break;
1015
case ')': c = TOK_RP; break;
1016
case ',': c = TOK_COMMA; break;
1017
case '?': c = TOK_HOOK; break;
1021
* Default so compiler can modify to JSOP_GETTER if 'p getter: v' in an
1022
* object initializer, likewise for setter.
1024
tp->t_op = JSOP_NOP;
1029
if (MatchChar(ts, c)) {
1031
} else if (MatchChar(ts, '=')) {
1032
tp->t_op = JSOP_BITOR;
1040
if (MatchChar(ts, '=')) {
1041
tp->t_op = JSOP_BITXOR;
1049
if (MatchChar(ts, c)) {
1051
} else if (MatchChar(ts, '=')) {
1052
tp->t_op = JSOP_BITAND;
1060
if (MatchChar(ts, c)) {
1061
#if JS_HAS_TRIPLE_EQOPS
1062
tp->t_op = MatchChar(ts, c) ? JSOP_NEW_EQ : (JSOp)cx->jsop_eq;
1064
tp->t_op = cx->jsop_eq;
1068
tp->t_op = JSOP_NOP;
1074
if (MatchChar(ts, '=')) {
1075
#if JS_HAS_TRIPLE_EQOPS
1076
tp->t_op = MatchChar(ts, '=') ? JSOP_NEW_NE : (JSOp)cx->jsop_ne;
1078
tp->t_op = cx->jsop_ne;
1082
tp->t_op = JSOP_NOT;
1088
/* NB: treat HTML begin-comment as comment-till-end-of-line */
1089
if (MatchChar(ts, '!')) {
1090
if (MatchChar(ts, '-')) {
1091
if (MatchChar(ts, '-'))
1097
if (MatchChar(ts, c)) {
1098
tp->t_op = JSOP_LSH;
1099
c = MatchChar(ts, '=') ? TOK_ASSIGN : TOK_SHOP;
1101
tp->t_op = MatchChar(ts, '=') ? JSOP_LE : JSOP_LT;
1107
if (MatchChar(ts, c)) {
1108
tp->t_op = MatchChar(ts, c) ? JSOP_URSH : JSOP_RSH;
1109
c = MatchChar(ts, '=') ? TOK_ASSIGN : TOK_SHOP;
1111
tp->t_op = MatchChar(ts, '=') ? JSOP_GE : JSOP_GT;
1117
tp->t_op = JSOP_MUL;
1118
c = MatchChar(ts, '=') ? TOK_ASSIGN : TOK_STAR;
1122
if (MatchChar(ts, '/')) {
1124
while ((c = GetChar(ts)) != EOF && c != '\n')
1125
/* skip to end of line */;
1129
if (MatchChar(ts, '*')) {
1130
while ((c = GetChar(ts)) != EOF &&
1131
!(c == '*' && MatchChar(ts, '/'))) {
1132
/* Ignore all characters until comment close. */
1135
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
1136
JSMSG_UNTERMINATED_COMMENT);
1143
if (ts->flags & TSF_REGEXP) {
1147
INIT_TOKENBUF(&ts->tokenbuf);
1148
while ((c = GetChar(ts)) != '/') {
1149
if (c == '\n' || c == EOF) {
1151
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
1152
JSMSG_UNTERMINATED_REGEXP);
1156
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
1160
if (!AddToTokenBuf(cx, &ts->tokenbuf, (jschar)c))
1163
FINISH_TOKENBUF(&ts->tokenbuf);
1164
for (flags = 0; ; ) {
1165
if (MatchChar(ts, 'g'))
1166
flags |= JSREG_GLOB;
1167
else if (MatchChar(ts, 'i'))
1168
flags |= JSREG_FOLD;
1169
else if (MatchChar(ts, 'm'))
1170
flags |= JSREG_MULTILINE;
1176
tp->ptr = ts->linebuf.ptr - 1;
1177
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
1178
JSMSG_BAD_REGEXP_FLAG);
1182
obj = js_NewRegExpObject(cx, ts,
1184
TOKEN_LENGTH(&ts->tokenbuf),
1188
atom = js_AtomizeObject(cx, obj, 0);
1191
tp->t_op = JSOP_OBJECT;
1195
#endif /* JS_HAS_REGEXPS */
1197
tp->t_op = JSOP_DIV;
1198
c = MatchChar(ts, '=') ? TOK_ASSIGN : TOK_DIVOP;
1202
tp->t_op = JSOP_MOD;
1203
c = MatchChar(ts, '=') ? TOK_ASSIGN : TOK_DIVOP;
1207
tp->t_op = JSOP_BITNOT;
1212
if (MatchChar(ts, '=')) {
1213
tp->t_op = JSOP_ADD;
1215
} else if (MatchChar(ts, c)) {
1218
tp->t_op = JSOP_POS;
1224
if (MatchChar(ts, '=')) {
1225
tp->t_op = JSOP_SUB;
1227
} else if (MatchChar(ts, c)) {
1228
if (PeekChar(ts) == '>' && !(ts->flags & TSF_DIRTYLINE))
1232
tp->t_op = JSOP_NEG;
1235
ts->flags |= TSF_DIRTYLINE;
1238
#if JS_HAS_SHARP_VARS
1244
if (!JS7_ISDEC(c)) {
1248
n = (uint32)JS7_UNDEC(c);
1253
n = 10 * n + JS7_UNDEC(c);
1254
if (n >= ATOM_INDEX_LIMIT) {
1255
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
1256
JSMSG_SHARPVAR_TOO_BIG);
1260
tp->t_dval = (jsdouble) n;
1261
if (JS_HAS_STRICT_OPTION(cx) &&
1262
(c == '=' || c == '#')) {
1264
JS_snprintf(buf, sizeof buf, "#%u%c", n, c);
1265
if (!js_ReportCompileErrorNumber(cx, ts, NULL,
1268
JSMSG_DEPRECATED_USAGE,
1274
RETURN(TOK_DEFSHARP);
1276
RETURN(TOK_USESHARP);
1281
#endif /* JS_HAS_SHARP_VARS */
1284
js_ReportCompileErrorNumber(cx, ts, NULL, JSREPORT_ERROR,
1285
JSMSG_ILLEGAL_CHARACTER);
1289
JS_ASSERT(c < TOK_LIMIT);
1290
RETURN((JSTokenType)c);
1292
#undef INIT_TOKENBUF
1293
#undef FINISH_TOKENBUF
1299
js_UngetToken(JSTokenStream *ts)
1301
JS_ASSERT(ts->lookahead < NTOKENS_MASK);
1302
if (ts->flags & TSF_ERROR)
1305
ts->cursor = (ts->cursor - 1) & NTOKENS_MASK;
1309
js_MatchToken(JSContext *cx, JSTokenStream *ts, JSTokenType tt)
1311
if (js_GetToken(cx, ts) == tt)