esperanto-source 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 604c8cad45226941fd94e70045d115f5ab5375cc
4
+ data.tar.gz: 07ab5320828f2a0acfce8c7fd36862567cadc990
5
+ SHA512:
6
+ metadata.gz: c72ba245664ca25529855bc9552c34a42d590afb58ffff47faf955bd2733493bb2a962216008ed072bdc997596d6f04102a217d181863d379d0460467d45f724
7
+ data.tar.gz: 4299c8e58995567cba7ab4ba3ba08fd742dd4d74dda10f780948890ee2d721cba1cd72357ff4972b9af95e6d1d278632c748a7ea2df8cffd512fc711e26d997f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+
5
+ rvm:
6
+ - 2.0.0
7
+ - 2.1
8
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in esperanto-source.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ryunosuke SATO
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Esperanto::Source
2
+
3
+ [esperanto](http://esperantojs.org) for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'esperanto-source'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install esperanto-source
20
+
21
+ ## Usage
22
+
23
+ ``` ruby
24
+ require 'esperanto/source'
25
+
26
+ Esperanto::Source.bundled_path_for('esperanto.js')
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.warning = true
9
+ end
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'esperanto/source/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'esperanto-source'
7
+ spec.version = Esperanto::Source::VERSION
8
+ spec.authors = ['Ryunosuke SATO']
9
+ spec.email = ['tricknotes.rs@gmail.com']
10
+ spec.summary = %q{esperanto for Ruby}
11
+ spec.description = %q{esperanto for Ruby}
12
+ spec.homepage = 'https://github.com/tricknotes/ruby-esperanto-source'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'minitest'
23
+ spec.add_development_dependency 'minitest-power_assert'
24
+ spec.add_development_dependency 'execjs'
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'esperanto/source/version'
2
+
3
+ module Esperanto
4
+ module Source
5
+ class << self
6
+ def bundled_path_for(filename)
7
+ File.expand_path("../../../vendor/#{filename}", __FILE__)
8
+ end
9
+
10
+ def bundled_paths
11
+ %w(
12
+ acorn.js
13
+ estraverse.js
14
+ vlq.js
15
+ magic-string.js
16
+ esperanto.browser.js
17
+ ).map {|js| bundled_path_for(js) }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Esperanto
2
+ module Source
3
+ VERSION = '0.6.8'
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/power_assert'
3
+ require 'execjs'
4
+
5
+ require 'esperanto/source'
6
+
7
+ class TestEsperantoSource < MiniTest::Test
8
+ def test_bundled_file
9
+ assert { Esperanto::Source.bundled_path_for('esperanto.js') }
10
+ assert { File.exist?(Esperanto::Source.bundled_path_for('esperanto.js')) }
11
+
12
+ assert { Esperanto::Source.bundled_path_for('esperanto.browser.js') }
13
+ assert { File.exist?(Esperanto::Source.bundled_path_for('esperanto.browser.js')) }
14
+ end
15
+
16
+ def test_bundled_paths
17
+ assert { Esperanto::Source.bundled_paths.size > 0 }
18
+
19
+ Esperanto::Source.bundled_paths.each do |path|
20
+ assert { File.exist?(path) }
21
+ end
22
+ end
23
+
24
+ def test_bundled_files_executable
25
+ source = Esperanto::Source.bundled_paths.map {|path| File.read(path) }.join
26
+ context = ExecJS::Runtimes::Node.compile(source) # Currently, other runtimes are not supported.
27
+
28
+ assert { context.eval('typeof esperanto') == 'object' }
29
+ end
30
+ end
data/vendor/acorn.js ADDED
@@ -0,0 +1,2529 @@
1
+ // Acorn is a tiny, fast JavaScript parser written in JavaScript.
2
+ //
3
+ // Acorn was written by Marijn Haverbeke and various contributors and
4
+ // released under an MIT license. The Unicode regexps (for identifiers
5
+ // and whitespace) were taken from [Esprima](http://esprima.org) by
6
+ // Ariya Hidayat.
7
+ //
8
+ // Git repositories for Acorn are available at
9
+ //
10
+ // http://marijnhaverbeke.nl/git/acorn
11
+ // https://github.com/marijnh/acorn.git
12
+ //
13
+ // Please use the [github bug tracker][ghbt] to report issues.
14
+ //
15
+ // [ghbt]: https://github.com/marijnh/acorn/issues
16
+ //
17
+ // This file defines the main parser interface. The library also comes
18
+ // with a [error-tolerant parser][dammit] and an
19
+ // [abstract syntax tree walker][walk], defined in other files.
20
+ //
21
+ // [dammit]: acorn_loose.js
22
+ // [walk]: util/walk.js
23
+
24
+ (function(root, mod) {
25
+ if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
26
+ if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
27
+ mod(root.acorn || (root.acorn = {})); // Plain browser env
28
+ })(this, function(exports) {
29
+ "use strict";
30
+
31
+ exports.version = "0.7.0";
32
+
33
+ // The main exported interface (under `self.acorn` when in the
34
+ // browser) is a `parse` function that takes a code string and
35
+ // returns an abstract syntax tree as specified by [Mozilla parser
36
+ // API][api], with the caveat that inline XML 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, or 6. This influences support for strict
55
+ // mode, the set of reserved words, support for getters and
56
+ // setters and other features.
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. When this option has the
66
+ // value "everywhere", reserved words and keywords can also not be
67
+ // used as property names.
68
+ forbidReserved: false,
69
+ // When enabled, a return at the top level is not considered an
70
+ // error.
71
+ allowReturnOutsideFunction: false,
72
+ // When `locations` is on, `loc` properties holding objects with
73
+ // `start` and `end` properties in `{line, column}` form (with
74
+ // line being 1-based and column 0-based) will be attached to the
75
+ // nodes.
76
+ locations: false,
77
+ // A function can be passed as `onToken` option, which will
78
+ // cause Acorn to call that function with object in the same
79
+ // format as tokenize() returns. Note that you are not
80
+ // allowed to call the parser from the callback—that will
81
+ // corrupt its internal state.
82
+ onToken: null,
83
+ // A function can be passed as `onComment` option, which will
84
+ // cause Acorn to call that function with `(block, text, start,
85
+ // end)` parameters whenever a comment is skipped. `block` is a
86
+ // boolean indicating whether this is a block (`/* */`) comment,
87
+ // `text` is the content of the comment, and `start` and `end` are
88
+ // character offsets that denote the start and end of the comment.
89
+ // When the `locations` option is on, two more parameters are
90
+ // passed, the full `{line, column}` locations of the start and
91
+ // end of the comments. Note that you are not allowed to call the
92
+ // parser from the callback—that will corrupt its internal state.
93
+ onComment: null,
94
+ // Nodes have their start and end characters offsets recorded in
95
+ // `start` and `end` properties (directly on the node, rather than
96
+ // the `loc` object, which holds line/column data. To also add a
97
+ // [semi-standardized][range] `range` property holding a `[start,
98
+ // end]` array with the same numbers, set the `ranges` option to
99
+ // `true`.
100
+ //
101
+ // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
102
+ ranges: false,
103
+ // It is possible to parse multiple files into a single AST by
104
+ // passing the tree produced by parsing the first file as
105
+ // `program` option in subsequent parses. This will add the
106
+ // toplevel forms of the parsed file to the `Program` (top) node
107
+ // of an existing parse tree.
108
+ program: null,
109
+ // When `locations` is on, you can pass this to record the source
110
+ // file in every node's `loc` object.
111
+ sourceFile: null,
112
+ // This value, if given, is stored in every node, whether
113
+ // `locations` is on or off.
114
+ directSourceFile: null
115
+ };
116
+
117
+ function setOptions(opts) {
118
+ options = opts || {};
119
+ for (var opt in defaultOptions) if (!has(options, opt))
120
+ options[opt] = defaultOptions[opt];
121
+ sourceFile = options.sourceFile || null;
122
+
123
+ isKeyword = options.ecmaVersion >= 6 ? isEcma6Keyword : isEcma5AndLessKeyword;
124
+ }
125
+
126
+ // The `getLineInfo` function is mostly useful when the
127
+ // `locations` option is off (for performance reasons) and you
128
+ // want to find the line/column position for a given character
129
+ // offset. `input` should be the code string that the offset refers
130
+ // into.
131
+
132
+ var getLineInfo = exports.getLineInfo = function(input, offset) {
133
+ for (var line = 1, cur = 0;;) {
134
+ lineBreak.lastIndex = cur;
135
+ var match = lineBreak.exec(input);
136
+ if (match && match.index < offset) {
137
+ ++line;
138
+ cur = match.index + match[0].length;
139
+ } else break;
140
+ }
141
+ return {line: line, column: offset - cur};
142
+ };
143
+
144
+ var getCurrentToken = function () {
145
+ var token = {
146
+ type: tokType,
147
+ value: tokVal,
148
+ start: tokStart,
149
+ end: tokEnd
150
+ };
151
+ if (options.locations) {
152
+ token.startLoc = tokStartLoc;
153
+ token.endLoc = tokEndLoc;
154
+ }
155
+ return token;
156
+ };
157
+
158
+ // Acorn is organized as a tokenizer and a recursive-descent parser.
159
+ // The `tokenize` export provides an interface to the tokenizer.
160
+ // Because the tokenizer is optimized for being efficiently used by
161
+ // the Acorn parser itself, this interface is somewhat crude and not
162
+ // very modular. Performing another parse or call to `tokenize` will
163
+ // reset the internal state, and invalidate existing tokenizers.
164
+
165
+ exports.tokenize = function(inpt, opts) {
166
+ input = String(inpt); inputLen = input.length;
167
+ setOptions(opts);
168
+ initTokenState();
169
+
170
+ function getToken(forceRegexp) {
171
+ lastEnd = tokEnd;
172
+ readToken(forceRegexp);
173
+ return getCurrentToken();
174
+ }
175
+ getToken.jumpTo = function(pos, reAllowed) {
176
+ tokPos = pos;
177
+ if (options.locations) {
178
+ tokCurLine = 1;
179
+ tokLineStart = lineBreak.lastIndex = 0;
180
+ var match;
181
+ while ((match = lineBreak.exec(input)) && match.index < pos) {
182
+ ++tokCurLine;
183
+ tokLineStart = match.index + match[0].length;
184
+ }
185
+ }
186
+ tokRegexpAllowed = reAllowed;
187
+ skipSpace();
188
+ };
189
+ return getToken;
190
+ };
191
+
192
+ // State is kept in (closure-)global variables. We already saw the
193
+ // `options`, `input`, and `inputLen` variables above.
194
+
195
+ // The current position of the tokenizer in the input.
196
+
197
+ var tokPos;
198
+
199
+ // The start and end offsets of the current token.
200
+
201
+ var tokStart, tokEnd;
202
+
203
+ // When `options.locations` is true, these hold objects
204
+ // containing the tokens start and end line/column pairs.
205
+
206
+ var tokStartLoc, tokEndLoc;
207
+
208
+ // The type and value of the current token. Token types are objects,
209
+ // named by variables against which they can be compared, and
210
+ // holding properties that describe them (indicating, for example,
211
+ // the precedence of an infix operator, and the original name of a
212
+ // keyword token). The kind of value that's held in `tokVal` depends
213
+ // on the type of the token. For literals, it is the literal value,
214
+ // for operators, the operator name, and so on.
215
+
216
+ var tokType, tokVal;
217
+
218
+ // Internal state for the tokenizer. To distinguish between division
219
+ // operators and regular expressions, it remembers whether the last
220
+ // token was one that is allowed to be followed by an expression.
221
+ // (If it is, a slash is probably a regexp, if it isn't it's a
222
+ // division operator. See the `parseStatement` function for a
223
+ // caveat.)
224
+
225
+ var tokRegexpAllowed;
226
+
227
+ // When `options.locations` is true, these are used to keep
228
+ // track of the current line, and know when a new line has been
229
+ // entered.
230
+
231
+ var tokCurLine, tokLineStart;
232
+
233
+ // These store the position of the previous token, which is useful
234
+ // when finishing a node and assigning its `end` position.
235
+
236
+ var lastStart, lastEnd, lastEndLoc;
237
+
238
+ // This is the parser's state. `inFunction` is used to reject
239
+ // `return` statements outside of functions, `inGenerator` to
240
+ // reject `yield`s outside of generators, `labels` to verify
241
+ // that `break` and `continue` have somewhere to jump to, and
242
+ // `strict` indicates whether strict mode is on.
243
+
244
+ var inFunction, inGenerator, labels, strict;
245
+
246
+ // This counter is used for checking that arrow expressions did
247
+ // not contain nested parentheses in argument list.
248
+
249
+ var metParenL;
250
+
251
+ // This is used by parser for detecting if it's inside ES6
252
+ // Template String. If it is, it should treat '$' as prefix before
253
+ // '{expression}' and everything else as string literals.
254
+
255
+ var inTemplate;
256
+
257
+ // This function is used to raise exceptions on parse errors. It
258
+ // takes an offset integer (into the current `input`) to indicate
259
+ // the location of the error, attaches the position to the end
260
+ // of the error message, and then raises a `SyntaxError` with that
261
+ // message.
262
+
263
+ function raise(pos, message) {
264
+ var loc = getLineInfo(input, pos);
265
+ message += " (" + loc.line + ":" + loc.column + ")";
266
+ var err = new SyntaxError(message);
267
+ err.pos = pos; err.loc = loc; err.raisedAt = tokPos;
268
+ throw err;
269
+ }
270
+
271
+ // Reused empty array added for node fields that are always empty.
272
+
273
+ var empty = [];
274
+
275
+ // ## Token types
276
+
277
+ // The assignment of fine-grained, information-carrying type objects
278
+ // allows the tokenizer to store the information it has about a
279
+ // token in a way that is very cheap for the parser to look up.
280
+
281
+ // All token type variables start with an underscore, to make them
282
+ // easy to recognize.
283
+
284
+ // These are the general types. The `type` property is only used to
285
+ // make them recognizeable when debugging.
286
+
287
+ var _num = {type: "num"}, _regexp = {type: "regexp"}, _string = {type: "string"};
288
+ var _name = {type: "name"}, _eof = {type: "eof"};
289
+
290
+ // Keyword tokens. The `keyword` property (also used in keyword-like
291
+ // operators) indicates that the token originated from an
292
+ // identifier-like word, which is used when parsing property names.
293
+ //
294
+ // The `beforeExpr` property is used to disambiguate between regular
295
+ // expressions and divisions. It is set on all token types that can
296
+ // be followed by an expression (thus, a slash after them would be a
297
+ // regular expression).
298
+ //
299
+ // `isLoop` marks a keyword as starting a loop, which is important
300
+ // to know when parsing a label, in order to allow or disallow
301
+ // continue jumps to that label.
302
+
303
+ var _break = {keyword: "break"}, _case = {keyword: "case", beforeExpr: true}, _catch = {keyword: "catch"};
304
+ var _continue = {keyword: "continue"}, _debugger = {keyword: "debugger"}, _default = {keyword: "default"};
305
+ var _do = {keyword: "do", isLoop: true}, _else = {keyword: "else", beforeExpr: true};
306
+ var _finally = {keyword: "finally"}, _for = {keyword: "for", isLoop: true}, _function = {keyword: "function"};
307
+ var _if = {keyword: "if"}, _return = {keyword: "return", beforeExpr: true}, _switch = {keyword: "switch"};
308
+ var _throw = {keyword: "throw", beforeExpr: true}, _try = {keyword: "try"}, _var = {keyword: "var"};
309
+ var _let = {keyword: "let"}, _const = {keyword: "const"};
310
+ var _while = {keyword: "while", isLoop: true}, _with = {keyword: "with"}, _new = {keyword: "new", beforeExpr: true};
311
+ var _this = {keyword: "this"};
312
+ var _class = {keyword: "class"}, _extends = {keyword: "extends", beforeExpr: true};
313
+ var _export = {keyword: "export"}, _import = {keyword: "import"};
314
+ var _yield = {keyword: "yield", beforeExpr: true};
315
+
316
+ // The keywords that denote values.
317
+
318
+ var _null = {keyword: "null", atomValue: null}, _true = {keyword: "true", atomValue: true};
319
+ var _false = {keyword: "false", atomValue: false};
320
+
321
+ // Some keywords are treated as regular operators. `in` sometimes
322
+ // (when parsing `for`) needs to be tested against specifically, so
323
+ // we assign a variable name to it for quick comparing.
324
+
325
+ var _in = {keyword: "in", binop: 7, beforeExpr: true};
326
+
327
+ // Map keyword names to token types.
328
+
329
+ var keywordTypes = {"break": _break, "case": _case, "catch": _catch,
330
+ "continue": _continue, "debugger": _debugger, "default": _default,
331
+ "do": _do, "else": _else, "finally": _finally, "for": _for,
332
+ "function": _function, "if": _if, "return": _return, "switch": _switch,
333
+ "throw": _throw, "try": _try, "var": _var, "let": _let, "const": _const,
334
+ "while": _while, "with": _with,
335
+ "null": _null, "true": _true, "false": _false, "new": _new, "in": _in,
336
+ "instanceof": {keyword: "instanceof", binop: 7, beforeExpr: true}, "this": _this,
337
+ "typeof": {keyword: "typeof", prefix: true, beforeExpr: true},
338
+ "void": {keyword: "void", prefix: true, beforeExpr: true},
339
+ "delete": {keyword: "delete", prefix: true, beforeExpr: true},
340
+ "class": _class, "extends": _extends,
341
+ "export": _export, "import": _import, "yield": _yield};
342
+
343
+ // Punctuation token types. Again, the `type` property is purely for debugging.
344
+
345
+ var _bracketL = {type: "[", beforeExpr: true}, _bracketR = {type: "]"}, _braceL = {type: "{", beforeExpr: true};
346
+ var _braceR = {type: "}"}, _parenL = {type: "(", beforeExpr: true}, _parenR = {type: ")"};
347
+ var _comma = {type: ",", beforeExpr: true}, _semi = {type: ";", beforeExpr: true};
348
+ var _colon = {type: ":", beforeExpr: true}, _dot = {type: "."}, _ellipsis = {type: "..."}, _question = {type: "?", beforeExpr: true};
349
+ var _arrow = {type: "=>", beforeExpr: true}, _bquote = {type: "`"}, _dollarBraceL = {type: "${", beforeExpr: true};
350
+
351
+ // Operators. These carry several kinds of properties to help the
352
+ // parser use them properly (the presence of these properties is
353
+ // what categorizes them as operators).
354
+ //
355
+ // `binop`, when present, specifies that this operator is a binary
356
+ // operator, and will refer to its precedence.
357
+ //
358
+ // `prefix` and `postfix` mark the operator as a prefix or postfix
359
+ // unary operator. `isUpdate` specifies that the node produced by
360
+ // the operator should be of type UpdateExpression rather than
361
+ // simply UnaryExpression (`++` and `--`).
362
+ //
363
+ // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
364
+ // binary operators with a very low precedence, that should result
365
+ // in AssignmentExpression nodes.
366
+
367
+ var _slash = {binop: 10, beforeExpr: true}, _eq = {isAssign: true, beforeExpr: true};
368
+ var _assign = {isAssign: true, beforeExpr: true};
369
+ var _incDec = {postfix: true, prefix: true, isUpdate: true}, _prefix = {prefix: true, beforeExpr: true};
370
+ var _logicalOR = {binop: 1, beforeExpr: true};
371
+ var _logicalAND = {binop: 2, beforeExpr: true};
372
+ var _bitwiseOR = {binop: 3, beforeExpr: true};
373
+ var _bitwiseXOR = {binop: 4, beforeExpr: true};
374
+ var _bitwiseAND = {binop: 5, beforeExpr: true};
375
+ var _equality = {binop: 6, beforeExpr: true};
376
+ var _relational = {binop: 7, beforeExpr: true};
377
+ var _bitShift = {binop: 8, beforeExpr: true};
378
+ var _plusMin = {binop: 9, prefix: true, beforeExpr: true};
379
+ var _modulo = {binop: 10, beforeExpr: true};
380
+
381
+ // '*' may be multiply or have special meaning in ES6
382
+ var _star = {binop: 10, beforeExpr: true};
383
+
384
+ // Provide access to the token types for external users of the
385
+ // tokenizer.
386
+
387
+ exports.tokTypes = {bracketL: _bracketL, bracketR: _bracketR, braceL: _braceL, braceR: _braceR,
388
+ parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon,
389
+ dot: _dot, ellipsis: _ellipsis, question: _question, slash: _slash, eq: _eq,
390
+ name: _name, eof: _eof, num: _num, regexp: _regexp, string: _string,
391
+ arrow: _arrow, bquote: _bquote, dollarBraceL: _dollarBraceL};
392
+ for (var kw in keywordTypes) exports.tokTypes["_" + kw] = keywordTypes[kw];
393
+
394
+ // This is a trick taken from Esprima. It turns out that, on
395
+ // non-Chrome browsers, to check whether a string is in a set, a
396
+ // predicate containing a big ugly `switch` statement is faster than
397
+ // a regular expression, and on Chrome the two are about on par.
398
+ // This function uses `eval` (non-lexical) to produce such a
399
+ // predicate from a space-separated string of words.
400
+ //
401
+ // It starts by sorting the words by length.
402
+
403
+ function makePredicate(words) {
404
+ words = words.split(" ");
405
+ var f = "", cats = [];
406
+ out: for (var i = 0; i < words.length; ++i) {
407
+ for (var j = 0; j < cats.length; ++j)
408
+ if (cats[j][0].length == words[i].length) {
409
+ cats[j].push(words[i]);
410
+ continue out;
411
+ }
412
+ cats.push([words[i]]);
413
+ }
414
+ function compareTo(arr) {
415
+ if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
416
+ f += "switch(str){";
417
+ for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
418
+ f += "return true}return false;";
419
+ }
420
+
421
+ // When there are more than three length categories, an outer
422
+ // switch first dispatches on the lengths, to save on comparisons.
423
+
424
+ if (cats.length > 3) {
425
+ cats.sort(function(a, b) {return b.length - a.length;});
426
+ f += "switch(str.length){";
427
+ for (var i = 0; i < cats.length; ++i) {
428
+ var cat = cats[i];
429
+ f += "case " + cat[0].length + ":";
430
+ compareTo(cat);
431
+ }
432
+ f += "}";
433
+
434
+ // Otherwise, simply generate a flat `switch` statement.
435
+
436
+ } else {
437
+ compareTo(words);
438
+ }
439
+ return new Function("str", f);
440
+ }
441
+
442
+ // The ECMAScript 3 reserved word list.
443
+
444
+ 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");
445
+
446
+ // ECMAScript 5 reserved words.
447
+
448
+ var isReservedWord5 = makePredicate("class enum extends super const export import");
449
+
450
+ // The additional reserved words in strict mode.
451
+
452
+ var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield");
453
+
454
+ // The forbidden variable names in strict mode.
455
+
456
+ var isStrictBadIdWord = makePredicate("eval arguments");
457
+
458
+ // And the keywords.
459
+
460
+ var ecma5AndLessKeywords = "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";
461
+
462
+ var isEcma5AndLessKeyword = makePredicate(ecma5AndLessKeywords);
463
+
464
+ var isEcma6Keyword = makePredicate(ecma5AndLessKeywords + " let const class extends export import yield");
465
+
466
+ var isKeyword = isEcma5AndLessKeyword;
467
+
468
+ // ## Character categories
469
+
470
+ // Big ugly regular expressions that match characters in the
471
+ // whitespace, identifier, and identifier-start categories. These
472
+ // are only applied when a character is found to actually have a
473
+ // code point above 128.
474
+ // Generated by `tools/generate-identifier-regex.js`.
475
+
476
+ var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
477
+ var nonASCIIidentifierStartChars = "\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\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-\u08B2\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\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-\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-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\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-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\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";
478
+ var nonASCIIidentifierChars = "\u0300-\u036F\u0483-\u0487\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u0669\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u06F0-\u06F9\u0711\u0730-\u074A\u07A6-\u07B0\u07C0-\u07C9\u07EB-\u07F3\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08E4-\u0903\u093A-\u093C\u093E-\u094F\u0951-\u0957\u0962\u0963\u0966-\u096F\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u09E6-\u09EF\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\u0B62\u0B63\u0B66-\u0B6F\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0CE6-\u0CEF\u0D01-\u0D03\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D66-\u0D6F\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0E50-\u0E59\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0ED0-\u0ED9\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1040-\u1049\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F-\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u18A9\u1920-\u192B\u1930-\u193B\u1946-\u194F\u19B0-\u19C0\u19C8\u19C9\u19D0-\u19D9\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AB0-\u1ABD\u1B00-\u1B04\u1B34-\u1B44\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAD\u1BB0-\u1BB9\u1BE6-\u1BF3\u1C24-\u1C37\u1C40-\u1C49\u1C50-\u1C59\u1CD0-\u1CD2\u1CD4-\u1CE8\u1CED\u1CF2-\u1CF4\u1CF8\u1CF9\u1DC0-\u1DF5\u1DFC-\u1DFF\u200C\u200D\u203F\u2040\u2054\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA620-\uA629\uA66F\uA674-\uA67D\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\uA880\uA881\uA8B4-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F1\uA900-\uA909\uA926-\uA92D\uA947-\uA953\uA980-\uA983\uA9B3-\uA9C0\uA9D0-\uA9D9\uA9E5\uA9F0-\uA9F9\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uAA50-\uAA59\uAA7B-\uAA7D\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEB-\uAAEF\uAAF5\uAAF6\uABE3-\uABEA\uABEC\uABED\uABF0-\uABF9\uFB1E\uFE00-\uFE0F\uFE20-\uFE2D\uFE33\uFE34\uFE4D-\uFE4F\uFF10-\uFF19\uFF3F";
479
+ var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
480
+ var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
481
+
482
+ // Whether a single character denotes a newline.
483
+
484
+ var newline = /[\n\r\u2028\u2029]/;
485
+
486
+ // Matches a whole line break (where CRLF is considered a single
487
+ // line break). Used to count lines.
488
+
489
+ var lineBreak = /\r\n|[\n\r\u2028\u2029]/g;
490
+
491
+ // Test whether a given character code starts an identifier.
492
+
493
+ var isIdentifierStart = exports.isIdentifierStart = function(code) {
494
+ if (code < 65) return code === 36;
495
+ if (code < 91) return true;
496
+ if (code < 97) return code === 95;
497
+ if (code < 123)return true;
498
+ return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
499
+ };
500
+
501
+ // Test whether a given character is part of an identifier.
502
+
503
+ var isIdentifierChar = exports.isIdentifierChar = function(code) {
504
+ if (code < 48) return code === 36;
505
+ if (code < 58) return true;
506
+ if (code < 65) return false;
507
+ if (code < 91) return true;
508
+ if (code < 97) return code === 95;
509
+ if (code < 123)return true;
510
+ return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
511
+ };
512
+
513
+ // ## Tokenizer
514
+
515
+ // These are used when `options.locations` is on, for the
516
+ // `tokStartLoc` and `tokEndLoc` properties.
517
+
518
+ function Position() {
519
+ this.line = tokCurLine;
520
+ this.column = tokPos - tokLineStart;
521
+ }
522
+
523
+ // Reset the token state. Used at the start of a parse.
524
+
525
+ function initTokenState() {
526
+ tokCurLine = 1;
527
+ tokPos = tokLineStart = 0;
528
+ tokRegexpAllowed = true;
529
+ metParenL = 0;
530
+ inTemplate = false;
531
+ skipSpace();
532
+ }
533
+
534
+ // Called at the end of every token. Sets `tokEnd`, `tokVal`, and
535
+ // `tokRegexpAllowed`, and skips the space after the token, so that
536
+ // the next one's `tokStart` will point at the right position.
537
+
538
+ function finishToken(type, val, shouldSkipSpace) {
539
+ tokEnd = tokPos;
540
+ if (options.locations) tokEndLoc = new Position;
541
+ tokType = type;
542
+ if (shouldSkipSpace !== false) skipSpace();
543
+ tokVal = val;
544
+ tokRegexpAllowed = type.beforeExpr;
545
+ if (options.onToken) {
546
+ options.onToken(getCurrentToken());
547
+ }
548
+ }
549
+
550
+ function skipBlockComment() {
551
+ var startLoc = options.onComment && options.locations && new Position;
552
+ var start = tokPos, end = input.indexOf("*/", tokPos += 2);
553
+ if (end === -1) raise(tokPos - 2, "Unterminated comment");
554
+ tokPos = end + 2;
555
+ if (options.locations) {
556
+ lineBreak.lastIndex = start;
557
+ var match;
558
+ while ((match = lineBreak.exec(input)) && match.index < tokPos) {
559
+ ++tokCurLine;
560
+ tokLineStart = match.index + match[0].length;
561
+ }
562
+ }
563
+ if (options.onComment)
564
+ options.onComment(true, input.slice(start + 2, end), start, tokPos,
565
+ startLoc, options.locations && new Position);
566
+ }
567
+
568
+ function skipLineComment() {
569
+ var start = tokPos;
570
+ var startLoc = options.onComment && options.locations && new Position;
571
+ var ch = input.charCodeAt(tokPos+=2);
572
+ while (tokPos < inputLen && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) {
573
+ ++tokPos;
574
+ ch = input.charCodeAt(tokPos);
575
+ }
576
+ if (options.onComment)
577
+ options.onComment(false, input.slice(start + 2, tokPos), start, tokPos,
578
+ startLoc, options.locations && new Position);
579
+ }
580
+
581
+ // Called at the start of the parse and after every token. Skips
582
+ // whitespace and comments, and.
583
+
584
+ function skipSpace() {
585
+ while (tokPos < inputLen) {
586
+ var ch = input.charCodeAt(tokPos);
587
+ if (ch === 32) { // ' '
588
+ ++tokPos;
589
+ } else if (ch === 13) {
590
+ ++tokPos;
591
+ var next = input.charCodeAt(tokPos);
592
+ if (next === 10) {
593
+ ++tokPos;
594
+ }
595
+ if (options.locations) {
596
+ ++tokCurLine;
597
+ tokLineStart = tokPos;
598
+ }
599
+ } else if (ch === 10 || ch === 8232 || ch === 8233) {
600
+ ++tokPos;
601
+ if (options.locations) {
602
+ ++tokCurLine;
603
+ tokLineStart = tokPos;
604
+ }
605
+ } else if (ch > 8 && ch < 14) {
606
+ ++tokPos;
607
+ } else if (ch === 47) { // '/'
608
+ var next = input.charCodeAt(tokPos + 1);
609
+ if (next === 42) { // '*'
610
+ skipBlockComment();
611
+ } else if (next === 47) { // '/'
612
+ skipLineComment();
613
+ } else break;
614
+ } else if (ch === 160) { // '\xa0'
615
+ ++tokPos;
616
+ } else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
617
+ ++tokPos;
618
+ } else {
619
+ break;
620
+ }
621
+ }
622
+ }
623
+
624
+ // ### Token reading
625
+
626
+ // This is the function that is called to fetch the next token. It
627
+ // is somewhat obscure, because it works in character codes rather
628
+ // than characters, and because operator parsing has been inlined
629
+ // into it.
630
+ //
631
+ // All in the name of speed.
632
+ //
633
+ // The `forceRegexp` parameter is used in the one case where the
634
+ // `tokRegexpAllowed` trick does not work. See `parseStatement`.
635
+
636
+ function readToken_dot() {
637
+ var next = input.charCodeAt(tokPos + 1);
638
+ if (next >= 48 && next <= 57) return readNumber(true);
639
+ var next2 = input.charCodeAt(tokPos + 2);
640
+ if (options.ecmaVersion >= 6 && next === 46 && next2 === 46) { // 46 = dot '.'
641
+ tokPos += 3;
642
+ return finishToken(_ellipsis);
643
+ } else {
644
+ ++tokPos;
645
+ return finishToken(_dot);
646
+ }
647
+ }
648
+
649
+ function readToken_slash() { // '/'
650
+ var next = input.charCodeAt(tokPos + 1);
651
+ if (tokRegexpAllowed) {++tokPos; return readRegexp();}
652
+ if (next === 61) return finishOp(_assign, 2);
653
+ return finishOp(_slash, 1);
654
+ }
655
+
656
+ function readToken_mult_modulo(code) { // '%*'
657
+ var next = input.charCodeAt(tokPos + 1);
658
+ if (next === 61) return finishOp(_assign, 2);
659
+ return finishOp(code === 42 ? _star : _modulo, 1);
660
+ }
661
+
662
+ function readToken_pipe_amp(code) { // '|&'
663
+ var next = input.charCodeAt(tokPos + 1);
664
+ if (next === code) return finishOp(code === 124 ? _logicalOR : _logicalAND, 2);
665
+ if (next === 61) return finishOp(_assign, 2);
666
+ return finishOp(code === 124 ? _bitwiseOR : _bitwiseAND, 1);
667
+ }
668
+
669
+ function readToken_caret() { // '^'
670
+ var next = input.charCodeAt(tokPos + 1);
671
+ if (next === 61) return finishOp(_assign, 2);
672
+ return finishOp(_bitwiseXOR, 1);
673
+ }
674
+
675
+ function readToken_plus_min(code) { // '+-'
676
+ var next = input.charCodeAt(tokPos + 1);
677
+ if (next === code) {
678
+ if (next == 45 && input.charCodeAt(tokPos + 2) == 62 &&
679
+ newline.test(input.slice(lastEnd, tokPos))) {
680
+ // A `-->` line comment
681
+ tokPos += 3;
682
+ skipLineComment();
683
+ skipSpace();
684
+ return readToken();
685
+ }
686
+ return finishOp(_incDec, 2);
687
+ }
688
+ if (next === 61) return finishOp(_assign, 2);
689
+ return finishOp(_plusMin, 1);
690
+ }
691
+
692
+ function readToken_lt_gt(code) { // '<>'
693
+ var next = input.charCodeAt(tokPos + 1);
694
+ var size = 1;
695
+ if (next === code) {
696
+ size = code === 62 && input.charCodeAt(tokPos + 2) === 62 ? 3 : 2;
697
+ if (input.charCodeAt(tokPos + size) === 61) return finishOp(_assign, size + 1);
698
+ return finishOp(_bitShift, size);
699
+ }
700
+ if (next == 33 && code == 60 && input.charCodeAt(tokPos + 2) == 45 &&
701
+ input.charCodeAt(tokPos + 3) == 45) {
702
+ // `<!--`, an XML-style comment that should be interpreted as a line comment
703
+ tokPos += 4;
704
+ skipLineComment();
705
+ skipSpace();
706
+ return readToken();
707
+ }
708
+ if (next === 61)
709
+ size = input.charCodeAt(tokPos + 2) === 61 ? 3 : 2;
710
+ return finishOp(_relational, size);
711
+ }
712
+
713
+ function readToken_eq_excl(code) { // '=!', '=>'
714
+ var next = input.charCodeAt(tokPos + 1);
715
+ if (next === 61) return finishOp(_equality, input.charCodeAt(tokPos + 2) === 61 ? 3 : 2);
716
+ if (code === 61 && next === 62 && options.ecmaVersion >= 6) { // '=>'
717
+ tokPos += 2;
718
+ return finishToken(_arrow);
719
+ }
720
+ return finishOp(code === 61 ? _eq : _prefix, 1);
721
+ }
722
+
723
+ // Get token inside ES6 template (special rules work there).
724
+
725
+ function getTemplateToken(code) {
726
+ // '`' and '${' have special meanings, but they should follow
727
+ // string (can be empty)
728
+ if (tokType === _string) {
729
+ if (code === 96) { // '`'
730
+ ++tokPos;
731
+ return finishToken(_bquote);
732
+ } else
733
+ if (code === 36 && input.charCodeAt(tokPos + 1) === 123) { // '${'
734
+ tokPos += 2;
735
+ return finishToken(_dollarBraceL);
736
+ }
737
+ }
738
+
739
+ if (code === 125) { // '}'
740
+ ++tokPos;
741
+ return finishToken(_braceR, undefined, false);
742
+ }
743
+
744
+ // anything else is considered string literal
745
+ return readTmplString();
746
+ }
747
+
748
+ function getTokenFromCode(code) {
749
+ switch (code) {
750
+ // The interpretation of a dot depends on whether it is followed
751
+ // by a digit or another two dots.
752
+ case 46: // '.'
753
+ return readToken_dot();
754
+
755
+ // Punctuation tokens.
756
+ case 40: ++tokPos; return finishToken(_parenL);
757
+ case 41: ++tokPos; return finishToken(_parenR);
758
+ case 59: ++tokPos; return finishToken(_semi);
759
+ case 44: ++tokPos; return finishToken(_comma);
760
+ case 91: ++tokPos; return finishToken(_bracketL);
761
+ case 93: ++tokPos; return finishToken(_bracketR);
762
+ case 123: ++tokPos; return finishToken(_braceL);
763
+ case 125: ++tokPos; return finishToken(_braceR);
764
+ case 58: ++tokPos; return finishToken(_colon);
765
+ case 63: ++tokPos; return finishToken(_question);
766
+
767
+ case 96: // '`'
768
+ if (options.ecmaVersion >= 6) {
769
+ ++tokPos;
770
+ return finishToken(_bquote, undefined, false);
771
+ }
772
+
773
+ case 48: // '0'
774
+ var next = input.charCodeAt(tokPos + 1);
775
+ if (next === 120 || next === 88) return readRadixNumber(16); // '0x', '0X' - hex number
776
+ if (options.ecmaVersion >= 6) {
777
+ if (next === 111 || next === 79) return readRadixNumber(8); // '0o', '0O' - octal number
778
+ if (next === 98 || next === 66) return readRadixNumber(2); // '0b', '0B' - binary number
779
+ }
780
+ // Anything else beginning with a digit is an integer, octal
781
+ // number, or float.
782
+ case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
783
+ return readNumber(false);
784
+
785
+ // Quotes produce strings.
786
+ case 34: case 39: // '"', "'"
787
+ return readString(code);
788
+
789
+ // Operators are parsed inline in tiny state machines. '=' (61) is
790
+ // often referred to. `finishOp` simply skips the amount of
791
+ // characters it is given as second argument, and returns a token
792
+ // of the type given by its first argument.
793
+
794
+ case 47: // '/'
795
+ return readToken_slash();
796
+
797
+ case 37: case 42: // '%*'
798
+ return readToken_mult_modulo(code);
799
+
800
+ case 124: case 38: // '|&'
801
+ return readToken_pipe_amp(code);
802
+
803
+ case 94: // '^'
804
+ return readToken_caret();
805
+
806
+ case 43: case 45: // '+-'
807
+ return readToken_plus_min(code);
808
+
809
+ case 60: case 62: // '<>'
810
+ return readToken_lt_gt(code);
811
+
812
+ case 61: case 33: // '=!'
813
+ return readToken_eq_excl(code);
814
+
815
+ case 126: // '~'
816
+ return finishOp(_prefix, 1);
817
+ }
818
+
819
+ return false;
820
+ }
821
+
822
+ function readToken(forceRegexp) {
823
+ if (!forceRegexp) tokStart = tokPos;
824
+ else tokPos = tokStart + 1;
825
+ if (options.locations) tokStartLoc = new Position;
826
+ if (forceRegexp) return readRegexp();
827
+ if (tokPos >= inputLen) return finishToken(_eof);
828
+
829
+ var code = input.charCodeAt(tokPos);
830
+
831
+ if (inTemplate) return getTemplateToken(code);
832
+
833
+ // Identifier or keyword. '\uXXXX' sequences are allowed in
834
+ // identifiers, so '\' also dispatches to that.
835
+ if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord();
836
+
837
+ var tok = getTokenFromCode(code);
838
+
839
+ if (tok === false) {
840
+ // If we are here, we either found a non-ASCII identifier
841
+ // character, or something that's entirely disallowed.
842
+ var ch = String.fromCharCode(code);
843
+ if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord();
844
+ raise(tokPos, "Unexpected character '" + ch + "'");
845
+ }
846
+ return tok;
847
+ }
848
+
849
+ function finishOp(type, size) {
850
+ var str = input.slice(tokPos, tokPos + size);
851
+ tokPos += size;
852
+ finishToken(type, str);
853
+ }
854
+
855
+ // Parse a regular expression. Some context-awareness is necessary,
856
+ // since a '/' inside a '[]' set does not end the expression.
857
+
858
+ function readRegexp() {
859
+ var content = "", escaped, inClass, start = tokPos;
860
+ for (;;) {
861
+ if (tokPos >= inputLen) raise(start, "Unterminated regular expression");
862
+ var ch = input.charAt(tokPos);
863
+ if (newline.test(ch)) raise(start, "Unterminated regular expression");
864
+ if (!escaped) {
865
+ if (ch === "[") inClass = true;
866
+ else if (ch === "]" && inClass) inClass = false;
867
+ else if (ch === "/" && !inClass) break;
868
+ escaped = ch === "\\";
869
+ } else escaped = false;
870
+ ++tokPos;
871
+ }
872
+ var content = input.slice(start, tokPos);
873
+ ++tokPos;
874
+ // Need to use `readWord1` because '\uXXXX' sequences are allowed
875
+ // here (don't ask).
876
+ var mods = readWord1();
877
+ if (mods && !/^[gmsiy]*$/.test(mods)) raise(start, "Invalid regular expression flag");
878
+ try {
879
+ var value = new RegExp(content, mods);
880
+ } catch (e) {
881
+ if (e instanceof SyntaxError) raise(start, "Error parsing regular expression: " + e.message);
882
+ raise(e);
883
+ }
884
+ return finishToken(_regexp, value);
885
+ }
886
+
887
+ // Read an integer in the given radix. Return null if zero digits
888
+ // were read, the integer value otherwise. When `len` is given, this
889
+ // will return `null` unless the integer has exactly `len` digits.
890
+
891
+ function readInt(radix, len) {
892
+ var start = tokPos, total = 0;
893
+ for (var i = 0, e = len == null ? Infinity : len; i < e; ++i) {
894
+ var code = input.charCodeAt(tokPos), val;
895
+ if (code >= 97) val = code - 97 + 10; // a
896
+ else if (code >= 65) val = code - 65 + 10; // A
897
+ else if (code >= 48 && code <= 57) val = code - 48; // 0-9
898
+ else val = Infinity;
899
+ if (val >= radix) break;
900
+ ++tokPos;
901
+ total = total * radix + val;
902
+ }
903
+ if (tokPos === start || len != null && tokPos - start !== len) return null;
904
+
905
+ return total;
906
+ }
907
+
908
+ function readRadixNumber(radix) {
909
+ tokPos += 2; // 0x
910
+ var val = readInt(radix);
911
+ if (val == null) raise(tokStart + 2, "Expected number in radix " + radix);
912
+ if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
913
+ return finishToken(_num, val);
914
+ }
915
+
916
+ // Read an integer, octal integer, or floating-point number.
917
+
918
+ function readNumber(startsWithDot) {
919
+ var start = tokPos, isFloat = false, octal = input.charCodeAt(tokPos) === 48;
920
+ if (!startsWithDot && readInt(10) === null) raise(start, "Invalid number");
921
+ if (input.charCodeAt(tokPos) === 46) {
922
+ ++tokPos;
923
+ readInt(10);
924
+ isFloat = true;
925
+ }
926
+ var next = input.charCodeAt(tokPos);
927
+ if (next === 69 || next === 101) { // 'eE'
928
+ next = input.charCodeAt(++tokPos);
929
+ if (next === 43 || next === 45) ++tokPos; // '+-'
930
+ if (readInt(10) === null) raise(start, "Invalid number");
931
+ isFloat = true;
932
+ }
933
+ if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
934
+
935
+ var str = input.slice(start, tokPos), val;
936
+ if (isFloat) val = parseFloat(str);
937
+ else if (!octal || str.length === 1) val = parseInt(str, 10);
938
+ else if (/[89]/.test(str) || strict) raise(start, "Invalid number");
939
+ else val = parseInt(str, 8);
940
+ return finishToken(_num, val);
941
+ }
942
+
943
+ // Read a string value, interpreting backslash-escapes.
944
+
945
+ function readCodePoint() {
946
+ var ch = input.charCodeAt(tokPos), code;
947
+
948
+ if (ch === 123) {
949
+ if (options.ecmaVersion < 6) unexpected();
950
+ ++tokPos;
951
+ code = readHexChar(input.indexOf('}', tokPos) - tokPos);
952
+ ++tokPos;
953
+ if (code > 0x10FFFF) unexpected();
954
+ } else {
955
+ code = readHexChar(4);
956
+ }
957
+
958
+ // UTF-16 Encoding
959
+ if (code <= 0xFFFF) {
960
+ return String.fromCharCode(code);
961
+ }
962
+ var cu1 = ((code - 0x10000) >> 10) + 0xD800;
963
+ var cu2 = ((code - 0x10000) & 1023) + 0xDC00;
964
+ return String.fromCharCode(cu1, cu2);
965
+ }
966
+
967
+ function readString(quote) {
968
+ ++tokPos;
969
+ var out = "";
970
+ for (;;) {
971
+ if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
972
+ var ch = input.charCodeAt(tokPos);
973
+ if (ch === quote) {
974
+ ++tokPos;
975
+ return finishToken(_string, out);
976
+ }
977
+ if (ch === 92) { // '\'
978
+ out += readEscapedChar();
979
+ } else {
980
+ ++tokPos;
981
+ if (newline.test(String.fromCharCode(ch))) {
982
+ raise(tokStart, "Unterminated string constant");
983
+ }
984
+ out += String.fromCharCode(ch); // '\'
985
+ }
986
+ }
987
+ }
988
+
989
+ function readTmplString() {
990
+ var out = "";
991
+ for (;;) {
992
+ if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
993
+ var ch = input.charCodeAt(tokPos);
994
+ if (ch === 96 || ch === 36 && input.charCodeAt(tokPos + 1) === 123) // '`', '${'
995
+ return finishToken(_string, out);
996
+ if (ch === 92) { // '\'
997
+ out += readEscapedChar();
998
+ } else {
999
+ ++tokPos;
1000
+ if (newline.test(String.fromCharCode(ch))) {
1001
+ if (ch === 13 && input.charCodeAt(tokPos) === 10) {
1002
+ ++tokPos;
1003
+ ch = 10;
1004
+ }
1005
+ if (options.locations) {
1006
+ ++tokCurLine;
1007
+ tokLineStart = tokPos;
1008
+ }
1009
+ }
1010
+ out += String.fromCharCode(ch); // '\'
1011
+ }
1012
+ }
1013
+ }
1014
+
1015
+ // Used to read escaped characters
1016
+
1017
+ function readEscapedChar() {
1018
+ var ch = input.charCodeAt(++tokPos);
1019
+ var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3));
1020
+ if (octal) octal = octal[0];
1021
+ while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1);
1022
+ if (octal === "0") octal = null;
1023
+ ++tokPos;
1024
+ if (octal) {
1025
+ if (strict) raise(tokPos - 2, "Octal literal in strict mode");
1026
+ tokPos += octal.length - 1;
1027
+ return String.fromCharCode(parseInt(octal, 8));
1028
+ } else {
1029
+ switch (ch) {
1030
+ case 110: return "\n"; // 'n' -> '\n'
1031
+ case 114: return "\r"; // 'r' -> '\r'
1032
+ case 120: return String.fromCharCode(readHexChar(2)); // 'x'
1033
+ case 117: return readCodePoint(); // 'u'
1034
+ case 85: return String.fromCharCode(readHexChar(8)); // 'U'
1035
+ case 116: return "\t"; // 't' -> '\t'
1036
+ case 98: return "\b"; // 'b' -> '\b'
1037
+ case 118: return "\u000b"; // 'v' -> '\u000b'
1038
+ case 102: return "\f"; // 'f' -> '\f'
1039
+ case 48: return "\0"; // 0 -> '\0'
1040
+ case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
1041
+ case 10: // ' \n'
1042
+ if (options.locations) { tokLineStart = tokPos; ++tokCurLine; }
1043
+ return "";
1044
+ default: return String.fromCharCode(ch);
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ // Used to read character escape sequences ('\x', '\u', '\U').
1050
+
1051
+ function readHexChar(len) {
1052
+ var n = readInt(16, len);
1053
+ if (n === null) raise(tokStart, "Bad character escape sequence");
1054
+ return n;
1055
+ }
1056
+
1057
+ // Used to signal to callers of `readWord1` whether the word
1058
+ // contained any escape sequences. This is needed because words with
1059
+ // escape sequences must not be interpreted as keywords.
1060
+
1061
+ var containsEsc;
1062
+
1063
+ // Read an identifier, and return it as a string. Sets `containsEsc`
1064
+ // to whether the word contained a '\u' escape.
1065
+ //
1066
+ // Only builds up the word character-by-character when it actually
1067
+ // containeds an escape, as a micro-optimization.
1068
+
1069
+ function readWord1() {
1070
+ containsEsc = false;
1071
+ var word, first = true, start = tokPos;
1072
+ for (;;) {
1073
+ var ch = input.charCodeAt(tokPos);
1074
+ if (isIdentifierChar(ch)) {
1075
+ if (containsEsc) word += input.charAt(tokPos);
1076
+ ++tokPos;
1077
+ } else if (ch === 92) { // "\"
1078
+ if (!containsEsc) word = input.slice(start, tokPos);
1079
+ containsEsc = true;
1080
+ if (input.charCodeAt(++tokPos) != 117) // "u"
1081
+ raise(tokPos, "Expecting Unicode escape sequence \\uXXXX");
1082
+ ++tokPos;
1083
+ var esc = readHexChar(4);
1084
+ var escStr = String.fromCharCode(esc);
1085
+ if (!escStr) raise(tokPos - 1, "Invalid Unicode escape");
1086
+ if (!(first ? isIdentifierStart(esc) : isIdentifierChar(esc)))
1087
+ raise(tokPos - 4, "Invalid Unicode escape");
1088
+ word += escStr;
1089
+ } else {
1090
+ break;
1091
+ }
1092
+ first = false;
1093
+ }
1094
+ return containsEsc ? word : input.slice(start, tokPos);
1095
+ }
1096
+
1097
+ // Read an identifier or keyword token. Will check for reserved
1098
+ // words when necessary.
1099
+
1100
+ function readWord() {
1101
+ var word = readWord1();
1102
+ var type = _name;
1103
+ if (!containsEsc && isKeyword(word))
1104
+ type = keywordTypes[word];
1105
+ return finishToken(type, word);
1106
+ }
1107
+
1108
+ // ## Parser
1109
+
1110
+ // A recursive descent parser operates by defining functions for all
1111
+ // syntactic elements, and recursively calling those, each function
1112
+ // advancing the input stream and returning an AST node. Precedence
1113
+ // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
1114
+ // instead of `(!x)[1]` is handled by the fact that the parser
1115
+ // function that parses unary prefix operators is called first, and
1116
+ // in turn calls the function that parses `[]` subscripts — that
1117
+ // way, it'll receive the node for `x[1]` already parsed, and wraps
1118
+ // *that* in the unary operator node.
1119
+ //
1120
+ // Acorn uses an [operator precedence parser][opp] to handle binary
1121
+ // operator precedence, because it is much more compact than using
1122
+ // the technique outlined above, which uses different, nesting
1123
+ // functions to specify precedence, for all of the ten binary
1124
+ // precedence levels that JavaScript defines.
1125
+ //
1126
+ // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
1127
+
1128
+ // ### Parser utilities
1129
+
1130
+ // Continue to the next token.
1131
+
1132
+ function next() {
1133
+ lastStart = tokStart;
1134
+ lastEnd = tokEnd;
1135
+ lastEndLoc = tokEndLoc;
1136
+ readToken();
1137
+ }
1138
+
1139
+ // Enter strict mode. Re-reads the next token to please pedantic
1140
+ // tests ("use strict"; 010; -- should fail).
1141
+
1142
+ function setStrict(strct) {
1143
+ strict = strct;
1144
+ tokPos = tokStart;
1145
+ if (options.locations) {
1146
+ while (tokPos < tokLineStart) {
1147
+ tokLineStart = input.lastIndexOf("\n", tokLineStart - 2) + 1;
1148
+ --tokCurLine;
1149
+ }
1150
+ }
1151
+ skipSpace();
1152
+ readToken();
1153
+ }
1154
+
1155
+ // Start an AST node, attaching a start offset.
1156
+
1157
+ function Node() {
1158
+ this.type = null;
1159
+ this.start = tokStart;
1160
+ this.end = null;
1161
+ }
1162
+
1163
+ exports.Node = Node;
1164
+
1165
+ function SourceLocation() {
1166
+ this.start = tokStartLoc;
1167
+ this.end = null;
1168
+ if (sourceFile !== null) this.source = sourceFile;
1169
+ }
1170
+
1171
+ function startNode() {
1172
+ var node = new Node();
1173
+ if (options.locations)
1174
+ node.loc = new SourceLocation();
1175
+ if (options.directSourceFile)
1176
+ node.sourceFile = options.directSourceFile;
1177
+ if (options.ranges)
1178
+ node.range = [tokStart, 0];
1179
+ return node;
1180
+ }
1181
+
1182
+ // Start a node whose start offset information should be based on
1183
+ // the start of another node. For example, a binary operator node is
1184
+ // only started after its left-hand side has already been parsed.
1185
+
1186
+ function startNodeFrom(other) {
1187
+ var node = new Node();
1188
+ node.start = other.start;
1189
+ if (options.locations) {
1190
+ node.loc = new SourceLocation();
1191
+ node.loc.start = other.loc.start;
1192
+ }
1193
+ if (options.ranges)
1194
+ node.range = [other.range[0], 0];
1195
+
1196
+ return node;
1197
+ }
1198
+
1199
+ // Finish an AST node, adding `type` and `end` properties.
1200
+
1201
+ function finishNode(node, type) {
1202
+ node.type = type;
1203
+ node.end = lastEnd;
1204
+ if (options.locations)
1205
+ node.loc.end = lastEndLoc;
1206
+ if (options.ranges)
1207
+ node.range[1] = lastEnd;
1208
+ return node;
1209
+ }
1210
+
1211
+ // Test whether a statement node is the string literal `"use strict"`.
1212
+
1213
+ function isUseStrict(stmt) {
1214
+ return options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&
1215
+ stmt.expression.type === "Literal" && stmt.expression.value === "use strict";
1216
+ }
1217
+
1218
+ // Predicate that tests whether the next token is of the given
1219
+ // type, and if yes, consumes it as a side effect.
1220
+
1221
+ function eat(type) {
1222
+ if (tokType === type) {
1223
+ next();
1224
+ return true;
1225
+ } else {
1226
+ return false;
1227
+ }
1228
+ }
1229
+
1230
+ // Test whether a semicolon can be inserted at the current position.
1231
+
1232
+ function canInsertSemicolon() {
1233
+ return !options.strictSemicolons &&
1234
+ (tokType === _eof || tokType === _braceR || newline.test(input.slice(lastEnd, tokStart)));
1235
+ }
1236
+
1237
+ // Consume a semicolon, or, failing that, see if we are allowed to
1238
+ // pretend that there is a semicolon at this position.
1239
+
1240
+ function semicolon() {
1241
+ if (!eat(_semi) && !canInsertSemicolon()) unexpected();
1242
+ }
1243
+
1244
+ // Expect a token of a given type. If found, consume it, otherwise,
1245
+ // raise an unexpected token error.
1246
+
1247
+ function expect(type) {
1248
+ eat(type) || unexpected();
1249
+ }
1250
+
1251
+ // Raise an unexpected token error.
1252
+
1253
+ function unexpected(pos) {
1254
+ raise(pos != null ? pos : tokStart, "Unexpected token");
1255
+ }
1256
+
1257
+ // Checks if hash object has a property.
1258
+
1259
+ function has(obj, propName) {
1260
+ return Object.prototype.hasOwnProperty.call(obj, propName);
1261
+ }
1262
+ // Convert existing expression atom to assignable pattern
1263
+ // if possible.
1264
+
1265
+ function toAssignable(node, allowSpread, checkType) {
1266
+ if (options.ecmaVersion >= 6 && node) {
1267
+ switch (node.type) {
1268
+ case "Identifier":
1269
+ case "MemberExpression":
1270
+ break;
1271
+
1272
+ case "ObjectExpression":
1273
+ node.type = "ObjectPattern";
1274
+ for (var i = 0; i < node.properties.length; i++) {
1275
+ var prop = node.properties[i];
1276
+ if (prop.kind !== "init") unexpected(prop.key.start);
1277
+ toAssignable(prop.value, false, checkType);
1278
+ }
1279
+ break;
1280
+
1281
+ case "ArrayExpression":
1282
+ node.type = "ArrayPattern";
1283
+ for (var i = 0, lastI = node.elements.length - 1; i <= lastI; i++) {
1284
+ toAssignable(node.elements[i], i === lastI, checkType);
1285
+ }
1286
+ break;
1287
+
1288
+ case "SpreadElement":
1289
+ if (allowSpread) {
1290
+ toAssignable(node.argument, false, checkType);
1291
+ checkSpreadAssign(node.argument);
1292
+ } else {
1293
+ unexpected(node.start);
1294
+ }
1295
+ break;
1296
+
1297
+ default:
1298
+ if (checkType) unexpected(node.start);
1299
+ }
1300
+ }
1301
+ return node;
1302
+ }
1303
+
1304
+ // Checks if node can be assignable spread argument.
1305
+
1306
+ function checkSpreadAssign(node) {
1307
+ if (node.type !== "Identifier" && node.type !== "ArrayPattern")
1308
+ unexpected(node.start);
1309
+ }
1310
+
1311
+ // Verify that argument names are not repeated, and it does not
1312
+ // try to bind the words `eval` or `arguments`.
1313
+
1314
+ function checkFunctionParam(param, nameHash) {
1315
+ switch (param.type) {
1316
+ case "Identifier":
1317
+ if (isStrictReservedWord(param.name) || isStrictBadIdWord(param.name))
1318
+ raise(param.start, "Defining '" + param.name + "' in strict mode");
1319
+ if (has(nameHash, param.name))
1320
+ raise(param.start, "Argument name clash in strict mode");
1321
+ nameHash[param.name] = true;
1322
+ break;
1323
+
1324
+ case "ObjectPattern":
1325
+ for (var i = 0; i < param.properties.length; i++)
1326
+ checkFunctionParam(param.properties[i].value, nameHash);
1327
+ break;
1328
+
1329
+ case "ArrayPattern":
1330
+ for (var i = 0; i < param.elements.length; i++)
1331
+ checkFunctionParam(param.elements[i], nameHash);
1332
+ break;
1333
+ }
1334
+ }
1335
+
1336
+ // Check if property name clashes with already added.
1337
+ // Object/class getters and setters are not allowed to clash —
1338
+ // either with each other or with an init property — and in
1339
+ // strict mode, init properties are also not allowed to be repeated.
1340
+
1341
+ function checkPropClash(prop, propHash) {
1342
+ if (prop.computed) return;
1343
+ var key = prop.key, name;
1344
+ switch (key.type) {
1345
+ case "Identifier": name = key.name; break;
1346
+ case "Literal": name = String(key.value); break;
1347
+ default: return;
1348
+ }
1349
+ var kind = prop.kind || "init", other;
1350
+ if (has(propHash, name)) {
1351
+ other = propHash[name];
1352
+ var isGetSet = kind !== "init";
1353
+ if ((strict || isGetSet) && other[kind] || !(isGetSet ^ other.init))
1354
+ raise(key.start, "Redefinition of property");
1355
+ } else {
1356
+ other = propHash[name] = {
1357
+ init: false,
1358
+ get: false,
1359
+ set: false
1360
+ };
1361
+ }
1362
+ other[kind] = true;
1363
+ }
1364
+
1365
+ // Verify that a node is an lval — something that can be assigned
1366
+ // to.
1367
+
1368
+ function checkLVal(expr, isBinding) {
1369
+ switch (expr.type) {
1370
+ case "Identifier":
1371
+ if (strict && (isStrictBadIdWord(expr.name) || isStrictReservedWord(expr.name)))
1372
+ raise(expr.start, isBinding
1373
+ ? "Binding " + expr.name + " in strict mode"
1374
+ : "Assigning to " + expr.name + " in strict mode"
1375
+ );
1376
+ break;
1377
+
1378
+ case "MemberExpression":
1379
+ if (!isBinding) break;
1380
+
1381
+ case "ObjectPattern":
1382
+ for (var i = 0; i < expr.properties.length; i++)
1383
+ checkLVal(expr.properties[i].value, isBinding);
1384
+ break;
1385
+
1386
+ case "ArrayPattern":
1387
+ for (var i = 0; i < expr.elements.length; i++) {
1388
+ var elem = expr.elements[i];
1389
+ if (elem) checkLVal(elem, isBinding);
1390
+ }
1391
+ break;
1392
+
1393
+ case "SpreadElement":
1394
+ break;
1395
+
1396
+ default:
1397
+ raise(expr.start, "Assigning to rvalue");
1398
+ }
1399
+ }
1400
+
1401
+ // ### Statement parsing
1402
+
1403
+ // Parse a program. Initializes the parser, reads any number of
1404
+ // statements, and wraps them in a Program node. Optionally takes a
1405
+ // `program` argument. If present, the statements will be appended
1406
+ // to its body instead of creating a new node.
1407
+
1408
+ function parseTopLevel(program) {
1409
+ lastStart = lastEnd = tokPos;
1410
+ if (options.locations) lastEndLoc = new Position;
1411
+ inFunction = inGenerator = strict = null;
1412
+ labels = [];
1413
+ readToken();
1414
+
1415
+ var node = program || startNode(), first = true;
1416
+ if (!program) node.body = [];
1417
+ while (tokType !== _eof) {
1418
+ var stmt = parseStatement();
1419
+ node.body.push(stmt);
1420
+ if (first && isUseStrict(stmt)) setStrict(true);
1421
+ first = false;
1422
+ }
1423
+ return finishNode(node, "Program");
1424
+ }
1425
+
1426
+ var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
1427
+
1428
+ // Parse a single statement.
1429
+ //
1430
+ // If expecting a statement and finding a slash operator, parse a
1431
+ // regular expression literal. This is to handle cases like
1432
+ // `if (foo) /blah/.exec(foo);`, where looking at the previous token
1433
+ // does not help.
1434
+
1435
+ function parseStatement() {
1436
+ if (tokType === _slash || tokType === _assign && tokVal == "/=")
1437
+ readToken(true);
1438
+
1439
+ var starttype = tokType, node = startNode();
1440
+
1441
+ // Most types of statements are recognized by the keyword they
1442
+ // start with. Many are trivial to parse, some require a bit of
1443
+ // complexity.
1444
+
1445
+ switch (starttype) {
1446
+ case _break: case _continue: return parseBreakContinueStatement(node, starttype.keyword);
1447
+ case _debugger: return parseDebuggerStatement(node);
1448
+ case _do: return parseDoStatement(node);
1449
+ case _for: return parseForStatement(node);
1450
+ case _function: return parseFunctionStatement(node);
1451
+ case _class: return parseClass(node, true);
1452
+ case _if: return parseIfStatement(node);
1453
+ case _return: return parseReturnStatement(node);
1454
+ case _switch: return parseSwitchStatement(node);
1455
+ case _throw: return parseThrowStatement(node);
1456
+ case _try: return parseTryStatement(node);
1457
+ case _var: case _let: case _const: return parseVarStatement(node, starttype.keyword);
1458
+ case _while: return parseWhileStatement(node);
1459
+ case _with: return parseWithStatement(node);
1460
+ case _braceL: return parseBlock(); // no point creating a function for this
1461
+ case _semi: return parseEmptyStatement(node);
1462
+ case _export: return parseExport(node);
1463
+ case _import: return parseImport(node);
1464
+
1465
+ // If the statement does not start with a statement keyword or a
1466
+ // brace, it's an ExpressionStatement or LabeledStatement. We
1467
+ // simply start parsing an expression, and afterwards, if the
1468
+ // next token is a colon and the expression was a simple
1469
+ // Identifier node, we switch to interpreting it as a label.
1470
+ default:
1471
+ var maybeName = tokVal, expr = parseExpression();
1472
+ if (starttype === _name && expr.type === "Identifier" && eat(_colon))
1473
+ return parseLabeledStatement(node, maybeName, expr);
1474
+ else return parseExpressionStatement(node, expr);
1475
+ }
1476
+ }
1477
+
1478
+ function parseBreakContinueStatement(node, keyword) {
1479
+ var isBreak = keyword == "break";
1480
+ next();
1481
+ if (eat(_semi) || canInsertSemicolon()) node.label = null;
1482
+ else if (tokType !== _name) unexpected();
1483
+ else {
1484
+ node.label = parseIdent();
1485
+ semicolon();
1486
+ }
1487
+
1488
+ // Verify that there is an actual destination to break or
1489
+ // continue to.
1490
+ for (var i = 0; i < labels.length; ++i) {
1491
+ var lab = labels[i];
1492
+ if (node.label == null || lab.name === node.label.name) {
1493
+ if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
1494
+ if (node.label && isBreak) break;
1495
+ }
1496
+ }
1497
+ if (i === labels.length) raise(node.start, "Unsyntactic " + keyword);
1498
+ return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
1499
+ }
1500
+
1501
+ function parseDebuggerStatement(node) {
1502
+ next();
1503
+ semicolon();
1504
+ return finishNode(node, "DebuggerStatement");
1505
+ }
1506
+
1507
+ function parseDoStatement(node) {
1508
+ next();
1509
+ labels.push(loopLabel);
1510
+ node.body = parseStatement();
1511
+ labels.pop();
1512
+ expect(_while);
1513
+ node.test = parseParenExpression();
1514
+ semicolon();
1515
+ return finishNode(node, "DoWhileStatement");
1516
+ }
1517
+
1518
+ // Disambiguating between a `for` and a `for`/`in` or `for`/`of`
1519
+ // loop is non-trivial. Basically, we have to parse the init `var`
1520
+ // statement or expression, disallowing the `in` operator (see
1521
+ // the second parameter to `parseExpression`), and then check
1522
+ // whether the next token is `in` or `of`. When there is no init
1523
+ // part (semicolon immediately after the opening parenthesis), it
1524
+ // is a regular `for` loop.
1525
+
1526
+ function parseForStatement(node) {
1527
+ next();
1528
+ labels.push(loopLabel);
1529
+ expect(_parenL);
1530
+ if (tokType === _semi) return parseFor(node, null);
1531
+ if (tokType === _var || tokType === _let) {
1532
+ var init = startNode(), varKind = tokType.keyword, isLet = tokType === _let;
1533
+ next();
1534
+ parseVar(init, true, varKind);
1535
+ finishNode(init, "VariableDeclaration");
1536
+ if ((tokType === _in || (tokType === _name && tokVal === "of")) && init.declarations.length === 1 &&
1537
+ !(isLet && init.declarations[0].init))
1538
+ return parseForIn(node, init);
1539
+ return parseFor(node, init);
1540
+ }
1541
+ var init = parseExpression(false, true);
1542
+ if (tokType === _in || (tokType === _name && tokVal === "of")) {
1543
+ checkLVal(init);
1544
+ return parseForIn(node, init);
1545
+ }
1546
+ return parseFor(node, init);
1547
+ }
1548
+
1549
+ function parseFunctionStatement(node) {
1550
+ next();
1551
+ return parseFunction(node, true);
1552
+ }
1553
+
1554
+ function parseIfStatement(node) {
1555
+ next();
1556
+ node.test = parseParenExpression();
1557
+ node.consequent = parseStatement();
1558
+ node.alternate = eat(_else) ? parseStatement() : null;
1559
+ return finishNode(node, "IfStatement");
1560
+ }
1561
+
1562
+ function parseReturnStatement(node) {
1563
+ if (!inFunction && !options.allowReturnOutsideFunction)
1564
+ raise(tokStart, "'return' outside of function");
1565
+ next();
1566
+
1567
+ // In `return` (and `break`/`continue`), the keywords with
1568
+ // optional arguments, we eagerly look for a semicolon or the
1569
+ // possibility to insert one.
1570
+
1571
+ if (eat(_semi) || canInsertSemicolon()) node.argument = null;
1572
+ else { node.argument = parseExpression(); semicolon(); }
1573
+ return finishNode(node, "ReturnStatement");
1574
+ }
1575
+
1576
+ function parseSwitchStatement(node) {
1577
+ next();
1578
+ node.discriminant = parseParenExpression();
1579
+ node.cases = [];
1580
+ expect(_braceL);
1581
+ labels.push(switchLabel);
1582
+
1583
+ // Statements under must be grouped (by label) in SwitchCase
1584
+ // nodes. `cur` is used to keep the node that we are currently
1585
+ // adding statements to.
1586
+
1587
+ for (var cur, sawDefault; tokType != _braceR;) {
1588
+ if (tokType === _case || tokType === _default) {
1589
+ var isCase = tokType === _case;
1590
+ if (cur) finishNode(cur, "SwitchCase");
1591
+ node.cases.push(cur = startNode());
1592
+ cur.consequent = [];
1593
+ next();
1594
+ if (isCase) cur.test = parseExpression();
1595
+ else {
1596
+ if (sawDefault) raise(lastStart, "Multiple default clauses"); sawDefault = true;
1597
+ cur.test = null;
1598
+ }
1599
+ expect(_colon);
1600
+ } else {
1601
+ if (!cur) unexpected();
1602
+ cur.consequent.push(parseStatement());
1603
+ }
1604
+ }
1605
+ if (cur) finishNode(cur, "SwitchCase");
1606
+ next(); // Closing brace
1607
+ labels.pop();
1608
+ return finishNode(node, "SwitchStatement");
1609
+ }
1610
+
1611
+ function parseThrowStatement(node) {
1612
+ next();
1613
+ if (newline.test(input.slice(lastEnd, tokStart)))
1614
+ raise(lastEnd, "Illegal newline after throw");
1615
+ node.argument = parseExpression();
1616
+ semicolon();
1617
+ return finishNode(node, "ThrowStatement");
1618
+ }
1619
+
1620
+ function parseTryStatement(node) {
1621
+ next();
1622
+ node.block = parseBlock();
1623
+ node.handler = null;
1624
+ if (tokType === _catch) {
1625
+ var clause = startNode();
1626
+ next();
1627
+ expect(_parenL);
1628
+ clause.param = parseIdent();
1629
+ if (strict && isStrictBadIdWord(clause.param.name))
1630
+ raise(clause.param.start, "Binding " + clause.param.name + " in strict mode");
1631
+ expect(_parenR);
1632
+ clause.guard = null;
1633
+ clause.body = parseBlock();
1634
+ node.handler = finishNode(clause, "CatchClause");
1635
+ }
1636
+ node.guardedHandlers = empty;
1637
+ node.finalizer = eat(_finally) ? parseBlock() : null;
1638
+ if (!node.handler && !node.finalizer)
1639
+ raise(node.start, "Missing catch or finally clause");
1640
+ return finishNode(node, "TryStatement");
1641
+ }
1642
+
1643
+ function parseVarStatement(node, kind) {
1644
+ next();
1645
+ parseVar(node, false, kind);
1646
+ semicolon();
1647
+ return finishNode(node, "VariableDeclaration");
1648
+ }
1649
+
1650
+ function parseWhileStatement(node) {
1651
+ next();
1652
+ node.test = parseParenExpression();
1653
+ labels.push(loopLabel);
1654
+ node.body = parseStatement();
1655
+ labels.pop();
1656
+ return finishNode(node, "WhileStatement");
1657
+ }
1658
+
1659
+ function parseWithStatement(node) {
1660
+ if (strict) raise(tokStart, "'with' in strict mode");
1661
+ next();
1662
+ node.object = parseParenExpression();
1663
+ node.body = parseStatement();
1664
+ return finishNode(node, "WithStatement");
1665
+ }
1666
+
1667
+ function parseEmptyStatement(node) {
1668
+ next();
1669
+ return finishNode(node, "EmptyStatement");
1670
+ }
1671
+
1672
+ function parseLabeledStatement(node, maybeName, expr) {
1673
+ for (var i = 0; i < labels.length; ++i)
1674
+ if (labels[i].name === maybeName) raise(expr.start, "Label '" + maybeName + "' is already declared");
1675
+ var kind = tokType.isLoop ? "loop" : tokType === _switch ? "switch" : null;
1676
+ labels.push({name: maybeName, kind: kind});
1677
+ node.body = parseStatement();
1678
+ labels.pop();
1679
+ node.label = expr;
1680
+ return finishNode(node, "LabeledStatement");
1681
+ }
1682
+
1683
+ function parseExpressionStatement(node, expr) {
1684
+ node.expression = expr;
1685
+ semicolon();
1686
+ return finishNode(node, "ExpressionStatement");
1687
+ }
1688
+
1689
+ // Used for constructs like `switch` and `if` that insist on
1690
+ // parentheses around their expression.
1691
+
1692
+ function parseParenExpression() {
1693
+ expect(_parenL);
1694
+ var val = parseExpression();
1695
+ expect(_parenR);
1696
+ return val;
1697
+ }
1698
+
1699
+ // Parse a semicolon-enclosed block of statements, handling `"use
1700
+ // strict"` declarations when `allowStrict` is true (used for
1701
+ // function bodies).
1702
+
1703
+ function parseBlock(allowStrict) {
1704
+ var node = startNode(), first = true, strict = false, oldStrict;
1705
+ node.body = [];
1706
+ expect(_braceL);
1707
+ while (!eat(_braceR)) {
1708
+ var stmt = parseStatement();
1709
+ node.body.push(stmt);
1710
+ if (first && allowStrict && isUseStrict(stmt)) {
1711
+ oldStrict = strict;
1712
+ setStrict(strict = true);
1713
+ }
1714
+ first = false;
1715
+ }
1716
+ if (strict && !oldStrict) setStrict(false);
1717
+ return finishNode(node, "BlockStatement");
1718
+ }
1719
+
1720
+ // Parse a regular `for` loop. The disambiguation code in
1721
+ // `parseStatement` will already have parsed the init statement or
1722
+ // expression.
1723
+
1724
+ function parseFor(node, init) {
1725
+ node.init = init;
1726
+ expect(_semi);
1727
+ node.test = tokType === _semi ? null : parseExpression();
1728
+ expect(_semi);
1729
+ node.update = tokType === _parenR ? null : parseExpression();
1730
+ expect(_parenR);
1731
+ node.body = parseStatement();
1732
+ labels.pop();
1733
+ return finishNode(node, "ForStatement");
1734
+ }
1735
+
1736
+ // Parse a `for`/`in` and `for`/`of` loop, which are almost
1737
+ // same from parser's perspective.
1738
+
1739
+ function parseForIn(node, init) {
1740
+ var type = tokType === _in ? "ForInStatement" : "ForOfStatement";
1741
+ next();
1742
+ node.left = init;
1743
+ node.right = parseExpression();
1744
+ expect(_parenR);
1745
+ node.body = parseStatement();
1746
+ labels.pop();
1747
+ return finishNode(node, type);
1748
+ }
1749
+
1750
+ // Parse a list of variable declarations.
1751
+
1752
+ function parseVar(node, noIn, kind) {
1753
+ node.declarations = [];
1754
+ node.kind = kind;
1755
+ for (;;) {
1756
+ var decl = startNode();
1757
+ decl.id = options.ecmaVersion >= 6 ? toAssignable(parseExprAtom()) : parseIdent();
1758
+ checkLVal(decl.id, true);
1759
+ decl.init = eat(_eq) ? parseExpression(true, noIn) : (kind === _const.keyword ? unexpected() : null);
1760
+ node.declarations.push(finishNode(decl, "VariableDeclarator"));
1761
+ if (!eat(_comma)) break;
1762
+ }
1763
+ return node;
1764
+ }
1765
+
1766
+ // ### Expression parsing
1767
+
1768
+ // These nest, from the most general expression type at the top to
1769
+ // 'atomic', nondivisible expression types at the bottom. Most of
1770
+ // the functions will simply let the function(s) below them parse,
1771
+ // and, *if* the syntactic construct they handle is present, wrap
1772
+ // the AST node that the inner parser gave them in another node.
1773
+
1774
+ // Parse a full expression. The arguments are used to forbid comma
1775
+ // sequences (in argument lists, array literals, or object literals)
1776
+ // or the `in` operator (in for loops initalization expressions).
1777
+
1778
+ function parseExpression(noComma, noIn) {
1779
+ var expr = parseMaybeAssign(noIn);
1780
+ if (!noComma && tokType === _comma) {
1781
+ var node = startNodeFrom(expr);
1782
+ node.expressions = [expr];
1783
+ while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn));
1784
+ return finishNode(node, "SequenceExpression");
1785
+ }
1786
+ return expr;
1787
+ }
1788
+
1789
+ // Parse an assignment expression. This includes applications of
1790
+ // operators like `+=`.
1791
+
1792
+ function parseMaybeAssign(noIn) {
1793
+ var left = parseMaybeConditional(noIn);
1794
+ if (tokType.isAssign) {
1795
+ var node = startNodeFrom(left);
1796
+ node.operator = tokVal;
1797
+ node.left = tokType === _eq ? toAssignable(left) : left;
1798
+ checkLVal(left);
1799
+ next();
1800
+ node.right = parseMaybeAssign(noIn);
1801
+ return finishNode(node, "AssignmentExpression");
1802
+ }
1803
+ return left;
1804
+ }
1805
+
1806
+ // Parse a ternary conditional (`?:`) operator.
1807
+
1808
+ function parseMaybeConditional(noIn) {
1809
+ var expr = parseExprOps(noIn);
1810
+ if (eat(_question)) {
1811
+ var node = startNodeFrom(expr);
1812
+ node.test = expr;
1813
+ node.consequent = parseExpression(true);
1814
+ expect(_colon);
1815
+ node.alternate = parseExpression(true, noIn);
1816
+ return finishNode(node, "ConditionalExpression");
1817
+ }
1818
+ return expr;
1819
+ }
1820
+
1821
+ // Start the precedence parser.
1822
+
1823
+ function parseExprOps(noIn) {
1824
+ return parseExprOp(parseMaybeUnary(), -1, noIn);
1825
+ }
1826
+
1827
+ // Parse binary operators with the operator precedence parsing
1828
+ // algorithm. `left` is the left-hand side of the operator.
1829
+ // `minPrec` provides context that allows the function to stop and
1830
+ // defer further parser to one of its callers when it encounters an
1831
+ // operator that has a lower precedence than the set it is parsing.
1832
+
1833
+ function parseExprOp(left, minPrec, noIn) {
1834
+ var prec = tokType.binop;
1835
+ if (prec != null && (!noIn || tokType !== _in)) {
1836
+ if (prec > minPrec) {
1837
+ var node = startNodeFrom(left);
1838
+ node.left = left;
1839
+ node.operator = tokVal;
1840
+ var op = tokType;
1841
+ next();
1842
+ node.right = parseExprOp(parseMaybeUnary(), prec, noIn);
1843
+ var exprNode = finishNode(node, (op === _logicalOR || op === _logicalAND) ? "LogicalExpression" : "BinaryExpression");
1844
+ return parseExprOp(exprNode, minPrec, noIn);
1845
+ }
1846
+ }
1847
+ return left;
1848
+ }
1849
+
1850
+ // Parse unary operators, both prefix and postfix.
1851
+
1852
+ function parseMaybeUnary() {
1853
+ if (tokType.prefix) {
1854
+ var node = startNode(), update = tokType.isUpdate;
1855
+ node.operator = tokVal;
1856
+ node.prefix = true;
1857
+ tokRegexpAllowed = true;
1858
+ next();
1859
+ node.argument = parseMaybeUnary();
1860
+ if (update) checkLVal(node.argument);
1861
+ else if (strict && node.operator === "delete" &&
1862
+ node.argument.type === "Identifier")
1863
+ raise(node.start, "Deleting local variable in strict mode");
1864
+ return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
1865
+ }
1866
+ var expr = parseExprSubscripts();
1867
+ while (tokType.postfix && !canInsertSemicolon()) {
1868
+ var node = startNodeFrom(expr);
1869
+ node.operator = tokVal;
1870
+ node.prefix = false;
1871
+ node.argument = expr;
1872
+ checkLVal(expr);
1873
+ next();
1874
+ expr = finishNode(node, "UpdateExpression");
1875
+ }
1876
+ return expr;
1877
+ }
1878
+
1879
+ // Parse call, dot, and `[]`-subscript expressions.
1880
+
1881
+ function parseExprSubscripts() {
1882
+ return parseSubscripts(parseExprAtom());
1883
+ }
1884
+
1885
+ function parseSubscripts(base, noCalls) {
1886
+ if (eat(_dot)) {
1887
+ var node = startNodeFrom(base);
1888
+ node.object = base;
1889
+ node.property = parseIdent(true);
1890
+ node.computed = false;
1891
+ return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
1892
+ } else if (eat(_bracketL)) {
1893
+ var node = startNodeFrom(base);
1894
+ node.object = base;
1895
+ node.property = parseExpression();
1896
+ node.computed = true;
1897
+ expect(_bracketR);
1898
+ return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
1899
+ } else if (!noCalls && eat(_parenL)) {
1900
+ var node = startNodeFrom(base);
1901
+ node.callee = base;
1902
+ node.arguments = parseExprList(_parenR, false);
1903
+ return parseSubscripts(finishNode(node, "CallExpression"), noCalls);
1904
+ } else if (tokType === _bquote) {
1905
+ var node = startNodeFrom(base);
1906
+ node.tag = base;
1907
+ node.quasi = parseTemplate();
1908
+ return parseSubscripts(finishNode(node, "TaggedTemplateExpression"), noCalls);
1909
+ } return base;
1910
+ }
1911
+
1912
+ // Parse an atomic expression — either a single token that is an
1913
+ // expression, an expression started by a keyword like `function` or
1914
+ // `new`, or an expression wrapped in punctuation like `()`, `[]`,
1915
+ // or `{}`.
1916
+
1917
+ function parseExprAtom() {
1918
+ switch (tokType) {
1919
+ case _this:
1920
+ var node = startNode();
1921
+ next();
1922
+ return finishNode(node, "ThisExpression");
1923
+
1924
+ case _yield:
1925
+ if (inGenerator) return parseYield();
1926
+
1927
+ case _name:
1928
+ var id = parseIdent(tokType !== _name);
1929
+ if (eat(_arrow)) {
1930
+ return parseArrowExpression(startNodeFrom(id), [id]);
1931
+ }
1932
+ return id;
1933
+
1934
+ case _num: case _string: case _regexp:
1935
+ var node = startNode();
1936
+ node.value = tokVal;
1937
+ node.raw = input.slice(tokStart, tokEnd);
1938
+ next();
1939
+ return finishNode(node, "Literal");
1940
+
1941
+ case _null: case _true: case _false:
1942
+ var node = startNode();
1943
+ node.value = tokType.atomValue;
1944
+ node.raw = tokType.keyword;
1945
+ next();
1946
+ return finishNode(node, "Literal");
1947
+
1948
+ case _parenL:
1949
+ var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart, val, exprList;
1950
+ next();
1951
+ // check whether this is generator comprehension or regular expression
1952
+ if (options.ecmaVersion >= 6 && tokType === _for) {
1953
+ val = parseComprehension(startNode(), true);
1954
+ } else {
1955
+ var oldParenL = ++metParenL;
1956
+ if (tokType !== _parenR) {
1957
+ val = parseExpression();
1958
+ exprList = val.type === "SequenceExpression" ? val.expressions : [val];
1959
+ } else {
1960
+ exprList = [];
1961
+ }
1962
+ expect(_parenR);
1963
+ // if '=>' follows '(...)', convert contents to arguments
1964
+ if (metParenL === oldParenL && eat(_arrow)) {
1965
+ val = parseArrowExpression(startNode(), exprList);
1966
+ } else {
1967
+ // forbid '()' before everything but '=>'
1968
+ if (!val) unexpected(lastStart);
1969
+ // forbid '...' in sequence expressions
1970
+ if (options.ecmaVersion >= 6) {
1971
+ for (var i = 0; i < exprList.length; i++) {
1972
+ if (exprList[i].type === "SpreadElement") unexpected();
1973
+ }
1974
+ }
1975
+ }
1976
+ }
1977
+ val.start = tokStart1;
1978
+ val.end = lastEnd;
1979
+ if (options.locations) {
1980
+ val.loc.start = tokStartLoc1;
1981
+ val.loc.end = lastEndLoc;
1982
+ }
1983
+ if (options.ranges) {
1984
+ val.range = [tokStart1, lastEnd];
1985
+ }
1986
+ return val;
1987
+
1988
+ case _bracketL:
1989
+ var node = startNode();
1990
+ next();
1991
+ // check whether this is array comprehension or regular array
1992
+ if (options.ecmaVersion >= 6 && tokType === _for) {
1993
+ return parseComprehension(node, false);
1994
+ }
1995
+ node.elements = parseExprList(_bracketR, true, true);
1996
+ return finishNode(node, "ArrayExpression");
1997
+
1998
+ case _braceL:
1999
+ return parseObj();
2000
+
2001
+ case _function:
2002
+ var node = startNode();
2003
+ next();
2004
+ return parseFunction(node, false);
2005
+
2006
+ case _class:
2007
+ return parseClass(startNode(), false);
2008
+
2009
+ case _new:
2010
+ return parseNew();
2011
+
2012
+ case _ellipsis:
2013
+ return parseSpread();
2014
+
2015
+ case _bquote:
2016
+ return parseTemplate();
2017
+
2018
+ default:
2019
+ unexpected();
2020
+ }
2021
+ }
2022
+
2023
+ // New's precedence is slightly tricky. It must allow its argument
2024
+ // to be a `[]` or dot subscript expression, but not a call — at
2025
+ // least, not without wrapping it in parentheses. Thus, it uses the
2026
+
2027
+ function parseNew() {
2028
+ var node = startNode();
2029
+ next();
2030
+ node.callee = parseSubscripts(parseExprAtom(), true);
2031
+ if (eat(_parenL)) node.arguments = parseExprList(_parenR, false);
2032
+ else node.arguments = empty;
2033
+ return finishNode(node, "NewExpression");
2034
+ }
2035
+
2036
+ // Parse spread element '...expr'
2037
+
2038
+ function parseSpread() {
2039
+ var node = startNode();
2040
+ next();
2041
+ node.argument = parseExpression(true);
2042
+ return finishNode(node, "SpreadElement");
2043
+ }
2044
+
2045
+ // Parse template expression.
2046
+
2047
+ function parseTemplate() {
2048
+ var node = startNode();
2049
+ node.expressions = [];
2050
+ node.quasis = [];
2051
+ inTemplate = true;
2052
+ next();
2053
+ for (;;) {
2054
+ var elem = startNode();
2055
+ elem.value = {cooked: tokVal, raw: input.slice(tokStart, tokEnd)};
2056
+ elem.tail = false;
2057
+ next();
2058
+ node.quasis.push(finishNode(elem, "TemplateElement"));
2059
+ if (eat(_bquote)) { // '`', end of template
2060
+ elem.tail = true;
2061
+ break;
2062
+ }
2063
+ inTemplate = false;
2064
+ expect(_dollarBraceL);
2065
+ node.expressions.push(parseExpression());
2066
+ inTemplate = true;
2067
+ expect(_braceR);
2068
+ }
2069
+ inTemplate = false;
2070
+ return finishNode(node, "TemplateLiteral");
2071
+ }
2072
+
2073
+ // Parse an object literal.
2074
+
2075
+ function parseObj() {
2076
+ var node = startNode(), first = true, propHash = {};
2077
+ node.properties = [];
2078
+ next();
2079
+ while (!eat(_braceR)) {
2080
+ if (!first) {
2081
+ expect(_comma);
2082
+ if (options.allowTrailingCommas && eat(_braceR)) break;
2083
+ } else first = false;
2084
+
2085
+ var prop = startNode(), kind, isGenerator;
2086
+ if (options.ecmaVersion >= 6) {
2087
+ prop.method = false;
2088
+ prop.shorthand = false;
2089
+ isGenerator = eat(_star);
2090
+ }
2091
+ parsePropertyName(prop);
2092
+ if (eat(_colon)) {
2093
+ prop.value = parseExpression(true);
2094
+ kind = prop.kind = "init";
2095
+ } else if (options.ecmaVersion >= 6 && tokType === _parenL) {
2096
+ kind = prop.kind = "init";
2097
+ prop.method = true;
2098
+ prop.value = parseMethod(isGenerator);
2099
+ } else if (options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
2100
+ (prop.key.name === "get" || prop.key.name === "set")) {
2101
+ if (isGenerator) unexpected();
2102
+ kind = prop.kind = prop.key.name;
2103
+ parsePropertyName(prop);
2104
+ prop.value = parseMethod(false);
2105
+ } else if (options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
2106
+ kind = prop.kind = "init";
2107
+ prop.value = prop.key;
2108
+ prop.shorthand = true;
2109
+ } else unexpected();
2110
+
2111
+ checkPropClash(prop, propHash);
2112
+ node.properties.push(finishNode(prop, "Property"));
2113
+ }
2114
+ return finishNode(node, "ObjectExpression");
2115
+ }
2116
+
2117
+ function parsePropertyName(prop) {
2118
+ if (options.ecmaVersion >= 6) {
2119
+ if (eat(_bracketL)) {
2120
+ prop.computed = true;
2121
+ prop.key = parseExpression();
2122
+ expect(_bracketR);
2123
+ return;
2124
+ } else {
2125
+ prop.computed = false;
2126
+ }
2127
+ }
2128
+ prop.key = (tokType === _num || tokType === _string) ? parseExprAtom() : parseIdent(true);
2129
+ }
2130
+
2131
+ // Initialize empty function node.
2132
+
2133
+ function initFunction(node) {
2134
+ node.id = null;
2135
+ node.params = [];
2136
+ if (options.ecmaVersion >= 6) {
2137
+ node.defaults = [];
2138
+ node.rest = null;
2139
+ node.generator = false;
2140
+ }
2141
+ }
2142
+
2143
+ // Parse a function declaration or literal (depending on the
2144
+ // `isStatement` parameter).
2145
+
2146
+ function parseFunction(node, isStatement, allowExpressionBody) {
2147
+ initFunction(node);
2148
+ if (options.ecmaVersion >= 6) {
2149
+ node.generator = eat(_star);
2150
+ }
2151
+ if (isStatement || tokType === _name) {
2152
+ node.id = parseIdent();
2153
+ }
2154
+ parseFunctionParams(node);
2155
+ parseFunctionBody(node, allowExpressionBody);
2156
+ return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
2157
+ }
2158
+
2159
+ // Parse object or class method.
2160
+
2161
+ function parseMethod(isGenerator) {
2162
+ var node = startNode();
2163
+ initFunction(node);
2164
+ parseFunctionParams(node);
2165
+ var allowExpressionBody;
2166
+ if (options.ecmaVersion >= 6) {
2167
+ node.generator = isGenerator;
2168
+ allowExpressionBody = true;
2169
+ } else {
2170
+ allowExpressionBody = false;
2171
+ }
2172
+ parseFunctionBody(node, allowExpressionBody);
2173
+ return finishNode(node, "FunctionExpression");
2174
+ }
2175
+
2176
+ // Parse arrow function expression with given parameters.
2177
+
2178
+ function parseArrowExpression(node, params) {
2179
+ initFunction(node);
2180
+
2181
+ var defaults = node.defaults, hasDefaults = false;
2182
+
2183
+ for (var i = 0, lastI = params.length - 1; i <= lastI; i++) {
2184
+ var param = params[i];
2185
+
2186
+ if (param.type === "AssignmentExpression" && param.operator === "=") {
2187
+ hasDefaults = true;
2188
+ params[i] = param.left;
2189
+ defaults.push(param.right);
2190
+ } else {
2191
+ toAssignable(param, i === lastI, true);
2192
+ defaults.push(null);
2193
+ if (param.type === "SpreadElement") {
2194
+ params.length--;
2195
+ node.rest = param.argument;
2196
+ break;
2197
+ }
2198
+ }
2199
+ }
2200
+
2201
+ node.params = params;
2202
+ if (!hasDefaults) node.defaults = [];
2203
+
2204
+ parseFunctionBody(node, true);
2205
+ return finishNode(node, "ArrowFunctionExpression");
2206
+ }
2207
+
2208
+ // Parse function parameters.
2209
+
2210
+ function parseFunctionParams(node) {
2211
+ var defaults = [], hasDefaults = false;
2212
+
2213
+ expect(_parenL);
2214
+ for (;;) {
2215
+ if (eat(_parenR)) {
2216
+ break;
2217
+ } else if (options.ecmaVersion >= 6 && eat(_ellipsis)) {
2218
+ node.rest = toAssignable(parseExprAtom(), false, true);
2219
+ checkSpreadAssign(node.rest);
2220
+ expect(_parenR);
2221
+ break;
2222
+ } else {
2223
+ node.params.push(options.ecmaVersion >= 6 ? toAssignable(parseExprAtom(), false, true) : parseIdent());
2224
+ if (options.ecmaVersion >= 6 && tokType === _eq) {
2225
+ next();
2226
+ hasDefaults = true;
2227
+ defaults.push(parseExpression(true));
2228
+ }
2229
+ if (!eat(_comma)) {
2230
+ expect(_parenR);
2231
+ break;
2232
+ }
2233
+ }
2234
+ }
2235
+
2236
+ if (hasDefaults) node.defaults = defaults;
2237
+ }
2238
+
2239
+ // Parse function body and check parameters.
2240
+
2241
+ function parseFunctionBody(node, allowExpression) {
2242
+ var isExpression = allowExpression && tokType !== _braceL;
2243
+
2244
+ if (isExpression) {
2245
+ node.body = parseExpression(true);
2246
+ node.expression = true;
2247
+ } else {
2248
+ // Start a new scope with regard to labels and the `inFunction`
2249
+ // flag (restore them to their old value afterwards).
2250
+ var oldInFunc = inFunction, oldInGen = inGenerator, oldLabels = labels;
2251
+ inFunction = true; inGenerator = node.generator; labels = [];
2252
+ node.body = parseBlock(true);
2253
+ node.expression = false;
2254
+ inFunction = oldInFunc; inGenerator = oldInGen; labels = oldLabels;
2255
+ }
2256
+
2257
+ // If this is a strict mode function, verify that argument names
2258
+ // are not repeated, and it does not try to bind the words `eval`
2259
+ // or `arguments`.
2260
+ if (strict || !isExpression && node.body.body.length && isUseStrict(node.body.body[0])) {
2261
+ var nameHash = {};
2262
+ if (node.id)
2263
+ checkFunctionParam(node.id, nameHash);
2264
+ for (var i = 0; i < node.params.length; i++)
2265
+ checkFunctionParam(node.params[i], nameHash);
2266
+ if (node.rest)
2267
+ checkFunctionParam(node.rest, nameHash);
2268
+ }
2269
+ }
2270
+
2271
+ // Parse a class declaration or literal (depending on the
2272
+ // `isStatement` parameter).
2273
+
2274
+ function parseClass(node, isStatement) {
2275
+ next();
2276
+ node.id = tokType === _name ? parseIdent() : isStatement ? unexpected() : null;
2277
+ node.superClass = eat(_extends) ? parseExpression() : null;
2278
+ var classBody = startNode(), methodHash = {}, staticMethodHash = {};
2279
+ classBody.body = [];
2280
+ expect(_braceL);
2281
+ while (!eat(_braceR)) {
2282
+ var method = startNode();
2283
+ if (tokType === _name && tokVal === "static") {
2284
+ next();
2285
+ method['static'] = true;
2286
+ } else {
2287
+ method['static'] = false;
2288
+ }
2289
+ var isGenerator = eat(_star);
2290
+ parsePropertyName(method);
2291
+ if (tokType === _name && !method.computed && method.key.type === "Identifier" &&
2292
+ (method.key.name === "get" || method.key.name === "set")) {
2293
+ if (isGenerator) unexpected();
2294
+ method.kind = method.key.name;
2295
+ parsePropertyName(method);
2296
+ } else {
2297
+ method.kind = "";
2298
+ }
2299
+ method.value = parseMethod(isGenerator);
2300
+ checkPropClash(method, method['static'] ? staticMethodHash : methodHash);
2301
+ classBody.body.push(finishNode(method, "MethodDefinition"));
2302
+ eat(_semi);
2303
+ }
2304
+ node.body = finishNode(classBody, "ClassBody");
2305
+ return finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
2306
+ }
2307
+
2308
+ // Parses a comma-separated list of expressions, and returns them as
2309
+ // an array. `close` is the token type that ends the list, and
2310
+ // `allowEmpty` can be turned on to allow subsequent commas with
2311
+ // nothing in between them to be parsed as `null` (which is needed
2312
+ // for array literals).
2313
+
2314
+ function parseExprList(close, allowTrailingComma, allowEmpty) {
2315
+ var elts = [], first = true;
2316
+ while (!eat(close)) {
2317
+ if (!first) {
2318
+ expect(_comma);
2319
+ if (allowTrailingComma && options.allowTrailingCommas && eat(close)) break;
2320
+ } else first = false;
2321
+
2322
+ if (allowEmpty && tokType === _comma) elts.push(null);
2323
+ else elts.push(parseExpression(true));
2324
+ }
2325
+ return elts;
2326
+ }
2327
+
2328
+ // Parse the next token as an identifier. If `liberal` is true (used
2329
+ // when parsing properties), it will also convert keywords into
2330
+ // identifiers.
2331
+
2332
+ function parseIdent(liberal) {
2333
+ var node = startNode();
2334
+ if (liberal && options.forbidReserved == "everywhere") liberal = false;
2335
+ if (tokType === _name) {
2336
+ if (!liberal &&
2337
+ (options.forbidReserved &&
2338
+ (options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(tokVal) ||
2339
+ strict && isStrictReservedWord(tokVal)) &&
2340
+ input.slice(tokStart, tokEnd).indexOf("\\") == -1)
2341
+ raise(tokStart, "The keyword '" + tokVal + "' is reserved");
2342
+ node.name = tokVal;
2343
+ } else if (liberal && tokType.keyword) {
2344
+ node.name = tokType.keyword;
2345
+ } else {
2346
+ unexpected();
2347
+ }
2348
+ tokRegexpAllowed = false;
2349
+ next();
2350
+ return finishNode(node, "Identifier");
2351
+ }
2352
+
2353
+ // Parses module export declaration.
2354
+
2355
+ function parseExport(node) {
2356
+ next();
2357
+ // export var|const|let|function|class ...;
2358
+ if (tokType === _var || tokType === _const || tokType === _let || tokType === _function || tokType === _class) {
2359
+ node.declaration = parseStatement();
2360
+ node['default'] = false;
2361
+ node.specifiers = null;
2362
+ node.source = null;
2363
+ } else
2364
+ // export default ...;
2365
+ if (eat(_default)) {
2366
+ node.declaration = parseExpression(true);
2367
+ node['default'] = true;
2368
+ node.specifiers = null;
2369
+ node.source = null;
2370
+ semicolon();
2371
+ } else {
2372
+ // export * from '...'
2373
+ // export { x, y as z } [from '...']
2374
+ var isBatch = tokType === _star;
2375
+ node.declaration = null;
2376
+ node['default'] = false;
2377
+ node.specifiers = parseExportSpecifiers();
2378
+ if (tokType === _name && tokVal === "from") {
2379
+ next();
2380
+ node.source = tokType === _string ? parseExprAtom() : unexpected();
2381
+ } else {
2382
+ if (isBatch) unexpected();
2383
+ node.source = null;
2384
+ }
2385
+ }
2386
+ return finishNode(node, "ExportDeclaration");
2387
+ }
2388
+
2389
+ // Parses a comma-separated list of module exports.
2390
+
2391
+ function parseExportSpecifiers() {
2392
+ var nodes = [], first = true;
2393
+ if (tokType === _star) {
2394
+ // export * from '...'
2395
+ var node = startNode();
2396
+ next();
2397
+ nodes.push(finishNode(node, "ExportBatchSpecifier"));
2398
+ } else {
2399
+ // export { x, y as z } [from '...']
2400
+ expect(_braceL);
2401
+ while (!eat(_braceR)) {
2402
+ if (!first) {
2403
+ expect(_comma);
2404
+ if (options.allowTrailingCommas && eat(_braceR)) break;
2405
+ } else first = false;
2406
+
2407
+ var node = startNode();
2408
+ node.id = parseIdent();
2409
+ if (tokType === _name && tokVal === "as") {
2410
+ next();
2411
+ node.name = parseIdent(true);
2412
+ } else {
2413
+ node.name = null;
2414
+ }
2415
+ nodes.push(finishNode(node, "ExportSpecifier"));
2416
+ }
2417
+ }
2418
+ return nodes;
2419
+ }
2420
+
2421
+ // Parses import declaration.
2422
+
2423
+ function parseImport(node) {
2424
+ next();
2425
+ // import '...';
2426
+ if (tokType === _string) {
2427
+ node.specifiers = [];
2428
+ node.source = parseExprAtom();
2429
+ node.kind = "";
2430
+ } else {
2431
+ node.specifiers = parseImportSpecifiers();
2432
+ if (tokType !== _name || tokVal !== "from") unexpected();
2433
+ next();
2434
+ node.source = tokType === _string ? parseExprAtom() : unexpected();
2435
+ // only for backward compatibility with Esprima's AST
2436
+ // (it doesn't support mixed default + named yet)
2437
+ node.kind = node.specifiers[0]['default'] ? "default" : "named";
2438
+ }
2439
+ return finishNode(node, "ImportDeclaration");
2440
+ }
2441
+
2442
+ // Parses a comma-separated list of module imports.
2443
+
2444
+ function parseImportSpecifiers() {
2445
+ var nodes = [], first = true;
2446
+ if (tokType === _star) {
2447
+ var node = startNode();
2448
+ next();
2449
+ if (tokType !== _name || tokVal !== "as") unexpected();
2450
+ next();
2451
+ node.name = parseIdent();
2452
+ checkLVal(node.name, true);
2453
+ nodes.push(finishNode(node, "ImportBatchSpecifier"));
2454
+ return nodes;
2455
+ }
2456
+ if (tokType === _name) {
2457
+ // import defaultObj, { x, y as z } from '...'
2458
+ var node = startNode();
2459
+ node.id = parseIdent();
2460
+ checkLVal(node.id, true);
2461
+ node.name = null;
2462
+ node['default'] = true;
2463
+ nodes.push(finishNode(node, "ImportSpecifier"));
2464
+ if (!eat(_comma)) return nodes;
2465
+ }
2466
+ expect(_braceL);
2467
+ while (!eat(_braceR)) {
2468
+ if (!first) {
2469
+ expect(_comma);
2470
+ if (options.allowTrailingCommas && eat(_braceR)) break;
2471
+ } else first = false;
2472
+
2473
+ var node = startNode();
2474
+ node.id = parseIdent(true);
2475
+ if (tokType === _name && tokVal === "as") {
2476
+ next();
2477
+ node.name = parseIdent();
2478
+ } else {
2479
+ node.name = null;
2480
+ }
2481
+ checkLVal(node.name || node.id, true);
2482
+ node['default'] = false;
2483
+ nodes.push(finishNode(node, "ImportSpecifier"));
2484
+ }
2485
+ return nodes;
2486
+ }
2487
+
2488
+ // Parses yield expression inside generator.
2489
+
2490
+ function parseYield() {
2491
+ var node = startNode();
2492
+ next();
2493
+ if (eat(_semi) || canInsertSemicolon()) {
2494
+ node.delegate = false;
2495
+ node.argument = null;
2496
+ } else {
2497
+ node.delegate = eat(_star);
2498
+ node.argument = parseExpression(true);
2499
+ }
2500
+ return finishNode(node, "YieldExpression");
2501
+ }
2502
+
2503
+ // Parses array and generator comprehensions.
2504
+
2505
+ function parseComprehension(node, isGenerator) {
2506
+ node.blocks = [];
2507
+ while (tokType === _for) {
2508
+ var block = startNode();
2509
+ next();
2510
+ expect(_parenL);
2511
+ block.left = toAssignable(parseExprAtom());
2512
+ checkLVal(block.left, true);
2513
+ if (tokType !== _name || tokVal !== "of") unexpected();
2514
+ next();
2515
+ // `of` property is here for compatibility with Esprima's AST
2516
+ // which also supports deprecated [for (... in ...) expr]
2517
+ block.of = true;
2518
+ block.right = parseExpression();
2519
+ expect(_parenR);
2520
+ node.blocks.push(finishNode(block, "ComprehensionBlock"));
2521
+ }
2522
+ node.filter = eat(_if) ? parseParenExpression() : null;
2523
+ node.body = parseExpression();
2524
+ expect(isGenerator ? _parenR : _bracketR);
2525
+ node.generator = isGenerator;
2526
+ return finishNode(node, "ComprehensionExpression");
2527
+ }
2528
+
2529
+ });