|
|
1.1 root 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2: *
3: * The contents of this file are subject to the Netscape Public
4: * License Version 1.1 (the "License"); you may not use this file
5: * except in compliance with the License. You may obtain a copy of
6: * the License at http://www.mozilla.org/NPL/
7: *
8: * Software distributed under the License is distributed on an "AS
9: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10: * implied. See the License for the specific language governing
11: * rights and limitations under the License.
12: *
13: * The Original Code is Mozilla Communicator client code, released
14: * March 31, 1998.
15: *
16: * The Initial Developer of the Original Code is Netscape
17: * Communications Corporation. Portions created by Netscape are
18: * Copyright (C) 1998 Netscape Communications Corporation. All
19: * Rights Reserved.
20: *
21: * Contributor(s):
22: *
23: * Alternatively, the contents of this file may be used under the
24: * terms of the GNU Public License (the "GPL"), in which case the
25: * provisions of the GPL are applicable instead of those above.
26: * If you wish to allow use of your version of this file only
27: * under the terms of the GPL and not to allow others to use your
28: * version of this file under the NPL, indicate your decision by
29: * deleting the provisions above and replace them with the notice
30: * and other provisions required by the GPL. If you do not delete
31: * the provisions above, a recipient may use your version of this
32: * file under either the NPL or the GPL.
33: */
34:
35: #ifndef jsscan_h___
36: #define jsscan_h___
37: /*
38: * JS lexical scanner interface.
39: */
40: #include <stddef.h>
41: #include <stdio.h>
42: #include "jsopcode.h"
43: #include "jsprvtd.h"
44: #include "jspubtd.h"
45:
46: JS_BEGIN_EXTERN_C
47:
48: typedef enum JSTokenType {
49: TOK_ERROR = -1, /* well-known as the only code < EOF */
50: TOK_EOF = 0, /* end of file */
51: TOK_EOL = 1, /* end of line */
52: TOK_SEMI = 2, /* semicolon */
53: TOK_COMMA = 3, /* comma operator */
54: TOK_ASSIGN = 4, /* assignment ops (= += -= etc.) */
55: TOK_HOOK = 5, TOK_COLON = 6, /* conditional (?:) */
56: TOK_OR = 7, /* logical or (||) */
57: TOK_AND = 8, /* logical and (&&) */
58: TOK_BITOR = 9, /* bitwise-or (|) */
59: TOK_BITXOR = 10, /* bitwise-xor (^) */
60: TOK_BITAND = 11, /* bitwise-and (&) */
61: TOK_EQOP = 12, /* equality ops (== !=) */
62: TOK_RELOP = 13, /* relational ops (< <= > >=) */
63: TOK_SHOP = 14, /* shift ops (<< >> >>>) */
64: TOK_PLUS = 15, /* plus */
65: TOK_MINUS = 16, /* minus */
66: TOK_STAR = 17, TOK_DIVOP = 18, /* multiply/divide ops (* / %) */
67: TOK_UNARYOP = 19, /* unary prefix operator */
68: TOK_INC = 20, TOK_DEC = 21, /* increment/decrement (++ --) */
69: TOK_DOT = 22, /* member operator (.) */
70: TOK_LB = 23, TOK_RB = 24, /* left and right brackets */
71: TOK_LC = 25, TOK_RC = 26, /* left and right curlies (braces) */
72: TOK_LP = 27, TOK_RP = 28, /* left and right parentheses */
73: TOK_NAME = 29, /* identifier */
74: TOK_NUMBER = 30, /* numeric constant */
75: TOK_STRING = 31, /* string constant */
76: TOK_OBJECT = 32, /* RegExp or other object constant */
77: TOK_PRIMARY = 33, /* true, false, null, this, super */
78: TOK_FUNCTION = 34, /* function keyword */
79: TOK_EXPORT = 35, /* export keyword */
80: TOK_IMPORT = 36, /* import keyword */
81: TOK_IF = 37, /* if keyword */
82: TOK_ELSE = 38, /* else keyword */
83: TOK_SWITCH = 39, /* switch keyword */
84: TOK_CASE = 40, /* case keyword */
85: TOK_DEFAULT = 41, /* default keyword */
86: TOK_WHILE = 42, /* while keyword */
87: TOK_DO = 43, /* do keyword */
88: TOK_FOR = 44, /* for keyword */
89: TOK_BREAK = 45, /* break keyword */
90: TOK_CONTINUE = 46, /* continue keyword */
91: TOK_IN = 47, /* in keyword */
92: TOK_VAR = 48, /* var keyword */
93: TOK_WITH = 49, /* with keyword */
94: TOK_RETURN = 50, /* return keyword */
95: TOK_NEW = 51, /* new keyword */
96: TOK_DELETE = 52, /* delete keyword */
97: TOK_DEFSHARP = 53, /* #n= for object/array initializers */
98: TOK_USESHARP = 54, /* #n# for object/array initializers */
99: TOK_TRY = 55, /* try keyword */
100: TOK_CATCH = 56, /* catch keyword */
101: TOK_FINALLY = 57, /* finally keyword */
102: TOK_THROW = 58, /* throw keyword */
103: TOK_INSTANCEOF = 59, /* instanceof keyword */
104: TOK_DEBUGGER = 60, /* debugger keyword */
105: TOK_RESERVED, /* reserved keywords */
106: TOK_LIMIT /* domain size */
107: } JSTokenType;
108:
109: #define IS_PRIMARY_TOKEN(tt) \
110: ((uintN)((tt) - TOK_NAME) <= (uintN)(TOK_PRIMARY - TOK_NAME))
111:
112: struct JSTokenPtr {
113: uint16 index; /* index of char in physical line */
114: uint16 lineno; /* physical line number */
115: };
116:
117: struct JSTokenPos {
118: JSTokenPtr begin; /* first character and line of token */
119: JSTokenPtr end; /* index 1 past last char, last line */
120: };
121:
122: struct JSToken {
123: JSTokenType type; /* char value or above enumerator */
124: JSTokenPos pos; /* token position in file */
125: jschar *ptr; /* beginning of token in line buffer */
126: union {
127: struct {
128: JSOp op; /* operator, for minimal parser */
129: JSAtom *atom; /* atom table entry */
130: } s;
131: jsdouble dval; /* floating point number */
132: } u;
133: };
134:
135: #define t_op u.s.op
136: #define t_atom u.s.atom
137: #define t_dval u.dval
138:
139: typedef struct JSTokenBuf {
140: jschar *base; /* base of line or stream buffer */
141: jschar *limit; /* limit for quick bounds check */
142: jschar *ptr; /* next char to get, or slot to use */
143: } JSTokenBuf;
144:
145: #define JS_LINE_LIMIT 256 /* logical line buffer size limit --
146: physical line length is unlimited */
147: #define NTOKENS 4 /* 1 current + 2 lookahead, rounded */
148: #define NTOKENS_MASK (NTOKENS-1) /* to power of 2 to avoid divmod by 3 */
149:
150: struct JSTokenStream {
151: JSToken tokens[NTOKENS];/* circular token buffer */
152: uintN cursor; /* index of last parsed token */
153: uintN lookahead; /* count of lookahead tokens */
154: uintN lineno; /* current line number */
155: uintN ungetpos; /* next free char slot in ungetbuf */
156: jschar ungetbuf[6]; /* at most 6, for \uXXXX lookahead */
157: uintN flags; /* flags -- see below */
158: ptrdiff_t linelen; /* physical linebuf segment length */
159: ptrdiff_t linepos; /* linebuf offset in physical line */
160: JSTokenBuf linebuf; /* line buffer for diagnostics */
161: JSTokenBuf userbuf; /* user input buffer if !file */
162: JSTokenBuf tokenbuf; /* current token string buffer */
163: const char *filename; /* input filename or null */
164: FILE *file; /* stdio stream if reading from file */
165: JSPrincipals *principals; /* principals associated with source */
166: JSSourceHandler listener; /* callback for source; eg debugger */
167: void *listenerData; /* listener 'this' data */
168: void *listenerTSData;/* listener data for this TokenStream */
169: };
170:
171: #define CURRENT_TOKEN(ts) ((ts)->tokens[(ts)->cursor])
172:
173: /* JSTokenStream flags */
174: #define TSF_ERROR 0x01 /* fatal error while compiling */
175: #define TSF_EOF 0x02 /* hit end of file */
176: #define TSF_NEWLINES 0x04 /* tokenize newlines */
177: #define TSF_REGEXP 0x08 /* looking for a regular expression */
178: #define TSF_NLFLAG 0x20 /* last linebuf ended with \n */
179: #define TSF_CRFLAG 0x40 /* linebuf would have ended with \r */
180: #define TSF_DIRTYLINE 0x80 /* stuff other than whitespace since start of line */
181:
182: /* Unicode separators that are treated as line terminators, in addition to \n, \r */
183: #define LINE_SEPARATOR 0x2028
184: #define PARA_SEPARATOR 0x2029
185:
186: /*
187: * Create a new token stream, either from an input buffer or from a file.
188: * Return null on file-open or memory-allocation failure.
189: *
190: * NB: All of js_New{,Buffer,File}TokenStream() return a pointer to transient
191: * memory in the current context's temp pool. This memory is deallocated via
192: * JS_ARENA_RELEASE() after parsing is finished.
193: */
194: extern JSTokenStream *
195: js_NewTokenStream(JSContext *cx, const jschar *base, size_t length,
196: const char *filename, uintN lineno, JSPrincipals *principals);
197:
198: extern JS_FRIEND_API(JSTokenStream *)
199: js_NewBufferTokenStream(JSContext *cx, const jschar *base, size_t length);
200:
201: extern JS_FRIEND_API(JSTokenStream *)
202: js_NewFileTokenStream(JSContext *cx, const char *filename, FILE *defaultfp);
203:
204: extern JS_FRIEND_API(JSBool)
205: js_CloseTokenStream(JSContext *cx, JSTokenStream *ts);
206:
207: /*
208: * Initialize the scanner, installing JS keywords into cx's global scope.
209: */
210: extern JSBool
211: js_InitScanner(JSContext *cx);
212:
213: /*
214: * Friend-exported API entry point to call a mapping function on each reserved
215: * identifier in the scanner's keyword table.
216: */
217: extern JS_FRIEND_API(void)
218: js_MapKeywords(void (*mapfun)(const char *));
219:
220: /*
221: * Report a compile-time error by its number, using ts or cg to show context.
222: * Return true for a warning, false for an error.
223: */
224: extern JSBool
225: js_ReportCompileErrorNumber(JSContext *cx, JSTokenStream *ts,
226: JSCodeGenerator *cg, uintN flags,
227: const uintN errorNumber, ...);
228:
229: /*
230: * Look ahead one token and return its type.
231: */
232: extern JSTokenType
233: js_PeekToken(JSContext *cx, JSTokenStream *ts);
234:
235: extern JSTokenType
236: js_PeekTokenSameLine(JSContext *cx, JSTokenStream *ts);
237:
238: /*
239: * Get the next token from ts.
240: */
241: extern JSTokenType
242: js_GetToken(JSContext *cx, JSTokenStream *ts);
243:
244: /*
245: * Push back the last scanned token onto ts.
246: */
247: extern void
248: js_UngetToken(JSTokenStream *ts);
249:
250: /*
251: * Get the next token from ts if its type is tt.
252: */
253: extern JSBool
254: js_MatchToken(JSContext *cx, JSTokenStream *ts, JSTokenType tt);
255:
256: JS_END_EXTERN_C
257:
258: #endif /* jsscan_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.