rb-document-form-constructor 0.2.0 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -186,7 +186,4691 @@ function _nonIterableRest() {
186
186
  });
187
187
  return fields;
188
188
  }
189
- };var __clone = function __clone(data) {
189
+ };var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
190
+
191
+ function createCommonjsModule(fn, basedir, module) {
192
+ return module = {
193
+ path: basedir,
194
+ exports: {},
195
+ require: function (path, base) {
196
+ return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
197
+ }
198
+ }, fn(module, module.exports), module.exports;
199
+ }
200
+
201
+ function commonjsRequire () {
202
+ throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
203
+ }var esprima = createCommonjsModule(function (module, exports) {
204
+ /*
205
+ Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
206
+ Copyright (C) 2012 Mathias Bynens <mathias@qiwi.be>
207
+ Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
208
+ Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
209
+ Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
210
+ Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
211
+ Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
212
+
213
+ Redistribution and use in source and binary forms, with or without
214
+ modification, are permitted provided that the following conditions are met:
215
+
216
+ * Redistributions of source code must retain the above copyright
217
+ notice, this list of conditions and the following disclaimer.
218
+ * Redistributions in binary form must reproduce the above copyright
219
+ notice, this list of conditions and the following disclaimer in the
220
+ documentation and/or other materials provided with the distribution.
221
+
222
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
223
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225
+ ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
226
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
227
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
228
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
229
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
230
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
232
+ */
233
+
234
+ /*jslint bitwise:true plusplus:true */
235
+ /*global esprima:true, define:true, exports:true, window: true,
236
+ throwError: true, createLiteral: true, generateStatement: true,
237
+ parseAssignmentExpression: true, parseBlock: true, parseExpression: true,
238
+ parseFunctionDeclaration: true, parseFunctionExpression: true,
239
+ parseFunctionSourceElements: true, parseVariableIdentifier: true,
240
+ parseLeftHandSideExpression: true,
241
+ parseStatement: true, parseSourceElement: true */
242
+
243
+ (function (root, factory) {
244
+
245
+ // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
246
+ // Rhino, and plain browser loading.
247
+ {
248
+ factory(exports);
249
+ }
250
+ }(commonjsGlobal, function (exports) {
251
+
252
+ var Token,
253
+ TokenName,
254
+ Syntax,
255
+ PropertyKind,
256
+ Messages,
257
+ Regex,
258
+ source,
259
+ strict,
260
+ index,
261
+ lineNumber,
262
+ lineStart,
263
+ length,
264
+ buffer,
265
+ state,
266
+ extra;
267
+
268
+ Token = {
269
+ BooleanLiteral: 1,
270
+ EOF: 2,
271
+ Identifier: 3,
272
+ Keyword: 4,
273
+ NullLiteral: 5,
274
+ NumericLiteral: 6,
275
+ Punctuator: 7,
276
+ StringLiteral: 8
277
+ };
278
+
279
+ TokenName = {};
280
+ TokenName[Token.BooleanLiteral] = 'Boolean';
281
+ TokenName[Token.EOF] = '<end>';
282
+ TokenName[Token.Identifier] = 'Identifier';
283
+ TokenName[Token.Keyword] = 'Keyword';
284
+ TokenName[Token.NullLiteral] = 'Null';
285
+ TokenName[Token.NumericLiteral] = 'Numeric';
286
+ TokenName[Token.Punctuator] = 'Punctuator';
287
+ TokenName[Token.StringLiteral] = 'String';
288
+
289
+ Syntax = {
290
+ AssignmentExpression: 'AssignmentExpression',
291
+ ArrayExpression: 'ArrayExpression',
292
+ BlockStatement: 'BlockStatement',
293
+ BinaryExpression: 'BinaryExpression',
294
+ BreakStatement: 'BreakStatement',
295
+ CallExpression: 'CallExpression',
296
+ CatchClause: 'CatchClause',
297
+ ConditionalExpression: 'ConditionalExpression',
298
+ ContinueStatement: 'ContinueStatement',
299
+ DoWhileStatement: 'DoWhileStatement',
300
+ DebuggerStatement: 'DebuggerStatement',
301
+ EmptyStatement: 'EmptyStatement',
302
+ ExpressionStatement: 'ExpressionStatement',
303
+ ForStatement: 'ForStatement',
304
+ ForInStatement: 'ForInStatement',
305
+ FunctionDeclaration: 'FunctionDeclaration',
306
+ FunctionExpression: 'FunctionExpression',
307
+ Identifier: 'Identifier',
308
+ IfStatement: 'IfStatement',
309
+ Literal: 'Literal',
310
+ LabeledStatement: 'LabeledStatement',
311
+ LogicalExpression: 'LogicalExpression',
312
+ MemberExpression: 'MemberExpression',
313
+ NewExpression: 'NewExpression',
314
+ ObjectExpression: 'ObjectExpression',
315
+ Program: 'Program',
316
+ Property: 'Property',
317
+ ReturnStatement: 'ReturnStatement',
318
+ SequenceExpression: 'SequenceExpression',
319
+ SwitchStatement: 'SwitchStatement',
320
+ SwitchCase: 'SwitchCase',
321
+ ThisExpression: 'ThisExpression',
322
+ ThrowStatement: 'ThrowStatement',
323
+ TryStatement: 'TryStatement',
324
+ UnaryExpression: 'UnaryExpression',
325
+ UpdateExpression: 'UpdateExpression',
326
+ VariableDeclaration: 'VariableDeclaration',
327
+ VariableDeclarator: 'VariableDeclarator',
328
+ WhileStatement: 'WhileStatement',
329
+ WithStatement: 'WithStatement'
330
+ };
331
+
332
+ PropertyKind = {
333
+ Data: 1,
334
+ Get: 2,
335
+ Set: 4
336
+ };
337
+
338
+ // Error messages should be identical to V8.
339
+ Messages = {
340
+ UnexpectedToken: 'Unexpected token %0',
341
+ UnexpectedNumber: 'Unexpected number',
342
+ UnexpectedString: 'Unexpected string',
343
+ UnexpectedIdentifier: 'Unexpected identifier',
344
+ UnexpectedReserved: 'Unexpected reserved word',
345
+ UnexpectedEOS: 'Unexpected end of input',
346
+ NewlineAfterThrow: 'Illegal newline after throw',
347
+ InvalidRegExp: 'Invalid regular expression',
348
+ UnterminatedRegExp: 'Invalid regular expression: missing /',
349
+ InvalidLHSInAssignment: 'Invalid left-hand side in assignment',
350
+ InvalidLHSInForIn: 'Invalid left-hand side in for-in',
351
+ MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
352
+ NoCatchOrFinally: 'Missing catch or finally after try',
353
+ UnknownLabel: 'Undefined label \'%0\'',
354
+ Redeclaration: '%0 \'%1\' has already been declared',
355
+ IllegalContinue: 'Illegal continue statement',
356
+ IllegalBreak: 'Illegal break statement',
357
+ IllegalReturn: 'Illegal return statement',
358
+ StrictModeWith: 'Strict mode code may not include a with statement',
359
+ StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
360
+ StrictVarName: 'Variable name may not be eval or arguments in strict mode',
361
+ StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
362
+ StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
363
+ StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
364
+ StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
365
+ StrictDelete: 'Delete of an unqualified identifier in strict mode.',
366
+ StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',
367
+ AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',
368
+ AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',
369
+ StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
370
+ StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
371
+ StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
372
+ StrictReservedWord: 'Use of future reserved word in strict mode'
373
+ };
374
+
375
+ // See also tools/generate-unicode-regex.py.
376
+ Regex = {
377
+ NonAsciiIdentifierStart: new RegExp('[\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc]'),
378
+ NonAsciiIdentifierPart: new RegExp('[\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0300-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u0483-\u0487\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u05d0-\u05ea\u05f0-\u05f2\u0610-\u061a\u0620-\u0669\u066e-\u06d3\u06d5-\u06dc\u06df-\u06e8\u06ea-\u06fc\u06ff\u0710-\u074a\u074d-\u07b1\u07c0-\u07f5\u07fa\u0800-\u082d\u0840-\u085b\u08a0\u08a2-\u08ac\u08e4-\u08fe\u0900-\u0963\u0966-\u096f\u0971-\u0977\u0979-\u097f\u0981-\u0983\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bc-\u09c4\u09c7\u09c8\u09cb-\u09ce\u09d7\u09dc\u09dd\u09df-\u09e3\u09e6-\u09f1\u0a01-\u0a03\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a59-\u0a5c\u0a5e\u0a66-\u0a75\u0a81-\u0a83\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abc-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ad0\u0ae0-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3c-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5c\u0b5d\u0b5f-\u0b63\u0b66-\u0b6f\u0b71\u0b82\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd0\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c58\u0c59\u0c60-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbc-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0cde\u0ce0-\u0ce3\u0ce6-\u0cef\u0cf1\u0cf2\u0d02\u0d03\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d-\u0d44\u0d46-\u0d48\u0d4a-\u0d4e\u0d57\u0d60-\u0d63\u0d66-\u0d6f\u0d7a-\u0d7f\u0d82\u0d83\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e01-\u0e3a\u0e40-\u0e4e\u0e50-\u0e59\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb9\u0ebb-\u0ebd\u0ec0-\u0ec4\u0ec6\u0ec8-\u0ecd\u0ed0-\u0ed9\u0edc-\u0edf\u0f00\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e-\u0f47\u0f49-\u0f6c\u0f71-\u0f84\u0f86-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1049\u1050-\u109d\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\u135d-\u135f\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176c\u176e-\u1770\u1772\u1773\u1780-\u17d3\u17d7\u17dc\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1820-\u1877\u1880-\u18aa\u18b0-\u18f5\u1900-\u191c\u1920-\u192b\u1930-\u193b\u1946-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u19d0-\u19d9\u1a00-\u1a1b\u1a20-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1aa7\u1b00-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1bf3\u1c00-\u1c37\u1c40-\u1c49\u1c4d-\u1c7d\u1cd0-\u1cd2\u1cd4-\u1cf6\u1d00-\u1de6\u1dfc-\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\u200c\u200d\u203f\u2040\u2054\u2071\u207f\u2090-\u209c\u20d0-\u20dc\u20e1\u20e5-\u20f0\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-\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d7f-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2de0-\u2dff\u2e2f\u3005-\u3007\u3021-\u302f\u3031-\u3035\u3038-\u303c\u3041-\u3096\u3099\u309a\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-\ua62b\ua640-\ua66f\ua674-\ua67d\ua67f-\ua697\ua69f-\ua6f1\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua827\ua840-\ua873\ua880-\ua8c4\ua8d0-\ua8d9\ua8e0-\ua8f7\ua8fb\ua900-\ua92d\ua930-\ua953\ua960-\ua97c\ua980-\ua9c0\ua9cf-\ua9d9\uaa00-\uaa36\uaa40-\uaa4d\uaa50-\uaa59\uaa60-\uaa76\uaa7a\uaa7b\uaa80-\uaac2\uaadb-\uaadd\uaae0-\uaaef\uaaf2-\uaaf6\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabea\uabec\uabed\uabf0-\uabf9\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\ufe70-\ufe74\ufe76-\ufefc\uff10-\uff19\uff21-\uff3a\uff3f\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc]')
379
+ };
380
+
381
+ // Ensure the condition is true, otherwise throw an error.
382
+ // This is only to have a better contract semantic, i.e. another safety net
383
+ // to catch a logic error. The condition shall be fulfilled in normal case.
384
+ // Do NOT use this to enforce a certain condition on any user input.
385
+
386
+ function assert(condition, message) {
387
+ if (!condition) {
388
+ throw new Error('ASSERT: ' + message);
389
+ }
390
+ }
391
+
392
+ function sliceSource(from, to) {
393
+ return source.slice(from, to);
394
+ }
395
+
396
+ if (typeof 'esprima'[0] === 'undefined') {
397
+ sliceSource = function sliceArraySource(from, to) {
398
+ return source.slice(from, to).join('');
399
+ };
400
+ }
401
+
402
+ function isDecimalDigit(ch) {
403
+ return '0123456789'.indexOf(ch) >= 0;
404
+ }
405
+
406
+ function isHexDigit(ch) {
407
+ return '0123456789abcdefABCDEF'.indexOf(ch) >= 0;
408
+ }
409
+
410
+ function isOctalDigit(ch) {
411
+ return '01234567'.indexOf(ch) >= 0;
412
+ }
413
+
414
+
415
+ // 7.2 White Space
416
+
417
+ function isWhiteSpace(ch) {
418
+ return (ch === ' ') || (ch === '\u0009') || (ch === '\u000B') ||
419
+ (ch === '\u000C') || (ch === '\u00A0') ||
420
+ (ch.charCodeAt(0) >= 0x1680 &&
421
+ '\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\uFEFF'.indexOf(ch) >= 0);
422
+ }
423
+
424
+ // 7.3 Line Terminators
425
+
426
+ function isLineTerminator(ch) {
427
+ return (ch === '\n' || ch === '\r' || ch === '\u2028' || ch === '\u2029');
428
+ }
429
+
430
+ // 7.6 Identifier Names and Identifiers
431
+
432
+ function isIdentifierStart(ch) {
433
+ return (ch === '$') || (ch === '_') || (ch === '\\') ||
434
+ (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
435
+ ((ch.charCodeAt(0) >= 0x80) && Regex.NonAsciiIdentifierStart.test(ch));
436
+ }
437
+
438
+ function isIdentifierPart(ch) {
439
+ return (ch === '$') || (ch === '_') || (ch === '\\') ||
440
+ (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
441
+ ((ch >= '0') && (ch <= '9')) ||
442
+ ((ch.charCodeAt(0) >= 0x80) && Regex.NonAsciiIdentifierPart.test(ch));
443
+ }
444
+
445
+ // 7.6.1.2 Future Reserved Words
446
+
447
+ function isFutureReservedWord(id) {
448
+ switch (id) {
449
+
450
+ // Future reserved words.
451
+ case 'class':
452
+ case 'enum':
453
+ case 'export':
454
+ case 'extends':
455
+ case 'import':
456
+ case 'super':
457
+ return true;
458
+ }
459
+
460
+ return false;
461
+ }
462
+
463
+ function isStrictModeReservedWord(id) {
464
+ switch (id) {
465
+
466
+ // Strict Mode reserved words.
467
+ case 'implements':
468
+ case 'interface':
469
+ case 'package':
470
+ case 'private':
471
+ case 'protected':
472
+ case 'public':
473
+ case 'static':
474
+ case 'yield':
475
+ case 'let':
476
+ return true;
477
+ }
478
+
479
+ return false;
480
+ }
481
+
482
+ function isRestrictedWord(id) {
483
+ return id === 'eval' || id === 'arguments';
484
+ }
485
+
486
+ // 7.6.1.1 Keywords
487
+
488
+ function isKeyword(id) {
489
+ var keyword = false;
490
+ switch (id.length) {
491
+ case 2:
492
+ keyword = (id === 'if') || (id === 'in') || (id === 'do');
493
+ break;
494
+ case 3:
495
+ keyword = (id === 'var') || (id === 'for') || (id === 'new') || (id === 'try');
496
+ break;
497
+ case 4:
498
+ keyword = (id === 'this') || (id === 'else') || (id === 'case') || (id === 'void') || (id === 'with');
499
+ break;
500
+ case 5:
501
+ keyword = (id === 'while') || (id === 'break') || (id === 'catch') || (id === 'throw');
502
+ break;
503
+ case 6:
504
+ keyword = (id === 'return') || (id === 'typeof') || (id === 'delete') || (id === 'switch');
505
+ break;
506
+ case 7:
507
+ keyword = (id === 'default') || (id === 'finally');
508
+ break;
509
+ case 8:
510
+ keyword = (id === 'function') || (id === 'continue') || (id === 'debugger');
511
+ break;
512
+ case 10:
513
+ keyword = (id === 'instanceof');
514
+ break;
515
+ }
516
+
517
+ if (keyword) {
518
+ return true;
519
+ }
520
+
521
+ switch (id) {
522
+ // Future reserved words.
523
+ // 'const' is specialized as Keyword in V8.
524
+ case 'const':
525
+ return true;
526
+
527
+ // For compatiblity to SpiderMonkey and ES.next
528
+ case 'yield':
529
+ case 'let':
530
+ return true;
531
+ }
532
+
533
+ if (strict && isStrictModeReservedWord(id)) {
534
+ return true;
535
+ }
536
+
537
+ return isFutureReservedWord(id);
538
+ }
539
+
540
+ // 7.4 Comments
541
+
542
+ function skipComment() {
543
+ var ch, blockComment, lineComment;
544
+
545
+ blockComment = false;
546
+ lineComment = false;
547
+
548
+ while (index < length) {
549
+ ch = source[index];
550
+
551
+ if (lineComment) {
552
+ ch = source[index++];
553
+ if (isLineTerminator(ch)) {
554
+ lineComment = false;
555
+ if (ch === '\r' && source[index] === '\n') {
556
+ ++index;
557
+ }
558
+ ++lineNumber;
559
+ lineStart = index;
560
+ }
561
+ } else if (blockComment) {
562
+ if (isLineTerminator(ch)) {
563
+ if (ch === '\r' && source[index + 1] === '\n') {
564
+ ++index;
565
+ }
566
+ ++lineNumber;
567
+ ++index;
568
+ lineStart = index;
569
+ if (index >= length) {
570
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
571
+ }
572
+ } else {
573
+ ch = source[index++];
574
+ if (index >= length) {
575
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
576
+ }
577
+ if (ch === '*') {
578
+ ch = source[index];
579
+ if (ch === '/') {
580
+ ++index;
581
+ blockComment = false;
582
+ }
583
+ }
584
+ }
585
+ } else if (ch === '/') {
586
+ ch = source[index + 1];
587
+ if (ch === '/') {
588
+ index += 2;
589
+ lineComment = true;
590
+ } else if (ch === '*') {
591
+ index += 2;
592
+ blockComment = true;
593
+ if (index >= length) {
594
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
595
+ }
596
+ } else {
597
+ break;
598
+ }
599
+ } else if (isWhiteSpace(ch)) {
600
+ ++index;
601
+ } else if (isLineTerminator(ch)) {
602
+ ++index;
603
+ if (ch === '\r' && source[index] === '\n') {
604
+ ++index;
605
+ }
606
+ ++lineNumber;
607
+ lineStart = index;
608
+ } else {
609
+ break;
610
+ }
611
+ }
612
+ }
613
+
614
+ function scanHexEscape(prefix) {
615
+ var i, len, ch, code = 0;
616
+
617
+ len = (prefix === 'u') ? 4 : 2;
618
+ for (i = 0; i < len; ++i) {
619
+ if (index < length && isHexDigit(source[index])) {
620
+ ch = source[index++];
621
+ code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
622
+ } else {
623
+ return '';
624
+ }
625
+ }
626
+ return String.fromCharCode(code);
627
+ }
628
+
629
+ function scanIdentifier() {
630
+ var ch, start, id, restore;
631
+
632
+ ch = source[index];
633
+ if (!isIdentifierStart(ch)) {
634
+ return;
635
+ }
636
+
637
+ start = index;
638
+ if (ch === '\\') {
639
+ ++index;
640
+ if (source[index] !== 'u') {
641
+ return;
642
+ }
643
+ ++index;
644
+ restore = index;
645
+ ch = scanHexEscape('u');
646
+ if (ch) {
647
+ if (ch === '\\' || !isIdentifierStart(ch)) {
648
+ return;
649
+ }
650
+ id = ch;
651
+ } else {
652
+ index = restore;
653
+ id = 'u';
654
+ }
655
+ } else {
656
+ id = source[index++];
657
+ }
658
+
659
+ while (index < length) {
660
+ ch = source[index];
661
+ if (!isIdentifierPart(ch)) {
662
+ break;
663
+ }
664
+ if (ch === '\\') {
665
+ ++index;
666
+ if (source[index] !== 'u') {
667
+ return;
668
+ }
669
+ ++index;
670
+ restore = index;
671
+ ch = scanHexEscape('u');
672
+ if (ch) {
673
+ if (ch === '\\' || !isIdentifierPart(ch)) {
674
+ return;
675
+ }
676
+ id += ch;
677
+ } else {
678
+ index = restore;
679
+ id += 'u';
680
+ }
681
+ } else {
682
+ id += source[index++];
683
+ }
684
+ }
685
+
686
+ // There is no keyword or literal with only one character.
687
+ // Thus, it must be an identifier.
688
+ if (id.length === 1) {
689
+ return {
690
+ type: Token.Identifier,
691
+ value: id,
692
+ lineNumber: lineNumber,
693
+ lineStart: lineStart,
694
+ range: [start, index]
695
+ };
696
+ }
697
+
698
+ if (isKeyword(id)) {
699
+ return {
700
+ type: Token.Keyword,
701
+ value: id,
702
+ lineNumber: lineNumber,
703
+ lineStart: lineStart,
704
+ range: [start, index]
705
+ };
706
+ }
707
+
708
+ // 7.8.1 Null Literals
709
+
710
+ if (id === 'null') {
711
+ return {
712
+ type: Token.NullLiteral,
713
+ value: id,
714
+ lineNumber: lineNumber,
715
+ lineStart: lineStart,
716
+ range: [start, index]
717
+ };
718
+ }
719
+
720
+ // 7.8.2 Boolean Literals
721
+
722
+ if (id === 'true' || id === 'false') {
723
+ return {
724
+ type: Token.BooleanLiteral,
725
+ value: id,
726
+ lineNumber: lineNumber,
727
+ lineStart: lineStart,
728
+ range: [start, index]
729
+ };
730
+ }
731
+
732
+ return {
733
+ type: Token.Identifier,
734
+ value: id,
735
+ lineNumber: lineNumber,
736
+ lineStart: lineStart,
737
+ range: [start, index]
738
+ };
739
+ }
740
+
741
+ // 7.7 Punctuators
742
+
743
+ function scanPunctuator() {
744
+ var start = index,
745
+ ch1 = source[index],
746
+ ch2,
747
+ ch3,
748
+ ch4;
749
+
750
+ // Check for most common single-character punctuators.
751
+
752
+ if (ch1 === ';' || ch1 === '{' || ch1 === '}') {
753
+ ++index;
754
+ return {
755
+ type: Token.Punctuator,
756
+ value: ch1,
757
+ lineNumber: lineNumber,
758
+ lineStart: lineStart,
759
+ range: [start, index]
760
+ };
761
+ }
762
+
763
+ if (ch1 === ',' || ch1 === '(' || ch1 === ')') {
764
+ ++index;
765
+ return {
766
+ type: Token.Punctuator,
767
+ value: ch1,
768
+ lineNumber: lineNumber,
769
+ lineStart: lineStart,
770
+ range: [start, index]
771
+ };
772
+ }
773
+
774
+ // Dot (.) can also start a floating-point number, hence the need
775
+ // to check the next character.
776
+
777
+ ch2 = source[index + 1];
778
+ if (ch1 === '.' && !isDecimalDigit(ch2)) {
779
+ return {
780
+ type: Token.Punctuator,
781
+ value: source[index++],
782
+ lineNumber: lineNumber,
783
+ lineStart: lineStart,
784
+ range: [start, index]
785
+ };
786
+ }
787
+
788
+ // Peek more characters.
789
+
790
+ ch3 = source[index + 2];
791
+ ch4 = source[index + 3];
792
+
793
+ // 4-character punctuator: >>>=
794
+
795
+ if (ch1 === '>' && ch2 === '>' && ch3 === '>') {
796
+ if (ch4 === '=') {
797
+ index += 4;
798
+ return {
799
+ type: Token.Punctuator,
800
+ value: '>>>=',
801
+ lineNumber: lineNumber,
802
+ lineStart: lineStart,
803
+ range: [start, index]
804
+ };
805
+ }
806
+ }
807
+
808
+ // 3-character punctuators: === !== >>> <<= >>=
809
+
810
+ if (ch1 === '=' && ch2 === '=' && ch3 === '=') {
811
+ index += 3;
812
+ return {
813
+ type: Token.Punctuator,
814
+ value: '===',
815
+ lineNumber: lineNumber,
816
+ lineStart: lineStart,
817
+ range: [start, index]
818
+ };
819
+ }
820
+
821
+ if (ch1 === '!' && ch2 === '=' && ch3 === '=') {
822
+ index += 3;
823
+ return {
824
+ type: Token.Punctuator,
825
+ value: '!==',
826
+ lineNumber: lineNumber,
827
+ lineStart: lineStart,
828
+ range: [start, index]
829
+ };
830
+ }
831
+
832
+ if (ch1 === '>' && ch2 === '>' && ch3 === '>') {
833
+ index += 3;
834
+ return {
835
+ type: Token.Punctuator,
836
+ value: '>>>',
837
+ lineNumber: lineNumber,
838
+ lineStart: lineStart,
839
+ range: [start, index]
840
+ };
841
+ }
842
+
843
+ if (ch1 === '<' && ch2 === '<' && ch3 === '=') {
844
+ index += 3;
845
+ return {
846
+ type: Token.Punctuator,
847
+ value: '<<=',
848
+ lineNumber: lineNumber,
849
+ lineStart: lineStart,
850
+ range: [start, index]
851
+ };
852
+ }
853
+
854
+ if (ch1 === '>' && ch2 === '>' && ch3 === '=') {
855
+ index += 3;
856
+ return {
857
+ type: Token.Punctuator,
858
+ value: '>>=',
859
+ lineNumber: lineNumber,
860
+ lineStart: lineStart,
861
+ range: [start, index]
862
+ };
863
+ }
864
+
865
+ // 2-character punctuators: <= >= == != ++ -- << >> && ||
866
+ // += -= *= %= &= |= ^= /=
867
+
868
+ if (ch2 === '=') {
869
+ if ('<>=!+-*%&|^/'.indexOf(ch1) >= 0) {
870
+ index += 2;
871
+ return {
872
+ type: Token.Punctuator,
873
+ value: ch1 + ch2,
874
+ lineNumber: lineNumber,
875
+ lineStart: lineStart,
876
+ range: [start, index]
877
+ };
878
+ }
879
+ }
880
+
881
+ if (ch1 === ch2 && ('+-<>&|'.indexOf(ch1) >= 0)) {
882
+ if ('+-<>&|'.indexOf(ch2) >= 0) {
883
+ index += 2;
884
+ return {
885
+ type: Token.Punctuator,
886
+ value: ch1 + ch2,
887
+ lineNumber: lineNumber,
888
+ lineStart: lineStart,
889
+ range: [start, index]
890
+ };
891
+ }
892
+ }
893
+
894
+ // The remaining 1-character punctuators.
895
+
896
+ if ('[]<>+-*%&|^!~?:=/'.indexOf(ch1) >= 0) {
897
+ return {
898
+ type: Token.Punctuator,
899
+ value: source[index++],
900
+ lineNumber: lineNumber,
901
+ lineStart: lineStart,
902
+ range: [start, index]
903
+ };
904
+ }
905
+ }
906
+
907
+ // 7.8.3 Numeric Literals
908
+
909
+ function scanNumericLiteral() {
910
+ var number, start, ch;
911
+
912
+ ch = source[index];
913
+ assert(isDecimalDigit(ch) || (ch === '.'),
914
+ 'Numeric literal must start with a decimal digit or a decimal point');
915
+
916
+ start = index;
917
+ number = '';
918
+ if (ch !== '.') {
919
+ number = source[index++];
920
+ ch = source[index];
921
+
922
+ // Hex number starts with '0x'.
923
+ // Octal number starts with '0'.
924
+ if (number === '0') {
925
+ if (ch === 'x' || ch === 'X') {
926
+ number += source[index++];
927
+ while (index < length) {
928
+ ch = source[index];
929
+ if (!isHexDigit(ch)) {
930
+ break;
931
+ }
932
+ number += source[index++];
933
+ }
934
+
935
+ if (number.length <= 2) {
936
+ // only 0x
937
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
938
+ }
939
+
940
+ if (index < length) {
941
+ ch = source[index];
942
+ if (isIdentifierStart(ch)) {
943
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
944
+ }
945
+ }
946
+ return {
947
+ type: Token.NumericLiteral,
948
+ value: parseInt(number, 16),
949
+ lineNumber: lineNumber,
950
+ lineStart: lineStart,
951
+ range: [start, index]
952
+ };
953
+ } else if (isOctalDigit(ch)) {
954
+ number += source[index++];
955
+ while (index < length) {
956
+ ch = source[index];
957
+ if (!isOctalDigit(ch)) {
958
+ break;
959
+ }
960
+ number += source[index++];
961
+ }
962
+
963
+ if (index < length) {
964
+ ch = source[index];
965
+ if (isIdentifierStart(ch) || isDecimalDigit(ch)) {
966
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
967
+ }
968
+ }
969
+ return {
970
+ type: Token.NumericLiteral,
971
+ value: parseInt(number, 8),
972
+ octal: true,
973
+ lineNumber: lineNumber,
974
+ lineStart: lineStart,
975
+ range: [start, index]
976
+ };
977
+ }
978
+
979
+ // decimal number starts with '0' such as '09' is illegal.
980
+ if (isDecimalDigit(ch)) {
981
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
982
+ }
983
+ }
984
+
985
+ while (index < length) {
986
+ ch = source[index];
987
+ if (!isDecimalDigit(ch)) {
988
+ break;
989
+ }
990
+ number += source[index++];
991
+ }
992
+ }
993
+
994
+ if (ch === '.') {
995
+ number += source[index++];
996
+ while (index < length) {
997
+ ch = source[index];
998
+ if (!isDecimalDigit(ch)) {
999
+ break;
1000
+ }
1001
+ number += source[index++];
1002
+ }
1003
+ }
1004
+
1005
+ if (ch === 'e' || ch === 'E') {
1006
+ number += source[index++];
1007
+
1008
+ ch = source[index];
1009
+ if (ch === '+' || ch === '-') {
1010
+ number += source[index++];
1011
+ }
1012
+
1013
+ ch = source[index];
1014
+ if (isDecimalDigit(ch)) {
1015
+ number += source[index++];
1016
+ while (index < length) {
1017
+ ch = source[index];
1018
+ if (!isDecimalDigit(ch)) {
1019
+ break;
1020
+ }
1021
+ number += source[index++];
1022
+ }
1023
+ } else {
1024
+ ch = 'character ' + ch;
1025
+ if (index >= length) {
1026
+ ch = '<end>';
1027
+ }
1028
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
1029
+ }
1030
+ }
1031
+
1032
+ if (index < length) {
1033
+ ch = source[index];
1034
+ if (isIdentifierStart(ch)) {
1035
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
1036
+ }
1037
+ }
1038
+
1039
+ return {
1040
+ type: Token.NumericLiteral,
1041
+ value: parseFloat(number),
1042
+ lineNumber: lineNumber,
1043
+ lineStart: lineStart,
1044
+ range: [start, index]
1045
+ };
1046
+ }
1047
+
1048
+ // 7.8.4 String Literals
1049
+
1050
+ function scanStringLiteral() {
1051
+ var str = '', quote, start, ch, code, unescaped, restore, octal = false;
1052
+
1053
+ quote = source[index];
1054
+ assert((quote === '\'' || quote === '"'),
1055
+ 'String literal must starts with a quote');
1056
+
1057
+ start = index;
1058
+ ++index;
1059
+
1060
+ while (index < length) {
1061
+ ch = source[index++];
1062
+
1063
+ if (ch === quote) {
1064
+ quote = '';
1065
+ break;
1066
+ } else if (ch === '\\') {
1067
+ ch = source[index++];
1068
+ if (!isLineTerminator(ch)) {
1069
+ switch (ch) {
1070
+ case 'n':
1071
+ str += '\n';
1072
+ break;
1073
+ case 'r':
1074
+ str += '\r';
1075
+ break;
1076
+ case 't':
1077
+ str += '\t';
1078
+ break;
1079
+ case 'u':
1080
+ case 'x':
1081
+ restore = index;
1082
+ unescaped = scanHexEscape(ch);
1083
+ if (unescaped) {
1084
+ str += unescaped;
1085
+ } else {
1086
+ index = restore;
1087
+ str += ch;
1088
+ }
1089
+ break;
1090
+ case 'b':
1091
+ str += '\b';
1092
+ break;
1093
+ case 'f':
1094
+ str += '\f';
1095
+ break;
1096
+ case 'v':
1097
+ str += '\x0B';
1098
+ break;
1099
+
1100
+ default:
1101
+ if (isOctalDigit(ch)) {
1102
+ code = '01234567'.indexOf(ch);
1103
+
1104
+ // \0 is not octal escape sequence
1105
+ if (code !== 0) {
1106
+ octal = true;
1107
+ }
1108
+
1109
+ if (index < length && isOctalDigit(source[index])) {
1110
+ octal = true;
1111
+ code = code * 8 + '01234567'.indexOf(source[index++]);
1112
+
1113
+ // 3 digits are only allowed when string starts
1114
+ // with 0, 1, 2, 3
1115
+ if ('0123'.indexOf(ch) >= 0 &&
1116
+ index < length &&
1117
+ isOctalDigit(source[index])) {
1118
+ code = code * 8 + '01234567'.indexOf(source[index++]);
1119
+ }
1120
+ }
1121
+ str += String.fromCharCode(code);
1122
+ } else {
1123
+ str += ch;
1124
+ }
1125
+ break;
1126
+ }
1127
+ } else {
1128
+ ++lineNumber;
1129
+ if (ch === '\r' && source[index] === '\n') {
1130
+ ++index;
1131
+ }
1132
+ }
1133
+ } else if (isLineTerminator(ch)) {
1134
+ break;
1135
+ } else {
1136
+ str += ch;
1137
+ }
1138
+ }
1139
+
1140
+ if (quote !== '') {
1141
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
1142
+ }
1143
+
1144
+ return {
1145
+ type: Token.StringLiteral,
1146
+ value: str,
1147
+ octal: octal,
1148
+ lineNumber: lineNumber,
1149
+ lineStart: lineStart,
1150
+ range: [start, index]
1151
+ };
1152
+ }
1153
+
1154
+ function scanRegExp() {
1155
+ var str, ch, start, pattern, flags, value, classMarker = false, restore, terminated = false;
1156
+
1157
+ buffer = null;
1158
+ skipComment();
1159
+
1160
+ start = index;
1161
+ ch = source[index];
1162
+ assert(ch === '/', 'Regular expression literal must start with a slash');
1163
+ str = source[index++];
1164
+
1165
+ while (index < length) {
1166
+ ch = source[index++];
1167
+ str += ch;
1168
+ if (ch === '\\') {
1169
+ ch = source[index++];
1170
+ // ECMA-262 7.8.5
1171
+ if (isLineTerminator(ch)) {
1172
+ throwError({}, Messages.UnterminatedRegExp);
1173
+ }
1174
+ str += ch;
1175
+ } else if (classMarker) {
1176
+ if (ch === ']') {
1177
+ classMarker = false;
1178
+ }
1179
+ } else {
1180
+ if (ch === '/') {
1181
+ terminated = true;
1182
+ break;
1183
+ } else if (ch === '[') {
1184
+ classMarker = true;
1185
+ } else if (isLineTerminator(ch)) {
1186
+ throwError({}, Messages.UnterminatedRegExp);
1187
+ }
1188
+ }
1189
+ }
1190
+
1191
+ if (!terminated) {
1192
+ throwError({}, Messages.UnterminatedRegExp);
1193
+ }
1194
+
1195
+ // Exclude leading and trailing slash.
1196
+ pattern = str.substr(1, str.length - 2);
1197
+
1198
+ flags = '';
1199
+ while (index < length) {
1200
+ ch = source[index];
1201
+ if (!isIdentifierPart(ch)) {
1202
+ break;
1203
+ }
1204
+
1205
+ ++index;
1206
+ if (ch === '\\' && index < length) {
1207
+ ch = source[index];
1208
+ if (ch === 'u') {
1209
+ ++index;
1210
+ restore = index;
1211
+ ch = scanHexEscape('u');
1212
+ if (ch) {
1213
+ flags += ch;
1214
+ str += '\\u';
1215
+ for (; restore < index; ++restore) {
1216
+ str += source[restore];
1217
+ }
1218
+ } else {
1219
+ index = restore;
1220
+ flags += 'u';
1221
+ str += '\\u';
1222
+ }
1223
+ } else {
1224
+ str += '\\';
1225
+ }
1226
+ } else {
1227
+ flags += ch;
1228
+ str += ch;
1229
+ }
1230
+ }
1231
+
1232
+ try {
1233
+ value = new RegExp(pattern, flags);
1234
+ } catch (e) {
1235
+ throwError({}, Messages.InvalidRegExp);
1236
+ }
1237
+
1238
+ return {
1239
+ literal: str,
1240
+ value: value,
1241
+ range: [start, index]
1242
+ };
1243
+ }
1244
+
1245
+ function isIdentifierName(token) {
1246
+ return token.type === Token.Identifier ||
1247
+ token.type === Token.Keyword ||
1248
+ token.type === Token.BooleanLiteral ||
1249
+ token.type === Token.NullLiteral;
1250
+ }
1251
+
1252
+ function advance() {
1253
+ var ch, token;
1254
+
1255
+ skipComment();
1256
+
1257
+ if (index >= length) {
1258
+ return {
1259
+ type: Token.EOF,
1260
+ lineNumber: lineNumber,
1261
+ lineStart: lineStart,
1262
+ range: [index, index]
1263
+ };
1264
+ }
1265
+
1266
+ token = scanPunctuator();
1267
+ if (typeof token !== 'undefined') {
1268
+ return token;
1269
+ }
1270
+
1271
+ ch = source[index];
1272
+
1273
+ if (ch === '\'' || ch === '"') {
1274
+ return scanStringLiteral();
1275
+ }
1276
+
1277
+ if (ch === '.' || isDecimalDigit(ch)) {
1278
+ return scanNumericLiteral();
1279
+ }
1280
+
1281
+ token = scanIdentifier();
1282
+ if (typeof token !== 'undefined') {
1283
+ return token;
1284
+ }
1285
+
1286
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
1287
+ }
1288
+
1289
+ function lex() {
1290
+ var token;
1291
+
1292
+ if (buffer) {
1293
+ index = buffer.range[1];
1294
+ lineNumber = buffer.lineNumber;
1295
+ lineStart = buffer.lineStart;
1296
+ token = buffer;
1297
+ buffer = null;
1298
+ return token;
1299
+ }
1300
+
1301
+ buffer = null;
1302
+ return advance();
1303
+ }
1304
+
1305
+ function lookahead() {
1306
+ var pos, line, start;
1307
+
1308
+ if (buffer !== null) {
1309
+ return buffer;
1310
+ }
1311
+
1312
+ pos = index;
1313
+ line = lineNumber;
1314
+ start = lineStart;
1315
+ buffer = advance();
1316
+ index = pos;
1317
+ lineNumber = line;
1318
+ lineStart = start;
1319
+
1320
+ return buffer;
1321
+ }
1322
+
1323
+ // Return true if there is a line terminator before the next token.
1324
+
1325
+ function peekLineTerminator() {
1326
+ var pos, line, start, found;
1327
+
1328
+ pos = index;
1329
+ line = lineNumber;
1330
+ start = lineStart;
1331
+ skipComment();
1332
+ found = lineNumber !== line;
1333
+ index = pos;
1334
+ lineNumber = line;
1335
+ lineStart = start;
1336
+
1337
+ return found;
1338
+ }
1339
+
1340
+ // Throw an exception
1341
+
1342
+ function throwError(token, messageFormat) {
1343
+ var error,
1344
+ args = Array.prototype.slice.call(arguments, 2),
1345
+ msg = messageFormat.replace(
1346
+ /%(\d)/g,
1347
+ function (whole, index) {
1348
+ return args[index] || '';
1349
+ }
1350
+ );
1351
+
1352
+ if (typeof token.lineNumber === 'number') {
1353
+ error = new Error('Line ' + token.lineNumber + ': ' + msg);
1354
+ error.index = token.range[0];
1355
+ error.lineNumber = token.lineNumber;
1356
+ error.column = token.range[0] - lineStart + 1;
1357
+ } else {
1358
+ error = new Error('Line ' + lineNumber + ': ' + msg);
1359
+ error.index = index;
1360
+ error.lineNumber = lineNumber;
1361
+ error.column = index - lineStart + 1;
1362
+ }
1363
+
1364
+ throw error;
1365
+ }
1366
+
1367
+ function throwErrorTolerant() {
1368
+ try {
1369
+ throwError.apply(null, arguments);
1370
+ } catch (e) {
1371
+ if (extra.errors) {
1372
+ extra.errors.push(e);
1373
+ } else {
1374
+ throw e;
1375
+ }
1376
+ }
1377
+ }
1378
+
1379
+
1380
+ // Throw an exception because of the token.
1381
+
1382
+ function throwUnexpected(token) {
1383
+ if (token.type === Token.EOF) {
1384
+ throwError(token, Messages.UnexpectedEOS);
1385
+ }
1386
+
1387
+ if (token.type === Token.NumericLiteral) {
1388
+ throwError(token, Messages.UnexpectedNumber);
1389
+ }
1390
+
1391
+ if (token.type === Token.StringLiteral) {
1392
+ throwError(token, Messages.UnexpectedString);
1393
+ }
1394
+
1395
+ if (token.type === Token.Identifier) {
1396
+ throwError(token, Messages.UnexpectedIdentifier);
1397
+ }
1398
+
1399
+ if (token.type === Token.Keyword) {
1400
+ if (isFutureReservedWord(token.value)) {
1401
+ throwError(token, Messages.UnexpectedReserved);
1402
+ } else if (strict && isStrictModeReservedWord(token.value)) {
1403
+ throwErrorTolerant(token, Messages.StrictReservedWord);
1404
+ return;
1405
+ }
1406
+ throwError(token, Messages.UnexpectedToken, token.value);
1407
+ }
1408
+
1409
+ // BooleanLiteral, NullLiteral, or Punctuator.
1410
+ throwError(token, Messages.UnexpectedToken, token.value);
1411
+ }
1412
+
1413
+ // Expect the next token to match the specified punctuator.
1414
+ // If not, an exception will be thrown.
1415
+
1416
+ function expect(value) {
1417
+ var token = lex();
1418
+ if (token.type !== Token.Punctuator || token.value !== value) {
1419
+ throwUnexpected(token);
1420
+ }
1421
+ }
1422
+
1423
+ // Expect the next token to match the specified keyword.
1424
+ // If not, an exception will be thrown.
1425
+
1426
+ function expectKeyword(keyword) {
1427
+ var token = lex();
1428
+ if (token.type !== Token.Keyword || token.value !== keyword) {
1429
+ throwUnexpected(token);
1430
+ }
1431
+ }
1432
+
1433
+ // Return true if the next token matches the specified punctuator.
1434
+
1435
+ function match(value) {
1436
+ var token = lookahead();
1437
+ return token.type === Token.Punctuator && token.value === value;
1438
+ }
1439
+
1440
+ // Return true if the next token matches the specified keyword
1441
+
1442
+ function matchKeyword(keyword) {
1443
+ var token = lookahead();
1444
+ return token.type === Token.Keyword && token.value === keyword;
1445
+ }
1446
+
1447
+ // Return true if the next token is an assignment operator
1448
+
1449
+ function matchAssign() {
1450
+ var token = lookahead(),
1451
+ op = token.value;
1452
+
1453
+ if (token.type !== Token.Punctuator) {
1454
+ return false;
1455
+ }
1456
+ return op === '=' ||
1457
+ op === '*=' ||
1458
+ op === '/=' ||
1459
+ op === '%=' ||
1460
+ op === '+=' ||
1461
+ op === '-=' ||
1462
+ op === '<<=' ||
1463
+ op === '>>=' ||
1464
+ op === '>>>=' ||
1465
+ op === '&=' ||
1466
+ op === '^=' ||
1467
+ op === '|=';
1468
+ }
1469
+
1470
+ function consumeSemicolon() {
1471
+ var token, line;
1472
+
1473
+ // Catch the very common case first.
1474
+ if (source[index] === ';') {
1475
+ lex();
1476
+ return;
1477
+ }
1478
+
1479
+ line = lineNumber;
1480
+ skipComment();
1481
+ if (lineNumber !== line) {
1482
+ return;
1483
+ }
1484
+
1485
+ if (match(';')) {
1486
+ lex();
1487
+ return;
1488
+ }
1489
+
1490
+ token = lookahead();
1491
+ if (token.type !== Token.EOF && !match('}')) {
1492
+ throwUnexpected(token);
1493
+ }
1494
+ }
1495
+
1496
+ // Return true if provided expression is LeftHandSideExpression
1497
+
1498
+ function isLeftHandSide(expr) {
1499
+ return expr.type === Syntax.Identifier || expr.type === Syntax.MemberExpression;
1500
+ }
1501
+
1502
+ // 11.1.4 Array Initialiser
1503
+
1504
+ function parseArrayInitialiser() {
1505
+ var elements = [];
1506
+
1507
+ expect('[');
1508
+
1509
+ while (!match(']')) {
1510
+ if (match(',')) {
1511
+ lex();
1512
+ elements.push(null);
1513
+ } else {
1514
+ elements.push(parseAssignmentExpression());
1515
+
1516
+ if (!match(']')) {
1517
+ expect(',');
1518
+ }
1519
+ }
1520
+ }
1521
+
1522
+ expect(']');
1523
+
1524
+ return {
1525
+ type: Syntax.ArrayExpression,
1526
+ elements: elements
1527
+ };
1528
+ }
1529
+
1530
+ // 11.1.5 Object Initialiser
1531
+
1532
+ function parsePropertyFunction(param, first) {
1533
+ var previousStrict, body;
1534
+
1535
+ previousStrict = strict;
1536
+ body = parseFunctionSourceElements();
1537
+ if (first && strict && isRestrictedWord(param[0].name)) {
1538
+ throwErrorTolerant(first, Messages.StrictParamName);
1539
+ }
1540
+ strict = previousStrict;
1541
+
1542
+ return {
1543
+ type: Syntax.FunctionExpression,
1544
+ id: null,
1545
+ params: param,
1546
+ defaults: [],
1547
+ body: body,
1548
+ rest: null,
1549
+ generator: false,
1550
+ expression: false
1551
+ };
1552
+ }
1553
+
1554
+ function parseObjectPropertyKey() {
1555
+ var token = lex();
1556
+
1557
+ // Note: This function is called only from parseObjectProperty(), where
1558
+ // EOF and Punctuator tokens are already filtered out.
1559
+
1560
+ if (token.type === Token.StringLiteral || token.type === Token.NumericLiteral) {
1561
+ if (strict && token.octal) {
1562
+ throwErrorTolerant(token, Messages.StrictOctalLiteral);
1563
+ }
1564
+ return createLiteral(token);
1565
+ }
1566
+
1567
+ return {
1568
+ type: Syntax.Identifier,
1569
+ name: token.value
1570
+ };
1571
+ }
1572
+
1573
+ function parseObjectProperty() {
1574
+ var token, key, id, param;
1575
+
1576
+ token = lookahead();
1577
+
1578
+ if (token.type === Token.Identifier) {
1579
+
1580
+ id = parseObjectPropertyKey();
1581
+
1582
+ // Property Assignment: Getter and Setter.
1583
+
1584
+ if (token.value === 'get' && !match(':')) {
1585
+ key = parseObjectPropertyKey();
1586
+ expect('(');
1587
+ expect(')');
1588
+ return {
1589
+ type: Syntax.Property,
1590
+ key: key,
1591
+ value: parsePropertyFunction([]),
1592
+ kind: 'get'
1593
+ };
1594
+ } else if (token.value === 'set' && !match(':')) {
1595
+ key = parseObjectPropertyKey();
1596
+ expect('(');
1597
+ token = lookahead();
1598
+ if (token.type !== Token.Identifier) {
1599
+ expect(')');
1600
+ throwErrorTolerant(token, Messages.UnexpectedToken, token.value);
1601
+ return {
1602
+ type: Syntax.Property,
1603
+ key: key,
1604
+ value: parsePropertyFunction([]),
1605
+ kind: 'set'
1606
+ };
1607
+ } else {
1608
+ param = [ parseVariableIdentifier() ];
1609
+ expect(')');
1610
+ return {
1611
+ type: Syntax.Property,
1612
+ key: key,
1613
+ value: parsePropertyFunction(param, token),
1614
+ kind: 'set'
1615
+ };
1616
+ }
1617
+ } else {
1618
+ expect(':');
1619
+ return {
1620
+ type: Syntax.Property,
1621
+ key: id,
1622
+ value: parseAssignmentExpression(),
1623
+ kind: 'init'
1624
+ };
1625
+ }
1626
+ } else if (token.type === Token.EOF || token.type === Token.Punctuator) {
1627
+ throwUnexpected(token);
1628
+ } else {
1629
+ key = parseObjectPropertyKey();
1630
+ expect(':');
1631
+ return {
1632
+ type: Syntax.Property,
1633
+ key: key,
1634
+ value: parseAssignmentExpression(),
1635
+ kind: 'init'
1636
+ };
1637
+ }
1638
+ }
1639
+
1640
+ function parseObjectInitialiser() {
1641
+ var properties = [], property, name, kind, map = {}, toString = String;
1642
+
1643
+ expect('{');
1644
+
1645
+ while (!match('}')) {
1646
+ property = parseObjectProperty();
1647
+
1648
+ if (property.key.type === Syntax.Identifier) {
1649
+ name = property.key.name;
1650
+ } else {
1651
+ name = toString(property.key.value);
1652
+ }
1653
+ kind = (property.kind === 'init') ? PropertyKind.Data : (property.kind === 'get') ? PropertyKind.Get : PropertyKind.Set;
1654
+ if (Object.prototype.hasOwnProperty.call(map, name)) {
1655
+ if (map[name] === PropertyKind.Data) {
1656
+ if (strict && kind === PropertyKind.Data) {
1657
+ throwErrorTolerant({}, Messages.StrictDuplicateProperty);
1658
+ } else if (kind !== PropertyKind.Data) {
1659
+ throwErrorTolerant({}, Messages.AccessorDataProperty);
1660
+ }
1661
+ } else {
1662
+ if (kind === PropertyKind.Data) {
1663
+ throwErrorTolerant({}, Messages.AccessorDataProperty);
1664
+ } else if (map[name] & kind) {
1665
+ throwErrorTolerant({}, Messages.AccessorGetSet);
1666
+ }
1667
+ }
1668
+ map[name] |= kind;
1669
+ } else {
1670
+ map[name] = kind;
1671
+ }
1672
+
1673
+ properties.push(property);
1674
+
1675
+ if (!match('}')) {
1676
+ expect(',');
1677
+ }
1678
+ }
1679
+
1680
+ expect('}');
1681
+
1682
+ return {
1683
+ type: Syntax.ObjectExpression,
1684
+ properties: properties
1685
+ };
1686
+ }
1687
+
1688
+ // 11.1.6 The Grouping Operator
1689
+
1690
+ function parseGroupExpression() {
1691
+ var expr;
1692
+
1693
+ expect('(');
1694
+
1695
+ expr = parseExpression();
1696
+
1697
+ expect(')');
1698
+
1699
+ return expr;
1700
+ }
1701
+
1702
+
1703
+ // 11.1 Primary Expressions
1704
+
1705
+ function parsePrimaryExpression() {
1706
+ var token = lookahead(),
1707
+ type = token.type;
1708
+
1709
+ if (type === Token.Identifier) {
1710
+ return {
1711
+ type: Syntax.Identifier,
1712
+ name: lex().value
1713
+ };
1714
+ }
1715
+
1716
+ if (type === Token.StringLiteral || type === Token.NumericLiteral) {
1717
+ if (strict && token.octal) {
1718
+ throwErrorTolerant(token, Messages.StrictOctalLiteral);
1719
+ }
1720
+ return createLiteral(lex());
1721
+ }
1722
+
1723
+ if (type === Token.Keyword) {
1724
+ if (matchKeyword('this')) {
1725
+ lex();
1726
+ return {
1727
+ type: Syntax.ThisExpression
1728
+ };
1729
+ }
1730
+
1731
+ if (matchKeyword('function')) {
1732
+ return parseFunctionExpression();
1733
+ }
1734
+ }
1735
+
1736
+ if (type === Token.BooleanLiteral) {
1737
+ lex();
1738
+ token.value = (token.value === 'true');
1739
+ return createLiteral(token);
1740
+ }
1741
+
1742
+ if (type === Token.NullLiteral) {
1743
+ lex();
1744
+ token.value = null;
1745
+ return createLiteral(token);
1746
+ }
1747
+
1748
+ if (match('[')) {
1749
+ return parseArrayInitialiser();
1750
+ }
1751
+
1752
+ if (match('{')) {
1753
+ return parseObjectInitialiser();
1754
+ }
1755
+
1756
+ if (match('(')) {
1757
+ return parseGroupExpression();
1758
+ }
1759
+
1760
+ if (match('/') || match('/=')) {
1761
+ return createLiteral(scanRegExp());
1762
+ }
1763
+
1764
+ return throwUnexpected(lex());
1765
+ }
1766
+
1767
+ // 11.2 Left-Hand-Side Expressions
1768
+
1769
+ function parseArguments() {
1770
+ var args = [];
1771
+
1772
+ expect('(');
1773
+
1774
+ if (!match(')')) {
1775
+ while (index < length) {
1776
+ args.push(parseAssignmentExpression());
1777
+ if (match(')')) {
1778
+ break;
1779
+ }
1780
+ expect(',');
1781
+ }
1782
+ }
1783
+
1784
+ expect(')');
1785
+
1786
+ return args;
1787
+ }
1788
+
1789
+ function parseNonComputedProperty() {
1790
+ var token = lex();
1791
+
1792
+ if (!isIdentifierName(token)) {
1793
+ throwUnexpected(token);
1794
+ }
1795
+
1796
+ return {
1797
+ type: Syntax.Identifier,
1798
+ name: token.value
1799
+ };
1800
+ }
1801
+
1802
+ function parseNonComputedMember() {
1803
+ expect('.');
1804
+
1805
+ return parseNonComputedProperty();
1806
+ }
1807
+
1808
+ function parseComputedMember() {
1809
+ var expr;
1810
+
1811
+ expect('[');
1812
+
1813
+ expr = parseExpression();
1814
+
1815
+ expect(']');
1816
+
1817
+ return expr;
1818
+ }
1819
+
1820
+ function parseNewExpression() {
1821
+ var expr;
1822
+
1823
+ expectKeyword('new');
1824
+
1825
+ expr = {
1826
+ type: Syntax.NewExpression,
1827
+ callee: parseLeftHandSideExpression(),
1828
+ 'arguments': []
1829
+ };
1830
+
1831
+ if (match('(')) {
1832
+ expr['arguments'] = parseArguments();
1833
+ }
1834
+
1835
+ return expr;
1836
+ }
1837
+
1838
+ function parseLeftHandSideExpressionAllowCall() {
1839
+ var expr;
1840
+
1841
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
1842
+
1843
+ while (match('.') || match('[') || match('(')) {
1844
+ if (match('(')) {
1845
+ expr = {
1846
+ type: Syntax.CallExpression,
1847
+ callee: expr,
1848
+ 'arguments': parseArguments()
1849
+ };
1850
+ } else if (match('[')) {
1851
+ expr = {
1852
+ type: Syntax.MemberExpression,
1853
+ computed: true,
1854
+ object: expr,
1855
+ property: parseComputedMember()
1856
+ };
1857
+ } else {
1858
+ expr = {
1859
+ type: Syntax.MemberExpression,
1860
+ computed: false,
1861
+ object: expr,
1862
+ property: parseNonComputedMember()
1863
+ };
1864
+ }
1865
+ }
1866
+
1867
+ return expr;
1868
+ }
1869
+
1870
+
1871
+ function parseLeftHandSideExpression() {
1872
+ var expr;
1873
+
1874
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
1875
+
1876
+ while (match('.') || match('[')) {
1877
+ if (match('[')) {
1878
+ expr = {
1879
+ type: Syntax.MemberExpression,
1880
+ computed: true,
1881
+ object: expr,
1882
+ property: parseComputedMember()
1883
+ };
1884
+ } else {
1885
+ expr = {
1886
+ type: Syntax.MemberExpression,
1887
+ computed: false,
1888
+ object: expr,
1889
+ property: parseNonComputedMember()
1890
+ };
1891
+ }
1892
+ }
1893
+
1894
+ return expr;
1895
+ }
1896
+
1897
+ // 11.3 Postfix Expressions
1898
+
1899
+ function parsePostfixExpression() {
1900
+ var expr = parseLeftHandSideExpressionAllowCall(), token;
1901
+
1902
+ token = lookahead();
1903
+ if (token.type !== Token.Punctuator) {
1904
+ return expr;
1905
+ }
1906
+
1907
+ if ((match('++') || match('--')) && !peekLineTerminator()) {
1908
+ // 11.3.1, 11.3.2
1909
+ if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
1910
+ throwErrorTolerant({}, Messages.StrictLHSPostfix);
1911
+ }
1912
+ if (!isLeftHandSide(expr)) {
1913
+ throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
1914
+ }
1915
+
1916
+ expr = {
1917
+ type: Syntax.UpdateExpression,
1918
+ operator: lex().value,
1919
+ argument: expr,
1920
+ prefix: false
1921
+ };
1922
+ }
1923
+
1924
+ return expr;
1925
+ }
1926
+
1927
+ // 11.4 Unary Operators
1928
+
1929
+ function parseUnaryExpression() {
1930
+ var token, expr;
1931
+
1932
+ token = lookahead();
1933
+ if (token.type !== Token.Punctuator && token.type !== Token.Keyword) {
1934
+ return parsePostfixExpression();
1935
+ }
1936
+
1937
+ if (match('++') || match('--')) {
1938
+ token = lex();
1939
+ expr = parseUnaryExpression();
1940
+ // 11.4.4, 11.4.5
1941
+ if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
1942
+ throwErrorTolerant({}, Messages.StrictLHSPrefix);
1943
+ }
1944
+
1945
+ if (!isLeftHandSide(expr)) {
1946
+ throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
1947
+ }
1948
+
1949
+ expr = {
1950
+ type: Syntax.UpdateExpression,
1951
+ operator: token.value,
1952
+ argument: expr,
1953
+ prefix: true
1954
+ };
1955
+ return expr;
1956
+ }
1957
+
1958
+ if (match('+') || match('-') || match('~') || match('!')) {
1959
+ expr = {
1960
+ type: Syntax.UnaryExpression,
1961
+ operator: lex().value,
1962
+ argument: parseUnaryExpression(),
1963
+ prefix: true
1964
+ };
1965
+ return expr;
1966
+ }
1967
+
1968
+ if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) {
1969
+ expr = {
1970
+ type: Syntax.UnaryExpression,
1971
+ operator: lex().value,
1972
+ argument: parseUnaryExpression(),
1973
+ prefix: true
1974
+ };
1975
+ if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) {
1976
+ throwErrorTolerant({}, Messages.StrictDelete);
1977
+ }
1978
+ return expr;
1979
+ }
1980
+
1981
+ return parsePostfixExpression();
1982
+ }
1983
+
1984
+ // 11.5 Multiplicative Operators
1985
+
1986
+ function parseMultiplicativeExpression() {
1987
+ var expr = parseUnaryExpression();
1988
+
1989
+ while (match('*') || match('/') || match('%')) {
1990
+ expr = {
1991
+ type: Syntax.BinaryExpression,
1992
+ operator: lex().value,
1993
+ left: expr,
1994
+ right: parseUnaryExpression()
1995
+ };
1996
+ }
1997
+
1998
+ return expr;
1999
+ }
2000
+
2001
+ // 11.6 Additive Operators
2002
+
2003
+ function parseAdditiveExpression() {
2004
+ var expr = parseMultiplicativeExpression();
2005
+
2006
+ while (match('+') || match('-')) {
2007
+ expr = {
2008
+ type: Syntax.BinaryExpression,
2009
+ operator: lex().value,
2010
+ left: expr,
2011
+ right: parseMultiplicativeExpression()
2012
+ };
2013
+ }
2014
+
2015
+ return expr;
2016
+ }
2017
+
2018
+ // 11.7 Bitwise Shift Operators
2019
+
2020
+ function parseShiftExpression() {
2021
+ var expr = parseAdditiveExpression();
2022
+
2023
+ while (match('<<') || match('>>') || match('>>>')) {
2024
+ expr = {
2025
+ type: Syntax.BinaryExpression,
2026
+ operator: lex().value,
2027
+ left: expr,
2028
+ right: parseAdditiveExpression()
2029
+ };
2030
+ }
2031
+
2032
+ return expr;
2033
+ }
2034
+ // 11.8 Relational Operators
2035
+
2036
+ function parseRelationalExpression() {
2037
+ var expr, previousAllowIn;
2038
+
2039
+ previousAllowIn = state.allowIn;
2040
+ state.allowIn = true;
2041
+
2042
+ expr = parseShiftExpression();
2043
+
2044
+ while (match('<') || match('>') || match('<=') || match('>=') || (previousAllowIn && matchKeyword('in')) || matchKeyword('instanceof')) {
2045
+ expr = {
2046
+ type: Syntax.BinaryExpression,
2047
+ operator: lex().value,
2048
+ left: expr,
2049
+ right: parseShiftExpression()
2050
+ };
2051
+ }
2052
+
2053
+ state.allowIn = previousAllowIn;
2054
+ return expr;
2055
+ }
2056
+
2057
+ // 11.9 Equality Operators
2058
+
2059
+ function parseEqualityExpression() {
2060
+ var expr = parseRelationalExpression();
2061
+
2062
+ while (match('==') || match('!=') || match('===') || match('!==')) {
2063
+ expr = {
2064
+ type: Syntax.BinaryExpression,
2065
+ operator: lex().value,
2066
+ left: expr,
2067
+ right: parseRelationalExpression()
2068
+ };
2069
+ }
2070
+
2071
+ return expr;
2072
+ }
2073
+
2074
+ // 11.10 Binary Bitwise Operators
2075
+
2076
+ function parseBitwiseANDExpression() {
2077
+ var expr = parseEqualityExpression();
2078
+
2079
+ while (match('&')) {
2080
+ lex();
2081
+ expr = {
2082
+ type: Syntax.BinaryExpression,
2083
+ operator: '&',
2084
+ left: expr,
2085
+ right: parseEqualityExpression()
2086
+ };
2087
+ }
2088
+
2089
+ return expr;
2090
+ }
2091
+
2092
+ function parseBitwiseXORExpression() {
2093
+ var expr = parseBitwiseANDExpression();
2094
+
2095
+ while (match('^')) {
2096
+ lex();
2097
+ expr = {
2098
+ type: Syntax.BinaryExpression,
2099
+ operator: '^',
2100
+ left: expr,
2101
+ right: parseBitwiseANDExpression()
2102
+ };
2103
+ }
2104
+
2105
+ return expr;
2106
+ }
2107
+
2108
+ function parseBitwiseORExpression() {
2109
+ var expr = parseBitwiseXORExpression();
2110
+
2111
+ while (match('|')) {
2112
+ lex();
2113
+ expr = {
2114
+ type: Syntax.BinaryExpression,
2115
+ operator: '|',
2116
+ left: expr,
2117
+ right: parseBitwiseXORExpression()
2118
+ };
2119
+ }
2120
+
2121
+ return expr;
2122
+ }
2123
+
2124
+ // 11.11 Binary Logical Operators
2125
+
2126
+ function parseLogicalANDExpression() {
2127
+ var expr = parseBitwiseORExpression();
2128
+
2129
+ while (match('&&')) {
2130
+ lex();
2131
+ expr = {
2132
+ type: Syntax.LogicalExpression,
2133
+ operator: '&&',
2134
+ left: expr,
2135
+ right: parseBitwiseORExpression()
2136
+ };
2137
+ }
2138
+
2139
+ return expr;
2140
+ }
2141
+
2142
+ function parseLogicalORExpression() {
2143
+ var expr = parseLogicalANDExpression();
2144
+
2145
+ while (match('||')) {
2146
+ lex();
2147
+ expr = {
2148
+ type: Syntax.LogicalExpression,
2149
+ operator: '||',
2150
+ left: expr,
2151
+ right: parseLogicalANDExpression()
2152
+ };
2153
+ }
2154
+
2155
+ return expr;
2156
+ }
2157
+
2158
+ // 11.12 Conditional Operator
2159
+
2160
+ function parseConditionalExpression() {
2161
+ var expr, previousAllowIn, consequent;
2162
+
2163
+ expr = parseLogicalORExpression();
2164
+
2165
+ if (match('?')) {
2166
+ lex();
2167
+ previousAllowIn = state.allowIn;
2168
+ state.allowIn = true;
2169
+ consequent = parseAssignmentExpression();
2170
+ state.allowIn = previousAllowIn;
2171
+ expect(':');
2172
+
2173
+ expr = {
2174
+ type: Syntax.ConditionalExpression,
2175
+ test: expr,
2176
+ consequent: consequent,
2177
+ alternate: parseAssignmentExpression()
2178
+ };
2179
+ }
2180
+
2181
+ return expr;
2182
+ }
2183
+
2184
+ // 11.13 Assignment Operators
2185
+
2186
+ function parseAssignmentExpression() {
2187
+ var token, expr;
2188
+
2189
+ token = lookahead();
2190
+ expr = parseConditionalExpression();
2191
+
2192
+ if (matchAssign()) {
2193
+ // LeftHandSideExpression
2194
+ if (!isLeftHandSide(expr)) {
2195
+ throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
2196
+ }
2197
+
2198
+ // 11.13.1
2199
+ if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
2200
+ throwErrorTolerant(token, Messages.StrictLHSAssignment);
2201
+ }
2202
+
2203
+ expr = {
2204
+ type: Syntax.AssignmentExpression,
2205
+ operator: lex().value,
2206
+ left: expr,
2207
+ right: parseAssignmentExpression()
2208
+ };
2209
+ }
2210
+
2211
+ return expr;
2212
+ }
2213
+
2214
+ // 11.14 Comma Operator
2215
+
2216
+ function parseExpression() {
2217
+ var expr = parseAssignmentExpression();
2218
+
2219
+ if (match(',')) {
2220
+ expr = {
2221
+ type: Syntax.SequenceExpression,
2222
+ expressions: [ expr ]
2223
+ };
2224
+
2225
+ while (index < length) {
2226
+ if (!match(',')) {
2227
+ break;
2228
+ }
2229
+ lex();
2230
+ expr.expressions.push(parseAssignmentExpression());
2231
+ }
2232
+
2233
+ }
2234
+ return expr;
2235
+ }
2236
+
2237
+ // 12.1 Block
2238
+
2239
+ function parseStatementList() {
2240
+ var list = [],
2241
+ statement;
2242
+
2243
+ while (index < length) {
2244
+ if (match('}')) {
2245
+ break;
2246
+ }
2247
+ statement = parseSourceElement();
2248
+ if (typeof statement === 'undefined') {
2249
+ break;
2250
+ }
2251
+ list.push(statement);
2252
+ }
2253
+
2254
+ return list;
2255
+ }
2256
+
2257
+ function parseBlock() {
2258
+ var block;
2259
+
2260
+ expect('{');
2261
+
2262
+ block = parseStatementList();
2263
+
2264
+ expect('}');
2265
+
2266
+ return {
2267
+ type: Syntax.BlockStatement,
2268
+ body: block
2269
+ };
2270
+ }
2271
+
2272
+ // 12.2 Variable Statement
2273
+
2274
+ function parseVariableIdentifier() {
2275
+ var token = lex();
2276
+
2277
+ if (token.type !== Token.Identifier) {
2278
+ throwUnexpected(token);
2279
+ }
2280
+
2281
+ return {
2282
+ type: Syntax.Identifier,
2283
+ name: token.value
2284
+ };
2285
+ }
2286
+
2287
+ function parseVariableDeclaration(kind) {
2288
+ var id = parseVariableIdentifier(),
2289
+ init = null;
2290
+
2291
+ // 12.2.1
2292
+ if (strict && isRestrictedWord(id.name)) {
2293
+ throwErrorTolerant({}, Messages.StrictVarName);
2294
+ }
2295
+
2296
+ if (kind === 'const') {
2297
+ expect('=');
2298
+ init = parseAssignmentExpression();
2299
+ } else if (match('=')) {
2300
+ lex();
2301
+ init = parseAssignmentExpression();
2302
+ }
2303
+
2304
+ return {
2305
+ type: Syntax.VariableDeclarator,
2306
+ id: id,
2307
+ init: init
2308
+ };
2309
+ }
2310
+
2311
+ function parseVariableDeclarationList(kind) {
2312
+ var list = [];
2313
+
2314
+ do {
2315
+ list.push(parseVariableDeclaration(kind));
2316
+ if (!match(',')) {
2317
+ break;
2318
+ }
2319
+ lex();
2320
+ } while (index < length);
2321
+
2322
+ return list;
2323
+ }
2324
+
2325
+ function parseVariableStatement() {
2326
+ var declarations;
2327
+
2328
+ expectKeyword('var');
2329
+
2330
+ declarations = parseVariableDeclarationList();
2331
+
2332
+ consumeSemicolon();
2333
+
2334
+ return {
2335
+ type: Syntax.VariableDeclaration,
2336
+ declarations: declarations,
2337
+ kind: 'var'
2338
+ };
2339
+ }
2340
+
2341
+ // kind may be `const` or `let`
2342
+ // Both are experimental and not in the specification yet.
2343
+ // see http://wiki.ecmascript.org/doku.php?id=harmony:const
2344
+ // and http://wiki.ecmascript.org/doku.php?id=harmony:let
2345
+ function parseConstLetDeclaration(kind) {
2346
+ var declarations;
2347
+
2348
+ expectKeyword(kind);
2349
+
2350
+ declarations = parseVariableDeclarationList(kind);
2351
+
2352
+ consumeSemicolon();
2353
+
2354
+ return {
2355
+ type: Syntax.VariableDeclaration,
2356
+ declarations: declarations,
2357
+ kind: kind
2358
+ };
2359
+ }
2360
+
2361
+ // 12.3 Empty Statement
2362
+
2363
+ function parseEmptyStatement() {
2364
+ expect(';');
2365
+
2366
+ return {
2367
+ type: Syntax.EmptyStatement
2368
+ };
2369
+ }
2370
+
2371
+ // 12.4 Expression Statement
2372
+
2373
+ function parseExpressionStatement() {
2374
+ var expr = parseExpression();
2375
+
2376
+ consumeSemicolon();
2377
+
2378
+ return {
2379
+ type: Syntax.ExpressionStatement,
2380
+ expression: expr
2381
+ };
2382
+ }
2383
+
2384
+ // 12.5 If statement
2385
+
2386
+ function parseIfStatement() {
2387
+ var test, consequent, alternate;
2388
+
2389
+ expectKeyword('if');
2390
+
2391
+ expect('(');
2392
+
2393
+ test = parseExpression();
2394
+
2395
+ expect(')');
2396
+
2397
+ consequent = parseStatement();
2398
+
2399
+ if (matchKeyword('else')) {
2400
+ lex();
2401
+ alternate = parseStatement();
2402
+ } else {
2403
+ alternate = null;
2404
+ }
2405
+
2406
+ return {
2407
+ type: Syntax.IfStatement,
2408
+ test: test,
2409
+ consequent: consequent,
2410
+ alternate: alternate
2411
+ };
2412
+ }
2413
+
2414
+ // 12.6 Iteration Statements
2415
+
2416
+ function parseDoWhileStatement() {
2417
+ var body, test, oldInIteration;
2418
+
2419
+ expectKeyword('do');
2420
+
2421
+ oldInIteration = state.inIteration;
2422
+ state.inIteration = true;
2423
+
2424
+ body = parseStatement();
2425
+
2426
+ state.inIteration = oldInIteration;
2427
+
2428
+ expectKeyword('while');
2429
+
2430
+ expect('(');
2431
+
2432
+ test = parseExpression();
2433
+
2434
+ expect(')');
2435
+
2436
+ if (match(';')) {
2437
+ lex();
2438
+ }
2439
+
2440
+ return {
2441
+ type: Syntax.DoWhileStatement,
2442
+ body: body,
2443
+ test: test
2444
+ };
2445
+ }
2446
+
2447
+ function parseWhileStatement() {
2448
+ var test, body, oldInIteration;
2449
+
2450
+ expectKeyword('while');
2451
+
2452
+ expect('(');
2453
+
2454
+ test = parseExpression();
2455
+
2456
+ expect(')');
2457
+
2458
+ oldInIteration = state.inIteration;
2459
+ state.inIteration = true;
2460
+
2461
+ body = parseStatement();
2462
+
2463
+ state.inIteration = oldInIteration;
2464
+
2465
+ return {
2466
+ type: Syntax.WhileStatement,
2467
+ test: test,
2468
+ body: body
2469
+ };
2470
+ }
2471
+
2472
+ function parseForVariableDeclaration() {
2473
+ var token = lex();
2474
+
2475
+ return {
2476
+ type: Syntax.VariableDeclaration,
2477
+ declarations: parseVariableDeclarationList(),
2478
+ kind: token.value
2479
+ };
2480
+ }
2481
+
2482
+ function parseForStatement() {
2483
+ var init, test, update, left, right, body, oldInIteration;
2484
+
2485
+ init = test = update = null;
2486
+
2487
+ expectKeyword('for');
2488
+
2489
+ expect('(');
2490
+
2491
+ if (match(';')) {
2492
+ lex();
2493
+ } else {
2494
+ if (matchKeyword('var') || matchKeyword('let')) {
2495
+ state.allowIn = false;
2496
+ init = parseForVariableDeclaration();
2497
+ state.allowIn = true;
2498
+
2499
+ if (init.declarations.length === 1 && matchKeyword('in')) {
2500
+ lex();
2501
+ left = init;
2502
+ right = parseExpression();
2503
+ init = null;
2504
+ }
2505
+ } else {
2506
+ state.allowIn = false;
2507
+ init = parseExpression();
2508
+ state.allowIn = true;
2509
+
2510
+ if (matchKeyword('in')) {
2511
+ // LeftHandSideExpression
2512
+ if (!isLeftHandSide(init)) {
2513
+ throwErrorTolerant({}, Messages.InvalidLHSInForIn);
2514
+ }
2515
+
2516
+ lex();
2517
+ left = init;
2518
+ right = parseExpression();
2519
+ init = null;
2520
+ }
2521
+ }
2522
+
2523
+ if (typeof left === 'undefined') {
2524
+ expect(';');
2525
+ }
2526
+ }
2527
+
2528
+ if (typeof left === 'undefined') {
2529
+
2530
+ if (!match(';')) {
2531
+ test = parseExpression();
2532
+ }
2533
+ expect(';');
2534
+
2535
+ if (!match(')')) {
2536
+ update = parseExpression();
2537
+ }
2538
+ }
2539
+
2540
+ expect(')');
2541
+
2542
+ oldInIteration = state.inIteration;
2543
+ state.inIteration = true;
2544
+
2545
+ body = parseStatement();
2546
+
2547
+ state.inIteration = oldInIteration;
2548
+
2549
+ if (typeof left === 'undefined') {
2550
+ return {
2551
+ type: Syntax.ForStatement,
2552
+ init: init,
2553
+ test: test,
2554
+ update: update,
2555
+ body: body
2556
+ };
2557
+ }
2558
+
2559
+ return {
2560
+ type: Syntax.ForInStatement,
2561
+ left: left,
2562
+ right: right,
2563
+ body: body,
2564
+ each: false
2565
+ };
2566
+ }
2567
+
2568
+ // 12.7 The continue statement
2569
+
2570
+ function parseContinueStatement() {
2571
+ var token, label = null;
2572
+
2573
+ expectKeyword('continue');
2574
+
2575
+ // Optimize the most common form: 'continue;'.
2576
+ if (source[index] === ';') {
2577
+ lex();
2578
+
2579
+ if (!state.inIteration) {
2580
+ throwError({}, Messages.IllegalContinue);
2581
+ }
2582
+
2583
+ return {
2584
+ type: Syntax.ContinueStatement,
2585
+ label: null
2586
+ };
2587
+ }
2588
+
2589
+ if (peekLineTerminator()) {
2590
+ if (!state.inIteration) {
2591
+ throwError({}, Messages.IllegalContinue);
2592
+ }
2593
+
2594
+ return {
2595
+ type: Syntax.ContinueStatement,
2596
+ label: null
2597
+ };
2598
+ }
2599
+
2600
+ token = lookahead();
2601
+ if (token.type === Token.Identifier) {
2602
+ label = parseVariableIdentifier();
2603
+
2604
+ if (!Object.prototype.hasOwnProperty.call(state.labelSet, label.name)) {
2605
+ throwError({}, Messages.UnknownLabel, label.name);
2606
+ }
2607
+ }
2608
+
2609
+ consumeSemicolon();
2610
+
2611
+ if (label === null && !state.inIteration) {
2612
+ throwError({}, Messages.IllegalContinue);
2613
+ }
2614
+
2615
+ return {
2616
+ type: Syntax.ContinueStatement,
2617
+ label: label
2618
+ };
2619
+ }
2620
+
2621
+ // 12.8 The break statement
2622
+
2623
+ function parseBreakStatement() {
2624
+ var token, label = null;
2625
+
2626
+ expectKeyword('break');
2627
+
2628
+ // Optimize the most common form: 'break;'.
2629
+ if (source[index] === ';') {
2630
+ lex();
2631
+
2632
+ if (!(state.inIteration || state.inSwitch)) {
2633
+ throwError({}, Messages.IllegalBreak);
2634
+ }
2635
+
2636
+ return {
2637
+ type: Syntax.BreakStatement,
2638
+ label: null
2639
+ };
2640
+ }
2641
+
2642
+ if (peekLineTerminator()) {
2643
+ if (!(state.inIteration || state.inSwitch)) {
2644
+ throwError({}, Messages.IllegalBreak);
2645
+ }
2646
+
2647
+ return {
2648
+ type: Syntax.BreakStatement,
2649
+ label: null
2650
+ };
2651
+ }
2652
+
2653
+ token = lookahead();
2654
+ if (token.type === Token.Identifier) {
2655
+ label = parseVariableIdentifier();
2656
+
2657
+ if (!Object.prototype.hasOwnProperty.call(state.labelSet, label.name)) {
2658
+ throwError({}, Messages.UnknownLabel, label.name);
2659
+ }
2660
+ }
2661
+
2662
+ consumeSemicolon();
2663
+
2664
+ if (label === null && !(state.inIteration || state.inSwitch)) {
2665
+ throwError({}, Messages.IllegalBreak);
2666
+ }
2667
+
2668
+ return {
2669
+ type: Syntax.BreakStatement,
2670
+ label: label
2671
+ };
2672
+ }
2673
+
2674
+ // 12.9 The return statement
2675
+
2676
+ function parseReturnStatement() {
2677
+ var token, argument = null;
2678
+
2679
+ expectKeyword('return');
2680
+
2681
+ if (!state.inFunctionBody) {
2682
+ throwErrorTolerant({}, Messages.IllegalReturn);
2683
+ }
2684
+
2685
+ // 'return' followed by a space and an identifier is very common.
2686
+ if (source[index] === ' ') {
2687
+ if (isIdentifierStart(source[index + 1])) {
2688
+ argument = parseExpression();
2689
+ consumeSemicolon();
2690
+ return {
2691
+ type: Syntax.ReturnStatement,
2692
+ argument: argument
2693
+ };
2694
+ }
2695
+ }
2696
+
2697
+ if (peekLineTerminator()) {
2698
+ return {
2699
+ type: Syntax.ReturnStatement,
2700
+ argument: null
2701
+ };
2702
+ }
2703
+
2704
+ if (!match(';')) {
2705
+ token = lookahead();
2706
+ if (!match('}') && token.type !== Token.EOF) {
2707
+ argument = parseExpression();
2708
+ }
2709
+ }
2710
+
2711
+ consumeSemicolon();
2712
+
2713
+ return {
2714
+ type: Syntax.ReturnStatement,
2715
+ argument: argument
2716
+ };
2717
+ }
2718
+
2719
+ // 12.10 The with statement
2720
+
2721
+ function parseWithStatement() {
2722
+ var object, body;
2723
+
2724
+ if (strict) {
2725
+ throwErrorTolerant({}, Messages.StrictModeWith);
2726
+ }
2727
+
2728
+ expectKeyword('with');
2729
+
2730
+ expect('(');
2731
+
2732
+ object = parseExpression();
2733
+
2734
+ expect(')');
2735
+
2736
+ body = parseStatement();
2737
+
2738
+ return {
2739
+ type: Syntax.WithStatement,
2740
+ object: object,
2741
+ body: body
2742
+ };
2743
+ }
2744
+
2745
+ // 12.10 The swith statement
2746
+
2747
+ function parseSwitchCase() {
2748
+ var test,
2749
+ consequent = [],
2750
+ statement;
2751
+
2752
+ if (matchKeyword('default')) {
2753
+ lex();
2754
+ test = null;
2755
+ } else {
2756
+ expectKeyword('case');
2757
+ test = parseExpression();
2758
+ }
2759
+ expect(':');
2760
+
2761
+ while (index < length) {
2762
+ if (match('}') || matchKeyword('default') || matchKeyword('case')) {
2763
+ break;
2764
+ }
2765
+ statement = parseStatement();
2766
+ if (typeof statement === 'undefined') {
2767
+ break;
2768
+ }
2769
+ consequent.push(statement);
2770
+ }
2771
+
2772
+ return {
2773
+ type: Syntax.SwitchCase,
2774
+ test: test,
2775
+ consequent: consequent
2776
+ };
2777
+ }
2778
+
2779
+ function parseSwitchStatement() {
2780
+ var discriminant, cases, clause, oldInSwitch, defaultFound;
2781
+
2782
+ expectKeyword('switch');
2783
+
2784
+ expect('(');
2785
+
2786
+ discriminant = parseExpression();
2787
+
2788
+ expect(')');
2789
+
2790
+ expect('{');
2791
+
2792
+ cases = [];
2793
+
2794
+ if (match('}')) {
2795
+ lex();
2796
+ return {
2797
+ type: Syntax.SwitchStatement,
2798
+ discriminant: discriminant,
2799
+ cases: cases
2800
+ };
2801
+ }
2802
+
2803
+ oldInSwitch = state.inSwitch;
2804
+ state.inSwitch = true;
2805
+ defaultFound = false;
2806
+
2807
+ while (index < length) {
2808
+ if (match('}')) {
2809
+ break;
2810
+ }
2811
+ clause = parseSwitchCase();
2812
+ if (clause.test === null) {
2813
+ if (defaultFound) {
2814
+ throwError({}, Messages.MultipleDefaultsInSwitch);
2815
+ }
2816
+ defaultFound = true;
2817
+ }
2818
+ cases.push(clause);
2819
+ }
2820
+
2821
+ state.inSwitch = oldInSwitch;
2822
+
2823
+ expect('}');
2824
+
2825
+ return {
2826
+ type: Syntax.SwitchStatement,
2827
+ discriminant: discriminant,
2828
+ cases: cases
2829
+ };
2830
+ }
2831
+
2832
+ // 12.13 The throw statement
2833
+
2834
+ function parseThrowStatement() {
2835
+ var argument;
2836
+
2837
+ expectKeyword('throw');
2838
+
2839
+ if (peekLineTerminator()) {
2840
+ throwError({}, Messages.NewlineAfterThrow);
2841
+ }
2842
+
2843
+ argument = parseExpression();
2844
+
2845
+ consumeSemicolon();
2846
+
2847
+ return {
2848
+ type: Syntax.ThrowStatement,
2849
+ argument: argument
2850
+ };
2851
+ }
2852
+
2853
+ // 12.14 The try statement
2854
+
2855
+ function parseCatchClause() {
2856
+ var param;
2857
+
2858
+ expectKeyword('catch');
2859
+
2860
+ expect('(');
2861
+ if (match(')')) {
2862
+ throwUnexpected(lookahead());
2863
+ }
2864
+
2865
+ param = parseVariableIdentifier();
2866
+ // 12.14.1
2867
+ if (strict && isRestrictedWord(param.name)) {
2868
+ throwErrorTolerant({}, Messages.StrictCatchVariable);
2869
+ }
2870
+
2871
+ expect(')');
2872
+
2873
+ return {
2874
+ type: Syntax.CatchClause,
2875
+ param: param,
2876
+ body: parseBlock()
2877
+ };
2878
+ }
2879
+
2880
+ function parseTryStatement() {
2881
+ var block, handlers = [], finalizer = null;
2882
+
2883
+ expectKeyword('try');
2884
+
2885
+ block = parseBlock();
2886
+
2887
+ if (matchKeyword('catch')) {
2888
+ handlers.push(parseCatchClause());
2889
+ }
2890
+
2891
+ if (matchKeyword('finally')) {
2892
+ lex();
2893
+ finalizer = parseBlock();
2894
+ }
2895
+
2896
+ if (handlers.length === 0 && !finalizer) {
2897
+ throwError({}, Messages.NoCatchOrFinally);
2898
+ }
2899
+
2900
+ return {
2901
+ type: Syntax.TryStatement,
2902
+ block: block,
2903
+ guardedHandlers: [],
2904
+ handlers: handlers,
2905
+ finalizer: finalizer
2906
+ };
2907
+ }
2908
+
2909
+ // 12.15 The debugger statement
2910
+
2911
+ function parseDebuggerStatement() {
2912
+ expectKeyword('debugger');
2913
+
2914
+ consumeSemicolon();
2915
+
2916
+ return {
2917
+ type: Syntax.DebuggerStatement
2918
+ };
2919
+ }
2920
+
2921
+ // 12 Statements
2922
+
2923
+ function parseStatement() {
2924
+ var token = lookahead(),
2925
+ expr,
2926
+ labeledBody;
2927
+
2928
+ if (token.type === Token.EOF) {
2929
+ throwUnexpected(token);
2930
+ }
2931
+
2932
+ if (token.type === Token.Punctuator) {
2933
+ switch (token.value) {
2934
+ case ';':
2935
+ return parseEmptyStatement();
2936
+ case '{':
2937
+ return parseBlock();
2938
+ case '(':
2939
+ return parseExpressionStatement();
2940
+ }
2941
+ }
2942
+
2943
+ if (token.type === Token.Keyword) {
2944
+ switch (token.value) {
2945
+ case 'break':
2946
+ return parseBreakStatement();
2947
+ case 'continue':
2948
+ return parseContinueStatement();
2949
+ case 'debugger':
2950
+ return parseDebuggerStatement();
2951
+ case 'do':
2952
+ return parseDoWhileStatement();
2953
+ case 'for':
2954
+ return parseForStatement();
2955
+ case 'function':
2956
+ return parseFunctionDeclaration();
2957
+ case 'if':
2958
+ return parseIfStatement();
2959
+ case 'return':
2960
+ return parseReturnStatement();
2961
+ case 'switch':
2962
+ return parseSwitchStatement();
2963
+ case 'throw':
2964
+ return parseThrowStatement();
2965
+ case 'try':
2966
+ return parseTryStatement();
2967
+ case 'var':
2968
+ return parseVariableStatement();
2969
+ case 'while':
2970
+ return parseWhileStatement();
2971
+ case 'with':
2972
+ return parseWithStatement();
2973
+ }
2974
+ }
2975
+
2976
+ expr = parseExpression();
2977
+
2978
+ // 12.12 Labelled Statements
2979
+ if ((expr.type === Syntax.Identifier) && match(':')) {
2980
+ lex();
2981
+
2982
+ if (Object.prototype.hasOwnProperty.call(state.labelSet, expr.name)) {
2983
+ throwError({}, Messages.Redeclaration, 'Label', expr.name);
2984
+ }
2985
+
2986
+ state.labelSet[expr.name] = true;
2987
+ labeledBody = parseStatement();
2988
+ delete state.labelSet[expr.name];
2989
+
2990
+ return {
2991
+ type: Syntax.LabeledStatement,
2992
+ label: expr,
2993
+ body: labeledBody
2994
+ };
2995
+ }
2996
+
2997
+ consumeSemicolon();
2998
+
2999
+ return {
3000
+ type: Syntax.ExpressionStatement,
3001
+ expression: expr
3002
+ };
3003
+ }
3004
+
3005
+ // 13 Function Definition
3006
+
3007
+ function parseFunctionSourceElements() {
3008
+ var sourceElement, sourceElements = [], token, directive, firstRestricted,
3009
+ oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody;
3010
+
3011
+ expect('{');
3012
+
3013
+ while (index < length) {
3014
+ token = lookahead();
3015
+ if (token.type !== Token.StringLiteral) {
3016
+ break;
3017
+ }
3018
+
3019
+ sourceElement = parseSourceElement();
3020
+ sourceElements.push(sourceElement);
3021
+ if (sourceElement.expression.type !== Syntax.Literal) {
3022
+ // this is not directive
3023
+ break;
3024
+ }
3025
+ directive = sliceSource(token.range[0] + 1, token.range[1] - 1);
3026
+ if (directive === 'use strict') {
3027
+ strict = true;
3028
+ if (firstRestricted) {
3029
+ throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
3030
+ }
3031
+ } else {
3032
+ if (!firstRestricted && token.octal) {
3033
+ firstRestricted = token;
3034
+ }
3035
+ }
3036
+ }
3037
+
3038
+ oldLabelSet = state.labelSet;
3039
+ oldInIteration = state.inIteration;
3040
+ oldInSwitch = state.inSwitch;
3041
+ oldInFunctionBody = state.inFunctionBody;
3042
+
3043
+ state.labelSet = {};
3044
+ state.inIteration = false;
3045
+ state.inSwitch = false;
3046
+ state.inFunctionBody = true;
3047
+
3048
+ while (index < length) {
3049
+ if (match('}')) {
3050
+ break;
3051
+ }
3052
+ sourceElement = parseSourceElement();
3053
+ if (typeof sourceElement === 'undefined') {
3054
+ break;
3055
+ }
3056
+ sourceElements.push(sourceElement);
3057
+ }
3058
+
3059
+ expect('}');
3060
+
3061
+ state.labelSet = oldLabelSet;
3062
+ state.inIteration = oldInIteration;
3063
+ state.inSwitch = oldInSwitch;
3064
+ state.inFunctionBody = oldInFunctionBody;
3065
+
3066
+ return {
3067
+ type: Syntax.BlockStatement,
3068
+ body: sourceElements
3069
+ };
3070
+ }
3071
+
3072
+ function parseFunctionDeclaration() {
3073
+ var id, param, params = [], body, token, stricted, firstRestricted, message, previousStrict, paramSet;
3074
+
3075
+ expectKeyword('function');
3076
+ token = lookahead();
3077
+ id = parseVariableIdentifier();
3078
+ if (strict) {
3079
+ if (isRestrictedWord(token.value)) {
3080
+ throwErrorTolerant(token, Messages.StrictFunctionName);
3081
+ }
3082
+ } else {
3083
+ if (isRestrictedWord(token.value)) {
3084
+ firstRestricted = token;
3085
+ message = Messages.StrictFunctionName;
3086
+ } else if (isStrictModeReservedWord(token.value)) {
3087
+ firstRestricted = token;
3088
+ message = Messages.StrictReservedWord;
3089
+ }
3090
+ }
3091
+
3092
+ expect('(');
3093
+
3094
+ if (!match(')')) {
3095
+ paramSet = {};
3096
+ while (index < length) {
3097
+ token = lookahead();
3098
+ param = parseVariableIdentifier();
3099
+ if (strict) {
3100
+ if (isRestrictedWord(token.value)) {
3101
+ stricted = token;
3102
+ message = Messages.StrictParamName;
3103
+ }
3104
+ if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3105
+ stricted = token;
3106
+ message = Messages.StrictParamDupe;
3107
+ }
3108
+ } else if (!firstRestricted) {
3109
+ if (isRestrictedWord(token.value)) {
3110
+ firstRestricted = token;
3111
+ message = Messages.StrictParamName;
3112
+ } else if (isStrictModeReservedWord(token.value)) {
3113
+ firstRestricted = token;
3114
+ message = Messages.StrictReservedWord;
3115
+ } else if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3116
+ firstRestricted = token;
3117
+ message = Messages.StrictParamDupe;
3118
+ }
3119
+ }
3120
+ params.push(param);
3121
+ paramSet[param.name] = true;
3122
+ if (match(')')) {
3123
+ break;
3124
+ }
3125
+ expect(',');
3126
+ }
3127
+ }
3128
+
3129
+ expect(')');
3130
+
3131
+ previousStrict = strict;
3132
+ body = parseFunctionSourceElements();
3133
+ if (strict && firstRestricted) {
3134
+ throwError(firstRestricted, message);
3135
+ }
3136
+ if (strict && stricted) {
3137
+ throwErrorTolerant(stricted, message);
3138
+ }
3139
+ strict = previousStrict;
3140
+
3141
+ return {
3142
+ type: Syntax.FunctionDeclaration,
3143
+ id: id,
3144
+ params: params,
3145
+ defaults: [],
3146
+ body: body,
3147
+ rest: null,
3148
+ generator: false,
3149
+ expression: false
3150
+ };
3151
+ }
3152
+
3153
+ function parseFunctionExpression() {
3154
+ var token, id = null, stricted, firstRestricted, message, param, params = [], body, previousStrict, paramSet;
3155
+
3156
+ expectKeyword('function');
3157
+
3158
+ if (!match('(')) {
3159
+ token = lookahead();
3160
+ id = parseVariableIdentifier();
3161
+ if (strict) {
3162
+ if (isRestrictedWord(token.value)) {
3163
+ throwErrorTolerant(token, Messages.StrictFunctionName);
3164
+ }
3165
+ } else {
3166
+ if (isRestrictedWord(token.value)) {
3167
+ firstRestricted = token;
3168
+ message = Messages.StrictFunctionName;
3169
+ } else if (isStrictModeReservedWord(token.value)) {
3170
+ firstRestricted = token;
3171
+ message = Messages.StrictReservedWord;
3172
+ }
3173
+ }
3174
+ }
3175
+
3176
+ expect('(');
3177
+
3178
+ if (!match(')')) {
3179
+ paramSet = {};
3180
+ while (index < length) {
3181
+ token = lookahead();
3182
+ param = parseVariableIdentifier();
3183
+ if (strict) {
3184
+ if (isRestrictedWord(token.value)) {
3185
+ stricted = token;
3186
+ message = Messages.StrictParamName;
3187
+ }
3188
+ if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3189
+ stricted = token;
3190
+ message = Messages.StrictParamDupe;
3191
+ }
3192
+ } else if (!firstRestricted) {
3193
+ if (isRestrictedWord(token.value)) {
3194
+ firstRestricted = token;
3195
+ message = Messages.StrictParamName;
3196
+ } else if (isStrictModeReservedWord(token.value)) {
3197
+ firstRestricted = token;
3198
+ message = Messages.StrictReservedWord;
3199
+ } else if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3200
+ firstRestricted = token;
3201
+ message = Messages.StrictParamDupe;
3202
+ }
3203
+ }
3204
+ params.push(param);
3205
+ paramSet[param.name] = true;
3206
+ if (match(')')) {
3207
+ break;
3208
+ }
3209
+ expect(',');
3210
+ }
3211
+ }
3212
+
3213
+ expect(')');
3214
+
3215
+ previousStrict = strict;
3216
+ body = parseFunctionSourceElements();
3217
+ if (strict && firstRestricted) {
3218
+ throwError(firstRestricted, message);
3219
+ }
3220
+ if (strict && stricted) {
3221
+ throwErrorTolerant(stricted, message);
3222
+ }
3223
+ strict = previousStrict;
3224
+
3225
+ return {
3226
+ type: Syntax.FunctionExpression,
3227
+ id: id,
3228
+ params: params,
3229
+ defaults: [],
3230
+ body: body,
3231
+ rest: null,
3232
+ generator: false,
3233
+ expression: false
3234
+ };
3235
+ }
3236
+
3237
+ // 14 Program
3238
+
3239
+ function parseSourceElement() {
3240
+ var token = lookahead();
3241
+
3242
+ if (token.type === Token.Keyword) {
3243
+ switch (token.value) {
3244
+ case 'const':
3245
+ case 'let':
3246
+ return parseConstLetDeclaration(token.value);
3247
+ case 'function':
3248
+ return parseFunctionDeclaration();
3249
+ default:
3250
+ return parseStatement();
3251
+ }
3252
+ }
3253
+
3254
+ if (token.type !== Token.EOF) {
3255
+ return parseStatement();
3256
+ }
3257
+ }
3258
+
3259
+ function parseSourceElements() {
3260
+ var sourceElement, sourceElements = [], token, directive, firstRestricted;
3261
+
3262
+ while (index < length) {
3263
+ token = lookahead();
3264
+ if (token.type !== Token.StringLiteral) {
3265
+ break;
3266
+ }
3267
+
3268
+ sourceElement = parseSourceElement();
3269
+ sourceElements.push(sourceElement);
3270
+ if (sourceElement.expression.type !== Syntax.Literal) {
3271
+ // this is not directive
3272
+ break;
3273
+ }
3274
+ directive = sliceSource(token.range[0] + 1, token.range[1] - 1);
3275
+ if (directive === 'use strict') {
3276
+ strict = true;
3277
+ if (firstRestricted) {
3278
+ throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
3279
+ }
3280
+ } else {
3281
+ if (!firstRestricted && token.octal) {
3282
+ firstRestricted = token;
3283
+ }
3284
+ }
3285
+ }
3286
+
3287
+ while (index < length) {
3288
+ sourceElement = parseSourceElement();
3289
+ if (typeof sourceElement === 'undefined') {
3290
+ break;
3291
+ }
3292
+ sourceElements.push(sourceElement);
3293
+ }
3294
+ return sourceElements;
3295
+ }
3296
+
3297
+ function parseProgram() {
3298
+ var program;
3299
+ strict = false;
3300
+ program = {
3301
+ type: Syntax.Program,
3302
+ body: parseSourceElements()
3303
+ };
3304
+ return program;
3305
+ }
3306
+
3307
+ // The following functions are needed only when the option to preserve
3308
+ // the comments is active.
3309
+
3310
+ function addComment(type, value, start, end, loc) {
3311
+ assert(typeof start === 'number', 'Comment must have valid position');
3312
+
3313
+ // Because the way the actual token is scanned, often the comments
3314
+ // (if any) are skipped twice during the lexical analysis.
3315
+ // Thus, we need to skip adding a comment if the comment array already
3316
+ // handled it.
3317
+ if (extra.comments.length > 0) {
3318
+ if (extra.comments[extra.comments.length - 1].range[1] > start) {
3319
+ return;
3320
+ }
3321
+ }
3322
+
3323
+ extra.comments.push({
3324
+ type: type,
3325
+ value: value,
3326
+ range: [start, end],
3327
+ loc: loc
3328
+ });
3329
+ }
3330
+
3331
+ function scanComment() {
3332
+ var comment, ch, loc, start, blockComment, lineComment;
3333
+
3334
+ comment = '';
3335
+ blockComment = false;
3336
+ lineComment = false;
3337
+
3338
+ while (index < length) {
3339
+ ch = source[index];
3340
+
3341
+ if (lineComment) {
3342
+ ch = source[index++];
3343
+ if (isLineTerminator(ch)) {
3344
+ loc.end = {
3345
+ line: lineNumber,
3346
+ column: index - lineStart - 1
3347
+ };
3348
+ lineComment = false;
3349
+ addComment('Line', comment, start, index - 1, loc);
3350
+ if (ch === '\r' && source[index] === '\n') {
3351
+ ++index;
3352
+ }
3353
+ ++lineNumber;
3354
+ lineStart = index;
3355
+ comment = '';
3356
+ } else if (index >= length) {
3357
+ lineComment = false;
3358
+ comment += ch;
3359
+ loc.end = {
3360
+ line: lineNumber,
3361
+ column: length - lineStart
3362
+ };
3363
+ addComment('Line', comment, start, length, loc);
3364
+ } else {
3365
+ comment += ch;
3366
+ }
3367
+ } else if (blockComment) {
3368
+ if (isLineTerminator(ch)) {
3369
+ if (ch === '\r' && source[index + 1] === '\n') {
3370
+ ++index;
3371
+ comment += '\r\n';
3372
+ } else {
3373
+ comment += ch;
3374
+ }
3375
+ ++lineNumber;
3376
+ ++index;
3377
+ lineStart = index;
3378
+ if (index >= length) {
3379
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
3380
+ }
3381
+ } else {
3382
+ ch = source[index++];
3383
+ if (index >= length) {
3384
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
3385
+ }
3386
+ comment += ch;
3387
+ if (ch === '*') {
3388
+ ch = source[index];
3389
+ if (ch === '/') {
3390
+ comment = comment.substr(0, comment.length - 1);
3391
+ blockComment = false;
3392
+ ++index;
3393
+ loc.end = {
3394
+ line: lineNumber,
3395
+ column: index - lineStart
3396
+ };
3397
+ addComment('Block', comment, start, index, loc);
3398
+ comment = '';
3399
+ }
3400
+ }
3401
+ }
3402
+ } else if (ch === '/') {
3403
+ ch = source[index + 1];
3404
+ if (ch === '/') {
3405
+ loc = {
3406
+ start: {
3407
+ line: lineNumber,
3408
+ column: index - lineStart
3409
+ }
3410
+ };
3411
+ start = index;
3412
+ index += 2;
3413
+ lineComment = true;
3414
+ if (index >= length) {
3415
+ loc.end = {
3416
+ line: lineNumber,
3417
+ column: index - lineStart
3418
+ };
3419
+ lineComment = false;
3420
+ addComment('Line', comment, start, index, loc);
3421
+ }
3422
+ } else if (ch === '*') {
3423
+ start = index;
3424
+ index += 2;
3425
+ blockComment = true;
3426
+ loc = {
3427
+ start: {
3428
+ line: lineNumber,
3429
+ column: index - lineStart - 2
3430
+ }
3431
+ };
3432
+ if (index >= length) {
3433
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
3434
+ }
3435
+ } else {
3436
+ break;
3437
+ }
3438
+ } else if (isWhiteSpace(ch)) {
3439
+ ++index;
3440
+ } else if (isLineTerminator(ch)) {
3441
+ ++index;
3442
+ if (ch === '\r' && source[index] === '\n') {
3443
+ ++index;
3444
+ }
3445
+ ++lineNumber;
3446
+ lineStart = index;
3447
+ } else {
3448
+ break;
3449
+ }
3450
+ }
3451
+ }
3452
+
3453
+ function filterCommentLocation() {
3454
+ var i, entry, comment, comments = [];
3455
+
3456
+ for (i = 0; i < extra.comments.length; ++i) {
3457
+ entry = extra.comments[i];
3458
+ comment = {
3459
+ type: entry.type,
3460
+ value: entry.value
3461
+ };
3462
+ if (extra.range) {
3463
+ comment.range = entry.range;
3464
+ }
3465
+ if (extra.loc) {
3466
+ comment.loc = entry.loc;
3467
+ }
3468
+ comments.push(comment);
3469
+ }
3470
+
3471
+ extra.comments = comments;
3472
+ }
3473
+
3474
+ function collectToken() {
3475
+ var loc, token, range, value;
3476
+
3477
+ skipComment();
3478
+ loc = {
3479
+ start: {
3480
+ line: lineNumber,
3481
+ column: index - lineStart
3482
+ }
3483
+ };
3484
+
3485
+ token = extra.advance();
3486
+ loc.end = {
3487
+ line: lineNumber,
3488
+ column: index - lineStart
3489
+ };
3490
+
3491
+ if (token.type !== Token.EOF) {
3492
+ range = [token.range[0], token.range[1]];
3493
+ value = sliceSource(token.range[0], token.range[1]);
3494
+ extra.tokens.push({
3495
+ type: TokenName[token.type],
3496
+ value: value,
3497
+ range: range,
3498
+ loc: loc
3499
+ });
3500
+ }
3501
+
3502
+ return token;
3503
+ }
3504
+
3505
+ function collectRegex() {
3506
+ var pos, loc, regex, token;
3507
+
3508
+ skipComment();
3509
+
3510
+ pos = index;
3511
+ loc = {
3512
+ start: {
3513
+ line: lineNumber,
3514
+ column: index - lineStart
3515
+ }
3516
+ };
3517
+
3518
+ regex = extra.scanRegExp();
3519
+ loc.end = {
3520
+ line: lineNumber,
3521
+ column: index - lineStart
3522
+ };
3523
+
3524
+ // Pop the previous token, which is likely '/' or '/='
3525
+ if (extra.tokens.length > 0) {
3526
+ token = extra.tokens[extra.tokens.length - 1];
3527
+ if (token.range[0] === pos && token.type === 'Punctuator') {
3528
+ if (token.value === '/' || token.value === '/=') {
3529
+ extra.tokens.pop();
3530
+ }
3531
+ }
3532
+ }
3533
+
3534
+ extra.tokens.push({
3535
+ type: 'RegularExpression',
3536
+ value: regex.literal,
3537
+ range: [pos, index],
3538
+ loc: loc
3539
+ });
3540
+
3541
+ return regex;
3542
+ }
3543
+
3544
+ function filterTokenLocation() {
3545
+ var i, entry, token, tokens = [];
3546
+
3547
+ for (i = 0; i < extra.tokens.length; ++i) {
3548
+ entry = extra.tokens[i];
3549
+ token = {
3550
+ type: entry.type,
3551
+ value: entry.value
3552
+ };
3553
+ if (extra.range) {
3554
+ token.range = entry.range;
3555
+ }
3556
+ if (extra.loc) {
3557
+ token.loc = entry.loc;
3558
+ }
3559
+ tokens.push(token);
3560
+ }
3561
+
3562
+ extra.tokens = tokens;
3563
+ }
3564
+
3565
+ function createLiteral(token) {
3566
+ return {
3567
+ type: Syntax.Literal,
3568
+ value: token.value
3569
+ };
3570
+ }
3571
+
3572
+ function createRawLiteral(token) {
3573
+ return {
3574
+ type: Syntax.Literal,
3575
+ value: token.value,
3576
+ raw: sliceSource(token.range[0], token.range[1])
3577
+ };
3578
+ }
3579
+
3580
+ function createLocationMarker() {
3581
+ var marker = {};
3582
+
3583
+ marker.range = [index, index];
3584
+ marker.loc = {
3585
+ start: {
3586
+ line: lineNumber,
3587
+ column: index - lineStart
3588
+ },
3589
+ end: {
3590
+ line: lineNumber,
3591
+ column: index - lineStart
3592
+ }
3593
+ };
3594
+
3595
+ marker.end = function () {
3596
+ this.range[1] = index;
3597
+ this.loc.end.line = lineNumber;
3598
+ this.loc.end.column = index - lineStart;
3599
+ };
3600
+
3601
+ marker.applyGroup = function (node) {
3602
+ if (extra.range) {
3603
+ node.groupRange = [this.range[0], this.range[1]];
3604
+ }
3605
+ if (extra.loc) {
3606
+ node.groupLoc = {
3607
+ start: {
3608
+ line: this.loc.start.line,
3609
+ column: this.loc.start.column
3610
+ },
3611
+ end: {
3612
+ line: this.loc.end.line,
3613
+ column: this.loc.end.column
3614
+ }
3615
+ };
3616
+ }
3617
+ };
3618
+
3619
+ marker.apply = function (node) {
3620
+ if (extra.range) {
3621
+ node.range = [this.range[0], this.range[1]];
3622
+ }
3623
+ if (extra.loc) {
3624
+ node.loc = {
3625
+ start: {
3626
+ line: this.loc.start.line,
3627
+ column: this.loc.start.column
3628
+ },
3629
+ end: {
3630
+ line: this.loc.end.line,
3631
+ column: this.loc.end.column
3632
+ }
3633
+ };
3634
+ }
3635
+ };
3636
+
3637
+ return marker;
3638
+ }
3639
+
3640
+ function trackGroupExpression() {
3641
+ var marker, expr;
3642
+
3643
+ skipComment();
3644
+ marker = createLocationMarker();
3645
+ expect('(');
3646
+
3647
+ expr = parseExpression();
3648
+
3649
+ expect(')');
3650
+
3651
+ marker.end();
3652
+ marker.applyGroup(expr);
3653
+
3654
+ return expr;
3655
+ }
3656
+
3657
+ function trackLeftHandSideExpression() {
3658
+ var marker, expr;
3659
+
3660
+ skipComment();
3661
+ marker = createLocationMarker();
3662
+
3663
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
3664
+
3665
+ while (match('.') || match('[')) {
3666
+ if (match('[')) {
3667
+ expr = {
3668
+ type: Syntax.MemberExpression,
3669
+ computed: true,
3670
+ object: expr,
3671
+ property: parseComputedMember()
3672
+ };
3673
+ marker.end();
3674
+ marker.apply(expr);
3675
+ } else {
3676
+ expr = {
3677
+ type: Syntax.MemberExpression,
3678
+ computed: false,
3679
+ object: expr,
3680
+ property: parseNonComputedMember()
3681
+ };
3682
+ marker.end();
3683
+ marker.apply(expr);
3684
+ }
3685
+ }
3686
+
3687
+ return expr;
3688
+ }
3689
+
3690
+ function trackLeftHandSideExpressionAllowCall() {
3691
+ var marker, expr;
3692
+
3693
+ skipComment();
3694
+ marker = createLocationMarker();
3695
+
3696
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
3697
+
3698
+ while (match('.') || match('[') || match('(')) {
3699
+ if (match('(')) {
3700
+ expr = {
3701
+ type: Syntax.CallExpression,
3702
+ callee: expr,
3703
+ 'arguments': parseArguments()
3704
+ };
3705
+ marker.end();
3706
+ marker.apply(expr);
3707
+ } else if (match('[')) {
3708
+ expr = {
3709
+ type: Syntax.MemberExpression,
3710
+ computed: true,
3711
+ object: expr,
3712
+ property: parseComputedMember()
3713
+ };
3714
+ marker.end();
3715
+ marker.apply(expr);
3716
+ } else {
3717
+ expr = {
3718
+ type: Syntax.MemberExpression,
3719
+ computed: false,
3720
+ object: expr,
3721
+ property: parseNonComputedMember()
3722
+ };
3723
+ marker.end();
3724
+ marker.apply(expr);
3725
+ }
3726
+ }
3727
+
3728
+ return expr;
3729
+ }
3730
+
3731
+ function filterGroup(node) {
3732
+ var n, i, entry;
3733
+
3734
+ n = (Object.prototype.toString.apply(node) === '[object Array]') ? [] : {};
3735
+ for (i in node) {
3736
+ if (node.hasOwnProperty(i) && i !== 'groupRange' && i !== 'groupLoc') {
3737
+ entry = node[i];
3738
+ if (entry === null || typeof entry !== 'object' || entry instanceof RegExp) {
3739
+ n[i] = entry;
3740
+ } else {
3741
+ n[i] = filterGroup(entry);
3742
+ }
3743
+ }
3744
+ }
3745
+ return n;
3746
+ }
3747
+
3748
+ function wrapTrackingFunction(range, loc) {
3749
+
3750
+ return function (parseFunction) {
3751
+
3752
+ function isBinary(node) {
3753
+ return node.type === Syntax.LogicalExpression ||
3754
+ node.type === Syntax.BinaryExpression;
3755
+ }
3756
+
3757
+ function visit(node) {
3758
+ var start, end;
3759
+
3760
+ if (isBinary(node.left)) {
3761
+ visit(node.left);
3762
+ }
3763
+ if (isBinary(node.right)) {
3764
+ visit(node.right);
3765
+ }
3766
+
3767
+ if (range) {
3768
+ if (node.left.groupRange || node.right.groupRange) {
3769
+ start = node.left.groupRange ? node.left.groupRange[0] : node.left.range[0];
3770
+ end = node.right.groupRange ? node.right.groupRange[1] : node.right.range[1];
3771
+ node.range = [start, end];
3772
+ } else if (typeof node.range === 'undefined') {
3773
+ start = node.left.range[0];
3774
+ end = node.right.range[1];
3775
+ node.range = [start, end];
3776
+ }
3777
+ }
3778
+ if (loc) {
3779
+ if (node.left.groupLoc || node.right.groupLoc) {
3780
+ start = node.left.groupLoc ? node.left.groupLoc.start : node.left.loc.start;
3781
+ end = node.right.groupLoc ? node.right.groupLoc.end : node.right.loc.end;
3782
+ node.loc = {
3783
+ start: start,
3784
+ end: end
3785
+ };
3786
+ } else if (typeof node.loc === 'undefined') {
3787
+ node.loc = {
3788
+ start: node.left.loc.start,
3789
+ end: node.right.loc.end
3790
+ };
3791
+ }
3792
+ }
3793
+ }
3794
+
3795
+ return function () {
3796
+ var marker, node;
3797
+
3798
+ skipComment();
3799
+
3800
+ marker = createLocationMarker();
3801
+ node = parseFunction.apply(null, arguments);
3802
+ marker.end();
3803
+
3804
+ if (range && typeof node.range === 'undefined') {
3805
+ marker.apply(node);
3806
+ }
3807
+
3808
+ if (loc && typeof node.loc === 'undefined') {
3809
+ marker.apply(node);
3810
+ }
3811
+
3812
+ if (isBinary(node)) {
3813
+ visit(node);
3814
+ }
3815
+
3816
+ return node;
3817
+ };
3818
+ };
3819
+ }
3820
+
3821
+ function patch() {
3822
+
3823
+ var wrapTracking;
3824
+
3825
+ if (extra.comments) {
3826
+ extra.skipComment = skipComment;
3827
+ skipComment = scanComment;
3828
+ }
3829
+
3830
+ if (extra.raw) {
3831
+ extra.createLiteral = createLiteral;
3832
+ createLiteral = createRawLiteral;
3833
+ }
3834
+
3835
+ if (extra.range || extra.loc) {
3836
+
3837
+ extra.parseGroupExpression = parseGroupExpression;
3838
+ extra.parseLeftHandSideExpression = parseLeftHandSideExpression;
3839
+ extra.parseLeftHandSideExpressionAllowCall = parseLeftHandSideExpressionAllowCall;
3840
+ parseGroupExpression = trackGroupExpression;
3841
+ parseLeftHandSideExpression = trackLeftHandSideExpression;
3842
+ parseLeftHandSideExpressionAllowCall = trackLeftHandSideExpressionAllowCall;
3843
+
3844
+ wrapTracking = wrapTrackingFunction(extra.range, extra.loc);
3845
+
3846
+ extra.parseAdditiveExpression = parseAdditiveExpression;
3847
+ extra.parseAssignmentExpression = parseAssignmentExpression;
3848
+ extra.parseBitwiseANDExpression = parseBitwiseANDExpression;
3849
+ extra.parseBitwiseORExpression = parseBitwiseORExpression;
3850
+ extra.parseBitwiseXORExpression = parseBitwiseXORExpression;
3851
+ extra.parseBlock = parseBlock;
3852
+ extra.parseFunctionSourceElements = parseFunctionSourceElements;
3853
+ extra.parseCatchClause = parseCatchClause;
3854
+ extra.parseComputedMember = parseComputedMember;
3855
+ extra.parseConditionalExpression = parseConditionalExpression;
3856
+ extra.parseConstLetDeclaration = parseConstLetDeclaration;
3857
+ extra.parseEqualityExpression = parseEqualityExpression;
3858
+ extra.parseExpression = parseExpression;
3859
+ extra.parseForVariableDeclaration = parseForVariableDeclaration;
3860
+ extra.parseFunctionDeclaration = parseFunctionDeclaration;
3861
+ extra.parseFunctionExpression = parseFunctionExpression;
3862
+ extra.parseLogicalANDExpression = parseLogicalANDExpression;
3863
+ extra.parseLogicalORExpression = parseLogicalORExpression;
3864
+ extra.parseMultiplicativeExpression = parseMultiplicativeExpression;
3865
+ extra.parseNewExpression = parseNewExpression;
3866
+ extra.parseNonComputedProperty = parseNonComputedProperty;
3867
+ extra.parseObjectProperty = parseObjectProperty;
3868
+ extra.parseObjectPropertyKey = parseObjectPropertyKey;
3869
+ extra.parsePostfixExpression = parsePostfixExpression;
3870
+ extra.parsePrimaryExpression = parsePrimaryExpression;
3871
+ extra.parseProgram = parseProgram;
3872
+ extra.parsePropertyFunction = parsePropertyFunction;
3873
+ extra.parseRelationalExpression = parseRelationalExpression;
3874
+ extra.parseStatement = parseStatement;
3875
+ extra.parseShiftExpression = parseShiftExpression;
3876
+ extra.parseSwitchCase = parseSwitchCase;
3877
+ extra.parseUnaryExpression = parseUnaryExpression;
3878
+ extra.parseVariableDeclaration = parseVariableDeclaration;
3879
+ extra.parseVariableIdentifier = parseVariableIdentifier;
3880
+
3881
+ parseAdditiveExpression = wrapTracking(extra.parseAdditiveExpression);
3882
+ parseAssignmentExpression = wrapTracking(extra.parseAssignmentExpression);
3883
+ parseBitwiseANDExpression = wrapTracking(extra.parseBitwiseANDExpression);
3884
+ parseBitwiseORExpression = wrapTracking(extra.parseBitwiseORExpression);
3885
+ parseBitwiseXORExpression = wrapTracking(extra.parseBitwiseXORExpression);
3886
+ parseBlock = wrapTracking(extra.parseBlock);
3887
+ parseFunctionSourceElements = wrapTracking(extra.parseFunctionSourceElements);
3888
+ parseCatchClause = wrapTracking(extra.parseCatchClause);
3889
+ parseComputedMember = wrapTracking(extra.parseComputedMember);
3890
+ parseConditionalExpression = wrapTracking(extra.parseConditionalExpression);
3891
+ parseConstLetDeclaration = wrapTracking(extra.parseConstLetDeclaration);
3892
+ parseEqualityExpression = wrapTracking(extra.parseEqualityExpression);
3893
+ parseExpression = wrapTracking(extra.parseExpression);
3894
+ parseForVariableDeclaration = wrapTracking(extra.parseForVariableDeclaration);
3895
+ parseFunctionDeclaration = wrapTracking(extra.parseFunctionDeclaration);
3896
+ parseFunctionExpression = wrapTracking(extra.parseFunctionExpression);
3897
+ parseLeftHandSideExpression = wrapTracking(parseLeftHandSideExpression);
3898
+ parseLogicalANDExpression = wrapTracking(extra.parseLogicalANDExpression);
3899
+ parseLogicalORExpression = wrapTracking(extra.parseLogicalORExpression);
3900
+ parseMultiplicativeExpression = wrapTracking(extra.parseMultiplicativeExpression);
3901
+ parseNewExpression = wrapTracking(extra.parseNewExpression);
3902
+ parseNonComputedProperty = wrapTracking(extra.parseNonComputedProperty);
3903
+ parseObjectProperty = wrapTracking(extra.parseObjectProperty);
3904
+ parseObjectPropertyKey = wrapTracking(extra.parseObjectPropertyKey);
3905
+ parsePostfixExpression = wrapTracking(extra.parsePostfixExpression);
3906
+ parsePrimaryExpression = wrapTracking(extra.parsePrimaryExpression);
3907
+ parseProgram = wrapTracking(extra.parseProgram);
3908
+ parsePropertyFunction = wrapTracking(extra.parsePropertyFunction);
3909
+ parseRelationalExpression = wrapTracking(extra.parseRelationalExpression);
3910
+ parseStatement = wrapTracking(extra.parseStatement);
3911
+ parseShiftExpression = wrapTracking(extra.parseShiftExpression);
3912
+ parseSwitchCase = wrapTracking(extra.parseSwitchCase);
3913
+ parseUnaryExpression = wrapTracking(extra.parseUnaryExpression);
3914
+ parseVariableDeclaration = wrapTracking(extra.parseVariableDeclaration);
3915
+ parseVariableIdentifier = wrapTracking(extra.parseVariableIdentifier);
3916
+ }
3917
+
3918
+ if (typeof extra.tokens !== 'undefined') {
3919
+ extra.advance = advance;
3920
+ extra.scanRegExp = scanRegExp;
3921
+
3922
+ advance = collectToken;
3923
+ scanRegExp = collectRegex;
3924
+ }
3925
+ }
3926
+
3927
+ function unpatch() {
3928
+ if (typeof extra.skipComment === 'function') {
3929
+ skipComment = extra.skipComment;
3930
+ }
3931
+
3932
+ if (extra.raw) {
3933
+ createLiteral = extra.createLiteral;
3934
+ }
3935
+
3936
+ if (extra.range || extra.loc) {
3937
+ parseAdditiveExpression = extra.parseAdditiveExpression;
3938
+ parseAssignmentExpression = extra.parseAssignmentExpression;
3939
+ parseBitwiseANDExpression = extra.parseBitwiseANDExpression;
3940
+ parseBitwiseORExpression = extra.parseBitwiseORExpression;
3941
+ parseBitwiseXORExpression = extra.parseBitwiseXORExpression;
3942
+ parseBlock = extra.parseBlock;
3943
+ parseFunctionSourceElements = extra.parseFunctionSourceElements;
3944
+ parseCatchClause = extra.parseCatchClause;
3945
+ parseComputedMember = extra.parseComputedMember;
3946
+ parseConditionalExpression = extra.parseConditionalExpression;
3947
+ parseConstLetDeclaration = extra.parseConstLetDeclaration;
3948
+ parseEqualityExpression = extra.parseEqualityExpression;
3949
+ parseExpression = extra.parseExpression;
3950
+ parseForVariableDeclaration = extra.parseForVariableDeclaration;
3951
+ parseFunctionDeclaration = extra.parseFunctionDeclaration;
3952
+ parseFunctionExpression = extra.parseFunctionExpression;
3953
+ parseGroupExpression = extra.parseGroupExpression;
3954
+ parseLeftHandSideExpression = extra.parseLeftHandSideExpression;
3955
+ parseLeftHandSideExpressionAllowCall = extra.parseLeftHandSideExpressionAllowCall;
3956
+ parseLogicalANDExpression = extra.parseLogicalANDExpression;
3957
+ parseLogicalORExpression = extra.parseLogicalORExpression;
3958
+ parseMultiplicativeExpression = extra.parseMultiplicativeExpression;
3959
+ parseNewExpression = extra.parseNewExpression;
3960
+ parseNonComputedProperty = extra.parseNonComputedProperty;
3961
+ parseObjectProperty = extra.parseObjectProperty;
3962
+ parseObjectPropertyKey = extra.parseObjectPropertyKey;
3963
+ parsePrimaryExpression = extra.parsePrimaryExpression;
3964
+ parsePostfixExpression = extra.parsePostfixExpression;
3965
+ parseProgram = extra.parseProgram;
3966
+ parsePropertyFunction = extra.parsePropertyFunction;
3967
+ parseRelationalExpression = extra.parseRelationalExpression;
3968
+ parseStatement = extra.parseStatement;
3969
+ parseShiftExpression = extra.parseShiftExpression;
3970
+ parseSwitchCase = extra.parseSwitchCase;
3971
+ parseUnaryExpression = extra.parseUnaryExpression;
3972
+ parseVariableDeclaration = extra.parseVariableDeclaration;
3973
+ parseVariableIdentifier = extra.parseVariableIdentifier;
3974
+ }
3975
+
3976
+ if (typeof extra.scanRegExp === 'function') {
3977
+ advance = extra.advance;
3978
+ scanRegExp = extra.scanRegExp;
3979
+ }
3980
+ }
3981
+
3982
+ function stringToArray(str) {
3983
+ var length = str.length,
3984
+ result = [],
3985
+ i;
3986
+ for (i = 0; i < length; ++i) {
3987
+ result[i] = str.charAt(i);
3988
+ }
3989
+ return result;
3990
+ }
3991
+
3992
+ function parse(code, options) {
3993
+ var program, toString;
3994
+
3995
+ toString = String;
3996
+ if (typeof code !== 'string' && !(code instanceof String)) {
3997
+ code = toString(code);
3998
+ }
3999
+
4000
+ source = code;
4001
+ index = 0;
4002
+ lineNumber = (source.length > 0) ? 1 : 0;
4003
+ lineStart = 0;
4004
+ length = source.length;
4005
+ buffer = null;
4006
+ state = {
4007
+ allowIn: true,
4008
+ labelSet: {},
4009
+ inFunctionBody: false,
4010
+ inIteration: false,
4011
+ inSwitch: false
4012
+ };
4013
+
4014
+ extra = {};
4015
+ if (typeof options !== 'undefined') {
4016
+ extra.range = (typeof options.range === 'boolean') && options.range;
4017
+ extra.loc = (typeof options.loc === 'boolean') && options.loc;
4018
+ extra.raw = (typeof options.raw === 'boolean') && options.raw;
4019
+ if (typeof options.tokens === 'boolean' && options.tokens) {
4020
+ extra.tokens = [];
4021
+ }
4022
+ if (typeof options.comment === 'boolean' && options.comment) {
4023
+ extra.comments = [];
4024
+ }
4025
+ if (typeof options.tolerant === 'boolean' && options.tolerant) {
4026
+ extra.errors = [];
4027
+ }
4028
+ }
4029
+
4030
+ if (length > 0) {
4031
+ if (typeof source[0] === 'undefined') {
4032
+ // Try first to convert to a string. This is good as fast path
4033
+ // for old IE which understands string indexing for string
4034
+ // literals only and not for string object.
4035
+ if (code instanceof String) {
4036
+ source = code.valueOf();
4037
+ }
4038
+
4039
+ // Force accessing the characters via an array.
4040
+ if (typeof source[0] === 'undefined') {
4041
+ source = stringToArray(code);
4042
+ }
4043
+ }
4044
+ }
4045
+
4046
+ patch();
4047
+ try {
4048
+ program = parseProgram();
4049
+ if (typeof extra.comments !== 'undefined') {
4050
+ filterCommentLocation();
4051
+ program.comments = extra.comments;
4052
+ }
4053
+ if (typeof extra.tokens !== 'undefined') {
4054
+ filterTokenLocation();
4055
+ program.tokens = extra.tokens;
4056
+ }
4057
+ if (typeof extra.errors !== 'undefined') {
4058
+ program.errors = extra.errors;
4059
+ }
4060
+ if (extra.range || extra.loc) {
4061
+ program.body = filterGroup(program.body);
4062
+ }
4063
+ } catch (e) {
4064
+ throw e;
4065
+ } finally {
4066
+ unpatch();
4067
+ extra = {};
4068
+ }
4069
+
4070
+ return program;
4071
+ }
4072
+
4073
+ // Sync with package.json.
4074
+ exports.version = '1.0.4';
4075
+
4076
+ exports.parse = parse;
4077
+
4078
+ // Deep copy.
4079
+ exports.Syntax = (function () {
4080
+ var name, types = {};
4081
+
4082
+ if (typeof Object.create === 'function') {
4083
+ types = Object.create(null);
4084
+ }
4085
+
4086
+ for (name in Syntax) {
4087
+ if (Syntax.hasOwnProperty(name)) {
4088
+ types[name] = Syntax[name];
4089
+ }
4090
+ }
4091
+
4092
+ if (typeof Object.freeze === 'function') {
4093
+ Object.freeze(types);
4094
+ }
4095
+
4096
+ return types;
4097
+ }());
4098
+
4099
+ }));
4100
+ /* vim: set sw=4 ts=4 et tw=80 : */
4101
+ });var hoister = hoist;
4102
+
4103
+ function hoist(ast){
4104
+
4105
+ var parentStack = [];
4106
+ var variables = [];
4107
+ var functions = [];
4108
+
4109
+ if (Array.isArray(ast)){
4110
+
4111
+ walkAll(ast);
4112
+ prependScope(ast, variables, functions);
4113
+
4114
+ } else {
4115
+ walk(ast);
4116
+ }
4117
+
4118
+ return ast
4119
+
4120
+ // walk through each node of a program of block statement
4121
+ function walkAll(nodes){
4122
+ var result = null;
4123
+ for (var i=0;i<nodes.length;i++){
4124
+ var childNode = nodes[i];
4125
+ if (childNode.type === 'EmptyStatement') continue
4126
+ var result = walk(childNode);
4127
+ if (result === 'remove'){
4128
+ nodes.splice(i--, 1);
4129
+ }
4130
+ }
4131
+ }
4132
+
4133
+ function walk(node){
4134
+ var parent = parentStack[parentStack.length-1];
4135
+ var remove = false;
4136
+ parentStack.push(node);
4137
+
4138
+ var excludeBody = false;
4139
+ if (shouldScope(node, parent)){
4140
+ hoist(node.body);
4141
+ excludeBody = true;
4142
+ }
4143
+
4144
+ if (node.type === 'VariableDeclarator'){
4145
+ variables.push(node);
4146
+ }
4147
+
4148
+ if (node.type === 'FunctionDeclaration'){
4149
+ functions.push(node);
4150
+ remove = true;
4151
+ }
4152
+
4153
+ for (var key in node){
4154
+ if (key === 'type' || (excludeBody && key === 'body')) continue
4155
+ if (key in node && node[key] && typeof node[key] == 'object'){
4156
+ if (node[key].type){
4157
+ walk(node[key]);
4158
+ } else if (Array.isArray(node[key])){
4159
+ walkAll(node[key]);
4160
+ }
4161
+ }
4162
+ }
4163
+
4164
+ parentStack.pop();
4165
+ if (remove){
4166
+ return 'remove'
4167
+ }
4168
+ }
4169
+ }
4170
+
4171
+ function shouldScope(node, parent){
4172
+ if (node.type === 'Program'){
4173
+ return true
4174
+ } else if (node.type === 'BlockStatement'){
4175
+ if (parent && (parent.type === 'FunctionExpression' || parent.type === 'FunctionDeclaration')){
4176
+ return true
4177
+ }
4178
+ }
4179
+ }
4180
+
4181
+ function prependScope(nodes, variables, functions){
4182
+ if (variables && variables.length){
4183
+ var declarations = [];
4184
+ for (var i=0;i<variables.length;i++){
4185
+ declarations.push({
4186
+ type: 'VariableDeclarator',
4187
+ id: variables[i].id,
4188
+ init: null
4189
+ });
4190
+ }
4191
+
4192
+ nodes.unshift({
4193
+ type: 'VariableDeclaration',
4194
+ kind: 'var',
4195
+ declarations: declarations
4196
+ });
4197
+
4198
+ }
4199
+
4200
+ if (functions && functions.length){
4201
+ for (var i=0;i<functions.length;i++){
4202
+ nodes.unshift(functions[i]);
4203
+ }
4204
+ }
4205
+ }var infiniteChecker = InfiniteChecker;
4206
+
4207
+ function InfiniteChecker(maxIterations){
4208
+ if (this instanceof InfiniteChecker){
4209
+ this.maxIterations = maxIterations;
4210
+ this.count = 0;
4211
+ } else {
4212
+ return new InfiniteChecker(maxIterations)
4213
+ }
4214
+ }
4215
+
4216
+ InfiniteChecker.prototype.check = function(){
4217
+ this.count += 1;
4218
+ if (this.count > this.maxIterations){
4219
+ throw new Error('Infinite loop detected - reached max iterations')
4220
+ }
4221
+ };var names = ['Object', 'String', 'Boolean', 'Number', 'RegExp', 'Date', 'Array', 'Function'];
4222
+ var immutable = {string: 'String', boolean: 'Boolean', number: 'Number' };
4223
+
4224
+ var primitives = names.map(getGlobal);
4225
+ var protos = primitives.map(getProto);
4226
+
4227
+ var primitives_1 = Primitives;
4228
+
4229
+ function Primitives(context){
4230
+ if (this instanceof Primitives){
4231
+ this.context = context;
4232
+ for (var i=0;i<names.length;i++){
4233
+ if (!this.context[names[i]]){
4234
+ this.context[names[i]] = wrap(primitives[i]);
4235
+ }
4236
+ }
4237
+ } else {
4238
+ return new Primitives(context)
4239
+ }
4240
+ }
4241
+
4242
+ Primitives.prototype.replace = function(value){
4243
+ var primIndex = primitives.indexOf(value);
4244
+ var protoIndex = protos.indexOf(value);
4245
+
4246
+ if (~primIndex){
4247
+ var name = names[primIndex];
4248
+ return this.context[name]
4249
+ } else if (~protoIndex) {
4250
+ var name = names[protoIndex];
4251
+ return this.context[name].prototype
4252
+ } else {
4253
+ return value
4254
+ }
4255
+ };
4256
+
4257
+ Primitives.prototype.getPropertyObject = function(object, property){
4258
+ if (immutable[typeof object]){
4259
+ return this.getPrototypeOf(object)
4260
+ }
4261
+ return object
4262
+ };
4263
+
4264
+ Primitives.prototype.isPrimitive = function(value){
4265
+ return !!~primitives.indexOf(value) || !!~protos.indexOf(value)
4266
+ };
4267
+
4268
+ Primitives.prototype.getPrototypeOf = function(value){
4269
+ if (value == null){ // handle null and undefined
4270
+ return value
4271
+ }
4272
+
4273
+ var immutableType = immutable[typeof value];
4274
+ if (immutableType){
4275
+ var proto = this.context[immutableType].prototype;
4276
+ } else {
4277
+ var proto = Object.getPrototypeOf(value);
4278
+ }
4279
+
4280
+ if (!proto || proto === Object.prototype){
4281
+ return null
4282
+ } else {
4283
+ var replacement = this.replace(proto);
4284
+ if (replacement === value){
4285
+ replacement = this.replace(Object.prototype);
4286
+ }
4287
+ return replacement
4288
+ }
4289
+ };
4290
+
4291
+ Primitives.prototype.applyNew = function(func, args){
4292
+ if (func.wrapped){
4293
+ var prim = Object.getPrototypeOf(func);
4294
+ var instance = new (Function.prototype.bind.apply(prim, arguments));
4295
+ setProto(instance, func.prototype);
4296
+ return instance
4297
+ } else {
4298
+ return new (Function.prototype.bind.apply(func, arguments))
4299
+ }
4300
+ };
4301
+
4302
+ function getProto(func){
4303
+ return func.prototype
4304
+ }
4305
+
4306
+ function getGlobal(str){
4307
+ return commonjsGlobal[str]
4308
+ }
4309
+
4310
+ function setProto(obj, proto){
4311
+ obj.__proto__ = proto;
4312
+ }
4313
+
4314
+ function wrap(prim){
4315
+ var proto = Object.create(prim.prototype);
4316
+
4317
+ var result = function() {
4318
+ if (this instanceof result){
4319
+ prim.apply(this, arguments);
4320
+ } else {
4321
+ var instance = prim.apply(null, arguments);
4322
+ setProto(instance, proto);
4323
+ return instance
4324
+ }
4325
+ };
4326
+ setProto(result, prim);
4327
+ result.prototype = proto;
4328
+ result.wrapped = true;
4329
+ return result
4330
+ }var parse = esprima.parse;
4331
+
4332
+
4333
+
4334
+
4335
+
4336
+ var notevil = safeEval;
4337
+ var _eval = safeEval;
4338
+ var FunctionFactory_1 = FunctionFactory;
4339
+ var _Function = FunctionFactory();
4340
+
4341
+ var maxIterations = 1000000;
4342
+
4343
+ // 'eval' with a controlled environment
4344
+ function safeEval(src, parentContext){
4345
+ var tree = prepareAst(src);
4346
+ var context = Object.create(parentContext || {});
4347
+ return finalValue(evaluateAst(tree, context))
4348
+ }
4349
+
4350
+ // create a 'Function' constructor for a controlled environment
4351
+ function FunctionFactory(parentContext){
4352
+ var context = Object.create(parentContext || {});
4353
+ return function Function() {
4354
+ // normalize arguments array
4355
+ var args = Array.prototype.slice.call(arguments);
4356
+ var src = args.slice(-1)[0];
4357
+ args = args.slice(0,-1);
4358
+ if (typeof src === 'string'){
4359
+ //HACK: esprima doesn't like returns outside functions
4360
+ src = parse('function a(){' + src + '}').body[0].body;
4361
+ }
4362
+ var tree = prepareAst(src);
4363
+ return getFunction(tree, args, context)
4364
+ }
4365
+ }
4366
+
4367
+ // takes an AST or js source and returns an AST
4368
+ function prepareAst(src){
4369
+ var tree = (typeof src === 'string') ? parse(src, {loc: true}) : src;
4370
+ return hoister(tree)
4371
+ }
4372
+
4373
+ // evaluate an AST in the given context
4374
+ function evaluateAst(tree, context){
4375
+
4376
+ var safeFunction = FunctionFactory(context);
4377
+ var primitives = primitives_1(context);
4378
+
4379
+ // block scoped context for catch (ex) and 'let'
4380
+ var blockContext = context;
4381
+
4382
+ return walk(tree)
4383
+
4384
+ // recursively walk every node in an array
4385
+ function walkAll(nodes){
4386
+ var result = undefined;
4387
+ for (var i=0;i<nodes.length;i++){
4388
+ var childNode = nodes[i];
4389
+ if (childNode.type === 'EmptyStatement') continue
4390
+ result = walk(childNode);
4391
+ if (result instanceof ReturnValue){
4392
+ return result
4393
+ }
4394
+ }
4395
+ return result
4396
+ }
4397
+
4398
+ // recursively evalutate the node of an AST
4399
+ function walk(node, traceNode){
4400
+ try {
4401
+ if (!node) return
4402
+ switch (node.type) {
4403
+
4404
+ case 'Program':
4405
+ return walkAll(node.body)
4406
+
4407
+ case 'BlockStatement':
4408
+ enterBlock();
4409
+ var result = walkAll(node.body);
4410
+ leaveBlock();
4411
+ return result
4412
+
4413
+ case 'FunctionDeclaration':
4414
+ var params = node.params.map(getName);
4415
+ var value = getFunction(node.body, params, blockContext, node);
4416
+ return context[node.id.name] = value
4417
+
4418
+ case 'FunctionExpression':
4419
+ var params = node.params.map(getName);
4420
+
4421
+ // HACK: trace the function name for stack traces
4422
+ if (!node.id && traceNode && traceNode.key && traceNode.key.type === 'Identifier') {
4423
+ node.id = traceNode.key;
4424
+ }
4425
+
4426
+ return getFunction(node.body, params, blockContext, node)
4427
+
4428
+ case 'ReturnStatement':
4429
+ var value = walk(node.argument);
4430
+ return new ReturnValue('return', value)
4431
+
4432
+ case 'BreakStatement':
4433
+ return new ReturnValue('break')
4434
+
4435
+ case 'ContinueStatement':
4436
+ return new ReturnValue('continue')
4437
+
4438
+ case 'ExpressionStatement':
4439
+ return walk(node.expression)
4440
+
4441
+ case 'AssignmentExpression':
4442
+ return setValue(blockContext, node.left, node.right, node.operator)
4443
+
4444
+ case 'UpdateExpression':
4445
+ return setValue(blockContext, node.argument, null, node.operator)
4446
+
4447
+ case 'VariableDeclaration':
4448
+ node.declarations.forEach(function(declaration){
4449
+ var target = node.kind === 'let' ? blockContext : context;
4450
+ if (declaration.init){
4451
+ target[declaration.id.name] = walk(declaration.init);
4452
+ } else {
4453
+ target[declaration.id.name] = undefined;
4454
+ }
4455
+ });
4456
+ break
4457
+
4458
+ case 'SwitchStatement':
4459
+ var defaultHandler = null;
4460
+ var matched = false;
4461
+ var value = walk(node.discriminant);
4462
+ var result = undefined;
4463
+
4464
+ enterBlock();
4465
+
4466
+ var i = 0;
4467
+ while (result == null){
4468
+ if (i<node.cases.length){
4469
+ if (node.cases[i].test){ // check or fall through
4470
+ matched = matched || (walk(node.cases[i].test) === value);
4471
+ } else if (defaultHandler == null) {
4472
+ defaultHandler = i;
4473
+ }
4474
+ if (matched){
4475
+ var r = walkAll(node.cases[i].consequent);
4476
+ if (r instanceof ReturnValue){ // break out
4477
+ if (r.type == 'break') break
4478
+ result = r;
4479
+ }
4480
+ }
4481
+ i += 1; // continue
4482
+ } else if (!matched && defaultHandler != null){
4483
+ // go back and do the default handler
4484
+ i = defaultHandler;
4485
+ matched = true;
4486
+ } else {
4487
+ // nothing we can do
4488
+ break
4489
+ }
4490
+ }
4491
+
4492
+ leaveBlock();
4493
+ return result
4494
+
4495
+ case 'IfStatement':
4496
+ if (walk(node.test)){
4497
+ return walk(node.consequent)
4498
+ } else if (node.alternate) {
4499
+ return walk(node.alternate)
4500
+ }
4501
+
4502
+ case 'ForStatement':
4503
+ var infinite = infiniteChecker(maxIterations);
4504
+ var result = undefined;
4505
+
4506
+ enterBlock(); // allow lets on delarations
4507
+ for (walk(node.init); walk(node.test); walk(node.update)){
4508
+ var r = walk(node.body);
4509
+
4510
+ // handle early return, continue and break
4511
+ if (r instanceof ReturnValue){
4512
+ if (r.type == 'continue') continue
4513
+ if (r.type == 'break') break
4514
+ result = r;
4515
+ break
4516
+ }
4517
+
4518
+ infinite.check();
4519
+ }
4520
+ leaveBlock();
4521
+ return result
4522
+
4523
+ case 'ForInStatement':
4524
+ var infinite = infiniteChecker(maxIterations);
4525
+ var result = undefined;
4526
+
4527
+ var value = walk(node.right);
4528
+ var property = node.left;
4529
+
4530
+ var target = context;
4531
+ enterBlock();
4532
+
4533
+ if (property.type == 'VariableDeclaration'){
4534
+ walk(property);
4535
+ property = property.declarations[0].id;
4536
+ if (property.kind === 'let'){
4537
+ target = blockContext;
4538
+ }
4539
+ }
4540
+
4541
+ for (var key in value){
4542
+ setValue(target, property, {type: 'Literal', value: key});
4543
+ var r = walk(node.body);
4544
+
4545
+ // handle early return, continue and break
4546
+ if (r instanceof ReturnValue){
4547
+ if (r.type == 'continue') continue
4548
+ if (r.type == 'break') break
4549
+ result = r;
4550
+ break
4551
+ }
4552
+
4553
+ infinite.check();
4554
+ }
4555
+ leaveBlock();
4556
+
4557
+ return result
4558
+
4559
+ case 'WhileStatement':
4560
+ var infinite = infiniteChecker(maxIterations);
4561
+ while (walk(node.test)){
4562
+ walk(node.body);
4563
+ infinite.check();
4564
+ }
4565
+ break
4566
+
4567
+ case 'TryStatement':
4568
+ try {
4569
+ walk(node.block);
4570
+ } catch (error) {
4571
+ enterBlock();
4572
+ var catchClause = node.handlers[0];
4573
+ if (catchClause) {
4574
+ blockContext[catchClause.param.name] = error;
4575
+ walk(catchClause.body);
4576
+ }
4577
+ leaveBlock();
4578
+ } finally {
4579
+ if (node.finalizer) {
4580
+ walk(node.finalizer);
4581
+ }
4582
+ }
4583
+ break
4584
+
4585
+ case 'Literal':
4586
+ return node.value
4587
+
4588
+ case 'UnaryExpression':
4589
+ if (node.operator === 'delete' && node.argument.type === 'MemberExpression') {
4590
+ var arg = node.argument;
4591
+ var parent = walk(arg.object);
4592
+ var prop = arg.computed ? walk(arg.property) : arg.property.name;
4593
+ delete parent[prop];
4594
+ return true
4595
+ } else {
4596
+ var val = walk(node.argument);
4597
+ switch(node.operator) {
4598
+ case '+': return +val
4599
+ case '-': return -val
4600
+ case '~': return ~val
4601
+ case '!': return !val
4602
+ case 'typeof': return typeof val
4603
+ default: return unsupportedExpression(node)
4604
+ }
4605
+ }
4606
+
4607
+ case 'ArrayExpression':
4608
+ var obj = blockContext['Array']();
4609
+ for (var i=0;i<node.elements.length;i++){
4610
+ obj.push(walk(node.elements[i]));
4611
+ }
4612
+ return obj
4613
+
4614
+ case 'ObjectExpression':
4615
+ var obj = blockContext['Object']();
4616
+ for (var i = 0; i < node.properties.length; i++) {
4617
+ var prop = node.properties[i];
4618
+ var value = (prop.value === null) ? prop.value : walk(prop.value, prop);
4619
+ obj[prop.key.value || prop.key.name] = value;
4620
+ }
4621
+ return obj
4622
+
4623
+ case 'NewExpression':
4624
+ var args = node.arguments.map(function(arg){
4625
+ return walk(arg)
4626
+ });
4627
+ var target = walk(node.callee);
4628
+ return primitives.applyNew(target, args)
4629
+
4630
+
4631
+ case 'BinaryExpression':
4632
+ var l = walk(node.left);
4633
+ var r = walk(node.right);
4634
+ switch(node.operator) {
4635
+ case '==': return l == r
4636
+ case '===': return l === r
4637
+ case '!=': return l != r
4638
+ case '!==': return l !== r
4639
+ case '+': return l + r
4640
+ case '-': return l - r
4641
+ case '*': return l * r
4642
+ case '/': return l / r
4643
+ case '%': return l % r
4644
+ case '<': return l < r
4645
+ case '<=': return l <= r
4646
+ case '>': return l > r
4647
+ case '>=': return l >= r
4648
+ case '|': return l | r
4649
+ case '&': return l & r
4650
+ case '^': return l ^ r
4651
+ case 'instanceof': return l instanceof r
4652
+ default: return unsupportedExpression(node)
4653
+ }
4654
+
4655
+ case 'LogicalExpression':
4656
+ switch(node.operator) {
4657
+ case '&&': return walk(node.left) && walk(node.right)
4658
+ case '||': return walk(node.left) || walk(node.right)
4659
+ default: return unsupportedExpression(node)
4660
+ }
4661
+
4662
+ case 'ThisExpression':
4663
+ return blockContext['this']
4664
+
4665
+ case 'Identifier':
4666
+ if (node.name === 'undefined'){
4667
+ return undefined
4668
+ } else if (hasProperty(blockContext, node.name, primitives)){
4669
+ return checkValue(blockContext[node.name])
4670
+ } else {
4671
+ throw new ReferenceError(node.name + ' is not defined')
4672
+ }
4673
+
4674
+ case 'CallExpression':
4675
+ var args = node.arguments.map(function(arg){
4676
+ return walk(arg)
4677
+ });
4678
+ var object = null;
4679
+ var target = walk(node.callee);
4680
+
4681
+ if (node.callee.type === 'MemberExpression'){
4682
+ object = walk(node.callee.object);
4683
+ }
4684
+ return checkValue(target.apply(object, args))
4685
+
4686
+ case 'MemberExpression':
4687
+ var obj = walk(node.object);
4688
+ if (node.computed){
4689
+ var prop = walk(node.property);
4690
+ } else {
4691
+ var prop = node.property.name;
4692
+ }
4693
+ obj = primitives.getPropertyObject(obj, prop);
4694
+ return checkValue(obj[prop]);
4695
+
4696
+ case 'ConditionalExpression':
4697
+ var val = walk(node.test);
4698
+ return val ? walk(node.consequent) : walk(node.alternate)
4699
+
4700
+ case 'EmptyStatement':
4701
+ return
4702
+
4703
+ default:
4704
+ return unsupportedExpression(node)
4705
+ }
4706
+ } catch (ex) {
4707
+ ex.trace = ex.trace || [];
4708
+ ex.trace.push(node);
4709
+ throw ex
4710
+ }
4711
+ }
4712
+
4713
+ // safely retrieve a value
4714
+ function checkValue(value){
4715
+ if (value === Function){
4716
+ value = safeFunction;
4717
+ }
4718
+ return finalValue(value)
4719
+ }
4720
+
4721
+ // block scope context control
4722
+ function enterBlock(){
4723
+ blockContext = Object.create(blockContext);
4724
+ }
4725
+ function leaveBlock(){
4726
+ blockContext = Object.getPrototypeOf(blockContext);
4727
+ }
4728
+
4729
+ // set a value in the specified context if allowed
4730
+ function setValue(object, left, right, operator){
4731
+ var name = null;
4732
+
4733
+ if (left.type === 'Identifier'){
4734
+ name = left.name;
4735
+ // handle parent context shadowing
4736
+ object = objectForKey(object, name, primitives);
4737
+ } else if (left.type === 'MemberExpression'){
4738
+ if (left.computed){
4739
+ name = walk(left.property);
4740
+ } else {
4741
+ name = left.property.name;
4742
+ }
4743
+ object = walk(left.object);
4744
+ }
4745
+
4746
+ // stop built in properties from being able to be changed
4747
+ if (canSetProperty(object, name, primitives)){
4748
+ switch(operator) {
4749
+ case undefined: return object[name] = walk(right)
4750
+ case '=': return object[name] = walk(right)
4751
+ case '+=': return object[name] += walk(right)
4752
+ case '-=': return object[name] -= walk(right)
4753
+ case '++': return object[name]++
4754
+ case '--': return object[name]--
4755
+ }
4756
+ }
4757
+
4758
+ }
4759
+
4760
+ }
4761
+
4762
+ // when an unsupported expression is encountered, throw an error
4763
+ function unsupportedExpression(node){
4764
+ console.error(node);
4765
+ var err = new Error('Unsupported expression: ' + node.type);
4766
+ err.node = node;
4767
+ throw err
4768
+ }
4769
+
4770
+ // walk a provided object's prototypal hierarchy to retrieve an inherited object
4771
+ function objectForKey(object, key, primitives){
4772
+ var proto = primitives.getPrototypeOf(object);
4773
+ if (!proto || hasOwnProperty(object, key)){
4774
+ return object
4775
+ } else {
4776
+ return objectForKey(proto, key, primitives)
4777
+ }
4778
+ }
4779
+
4780
+ function hasProperty(object, key, primitives){
4781
+ var proto = primitives.getPrototypeOf(object);
4782
+ var hasOwn = hasOwnProperty(object, key);
4783
+ if (object[key] !== undefined){
4784
+ return true
4785
+ } else if (!proto || hasOwn){
4786
+ return hasOwn
4787
+ } else {
4788
+ return hasProperty(proto, key, primitives)
4789
+ }
4790
+ }
4791
+
4792
+ function hasOwnProperty(object, key){
4793
+ return Object.prototype.hasOwnProperty.call(object, key)
4794
+ }
4795
+
4796
+ function propertyIsEnumerable(object, key){
4797
+ return Object.prototype.propertyIsEnumerable.call(object, key)
4798
+ }
4799
+
4800
+
4801
+ // determine if we have write access to a property
4802
+ function canSetProperty(object, property, primitives){
4803
+ if (property === '__proto__' || primitives.isPrimitive(object)){
4804
+ return false
4805
+ } else if (object != null){
4806
+
4807
+ if (hasOwnProperty(object, property)){
4808
+ if (propertyIsEnumerable(object, property)){
4809
+ return true
4810
+ } else {
4811
+ return false
4812
+ }
4813
+ } else {
4814
+ return canSetProperty(primitives.getPrototypeOf(object), property, primitives)
4815
+ }
4816
+
4817
+ } else {
4818
+ return true
4819
+ }
4820
+ }
4821
+
4822
+ // generate a function with specified context
4823
+ function getFunction(body, params, parentContext, traceNode){
4824
+ return function(){
4825
+ try {
4826
+ var context = Object.create(parentContext);
4827
+ if (this == commonjsGlobal){
4828
+ context['this'] = null;
4829
+ } else {
4830
+ context['this'] = this;
4831
+ }
4832
+ // normalize arguments array
4833
+ var args = Array.prototype.slice.call(arguments);
4834
+ context['arguments'] = arguments;
4835
+ args.forEach(function(arg,idx){
4836
+ var param = params[idx];
4837
+ if (param){
4838
+ context[param] = arg;
4839
+ }
4840
+ });
4841
+ var result = evaluateAst(body, context);
4842
+
4843
+ if (result instanceof ReturnValue){
4844
+ return result.value
4845
+ }
4846
+ } catch (ex) {
4847
+ ex.trace = ex.trace || [];
4848
+ ex.trace.push(traceNode);
4849
+ throw ex
4850
+ }
4851
+ }
4852
+ }
4853
+
4854
+ function finalValue(value){
4855
+ if (value instanceof ReturnValue){
4856
+ return value.value
4857
+ }
4858
+ return value
4859
+ }
4860
+
4861
+ // get the name of an identifier
4862
+ function getName(identifier){
4863
+ return identifier.name
4864
+ }
4865
+
4866
+ // a ReturnValue struct for differentiating between expression result and return statement
4867
+ function ReturnValue(type, value){
4868
+ this.type = type;
4869
+ this.value = value;
4870
+ }
4871
+ notevil.eval = _eval;
4872
+ notevil.FunctionFactory = FunctionFactory_1;
4873
+ notevil.Function = _Function;var __clone = function __clone(data) {
190
4874
  return JSON.parse(JSON.stringify(data));
191
4875
  };
