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