|
1 // Acorn is a tiny, fast JavaScript parser written in JavaScript. |
|
2 // |
|
3 // Acorn was written by Marijn Haverbeke and released under an MIT |
|
4 // license. The Unicode regexps (for identifiers and whitespace) were |
|
5 // taken from [Esprima](http://esprima.org) by Ariya Hidayat. |
|
6 // |
|
7 // Git repositories for Acorn are available at |
|
8 // |
|
9 // http://marijnhaverbeke.nl/git/acorn |
|
10 // https://github.com/marijnh/acorn.git |
|
11 // |
|
12 // Please use the [github bug tracker][ghbt] to report issues. |
|
13 // |
|
14 // [ghbt]: https://github.com/marijnh/acorn/issues |
|
15 // |
|
16 // This file defines the main parser interface. The library also comes |
|
17 // with a [error-tolerant parser][dammit] and an |
|
18 // [abstract syntax tree walker][walk], defined in other files. |
|
19 // |
|
20 // [dammit]: acorn_loose.js |
|
21 // [walk]: util/walk.js |
|
22 |
|
23 (function(root, mod) { |
|
24 if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS |
|
25 if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD |
|
26 mod(root.acorn || (root.acorn = {})); // Plain browser env |
|
27 })(this, function(exports) { |
|
28 "use strict"; |
|
29 |
|
30 exports.version = "0.4.1"; |
|
31 |
|
32 // The main exported interface (under `self.acorn` when in the |
|
33 // browser) is a `parse` function that takes a code string and |
|
34 // returns an abstract syntax tree as specified by [Mozilla parser |
|
35 // API][api], with the caveat that the SpiderMonkey-specific syntax |
|
36 // (`let`, `yield`, inline XML, etc) is not recognized. |
|
37 // |
|
38 // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API |
|
39 |
|
40 var options, input, inputLen, sourceFile; |
|
41 |
|
42 exports.parse = function(inpt, opts) { |
|
43 input = String(inpt); inputLen = input.length; |
|
44 setOptions(opts); |
|
45 initTokenState(); |
|
46 return parseTopLevel(options.program); |
|
47 }; |
|
48 |
|
49 // A second optional argument can be given to further configure |
|
50 // the parser process. These options are recognized: |
|
51 |
|
52 var defaultOptions = exports.defaultOptions = { |
|
53 // `ecmaVersion` indicates the ECMAScript version to parse. Must |
|
54 // be either 3 or 5. This |
|
55 // influences support for strict mode, the set of reserved words, and |
|
56 // support for getters and setter. |
|
57 ecmaVersion: 5, |
|
58 // Turn on `strictSemicolons` to prevent the parser from doing |
|
59 // automatic semicolon insertion. |
|
60 strictSemicolons: false, |
|
61 // When `allowTrailingCommas` is false, the parser will not allow |
|
62 // trailing commas in array and object literals. |
|
63 allowTrailingCommas: true, |
|
64 // By default, reserved words are not enforced. Enable |
|
65 // `forbidReserved` to enforce them. |
|
66 forbidReserved: false, |
|
67 // When `locations` is on, `loc` properties holding objects with |
|
68 // `start` and `end` properties in `{line, column}` form (with |
|
69 // line being 1-based and column 0-based) will be attached to the |
|
70 // nodes. |
|
71 locations: false, |
|
72 // A function can be passed as `onComment` option, which will |
|
73 // cause Acorn to call that function with `(block, text, start, |
|
74 // end)` parameters whenever a comment is skipped. `block` is a |
|
75 // boolean indicating whether this is a block (`/* */`) comment, |
|
76 // `text` is the content of the comment, and `start` and `end` are |
|
77 // character offsets that denote the start and end of the comment. |
|
78 // When the `locations` option is on, two more parameters are |
|
79 // passed, the full `{line, column}` locations of the start and |
|
80 // end of the comments. Note that you are not allowed to call the |
|
81 // parser from the callback—that will corrupt its internal state. |
|
82 onComment: null, |
|
83 // Nodes have their start and end characters offsets recorded in |
|
84 // `start` and `end` properties (directly on the node, rather than |
|
85 // the `loc` object, which holds line/column data. To also add a |
|
86 // [semi-standardized][range] `range` property holding a `[start, |
|
87 // end]` array with the same numbers, set the `ranges` option to |
|
88 // `true`. |
|
89 // |
|
90 // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678 |
|
91 ranges: false, |
|
92 // It is possible to parse multiple files into a single AST by |
|
93 // passing the tree produced by parsing the first file as |
|
94 // `program` option in subsequent parses. This will add the |
|
95 // toplevel forms of the parsed file to the `Program` (top) node |
|
96 // of an existing parse tree. |
|
97 program: null, |
|
98 // When `locations` is on, you can pass this to record the source |
|
99 // file in every node's `loc` object. |
|
100 sourceFile: null, |
|
101 // This value, if given, is stored in every node, whether |
|
102 // `locations` is on or off. |
|
103 directSourceFile: null |
|
104 }; |
|
105 |
|
106 function setOptions(opts) { |
|
107 options = opts || {}; |
|
108 for (var opt in defaultOptions) if (!Object.prototype.hasOwnProperty.call(options, opt)) |
|
109 options[opt] = defaultOptions[opt]; |
|
110 sourceFile = options.sourceFile || null; |
|
111 } |
|
112 |
|
113 // The `getLineInfo` function is mostly useful when the |
|
114 // `locations` option is off (for performance reasons) and you |
|
115 // want to find the line/column position for a given character |
|
116 // offset. `input` should be the code string that the offset refers |
|
117 // into. |
|
118 |
|
119 var getLineInfo = exports.getLineInfo = function(input, offset) { |
|
120 for (var line = 1, cur = 0;;) { |
|
121 lineBreak.lastIndex = cur; |
|
122 var match = lineBreak.exec(input); |
|
123 if (match && match.index < offset) { |
|
124 ++line; |
|
125 cur = match.index + match[0].length; |
|
126 } else break; |
|
127 } |
|
128 return {line: line, column: offset - cur}; |
|
129 }; |
|
130 |
|
131 // Acorn is organized as a tokenizer and a recursive-descent parser. |
|
132 // The `tokenize` export provides an interface to the tokenizer. |
|
133 // Because the tokenizer is optimized for being efficiently used by |
|
134 // the Acorn parser itself, this interface is somewhat crude and not |
|
135 // very modular. Performing another parse or call to `tokenize` will |
|
136 // reset the internal state, and invalidate existing tokenizers. |
|
137 |
|
138 exports.tokenize = function(inpt, opts) { |
|
139 input = String(inpt); inputLen = input.length; |
|
140 setOptions(opts); |
|
141 initTokenState(); |
|
142 |
|
143 var t = {}; |
|
144 function getToken(forceRegexp) { |
|
145 lastEnd = tokEnd; |
|
146 readToken(forceRegexp); |
|
147 t.start = tokStart; t.end = tokEnd; |
|
148 t.startLoc = tokStartLoc; t.endLoc = tokEndLoc; |
|
149 t.type = tokType; t.value = tokVal; |
|
150 return t; |
|
151 } |
|
152 getToken.jumpTo = function(pos, reAllowed) { |
|
153 tokPos = pos; |
|
154 if (options.locations) { |
|
155 tokCurLine = 1; |
|
156 tokLineStart = lineBreak.lastIndex = 0; |
|
157 var match; |
|
158 while ((match = lineBreak.exec(input)) && match.index < pos) { |
|
159 ++tokCurLine; |
|
160 tokLineStart = match.index + match[0].length; |
|
161 } |
|
162 } |
|
163 tokRegexpAllowed = reAllowed; |
|
164 skipSpace(); |
|
165 }; |
|
166 return getToken; |
|
167 }; |
|
168 |
|
169 // State is kept in (closure-)global variables. We already saw the |
|
170 // `options`, `input`, and `inputLen` variables above. |
|
171 |
|
172 // The current position of the tokenizer in the input. |
|
173 |
|
174 var tokPos; |
|
175 |
|
176 // The start and end offsets of the current token. |
|
177 |
|
178 var tokStart, tokEnd; |
|
179 |
|
180 // When `options.locations` is true, these hold objects |
|
181 // containing the tokens start and end line/column pairs. |
|
182 |
|
183 var tokStartLoc, tokEndLoc; |
|
184 |
|
185 // The type and value of the current token. Token types are objects, |
|
186 // named by variables against which they can be compared, and |
|
187 // holding properties that describe them (indicating, for example, |
|
188 // the precedence of an infix operator, and the original name of a |
|
189 // keyword token). The kind of value that's held in `tokVal` depends |
|
190 // on the type of the token. For literals, it is the literal value, |
|
191 // for operators, the operator name, and so on. |
|
192 |
|
193 var tokType, tokVal; |
|
194 |
|
195 // Interal state for the tokenizer. To distinguish between division |
|
196 // operators and regular expressions, it remembers whether the last |
|
197 // token was one that is allowed to be followed by an expression. |
|
198 // (If it is, a slash is probably a regexp, if it isn't it's a |
|
199 // division operator. See the `parseStatement` function for a |
|
200 // caveat.) |
|
201 |
|
202 var tokRegexpAllowed; |
|
203 |
|
204 // When `options.locations` is true, these are used to keep |
|
205 // track of the current line, and know when a new line has been |
|
206 // entered. |
|
207 |
|
208 var tokCurLine, tokLineStart; |
|
209 |
|
210 // These store the position of the previous token, which is useful |
|
211 // when finishing a node and assigning its `end` position. |
|
212 |
|
213 var lastStart, lastEnd, lastEndLoc; |
|
214 |
|
215 // This is the parser's state. `inFunction` is used to reject |
|
216 // `return` statements outside of functions, `labels` to verify that |
|
217 // `break` and `continue` have somewhere to jump to, and `strict` |
|
218 // indicates whether strict mode is on. |
|
219 |
|
220 var inFunction, labels, strict; |
|
221 |
|
222 // This function is used to raise exceptions on parse errors. It |
|
223 // takes an offset integer (into the current `input`) to indicate |
|
224 // the location of the error, attaches the position to the end |
|
225 // of the error message, and then raises a `SyntaxError` with that |
|
226 // message. |
|
227 |
|
228 function raise(pos, message) { |
|
229 var loc = getLineInfo(input, pos); |
|
230 message += " (" + loc.line + ":" + loc.column + ")"; |
|
231 var err = new SyntaxError(message); |
|
232 err.pos = pos; err.loc = loc; err.raisedAt = tokPos; |
|
233 throw err; |
|
234 } |
|
235 |
|
236 // Reused empty array added for node fields that are always empty. |
|
237 |
|
238 var empty = []; |
|
239 |
|
240 // ## Token types |
|
241 |
|
242 // The assignment of fine-grained, information-carrying type objects |
|
243 // allows the tokenizer to store the information it has about a |
|
244 // token in a way that is very cheap for the parser to look up. |
|
245 |
|
246 // All token type variables start with an underscore, to make them |
|
247 // easy to recognize. |
|
248 |
|
249 // These are the general types. The `type` property is only used to |
|
250 // make them recognizeable when debugging. |
|
251 |
|
252 var _num = {type: "num"}, _regexp = {type: "regexp"}, _string = {type: "string"}; |
|
253 var _name = {type: "name"}, _eof = {type: "eof"}; |
|
254 |
|
255 // Keyword tokens. The `keyword` property (also used in keyword-like |
|
256 // operators) indicates that the token originated from an |
|
257 // identifier-like word, which is used when parsing property names. |
|
258 // |
|
259 // The `beforeExpr` property is used to disambiguate between regular |
|
260 // expressions and divisions. It is set on all token types that can |
|
261 // be followed by an expression (thus, a slash after them would be a |
|
262 // regular expression). |
|
263 // |
|
264 // `isLoop` marks a keyword as starting a loop, which is important |
|
265 // to know when parsing a label, in order to allow or disallow |
|
266 // continue jumps to that label. |
|
267 |
|
268 var _break = {keyword: "break"}, _case = {keyword: "case", beforeExpr: true}, _catch = {keyword: "catch"}; |
|
269 var _continue = {keyword: "continue"}, _debugger = {keyword: "debugger"}, _default = {keyword: "default"}; |
|
270 var _do = {keyword: "do", isLoop: true}, _else = {keyword: "else", beforeExpr: true}; |
|
271 var _finally = {keyword: "finally"}, _for = {keyword: "for", isLoop: true}, _function = {keyword: "function"}; |
|
272 var _if = {keyword: "if"}, _return = {keyword: "return", beforeExpr: true}, _switch = {keyword: "switch"}; |
|
273 var _throw = {keyword: "throw", beforeExpr: true}, _try = {keyword: "try"}, _var = {keyword: "var"}; |
|
274 var _while = {keyword: "while", isLoop: true}, _with = {keyword: "with"}, _new = {keyword: "new", beforeExpr: true}; |
|
275 var _this = {keyword: "this"}; |
|
276 |
|
277 // The keywords that denote values. |
|
278 |
|
279 var _null = {keyword: "null", atomValue: null}, _true = {keyword: "true", atomValue: true}; |
|
280 var _false = {keyword: "false", atomValue: false}; |
|
281 |
|
282 // Some keywords are treated as regular operators. `in` sometimes |
|
283 // (when parsing `for`) needs to be tested against specifically, so |
|
284 // we assign a variable name to it for quick comparing. |
|
285 |
|
286 var _in = {keyword: "in", binop: 7, beforeExpr: true}; |
|
287 |
|
288 // Map keyword names to token types. |
|
289 |
|
290 var keywordTypes = {"break": _break, "case": _case, "catch": _catch, |
|
291 "continue": _continue, "debugger": _debugger, "default": _default, |
|
292 "do": _do, "else": _else, "finally": _finally, "for": _for, |
|
293 "function": _function, "if": _if, "return": _return, "switch": _switch, |
|
294 "throw": _throw, "try": _try, "var": _var, "while": _while, "with": _with, |
|
295 "null": _null, "true": _true, "false": _false, "new": _new, "in": _in, |
|
296 "instanceof": {keyword: "instanceof", binop: 7, beforeExpr: true}, "this": _this, |
|
297 "typeof": {keyword: "typeof", prefix: true, beforeExpr: true}, |
|
298 "void": {keyword: "void", prefix: true, beforeExpr: true}, |
|
299 "delete": {keyword: "delete", prefix: true, beforeExpr: true}}; |
|
300 |
|
301 // Punctuation token types. Again, the `type` property is purely for debugging. |
|
302 |
|
303 var _bracketL = {type: "[", beforeExpr: true}, _bracketR = {type: "]"}, _braceL = {type: "{", beforeExpr: true}; |
|
304 var _braceR = {type: "}"}, _parenL = {type: "(", beforeExpr: true}, _parenR = {type: ")"}; |
|
305 var _comma = {type: ",", beforeExpr: true}, _semi = {type: ";", beforeExpr: true}; |
|
306 var _colon = {type: ":", beforeExpr: true}, _dot = {type: "."}, _question = {type: "?", beforeExpr: true}; |
|
307 |
|
308 // Operators. These carry several kinds of properties to help the |
|
309 // parser use them properly (the presence of these properties is |
|
310 // what categorizes them as operators). |
|
311 // |
|
312 // `binop`, when present, specifies that this operator is a binary |
|
313 // operator, and will refer to its precedence. |
|
314 // |
|
315 // `prefix` and `postfix` mark the operator as a prefix or postfix |
|
316 // unary operator. `isUpdate` specifies that the node produced by |
|
317 // the operator should be of type UpdateExpression rather than |
|
318 // simply UnaryExpression (`++` and `--`). |
|
319 // |
|
320 // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as |
|
321 // binary operators with a very low precedence, that should result |
|
322 // in AssignmentExpression nodes. |
|
323 |
|
324 var _slash = {binop: 10, beforeExpr: true}, _eq = {isAssign: true, beforeExpr: true}; |
|
325 var _assign = {isAssign: true, beforeExpr: true}; |
|
326 var _incDec = {postfix: true, prefix: true, isUpdate: true}, _prefix = {prefix: true, beforeExpr: true}; |
|
327 var _logicalOR = {binop: 1, beforeExpr: true}; |
|
328 var _logicalAND = {binop: 2, beforeExpr: true}; |
|
329 var _bitwiseOR = {binop: 3, beforeExpr: true}; |
|
330 var _bitwiseXOR = {binop: 4, beforeExpr: true}; |
|
331 var _bitwiseAND = {binop: 5, beforeExpr: true}; |
|
332 var _equality = {binop: 6, beforeExpr: true}; |
|
333 var _relational = {binop: 7, beforeExpr: true}; |
|
334 var _bitShift = {binop: 8, beforeExpr: true}; |
|
335 var _plusMin = {binop: 9, prefix: true, beforeExpr: true}; |
|
336 var _multiplyModulo = {binop: 10, beforeExpr: true}; |
|
337 |
|
338 // Provide access to the token types for external users of the |
|
339 // tokenizer. |
|
340 |
|
341 exports.tokTypes = {bracketL: _bracketL, bracketR: _bracketR, braceL: _braceL, braceR: _braceR, |
|
342 parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon, |
|
343 dot: _dot, question: _question, slash: _slash, eq: _eq, name: _name, eof: _eof, |
|
344 num: _num, regexp: _regexp, string: _string}; |
|
345 for (var kw in keywordTypes) exports.tokTypes["_" + kw] = keywordTypes[kw]; |
|
346 |
|
347 // This is a trick taken from Esprima. It turns out that, on |
|
348 // non-Chrome browsers, to check whether a string is in a set, a |
|
349 // predicate containing a big ugly `switch` statement is faster than |
|
350 // a regular expression, and on Chrome the two are about on par. |
|
351 // This function uses `eval` (non-lexical) to produce such a |
|
352 // predicate from a space-separated string of words. |
|
353 // |
|
354 // It starts by sorting the words by length. |
|
355 |
|
356 function makePredicate(words) { |
|
357 words = words.split(" "); |
|
358 var f = "", cats = []; |
|
359 out: for (var i = 0; i < words.length; ++i) { |
|
360 for (var j = 0; j < cats.length; ++j) |
|
361 if (cats[j][0].length == words[i].length) { |
|
362 cats[j].push(words[i]); |
|
363 continue out; |
|
364 } |
|
365 cats.push([words[i]]); |
|
366 } |
|
367 function compareTo(arr) { |
|
368 if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";"; |
|
369 f += "switch(str){"; |
|
370 for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":"; |
|
371 f += "return true}return false;"; |
|
372 } |
|
373 |
|
374 // When there are more than three length categories, an outer |
|
375 // switch first dispatches on the lengths, to save on comparisons. |
|
376 |
|
377 if (cats.length > 3) { |
|
378 cats.sort(function(a, b) {return b.length - a.length;}); |
|
379 f += "switch(str.length){"; |
|
380 for (var i = 0; i < cats.length; ++i) { |
|
381 var cat = cats[i]; |
|
382 f += "case " + cat[0].length + ":"; |
|
383 compareTo(cat); |
|
384 } |
|
385 f += "}"; |
|
386 |
|
387 // Otherwise, simply generate a flat `switch` statement. |
|
388 |
|
389 } else { |
|
390 compareTo(words); |
|
391 } |
|
392 return new Function("str", f); |
|
393 } |
|
394 |
|
395 // The ECMAScript 3 reserved word list. |
|
396 |
|
397 var isReservedWord3 = makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile"); |
|
398 |
|
399 // ECMAScript 5 reserved words. |
|
400 |
|
401 var isReservedWord5 = makePredicate("class enum extends super const export import"); |
|
402 |
|
403 // The additional reserved words in strict mode. |
|
404 |
|
405 var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield"); |
|
406 |
|
407 // The forbidden variable names in strict mode. |
|
408 |
|
409 var isStrictBadIdWord = makePredicate("eval arguments"); |
|
410 |
|
411 // And the keywords. |
|
412 |
|
413 var isKeyword = makePredicate("break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this"); |
|
414 |
|
415 // ## Character categories |
|
416 |
|
417 // Big ugly regular expressions that match characters in the |
|
418 // whitespace, identifier, and identifier-start categories. These |
|
419 // are only applied when a character is found to actually have a |
|
420 // code point above 128. |
|
421 |
|
422 var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/; |
|
423 var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc"; |
|
424 var nonASCIIidentifierChars = "\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f"; |
|
425 var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]"); |
|
426 var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]"); |
|
427 |
|
428 // Whether a single character denotes a newline. |
|
429 |
|
430 var newline = /[\n\r\u2028\u2029]/; |
|
431 |
|
432 // Matches a whole line break (where CRLF is considered a single |
|
433 // line break). Used to count lines. |
|
434 |
|
435 var lineBreak = /\r\n|[\n\r\u2028\u2029]/g; |
|
436 |
|
437 // Test whether a given character code starts an identifier. |
|
438 |
|
439 var isIdentifierStart = exports.isIdentifierStart = function(code) { |
|
440 if (code < 65) return code === 36; |
|
441 if (code < 91) return true; |
|
442 if (code < 97) return code === 95; |
|
443 if (code < 123)return true; |
|
444 return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)); |
|
445 }; |
|
446 |
|
447 // Test whether a given character is part of an identifier. |
|
448 |
|
449 var isIdentifierChar = exports.isIdentifierChar = function(code) { |
|
450 if (code < 48) return code === 36; |
|
451 if (code < 58) return true; |
|
452 if (code < 65) return false; |
|
453 if (code < 91) return true; |
|
454 if (code < 97) return code === 95; |
|
455 if (code < 123)return true; |
|
456 return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)); |
|
457 }; |
|
458 |
|
459 // ## Tokenizer |
|
460 |
|
461 // These are used when `options.locations` is on, for the |
|
462 // `tokStartLoc` and `tokEndLoc` properties. |
|
463 |
|
464 function line_loc_t() { |
|
465 this.line = tokCurLine; |
|
466 this.column = tokPos - tokLineStart; |
|
467 } |
|
468 |
|
469 // Reset the token state. Used at the start of a parse. |
|
470 |
|
471 function initTokenState() { |
|
472 tokCurLine = 1; |
|
473 tokPos = tokLineStart = 0; |
|
474 tokRegexpAllowed = true; |
|
475 skipSpace(); |
|
476 } |
|
477 |
|
478 // Called at the end of every token. Sets `tokEnd`, `tokVal`, and |
|
479 // `tokRegexpAllowed`, and skips the space after the token, so that |
|
480 // the next one's `tokStart` will point at the right position. |
|
481 |
|
482 function finishToken(type, val) { |
|
483 tokEnd = tokPos; |
|
484 if (options.locations) tokEndLoc = new line_loc_t; |
|
485 tokType = type; |
|
486 skipSpace(); |
|
487 tokVal = val; |
|
488 tokRegexpAllowed = type.beforeExpr; |
|
489 } |
|
490 |
|
491 function skipBlockComment() { |
|
492 var startLoc = options.onComment && options.locations && new line_loc_t; |
|
493 var start = tokPos, end = input.indexOf("*/", tokPos += 2); |
|
494 if (end === -1) raise(tokPos - 2, "Unterminated comment"); |
|
495 tokPos = end + 2; |
|
496 if (options.locations) { |
|
497 lineBreak.lastIndex = start; |
|
498 var match; |
|
499 while ((match = lineBreak.exec(input)) && match.index < tokPos) { |
|
500 ++tokCurLine; |
|
501 tokLineStart = match.index + match[0].length; |
|
502 } |
|
503 } |
|
504 if (options.onComment) |
|
505 options.onComment(true, input.slice(start + 2, end), start, tokPos, |
|
506 startLoc, options.locations && new line_loc_t); |
|
507 } |
|
508 |
|
509 function skipLineComment() { |
|
510 var start = tokPos; |
|
511 var startLoc = options.onComment && options.locations && new line_loc_t; |
|
512 var ch = input.charCodeAt(tokPos+=2); |
|
513 while (tokPos < inputLen && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) { |
|
514 ++tokPos; |
|
515 ch = input.charCodeAt(tokPos); |
|
516 } |
|
517 if (options.onComment) |
|
518 options.onComment(false, input.slice(start + 2, tokPos), start, tokPos, |
|
519 startLoc, options.locations && new line_loc_t); |
|
520 } |
|
521 |
|
522 // Called at the start of the parse and after every token. Skips |
|
523 // whitespace and comments, and. |
|
524 |
|
525 function skipSpace() { |
|
526 while (tokPos < inputLen) { |
|
527 var ch = input.charCodeAt(tokPos); |
|
528 if (ch === 32) { // ' ' |
|
529 ++tokPos; |
|
530 } else if (ch === 13) { |
|
531 ++tokPos; |
|
532 var next = input.charCodeAt(tokPos); |
|
533 if (next === 10) { |
|
534 ++tokPos; |
|
535 } |
|
536 if (options.locations) { |
|
537 ++tokCurLine; |
|
538 tokLineStart = tokPos; |
|
539 } |
|
540 } else if (ch === 10 || ch === 8232 || ch === 8233) { |
|
541 ++tokPos; |
|
542 if (options.locations) { |
|
543 ++tokCurLine; |
|
544 tokLineStart = tokPos; |
|
545 } |
|
546 } else if (ch > 8 && ch < 14) { |
|
547 ++tokPos; |
|
548 } else if (ch === 47) { // '/' |
|
549 var next = input.charCodeAt(tokPos + 1); |
|
550 if (next === 42) { // '*' |
|
551 skipBlockComment(); |
|
552 } else if (next === 47) { // '/' |
|
553 skipLineComment(); |
|
554 } else break; |
|
555 } else if (ch === 160) { // '\xa0' |
|
556 ++tokPos; |
|
557 } else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) { |
|
558 ++tokPos; |
|
559 } else { |
|
560 break; |
|
561 } |
|
562 } |
|
563 } |
|
564 |
|
565 // ### Token reading |
|
566 |
|
567 // This is the function that is called to fetch the next token. It |
|
568 // is somewhat obscure, because it works in character codes rather |
|
569 // than characters, and because operator parsing has been inlined |
|
570 // into it. |
|
571 // |
|
572 // All in the name of speed. |
|
573 // |
|
574 // The `forceRegexp` parameter is used in the one case where the |
|
575 // `tokRegexpAllowed` trick does not work. See `parseStatement`. |
|
576 |
|
577 function readToken_dot() { |
|
578 var next = input.charCodeAt(tokPos + 1); |
|
579 if (next >= 48 && next <= 57) return readNumber(true); |
|
580 ++tokPos; |
|
581 return finishToken(_dot); |
|
582 } |
|
583 |
|
584 function readToken_slash() { // '/' |
|
585 var next = input.charCodeAt(tokPos + 1); |
|
586 if (tokRegexpAllowed) {++tokPos; return readRegexp();} |
|
587 if (next === 61) return finishOp(_assign, 2); |
|
588 return finishOp(_slash, 1); |
|
589 } |
|
590 |
|
591 function readToken_mult_modulo() { // '%*' |
|
592 var next = input.charCodeAt(tokPos + 1); |
|
593 if (next === 61) return finishOp(_assign, 2); |
|
594 return finishOp(_multiplyModulo, 1); |
|
595 } |
|
596 |
|
597 function readToken_pipe_amp(code) { // '|&' |
|
598 var next = input.charCodeAt(tokPos + 1); |
|
599 if (next === code) return finishOp(code === 124 ? _logicalOR : _logicalAND, 2); |
|
600 if (next === 61) return finishOp(_assign, 2); |
|
601 return finishOp(code === 124 ? _bitwiseOR : _bitwiseAND, 1); |
|
602 } |
|
603 |
|
604 function readToken_caret() { // '^' |
|
605 var next = input.charCodeAt(tokPos + 1); |
|
606 if (next === 61) return finishOp(_assign, 2); |
|
607 return finishOp(_bitwiseXOR, 1); |
|
608 } |
|
609 |
|
610 function readToken_plus_min(code) { // '+-' |
|
611 var next = input.charCodeAt(tokPos + 1); |
|
612 if (next === code) { |
|
613 if (next == 45 && input.charCodeAt(tokPos + 2) == 62 && |
|
614 newline.test(input.slice(lastEnd, tokPos))) { |
|
615 // A `-->` line comment |
|
616 tokPos += 3; |
|
617 skipLineComment(); |
|
618 skipSpace(); |
|
619 return readToken(); |
|
620 } |
|
621 return finishOp(_incDec, 2); |
|
622 } |
|
623 if (next === 61) return finishOp(_assign, 2); |
|
624 return finishOp(_plusMin, 1); |
|
625 } |
|
626 |
|
627 function readToken_lt_gt(code) { // '<>' |
|
628 var next = input.charCodeAt(tokPos + 1); |
|
629 var size = 1; |
|
630 if (next === code) { |
|
631 size = code === 62 && input.charCodeAt(tokPos + 2) === 62 ? 3 : 2; |
|
632 if (input.charCodeAt(tokPos + size) === 61) return finishOp(_assign, size + 1); |
|
633 return finishOp(_bitShift, size); |
|
634 } |
|
635 if (next == 33 && code == 60 && input.charCodeAt(tokPos + 2) == 45 && |
|
636 input.charCodeAt(tokPos + 3) == 45) { |
|
637 // `<!--`, an XML-style comment that should be interpreted as a line comment |
|
638 tokPos += 4; |
|
639 skipLineComment(); |
|
640 skipSpace(); |
|
641 return readToken(); |
|
642 } |
|
643 if (next === 61) |
|
644 size = input.charCodeAt(tokPos + 2) === 61 ? 3 : 2; |
|
645 return finishOp(_relational, size); |
|
646 } |
|
647 |
|
648 function readToken_eq_excl(code) { // '=!' |
|
649 var next = input.charCodeAt(tokPos + 1); |
|
650 if (next === 61) return finishOp(_equality, input.charCodeAt(tokPos + 2) === 61 ? 3 : 2); |
|
651 return finishOp(code === 61 ? _eq : _prefix, 1); |
|
652 } |
|
653 |
|
654 function getTokenFromCode(code) { |
|
655 switch(code) { |
|
656 // The interpretation of a dot depends on whether it is followed |
|
657 // by a digit. |
|
658 case 46: // '.' |
|
659 return readToken_dot(); |
|
660 |
|
661 // Punctuation tokens. |
|
662 case 40: ++tokPos; return finishToken(_parenL); |
|
663 case 41: ++tokPos; return finishToken(_parenR); |
|
664 case 59: ++tokPos; return finishToken(_semi); |
|
665 case 44: ++tokPos; return finishToken(_comma); |
|
666 case 91: ++tokPos; return finishToken(_bracketL); |
|
667 case 93: ++tokPos; return finishToken(_bracketR); |
|
668 case 123: ++tokPos; return finishToken(_braceL); |
|
669 case 125: ++tokPos; return finishToken(_braceR); |
|
670 case 58: ++tokPos; return finishToken(_colon); |
|
671 case 63: ++tokPos; return finishToken(_question); |
|
672 |
|
673 // '0x' is a hexadecimal number. |
|
674 case 48: // '0' |
|
675 var next = input.charCodeAt(tokPos + 1); |
|
676 if (next === 120 || next === 88) return readHexNumber(); |
|
677 // Anything else beginning with a digit is an integer, octal |
|
678 // number, or float. |
|
679 case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9 |
|
680 return readNumber(false); |
|
681 |
|
682 // Quotes produce strings. |
|
683 case 34: case 39: // '"', "'" |
|
684 return readString(code); |
|
685 |
|
686 // Operators are parsed inline in tiny state machines. '=' (61) is |
|
687 // often referred to. `finishOp` simply skips the amount of |
|
688 // characters it is given as second argument, and returns a token |
|
689 // of the type given by its first argument. |
|
690 |
|
691 case 47: // '/' |
|
692 return readToken_slash(code); |
|
693 |
|
694 case 37: case 42: // '%*' |
|
695 return readToken_mult_modulo(); |
|
696 |
|
697 case 124: case 38: // '|&' |
|
698 return readToken_pipe_amp(code); |
|
699 |
|
700 case 94: // '^' |
|
701 return readToken_caret(); |
|
702 |
|
703 case 43: case 45: // '+-' |
|
704 return readToken_plus_min(code); |
|
705 |
|
706 case 60: case 62: // '<>' |
|
707 return readToken_lt_gt(code); |
|
708 |
|
709 case 61: case 33: // '=!' |
|
710 return readToken_eq_excl(code); |
|
711 |
|
712 case 126: // '~' |
|
713 return finishOp(_prefix, 1); |
|
714 } |
|
715 |
|
716 return false; |
|
717 } |
|
718 |
|
719 function readToken(forceRegexp) { |
|
720 if (!forceRegexp) tokStart = tokPos; |
|
721 else tokPos = tokStart + 1; |
|
722 if (options.locations) tokStartLoc = new line_loc_t; |
|
723 if (forceRegexp) return readRegexp(); |
|
724 if (tokPos >= inputLen) return finishToken(_eof); |
|
725 |
|
726 var code = input.charCodeAt(tokPos); |
|
727 // Identifier or keyword. '\uXXXX' sequences are allowed in |
|
728 // identifiers, so '\' also dispatches to that. |
|
729 if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord(); |
|
730 |
|
731 var tok = getTokenFromCode(code); |
|
732 |
|
733 if (tok === false) { |
|
734 // If we are here, we either found a non-ASCII identifier |
|
735 // character, or something that's entirely disallowed. |
|
736 var ch = String.fromCharCode(code); |
|
737 if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord(); |
|
738 raise(tokPos, "Unexpected character '" + ch + "'"); |
|
739 } |
|
740 return tok; |
|
741 } |
|
742 |
|
743 function finishOp(type, size) { |
|
744 var str = input.slice(tokPos, tokPos + size); |
|
745 tokPos += size; |
|
746 finishToken(type, str); |
|
747 } |
|
748 |
|
749 // Parse a regular expression. Some context-awareness is necessary, |
|
750 // since a '/' inside a '[]' set does not end the expression. |
|
751 |
|
752 function readRegexp() { |
|
753 var content = "", escaped, inClass, start = tokPos; |
|
754 for (;;) { |
|
755 if (tokPos >= inputLen) raise(start, "Unterminated regular expression"); |
|
756 var ch = input.charAt(tokPos); |
|
757 if (newline.test(ch)) raise(start, "Unterminated regular expression"); |
|
758 if (!escaped) { |
|
759 if (ch === "[") inClass = true; |
|
760 else if (ch === "]" && inClass) inClass = false; |
|
761 else if (ch === "/" && !inClass) break; |
|
762 escaped = ch === "\\"; |
|
763 } else escaped = false; |
|
764 ++tokPos; |
|
765 } |
|
766 var content = input.slice(start, tokPos); |
|
767 ++tokPos; |
|
768 // Need to use `readWord1` because '\uXXXX' sequences are allowed |
|
769 // here (don't ask). |
|
770 var mods = readWord1(); |
|
771 if (mods && !/^[gmsiy]*$/.test(mods)) raise(start, "Invalid regexp flag"); |
|
772 try { |
|
773 var value = new RegExp(content, mods); |
|
774 } catch (e) { |
|
775 if (e instanceof SyntaxError) raise(start, e.message); |
|
776 raise(e); |
|
777 } |
|
778 return finishToken(_regexp, value); |
|
779 } |
|
780 |
|
781 // Read an integer in the given radix. Return null if zero digits |
|
782 // were read, the integer value otherwise. When `len` is given, this |
|
783 // will return `null` unless the integer has exactly `len` digits. |
|
784 |
|
785 function readInt(radix, len) { |
|
786 var start = tokPos, total = 0; |
|
787 for (var i = 0, e = len == null ? Infinity : len; i < e; ++i) { |
|
788 var code = input.charCodeAt(tokPos), val; |
|
789 if (code >= 97) val = code - 97 + 10; // a |
|
790 else if (code >= 65) val = code - 65 + 10; // A |
|
791 else if (code >= 48 && code <= 57) val = code - 48; // 0-9 |
|
792 else val = Infinity; |
|
793 if (val >= radix) break; |
|
794 ++tokPos; |
|
795 total = total * radix + val; |
|
796 } |
|
797 if (tokPos === start || len != null && tokPos - start !== len) return null; |
|
798 |
|
799 return total; |
|
800 } |
|
801 |
|
802 function readHexNumber() { |
|
803 tokPos += 2; // 0x |
|
804 var val = readInt(16); |
|
805 if (val == null) raise(tokStart + 2, "Expected hexadecimal number"); |
|
806 if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number"); |
|
807 return finishToken(_num, val); |
|
808 } |
|
809 |
|
810 // Read an integer, octal integer, or floating-point number. |
|
811 |
|
812 function readNumber(startsWithDot) { |
|
813 var start = tokPos, isFloat = false, octal = input.charCodeAt(tokPos) === 48; |
|
814 if (!startsWithDot && readInt(10) === null) raise(start, "Invalid number"); |
|
815 if (input.charCodeAt(tokPos) === 46) { |
|
816 ++tokPos; |
|
817 readInt(10); |
|
818 isFloat = true; |
|
819 } |
|
820 var next = input.charCodeAt(tokPos); |
|
821 if (next === 69 || next === 101) { // 'eE' |
|
822 next = input.charCodeAt(++tokPos); |
|
823 if (next === 43 || next === 45) ++tokPos; // '+-' |
|
824 if (readInt(10) === null) raise(start, "Invalid number"); |
|
825 isFloat = true; |
|
826 } |
|
827 if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number"); |
|
828 |
|
829 var str = input.slice(start, tokPos), val; |
|
830 if (isFloat) val = parseFloat(str); |
|
831 else if (!octal || str.length === 1) val = parseInt(str, 10); |
|
832 else if (/[89]/.test(str) || strict) raise(start, "Invalid number"); |
|
833 else val = parseInt(str, 8); |
|
834 return finishToken(_num, val); |
|
835 } |
|
836 |
|
837 // Read a string value, interpreting backslash-escapes. |
|
838 |
|
839 function readString(quote) { |
|
840 tokPos++; |
|
841 var out = ""; |
|
842 for (;;) { |
|
843 if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant"); |
|
844 var ch = input.charCodeAt(tokPos); |
|
845 if (ch === quote) { |
|
846 ++tokPos; |
|
847 return finishToken(_string, out); |
|
848 } |
|
849 if (ch === 92) { // '\' |
|
850 ch = input.charCodeAt(++tokPos); |
|
851 var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3)); |
|
852 if (octal) octal = octal[0]; |
|
853 while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1); |
|
854 if (octal === "0") octal = null; |
|
855 ++tokPos; |
|
856 if (octal) { |
|
857 if (strict) raise(tokPos - 2, "Octal literal in strict mode"); |
|
858 out += String.fromCharCode(parseInt(octal, 8)); |
|
859 tokPos += octal.length - 1; |
|
860 } else { |
|
861 switch (ch) { |
|
862 case 110: out += "\n"; break; // 'n' -> '\n' |
|
863 case 114: out += "\r"; break; // 'r' -> '\r' |
|
864 case 120: out += String.fromCharCode(readHexChar(2)); break; // 'x' |
|
865 case 117: out += String.fromCharCode(readHexChar(4)); break; // 'u' |
|
866 case 85: out += String.fromCharCode(readHexChar(8)); break; // 'U' |
|
867 case 116: out += "\t"; break; // 't' -> '\t' |
|
868 case 98: out += "\b"; break; // 'b' -> '\b' |
|
869 case 118: out += "\u000b"; break; // 'v' -> '\u000b' |
|
870 case 102: out += "\f"; break; // 'f' -> '\f' |
|
871 case 48: out += "\0"; break; // 0 -> '\0' |
|
872 case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n' |
|
873 case 10: // ' \n' |
|
874 if (options.locations) { tokLineStart = tokPos; ++tokCurLine; } |
|
875 break; |
|
876 default: out += String.fromCharCode(ch); break; |
|
877 } |
|
878 } |
|
879 } else { |
|
880 if (ch === 13 || ch === 10 || ch === 8232 || ch === 8233) raise(tokStart, "Unterminated string constant"); |
|
881 out += String.fromCharCode(ch); // '\' |
|
882 ++tokPos; |
|
883 } |
|
884 } |
|
885 } |
|
886 |
|
887 // Used to read character escape sequences ('\x', '\u', '\U'). |
|
888 |
|
889 function readHexChar(len) { |
|
890 var n = readInt(16, len); |
|
891 if (n === null) raise(tokStart, "Bad character escape sequence"); |
|
892 return n; |
|
893 } |
|
894 |
|
895 // Used to signal to callers of `readWord1` whether the word |
|
896 // contained any escape sequences. This is needed because words with |
|
897 // escape sequences must not be interpreted as keywords. |
|
898 |
|
899 var containsEsc; |
|
900 |
|
901 // Read an identifier, and return it as a string. Sets `containsEsc` |
|
902 // to whether the word contained a '\u' escape. |
|
903 // |
|
904 // Only builds up the word character-by-character when it actually |
|
905 // containeds an escape, as a micro-optimization. |
|
906 |
|
907 function readWord1() { |
|
908 containsEsc = false; |
|
909 var word, first = true, start = tokPos; |
|
910 for (;;) { |
|
911 var ch = input.charCodeAt(tokPos); |
|
912 if (isIdentifierChar(ch)) { |
|
913 if (containsEsc) word += input.charAt(tokPos); |
|
914 ++tokPos; |
|
915 } else if (ch === 92) { // "\" |
|
916 if (!containsEsc) word = input.slice(start, tokPos); |
|
917 containsEsc = true; |
|
918 if (input.charCodeAt(++tokPos) != 117) // "u" |
|
919 raise(tokPos, "Expecting Unicode escape sequence \\uXXXX"); |
|
920 ++tokPos; |
|
921 var esc = readHexChar(4); |
|
922 var escStr = String.fromCharCode(esc); |
|
923 if (!escStr) raise(tokPos - 1, "Invalid Unicode escape"); |
|
924 if (!(first ? isIdentifierStart(esc) : isIdentifierChar(esc))) |
|
925 raise(tokPos - 4, "Invalid Unicode escape"); |
|
926 word += escStr; |
|
927 } else { |
|
928 break; |
|
929 } |
|
930 first = false; |
|
931 } |
|
932 return containsEsc ? word : input.slice(start, tokPos); |
|
933 } |
|
934 |
|
935 // Read an identifier or keyword token. Will check for reserved |
|
936 // words when necessary. |
|
937 |
|
938 function readWord() { |
|
939 var word = readWord1(); |
|
940 var type = _name; |
|
941 if (!containsEsc) { |
|
942 if (isKeyword(word)) type = keywordTypes[word]; |
|
943 else if (options.forbidReserved && |
|
944 (options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(word) || |
|
945 strict && isStrictReservedWord(word)) |
|
946 raise(tokStart, "The keyword '" + word + "' is reserved"); |
|
947 } |
|
948 return finishToken(type, word); |
|
949 } |
|
950 |
|
951 // ## Parser |
|
952 |
|
953 // A recursive descent parser operates by defining functions for all |
|
954 // syntactic elements, and recursively calling those, each function |
|
955 // advancing the input stream and returning an AST node. Precedence |
|
956 // of constructs (for example, the fact that `!x[1]` means `!(x[1])` |
|
957 // instead of `(!x)[1]` is handled by the fact that the parser |
|
958 // function that parses unary prefix operators is called first, and |
|
959 // in turn calls the function that parses `[]` subscripts — that |
|
960 // way, it'll receive the node for `x[1]` already parsed, and wraps |
|
961 // *that* in the unary operator node. |
|
962 // |
|
963 // Acorn uses an [operator precedence parser][opp] to handle binary |
|
964 // operator precedence, because it is much more compact than using |
|
965 // the technique outlined above, which uses different, nesting |
|
966 // functions to specify precedence, for all of the ten binary |
|
967 // precedence levels that JavaScript defines. |
|
968 // |
|
969 // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser |
|
970 |
|
971 // ### Parser utilities |
|
972 |
|
973 // Continue to the next token. |
|
974 |
|
975 function next() { |
|
976 lastStart = tokStart; |
|
977 lastEnd = tokEnd; |
|
978 lastEndLoc = tokEndLoc; |
|
979 readToken(); |
|
980 } |
|
981 |
|
982 // Enter strict mode. Re-reads the next token to please pedantic |
|
983 // tests ("use strict"; 010; -- should fail). |
|
984 |
|
985 function setStrict(strct) { |
|
986 strict = strct; |
|
987 tokPos = tokStart; |
|
988 if (options.locations) { |
|
989 while (tokPos < tokLineStart) { |
|
990 tokLineStart = input.lastIndexOf("\n", tokLineStart - 2) + 1; |
|
991 --tokCurLine; |
|
992 } |
|
993 } |
|
994 skipSpace(); |
|
995 readToken(); |
|
996 } |
|
997 |
|
998 // Start an AST node, attaching a start offset. |
|
999 |
|
1000 function node_t() { |
|
1001 this.type = null; |
|
1002 this.start = tokStart; |
|
1003 this.end = null; |
|
1004 } |
|
1005 |
|
1006 function node_loc_t() { |
|
1007 this.start = tokStartLoc; |
|
1008 this.end = null; |
|
1009 if (sourceFile !== null) this.source = sourceFile; |
|
1010 } |
|
1011 |
|
1012 function startNode() { |
|
1013 var node = new node_t(); |
|
1014 if (options.locations) |
|
1015 node.loc = new node_loc_t(); |
|
1016 if (options.directSourceFile) |
|
1017 node.sourceFile = options.directSourceFile; |
|
1018 if (options.ranges) |
|
1019 node.range = [tokStart, 0]; |
|
1020 return node; |
|
1021 } |
|
1022 |
|
1023 // Start a node whose start offset information should be based on |
|
1024 // the start of another node. For example, a binary operator node is |
|
1025 // only started after its left-hand side has already been parsed. |
|
1026 |
|
1027 function startNodeFrom(other) { |
|
1028 var node = new node_t(); |
|
1029 node.start = other.start; |
|
1030 if (options.locations) { |
|
1031 node.loc = new node_loc_t(); |
|
1032 node.loc.start = other.loc.start; |
|
1033 } |
|
1034 if (options.ranges) |
|
1035 node.range = [other.range[0], 0]; |
|
1036 |
|
1037 return node; |
|
1038 } |
|
1039 |
|
1040 // Finish an AST node, adding `type` and `end` properties. |
|
1041 |
|
1042 function finishNode(node, type) { |
|
1043 node.type = type; |
|
1044 node.end = lastEnd; |
|
1045 if (options.locations) |
|
1046 node.loc.end = lastEndLoc; |
|
1047 if (options.ranges) |
|
1048 node.range[1] = lastEnd; |
|
1049 return node; |
|
1050 } |
|
1051 |
|
1052 // Test whether a statement node is the string literal `"use strict"`. |
|
1053 |
|
1054 function isUseStrict(stmt) { |
|
1055 return options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" && |
|
1056 stmt.expression.type === "Literal" && stmt.expression.value === "use strict"; |
|
1057 } |
|
1058 |
|
1059 // Predicate that tests whether the next token is of the given |
|
1060 // type, and if yes, consumes it as a side effect. |
|
1061 |
|
1062 function eat(type) { |
|
1063 if (tokType === type) { |
|
1064 next(); |
|
1065 return true; |
|
1066 } |
|
1067 } |
|
1068 |
|
1069 // Test whether a semicolon can be inserted at the current position. |
|
1070 |
|
1071 function canInsertSemicolon() { |
|
1072 return !options.strictSemicolons && |
|
1073 (tokType === _eof || tokType === _braceR || newline.test(input.slice(lastEnd, tokStart))); |
|
1074 } |
|
1075 |
|
1076 // Consume a semicolon, or, failing that, see if we are allowed to |
|
1077 // pretend that there is a semicolon at this position. |
|
1078 |
|
1079 function semicolon() { |
|
1080 if (!eat(_semi) && !canInsertSemicolon()) unexpected(); |
|
1081 } |
|
1082 |
|
1083 // Expect a token of a given type. If found, consume it, otherwise, |
|
1084 // raise an unexpected token error. |
|
1085 |
|
1086 function expect(type) { |
|
1087 if (tokType === type) next(); |
|
1088 else unexpected(); |
|
1089 } |
|
1090 |
|
1091 // Raise an unexpected token error. |
|
1092 |
|
1093 function unexpected() { |
|
1094 raise(tokStart, "Unexpected token"); |
|
1095 } |
|
1096 |
|
1097 // Verify that a node is an lval — something that can be assigned |
|
1098 // to. |
|
1099 |
|
1100 function checkLVal(expr) { |
|
1101 if (expr.type !== "Identifier" && expr.type !== "MemberExpression") |
|
1102 raise(expr.start, "Assigning to rvalue"); |
|
1103 if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name)) |
|
1104 raise(expr.start, "Assigning to " + expr.name + " in strict mode"); |
|
1105 } |
|
1106 |
|
1107 // ### Statement parsing |
|
1108 |
|
1109 // Parse a program. Initializes the parser, reads any number of |
|
1110 // statements, and wraps them in a Program node. Optionally takes a |
|
1111 // `program` argument. If present, the statements will be appended |
|
1112 // to its body instead of creating a new node. |
|
1113 |
|
1114 function parseTopLevel(program) { |
|
1115 lastStart = lastEnd = tokPos; |
|
1116 if (options.locations) lastEndLoc = new line_loc_t; |
|
1117 inFunction = strict = null; |
|
1118 labels = []; |
|
1119 readToken(); |
|
1120 |
|
1121 var node = program || startNode(), first = true; |
|
1122 if (!program) node.body = []; |
|
1123 while (tokType !== _eof) { |
|
1124 var stmt = parseStatement(); |
|
1125 node.body.push(stmt); |
|
1126 if (first && isUseStrict(stmt)) setStrict(true); |
|
1127 first = false; |
|
1128 } |
|
1129 return finishNode(node, "Program"); |
|
1130 } |
|
1131 |
|
1132 var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"}; |
|
1133 |
|
1134 // Parse a single statement. |
|
1135 // |
|
1136 // If expecting a statement and finding a slash operator, parse a |
|
1137 // regular expression literal. This is to handle cases like |
|
1138 // `if (foo) /blah/.exec(foo);`, where looking at the previous token |
|
1139 // does not help. |
|
1140 |
|
1141 function parseStatement() { |
|
1142 if (tokType === _slash || tokType === _assign && tokVal == "/=") |
|
1143 readToken(true); |
|
1144 |
|
1145 var starttype = tokType, node = startNode(); |
|
1146 |
|
1147 // Most types of statements are recognized by the keyword they |
|
1148 // start with. Many are trivial to parse, some require a bit of |
|
1149 // complexity. |
|
1150 |
|
1151 switch (starttype) { |
|
1152 case _break: case _continue: |
|
1153 next(); |
|
1154 var isBreak = starttype === _break; |
|
1155 if (eat(_semi) || canInsertSemicolon()) node.label = null; |
|
1156 else if (tokType !== _name) unexpected(); |
|
1157 else { |
|
1158 node.label = parseIdent(); |
|
1159 semicolon(); |
|
1160 } |
|
1161 |
|
1162 // Verify that there is an actual destination to break or |
|
1163 // continue to. |
|
1164 for (var i = 0; i < labels.length; ++i) { |
|
1165 var lab = labels[i]; |
|
1166 if (node.label == null || lab.name === node.label.name) { |
|
1167 if (lab.kind != null && (isBreak || lab.kind === "loop")) break; |
|
1168 if (node.label && isBreak) break; |
|
1169 } |
|
1170 } |
|
1171 if (i === labels.length) raise(node.start, "Unsyntactic " + starttype.keyword); |
|
1172 return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement"); |
|
1173 |
|
1174 case _debugger: |
|
1175 next(); |
|
1176 semicolon(); |
|
1177 return finishNode(node, "DebuggerStatement"); |
|
1178 |
|
1179 case _do: |
|
1180 next(); |
|
1181 labels.push(loopLabel); |
|
1182 node.body = parseStatement(); |
|
1183 labels.pop(); |
|
1184 expect(_while); |
|
1185 node.test = parseParenExpression(); |
|
1186 semicolon(); |
|
1187 return finishNode(node, "DoWhileStatement"); |
|
1188 |
|
1189 // Disambiguating between a `for` and a `for`/`in` loop is |
|
1190 // non-trivial. Basically, we have to parse the init `var` |
|
1191 // statement or expression, disallowing the `in` operator (see |
|
1192 // the second parameter to `parseExpression`), and then check |
|
1193 // whether the next token is `in`. When there is no init part |
|
1194 // (semicolon immediately after the opening parenthesis), it is |
|
1195 // a regular `for` loop. |
|
1196 |
|
1197 case _for: |
|
1198 next(); |
|
1199 labels.push(loopLabel); |
|
1200 expect(_parenL); |
|
1201 if (tokType === _semi) return parseFor(node, null); |
|
1202 if (tokType === _var) { |
|
1203 var init = startNode(); |
|
1204 next(); |
|
1205 parseVar(init, true); |
|
1206 finishNode(init, "VariableDeclaration"); |
|
1207 if (init.declarations.length === 1 && eat(_in)) |
|
1208 return parseForIn(node, init); |
|
1209 return parseFor(node, init); |
|
1210 } |
|
1211 var init = parseExpression(false, true); |
|
1212 if (eat(_in)) {checkLVal(init); return parseForIn(node, init);} |
|
1213 return parseFor(node, init); |
|
1214 |
|
1215 case _function: |
|
1216 next(); |
|
1217 return parseFunction(node, true); |
|
1218 |
|
1219 case _if: |
|
1220 next(); |
|
1221 node.test = parseParenExpression(); |
|
1222 node.consequent = parseStatement(); |
|
1223 node.alternate = eat(_else) ? parseStatement() : null; |
|
1224 return finishNode(node, "IfStatement"); |
|
1225 |
|
1226 case _return: |
|
1227 if (!inFunction) raise(tokStart, "'return' outside of function"); |
|
1228 next(); |
|
1229 |
|
1230 // In `return` (and `break`/`continue`), the keywords with |
|
1231 // optional arguments, we eagerly look for a semicolon or the |
|
1232 // possibility to insert one. |
|
1233 |
|
1234 if (eat(_semi) || canInsertSemicolon()) node.argument = null; |
|
1235 else { node.argument = parseExpression(); semicolon(); } |
|
1236 return finishNode(node, "ReturnStatement"); |
|
1237 |
|
1238 case _switch: |
|
1239 next(); |
|
1240 node.discriminant = parseParenExpression(); |
|
1241 node.cases = []; |
|
1242 expect(_braceL); |
|
1243 labels.push(switchLabel); |
|
1244 |
|
1245 // Statements under must be grouped (by label) in SwitchCase |
|
1246 // nodes. `cur` is used to keep the node that we are currently |
|
1247 // adding statements to. |
|
1248 |
|
1249 for (var cur, sawDefault; tokType != _braceR;) { |
|
1250 if (tokType === _case || tokType === _default) { |
|
1251 var isCase = tokType === _case; |
|
1252 if (cur) finishNode(cur, "SwitchCase"); |
|
1253 node.cases.push(cur = startNode()); |
|
1254 cur.consequent = []; |
|
1255 next(); |
|
1256 if (isCase) cur.test = parseExpression(); |
|
1257 else { |
|
1258 if (sawDefault) raise(lastStart, "Multiple default clauses"); sawDefault = true; |
|
1259 cur.test = null; |
|
1260 } |
|
1261 expect(_colon); |
|
1262 } else { |
|
1263 if (!cur) unexpected(); |
|
1264 cur.consequent.push(parseStatement()); |
|
1265 } |
|
1266 } |
|
1267 if (cur) finishNode(cur, "SwitchCase"); |
|
1268 next(); // Closing brace |
|
1269 labels.pop(); |
|
1270 return finishNode(node, "SwitchStatement"); |
|
1271 |
|
1272 case _throw: |
|
1273 next(); |
|
1274 if (newline.test(input.slice(lastEnd, tokStart))) |
|
1275 raise(lastEnd, "Illegal newline after throw"); |
|
1276 node.argument = parseExpression(); |
|
1277 semicolon(); |
|
1278 return finishNode(node, "ThrowStatement"); |
|
1279 |
|
1280 case _try: |
|
1281 next(); |
|
1282 node.block = parseBlock(); |
|
1283 node.handler = null; |
|
1284 if (tokType === _catch) { |
|
1285 var clause = startNode(); |
|
1286 next(); |
|
1287 expect(_parenL); |
|
1288 clause.param = parseIdent(); |
|
1289 if (strict && isStrictBadIdWord(clause.param.name)) |
|
1290 raise(clause.param.start, "Binding " + clause.param.name + " in strict mode"); |
|
1291 expect(_parenR); |
|
1292 clause.guard = null; |
|
1293 clause.body = parseBlock(); |
|
1294 node.handler = finishNode(clause, "CatchClause"); |
|
1295 } |
|
1296 node.guardedHandlers = empty; |
|
1297 node.finalizer = eat(_finally) ? parseBlock() : null; |
|
1298 if (!node.handler && !node.finalizer) |
|
1299 raise(node.start, "Missing catch or finally clause"); |
|
1300 return finishNode(node, "TryStatement"); |
|
1301 |
|
1302 case _var: |
|
1303 next(); |
|
1304 parseVar(node); |
|
1305 semicolon(); |
|
1306 return finishNode(node, "VariableDeclaration"); |
|
1307 |
|
1308 case _while: |
|
1309 next(); |
|
1310 node.test = parseParenExpression(); |
|
1311 labels.push(loopLabel); |
|
1312 node.body = parseStatement(); |
|
1313 labels.pop(); |
|
1314 return finishNode(node, "WhileStatement"); |
|
1315 |
|
1316 case _with: |
|
1317 if (strict) raise(tokStart, "'with' in strict mode"); |
|
1318 next(); |
|
1319 node.object = parseParenExpression(); |
|
1320 node.body = parseStatement(); |
|
1321 return finishNode(node, "WithStatement"); |
|
1322 |
|
1323 case _braceL: |
|
1324 return parseBlock(); |
|
1325 |
|
1326 case _semi: |
|
1327 next(); |
|
1328 return finishNode(node, "EmptyStatement"); |
|
1329 |
|
1330 // If the statement does not start with a statement keyword or a |
|
1331 // brace, it's an ExpressionStatement or LabeledStatement. We |
|
1332 // simply start parsing an expression, and afterwards, if the |
|
1333 // next token is a colon and the expression was a simple |
|
1334 // Identifier node, we switch to interpreting it as a label. |
|
1335 |
|
1336 default: |
|
1337 var maybeName = tokVal, expr = parseExpression(); |
|
1338 if (starttype === _name && expr.type === "Identifier" && eat(_colon)) { |
|
1339 for (var i = 0; i < labels.length; ++i) |
|
1340 if (labels[i].name === maybeName) raise(expr.start, "Label '" + maybeName + "' is already declared"); |
|
1341 var kind = tokType.isLoop ? "loop" : tokType === _switch ? "switch" : null; |
|
1342 labels.push({name: maybeName, kind: kind}); |
|
1343 node.body = parseStatement(); |
|
1344 labels.pop(); |
|
1345 node.label = expr; |
|
1346 return finishNode(node, "LabeledStatement"); |
|
1347 } else { |
|
1348 node.expression = expr; |
|
1349 semicolon(); |
|
1350 return finishNode(node, "ExpressionStatement"); |
|
1351 } |
|
1352 } |
|
1353 } |
|
1354 |
|
1355 // Used for constructs like `switch` and `if` that insist on |
|
1356 // parentheses around their expression. |
|
1357 |
|
1358 function parseParenExpression() { |
|
1359 expect(_parenL); |
|
1360 var val = parseExpression(); |
|
1361 expect(_parenR); |
|
1362 return val; |
|
1363 } |
|
1364 |
|
1365 // Parse a semicolon-enclosed block of statements, handling `"use |
|
1366 // strict"` declarations when `allowStrict` is true (used for |
|
1367 // function bodies). |
|
1368 |
|
1369 function parseBlock(allowStrict) { |
|
1370 var node = startNode(), first = true, strict = false, oldStrict; |
|
1371 node.body = []; |
|
1372 expect(_braceL); |
|
1373 while (!eat(_braceR)) { |
|
1374 var stmt = parseStatement(); |
|
1375 node.body.push(stmt); |
|
1376 if (first && allowStrict && isUseStrict(stmt)) { |
|
1377 oldStrict = strict; |
|
1378 setStrict(strict = true); |
|
1379 } |
|
1380 first = false; |
|
1381 } |
|
1382 if (strict && !oldStrict) setStrict(false); |
|
1383 return finishNode(node, "BlockStatement"); |
|
1384 } |
|
1385 |
|
1386 // Parse a regular `for` loop. The disambiguation code in |
|
1387 // `parseStatement` will already have parsed the init statement or |
|
1388 // expression. |
|
1389 |
|
1390 function parseFor(node, init) { |
|
1391 node.init = init; |
|
1392 expect(_semi); |
|
1393 node.test = tokType === _semi ? null : parseExpression(); |
|
1394 expect(_semi); |
|
1395 node.update = tokType === _parenR ? null : parseExpression(); |
|
1396 expect(_parenR); |
|
1397 node.body = parseStatement(); |
|
1398 labels.pop(); |
|
1399 return finishNode(node, "ForStatement"); |
|
1400 } |
|
1401 |
|
1402 // Parse a `for`/`in` loop. |
|
1403 |
|
1404 function parseForIn(node, init) { |
|
1405 node.left = init; |
|
1406 node.right = parseExpression(); |
|
1407 expect(_parenR); |
|
1408 node.body = parseStatement(); |
|
1409 labels.pop(); |
|
1410 return finishNode(node, "ForInStatement"); |
|
1411 } |
|
1412 |
|
1413 // Parse a list of variable declarations. |
|
1414 |
|
1415 function parseVar(node, noIn) { |
|
1416 node.declarations = []; |
|
1417 node.kind = "var"; |
|
1418 for (;;) { |
|
1419 var decl = startNode(); |
|
1420 decl.id = parseIdent(); |
|
1421 if (strict && isStrictBadIdWord(decl.id.name)) |
|
1422 raise(decl.id.start, "Binding " + decl.id.name + " in strict mode"); |
|
1423 decl.init = eat(_eq) ? parseExpression(true, noIn) : null; |
|
1424 node.declarations.push(finishNode(decl, "VariableDeclarator")); |
|
1425 if (!eat(_comma)) break; |
|
1426 } |
|
1427 return node; |
|
1428 } |
|
1429 |
|
1430 // ### Expression parsing |
|
1431 |
|
1432 // These nest, from the most general expression type at the top to |
|
1433 // 'atomic', nondivisible expression types at the bottom. Most of |
|
1434 // the functions will simply let the function(s) below them parse, |
|
1435 // and, *if* the syntactic construct they handle is present, wrap |
|
1436 // the AST node that the inner parser gave them in another node. |
|
1437 |
|
1438 // Parse a full expression. The arguments are used to forbid comma |
|
1439 // sequences (in argument lists, array literals, or object literals) |
|
1440 // or the `in` operator (in for loops initalization expressions). |
|
1441 |
|
1442 function parseExpression(noComma, noIn) { |
|
1443 var expr = parseMaybeAssign(noIn); |
|
1444 if (!noComma && tokType === _comma) { |
|
1445 var node = startNodeFrom(expr); |
|
1446 node.expressions = [expr]; |
|
1447 while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn)); |
|
1448 return finishNode(node, "SequenceExpression"); |
|
1449 } |
|
1450 return expr; |
|
1451 } |
|
1452 |
|
1453 // Parse an assignment expression. This includes applications of |
|
1454 // operators like `+=`. |
|
1455 |
|
1456 function parseMaybeAssign(noIn) { |
|
1457 var left = parseMaybeConditional(noIn); |
|
1458 if (tokType.isAssign) { |
|
1459 var node = startNodeFrom(left); |
|
1460 node.operator = tokVal; |
|
1461 node.left = left; |
|
1462 next(); |
|
1463 node.right = parseMaybeAssign(noIn); |
|
1464 checkLVal(left); |
|
1465 return finishNode(node, "AssignmentExpression"); |
|
1466 } |
|
1467 return left; |
|
1468 } |
|
1469 |
|
1470 // Parse a ternary conditional (`?:`) operator. |
|
1471 |
|
1472 function parseMaybeConditional(noIn) { |
|
1473 var expr = parseExprOps(noIn); |
|
1474 if (eat(_question)) { |
|
1475 var node = startNodeFrom(expr); |
|
1476 node.test = expr; |
|
1477 node.consequent = parseExpression(true); |
|
1478 expect(_colon); |
|
1479 node.alternate = parseExpression(true, noIn); |
|
1480 return finishNode(node, "ConditionalExpression"); |
|
1481 } |
|
1482 return expr; |
|
1483 } |
|
1484 |
|
1485 // Start the precedence parser. |
|
1486 |
|
1487 function parseExprOps(noIn) { |
|
1488 return parseExprOp(parseMaybeUnary(), -1, noIn); |
|
1489 } |
|
1490 |
|
1491 // Parse binary operators with the operator precedence parsing |
|
1492 // algorithm. `left` is the left-hand side of the operator. |
|
1493 // `minPrec` provides context that allows the function to stop and |
|
1494 // defer further parser to one of its callers when it encounters an |
|
1495 // operator that has a lower precedence than the set it is parsing. |
|
1496 |
|
1497 function parseExprOp(left, minPrec, noIn) { |
|
1498 var prec = tokType.binop; |
|
1499 if (prec != null && (!noIn || tokType !== _in)) { |
|
1500 if (prec > minPrec) { |
|
1501 var node = startNodeFrom(left); |
|
1502 node.left = left; |
|
1503 node.operator = tokVal; |
|
1504 var op = tokType; |
|
1505 next(); |
|
1506 node.right = parseExprOp(parseMaybeUnary(), prec, noIn); |
|
1507 var exprNode = finishNode(node, (op === _logicalOR || op === _logicalAND) ? "LogicalExpression" : "BinaryExpression"); |
|
1508 return parseExprOp(exprNode, minPrec, noIn); |
|
1509 } |
|
1510 } |
|
1511 return left; |
|
1512 } |
|
1513 |
|
1514 // Parse unary operators, both prefix and postfix. |
|
1515 |
|
1516 function parseMaybeUnary() { |
|
1517 if (tokType.prefix) { |
|
1518 var node = startNode(), update = tokType.isUpdate; |
|
1519 node.operator = tokVal; |
|
1520 node.prefix = true; |
|
1521 tokRegexpAllowed = true; |
|
1522 next(); |
|
1523 node.argument = parseMaybeUnary(); |
|
1524 if (update) checkLVal(node.argument); |
|
1525 else if (strict && node.operator === "delete" && |
|
1526 node.argument.type === "Identifier") |
|
1527 raise(node.start, "Deleting local variable in strict mode"); |
|
1528 return finishNode(node, update ? "UpdateExpression" : "UnaryExpression"); |
|
1529 } |
|
1530 var expr = parseExprSubscripts(); |
|
1531 while (tokType.postfix && !canInsertSemicolon()) { |
|
1532 var node = startNodeFrom(expr); |
|
1533 node.operator = tokVal; |
|
1534 node.prefix = false; |
|
1535 node.argument = expr; |
|
1536 checkLVal(expr); |
|
1537 next(); |
|
1538 expr = finishNode(node, "UpdateExpression"); |
|
1539 } |
|
1540 return expr; |
|
1541 } |
|
1542 |
|
1543 // Parse call, dot, and `[]`-subscript expressions. |
|
1544 |
|
1545 function parseExprSubscripts() { |
|
1546 return parseSubscripts(parseExprAtom()); |
|
1547 } |
|
1548 |
|
1549 function parseSubscripts(base, noCalls) { |
|
1550 if (eat(_dot)) { |
|
1551 var node = startNodeFrom(base); |
|
1552 node.object = base; |
|
1553 node.property = parseIdent(true); |
|
1554 node.computed = false; |
|
1555 return parseSubscripts(finishNode(node, "MemberExpression"), noCalls); |
|
1556 } else if (eat(_bracketL)) { |
|
1557 var node = startNodeFrom(base); |
|
1558 node.object = base; |
|
1559 node.property = parseExpression(); |
|
1560 node.computed = true; |
|
1561 expect(_bracketR); |
|
1562 return parseSubscripts(finishNode(node, "MemberExpression"), noCalls); |
|
1563 } else if (!noCalls && eat(_parenL)) { |
|
1564 var node = startNodeFrom(base); |
|
1565 node.callee = base; |
|
1566 node.arguments = parseExprList(_parenR, false); |
|
1567 return parseSubscripts(finishNode(node, "CallExpression"), noCalls); |
|
1568 } else return base; |
|
1569 } |
|
1570 |
|
1571 // Parse an atomic expression — either a single token that is an |
|
1572 // expression, an expression started by a keyword like `function` or |
|
1573 // `new`, or an expression wrapped in punctuation like `()`, `[]`, |
|
1574 // or `{}`. |
|
1575 |
|
1576 function parseExprAtom() { |
|
1577 switch (tokType) { |
|
1578 case _this: |
|
1579 var node = startNode(); |
|
1580 next(); |
|
1581 return finishNode(node, "ThisExpression"); |
|
1582 case _name: |
|
1583 return parseIdent(); |
|
1584 case _num: case _string: case _regexp: |
|
1585 var node = startNode(); |
|
1586 node.value = tokVal; |
|
1587 node.raw = input.slice(tokStart, tokEnd); |
|
1588 next(); |
|
1589 return finishNode(node, "Literal"); |
|
1590 |
|
1591 case _null: case _true: case _false: |
|
1592 var node = startNode(); |
|
1593 node.value = tokType.atomValue; |
|
1594 node.raw = tokType.keyword; |
|
1595 next(); |
|
1596 return finishNode(node, "Literal"); |
|
1597 |
|
1598 case _parenL: |
|
1599 var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart; |
|
1600 next(); |
|
1601 var val = parseExpression(); |
|
1602 val.start = tokStart1; |
|
1603 val.end = tokEnd; |
|
1604 if (options.locations) { |
|
1605 val.loc.start = tokStartLoc1; |
|
1606 val.loc.end = tokEndLoc; |
|
1607 } |
|
1608 if (options.ranges) |
|
1609 val.range = [tokStart1, tokEnd]; |
|
1610 expect(_parenR); |
|
1611 return val; |
|
1612 |
|
1613 case _bracketL: |
|
1614 var node = startNode(); |
|
1615 next(); |
|
1616 node.elements = parseExprList(_bracketR, true, true); |
|
1617 return finishNode(node, "ArrayExpression"); |
|
1618 |
|
1619 case _braceL: |
|
1620 return parseObj(); |
|
1621 |
|
1622 case _function: |
|
1623 var node = startNode(); |
|
1624 next(); |
|
1625 return parseFunction(node, false); |
|
1626 |
|
1627 case _new: |
|
1628 return parseNew(); |
|
1629 |
|
1630 default: |
|
1631 unexpected(); |
|
1632 } |
|
1633 } |
|
1634 |
|
1635 // New's precedence is slightly tricky. It must allow its argument |
|
1636 // to be a `[]` or dot subscript expression, but not a call — at |
|
1637 // least, not without wrapping it in parentheses. Thus, it uses the |
|
1638 |
|
1639 function parseNew() { |
|
1640 var node = startNode(); |
|
1641 next(); |
|
1642 node.callee = parseSubscripts(parseExprAtom(), true); |
|
1643 if (eat(_parenL)) node.arguments = parseExprList(_parenR, false); |
|
1644 else node.arguments = empty; |
|
1645 return finishNode(node, "NewExpression"); |
|
1646 } |
|
1647 |
|
1648 // Parse an object literal. |
|
1649 |
|
1650 function parseObj() { |
|
1651 var node = startNode(), first = true, sawGetSet = false; |
|
1652 node.properties = []; |
|
1653 next(); |
|
1654 while (!eat(_braceR)) { |
|
1655 if (!first) { |
|
1656 expect(_comma); |
|
1657 if (options.allowTrailingCommas && eat(_braceR)) break; |
|
1658 } else first = false; |
|
1659 |
|
1660 var prop = {key: parsePropertyName()}, isGetSet = false, kind; |
|
1661 if (eat(_colon)) { |
|
1662 prop.value = parseExpression(true); |
|
1663 kind = prop.kind = "init"; |
|
1664 } else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" && |
|
1665 (prop.key.name === "get" || prop.key.name === "set")) { |
|
1666 isGetSet = sawGetSet = true; |
|
1667 kind = prop.kind = prop.key.name; |
|
1668 prop.key = parsePropertyName(); |
|
1669 if (tokType !== _parenL) unexpected(); |
|
1670 prop.value = parseFunction(startNode(), false); |
|
1671 } else unexpected(); |
|
1672 |
|
1673 // getters and setters are not allowed to clash — either with |
|
1674 // each other or with an init property — and in strict mode, |
|
1675 // init properties are also not allowed to be repeated. |
|
1676 |
|
1677 if (prop.key.type === "Identifier" && (strict || sawGetSet)) { |
|
1678 for (var i = 0; i < node.properties.length; ++i) { |
|
1679 var other = node.properties[i]; |
|
1680 if (other.key.name === prop.key.name) { |
|
1681 var conflict = kind == other.kind || isGetSet && other.kind === "init" || |
|
1682 kind === "init" && (other.kind === "get" || other.kind === "set"); |
|
1683 if (conflict && !strict && kind === "init" && other.kind === "init") conflict = false; |
|
1684 if (conflict) raise(prop.key.start, "Redefinition of property"); |
|
1685 } |
|
1686 } |
|
1687 } |
|
1688 node.properties.push(prop); |
|
1689 } |
|
1690 return finishNode(node, "ObjectExpression"); |
|
1691 } |
|
1692 |
|
1693 function parsePropertyName() { |
|
1694 if (tokType === _num || tokType === _string) return parseExprAtom(); |
|
1695 return parseIdent(true); |
|
1696 } |
|
1697 |
|
1698 // Parse a function declaration or literal (depending on the |
|
1699 // `isStatement` parameter). |
|
1700 |
|
1701 function parseFunction(node, isStatement) { |
|
1702 if (tokType === _name) node.id = parseIdent(); |
|
1703 else if (isStatement) unexpected(); |
|
1704 else node.id = null; |
|
1705 node.params = []; |
|
1706 var first = true; |
|
1707 expect(_parenL); |
|
1708 while (!eat(_parenR)) { |
|
1709 if (!first) expect(_comma); else first = false; |
|
1710 node.params.push(parseIdent()); |
|
1711 } |
|
1712 |
|
1713 // Start a new scope with regard to labels and the `inFunction` |
|
1714 // flag (restore them to their old value afterwards). |
|
1715 var oldInFunc = inFunction, oldLabels = labels; |
|
1716 inFunction = true; labels = []; |
|
1717 node.body = parseBlock(true); |
|
1718 inFunction = oldInFunc; labels = oldLabels; |
|
1719 |
|
1720 // If this is a strict mode function, verify that argument names |
|
1721 // are not repeated, and it does not try to bind the words `eval` |
|
1722 // or `arguments`. |
|
1723 if (strict || node.body.body.length && isUseStrict(node.body.body[0])) { |
|
1724 for (var i = node.id ? -1 : 0; i < node.params.length; ++i) { |
|
1725 var id = i < 0 ? node.id : node.params[i]; |
|
1726 if (isStrictReservedWord(id.name) || isStrictBadIdWord(id.name)) |
|
1727 raise(id.start, "Defining '" + id.name + "' in strict mode"); |
|
1728 if (i >= 0) for (var j = 0; j < i; ++j) if (id.name === node.params[j].name) |
|
1729 raise(id.start, "Argument name clash in strict mode"); |
|
1730 } |
|
1731 } |
|
1732 |
|
1733 return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); |
|
1734 } |
|
1735 |
|
1736 // Parses a comma-separated list of expressions, and returns them as |
|
1737 // an array. `close` is the token type that ends the list, and |
|
1738 // `allowEmpty` can be turned on to allow subsequent commas with |
|
1739 // nothing in between them to be parsed as `null` (which is needed |
|
1740 // for array literals). |
|
1741 |
|
1742 function parseExprList(close, allowTrailingComma, allowEmpty) { |
|
1743 var elts = [], first = true; |
|
1744 while (!eat(close)) { |
|
1745 if (!first) { |
|
1746 expect(_comma); |
|
1747 if (allowTrailingComma && options.allowTrailingCommas && eat(close)) break; |
|
1748 } else first = false; |
|
1749 |
|
1750 if (allowEmpty && tokType === _comma) elts.push(null); |
|
1751 else elts.push(parseExpression(true)); |
|
1752 } |
|
1753 return elts; |
|
1754 } |
|
1755 |
|
1756 // Parse the next token as an identifier. If `liberal` is true (used |
|
1757 // when parsing properties), it will also convert keywords into |
|
1758 // identifiers. |
|
1759 |
|
1760 function parseIdent(liberal) { |
|
1761 var node = startNode(); |
|
1762 node.name = tokType === _name ? tokVal : (liberal && !options.forbidReserved && tokType.keyword) || unexpected(); |
|
1763 tokRegexpAllowed = false; |
|
1764 next(); |
|
1765 return finishNode(node, "Identifier"); |
|
1766 } |
|
1767 |
|
1768 }); |