192
4876
 
@@ -253,9 +4937,9 @@ var __applyRefProps = function __applyRefProps(input, refConfig) {
253
4937
  };
254
4938
 
255
4939
  var fcInputs = {
256
- text: {
4940
+ string: {
257
4941
  text: 'Текст',
258
- name: 'text',
4942
+ name: 'string',
259
4943
  type: 'b-form-input',
260
4944
  props: {
261
4945
  placeholder: {
@@ -335,9 +5019,9 @@ var fcInputs = {
335
5019
  }
336
5020
  };
337
5021
  var fcPrimitiveInputs = {
338
- text: [__clone(fcInputs.text), __clone(fcInputs.number), __clone(fcInputs.memo), __clone(fcInputs.phone)],
339
- memo: [__clone(fcInputs.memo), __clone(fcInputs.text)],
340
- number: [__clone(fcInputs.text), __clone(fcInputs.number)],
5022
+ string: [__clone(fcInputs.string), __clone(fcInputs.number), __clone(fcInputs.memo), __clone(fcInputs.phone)],
5023
+ memo: [__clone(fcInputs.memo), __clone(fcInputs.string)],
5024
+ number: [__clone(fcInputs.string), __clone(fcInputs.number)],
341
5025
  date: [__clone(fcInputs.date)]
342
5026
  };
343
5027
  var fcDictInputs = [];
@@ -469,10 +5153,16 @@ var UtFormConstructor = {
469
5153
  return this.config.ruleContext;
470
5154
  },
471
5155
  runRule: function runRule(context, script) {
472
- var appendScript = "\n var doc = this.form.doc;\n var form = this.form;\n var event = this.event;\n var eventName = this.eventName;\n \n ";
5156
+ context = context ? context : {};
5157
+ context['console'] = console;
5158
+ var appendScript = '';
5159
+ /*for(let v in context) {
5160
+ appendScript = appendScript + `var ${v} = this.${v};\n`;
5161
+ }
5162
+ appendScript = appendScript? appendScript + '\n': '';*/
473
5163
 
474
5164
  var func = function func(script) {
475
- return eval(script);
5165
+ return notevil(script, context);
476
5166
  };
477
5167
 
478
5168
  func.call(context, appendScript + script);
@@ -696,21 +5386,7 @@ var __vue_component__$6 = /*#__PURE__*/normalizeComponent({
696
5386
  staticRenderFns: __vue_staticRenderFns__$5
697
5387
  }, __vue_inject_styles__$5, __vue_script__$5, __vue_scope_id__$5, __vue_is_functional_template__$5, __vue_module_identifier__$5, false, undefined, undefined, undefined);
698
5388
 
699
- var DocTemplateSectionModal = __vue_component__$6;var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
700
-
701
- function createCommonjsModule(fn, basedir, module) {
702
- return module = {
703
- path: basedir,
704
- exports: {},
705
- require: function (path, base) {
706
- return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
707
- }
708
- }, fn(module, module.exports), module.exports;
709
- }
710
-
711
- function commonjsRequire () {
712
- throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
713
- }/**!
5389
+ var DocTemplateSectionModal = __vue_component__$6;/**!
714
5390
  * Sortable 1.10.2
715
5391
  * @author RubaXa <trash@rubaxa.org>
716
5392
  * @author owenm <owen23355@gmail.com>
@@ -6336,6 +11012,7 @@ var script$3 = {
6336
11012
  if (rule.event === eventName && rule.script) {
6337
11013
  var ruleContext = UtFormConstructor.getRuleContext();
6338
11014
  ruleContext.form = _this;
11015
+ ruleContext.doc = _this.doc;
6339
11016
  ruleContext.event = event;
6340
11017
  ruleContext.eventName = eventName;
6341
11018
  UtFormConstructor.runRule(ruleContext, rule.script);
@@ -6434,7 +11111,7 @@ var __vue_inject_styles__$3 = undefined;
6434
11111
  var __vue_scope_id__$3 = undefined;
6435
11112
  /* module identifier */
6436
11113
 
6437
- var __vue_module_identifier__$3 = "data-v-a98da412";
11114
+ var __vue_module_identifier__$3 = "data-v-c3fb224c";
6438
11115
  /* functional template */
6439
11116
 
6440
11117
  var __vue_is_functional_template__$3 = false;
@@ -6556,18 +11233,23 @@ var script$2 = {
6556
11233
  this.innerRule = null;
6557
11234
  },
6558
11235
  addVariableToScript: function addVariableToScript(varName) {
6559
- var r = this.innerRule;
6560
- r.script = r.script ? r.script + varName : varName;
11236
+ var caretPosition = this.$refs.scriptInput.selectionStart;
11237
+ this.insertTextToScript(varName, caretPosition);
6561
11238
  },
6562
11239
  addSetVariableToScript: function addSetVariableToScript(field) {
6563
- var r = this.innerRule;
6564
- var setVariableScript = "doc['".concat(field.name, "'] = \u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435;");
6565
- r.script = r.script ? r.script + setVariableScript : setVariableScript;
11240
+ var caretPosition = this.$refs.scriptInput.selectionStart;
11241
+ this.insertTextToScript("doc['".concat(field.name, "'] = \u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435;"), caretPosition);
6566
11242
  },
6567
11243
  addCallInputFunction: function addCallInputFunction(field) {
6568
- var r = this.innerRule;
6569
- var setVariableScript = "form.$refs['".concat(field.name, "'][0].\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u0444\u0443\u043D\u043A\u0446\u0438\u0438();");
6570
- r.script = r.script ? r.script + setVariableScript : setVariableScript;
11244
+ var caretPosition = this.$refs.scriptInput.selectionStart;
11245
+ this.insertTextToScript("form.$refs['".concat(field.name, "'][0].\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u0444\u0443\u043D\u043A\u0446\u0438\u0438();"), caretPosition);
11246
+ },
11247
+ insertTextToScript: function insertTextToScript(text, position) {
11248
+ position = position != null ? position : this.script.length;
11249
+ this.innerRule.script = this.innerRule.script != null ? this.innerRule.script : '';
11250
+ var scriptSplit = this.innerRule.script.split('');
11251
+ scriptSplit.splice(position, 0, text);
11252
+ this.innerRule.script = scriptSplit.join('');
6571
11253
  },
6572
11254
  onRuleSelected: function onRuleSelected(ruleName) {
6573
11255
  var rule = this.rulePresets.find(function (rule) {
@@ -6670,7 +11352,7 @@ var __vue_render__$2 = function __vue_render__() {
6670
11352
  attrs: {
6671
11353
  "value": r.name
6672
11354
  }
6673
- }, [_vm._v("\n " + _vm._s(r.name) + "\n ")]);
11355
+ }, [_vm._v("\n " + _vm._s(r.labelRu) + " (" + _vm._s(r.name) + ")\n ")]);
6674
11356
  }), 1)], 1) : _vm._e()], 1), _vm._v(" "), _c('b-col', {
6675
11357
  attrs: {
6676
11358
  "lg": "12"
@@ -6794,7 +11476,7 @@ var __vue_render__$2 = function __vue_render__() {
6794
11476
  return _vm.addSetVariableToScript(f);
6795
11477
  }
6796
11478
  }
6797
- }, [_vm._v("\n " + _vm._s(f.name) + "\n ")]) : _vm._e();
11479
+ }, [_vm._v("\n " + _vm._s(f.labelRu) + " (" + _vm._s(f.name) + ")\n ")]) : _vm._e();
6798
11480
  }), 1), _vm._v(" "), _c('b-dropdown', {
6799
11481
  staticClass: "mx-1",
6800
11482
  attrs: {
@@ -6810,8 +11492,9 @@ var __vue_render__$2 = function __vue_render__() {
6810
11492
  return _vm.addCallInputFunction(f);
6811
11493
  }
6812
11494
  }
6813
- }, [_vm._v("\n " + _vm._s(f.name) + "\n ")]) : _vm._e();
11495
+ }, [_vm._v("\n " + _vm._s(f.labelRu) + " (" + _vm._s(f.name) + ")\n ")]) : _vm._e();
6814
11496
  }), 1)], 1), _vm._v(" "), _c('b-form-textarea', {
11497
+ ref: "scriptInput",
6815
11498
  attrs: {
6816
11499
  "state": _vm.state.script,
6817
11500
  "invalid-feedback": _vm.state.script_feedback,
@@ -6844,7 +11527,7 @@ var __vue_inject_styles__$2 = undefined;
6844
11527
  var __vue_scope_id__$2 = undefined;
6845
11528
  /* module identifier */
6846
11529
 
6847
- var __vue_module_identifier__$2 = "data-v-3509eca6";
11530
+ var __vue_module_identifier__$2 = "data-v-3e86235a";
6848
11531
  /* functional template */
6849
11532
 
6850
11533
  var __vue_is_functional_template__$2 = false;