rb-document-form-constructor 0.8.73 → 0.8.75

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.
@@ -1,4 +1,5 @@
1
1
  import Vue$1 from 'vue';
2
+ import crypto from 'crypto';
2
3
 
3
4
  const UtFormConfig = {
4
5
  findField(fieldName, formConfig) {
@@ -86,6 +87,4702 @@ const UtFormConfig = {
86
87
  }
87
88
  };
88
89
 
90
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
91
+
92
+ function createCommonjsModule(fn, basedir, module) {
93
+ return module = {
94
+ path: basedir,
95
+ exports: {},
96
+ require: function (path, base) {
97
+ return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
98
+ }
99
+ }, fn(module, module.exports), module.exports;
100
+ }
101
+
102
+ function commonjsRequire () {
103
+ throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
104
+ }
105
+
106
+ var esprima = createCommonjsModule(function (module, exports) {
107
+ /*
108
+ Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
109
+ Copyright (C) 2012 Mathias Bynens <mathias@qiwi.be>
110
+ Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
111
+ Copyright (C) 2012 Kris Kowal <kris.kowal@cixar.com>
112
+ Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
113
+ Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
114
+ Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
115
+
116
+ Redistribution and use in source and binary forms, with or without
117
+ modification, are permitted provided that the following conditions are met:
118
+
119
+ * Redistributions of source code must retain the above copyright
120
+ notice, this list of conditions and the following disclaimer.
121
+ * Redistributions in binary form must reproduce the above copyright
122
+ notice, this list of conditions and the following disclaimer in the
123
+ documentation and/or other materials provided with the distribution.
124
+
125
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
126
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
127
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
128
+ ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
129
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
130
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
131
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
132
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
133
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
134
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135
+ */
136
+
137
+ /*jslint bitwise:true plusplus:true */
138
+ /*global esprima:true, define:true, exports:true, window: true,
139
+ throwError: true, createLiteral: true, generateStatement: true,
140
+ parseAssignmentExpression: true, parseBlock: true, parseExpression: true,
141
+ parseFunctionDeclaration: true, parseFunctionExpression: true,
142
+ parseFunctionSourceElements: true, parseVariableIdentifier: true,
143
+ parseLeftHandSideExpression: true,
144
+ parseStatement: true, parseSourceElement: true */
145
+
146
+ (function (root, factory) {
147
+
148
+ // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
149
+ // Rhino, and plain browser loading.
150
+ {
151
+ factory(exports);
152
+ }
153
+ }(commonjsGlobal, function (exports) {
154
+
155
+ var Token,
156
+ TokenName,
157
+ Syntax,
158
+ PropertyKind,
159
+ Messages,
160
+ Regex,
161
+ source,
162
+ strict,
163
+ index,
164
+ lineNumber,
165
+ lineStart,
166
+ length,
167
+ buffer,
168
+ state,
169
+ extra;
170
+
171
+ Token = {
172
+ BooleanLiteral: 1,
173
+ EOF: 2,
174
+ Identifier: 3,
175
+ Keyword: 4,
176
+ NullLiteral: 5,
177
+ NumericLiteral: 6,
178
+ Punctuator: 7,
179
+ StringLiteral: 8
180
+ };
181
+
182
+ TokenName = {};
183
+ TokenName[Token.BooleanLiteral] = 'Boolean';
184
+ TokenName[Token.EOF] = '<end>';
185
+ TokenName[Token.Identifier] = 'Identifier';
186
+ TokenName[Token.Keyword] = 'Keyword';
187
+ TokenName[Token.NullLiteral] = 'Null';
188
+ TokenName[Token.NumericLiteral] = 'Numeric';
189
+ TokenName[Token.Punctuator] = 'Punctuator';
190
+ TokenName[Token.StringLiteral] = 'String';
191
+
192
+ Syntax = {
193
+ AssignmentExpression: 'AssignmentExpression',
194
+ ArrayExpression: 'ArrayExpression',
195
+ BlockStatement: 'BlockStatement',
196
+ BinaryExpression: 'BinaryExpression',
197
+ BreakStatement: 'BreakStatement',
198
+ CallExpression: 'CallExpression',
199
+ CatchClause: 'CatchClause',
200
+ ConditionalExpression: 'ConditionalExpression',
201
+ ContinueStatement: 'ContinueStatement',
202
+ DoWhileStatement: 'DoWhileStatement',
203
+ DebuggerStatement: 'DebuggerStatement',
204
+ EmptyStatement: 'EmptyStatement',
205
+ ExpressionStatement: 'ExpressionStatement',
206
+ ForStatement: 'ForStatement',
207
+ ForInStatement: 'ForInStatement',
208
+ FunctionDeclaration: 'FunctionDeclaration',
209
+ FunctionExpression: 'FunctionExpression',
210
+ Identifier: 'Identifier',
211
+ IfStatement: 'IfStatement',
212
+ Literal: 'Literal',
213
+ LabeledStatement: 'LabeledStatement',
214
+ LogicalExpression: 'LogicalExpression',
215
+ MemberExpression: 'MemberExpression',
216
+ NewExpression: 'NewExpression',
217
+ ObjectExpression: 'ObjectExpression',
218
+ Program: 'Program',
219
+ Property: 'Property',
220
+ ReturnStatement: 'ReturnStatement',
221
+ SequenceExpression: 'SequenceExpression',
222
+ SwitchStatement: 'SwitchStatement',
223
+ SwitchCase: 'SwitchCase',
224
+ ThisExpression: 'ThisExpression',
225
+ ThrowStatement: 'ThrowStatement',
226
+ TryStatement: 'TryStatement',
227
+ UnaryExpression: 'UnaryExpression',
228
+ UpdateExpression: 'UpdateExpression',
229
+ VariableDeclaration: 'VariableDeclaration',
230
+ VariableDeclarator: 'VariableDeclarator',
231
+ WhileStatement: 'WhileStatement',
232
+ WithStatement: 'WithStatement'
233
+ };
234
+
235
+ PropertyKind = {
236
+ Data: 1,
237
+ Get: 2,
238
+ Set: 4
239
+ };
240
+
241
+ // Error messages should be identical to V8.
242
+ Messages = {
243
+ UnexpectedToken: 'Unexpected token %0',
244
+ UnexpectedNumber: 'Unexpected number',
245
+ UnexpectedString: 'Unexpected string',
246
+ UnexpectedIdentifier: 'Unexpected identifier',
247
+ UnexpectedReserved: 'Unexpected reserved word',
248
+ UnexpectedEOS: 'Unexpected end of input',
249
+ NewlineAfterThrow: 'Illegal newline after throw',
250
+ InvalidRegExp: 'Invalid regular expression',
251
+ UnterminatedRegExp: 'Invalid regular expression: missing /',
252
+ InvalidLHSInAssignment: 'Invalid left-hand side in assignment',
253
+ InvalidLHSInForIn: 'Invalid left-hand side in for-in',
254
+ MultipleDefaultsInSwitch: 'More than one default clause in switch statement',
255
+ NoCatchOrFinally: 'Missing catch or finally after try',
256
+ UnknownLabel: 'Undefined label \'%0\'',
257
+ Redeclaration: '%0 \'%1\' has already been declared',
258
+ IllegalContinue: 'Illegal continue statement',
259
+ IllegalBreak: 'Illegal break statement',
260
+ IllegalReturn: 'Illegal return statement',
261
+ StrictModeWith: 'Strict mode code may not include a with statement',
262
+ StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',
263
+ StrictVarName: 'Variable name may not be eval or arguments in strict mode',
264
+ StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',
265
+ StrictParamDupe: 'Strict mode function may not have duplicate parameter names',
266
+ StrictFunctionName: 'Function name may not be eval or arguments in strict mode',
267
+ StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',
268
+ StrictDelete: 'Delete of an unqualified identifier in strict mode.',
269
+ StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',
270
+ AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',
271
+ AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',
272
+ StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',
273
+ StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',
274
+ StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',
275
+ StrictReservedWord: 'Use of future reserved word in strict mode'
276
+ };
277
+
278
+ // See also tools/generate-unicode-regex.py.
279
+ Regex = {
280
+ 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]'),
281
+ 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]')
282
+ };
283
+
284
+ // Ensure the condition is true, otherwise throw an error.
285
+ // This is only to have a better contract semantic, i.e. another safety net
286
+ // to catch a logic error. The condition shall be fulfilled in normal case.
287
+ // Do NOT use this to enforce a certain condition on any user input.
288
+
289
+ function assert(condition, message) {
290
+ if (!condition) {
291
+ throw new Error('ASSERT: ' + message);
292
+ }
293
+ }
294
+
295
+ function sliceSource(from, to) {
296
+ return source.slice(from, to);
297
+ }
298
+
299
+ if (typeof 'esprima'[0] === 'undefined') {
300
+ sliceSource = function sliceArraySource(from, to) {
301
+ return source.slice(from, to).join('');
302
+ };
303
+ }
304
+
305
+ function isDecimalDigit(ch) {
306
+ return '0123456789'.indexOf(ch) >= 0;
307
+ }
308
+
309
+ function isHexDigit(ch) {
310
+ return '0123456789abcdefABCDEF'.indexOf(ch) >= 0;
311
+ }
312
+
313
+ function isOctalDigit(ch) {
314
+ return '01234567'.indexOf(ch) >= 0;
315
+ }
316
+
317
+
318
+ // 7.2 White Space
319
+
320
+ function isWhiteSpace(ch) {
321
+ return (ch === ' ') || (ch === '\u0009') || (ch === '\u000B') ||
322
+ (ch === '\u000C') || (ch === '\u00A0') ||
323
+ (ch.charCodeAt(0) >= 0x1680 &&
324
+ '\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\uFEFF'.indexOf(ch) >= 0);
325
+ }
326
+
327
+ // 7.3 Line Terminators
328
+
329
+ function isLineTerminator(ch) {
330
+ return (ch === '\n' || ch === '\r' || ch === '\u2028' || ch === '\u2029');
331
+ }
332
+
333
+ // 7.6 Identifier Names and Identifiers
334
+
335
+ function isIdentifierStart(ch) {
336
+ return (ch === '$') || (ch === '_') || (ch === '\\') ||
337
+ (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
338
+ ((ch.charCodeAt(0) >= 0x80) && Regex.NonAsciiIdentifierStart.test(ch));
339
+ }
340
+
341
+ function isIdentifierPart(ch) {
342
+ return (ch === '$') || (ch === '_') || (ch === '\\') ||
343
+ (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
344
+ ((ch >= '0') && (ch <= '9')) ||
345
+ ((ch.charCodeAt(0) >= 0x80) && Regex.NonAsciiIdentifierPart.test(ch));
346
+ }
347
+
348
+ // 7.6.1.2 Future Reserved Words
349
+
350
+ function isFutureReservedWord(id) {
351
+ switch (id) {
352
+
353
+ // Future reserved words.
354
+ case 'class':
355
+ case 'enum':
356
+ case 'export':
357
+ case 'extends':
358
+ case 'import':
359
+ case 'super':
360
+ return true;
361
+ }
362
+
363
+ return false;
364
+ }
365
+
366
+ function isStrictModeReservedWord(id) {
367
+ switch (id) {
368
+
369
+ // Strict Mode reserved words.
370
+ case 'implements':
371
+ case 'interface':
372
+ case 'package':
373
+ case 'private':
374
+ case 'protected':
375
+ case 'public':
376
+ case 'static':
377
+ case 'yield':
378
+ case 'let':
379
+ return true;
380
+ }
381
+
382
+ return false;
383
+ }
384
+
385
+ function isRestrictedWord(id) {
386
+ return id === 'eval' || id === 'arguments';
387
+ }
388
+
389
+ // 7.6.1.1 Keywords
390
+
391
+ function isKeyword(id) {
392
+ var keyword = false;
393
+ switch (id.length) {
394
+ case 2:
395
+ keyword = (id === 'if') || (id === 'in') || (id === 'do');
396
+ break;
397
+ case 3:
398
+ keyword = (id === 'var') || (id === 'for') || (id === 'new') || (id === 'try');
399
+ break;
400
+ case 4:
401
+ keyword = (id === 'this') || (id === 'else') || (id === 'case') || (id === 'void') || (id === 'with');
402
+ break;
403
+ case 5:
404
+ keyword = (id === 'while') || (id === 'break') || (id === 'catch') || (id === 'throw');
405
+ break;
406
+ case 6:
407
+ keyword = (id === 'return') || (id === 'typeof') || (id === 'delete') || (id === 'switch');
408
+ break;
409
+ case 7:
410
+ keyword = (id === 'default') || (id === 'finally');
411
+ break;
412
+ case 8:
413
+ keyword = (id === 'function') || (id === 'continue') || (id === 'debugger');
414
+ break;
415
+ case 10:
416
+ keyword = (id === 'instanceof');
417
+ break;
418
+ }
419
+
420
+ if (keyword) {
421
+ return true;
422
+ }
423
+
424
+ switch (id) {
425
+ // Future reserved words.
426
+ // 'const' is specialized as Keyword in V8.
427
+ case 'const':
428
+ return true;
429
+
430
+ // For compatiblity to SpiderMonkey and ES.next
431
+ case 'yield':
432
+ case 'let':
433
+ return true;
434
+ }
435
+
436
+ if (strict && isStrictModeReservedWord(id)) {
437
+ return true;
438
+ }
439
+
440
+ return isFutureReservedWord(id);
441
+ }
442
+
443
+ // 7.4 Comments
444
+
445
+ function skipComment() {
446
+ var ch, blockComment, lineComment;
447
+
448
+ blockComment = false;
449
+ lineComment = false;
450
+
451
+ while (index < length) {
452
+ ch = source[index];
453
+
454
+ if (lineComment) {
455
+ ch = source[index++];
456
+ if (isLineTerminator(ch)) {
457
+ lineComment = false;
458
+ if (ch === '\r' && source[index] === '\n') {
459
+ ++index;
460
+ }
461
+ ++lineNumber;
462
+ lineStart = index;
463
+ }
464
+ } else if (blockComment) {
465
+ if (isLineTerminator(ch)) {
466
+ if (ch === '\r' && source[index + 1] === '\n') {
467
+ ++index;
468
+ }
469
+ ++lineNumber;
470
+ ++index;
471
+ lineStart = index;
472
+ if (index >= length) {
473
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
474
+ }
475
+ } else {
476
+ ch = source[index++];
477
+ if (index >= length) {
478
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
479
+ }
480
+ if (ch === '*') {
481
+ ch = source[index];
482
+ if (ch === '/') {
483
+ ++index;
484
+ blockComment = false;
485
+ }
486
+ }
487
+ }
488
+ } else if (ch === '/') {
489
+ ch = source[index + 1];
490
+ if (ch === '/') {
491
+ index += 2;
492
+ lineComment = true;
493
+ } else if (ch === '*') {
494
+ index += 2;
495
+ blockComment = true;
496
+ if (index >= length) {
497
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
498
+ }
499
+ } else {
500
+ break;
501
+ }
502
+ } else if (isWhiteSpace(ch)) {
503
+ ++index;
504
+ } else if (isLineTerminator(ch)) {
505
+ ++index;
506
+ if (ch === '\r' && source[index] === '\n') {
507
+ ++index;
508
+ }
509
+ ++lineNumber;
510
+ lineStart = index;
511
+ } else {
512
+ break;
513
+ }
514
+ }
515
+ }
516
+
517
+ function scanHexEscape(prefix) {
518
+ var i, len, ch, code = 0;
519
+
520
+ len = (prefix === 'u') ? 4 : 2;
521
+ for (i = 0; i < len; ++i) {
522
+ if (index < length && isHexDigit(source[index])) {
523
+ ch = source[index++];
524
+ code = code * 16 + '0123456789abcdef'.indexOf(ch.toLowerCase());
525
+ } else {
526
+ return '';
527
+ }
528
+ }
529
+ return String.fromCharCode(code);
530
+ }
531
+
532
+ function scanIdentifier() {
533
+ var ch, start, id, restore;
534
+
535
+ ch = source[index];
536
+ if (!isIdentifierStart(ch)) {
537
+ return;
538
+ }
539
+
540
+ start = index;
541
+ if (ch === '\\') {
542
+ ++index;
543
+ if (source[index] !== 'u') {
544
+ return;
545
+ }
546
+ ++index;
547
+ restore = index;
548
+ ch = scanHexEscape('u');
549
+ if (ch) {
550
+ if (ch === '\\' || !isIdentifierStart(ch)) {
551
+ return;
552
+ }
553
+ id = ch;
554
+ } else {
555
+ index = restore;
556
+ id = 'u';
557
+ }
558
+ } else {
559
+ id = source[index++];
560
+ }
561
+
562
+ while (index < length) {
563
+ ch = source[index];
564
+ if (!isIdentifierPart(ch)) {
565
+ break;
566
+ }
567
+ if (ch === '\\') {
568
+ ++index;
569
+ if (source[index] !== 'u') {
570
+ return;
571
+ }
572
+ ++index;
573
+ restore = index;
574
+ ch = scanHexEscape('u');
575
+ if (ch) {
576
+ if (ch === '\\' || !isIdentifierPart(ch)) {
577
+ return;
578
+ }
579
+ id += ch;
580
+ } else {
581
+ index = restore;
582
+ id += 'u';
583
+ }
584
+ } else {
585
+ id += source[index++];
586
+ }
587
+ }
588
+
589
+ // There is no keyword or literal with only one character.
590
+ // Thus, it must be an identifier.
591
+ if (id.length === 1) {
592
+ return {
593
+ type: Token.Identifier,
594
+ value: id,
595
+ lineNumber: lineNumber,
596
+ lineStart: lineStart,
597
+ range: [start, index]
598
+ };
599
+ }
600
+
601
+ if (isKeyword(id)) {
602
+ return {
603
+ type: Token.Keyword,
604
+ value: id,
605
+ lineNumber: lineNumber,
606
+ lineStart: lineStart,
607
+ range: [start, index]
608
+ };
609
+ }
610
+
611
+ // 7.8.1 Null Literals
612
+
613
+ if (id === 'null') {
614
+ return {
615
+ type: Token.NullLiteral,
616
+ value: id,
617
+ lineNumber: lineNumber,
618
+ lineStart: lineStart,
619
+ range: [start, index]
620
+ };
621
+ }
622
+
623
+ // 7.8.2 Boolean Literals
624
+
625
+ if (id === 'true' || id === 'false') {
626
+ return {
627
+ type: Token.BooleanLiteral,
628
+ value: id,
629
+ lineNumber: lineNumber,
630
+ lineStart: lineStart,
631
+ range: [start, index]
632
+ };
633
+ }
634
+
635
+ return {
636
+ type: Token.Identifier,
637
+ value: id,
638
+ lineNumber: lineNumber,
639
+ lineStart: lineStart,
640
+ range: [start, index]
641
+ };
642
+ }
643
+
644
+ // 7.7 Punctuators
645
+
646
+ function scanPunctuator() {
647
+ var start = index,
648
+ ch1 = source[index],
649
+ ch2,
650
+ ch3,
651
+ ch4;
652
+
653
+ // Check for most common single-character punctuators.
654
+
655
+ if (ch1 === ';' || ch1 === '{' || ch1 === '}') {
656
+ ++index;
657
+ return {
658
+ type: Token.Punctuator,
659
+ value: ch1,
660
+ lineNumber: lineNumber,
661
+ lineStart: lineStart,
662
+ range: [start, index]
663
+ };
664
+ }
665
+
666
+ if (ch1 === ',' || ch1 === '(' || ch1 === ')') {
667
+ ++index;
668
+ return {
669
+ type: Token.Punctuator,
670
+ value: ch1,
671
+ lineNumber: lineNumber,
672
+ lineStart: lineStart,
673
+ range: [start, index]
674
+ };
675
+ }
676
+
677
+ // Dot (.) can also start a floating-point number, hence the need
678
+ // to check the next character.
679
+
680
+ ch2 = source[index + 1];
681
+ if (ch1 === '.' && !isDecimalDigit(ch2)) {
682
+ return {
683
+ type: Token.Punctuator,
684
+ value: source[index++],
685
+ lineNumber: lineNumber,
686
+ lineStart: lineStart,
687
+ range: [start, index]
688
+ };
689
+ }
690
+
691
+ // Peek more characters.
692
+
693
+ ch3 = source[index + 2];
694
+ ch4 = source[index + 3];
695
+
696
+ // 4-character punctuator: >>>=
697
+
698
+ if (ch1 === '>' && ch2 === '>' && ch3 === '>') {
699
+ if (ch4 === '=') {
700
+ index += 4;
701
+ return {
702
+ type: Token.Punctuator,
703
+ value: '>>>=',
704
+ lineNumber: lineNumber,
705
+ lineStart: lineStart,
706
+ range: [start, index]
707
+ };
708
+ }
709
+ }
710
+
711
+ // 3-character punctuators: === !== >>> <<= >>=
712
+
713
+ if (ch1 === '=' && ch2 === '=' && ch3 === '=') {
714
+ index += 3;
715
+ return {
716
+ type: Token.Punctuator,
717
+ value: '===',
718
+ lineNumber: lineNumber,
719
+ lineStart: lineStart,
720
+ range: [start, index]
721
+ };
722
+ }
723
+
724
+ if (ch1 === '!' && ch2 === '=' && ch3 === '=') {
725
+ index += 3;
726
+ return {
727
+ type: Token.Punctuator,
728
+ value: '!==',
729
+ lineNumber: lineNumber,
730
+ lineStart: lineStart,
731
+ range: [start, index]
732
+ };
733
+ }
734
+
735
+ if (ch1 === '>' && ch2 === '>' && ch3 === '>') {
736
+ index += 3;
737
+ return {
738
+ type: Token.Punctuator,
739
+ value: '>>>',
740
+ lineNumber: lineNumber,
741
+ lineStart: lineStart,
742
+ range: [start, index]
743
+ };
744
+ }
745
+
746
+ if (ch1 === '<' && ch2 === '<' && ch3 === '=') {
747
+ index += 3;
748
+ return {
749
+ type: Token.Punctuator,
750
+ value: '<<=',
751
+ lineNumber: lineNumber,
752
+ lineStart: lineStart,
753
+ range: [start, index]
754
+ };
755
+ }
756
+
757
+ if (ch1 === '>' && ch2 === '>' && ch3 === '=') {
758
+ index += 3;
759
+ return {
760
+ type: Token.Punctuator,
761
+ value: '>>=',
762
+ lineNumber: lineNumber,
763
+ lineStart: lineStart,
764
+ range: [start, index]
765
+ };
766
+ }
767
+
768
+ // 2-character punctuators: <= >= == != ++ -- << >> && ||
769
+ // += -= *= %= &= |= ^= /=
770
+
771
+ if (ch2 === '=') {
772
+ if ('<>=!+-*%&|^/'.indexOf(ch1) >= 0) {
773
+ index += 2;
774
+ return {
775
+ type: Token.Punctuator,
776
+ value: ch1 + ch2,
777
+ lineNumber: lineNumber,
778
+ lineStart: lineStart,
779
+ range: [start, index]
780
+ };
781
+ }
782
+ }
783
+
784
+ if (ch1 === ch2 && ('+-<>&|'.indexOf(ch1) >= 0)) {
785
+ if ('+-<>&|'.indexOf(ch2) >= 0) {
786
+ index += 2;
787
+ return {
788
+ type: Token.Punctuator,
789
+ value: ch1 + ch2,
790
+ lineNumber: lineNumber,
791
+ lineStart: lineStart,
792
+ range: [start, index]
793
+ };
794
+ }
795
+ }
796
+
797
+ // The remaining 1-character punctuators.
798
+
799
+ if ('[]<>+-*%&|^!~?:=/'.indexOf(ch1) >= 0) {
800
+ return {
801
+ type: Token.Punctuator,
802
+ value: source[index++],
803
+ lineNumber: lineNumber,
804
+ lineStart: lineStart,
805
+ range: [start, index]
806
+ };
807
+ }
808
+ }
809
+
810
+ // 7.8.3 Numeric Literals
811
+
812
+ function scanNumericLiteral() {
813
+ var number, start, ch;
814
+
815
+ ch = source[index];
816
+ assert(isDecimalDigit(ch) || (ch === '.'),
817
+ 'Numeric literal must start with a decimal digit or a decimal point');
818
+
819
+ start = index;
820
+ number = '';
821
+ if (ch !== '.') {
822
+ number = source[index++];
823
+ ch = source[index];
824
+
825
+ // Hex number starts with '0x'.
826
+ // Octal number starts with '0'.
827
+ if (number === '0') {
828
+ if (ch === 'x' || ch === 'X') {
829
+ number += source[index++];
830
+ while (index < length) {
831
+ ch = source[index];
832
+ if (!isHexDigit(ch)) {
833
+ break;
834
+ }
835
+ number += source[index++];
836
+ }
837
+
838
+ if (number.length <= 2) {
839
+ // only 0x
840
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
841
+ }
842
+
843
+ if (index < length) {
844
+ ch = source[index];
845
+ if (isIdentifierStart(ch)) {
846
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
847
+ }
848
+ }
849
+ return {
850
+ type: Token.NumericLiteral,
851
+ value: parseInt(number, 16),
852
+ lineNumber: lineNumber,
853
+ lineStart: lineStart,
854
+ range: [start, index]
855
+ };
856
+ } else if (isOctalDigit(ch)) {
857
+ number += source[index++];
858
+ while (index < length) {
859
+ ch = source[index];
860
+ if (!isOctalDigit(ch)) {
861
+ break;
862
+ }
863
+ number += source[index++];
864
+ }
865
+
866
+ if (index < length) {
867
+ ch = source[index];
868
+ if (isIdentifierStart(ch) || isDecimalDigit(ch)) {
869
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
870
+ }
871
+ }
872
+ return {
873
+ type: Token.NumericLiteral,
874
+ value: parseInt(number, 8),
875
+ octal: true,
876
+ lineNumber: lineNumber,
877
+ lineStart: lineStart,
878
+ range: [start, index]
879
+ };
880
+ }
881
+
882
+ // decimal number starts with '0' such as '09' is illegal.
883
+ if (isDecimalDigit(ch)) {
884
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
885
+ }
886
+ }
887
+
888
+ while (index < length) {
889
+ ch = source[index];
890
+ if (!isDecimalDigit(ch)) {
891
+ break;
892
+ }
893
+ number += source[index++];
894
+ }
895
+ }
896
+
897
+ if (ch === '.') {
898
+ number += source[index++];
899
+ while (index < length) {
900
+ ch = source[index];
901
+ if (!isDecimalDigit(ch)) {
902
+ break;
903
+ }
904
+ number += source[index++];
905
+ }
906
+ }
907
+
908
+ if (ch === 'e' || ch === 'E') {
909
+ number += source[index++];
910
+
911
+ ch = source[index];
912
+ if (ch === '+' || ch === '-') {
913
+ number += source[index++];
914
+ }
915
+
916
+ ch = source[index];
917
+ if (isDecimalDigit(ch)) {
918
+ number += source[index++];
919
+ while (index < length) {
920
+ ch = source[index];
921
+ if (!isDecimalDigit(ch)) {
922
+ break;
923
+ }
924
+ number += source[index++];
925
+ }
926
+ } else {
927
+ ch = 'character ' + ch;
928
+ if (index >= length) {
929
+ ch = '<end>';
930
+ }
931
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
932
+ }
933
+ }
934
+
935
+ if (index < length) {
936
+ ch = source[index];
937
+ if (isIdentifierStart(ch)) {
938
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
939
+ }
940
+ }
941
+
942
+ return {
943
+ type: Token.NumericLiteral,
944
+ value: parseFloat(number),
945
+ lineNumber: lineNumber,
946
+ lineStart: lineStart,
947
+ range: [start, index]
948
+ };
949
+ }
950
+
951
+ // 7.8.4 String Literals
952
+
953
+ function scanStringLiteral() {
954
+ var str = '', quote, start, ch, code, unescaped, restore, octal = false;
955
+
956
+ quote = source[index];
957
+ assert((quote === '\'' || quote === '"'),
958
+ 'String literal must starts with a quote');
959
+
960
+ start = index;
961
+ ++index;
962
+
963
+ while (index < length) {
964
+ ch = source[index++];
965
+
966
+ if (ch === quote) {
967
+ quote = '';
968
+ break;
969
+ } else if (ch === '\\') {
970
+ ch = source[index++];
971
+ if (!isLineTerminator(ch)) {
972
+ switch (ch) {
973
+ case 'n':
974
+ str += '\n';
975
+ break;
976
+ case 'r':
977
+ str += '\r';
978
+ break;
979
+ case 't':
980
+ str += '\t';
981
+ break;
982
+ case 'u':
983
+ case 'x':
984
+ restore = index;
985
+ unescaped = scanHexEscape(ch);
986
+ if (unescaped) {
987
+ str += unescaped;
988
+ } else {
989
+ index = restore;
990
+ str += ch;
991
+ }
992
+ break;
993
+ case 'b':
994
+ str += '\b';
995
+ break;
996
+ case 'f':
997
+ str += '\f';
998
+ break;
999
+ case 'v':
1000
+ str += '\x0B';
1001
+ break;
1002
+
1003
+ default:
1004
+ if (isOctalDigit(ch)) {
1005
+ code = '01234567'.indexOf(ch);
1006
+
1007
+ // \0 is not octal escape sequence
1008
+ if (code !== 0) {
1009
+ octal = true;
1010
+ }
1011
+
1012
+ if (index < length && isOctalDigit(source[index])) {
1013
+ octal = true;
1014
+ code = code * 8 + '01234567'.indexOf(source[index++]);
1015
+
1016
+ // 3 digits are only allowed when string starts
1017
+ // with 0, 1, 2, 3
1018
+ if ('0123'.indexOf(ch) >= 0 &&
1019
+ index < length &&
1020
+ isOctalDigit(source[index])) {
1021
+ code = code * 8 + '01234567'.indexOf(source[index++]);
1022
+ }
1023
+ }
1024
+ str += String.fromCharCode(code);
1025
+ } else {
1026
+ str += ch;
1027
+ }
1028
+ break;
1029
+ }
1030
+ } else {
1031
+ ++lineNumber;
1032
+ if (ch === '\r' && source[index] === '\n') {
1033
+ ++index;
1034
+ }
1035
+ }
1036
+ } else if (isLineTerminator(ch)) {
1037
+ break;
1038
+ } else {
1039
+ str += ch;
1040
+ }
1041
+ }
1042
+
1043
+ if (quote !== '') {
1044
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
1045
+ }
1046
+
1047
+ return {
1048
+ type: Token.StringLiteral,
1049
+ value: str,
1050
+ octal: octal,
1051
+ lineNumber: lineNumber,
1052
+ lineStart: lineStart,
1053
+ range: [start, index]
1054
+ };
1055
+ }
1056
+
1057
+ function scanRegExp() {
1058
+ var str, ch, start, pattern, flags, value, classMarker = false, restore, terminated = false;
1059
+
1060
+ buffer = null;
1061
+ skipComment();
1062
+
1063
+ start = index;
1064
+ ch = source[index];
1065
+ assert(ch === '/', 'Regular expression literal must start with a slash');
1066
+ str = source[index++];
1067
+
1068
+ while (index < length) {
1069
+ ch = source[index++];
1070
+ str += ch;
1071
+ if (ch === '\\') {
1072
+ ch = source[index++];
1073
+ // ECMA-262 7.8.5
1074
+ if (isLineTerminator(ch)) {
1075
+ throwError({}, Messages.UnterminatedRegExp);
1076
+ }
1077
+ str += ch;
1078
+ } else if (classMarker) {
1079
+ if (ch === ']') {
1080
+ classMarker = false;
1081
+ }
1082
+ } else {
1083
+ if (ch === '/') {
1084
+ terminated = true;
1085
+ break;
1086
+ } else if (ch === '[') {
1087
+ classMarker = true;
1088
+ } else if (isLineTerminator(ch)) {
1089
+ throwError({}, Messages.UnterminatedRegExp);
1090
+ }
1091
+ }
1092
+ }
1093
+
1094
+ if (!terminated) {
1095
+ throwError({}, Messages.UnterminatedRegExp);
1096
+ }
1097
+
1098
+ // Exclude leading and trailing slash.
1099
+ pattern = str.substr(1, str.length - 2);
1100
+
1101
+ flags = '';
1102
+ while (index < length) {
1103
+ ch = source[index];
1104
+ if (!isIdentifierPart(ch)) {
1105
+ break;
1106
+ }
1107
+
1108
+ ++index;
1109
+ if (ch === '\\' && index < length) {
1110
+ ch = source[index];
1111
+ if (ch === 'u') {
1112
+ ++index;
1113
+ restore = index;
1114
+ ch = scanHexEscape('u');
1115
+ if (ch) {
1116
+ flags += ch;
1117
+ str += '\\u';
1118
+ for (; restore < index; ++restore) {
1119
+ str += source[restore];
1120
+ }
1121
+ } else {
1122
+ index = restore;
1123
+ flags += 'u';
1124
+ str += '\\u';
1125
+ }
1126
+ } else {
1127
+ str += '\\';
1128
+ }
1129
+ } else {
1130
+ flags += ch;
1131
+ str += ch;
1132
+ }
1133
+ }
1134
+
1135
+ try {
1136
+ value = new RegExp(pattern, flags);
1137
+ } catch (e) {
1138
+ throwError({}, Messages.InvalidRegExp);
1139
+ }
1140
+
1141
+ return {
1142
+ literal: str,
1143
+ value: value,
1144
+ range: [start, index]
1145
+ };
1146
+ }
1147
+
1148
+ function isIdentifierName(token) {
1149
+ return token.type === Token.Identifier ||
1150
+ token.type === Token.Keyword ||
1151
+ token.type === Token.BooleanLiteral ||
1152
+ token.type === Token.NullLiteral;
1153
+ }
1154
+
1155
+ function advance() {
1156
+ var ch, token;
1157
+
1158
+ skipComment();
1159
+
1160
+ if (index >= length) {
1161
+ return {
1162
+ type: Token.EOF,
1163
+ lineNumber: lineNumber,
1164
+ lineStart: lineStart,
1165
+ range: [index, index]
1166
+ };
1167
+ }
1168
+
1169
+ token = scanPunctuator();
1170
+ if (typeof token !== 'undefined') {
1171
+ return token;
1172
+ }
1173
+
1174
+ ch = source[index];
1175
+
1176
+ if (ch === '\'' || ch === '"') {
1177
+ return scanStringLiteral();
1178
+ }
1179
+
1180
+ if (ch === '.' || isDecimalDigit(ch)) {
1181
+ return scanNumericLiteral();
1182
+ }
1183
+
1184
+ token = scanIdentifier();
1185
+ if (typeof token !== 'undefined') {
1186
+ return token;
1187
+ }
1188
+
1189
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
1190
+ }
1191
+
1192
+ function lex() {
1193
+ var token;
1194
+
1195
+ if (buffer) {
1196
+ index = buffer.range[1];
1197
+ lineNumber = buffer.lineNumber;
1198
+ lineStart = buffer.lineStart;
1199
+ token = buffer;
1200
+ buffer = null;
1201
+ return token;
1202
+ }
1203
+
1204
+ buffer = null;
1205
+ return advance();
1206
+ }
1207
+
1208
+ function lookahead() {
1209
+ var pos, line, start;
1210
+
1211
+ if (buffer !== null) {
1212
+ return buffer;
1213
+ }
1214
+
1215
+ pos = index;
1216
+ line = lineNumber;
1217
+ start = lineStart;
1218
+ buffer = advance();
1219
+ index = pos;
1220
+ lineNumber = line;
1221
+ lineStart = start;
1222
+
1223
+ return buffer;
1224
+ }
1225
+
1226
+ // Return true if there is a line terminator before the next token.
1227
+
1228
+ function peekLineTerminator() {
1229
+ var pos, line, start, found;
1230
+
1231
+ pos = index;
1232
+ line = lineNumber;
1233
+ start = lineStart;
1234
+ skipComment();
1235
+ found = lineNumber !== line;
1236
+ index = pos;
1237
+ lineNumber = line;
1238
+ lineStart = start;
1239
+
1240
+ return found;
1241
+ }
1242
+
1243
+ // Throw an exception
1244
+
1245
+ function throwError(token, messageFormat) {
1246
+ var error,
1247
+ args = Array.prototype.slice.call(arguments, 2),
1248
+ msg = messageFormat.replace(
1249
+ /%(\d)/g,
1250
+ function (whole, index) {
1251
+ return args[index] || '';
1252
+ }
1253
+ );
1254
+
1255
+ if (typeof token.lineNumber === 'number') {
1256
+ error = new Error('Line ' + token.lineNumber + ': ' + msg);
1257
+ error.index = token.range[0];
1258
+ error.lineNumber = token.lineNumber;
1259
+ error.column = token.range[0] - lineStart + 1;
1260
+ } else {
1261
+ error = new Error('Line ' + lineNumber + ': ' + msg);
1262
+ error.index = index;
1263
+ error.lineNumber = lineNumber;
1264
+ error.column = index - lineStart + 1;
1265
+ }
1266
+
1267
+ throw error;
1268
+ }
1269
+
1270
+ function throwErrorTolerant() {
1271
+ try {
1272
+ throwError.apply(null, arguments);
1273
+ } catch (e) {
1274
+ if (extra.errors) {
1275
+ extra.errors.push(e);
1276
+ } else {
1277
+ throw e;
1278
+ }
1279
+ }
1280
+ }
1281
+
1282
+
1283
+ // Throw an exception because of the token.
1284
+
1285
+ function throwUnexpected(token) {
1286
+ if (token.type === Token.EOF) {
1287
+ throwError(token, Messages.UnexpectedEOS);
1288
+ }
1289
+
1290
+ if (token.type === Token.NumericLiteral) {
1291
+ throwError(token, Messages.UnexpectedNumber);
1292
+ }
1293
+
1294
+ if (token.type === Token.StringLiteral) {
1295
+ throwError(token, Messages.UnexpectedString);
1296
+ }
1297
+
1298
+ if (token.type === Token.Identifier) {
1299
+ throwError(token, Messages.UnexpectedIdentifier);
1300
+ }
1301
+
1302
+ if (token.type === Token.Keyword) {
1303
+ if (isFutureReservedWord(token.value)) {
1304
+ throwError(token, Messages.UnexpectedReserved);
1305
+ } else if (strict && isStrictModeReservedWord(token.value)) {
1306
+ throwErrorTolerant(token, Messages.StrictReservedWord);
1307
+ return;
1308
+ }
1309
+ throwError(token, Messages.UnexpectedToken, token.value);
1310
+ }
1311
+
1312
+ // BooleanLiteral, NullLiteral, or Punctuator.
1313
+ throwError(token, Messages.UnexpectedToken, token.value);
1314
+ }
1315
+
1316
+ // Expect the next token to match the specified punctuator.
1317
+ // If not, an exception will be thrown.
1318
+
1319
+ function expect(value) {
1320
+ var token = lex();
1321
+ if (token.type !== Token.Punctuator || token.value !== value) {
1322
+ throwUnexpected(token);
1323
+ }
1324
+ }
1325
+
1326
+ // Expect the next token to match the specified keyword.
1327
+ // If not, an exception will be thrown.
1328
+
1329
+ function expectKeyword(keyword) {
1330
+ var token = lex();
1331
+ if (token.type !== Token.Keyword || token.value !== keyword) {
1332
+ throwUnexpected(token);
1333
+ }
1334
+ }
1335
+
1336
+ // Return true if the next token matches the specified punctuator.
1337
+
1338
+ function match(value) {
1339
+ var token = lookahead();
1340
+ return token.type === Token.Punctuator && token.value === value;
1341
+ }
1342
+
1343
+ // Return true if the next token matches the specified keyword
1344
+
1345
+ function matchKeyword(keyword) {
1346
+ var token = lookahead();
1347
+ return token.type === Token.Keyword && token.value === keyword;
1348
+ }
1349
+
1350
+ // Return true if the next token is an assignment operator
1351
+
1352
+ function matchAssign() {
1353
+ var token = lookahead(),
1354
+ op = token.value;
1355
+
1356
+ if (token.type !== Token.Punctuator) {
1357
+ return false;
1358
+ }
1359
+ return op === '=' ||
1360
+ op === '*=' ||
1361
+ op === '/=' ||
1362
+ op === '%=' ||
1363
+ op === '+=' ||
1364
+ op === '-=' ||
1365
+ op === '<<=' ||
1366
+ op === '>>=' ||
1367
+ op === '>>>=' ||
1368
+ op === '&=' ||
1369
+ op === '^=' ||
1370
+ op === '|=';
1371
+ }
1372
+
1373
+ function consumeSemicolon() {
1374
+ var token, line;
1375
+
1376
+ // Catch the very common case first.
1377
+ if (source[index] === ';') {
1378
+ lex();
1379
+ return;
1380
+ }
1381
+
1382
+ line = lineNumber;
1383
+ skipComment();
1384
+ if (lineNumber !== line) {
1385
+ return;
1386
+ }
1387
+
1388
+ if (match(';')) {
1389
+ lex();
1390
+ return;
1391
+ }
1392
+
1393
+ token = lookahead();
1394
+ if (token.type !== Token.EOF && !match('}')) {
1395
+ throwUnexpected(token);
1396
+ }
1397
+ }
1398
+
1399
+ // Return true if provided expression is LeftHandSideExpression
1400
+
1401
+ function isLeftHandSide(expr) {
1402
+ return expr.type === Syntax.Identifier || expr.type === Syntax.MemberExpression;
1403
+ }
1404
+
1405
+ // 11.1.4 Array Initialiser
1406
+
1407
+ function parseArrayInitialiser() {
1408
+ var elements = [];
1409
+
1410
+ expect('[');
1411
+
1412
+ while (!match(']')) {
1413
+ if (match(',')) {
1414
+ lex();
1415
+ elements.push(null);
1416
+ } else {
1417
+ elements.push(parseAssignmentExpression());
1418
+
1419
+ if (!match(']')) {
1420
+ expect(',');
1421
+ }
1422
+ }
1423
+ }
1424
+
1425
+ expect(']');
1426
+
1427
+ return {
1428
+ type: Syntax.ArrayExpression,
1429
+ elements: elements
1430
+ };
1431
+ }
1432
+
1433
+ // 11.1.5 Object Initialiser
1434
+
1435
+ function parsePropertyFunction(param, first) {
1436
+ var previousStrict, body;
1437
+
1438
+ previousStrict = strict;
1439
+ body = parseFunctionSourceElements();
1440
+ if (first && strict && isRestrictedWord(param[0].name)) {
1441
+ throwErrorTolerant(first, Messages.StrictParamName);
1442
+ }
1443
+ strict = previousStrict;
1444
+
1445
+ return {
1446
+ type: Syntax.FunctionExpression,
1447
+ id: null,
1448
+ params: param,
1449
+ defaults: [],
1450
+ body: body,
1451
+ rest: null,
1452
+ generator: false,
1453
+ expression: false
1454
+ };
1455
+ }
1456
+
1457
+ function parseObjectPropertyKey() {
1458
+ var token = lex();
1459
+
1460
+ // Note: This function is called only from parseObjectProperty(), where
1461
+ // EOF and Punctuator tokens are already filtered out.
1462
+
1463
+ if (token.type === Token.StringLiteral || token.type === Token.NumericLiteral) {
1464
+ if (strict && token.octal) {
1465
+ throwErrorTolerant(token, Messages.StrictOctalLiteral);
1466
+ }
1467
+ return createLiteral(token);
1468
+ }
1469
+
1470
+ return {
1471
+ type: Syntax.Identifier,
1472
+ name: token.value
1473
+ };
1474
+ }
1475
+
1476
+ function parseObjectProperty() {
1477
+ var token, key, id, param;
1478
+
1479
+ token = lookahead();
1480
+
1481
+ if (token.type === Token.Identifier) {
1482
+
1483
+ id = parseObjectPropertyKey();
1484
+
1485
+ // Property Assignment: Getter and Setter.
1486
+
1487
+ if (token.value === 'get' && !match(':')) {
1488
+ key = parseObjectPropertyKey();
1489
+ expect('(');
1490
+ expect(')');
1491
+ return {
1492
+ type: Syntax.Property,
1493
+ key: key,
1494
+ value: parsePropertyFunction([]),
1495
+ kind: 'get'
1496
+ };
1497
+ } else if (token.value === 'set' && !match(':')) {
1498
+ key = parseObjectPropertyKey();
1499
+ expect('(');
1500
+ token = lookahead();
1501
+ if (token.type !== Token.Identifier) {
1502
+ expect(')');
1503
+ throwErrorTolerant(token, Messages.UnexpectedToken, token.value);
1504
+ return {
1505
+ type: Syntax.Property,
1506
+ key: key,
1507
+ value: parsePropertyFunction([]),
1508
+ kind: 'set'
1509
+ };
1510
+ } else {
1511
+ param = [ parseVariableIdentifier() ];
1512
+ expect(')');
1513
+ return {
1514
+ type: Syntax.Property,
1515
+ key: key,
1516
+ value: parsePropertyFunction(param, token),
1517
+ kind: 'set'
1518
+ };
1519
+ }
1520
+ } else {
1521
+ expect(':');
1522
+ return {
1523
+ type: Syntax.Property,
1524
+ key: id,
1525
+ value: parseAssignmentExpression(),
1526
+ kind: 'init'
1527
+ };
1528
+ }
1529
+ } else if (token.type === Token.EOF || token.type === Token.Punctuator) {
1530
+ throwUnexpected(token);
1531
+ } else {
1532
+ key = parseObjectPropertyKey();
1533
+ expect(':');
1534
+ return {
1535
+ type: Syntax.Property,
1536
+ key: key,
1537
+ value: parseAssignmentExpression(),
1538
+ kind: 'init'
1539
+ };
1540
+ }
1541
+ }
1542
+
1543
+ function parseObjectInitialiser() {
1544
+ var properties = [], property, name, kind, map = {}, toString = String;
1545
+
1546
+ expect('{');
1547
+
1548
+ while (!match('}')) {
1549
+ property = parseObjectProperty();
1550
+
1551
+ if (property.key.type === Syntax.Identifier) {
1552
+ name = property.key.name;
1553
+ } else {
1554
+ name = toString(property.key.value);
1555
+ }
1556
+ kind = (property.kind === 'init') ? PropertyKind.Data : (property.kind === 'get') ? PropertyKind.Get : PropertyKind.Set;
1557
+ if (Object.prototype.hasOwnProperty.call(map, name)) {
1558
+ if (map[name] === PropertyKind.Data) {
1559
+ if (strict && kind === PropertyKind.Data) {
1560
+ throwErrorTolerant({}, Messages.StrictDuplicateProperty);
1561
+ } else if (kind !== PropertyKind.Data) {
1562
+ throwErrorTolerant({}, Messages.AccessorDataProperty);
1563
+ }
1564
+ } else {
1565
+ if (kind === PropertyKind.Data) {
1566
+ throwErrorTolerant({}, Messages.AccessorDataProperty);
1567
+ } else if (map[name] & kind) {
1568
+ throwErrorTolerant({}, Messages.AccessorGetSet);
1569
+ }
1570
+ }
1571
+ map[name] |= kind;
1572
+ } else {
1573
+ map[name] = kind;
1574
+ }
1575
+
1576
+ properties.push(property);
1577
+
1578
+ if (!match('}')) {
1579
+ expect(',');
1580
+ }
1581
+ }
1582
+
1583
+ expect('}');
1584
+
1585
+ return {
1586
+ type: Syntax.ObjectExpression,
1587
+ properties: properties
1588
+ };
1589
+ }
1590
+
1591
+ // 11.1.6 The Grouping Operator
1592
+
1593
+ function parseGroupExpression() {
1594
+ var expr;
1595
+
1596
+ expect('(');
1597
+
1598
+ expr = parseExpression();
1599
+
1600
+ expect(')');
1601
+
1602
+ return expr;
1603
+ }
1604
+
1605
+
1606
+ // 11.1 Primary Expressions
1607
+
1608
+ function parsePrimaryExpression() {
1609
+ var token = lookahead(),
1610
+ type = token.type;
1611
+
1612
+ if (type === Token.Identifier) {
1613
+ return {
1614
+ type: Syntax.Identifier,
1615
+ name: lex().value
1616
+ };
1617
+ }
1618
+
1619
+ if (type === Token.StringLiteral || type === Token.NumericLiteral) {
1620
+ if (strict && token.octal) {
1621
+ throwErrorTolerant(token, Messages.StrictOctalLiteral);
1622
+ }
1623
+ return createLiteral(lex());
1624
+ }
1625
+
1626
+ if (type === Token.Keyword) {
1627
+ if (matchKeyword('this')) {
1628
+ lex();
1629
+ return {
1630
+ type: Syntax.ThisExpression
1631
+ };
1632
+ }
1633
+
1634
+ if (matchKeyword('function')) {
1635
+ return parseFunctionExpression();
1636
+ }
1637
+ }
1638
+
1639
+ if (type === Token.BooleanLiteral) {
1640
+ lex();
1641
+ token.value = (token.value === 'true');
1642
+ return createLiteral(token);
1643
+ }
1644
+
1645
+ if (type === Token.NullLiteral) {
1646
+ lex();
1647
+ token.value = null;
1648
+ return createLiteral(token);
1649
+ }
1650
+
1651
+ if (match('[')) {
1652
+ return parseArrayInitialiser();
1653
+ }
1654
+
1655
+ if (match('{')) {
1656
+ return parseObjectInitialiser();
1657
+ }
1658
+
1659
+ if (match('(')) {
1660
+ return parseGroupExpression();
1661
+ }
1662
+
1663
+ if (match('/') || match('/=')) {
1664
+ return createLiteral(scanRegExp());
1665
+ }
1666
+
1667
+ return throwUnexpected(lex());
1668
+ }
1669
+
1670
+ // 11.2 Left-Hand-Side Expressions
1671
+
1672
+ function parseArguments() {
1673
+ var args = [];
1674
+
1675
+ expect('(');
1676
+
1677
+ if (!match(')')) {
1678
+ while (index < length) {
1679
+ args.push(parseAssignmentExpression());
1680
+ if (match(')')) {
1681
+ break;
1682
+ }
1683
+ expect(',');
1684
+ }
1685
+ }
1686
+
1687
+ expect(')');
1688
+
1689
+ return args;
1690
+ }
1691
+
1692
+ function parseNonComputedProperty() {
1693
+ var token = lex();
1694
+
1695
+ if (!isIdentifierName(token)) {
1696
+ throwUnexpected(token);
1697
+ }
1698
+
1699
+ return {
1700
+ type: Syntax.Identifier,
1701
+ name: token.value
1702
+ };
1703
+ }
1704
+
1705
+ function parseNonComputedMember() {
1706
+ expect('.');
1707
+
1708
+ return parseNonComputedProperty();
1709
+ }
1710
+
1711
+ function parseComputedMember() {
1712
+ var expr;
1713
+
1714
+ expect('[');
1715
+
1716
+ expr = parseExpression();
1717
+
1718
+ expect(']');
1719
+
1720
+ return expr;
1721
+ }
1722
+
1723
+ function parseNewExpression() {
1724
+ var expr;
1725
+
1726
+ expectKeyword('new');
1727
+
1728
+ expr = {
1729
+ type: Syntax.NewExpression,
1730
+ callee: parseLeftHandSideExpression(),
1731
+ 'arguments': []
1732
+ };
1733
+
1734
+ if (match('(')) {
1735
+ expr['arguments'] = parseArguments();
1736
+ }
1737
+
1738
+ return expr;
1739
+ }
1740
+
1741
+ function parseLeftHandSideExpressionAllowCall() {
1742
+ var expr;
1743
+
1744
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
1745
+
1746
+ while (match('.') || match('[') || match('(')) {
1747
+ if (match('(')) {
1748
+ expr = {
1749
+ type: Syntax.CallExpression,
1750
+ callee: expr,
1751
+ 'arguments': parseArguments()
1752
+ };
1753
+ } else if (match('[')) {
1754
+ expr = {
1755
+ type: Syntax.MemberExpression,
1756
+ computed: true,
1757
+ object: expr,
1758
+ property: parseComputedMember()
1759
+ };
1760
+ } else {
1761
+ expr = {
1762
+ type: Syntax.MemberExpression,
1763
+ computed: false,
1764
+ object: expr,
1765
+ property: parseNonComputedMember()
1766
+ };
1767
+ }
1768
+ }
1769
+
1770
+ return expr;
1771
+ }
1772
+
1773
+
1774
+ function parseLeftHandSideExpression() {
1775
+ var expr;
1776
+
1777
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
1778
+
1779
+ while (match('.') || match('[')) {
1780
+ if (match('[')) {
1781
+ expr = {
1782
+ type: Syntax.MemberExpression,
1783
+ computed: true,
1784
+ object: expr,
1785
+ property: parseComputedMember()
1786
+ };
1787
+ } else {
1788
+ expr = {
1789
+ type: Syntax.MemberExpression,
1790
+ computed: false,
1791
+ object: expr,
1792
+ property: parseNonComputedMember()
1793
+ };
1794
+ }
1795
+ }
1796
+
1797
+ return expr;
1798
+ }
1799
+
1800
+ // 11.3 Postfix Expressions
1801
+
1802
+ function parsePostfixExpression() {
1803
+ var expr = parseLeftHandSideExpressionAllowCall(), token;
1804
+
1805
+ token = lookahead();
1806
+ if (token.type !== Token.Punctuator) {
1807
+ return expr;
1808
+ }
1809
+
1810
+ if ((match('++') || match('--')) && !peekLineTerminator()) {
1811
+ // 11.3.1, 11.3.2
1812
+ if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
1813
+ throwErrorTolerant({}, Messages.StrictLHSPostfix);
1814
+ }
1815
+ if (!isLeftHandSide(expr)) {
1816
+ throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
1817
+ }
1818
+
1819
+ expr = {
1820
+ type: Syntax.UpdateExpression,
1821
+ operator: lex().value,
1822
+ argument: expr,
1823
+ prefix: false
1824
+ };
1825
+ }
1826
+
1827
+ return expr;
1828
+ }
1829
+
1830
+ // 11.4 Unary Operators
1831
+
1832
+ function parseUnaryExpression() {
1833
+ var token, expr;
1834
+
1835
+ token = lookahead();
1836
+ if (token.type !== Token.Punctuator && token.type !== Token.Keyword) {
1837
+ return parsePostfixExpression();
1838
+ }
1839
+
1840
+ if (match('++') || match('--')) {
1841
+ token = lex();
1842
+ expr = parseUnaryExpression();
1843
+ // 11.4.4, 11.4.5
1844
+ if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
1845
+ throwErrorTolerant({}, Messages.StrictLHSPrefix);
1846
+ }
1847
+
1848
+ if (!isLeftHandSide(expr)) {
1849
+ throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
1850
+ }
1851
+
1852
+ expr = {
1853
+ type: Syntax.UpdateExpression,
1854
+ operator: token.value,
1855
+ argument: expr,
1856
+ prefix: true
1857
+ };
1858
+ return expr;
1859
+ }
1860
+
1861
+ if (match('+') || match('-') || match('~') || match('!')) {
1862
+ expr = {
1863
+ type: Syntax.UnaryExpression,
1864
+ operator: lex().value,
1865
+ argument: parseUnaryExpression(),
1866
+ prefix: true
1867
+ };
1868
+ return expr;
1869
+ }
1870
+
1871
+ if (matchKeyword('delete') || matchKeyword('void') || matchKeyword('typeof')) {
1872
+ expr = {
1873
+ type: Syntax.UnaryExpression,
1874
+ operator: lex().value,
1875
+ argument: parseUnaryExpression(),
1876
+ prefix: true
1877
+ };
1878
+ if (strict && expr.operator === 'delete' && expr.argument.type === Syntax.Identifier) {
1879
+ throwErrorTolerant({}, Messages.StrictDelete);
1880
+ }
1881
+ return expr;
1882
+ }
1883
+
1884
+ return parsePostfixExpression();
1885
+ }
1886
+
1887
+ // 11.5 Multiplicative Operators
1888
+
1889
+ function parseMultiplicativeExpression() {
1890
+ var expr = parseUnaryExpression();
1891
+
1892
+ while (match('*') || match('/') || match('%')) {
1893
+ expr = {
1894
+ type: Syntax.BinaryExpression,
1895
+ operator: lex().value,
1896
+ left: expr,
1897
+ right: parseUnaryExpression()
1898
+ };
1899
+ }
1900
+
1901
+ return expr;
1902
+ }
1903
+
1904
+ // 11.6 Additive Operators
1905
+
1906
+ function parseAdditiveExpression() {
1907
+ var expr = parseMultiplicativeExpression();
1908
+
1909
+ while (match('+') || match('-')) {
1910
+ expr = {
1911
+ type: Syntax.BinaryExpression,
1912
+ operator: lex().value,
1913
+ left: expr,
1914
+ right: parseMultiplicativeExpression()
1915
+ };
1916
+ }
1917
+
1918
+ return expr;
1919
+ }
1920
+
1921
+ // 11.7 Bitwise Shift Operators
1922
+
1923
+ function parseShiftExpression() {
1924
+ var expr = parseAdditiveExpression();
1925
+
1926
+ while (match('<<') || match('>>') || match('>>>')) {
1927
+ expr = {
1928
+ type: Syntax.BinaryExpression,
1929
+ operator: lex().value,
1930
+ left: expr,
1931
+ right: parseAdditiveExpression()
1932
+ };
1933
+ }
1934
+
1935
+ return expr;
1936
+ }
1937
+ // 11.8 Relational Operators
1938
+
1939
+ function parseRelationalExpression() {
1940
+ var expr, previousAllowIn;
1941
+
1942
+ previousAllowIn = state.allowIn;
1943
+ state.allowIn = true;
1944
+
1945
+ expr = parseShiftExpression();
1946
+
1947
+ while (match('<') || match('>') || match('<=') || match('>=') || (previousAllowIn && matchKeyword('in')) || matchKeyword('instanceof')) {
1948
+ expr = {
1949
+ type: Syntax.BinaryExpression,
1950
+ operator: lex().value,
1951
+ left: expr,
1952
+ right: parseShiftExpression()
1953
+ };
1954
+ }
1955
+
1956
+ state.allowIn = previousAllowIn;
1957
+ return expr;
1958
+ }
1959
+
1960
+ // 11.9 Equality Operators
1961
+
1962
+ function parseEqualityExpression() {
1963
+ var expr = parseRelationalExpression();
1964
+
1965
+ while (match('==') || match('!=') || match('===') || match('!==')) {
1966
+ expr = {
1967
+ type: Syntax.BinaryExpression,
1968
+ operator: lex().value,
1969
+ left: expr,
1970
+ right: parseRelationalExpression()
1971
+ };
1972
+ }
1973
+
1974
+ return expr;
1975
+ }
1976
+
1977
+ // 11.10 Binary Bitwise Operators
1978
+
1979
+ function parseBitwiseANDExpression() {
1980
+ var expr = parseEqualityExpression();
1981
+
1982
+ while (match('&')) {
1983
+ lex();
1984
+ expr = {
1985
+ type: Syntax.BinaryExpression,
1986
+ operator: '&',
1987
+ left: expr,
1988
+ right: parseEqualityExpression()
1989
+ };
1990
+ }
1991
+
1992
+ return expr;
1993
+ }
1994
+
1995
+ function parseBitwiseXORExpression() {
1996
+ var expr = parseBitwiseANDExpression();
1997
+
1998
+ while (match('^')) {
1999
+ lex();
2000
+ expr = {
2001
+ type: Syntax.BinaryExpression,
2002
+ operator: '^',
2003
+ left: expr,
2004
+ right: parseBitwiseANDExpression()
2005
+ };
2006
+ }
2007
+
2008
+ return expr;
2009
+ }
2010
+
2011
+ function parseBitwiseORExpression() {
2012
+ var expr = parseBitwiseXORExpression();
2013
+
2014
+ while (match('|')) {
2015
+ lex();
2016
+ expr = {
2017
+ type: Syntax.BinaryExpression,
2018
+ operator: '|',
2019
+ left: expr,
2020
+ right: parseBitwiseXORExpression()
2021
+ };
2022
+ }
2023
+
2024
+ return expr;
2025
+ }
2026
+
2027
+ // 11.11 Binary Logical Operators
2028
+
2029
+ function parseLogicalANDExpression() {
2030
+ var expr = parseBitwiseORExpression();
2031
+
2032
+ while (match('&&')) {
2033
+ lex();
2034
+ expr = {
2035
+ type: Syntax.LogicalExpression,
2036
+ operator: '&&',
2037
+ left: expr,
2038
+ right: parseBitwiseORExpression()
2039
+ };
2040
+ }
2041
+
2042
+ return expr;
2043
+ }
2044
+
2045
+ function parseLogicalORExpression() {
2046
+ var expr = parseLogicalANDExpression();
2047
+
2048
+ while (match('||')) {
2049
+ lex();
2050
+ expr = {
2051
+ type: Syntax.LogicalExpression,
2052
+ operator: '||',
2053
+ left: expr,
2054
+ right: parseLogicalANDExpression()
2055
+ };
2056
+ }
2057
+
2058
+ return expr;
2059
+ }
2060
+
2061
+ // 11.12 Conditional Operator
2062
+
2063
+ function parseConditionalExpression() {
2064
+ var expr, previousAllowIn, consequent;
2065
+
2066
+ expr = parseLogicalORExpression();
2067
+
2068
+ if (match('?')) {
2069
+ lex();
2070
+ previousAllowIn = state.allowIn;
2071
+ state.allowIn = true;
2072
+ consequent = parseAssignmentExpression();
2073
+ state.allowIn = previousAllowIn;
2074
+ expect(':');
2075
+
2076
+ expr = {
2077
+ type: Syntax.ConditionalExpression,
2078
+ test: expr,
2079
+ consequent: consequent,
2080
+ alternate: parseAssignmentExpression()
2081
+ };
2082
+ }
2083
+
2084
+ return expr;
2085
+ }
2086
+
2087
+ // 11.13 Assignment Operators
2088
+
2089
+ function parseAssignmentExpression() {
2090
+ var token, expr;
2091
+
2092
+ token = lookahead();
2093
+ expr = parseConditionalExpression();
2094
+
2095
+ if (matchAssign()) {
2096
+ // LeftHandSideExpression
2097
+ if (!isLeftHandSide(expr)) {
2098
+ throwErrorTolerant({}, Messages.InvalidLHSInAssignment);
2099
+ }
2100
+
2101
+ // 11.13.1
2102
+ if (strict && expr.type === Syntax.Identifier && isRestrictedWord(expr.name)) {
2103
+ throwErrorTolerant(token, Messages.StrictLHSAssignment);
2104
+ }
2105
+
2106
+ expr = {
2107
+ type: Syntax.AssignmentExpression,
2108
+ operator: lex().value,
2109
+ left: expr,
2110
+ right: parseAssignmentExpression()
2111
+ };
2112
+ }
2113
+
2114
+ return expr;
2115
+ }
2116
+
2117
+ // 11.14 Comma Operator
2118
+
2119
+ function parseExpression() {
2120
+ var expr = parseAssignmentExpression();
2121
+
2122
+ if (match(',')) {
2123
+ expr = {
2124
+ type: Syntax.SequenceExpression,
2125
+ expressions: [ expr ]
2126
+ };
2127
+
2128
+ while (index < length) {
2129
+ if (!match(',')) {
2130
+ break;
2131
+ }
2132
+ lex();
2133
+ expr.expressions.push(parseAssignmentExpression());
2134
+ }
2135
+
2136
+ }
2137
+ return expr;
2138
+ }
2139
+
2140
+ // 12.1 Block
2141
+
2142
+ function parseStatementList() {
2143
+ var list = [],
2144
+ statement;
2145
+
2146
+ while (index < length) {
2147
+ if (match('}')) {
2148
+ break;
2149
+ }
2150
+ statement = parseSourceElement();
2151
+ if (typeof statement === 'undefined') {
2152
+ break;
2153
+ }
2154
+ list.push(statement);
2155
+ }
2156
+
2157
+ return list;
2158
+ }
2159
+
2160
+ function parseBlock() {
2161
+ var block;
2162
+
2163
+ expect('{');
2164
+
2165
+ block = parseStatementList();
2166
+
2167
+ expect('}');
2168
+
2169
+ return {
2170
+ type: Syntax.BlockStatement,
2171
+ body: block
2172
+ };
2173
+ }
2174
+
2175
+ // 12.2 Variable Statement
2176
+
2177
+ function parseVariableIdentifier() {
2178
+ var token = lex();
2179
+
2180
+ if (token.type !== Token.Identifier) {
2181
+ throwUnexpected(token);
2182
+ }
2183
+
2184
+ return {
2185
+ type: Syntax.Identifier,
2186
+ name: token.value
2187
+ };
2188
+ }
2189
+
2190
+ function parseVariableDeclaration(kind) {
2191
+ var id = parseVariableIdentifier(),
2192
+ init = null;
2193
+
2194
+ // 12.2.1
2195
+ if (strict && isRestrictedWord(id.name)) {
2196
+ throwErrorTolerant({}, Messages.StrictVarName);
2197
+ }
2198
+
2199
+ if (kind === 'const') {
2200
+ expect('=');
2201
+ init = parseAssignmentExpression();
2202
+ } else if (match('=')) {
2203
+ lex();
2204
+ init = parseAssignmentExpression();
2205
+ }
2206
+
2207
+ return {
2208
+ type: Syntax.VariableDeclarator,
2209
+ id: id,
2210
+ init: init
2211
+ };
2212
+ }
2213
+
2214
+ function parseVariableDeclarationList(kind) {
2215
+ var list = [];
2216
+
2217
+ do {
2218
+ list.push(parseVariableDeclaration(kind));
2219
+ if (!match(',')) {
2220
+ break;
2221
+ }
2222
+ lex();
2223
+ } while (index < length);
2224
+
2225
+ return list;
2226
+ }
2227
+
2228
+ function parseVariableStatement() {
2229
+ var declarations;
2230
+
2231
+ expectKeyword('var');
2232
+
2233
+ declarations = parseVariableDeclarationList();
2234
+
2235
+ consumeSemicolon();
2236
+
2237
+ return {
2238
+ type: Syntax.VariableDeclaration,
2239
+ declarations: declarations,
2240
+ kind: 'var'
2241
+ };
2242
+ }
2243
+
2244
+ // kind may be `const` or `let`
2245
+ // Both are experimental and not in the specification yet.
2246
+ // see http://wiki.ecmascript.org/doku.php?id=harmony:const
2247
+ // and http://wiki.ecmascript.org/doku.php?id=harmony:let
2248
+ function parseConstLetDeclaration(kind) {
2249
+ var declarations;
2250
+
2251
+ expectKeyword(kind);
2252
+
2253
+ declarations = parseVariableDeclarationList(kind);
2254
+
2255
+ consumeSemicolon();
2256
+
2257
+ return {
2258
+ type: Syntax.VariableDeclaration,
2259
+ declarations: declarations,
2260
+ kind: kind
2261
+ };
2262
+ }
2263
+
2264
+ // 12.3 Empty Statement
2265
+
2266
+ function parseEmptyStatement() {
2267
+ expect(';');
2268
+
2269
+ return {
2270
+ type: Syntax.EmptyStatement
2271
+ };
2272
+ }
2273
+
2274
+ // 12.4 Expression Statement
2275
+
2276
+ function parseExpressionStatement() {
2277
+ var expr = parseExpression();
2278
+
2279
+ consumeSemicolon();
2280
+
2281
+ return {
2282
+ type: Syntax.ExpressionStatement,
2283
+ expression: expr
2284
+ };
2285
+ }
2286
+
2287
+ // 12.5 If statement
2288
+
2289
+ function parseIfStatement() {
2290
+ var test, consequent, alternate;
2291
+
2292
+ expectKeyword('if');
2293
+
2294
+ expect('(');
2295
+
2296
+ test = parseExpression();
2297
+
2298
+ expect(')');
2299
+
2300
+ consequent = parseStatement();
2301
+
2302
+ if (matchKeyword('else')) {
2303
+ lex();
2304
+ alternate = parseStatement();
2305
+ } else {
2306
+ alternate = null;
2307
+ }
2308
+
2309
+ return {
2310
+ type: Syntax.IfStatement,
2311
+ test: test,
2312
+ consequent: consequent,
2313
+ alternate: alternate
2314
+ };
2315
+ }
2316
+
2317
+ // 12.6 Iteration Statements
2318
+
2319
+ function parseDoWhileStatement() {
2320
+ var body, test, oldInIteration;
2321
+
2322
+ expectKeyword('do');
2323
+
2324
+ oldInIteration = state.inIteration;
2325
+ state.inIteration = true;
2326
+
2327
+ body = parseStatement();
2328
+
2329
+ state.inIteration = oldInIteration;
2330
+
2331
+ expectKeyword('while');
2332
+
2333
+ expect('(');
2334
+
2335
+ test = parseExpression();
2336
+
2337
+ expect(')');
2338
+
2339
+ if (match(';')) {
2340
+ lex();
2341
+ }
2342
+
2343
+ return {
2344
+ type: Syntax.DoWhileStatement,
2345
+ body: body,
2346
+ test: test
2347
+ };
2348
+ }
2349
+
2350
+ function parseWhileStatement() {
2351
+ var test, body, oldInIteration;
2352
+
2353
+ expectKeyword('while');
2354
+
2355
+ expect('(');
2356
+
2357
+ test = parseExpression();
2358
+
2359
+ expect(')');
2360
+
2361
+ oldInIteration = state.inIteration;
2362
+ state.inIteration = true;
2363
+
2364
+ body = parseStatement();
2365
+
2366
+ state.inIteration = oldInIteration;
2367
+
2368
+ return {
2369
+ type: Syntax.WhileStatement,
2370
+ test: test,
2371
+ body: body
2372
+ };
2373
+ }
2374
+
2375
+ function parseForVariableDeclaration() {
2376
+ var token = lex();
2377
+
2378
+ return {
2379
+ type: Syntax.VariableDeclaration,
2380
+ declarations: parseVariableDeclarationList(),
2381
+ kind: token.value
2382
+ };
2383
+ }
2384
+
2385
+ function parseForStatement() {
2386
+ var init, test, update, left, right, body, oldInIteration;
2387
+
2388
+ init = test = update = null;
2389
+
2390
+ expectKeyword('for');
2391
+
2392
+ expect('(');
2393
+
2394
+ if (match(';')) {
2395
+ lex();
2396
+ } else {
2397
+ if (matchKeyword('var') || matchKeyword('let')) {
2398
+ state.allowIn = false;
2399
+ init = parseForVariableDeclaration();
2400
+ state.allowIn = true;
2401
+
2402
+ if (init.declarations.length === 1 && matchKeyword('in')) {
2403
+ lex();
2404
+ left = init;
2405
+ right = parseExpression();
2406
+ init = null;
2407
+ }
2408
+ } else {
2409
+ state.allowIn = false;
2410
+ init = parseExpression();
2411
+ state.allowIn = true;
2412
+
2413
+ if (matchKeyword('in')) {
2414
+ // LeftHandSideExpression
2415
+ if (!isLeftHandSide(init)) {
2416
+ throwErrorTolerant({}, Messages.InvalidLHSInForIn);
2417
+ }
2418
+
2419
+ lex();
2420
+ left = init;
2421
+ right = parseExpression();
2422
+ init = null;
2423
+ }
2424
+ }
2425
+
2426
+ if (typeof left === 'undefined') {
2427
+ expect(';');
2428
+ }
2429
+ }
2430
+
2431
+ if (typeof left === 'undefined') {
2432
+
2433
+ if (!match(';')) {
2434
+ test = parseExpression();
2435
+ }
2436
+ expect(';');
2437
+
2438
+ if (!match(')')) {
2439
+ update = parseExpression();
2440
+ }
2441
+ }
2442
+
2443
+ expect(')');
2444
+
2445
+ oldInIteration = state.inIteration;
2446
+ state.inIteration = true;
2447
+
2448
+ body = parseStatement();
2449
+
2450
+ state.inIteration = oldInIteration;
2451
+
2452
+ if (typeof left === 'undefined') {
2453
+ return {
2454
+ type: Syntax.ForStatement,
2455
+ init: init,
2456
+ test: test,
2457
+ update: update,
2458
+ body: body
2459
+ };
2460
+ }
2461
+
2462
+ return {
2463
+ type: Syntax.ForInStatement,
2464
+ left: left,
2465
+ right: right,
2466
+ body: body,
2467
+ each: false
2468
+ };
2469
+ }
2470
+
2471
+ // 12.7 The continue statement
2472
+
2473
+ function parseContinueStatement() {
2474
+ var token, label = null;
2475
+
2476
+ expectKeyword('continue');
2477
+
2478
+ // Optimize the most common form: 'continue;'.
2479
+ if (source[index] === ';') {
2480
+ lex();
2481
+
2482
+ if (!state.inIteration) {
2483
+ throwError({}, Messages.IllegalContinue);
2484
+ }
2485
+
2486
+ return {
2487
+ type: Syntax.ContinueStatement,
2488
+ label: null
2489
+ };
2490
+ }
2491
+
2492
+ if (peekLineTerminator()) {
2493
+ if (!state.inIteration) {
2494
+ throwError({}, Messages.IllegalContinue);
2495
+ }
2496
+
2497
+ return {
2498
+ type: Syntax.ContinueStatement,
2499
+ label: null
2500
+ };
2501
+ }
2502
+
2503
+ token = lookahead();
2504
+ if (token.type === Token.Identifier) {
2505
+ label = parseVariableIdentifier();
2506
+
2507
+ if (!Object.prototype.hasOwnProperty.call(state.labelSet, label.name)) {
2508
+ throwError({}, Messages.UnknownLabel, label.name);
2509
+ }
2510
+ }
2511
+
2512
+ consumeSemicolon();
2513
+
2514
+ if (label === null && !state.inIteration) {
2515
+ throwError({}, Messages.IllegalContinue);
2516
+ }
2517
+
2518
+ return {
2519
+ type: Syntax.ContinueStatement,
2520
+ label: label
2521
+ };
2522
+ }
2523
+
2524
+ // 12.8 The break statement
2525
+
2526
+ function parseBreakStatement() {
2527
+ var token, label = null;
2528
+
2529
+ expectKeyword('break');
2530
+
2531
+ // Optimize the most common form: 'break;'.
2532
+ if (source[index] === ';') {
2533
+ lex();
2534
+
2535
+ if (!(state.inIteration || state.inSwitch)) {
2536
+ throwError({}, Messages.IllegalBreak);
2537
+ }
2538
+
2539
+ return {
2540
+ type: Syntax.BreakStatement,
2541
+ label: null
2542
+ };
2543
+ }
2544
+
2545
+ if (peekLineTerminator()) {
2546
+ if (!(state.inIteration || state.inSwitch)) {
2547
+ throwError({}, Messages.IllegalBreak);
2548
+ }
2549
+
2550
+ return {
2551
+ type: Syntax.BreakStatement,
2552
+ label: null
2553
+ };
2554
+ }
2555
+
2556
+ token = lookahead();
2557
+ if (token.type === Token.Identifier) {
2558
+ label = parseVariableIdentifier();
2559
+
2560
+ if (!Object.prototype.hasOwnProperty.call(state.labelSet, label.name)) {
2561
+ throwError({}, Messages.UnknownLabel, label.name);
2562
+ }
2563
+ }
2564
+
2565
+ consumeSemicolon();
2566
+
2567
+ if (label === null && !(state.inIteration || state.inSwitch)) {
2568
+ throwError({}, Messages.IllegalBreak);
2569
+ }
2570
+
2571
+ return {
2572
+ type: Syntax.BreakStatement,
2573
+ label: label
2574
+ };
2575
+ }
2576
+
2577
+ // 12.9 The return statement
2578
+
2579
+ function parseReturnStatement() {
2580
+ var token, argument = null;
2581
+
2582
+ expectKeyword('return');
2583
+
2584
+ if (!state.inFunctionBody) {
2585
+ throwErrorTolerant({}, Messages.IllegalReturn);
2586
+ }
2587
+
2588
+ // 'return' followed by a space and an identifier is very common.
2589
+ if (source[index] === ' ') {
2590
+ if (isIdentifierStart(source[index + 1])) {
2591
+ argument = parseExpression();
2592
+ consumeSemicolon();
2593
+ return {
2594
+ type: Syntax.ReturnStatement,
2595
+ argument: argument
2596
+ };
2597
+ }
2598
+ }
2599
+
2600
+ if (peekLineTerminator()) {
2601
+ return {
2602
+ type: Syntax.ReturnStatement,
2603
+ argument: null
2604
+ };
2605
+ }
2606
+
2607
+ if (!match(';')) {
2608
+ token = lookahead();
2609
+ if (!match('}') && token.type !== Token.EOF) {
2610
+ argument = parseExpression();
2611
+ }
2612
+ }
2613
+
2614
+ consumeSemicolon();
2615
+
2616
+ return {
2617
+ type: Syntax.ReturnStatement,
2618
+ argument: argument
2619
+ };
2620
+ }
2621
+
2622
+ // 12.10 The with statement
2623
+
2624
+ function parseWithStatement() {
2625
+ var object, body;
2626
+
2627
+ if (strict) {
2628
+ throwErrorTolerant({}, Messages.StrictModeWith);
2629
+ }
2630
+
2631
+ expectKeyword('with');
2632
+
2633
+ expect('(');
2634
+
2635
+ object = parseExpression();
2636
+
2637
+ expect(')');
2638
+
2639
+ body = parseStatement();
2640
+
2641
+ return {
2642
+ type: Syntax.WithStatement,
2643
+ object: object,
2644
+ body: body
2645
+ };
2646
+ }
2647
+
2648
+ // 12.10 The swith statement
2649
+
2650
+ function parseSwitchCase() {
2651
+ var test,
2652
+ consequent = [],
2653
+ statement;
2654
+
2655
+ if (matchKeyword('default')) {
2656
+ lex();
2657
+ test = null;
2658
+ } else {
2659
+ expectKeyword('case');
2660
+ test = parseExpression();
2661
+ }
2662
+ expect(':');
2663
+
2664
+ while (index < length) {
2665
+ if (match('}') || matchKeyword('default') || matchKeyword('case')) {
2666
+ break;
2667
+ }
2668
+ statement = parseStatement();
2669
+ if (typeof statement === 'undefined') {
2670
+ break;
2671
+ }
2672
+ consequent.push(statement);
2673
+ }
2674
+
2675
+ return {
2676
+ type: Syntax.SwitchCase,
2677
+ test: test,
2678
+ consequent: consequent
2679
+ };
2680
+ }
2681
+
2682
+ function parseSwitchStatement() {
2683
+ var discriminant, cases, clause, oldInSwitch, defaultFound;
2684
+
2685
+ expectKeyword('switch');
2686
+
2687
+ expect('(');
2688
+
2689
+ discriminant = parseExpression();
2690
+
2691
+ expect(')');
2692
+
2693
+ expect('{');
2694
+
2695
+ cases = [];
2696
+
2697
+ if (match('}')) {
2698
+ lex();
2699
+ return {
2700
+ type: Syntax.SwitchStatement,
2701
+ discriminant: discriminant,
2702
+ cases: cases
2703
+ };
2704
+ }
2705
+
2706
+ oldInSwitch = state.inSwitch;
2707
+ state.inSwitch = true;
2708
+ defaultFound = false;
2709
+
2710
+ while (index < length) {
2711
+ if (match('}')) {
2712
+ break;
2713
+ }
2714
+ clause = parseSwitchCase();
2715
+ if (clause.test === null) {
2716
+ if (defaultFound) {
2717
+ throwError({}, Messages.MultipleDefaultsInSwitch);
2718
+ }
2719
+ defaultFound = true;
2720
+ }
2721
+ cases.push(clause);
2722
+ }
2723
+
2724
+ state.inSwitch = oldInSwitch;
2725
+
2726
+ expect('}');
2727
+
2728
+ return {
2729
+ type: Syntax.SwitchStatement,
2730
+ discriminant: discriminant,
2731
+ cases: cases
2732
+ };
2733
+ }
2734
+
2735
+ // 12.13 The throw statement
2736
+
2737
+ function parseThrowStatement() {
2738
+ var argument;
2739
+
2740
+ expectKeyword('throw');
2741
+
2742
+ if (peekLineTerminator()) {
2743
+ throwError({}, Messages.NewlineAfterThrow);
2744
+ }
2745
+
2746
+ argument = parseExpression();
2747
+
2748
+ consumeSemicolon();
2749
+
2750
+ return {
2751
+ type: Syntax.ThrowStatement,
2752
+ argument: argument
2753
+ };
2754
+ }
2755
+
2756
+ // 12.14 The try statement
2757
+
2758
+ function parseCatchClause() {
2759
+ var param;
2760
+
2761
+ expectKeyword('catch');
2762
+
2763
+ expect('(');
2764
+ if (match(')')) {
2765
+ throwUnexpected(lookahead());
2766
+ }
2767
+
2768
+ param = parseVariableIdentifier();
2769
+ // 12.14.1
2770
+ if (strict && isRestrictedWord(param.name)) {
2771
+ throwErrorTolerant({}, Messages.StrictCatchVariable);
2772
+ }
2773
+
2774
+ expect(')');
2775
+
2776
+ return {
2777
+ type: Syntax.CatchClause,
2778
+ param: param,
2779
+ body: parseBlock()
2780
+ };
2781
+ }
2782
+
2783
+ function parseTryStatement() {
2784
+ var block, handlers = [], finalizer = null;
2785
+
2786
+ expectKeyword('try');
2787
+
2788
+ block = parseBlock();
2789
+
2790
+ if (matchKeyword('catch')) {
2791
+ handlers.push(parseCatchClause());
2792
+ }
2793
+
2794
+ if (matchKeyword('finally')) {
2795
+ lex();
2796
+ finalizer = parseBlock();
2797
+ }
2798
+
2799
+ if (handlers.length === 0 && !finalizer) {
2800
+ throwError({}, Messages.NoCatchOrFinally);
2801
+ }
2802
+
2803
+ return {
2804
+ type: Syntax.TryStatement,
2805
+ block: block,
2806
+ guardedHandlers: [],
2807
+ handlers: handlers,
2808
+ finalizer: finalizer
2809
+ };
2810
+ }
2811
+
2812
+ // 12.15 The debugger statement
2813
+
2814
+ function parseDebuggerStatement() {
2815
+ expectKeyword('debugger');
2816
+
2817
+ consumeSemicolon();
2818
+
2819
+ return {
2820
+ type: Syntax.DebuggerStatement
2821
+ };
2822
+ }
2823
+
2824
+ // 12 Statements
2825
+
2826
+ function parseStatement() {
2827
+ var token = lookahead(),
2828
+ expr,
2829
+ labeledBody;
2830
+
2831
+ if (token.type === Token.EOF) {
2832
+ throwUnexpected(token);
2833
+ }
2834
+
2835
+ if (token.type === Token.Punctuator) {
2836
+ switch (token.value) {
2837
+ case ';':
2838
+ return parseEmptyStatement();
2839
+ case '{':
2840
+ return parseBlock();
2841
+ case '(':
2842
+ return parseExpressionStatement();
2843
+ }
2844
+ }
2845
+
2846
+ if (token.type === Token.Keyword) {
2847
+ switch (token.value) {
2848
+ case 'break':
2849
+ return parseBreakStatement();
2850
+ case 'continue':
2851
+ return parseContinueStatement();
2852
+ case 'debugger':
2853
+ return parseDebuggerStatement();
2854
+ case 'do':
2855
+ return parseDoWhileStatement();
2856
+ case 'for':
2857
+ return parseForStatement();
2858
+ case 'function':
2859
+ return parseFunctionDeclaration();
2860
+ case 'if':
2861
+ return parseIfStatement();
2862
+ case 'return':
2863
+ return parseReturnStatement();
2864
+ case 'switch':
2865
+ return parseSwitchStatement();
2866
+ case 'throw':
2867
+ return parseThrowStatement();
2868
+ case 'try':
2869
+ return parseTryStatement();
2870
+ case 'var':
2871
+ return parseVariableStatement();
2872
+ case 'while':
2873
+ return parseWhileStatement();
2874
+ case 'with':
2875
+ return parseWithStatement();
2876
+ }
2877
+ }
2878
+
2879
+ expr = parseExpression();
2880
+
2881
+ // 12.12 Labelled Statements
2882
+ if ((expr.type === Syntax.Identifier) && match(':')) {
2883
+ lex();
2884
+
2885
+ if (Object.prototype.hasOwnProperty.call(state.labelSet, expr.name)) {
2886
+ throwError({}, Messages.Redeclaration, 'Label', expr.name);
2887
+ }
2888
+
2889
+ state.labelSet[expr.name] = true;
2890
+ labeledBody = parseStatement();
2891
+ delete state.labelSet[expr.name];
2892
+
2893
+ return {
2894
+ type: Syntax.LabeledStatement,
2895
+ label: expr,
2896
+ body: labeledBody
2897
+ };
2898
+ }
2899
+
2900
+ consumeSemicolon();
2901
+
2902
+ return {
2903
+ type: Syntax.ExpressionStatement,
2904
+ expression: expr
2905
+ };
2906
+ }
2907
+
2908
+ // 13 Function Definition
2909
+
2910
+ function parseFunctionSourceElements() {
2911
+ var sourceElement, sourceElements = [], token, directive, firstRestricted,
2912
+ oldLabelSet, oldInIteration, oldInSwitch, oldInFunctionBody;
2913
+
2914
+ expect('{');
2915
+
2916
+ while (index < length) {
2917
+ token = lookahead();
2918
+ if (token.type !== Token.StringLiteral) {
2919
+ break;
2920
+ }
2921
+
2922
+ sourceElement = parseSourceElement();
2923
+ sourceElements.push(sourceElement);
2924
+ if (sourceElement.expression.type !== Syntax.Literal) {
2925
+ // this is not directive
2926
+ break;
2927
+ }
2928
+ directive = sliceSource(token.range[0] + 1, token.range[1] - 1);
2929
+ if (directive === 'use strict') {
2930
+ strict = true;
2931
+ if (firstRestricted) {
2932
+ throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
2933
+ }
2934
+ } else {
2935
+ if (!firstRestricted && token.octal) {
2936
+ firstRestricted = token;
2937
+ }
2938
+ }
2939
+ }
2940
+
2941
+ oldLabelSet = state.labelSet;
2942
+ oldInIteration = state.inIteration;
2943
+ oldInSwitch = state.inSwitch;
2944
+ oldInFunctionBody = state.inFunctionBody;
2945
+
2946
+ state.labelSet = {};
2947
+ state.inIteration = false;
2948
+ state.inSwitch = false;
2949
+ state.inFunctionBody = true;
2950
+
2951
+ while (index < length) {
2952
+ if (match('}')) {
2953
+ break;
2954
+ }
2955
+ sourceElement = parseSourceElement();
2956
+ if (typeof sourceElement === 'undefined') {
2957
+ break;
2958
+ }
2959
+ sourceElements.push(sourceElement);
2960
+ }
2961
+
2962
+ expect('}');
2963
+
2964
+ state.labelSet = oldLabelSet;
2965
+ state.inIteration = oldInIteration;
2966
+ state.inSwitch = oldInSwitch;
2967
+ state.inFunctionBody = oldInFunctionBody;
2968
+
2969
+ return {
2970
+ type: Syntax.BlockStatement,
2971
+ body: sourceElements
2972
+ };
2973
+ }
2974
+
2975
+ function parseFunctionDeclaration() {
2976
+ var id, param, params = [], body, token, stricted, firstRestricted, message, previousStrict, paramSet;
2977
+
2978
+ expectKeyword('function');
2979
+ token = lookahead();
2980
+ id = parseVariableIdentifier();
2981
+ if (strict) {
2982
+ if (isRestrictedWord(token.value)) {
2983
+ throwErrorTolerant(token, Messages.StrictFunctionName);
2984
+ }
2985
+ } else {
2986
+ if (isRestrictedWord(token.value)) {
2987
+ firstRestricted = token;
2988
+ message = Messages.StrictFunctionName;
2989
+ } else if (isStrictModeReservedWord(token.value)) {
2990
+ firstRestricted = token;
2991
+ message = Messages.StrictReservedWord;
2992
+ }
2993
+ }
2994
+
2995
+ expect('(');
2996
+
2997
+ if (!match(')')) {
2998
+ paramSet = {};
2999
+ while (index < length) {
3000
+ token = lookahead();
3001
+ param = parseVariableIdentifier();
3002
+ if (strict) {
3003
+ if (isRestrictedWord(token.value)) {
3004
+ stricted = token;
3005
+ message = Messages.StrictParamName;
3006
+ }
3007
+ if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3008
+ stricted = token;
3009
+ message = Messages.StrictParamDupe;
3010
+ }
3011
+ } else if (!firstRestricted) {
3012
+ if (isRestrictedWord(token.value)) {
3013
+ firstRestricted = token;
3014
+ message = Messages.StrictParamName;
3015
+ } else if (isStrictModeReservedWord(token.value)) {
3016
+ firstRestricted = token;
3017
+ message = Messages.StrictReservedWord;
3018
+ } else if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3019
+ firstRestricted = token;
3020
+ message = Messages.StrictParamDupe;
3021
+ }
3022
+ }
3023
+ params.push(param);
3024
+ paramSet[param.name] = true;
3025
+ if (match(')')) {
3026
+ break;
3027
+ }
3028
+ expect(',');
3029
+ }
3030
+ }
3031
+
3032
+ expect(')');
3033
+
3034
+ previousStrict = strict;
3035
+ body = parseFunctionSourceElements();
3036
+ if (strict && firstRestricted) {
3037
+ throwError(firstRestricted, message);
3038
+ }
3039
+ if (strict && stricted) {
3040
+ throwErrorTolerant(stricted, message);
3041
+ }
3042
+ strict = previousStrict;
3043
+
3044
+ return {
3045
+ type: Syntax.FunctionDeclaration,
3046
+ id: id,
3047
+ params: params,
3048
+ defaults: [],
3049
+ body: body,
3050
+ rest: null,
3051
+ generator: false,
3052
+ expression: false
3053
+ };
3054
+ }
3055
+
3056
+ function parseFunctionExpression() {
3057
+ var token, id = null, stricted, firstRestricted, message, param, params = [], body, previousStrict, paramSet;
3058
+
3059
+ expectKeyword('function');
3060
+
3061
+ if (!match('(')) {
3062
+ token = lookahead();
3063
+ id = parseVariableIdentifier();
3064
+ if (strict) {
3065
+ if (isRestrictedWord(token.value)) {
3066
+ throwErrorTolerant(token, Messages.StrictFunctionName);
3067
+ }
3068
+ } else {
3069
+ if (isRestrictedWord(token.value)) {
3070
+ firstRestricted = token;
3071
+ message = Messages.StrictFunctionName;
3072
+ } else if (isStrictModeReservedWord(token.value)) {
3073
+ firstRestricted = token;
3074
+ message = Messages.StrictReservedWord;
3075
+ }
3076
+ }
3077
+ }
3078
+
3079
+ expect('(');
3080
+
3081
+ if (!match(')')) {
3082
+ paramSet = {};
3083
+ while (index < length) {
3084
+ token = lookahead();
3085
+ param = parseVariableIdentifier();
3086
+ if (strict) {
3087
+ if (isRestrictedWord(token.value)) {
3088
+ stricted = token;
3089
+ message = Messages.StrictParamName;
3090
+ }
3091
+ if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3092
+ stricted = token;
3093
+ message = Messages.StrictParamDupe;
3094
+ }
3095
+ } else if (!firstRestricted) {
3096
+ if (isRestrictedWord(token.value)) {
3097
+ firstRestricted = token;
3098
+ message = Messages.StrictParamName;
3099
+ } else if (isStrictModeReservedWord(token.value)) {
3100
+ firstRestricted = token;
3101
+ message = Messages.StrictReservedWord;
3102
+ } else if (Object.prototype.hasOwnProperty.call(paramSet, token.value)) {
3103
+ firstRestricted = token;
3104
+ message = Messages.StrictParamDupe;
3105
+ }
3106
+ }
3107
+ params.push(param);
3108
+ paramSet[param.name] = true;
3109
+ if (match(')')) {
3110
+ break;
3111
+ }
3112
+ expect(',');
3113
+ }
3114
+ }
3115
+
3116
+ expect(')');
3117
+
3118
+ previousStrict = strict;
3119
+ body = parseFunctionSourceElements();
3120
+ if (strict && firstRestricted) {
3121
+ throwError(firstRestricted, message);
3122
+ }
3123
+ if (strict && stricted) {
3124
+ throwErrorTolerant(stricted, message);
3125
+ }
3126
+ strict = previousStrict;
3127
+
3128
+ return {
3129
+ type: Syntax.FunctionExpression,
3130
+ id: id,
3131
+ params: params,
3132
+ defaults: [],
3133
+ body: body,
3134
+ rest: null,
3135
+ generator: false,
3136
+ expression: false
3137
+ };
3138
+ }
3139
+
3140
+ // 14 Program
3141
+
3142
+ function parseSourceElement() {
3143
+ var token = lookahead();
3144
+
3145
+ if (token.type === Token.Keyword) {
3146
+ switch (token.value) {
3147
+ case 'const':
3148
+ case 'let':
3149
+ return parseConstLetDeclaration(token.value);
3150
+ case 'function':
3151
+ return parseFunctionDeclaration();
3152
+ default:
3153
+ return parseStatement();
3154
+ }
3155
+ }
3156
+
3157
+ if (token.type !== Token.EOF) {
3158
+ return parseStatement();
3159
+ }
3160
+ }
3161
+
3162
+ function parseSourceElements() {
3163
+ var sourceElement, sourceElements = [], token, directive, firstRestricted;
3164
+
3165
+ while (index < length) {
3166
+ token = lookahead();
3167
+ if (token.type !== Token.StringLiteral) {
3168
+ break;
3169
+ }
3170
+
3171
+ sourceElement = parseSourceElement();
3172
+ sourceElements.push(sourceElement);
3173
+ if (sourceElement.expression.type !== Syntax.Literal) {
3174
+ // this is not directive
3175
+ break;
3176
+ }
3177
+ directive = sliceSource(token.range[0] + 1, token.range[1] - 1);
3178
+ if (directive === 'use strict') {
3179
+ strict = true;
3180
+ if (firstRestricted) {
3181
+ throwErrorTolerant(firstRestricted, Messages.StrictOctalLiteral);
3182
+ }
3183
+ } else {
3184
+ if (!firstRestricted && token.octal) {
3185
+ firstRestricted = token;
3186
+ }
3187
+ }
3188
+ }
3189
+
3190
+ while (index < length) {
3191
+ sourceElement = parseSourceElement();
3192
+ if (typeof sourceElement === 'undefined') {
3193
+ break;
3194
+ }
3195
+ sourceElements.push(sourceElement);
3196
+ }
3197
+ return sourceElements;
3198
+ }
3199
+
3200
+ function parseProgram() {
3201
+ var program;
3202
+ strict = false;
3203
+ program = {
3204
+ type: Syntax.Program,
3205
+ body: parseSourceElements()
3206
+ };
3207
+ return program;
3208
+ }
3209
+
3210
+ // The following functions are needed only when the option to preserve
3211
+ // the comments is active.
3212
+
3213
+ function addComment(type, value, start, end, loc) {
3214
+ assert(typeof start === 'number', 'Comment must have valid position');
3215
+
3216
+ // Because the way the actual token is scanned, often the comments
3217
+ // (if any) are skipped twice during the lexical analysis.
3218
+ // Thus, we need to skip adding a comment if the comment array already
3219
+ // handled it.
3220
+ if (extra.comments.length > 0) {
3221
+ if (extra.comments[extra.comments.length - 1].range[1] > start) {
3222
+ return;
3223
+ }
3224
+ }
3225
+
3226
+ extra.comments.push({
3227
+ type: type,
3228
+ value: value,
3229
+ range: [start, end],
3230
+ loc: loc
3231
+ });
3232
+ }
3233
+
3234
+ function scanComment() {
3235
+ var comment, ch, loc, start, blockComment, lineComment;
3236
+
3237
+ comment = '';
3238
+ blockComment = false;
3239
+ lineComment = false;
3240
+
3241
+ while (index < length) {
3242
+ ch = source[index];
3243
+
3244
+ if (lineComment) {
3245
+ ch = source[index++];
3246
+ if (isLineTerminator(ch)) {
3247
+ loc.end = {
3248
+ line: lineNumber,
3249
+ column: index - lineStart - 1
3250
+ };
3251
+ lineComment = false;
3252
+ addComment('Line', comment, start, index - 1, loc);
3253
+ if (ch === '\r' && source[index] === '\n') {
3254
+ ++index;
3255
+ }
3256
+ ++lineNumber;
3257
+ lineStart = index;
3258
+ comment = '';
3259
+ } else if (index >= length) {
3260
+ lineComment = false;
3261
+ comment += ch;
3262
+ loc.end = {
3263
+ line: lineNumber,
3264
+ column: length - lineStart
3265
+ };
3266
+ addComment('Line', comment, start, length, loc);
3267
+ } else {
3268
+ comment += ch;
3269
+ }
3270
+ } else if (blockComment) {
3271
+ if (isLineTerminator(ch)) {
3272
+ if (ch === '\r' && source[index + 1] === '\n') {
3273
+ ++index;
3274
+ comment += '\r\n';
3275
+ } else {
3276
+ comment += ch;
3277
+ }
3278
+ ++lineNumber;
3279
+ ++index;
3280
+ lineStart = index;
3281
+ if (index >= length) {
3282
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
3283
+ }
3284
+ } else {
3285
+ ch = source[index++];
3286
+ if (index >= length) {
3287
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
3288
+ }
3289
+ comment += ch;
3290
+ if (ch === '*') {
3291
+ ch = source[index];
3292
+ if (ch === '/') {
3293
+ comment = comment.substr(0, comment.length - 1);
3294
+ blockComment = false;
3295
+ ++index;
3296
+ loc.end = {
3297
+ line: lineNumber,
3298
+ column: index - lineStart
3299
+ };
3300
+ addComment('Block', comment, start, index, loc);
3301
+ comment = '';
3302
+ }
3303
+ }
3304
+ }
3305
+ } else if (ch === '/') {
3306
+ ch = source[index + 1];
3307
+ if (ch === '/') {
3308
+ loc = {
3309
+ start: {
3310
+ line: lineNumber,
3311
+ column: index - lineStart
3312
+ }
3313
+ };
3314
+ start = index;
3315
+ index += 2;
3316
+ lineComment = true;
3317
+ if (index >= length) {
3318
+ loc.end = {
3319
+ line: lineNumber,
3320
+ column: index - lineStart
3321
+ };
3322
+ lineComment = false;
3323
+ addComment('Line', comment, start, index, loc);
3324
+ }
3325
+ } else if (ch === '*') {
3326
+ start = index;
3327
+ index += 2;
3328
+ blockComment = true;
3329
+ loc = {
3330
+ start: {
3331
+ line: lineNumber,
3332
+ column: index - lineStart - 2
3333
+ }
3334
+ };
3335
+ if (index >= length) {
3336
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
3337
+ }
3338
+ } else {
3339
+ break;
3340
+ }
3341
+ } else if (isWhiteSpace(ch)) {
3342
+ ++index;
3343
+ } else if (isLineTerminator(ch)) {
3344
+ ++index;
3345
+ if (ch === '\r' && source[index] === '\n') {
3346
+ ++index;
3347
+ }
3348
+ ++lineNumber;
3349
+ lineStart = index;
3350
+ } else {
3351
+ break;
3352
+ }
3353
+ }
3354
+ }
3355
+
3356
+ function filterCommentLocation() {
3357
+ var i, entry, comment, comments = [];
3358
+
3359
+ for (i = 0; i < extra.comments.length; ++i) {
3360
+ entry = extra.comments[i];
3361
+ comment = {
3362
+ type: entry.type,
3363
+ value: entry.value
3364
+ };
3365
+ if (extra.range) {
3366
+ comment.range = entry.range;
3367
+ }
3368
+ if (extra.loc) {
3369
+ comment.loc = entry.loc;
3370
+ }
3371
+ comments.push(comment);
3372
+ }
3373
+
3374
+ extra.comments = comments;
3375
+ }
3376
+
3377
+ function collectToken() {
3378
+ var loc, token, range, value;
3379
+
3380
+ skipComment();
3381
+ loc = {
3382
+ start: {
3383
+ line: lineNumber,
3384
+ column: index - lineStart
3385
+ }
3386
+ };
3387
+
3388
+ token = extra.advance();
3389
+ loc.end = {
3390
+ line: lineNumber,
3391
+ column: index - lineStart
3392
+ };
3393
+
3394
+ if (token.type !== Token.EOF) {
3395
+ range = [token.range[0], token.range[1]];
3396
+ value = sliceSource(token.range[0], token.range[1]);
3397
+ extra.tokens.push({
3398
+ type: TokenName[token.type],
3399
+ value: value,
3400
+ range: range,
3401
+ loc: loc
3402
+ });
3403
+ }
3404
+
3405
+ return token;
3406
+ }
3407
+
3408
+ function collectRegex() {
3409
+ var pos, loc, regex, token;
3410
+
3411
+ skipComment();
3412
+
3413
+ pos = index;
3414
+ loc = {
3415
+ start: {
3416
+ line: lineNumber,
3417
+ column: index - lineStart
3418
+ }
3419
+ };
3420
+
3421
+ regex = extra.scanRegExp();
3422
+ loc.end = {
3423
+ line: lineNumber,
3424
+ column: index - lineStart
3425
+ };
3426
+
3427
+ // Pop the previous token, which is likely '/' or '/='
3428
+ if (extra.tokens.length > 0) {
3429
+ token = extra.tokens[extra.tokens.length - 1];
3430
+ if (token.range[0] === pos && token.type === 'Punctuator') {
3431
+ if (token.value === '/' || token.value === '/=') {
3432
+ extra.tokens.pop();
3433
+ }
3434
+ }
3435
+ }
3436
+
3437
+ extra.tokens.push({
3438
+ type: 'RegularExpression',
3439
+ value: regex.literal,
3440
+ range: [pos, index],
3441
+ loc: loc
3442
+ });
3443
+
3444
+ return regex;
3445
+ }
3446
+
3447
+ function filterTokenLocation() {
3448
+ var i, entry, token, tokens = [];
3449
+
3450
+ for (i = 0; i < extra.tokens.length; ++i) {
3451
+ entry = extra.tokens[i];
3452
+ token = {
3453
+ type: entry.type,
3454
+ value: entry.value
3455
+ };
3456
+ if (extra.range) {
3457
+ token.range = entry.range;
3458
+ }
3459
+ if (extra.loc) {
3460
+ token.loc = entry.loc;
3461
+ }
3462
+ tokens.push(token);
3463
+ }
3464
+
3465
+ extra.tokens = tokens;
3466
+ }
3467
+
3468
+ function createLiteral(token) {
3469
+ return {
3470
+ type: Syntax.Literal,
3471
+ value: token.value
3472
+ };
3473
+ }
3474
+
3475
+ function createRawLiteral(token) {
3476
+ return {
3477
+ type: Syntax.Literal,
3478
+ value: token.value,
3479
+ raw: sliceSource(token.range[0], token.range[1])
3480
+ };
3481
+ }
3482
+
3483
+ function createLocationMarker() {
3484
+ var marker = {};
3485
+
3486
+ marker.range = [index, index];
3487
+ marker.loc = {
3488
+ start: {
3489
+ line: lineNumber,
3490
+ column: index - lineStart
3491
+ },
3492
+ end: {
3493
+ line: lineNumber,
3494
+ column: index - lineStart
3495
+ }
3496
+ };
3497
+
3498
+ marker.end = function () {
3499
+ this.range[1] = index;
3500
+ this.loc.end.line = lineNumber;
3501
+ this.loc.end.column = index - lineStart;
3502
+ };
3503
+
3504
+ marker.applyGroup = function (node) {
3505
+ if (extra.range) {
3506
+ node.groupRange = [this.range[0], this.range[1]];
3507
+ }
3508
+ if (extra.loc) {
3509
+ node.groupLoc = {
3510
+ start: {
3511
+ line: this.loc.start.line,
3512
+ column: this.loc.start.column
3513
+ },
3514
+ end: {
3515
+ line: this.loc.end.line,
3516
+ column: this.loc.end.column
3517
+ }
3518
+ };
3519
+ }
3520
+ };
3521
+
3522
+ marker.apply = function (node) {
3523
+ if (extra.range) {
3524
+ node.range = [this.range[0], this.range[1]];
3525
+ }
3526
+ if (extra.loc) {
3527
+ node.loc = {
3528
+ start: {
3529
+ line: this.loc.start.line,
3530
+ column: this.loc.start.column
3531
+ },
3532
+ end: {
3533
+ line: this.loc.end.line,
3534
+ column: this.loc.end.column
3535
+ }
3536
+ };
3537
+ }
3538
+ };
3539
+
3540
+ return marker;
3541
+ }
3542
+
3543
+ function trackGroupExpression() {
3544
+ var marker, expr;
3545
+
3546
+ skipComment();
3547
+ marker = createLocationMarker();
3548
+ expect('(');
3549
+
3550
+ expr = parseExpression();
3551
+
3552
+ expect(')');
3553
+
3554
+ marker.end();
3555
+ marker.applyGroup(expr);
3556
+
3557
+ return expr;
3558
+ }
3559
+
3560
+ function trackLeftHandSideExpression() {
3561
+ var marker, expr;
3562
+
3563
+ skipComment();
3564
+ marker = createLocationMarker();
3565
+
3566
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
3567
+
3568
+ while (match('.') || match('[')) {
3569
+ if (match('[')) {
3570
+ expr = {
3571
+ type: Syntax.MemberExpression,
3572
+ computed: true,
3573
+ object: expr,
3574
+ property: parseComputedMember()
3575
+ };
3576
+ marker.end();
3577
+ marker.apply(expr);
3578
+ } else {
3579
+ expr = {
3580
+ type: Syntax.MemberExpression,
3581
+ computed: false,
3582
+ object: expr,
3583
+ property: parseNonComputedMember()
3584
+ };
3585
+ marker.end();
3586
+ marker.apply(expr);
3587
+ }
3588
+ }
3589
+
3590
+ return expr;
3591
+ }
3592
+
3593
+ function trackLeftHandSideExpressionAllowCall() {
3594
+ var marker, expr;
3595
+
3596
+ skipComment();
3597
+ marker = createLocationMarker();
3598
+
3599
+ expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();
3600
+
3601
+ while (match('.') || match('[') || match('(')) {
3602
+ if (match('(')) {
3603
+ expr = {
3604
+ type: Syntax.CallExpression,
3605
+ callee: expr,
3606
+ 'arguments': parseArguments()
3607
+ };
3608
+ marker.end();
3609
+ marker.apply(expr);
3610
+ } else if (match('[')) {
3611
+ expr = {
3612
+ type: Syntax.MemberExpression,
3613
+ computed: true,
3614
+ object: expr,
3615
+ property: parseComputedMember()
3616
+ };
3617
+ marker.end();
3618
+ marker.apply(expr);
3619
+ } else {
3620
+ expr = {
3621
+ type: Syntax.MemberExpression,
3622
+ computed: false,
3623
+ object: expr,
3624
+ property: parseNonComputedMember()
3625
+ };
3626
+ marker.end();
3627
+ marker.apply(expr);
3628
+ }
3629
+ }
3630
+
3631
+ return expr;
3632
+ }
3633
+
3634
+ function filterGroup(node) {
3635
+ var n, i, entry;
3636
+
3637
+ n = (Object.prototype.toString.apply(node) === '[object Array]') ? [] : {};
3638
+ for (i in node) {
3639
+ if (node.hasOwnProperty(i) && i !== 'groupRange' && i !== 'groupLoc') {
3640
+ entry = node[i];
3641
+ if (entry === null || typeof entry !== 'object' || entry instanceof RegExp) {
3642
+ n[i] = entry;
3643
+ } else {
3644
+ n[i] = filterGroup(entry);
3645
+ }
3646
+ }
3647
+ }
3648
+ return n;
3649
+ }
3650
+
3651
+ function wrapTrackingFunction(range, loc) {
3652
+
3653
+ return function (parseFunction) {
3654
+
3655
+ function isBinary(node) {
3656
+ return node.type === Syntax.LogicalExpression ||
3657
+ node.type === Syntax.BinaryExpression;
3658
+ }
3659
+
3660
+ function visit(node) {
3661
+ var start, end;
3662
+
3663
+ if (isBinary(node.left)) {
3664
+ visit(node.left);
3665
+ }
3666
+ if (isBinary(node.right)) {
3667
+ visit(node.right);
3668
+ }
3669
+
3670
+ if (range) {
3671
+ if (node.left.groupRange || node.right.groupRange) {
3672
+ start = node.left.groupRange ? node.left.groupRange[0] : node.left.range[0];
3673
+ end = node.right.groupRange ? node.right.groupRange[1] : node.right.range[1];
3674
+ node.range = [start, end];
3675
+ } else if (typeof node.range === 'undefined') {
3676
+ start = node.left.range[0];
3677
+ end = node.right.range[1];
3678
+ node.range = [start, end];
3679
+ }
3680
+ }
3681
+ if (loc) {
3682
+ if (node.left.groupLoc || node.right.groupLoc) {
3683
+ start = node.left.groupLoc ? node.left.groupLoc.start : node.left.loc.start;
3684
+ end = node.right.groupLoc ? node.right.groupLoc.end : node.right.loc.end;
3685
+ node.loc = {
3686
+ start: start,
3687
+ end: end
3688
+ };
3689
+ } else if (typeof node.loc === 'undefined') {
3690
+ node.loc = {
3691
+ start: node.left.loc.start,
3692
+ end: node.right.loc.end
3693
+ };
3694
+ }
3695
+ }
3696
+ }
3697
+
3698
+ return function () {
3699
+ var marker, node;
3700
+
3701
+ skipComment();
3702
+
3703
+ marker = createLocationMarker();
3704
+ node = parseFunction.apply(null, arguments);
3705
+ marker.end();
3706
+
3707
+ if (range && typeof node.range === 'undefined') {
3708
+ marker.apply(node);
3709
+ }
3710
+
3711
+ if (loc && typeof node.loc === 'undefined') {
3712
+ marker.apply(node);
3713
+ }
3714
+
3715
+ if (isBinary(node)) {
3716
+ visit(node);
3717
+ }
3718
+
3719
+ return node;
3720
+ };
3721
+ };
3722
+ }
3723
+
3724
+ function patch() {
3725
+
3726
+ var wrapTracking;
3727
+
3728
+ if (extra.comments) {
3729
+ extra.skipComment = skipComment;
3730
+ skipComment = scanComment;
3731
+ }
3732
+
3733
+ if (extra.raw) {
3734
+ extra.createLiteral = createLiteral;
3735
+ createLiteral = createRawLiteral;
3736
+ }
3737
+
3738
+ if (extra.range || extra.loc) {
3739
+
3740
+ extra.parseGroupExpression = parseGroupExpression;
3741
+ extra.parseLeftHandSideExpression = parseLeftHandSideExpression;
3742
+ extra.parseLeftHandSideExpressionAllowCall = parseLeftHandSideExpressionAllowCall;
3743
+ parseGroupExpression = trackGroupExpression;
3744
+ parseLeftHandSideExpression = trackLeftHandSideExpression;
3745
+ parseLeftHandSideExpressionAllowCall = trackLeftHandSideExpressionAllowCall;
3746
+
3747
+ wrapTracking = wrapTrackingFunction(extra.range, extra.loc);
3748
+
3749
+ extra.parseAdditiveExpression = parseAdditiveExpression;
3750
+ extra.parseAssignmentExpression = parseAssignmentExpression;
3751
+ extra.parseBitwiseANDExpression = parseBitwiseANDExpression;
3752
+ extra.parseBitwiseORExpression = parseBitwiseORExpression;
3753
+ extra.parseBitwiseXORExpression = parseBitwiseXORExpression;
3754
+ extra.parseBlock = parseBlock;
3755
+ extra.parseFunctionSourceElements = parseFunctionSourceElements;
3756
+ extra.parseCatchClause = parseCatchClause;
3757
+ extra.parseComputedMember = parseComputedMember;
3758
+ extra.parseConditionalExpression = parseConditionalExpression;
3759
+ extra.parseConstLetDeclaration = parseConstLetDeclaration;
3760
+ extra.parseEqualityExpression = parseEqualityExpression;
3761
+ extra.parseExpression = parseExpression;
3762
+ extra.parseForVariableDeclaration = parseForVariableDeclaration;
3763
+ extra.parseFunctionDeclaration = parseFunctionDeclaration;
3764
+ extra.parseFunctionExpression = parseFunctionExpression;
3765
+ extra.parseLogicalANDExpression = parseLogicalANDExpression;
3766
+ extra.parseLogicalORExpression = parseLogicalORExpression;
3767
+ extra.parseMultiplicativeExpression = parseMultiplicativeExpression;
3768
+ extra.parseNewExpression = parseNewExpression;
3769
+ extra.parseNonComputedProperty = parseNonComputedProperty;
3770
+ extra.parseObjectProperty = parseObjectProperty;
3771
+ extra.parseObjectPropertyKey = parseObjectPropertyKey;
3772
+ extra.parsePostfixExpression = parsePostfixExpression;
3773
+ extra.parsePrimaryExpression = parsePrimaryExpression;
3774
+ extra.parseProgram = parseProgram;
3775
+ extra.parsePropertyFunction = parsePropertyFunction;
3776
+ extra.parseRelationalExpression = parseRelationalExpression;
3777
+ extra.parseStatement = parseStatement;
3778
+ extra.parseShiftExpression = parseShiftExpression;
3779
+ extra.parseSwitchCase = parseSwitchCase;
3780
+ extra.parseUnaryExpression = parseUnaryExpression;
3781
+ extra.parseVariableDeclaration = parseVariableDeclaration;
3782
+ extra.parseVariableIdentifier = parseVariableIdentifier;
3783
+
3784
+ parseAdditiveExpression = wrapTracking(extra.parseAdditiveExpression);
3785
+ parseAssignmentExpression = wrapTracking(extra.parseAssignmentExpression);
3786
+ parseBitwiseANDExpression = wrapTracking(extra.parseBitwiseANDExpression);
3787
+ parseBitwiseORExpression = wrapTracking(extra.parseBitwiseORExpression);
3788
+ parseBitwiseXORExpression = wrapTracking(extra.parseBitwiseXORExpression);
3789
+ parseBlock = wrapTracking(extra.parseBlock);
3790
+ parseFunctionSourceElements = wrapTracking(extra.parseFunctionSourceElements);
3791
+ parseCatchClause = wrapTracking(extra.parseCatchClause);
3792
+ parseComputedMember = wrapTracking(extra.parseComputedMember);
3793
+ parseConditionalExpression = wrapTracking(extra.parseConditionalExpression);
3794
+ parseConstLetDeclaration = wrapTracking(extra.parseConstLetDeclaration);
3795
+ parseEqualityExpression = wrapTracking(extra.parseEqualityExpression);
3796
+ parseExpression = wrapTracking(extra.parseExpression);
3797
+ parseForVariableDeclaration = wrapTracking(extra.parseForVariableDeclaration);
3798
+ parseFunctionDeclaration = wrapTracking(extra.parseFunctionDeclaration);
3799
+ parseFunctionExpression = wrapTracking(extra.parseFunctionExpression);
3800
+ parseLeftHandSideExpression = wrapTracking(parseLeftHandSideExpression);
3801
+ parseLogicalANDExpression = wrapTracking(extra.parseLogicalANDExpression);
3802
+ parseLogicalORExpression = wrapTracking(extra.parseLogicalORExpression);
3803
+ parseMultiplicativeExpression = wrapTracking(extra.parseMultiplicativeExpression);
3804
+ parseNewExpression = wrapTracking(extra.parseNewExpression);
3805
+ parseNonComputedProperty = wrapTracking(extra.parseNonComputedProperty);
3806
+ parseObjectProperty = wrapTracking(extra.parseObjectProperty);
3807
+ parseObjectPropertyKey = wrapTracking(extra.parseObjectPropertyKey);
3808
+ parsePostfixExpression = wrapTracking(extra.parsePostfixExpression);
3809
+ parsePrimaryExpression = wrapTracking(extra.parsePrimaryExpression);
3810
+ parseProgram = wrapTracking(extra.parseProgram);
3811
+ parsePropertyFunction = wrapTracking(extra.parsePropertyFunction);
3812
+ parseRelationalExpression = wrapTracking(extra.parseRelationalExpression);
3813
+ parseStatement = wrapTracking(extra.parseStatement);
3814
+ parseShiftExpression = wrapTracking(extra.parseShiftExpression);
3815
+ parseSwitchCase = wrapTracking(extra.parseSwitchCase);
3816
+ parseUnaryExpression = wrapTracking(extra.parseUnaryExpression);
3817
+ parseVariableDeclaration = wrapTracking(extra.parseVariableDeclaration);
3818
+ parseVariableIdentifier = wrapTracking(extra.parseVariableIdentifier);
3819
+ }
3820
+
3821
+ if (typeof extra.tokens !== 'undefined') {
3822
+ extra.advance = advance;
3823
+ extra.scanRegExp = scanRegExp;
3824
+
3825
+ advance = collectToken;
3826
+ scanRegExp = collectRegex;
3827
+ }
3828
+ }
3829
+
3830
+ function unpatch() {
3831
+ if (typeof extra.skipComment === 'function') {
3832
+ skipComment = extra.skipComment;
3833
+ }
3834
+
3835
+ if (extra.raw) {
3836
+ createLiteral = extra.createLiteral;
3837
+ }
3838
+
3839
+ if (extra.range || extra.loc) {
3840
+ parseAdditiveExpression = extra.parseAdditiveExpression;
3841
+ parseAssignmentExpression = extra.parseAssignmentExpression;
3842
+ parseBitwiseANDExpression = extra.parseBitwiseANDExpression;
3843
+ parseBitwiseORExpression = extra.parseBitwiseORExpression;
3844
+ parseBitwiseXORExpression = extra.parseBitwiseXORExpression;
3845
+ parseBlock = extra.parseBlock;
3846
+ parseFunctionSourceElements = extra.parseFunctionSourceElements;
3847
+ parseCatchClause = extra.parseCatchClause;
3848
+ parseComputedMember = extra.parseComputedMember;
3849
+ parseConditionalExpression = extra.parseConditionalExpression;
3850
+ parseConstLetDeclaration = extra.parseConstLetDeclaration;
3851
+ parseEqualityExpression = extra.parseEqualityExpression;
3852
+ parseExpression = extra.parseExpression;
3853
+ parseForVariableDeclaration = extra.parseForVariableDeclaration;
3854
+ parseFunctionDeclaration = extra.parseFunctionDeclaration;
3855
+ parseFunctionExpression = extra.parseFunctionExpression;
3856
+ parseGroupExpression = extra.parseGroupExpression;
3857
+ parseLeftHandSideExpression = extra.parseLeftHandSideExpression;
3858
+ parseLeftHandSideExpressionAllowCall = extra.parseLeftHandSideExpressionAllowCall;
3859
+ parseLogicalANDExpression = extra.parseLogicalANDExpression;
3860
+ parseLogicalORExpression = extra.parseLogicalORExpression;
3861
+ parseMultiplicativeExpression = extra.parseMultiplicativeExpression;
3862
+ parseNewExpression = extra.parseNewExpression;
3863
+ parseNonComputedProperty = extra.parseNonComputedProperty;
3864
+ parseObjectProperty = extra.parseObjectProperty;
3865
+ parseObjectPropertyKey = extra.parseObjectPropertyKey;
3866
+ parsePrimaryExpression = extra.parsePrimaryExpression;
3867
+ parsePostfixExpression = extra.parsePostfixExpression;
3868
+ parseProgram = extra.parseProgram;
3869
+ parsePropertyFunction = extra.parsePropertyFunction;
3870
+ parseRelationalExpression = extra.parseRelationalExpression;
3871
+ parseStatement = extra.parseStatement;
3872
+ parseShiftExpression = extra.parseShiftExpression;
3873
+ parseSwitchCase = extra.parseSwitchCase;
3874
+ parseUnaryExpression = extra.parseUnaryExpression;
3875
+ parseVariableDeclaration = extra.parseVariableDeclaration;
3876
+ parseVariableIdentifier = extra.parseVariableIdentifier;
3877
+ }
3878
+
3879
+ if (typeof extra.scanRegExp === 'function') {
3880
+ advance = extra.advance;
3881
+ scanRegExp = extra.scanRegExp;
3882
+ }
3883
+ }
3884
+
3885
+ function stringToArray(str) {
3886
+ var length = str.length,
3887
+ result = [],
3888
+ i;
3889
+ for (i = 0; i < length; ++i) {
3890
+ result[i] = str.charAt(i);
3891
+ }
3892
+ return result;
3893
+ }
3894
+
3895
+ function parse(code, options) {
3896
+ var program, toString;
3897
+
3898
+ toString = String;
3899
+ if (typeof code !== 'string' && !(code instanceof String)) {
3900
+ code = toString(code);
3901
+ }
3902
+
3903
+ source = code;
3904
+ index = 0;
3905
+ lineNumber = (source.length > 0) ? 1 : 0;
3906
+ lineStart = 0;
3907
+ length = source.length;
3908
+ buffer = null;
3909
+ state = {
3910
+ allowIn: true,
3911
+ labelSet: {},
3912
+ inFunctionBody: false,
3913
+ inIteration: false,
3914
+ inSwitch: false
3915
+ };
3916
+
3917
+ extra = {};
3918
+ if (typeof options !== 'undefined') {
3919
+ extra.range = (typeof options.range === 'boolean') && options.range;
3920
+ extra.loc = (typeof options.loc === 'boolean') && options.loc;
3921
+ extra.raw = (typeof options.raw === 'boolean') && options.raw;
3922
+ if (typeof options.tokens === 'boolean' && options.tokens) {
3923
+ extra.tokens = [];
3924
+ }
3925
+ if (typeof options.comment === 'boolean' && options.comment) {
3926
+ extra.comments = [];
3927
+ }
3928
+ if (typeof options.tolerant === 'boolean' && options.tolerant) {
3929
+ extra.errors = [];
3930
+ }
3931
+ }
3932
+
3933
+ if (length > 0) {
3934
+ if (typeof source[0] === 'undefined') {
3935
+ // Try first to convert to a string. This is good as fast path
3936
+ // for old IE which understands string indexing for string
3937
+ // literals only and not for string object.
3938
+ if (code instanceof String) {
3939
+ source = code.valueOf();
3940
+ }
3941
+
3942
+ // Force accessing the characters via an array.
3943
+ if (typeof source[0] === 'undefined') {
3944
+ source = stringToArray(code);
3945
+ }
3946
+ }
3947
+ }
3948
+
3949
+ patch();
3950
+ try {
3951
+ program = parseProgram();
3952
+ if (typeof extra.comments !== 'undefined') {
3953
+ filterCommentLocation();
3954
+ program.comments = extra.comments;
3955
+ }
3956
+ if (typeof extra.tokens !== 'undefined') {
3957
+ filterTokenLocation();
3958
+ program.tokens = extra.tokens;
3959
+ }
3960
+ if (typeof extra.errors !== 'undefined') {
3961
+ program.errors = extra.errors;
3962
+ }
3963
+ if (extra.range || extra.loc) {
3964
+ program.body = filterGroup(program.body);
3965
+ }
3966
+ } catch (e) {
3967
+ throw e;
3968
+ } finally {
3969
+ unpatch();
3970
+ extra = {};
3971
+ }
3972
+
3973
+ return program;
3974
+ }
3975
+
3976
+ // Sync with package.json.
3977
+ exports.version = '1.0.4';
3978
+
3979
+ exports.parse = parse;
3980
+
3981
+ // Deep copy.
3982
+ exports.Syntax = (function () {
3983
+ var name, types = {};
3984
+
3985
+ if (typeof Object.create === 'function') {
3986
+ types = Object.create(null);
3987
+ }
3988
+
3989
+ for (name in Syntax) {
3990
+ if (Syntax.hasOwnProperty(name)) {
3991
+ types[name] = Syntax[name];
3992
+ }
3993
+ }
3994
+
3995
+ if (typeof Object.freeze === 'function') {
3996
+ Object.freeze(types);
3997
+ }
3998
+
3999
+ return types;
4000
+ }());
4001
+
4002
+ }));
4003
+ /* vim: set sw=4 ts=4 et tw=80 : */
4004
+ });
4005
+
4006
+ var hoister = hoist;
4007
+
4008
+ function hoist(ast){
4009
+
4010
+ var parentStack = [];
4011
+ var variables = [];
4012
+ var functions = [];
4013
+
4014
+ if (Array.isArray(ast)){
4015
+
4016
+ walkAll(ast);
4017
+ prependScope(ast, variables, functions);
4018
+
4019
+ } else {
4020
+ walk(ast);
4021
+ }
4022
+
4023
+ return ast
4024
+
4025
+ // walk through each node of a program of block statement
4026
+ function walkAll(nodes){
4027
+ var result = null;
4028
+ for (var i=0;i<nodes.length;i++){
4029
+ var childNode = nodes[i];
4030
+ if (childNode.type === 'EmptyStatement') continue
4031
+ var result = walk(childNode);
4032
+ if (result === 'remove'){
4033
+ nodes.splice(i--, 1);
4034
+ }
4035
+ }
4036
+ }
4037
+
4038
+ function walk(node){
4039
+ var parent = parentStack[parentStack.length-1];
4040
+ var remove = false;
4041
+ parentStack.push(node);
4042
+
4043
+ var excludeBody = false;
4044
+ if (shouldScope(node, parent)){
4045
+ hoist(node.body);
4046
+ excludeBody = true;
4047
+ }
4048
+
4049
+ if (node.type === 'VariableDeclarator'){
4050
+ variables.push(node);
4051
+ }
4052
+
4053
+ if (node.type === 'FunctionDeclaration'){
4054
+ functions.push(node);
4055
+ remove = true;
4056
+ }
4057
+
4058
+ for (var key in node){
4059
+ if (key === 'type' || (excludeBody && key === 'body')) continue
4060
+ if (key in node && node[key] && typeof node[key] == 'object'){
4061
+ if (node[key].type){
4062
+ walk(node[key]);
4063
+ } else if (Array.isArray(node[key])){
4064
+ walkAll(node[key]);
4065
+ }
4066
+ }
4067
+ }
4068
+
4069
+ parentStack.pop();
4070
+ if (remove){
4071
+ return 'remove'
4072
+ }
4073
+ }
4074
+ }
4075
+
4076
+ function shouldScope(node, parent){
4077
+ if (node.type === 'Program'){
4078
+ return true
4079
+ } else if (node.type === 'BlockStatement'){
4080
+ if (parent && (parent.type === 'FunctionExpression' || parent.type === 'FunctionDeclaration')){
4081
+ return true
4082
+ }
4083
+ }
4084
+ }
4085
+
4086
+ function prependScope(nodes, variables, functions){
4087
+ if (variables && variables.length){
4088
+ var declarations = [];
4089
+ for (var i=0;i<variables.length;i++){
4090
+ declarations.push({
4091
+ type: 'VariableDeclarator',
4092
+ id: variables[i].id,
4093
+ init: null
4094
+ });
4095
+ }
4096
+
4097
+ nodes.unshift({
4098
+ type: 'VariableDeclaration',
4099
+ kind: 'var',
4100
+ declarations: declarations
4101
+ });
4102
+
4103
+ }
4104
+
4105
+ if (functions && functions.length){
4106
+ for (var i=0;i<functions.length;i++){
4107
+ nodes.unshift(functions[i]);
4108
+ }
4109
+ }
4110
+ }
4111
+
4112
+ var infiniteChecker = InfiniteChecker;
4113
+
4114
+ function InfiniteChecker(maxIterations){
4115
+ if (this instanceof InfiniteChecker){
4116
+ this.maxIterations = maxIterations;
4117
+ this.count = 0;
4118
+ } else {
4119
+ return new InfiniteChecker(maxIterations)
4120
+ }
4121
+ }
4122
+
4123
+ InfiniteChecker.prototype.check = function(){
4124
+ this.count += 1;
4125
+ if (this.count > this.maxIterations){
4126
+ throw new Error('Infinite loop detected - reached max iterations')
4127
+ }
4128
+ };
4129
+
4130
+ var names = ['Object', 'String', 'Boolean', 'Number', 'RegExp', 'Date', 'Array', 'Function'];
4131
+ var immutable = {string: 'String', boolean: 'Boolean', number: 'Number' };
4132
+
4133
+ var primitives = names.map(getGlobal);
4134
+ var protos = primitives.map(getProto);
4135
+
4136
+ var primitives_1 = Primitives;
4137
+
4138
+ function Primitives(context){
4139
+ if (this instanceof Primitives){
4140
+ this.context = context;
4141
+ for (var i=0;i<names.length;i++){
4142
+ if (!this.context[names[i]]){
4143
+ this.context[names[i]] = wrap(primitives[i]);
4144
+ }
4145
+ }
4146
+ } else {
4147
+ return new Primitives(context)
4148
+ }
4149
+ }
4150
+
4151
+ Primitives.prototype.replace = function(value){
4152
+ var primIndex = primitives.indexOf(value);
4153
+ var protoIndex = protos.indexOf(value);
4154
+
4155
+ if (~primIndex){
4156
+ var name = names[primIndex];
4157
+ return this.context[name]
4158
+ } else if (~protoIndex) {
4159
+ var name = names[protoIndex];
4160
+ return this.context[name].prototype
4161
+ } else {
4162
+ return value
4163
+ }
4164
+ };
4165
+
4166
+ Primitives.prototype.getPropertyObject = function(object, property){
4167
+ if (immutable[typeof object]){
4168
+ return this.getPrototypeOf(object)
4169
+ }
4170
+ return object
4171
+ };
4172
+
4173
+ Primitives.prototype.isPrimitive = function(value){
4174
+ return !!~primitives.indexOf(value) || !!~protos.indexOf(value)
4175
+ };
4176
+
4177
+ Primitives.prototype.getPrototypeOf = function(value){
4178
+ if (value == null){ // handle null and undefined
4179
+ return value
4180
+ }
4181
+
4182
+ var immutableType = immutable[typeof value];
4183
+ if (immutableType){
4184
+ var proto = this.context[immutableType].prototype;
4185
+ } else {
4186
+ var proto = Object.getPrototypeOf(value);
4187
+ }
4188
+
4189
+ if (!proto || proto === Object.prototype){
4190
+ return null
4191
+ } else {
4192
+ var replacement = this.replace(proto);
4193
+ if (replacement === value){
4194
+ replacement = this.replace(Object.prototype);
4195
+ }
4196
+ return replacement
4197
+ }
4198
+ };
4199
+
4200
+ Primitives.prototype.applyNew = function(func, args){
4201
+ if (func.wrapped){
4202
+ var prim = Object.getPrototypeOf(func);
4203
+ var instance = new (Function.prototype.bind.apply(prim, arguments));
4204
+ setProto(instance, func.prototype);
4205
+ return instance
4206
+ } else {
4207
+ return new (Function.prototype.bind.apply(func, arguments))
4208
+ }
4209
+ };
4210
+
4211
+ function getProto(func){
4212
+ return func.prototype
4213
+ }
4214
+
4215
+ function getGlobal(str){
4216
+ return commonjsGlobal[str]
4217
+ }
4218
+
4219
+ function setProto(obj, proto){
4220
+ obj.__proto__ = proto;
4221
+ }
4222
+
4223
+ function wrap(prim){
4224
+ var proto = Object.create(prim.prototype);
4225
+
4226
+ var result = function() {
4227
+ if (this instanceof result){
4228
+ prim.apply(this, arguments);
4229
+ } else {
4230
+ var instance = prim.apply(null, arguments);
4231
+ setProto(instance, proto);
4232
+ return instance
4233
+ }
4234
+ };
4235
+ setProto(result, prim);
4236
+ result.prototype = proto;
4237
+ result.wrapped = true;
4238
+ return result
4239
+ }
4240
+
4241
+ var parse$2 = esprima.parse;
4242
+
4243
+
4244
+
4245
+
4246
+
4247
+ var notevil = safeEval;
4248
+ var _eval = safeEval;
4249
+ var FunctionFactory_1 = FunctionFactory;
4250
+ var _Function = FunctionFactory();
4251
+
4252
+ var maxIterations = 1000000;
4253
+
4254
+ // 'eval' with a controlled environment
4255
+ function safeEval(src, parentContext){
4256
+ var tree = prepareAst(src);
4257
+ var context = Object.create(parentContext || {});
4258
+ return finalValue(evaluateAst(tree, context))
4259
+ }
4260
+
4261
+ // create a 'Function' constructor for a controlled environment
4262
+ function FunctionFactory(parentContext){
4263
+ var context = Object.create(parentContext || {});
4264
+ return function Function() {
4265
+ // normalize arguments array
4266
+ var args = Array.prototype.slice.call(arguments);
4267
+ var src = args.slice(-1)[0];
4268
+ args = args.slice(0,-1);
4269
+ if (typeof src === 'string'){
4270
+ //HACK: esprima doesn't like returns outside functions
4271
+ src = parse$2('function a(){' + src + '}').body[0].body;
4272
+ }
4273
+ var tree = prepareAst(src);
4274
+ return getFunction(tree, args, context)
4275
+ }
4276
+ }
4277
+
4278
+ // takes an AST or js source and returns an AST
4279
+ function prepareAst(src){
4280
+ var tree = (typeof src === 'string') ? parse$2(src, {loc: true}) : src;
4281
+ return hoister(tree)
4282
+ }
4283
+
4284
+ // evaluate an AST in the given context
4285
+ function evaluateAst(tree, context){
4286
+
4287
+ var safeFunction = FunctionFactory(context);
4288
+ var primitives = primitives_1(context);
4289
+
4290
+ // block scoped context for catch (ex) and 'let'
4291
+ var blockContext = context;
4292
+
4293
+ return walk(tree)
4294
+
4295
+ // recursively walk every node in an array
4296
+ function walkAll(nodes){
4297
+ var result = undefined;
4298
+ for (var i=0;i<nodes.length;i++){
4299
+ var childNode = nodes[i];
4300
+ if (childNode.type === 'EmptyStatement') continue
4301
+ result = walk(childNode);
4302
+ if (result instanceof ReturnValue){
4303
+ return result
4304
+ }
4305
+ }
4306
+ return result
4307
+ }
4308
+
4309
+ // recursively evalutate the node of an AST
4310
+ function walk(node, traceNode){
4311
+ try {
4312
+ if (!node) return
4313
+ switch (node.type) {
4314
+
4315
+ case 'Program':
4316
+ return walkAll(node.body)
4317
+
4318
+ case 'BlockStatement':
4319
+ enterBlock();
4320
+ var result = walkAll(node.body);
4321
+ leaveBlock();
4322
+ return result
4323
+
4324
+ case 'FunctionDeclaration':
4325
+ var params = node.params.map(getName);
4326
+ var value = getFunction(node.body, params, blockContext, node);
4327
+ return context[node.id.name] = value
4328
+
4329
+ case 'FunctionExpression':
4330
+ var params = node.params.map(getName);
4331
+
4332
+ // HACK: trace the function name for stack traces
4333
+ if (!node.id && traceNode && traceNode.key && traceNode.key.type === 'Identifier') {
4334
+ node.id = traceNode.key;
4335
+ }
4336
+
4337
+ return getFunction(node.body, params, blockContext, node)
4338
+
4339
+ case 'ReturnStatement':
4340
+ var value = walk(node.argument);
4341
+ return new ReturnValue('return', value)
4342
+
4343
+ case 'BreakStatement':
4344
+ return new ReturnValue('break')
4345
+
4346
+ case 'ContinueStatement':
4347
+ return new ReturnValue('continue')
4348
+
4349
+ case 'ExpressionStatement':
4350
+ return walk(node.expression)
4351
+
4352
+ case 'AssignmentExpression':
4353
+ return setValue(blockContext, node.left, node.right, node.operator)
4354
+
4355
+ case 'UpdateExpression':
4356
+ return setValue(blockContext, node.argument, null, node.operator)
4357
+
4358
+ case 'VariableDeclaration':
4359
+ node.declarations.forEach(function(declaration){
4360
+ var target = node.kind === 'let' ? blockContext : context;
4361
+ if (declaration.init){
4362
+ target[declaration.id.name] = walk(declaration.init);
4363
+ } else {
4364
+ target[declaration.id.name] = undefined;
4365
+ }
4366
+ });
4367
+ break
4368
+
4369
+ case 'SwitchStatement':
4370
+ var defaultHandler = null;
4371
+ var matched = false;
4372
+ var value = walk(node.discriminant);
4373
+ var result = undefined;
4374
+
4375
+ enterBlock();
4376
+
4377
+ var i = 0;
4378
+ while (result == null){
4379
+ if (i<node.cases.length){
4380
+ if (node.cases[i].test){ // check or fall through
4381
+ matched = matched || (walk(node.cases[i].test) === value);
4382
+ } else if (defaultHandler == null) {
4383
+ defaultHandler = i;
4384
+ }
4385
+ if (matched){
4386
+ var r = walkAll(node.cases[i].consequent);
4387
+ if (r instanceof ReturnValue){ // break out
4388
+ if (r.type == 'break') break
4389
+ result = r;
4390
+ }
4391
+ }
4392
+ i += 1; // continue
4393
+ } else if (!matched && defaultHandler != null){
4394
+ // go back and do the default handler
4395
+ i = defaultHandler;
4396
+ matched = true;
4397
+ } else {
4398
+ // nothing we can do
4399
+ break
4400
+ }
4401
+ }
4402
+
4403
+ leaveBlock();
4404
+ return result
4405
+
4406
+ case 'IfStatement':
4407
+ if (walk(node.test)){
4408
+ return walk(node.consequent)
4409
+ } else if (node.alternate) {
4410
+ return walk(node.alternate)
4411
+ }
4412
+
4413
+ case 'ForStatement':
4414
+ var infinite = infiniteChecker(maxIterations);
4415
+ var result = undefined;
4416
+
4417
+ enterBlock(); // allow lets on delarations
4418
+ for (walk(node.init); walk(node.test); walk(node.update)){
4419
+ var r = walk(node.body);
4420
+
4421
+ // handle early return, continue and break
4422
+ if (r instanceof ReturnValue){
4423
+ if (r.type == 'continue') continue
4424
+ if (r.type == 'break') break
4425
+ result = r;
4426
+ break
4427
+ }
4428
+
4429
+ infinite.check();
4430
+ }
4431
+ leaveBlock();
4432
+ return result
4433
+
4434
+ case 'ForInStatement':
4435
+ var infinite = infiniteChecker(maxIterations);
4436
+ var result = undefined;
4437
+
4438
+ var value = walk(node.right);
4439
+ var property = node.left;
4440
+
4441
+ var target = context;
4442
+ enterBlock();
4443
+
4444
+ if (property.type == 'VariableDeclaration'){
4445
+ walk(property);
4446
+ property = property.declarations[0].id;
4447
+ if (property.kind === 'let'){
4448
+ target = blockContext;
4449
+ }
4450
+ }
4451
+
4452
+ for (var key in value){
4453
+ setValue(target, property, {type: 'Literal', value: key});
4454
+ var r = walk(node.body);
4455
+
4456
+ // handle early return, continue and break
4457
+ if (r instanceof ReturnValue){
4458
+ if (r.type == 'continue') continue
4459
+ if (r.type == 'break') break
4460
+ result = r;
4461
+ break
4462
+ }
4463
+
4464
+ infinite.check();
4465
+ }
4466
+ leaveBlock();
4467
+
4468
+ return result
4469
+
4470
+ case 'WhileStatement':
4471
+ var infinite = infiniteChecker(maxIterations);
4472
+ while (walk(node.test)){
4473
+ walk(node.body);
4474
+ infinite.check();
4475
+ }
4476
+ break
4477
+
4478
+ case 'TryStatement':
4479
+ try {
4480
+ walk(node.block);
4481
+ } catch (error) {
4482
+ enterBlock();
4483
+ var catchClause = node.handlers[0];
4484
+ if (catchClause) {
4485
+ blockContext[catchClause.param.name] = error;
4486
+ walk(catchClause.body);
4487
+ }
4488
+ leaveBlock();
4489
+ } finally {
4490
+ if (node.finalizer) {
4491
+ walk(node.finalizer);
4492
+ }
4493
+ }
4494
+ break
4495
+
4496
+ case 'Literal':
4497
+ return node.value
4498
+
4499
+ case 'UnaryExpression':
4500
+ if (node.operator === 'delete' && node.argument.type === 'MemberExpression') {
4501
+ var arg = node.argument;
4502
+ var parent = walk(arg.object);
4503
+ var prop = arg.computed ? walk(arg.property) : arg.property.name;
4504
+ delete parent[prop];
4505
+ return true
4506
+ } else {
4507
+ var val = walk(node.argument);
4508
+ switch(node.operator) {
4509
+ case '+': return +val
4510
+ case '-': return -val
4511
+ case '~': return ~val
4512
+ case '!': return !val
4513
+ case 'typeof': return typeof val
4514
+ default: return unsupportedExpression(node)
4515
+ }
4516
+ }
4517
+
4518
+ case 'ArrayExpression':
4519
+ var obj = blockContext['Array']();
4520
+ for (var i=0;i<node.elements.length;i++){
4521
+ obj.push(walk(node.elements[i]));
4522
+ }
4523
+ return obj
4524
+
4525
+ case 'ObjectExpression':
4526
+ var obj = blockContext['Object']();
4527
+ for (var i = 0; i < node.properties.length; i++) {
4528
+ var prop = node.properties[i];
4529
+ var value = (prop.value === null) ? prop.value : walk(prop.value, prop);
4530
+ obj[prop.key.value || prop.key.name] = value;
4531
+ }
4532
+ return obj
4533
+
4534
+ case 'NewExpression':
4535
+ var args = node.arguments.map(function(arg){
4536
+ return walk(arg)
4537
+ });
4538
+ var target = walk(node.callee);
4539
+ return primitives.applyNew(target, args)
4540
+
4541
+
4542
+ case 'BinaryExpression':
4543
+ var l = walk(node.left);
4544
+ var r = walk(node.right);
4545
+ switch(node.operator) {
4546
+ case '==': return l == r
4547
+ case '===': return l === r
4548
+ case '!=': return l != r
4549
+ case '!==': return l !== r
4550
+ case '+': return l + r
4551
+ case '-': return l - r
4552
+ case '*': return l * r
4553
+ case '/': return l / r
4554
+ case '%': return l % r
4555
+ case '<': return l < r
4556
+ case '<=': return l <= r
4557
+ case '>': return l > r
4558
+ case '>=': return l >= r
4559
+ case '|': return l | r
4560
+ case '&': return l & r
4561
+ case '^': return l ^ r
4562
+ case 'instanceof': return l instanceof r
4563
+ default: return unsupportedExpression(node)
4564
+ }
4565
+
4566
+ case 'LogicalExpression':
4567
+ switch(node.operator) {
4568
+ case '&&': return walk(node.left) && walk(node.right)
4569
+ case '||': return walk(node.left) || walk(node.right)
4570
+ default: return unsupportedExpression(node)
4571
+ }
4572
+
4573
+ case 'ThisExpression':
4574
+ return blockContext['this']
4575
+
4576
+ case 'Identifier':
4577
+ if (node.name === 'undefined'){
4578
+ return undefined
4579
+ } else if (hasProperty(blockContext, node.name, primitives)){
4580
+ return checkValue(blockContext[node.name])
4581
+ } else {
4582
+ throw new ReferenceError(node.name + ' is not defined')
4583
+ }
4584
+
4585
+ case 'CallExpression':
4586
+ var args = node.arguments.map(function(arg){
4587
+ return walk(arg)
4588
+ });
4589
+ var object = null;
4590
+ var target = walk(node.callee);
4591
+
4592
+ if (node.callee.type === 'MemberExpression'){
4593
+ object = walk(node.callee.object);
4594
+ }
4595
+ return checkValue(target.apply(object, args))
4596
+
4597
+ case 'MemberExpression':
4598
+ var obj = walk(node.object);
4599
+ if (node.computed){
4600
+ var prop = walk(node.property);
4601
+ } else {
4602
+ var prop = node.property.name;
4603
+ }
4604
+ obj = primitives.getPropertyObject(obj, prop);
4605
+ return checkValue(obj[prop]);
4606
+
4607
+ case 'ConditionalExpression':
4608
+ var val = walk(node.test);
4609
+ return val ? walk(node.consequent) : walk(node.alternate)
4610
+
4611
+ case 'EmptyStatement':
4612
+ return
4613
+
4614
+ default:
4615
+ return unsupportedExpression(node)
4616
+ }
4617
+ } catch (ex) {
4618
+ ex.trace = ex.trace || [];
4619
+ ex.trace.push(node);
4620
+ throw ex
4621
+ }
4622
+ }
4623
+
4624
+ // safely retrieve a value
4625
+ function checkValue(value){
4626
+ if (value === Function){
4627
+ value = safeFunction;
4628
+ }
4629
+ return finalValue(value)
4630
+ }
4631
+
4632
+ // block scope context control
4633
+ function enterBlock(){
4634
+ blockContext = Object.create(blockContext);
4635
+ }
4636
+ function leaveBlock(){
4637
+ blockContext = Object.getPrototypeOf(blockContext);
4638
+ }
4639
+
4640
+ // set a value in the specified context if allowed
4641
+ function setValue(object, left, right, operator){
4642
+ var name = null;
4643
+
4644
+ if (left.type === 'Identifier'){
4645
+ name = left.name;
4646
+ // handle parent context shadowing
4647
+ object = objectForKey(object, name, primitives);
4648
+ } else if (left.type === 'MemberExpression'){
4649
+ if (left.computed){
4650
+ name = walk(left.property);
4651
+ } else {
4652
+ name = left.property.name;
4653
+ }
4654
+ object = walk(left.object);
4655
+ }
4656
+
4657
+ // stop built in properties from being able to be changed
4658
+ if (canSetProperty(object, name, primitives)){
4659
+ switch(operator) {
4660
+ case undefined: return object[name] = walk(right)
4661
+ case '=': return object[name] = walk(right)
4662
+ case '+=': return object[name] += walk(right)
4663
+ case '-=': return object[name] -= walk(right)
4664
+ case '++': return object[name]++
4665
+ case '--': return object[name]--
4666
+ }
4667
+ }
4668
+
4669
+ }
4670
+
4671
+ }
4672
+
4673
+ // when an unsupported expression is encountered, throw an error
4674
+ function unsupportedExpression(node){
4675
+ console.error(node);
4676
+ var err = new Error('Unsupported expression: ' + node.type);
4677
+ err.node = node;
4678
+ throw err
4679
+ }
4680
+
4681
+ // walk a provided object's prototypal hierarchy to retrieve an inherited object
4682
+ function objectForKey(object, key, primitives){
4683
+ var proto = primitives.getPrototypeOf(object);
4684
+ if (!proto || hasOwnProperty$1(object, key)){
4685
+ return object
4686
+ } else {
4687
+ return objectForKey(proto, key, primitives)
4688
+ }
4689
+ }
4690
+
4691
+ function hasProperty(object, key, primitives){
4692
+ var proto = primitives.getPrototypeOf(object);
4693
+ var hasOwn = hasOwnProperty$1(object, key);
4694
+ if (object[key] !== undefined){
4695
+ return true
4696
+ } else if (!proto || hasOwn){
4697
+ return hasOwn
4698
+ } else {
4699
+ return hasProperty(proto, key, primitives)
4700
+ }
4701
+ }
4702
+
4703
+ function hasOwnProperty$1(object, key){
4704
+ return Object.prototype.hasOwnProperty.call(object, key)
4705
+ }
4706
+
4707
+ function propertyIsEnumerable(object, key){
4708
+ return Object.prototype.propertyIsEnumerable.call(object, key)
4709
+ }
4710
+
4711
+
4712
+ // determine if we have write access to a property
4713
+ function canSetProperty(object, property, primitives){
4714
+ if (property === '__proto__' || primitives.isPrimitive(object)){
4715
+ return false
4716
+ } else if (object != null){
4717
+
4718
+ if (hasOwnProperty$1(object, property)){
4719
+ if (propertyIsEnumerable(object, property)){
4720
+ return true
4721
+ } else {
4722
+ return false
4723
+ }
4724
+ } else {
4725
+ return canSetProperty(primitives.getPrototypeOf(object), property, primitives)
4726
+ }
4727
+
4728
+ } else {
4729
+ return true
4730
+ }
4731
+ }
4732
+
4733
+ // generate a function with specified context
4734
+ function getFunction(body, params, parentContext, traceNode){
4735
+ return function(){
4736
+ try {
4737
+ var context = Object.create(parentContext);
4738
+ if (this == commonjsGlobal){
4739
+ context['this'] = null;
4740
+ } else {
4741
+ context['this'] = this;
4742
+ }
4743
+ // normalize arguments array
4744
+ var args = Array.prototype.slice.call(arguments);
4745
+ context['arguments'] = arguments;
4746
+ args.forEach(function(arg,idx){
4747
+ var param = params[idx];
4748
+ if (param){
4749
+ context[param] = arg;
4750
+ }
4751
+ });
4752
+ var result = evaluateAst(body, context);
4753
+
4754
+ if (result instanceof ReturnValue){
4755
+ return result.value
4756
+ }
4757
+ } catch (ex) {
4758
+ ex.trace = ex.trace || [];
4759
+ ex.trace.push(traceNode);
4760
+ throw ex
4761
+ }
4762
+ }
4763
+ }
4764
+
4765
+ function finalValue(value){
4766
+ if (value instanceof ReturnValue){
4767
+ return value.value
4768
+ }
4769
+ return value
4770
+ }
4771
+
4772
+ // get the name of an identifier
4773
+ function getName(identifier){
4774
+ return identifier.name
4775
+ }
4776
+
4777
+ // a ReturnValue struct for differentiating between expression result and return statement
4778
+ function ReturnValue(type, value){
4779
+ this.type = type;
4780
+ this.value = value;
4781
+ }
4782
+ notevil.eval = _eval;
4783
+ notevil.FunctionFactory = FunctionFactory_1;
4784
+ notevil.Function = _Function;
4785
+
89
4786
  let __clone = function (data) {
90
4787
  return JSON.parse(JSON.stringify(data));
91
4788
  };
@@ -452,15 +5149,10 @@ const UtFormConstructor = {
452
5149
  context['console'] = console;
453
5150
  context['Date'] = Date;
454
5151
  context['Math'] = Math;
455
- let appendScript = '';
456
- for (let v in context) {
457
- appendScript = appendScript + `var ${v} = this.${v};\n`;
458
- }
459
- appendScript = appendScript ? appendScript + '\n' : '';
460
5152
  let func = function (script) {
461
- return eval(script);
5153
+ return notevil(script, context);
462
5154
  };
463
- func.call(context, appendScript + script);
5155
+ func.call(context, script);
464
5156
  }
465
5157
  };
466
5158
 
@@ -2919,22 +7611,6 @@ const __vue_component__$6 = /*#__PURE__*/normalizeComponent({
2919
7611
  }, __vue_inject_styles__$5, __vue_script__$5, __vue_scope_id__$5, __vue_is_functional_template__$5, __vue_module_identifier__$5, false, undefined, undefined, undefined);
2920
7612
  var DocTemplateSectionModal = __vue_component__$6;
2921
7613
 
2922
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
2923
-
2924
- function createCommonjsModule(fn, basedir, module) {
2925
- return module = {
2926
- path: basedir,
2927
- exports: {},
2928
- require: function (path, base) {
2929
- return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
2930
- }
2931
- }, fn(module, module.exports), module.exports;
2932
- }
2933
-
2934
- function commonjsRequire () {
2935
- throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
2936
- }
2937
-
2938
7614
  /**!
2939
7615
  * Sortable 1.10.2
2940
7616
  * @author RubaXa <trash@rubaxa.org>
@@ -8489,6 +13165,182 @@ const __vue_component__$5 = /*#__PURE__*/normalizeComponent({
8489
13165
  }, __vue_inject_styles__$4, __vue_script__$4, __vue_scope_id__$4, __vue_is_functional_template__$4, __vue_module_identifier__$4, false, undefined, undefined, undefined);
8490
13166
  var DocTemplateFacetList = __vue_component__$5;
8491
13167
 
13168
+ // Unique ID creation requires a high quality random # generator. In node.js
13169
+ // this is pretty straight-forward - we use the crypto API.
13170
+
13171
+
13172
+
13173
+ var rng = function nodeRNG() {
13174
+ return crypto.randomBytes(16);
13175
+ };
13176
+
13177
+ /**
13178
+ * Convert array of 16 byte values to UUID string format of the form:
13179
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
13180
+ */
13181
+ var byteToHex = [];
13182
+ for (var i = 0; i < 256; ++i) {
13183
+ byteToHex[i] = (i + 0x100).toString(16).substr(1);
13184
+ }
13185
+
13186
+ function bytesToUuid(buf, offset) {
13187
+ var i = offset || 0;
13188
+ var bth = byteToHex;
13189
+ // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
13190
+ return ([
13191
+ bth[buf[i++]], bth[buf[i++]],
13192
+ bth[buf[i++]], bth[buf[i++]], '-',
13193
+ bth[buf[i++]], bth[buf[i++]], '-',
13194
+ bth[buf[i++]], bth[buf[i++]], '-',
13195
+ bth[buf[i++]], bth[buf[i++]], '-',
13196
+ bth[buf[i++]], bth[buf[i++]],
13197
+ bth[buf[i++]], bth[buf[i++]],
13198
+ bth[buf[i++]], bth[buf[i++]]
13199
+ ]).join('');
13200
+ }
13201
+
13202
+ var bytesToUuid_1 = bytesToUuid;
13203
+
13204
+ // **`v1()` - Generate time-based UUID**
13205
+ //
13206
+ // Inspired by https://github.com/LiosK/UUID.js
13207
+ // and http://docs.python.org/library/uuid.html
13208
+
13209
+ var _nodeId;
13210
+ var _clockseq;
13211
+
13212
+ // Previous uuid creation time
13213
+ var _lastMSecs = 0;
13214
+ var _lastNSecs = 0;
13215
+
13216
+ // See https://github.com/uuidjs/uuid for API details
13217
+ function v1(options, buf, offset) {
13218
+ var i = buf && offset || 0;
13219
+ var b = buf || [];
13220
+
13221
+ options = options || {};
13222
+ var node = options.node || _nodeId;
13223
+ var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;
13224
+
13225
+ // node and clockseq need to be initialized to random values if they're not
13226
+ // specified. We do this lazily to minimize issues related to insufficient
13227
+ // system entropy. See #189
13228
+ if (node == null || clockseq == null) {
13229
+ var seedBytes = rng();
13230
+ if (node == null) {
13231
+ // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
13232
+ node = _nodeId = [
13233
+ seedBytes[0] | 0x01,
13234
+ seedBytes[1], seedBytes[2], seedBytes[3], seedBytes[4], seedBytes[5]
13235
+ ];
13236
+ }
13237
+ if (clockseq == null) {
13238
+ // Per 4.2.2, randomize (14 bit) clockseq
13239
+ clockseq = _clockseq = (seedBytes[6] << 8 | seedBytes[7]) & 0x3fff;
13240
+ }
13241
+ }
13242
+
13243
+ // UUID timestamps are 100 nano-second units since the Gregorian epoch,
13244
+ // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
13245
+ // time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
13246
+ // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
13247
+ var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();
13248
+
13249
+ // Per 4.2.1.2, use count of uuid's generated during the current clock
13250
+ // cycle to simulate higher resolution clock
13251
+ var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;
13252
+
13253
+ // Time since last uuid creation (in msecs)
13254
+ var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
13255
+
13256
+ // Per 4.2.1.2, Bump clockseq on clock regression
13257
+ if (dt < 0 && options.clockseq === undefined) {
13258
+ clockseq = clockseq + 1 & 0x3fff;
13259
+ }
13260
+
13261
+ // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
13262
+ // time interval
13263
+ if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
13264
+ nsecs = 0;
13265
+ }
13266
+
13267
+ // Per 4.2.1.2 Throw error if too many uuids are requested
13268
+ if (nsecs >= 10000) {
13269
+ throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
13270
+ }
13271
+
13272
+ _lastMSecs = msecs;
13273
+ _lastNSecs = nsecs;
13274
+ _clockseq = clockseq;
13275
+
13276
+ // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
13277
+ msecs += 12219292800000;
13278
+
13279
+ // `time_low`
13280
+ var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
13281
+ b[i++] = tl >>> 24 & 0xff;
13282
+ b[i++] = tl >>> 16 & 0xff;
13283
+ b[i++] = tl >>> 8 & 0xff;
13284
+ b[i++] = tl & 0xff;
13285
+
13286
+ // `time_mid`
13287
+ var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
13288
+ b[i++] = tmh >>> 8 & 0xff;
13289
+ b[i++] = tmh & 0xff;
13290
+
13291
+ // `time_high_and_version`
13292
+ b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
13293
+ b[i++] = tmh >>> 16 & 0xff;
13294
+
13295
+ // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
13296
+ b[i++] = clockseq >>> 8 | 0x80;
13297
+
13298
+ // `clock_seq_low`
13299
+ b[i++] = clockseq & 0xff;
13300
+
13301
+ // `node`
13302
+ for (var n = 0; n < 6; ++n) {
13303
+ b[i + n] = node[n];
13304
+ }
13305
+
13306
+ return buf ? buf : bytesToUuid_1(b);
13307
+ }
13308
+
13309
+ var v1_1 = v1;
13310
+
13311
+ function v4(options, buf, offset) {
13312
+ var i = buf && offset || 0;
13313
+
13314
+ if (typeof(options) == 'string') {
13315
+ buf = options === 'binary' ? new Array(16) : null;
13316
+ options = null;
13317
+ }
13318
+ options = options || {};
13319
+
13320
+ var rnds = options.random || (options.rng || rng)();
13321
+
13322
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
13323
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
13324
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
13325
+
13326
+ // Copy bytes to buffer, if provided
13327
+ if (buf) {
13328
+ for (var ii = 0; ii < 16; ++ii) {
13329
+ buf[i + ii] = rnds[ii];
13330
+ }
13331
+ }
13332
+
13333
+ return buf || bytesToUuid_1(rnds);
13334
+ }
13335
+
13336
+ var v4_1 = v4;
13337
+
13338
+ var uuid = v4_1;
13339
+ uuid.v1 = v1_1;
13340
+ uuid.v4 = v4_1;
13341
+
13342
+ var uuid_1 = uuid;
13343
+
8492
13344
  var toString = Object.prototype.toString;
8493
13345
 
8494
13346
  var _typeof = function(object) {
@@ -8508,6 +13360,8 @@ var _typeof = function(object) {
8508
13360
  };
8509
13361
 
8510
13362
  //
13363
+ // import safeEval from "notevil";
13364
+
8511
13365
  var script$3 = {
8512
13366
  name: 'DocForm',
8513
13367
  props: {
@@ -8864,21 +13718,6 @@ const __vue_component__$4 = /*#__PURE__*/normalizeComponent({
8864
13718
  }, __vue_inject_styles__$3, __vue_script__$3, __vue_scope_id__$3, __vue_is_functional_template__$3, __vue_module_identifier__$3, false, undefined, undefined, undefined);
8865
13719
  var DocForm = __vue_component__$4;
8866
13720
 
8867
- const generateRandomString = length => {
8868
- let result = '';
8869
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
8870
- const charactersLength = characters.length;
8871
- for (let i = 0; i < length; i++) {
8872
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
8873
- }
8874
- return result;
8875
- };
8876
- const UtRandom = {
8877
- getRandomString(length) {
8878
- return generateRandomString(length);
8879
- }
8880
- };
8881
-
8882
13721
  //
8883
13722
  var script$2 = {
8884
13723
  name: 'FieldRuleFormModal',
@@ -8971,7 +13810,7 @@ var script$2 = {
8971
13810
  },
8972
13811
  getDefaultRule() {
8973
13812
  return {
8974
- id: UtRandom.getRandomString(10),
13813
+ id: uuid_1.v4(),
8975
13814
  name: null,
8976
13815
  event: null,
8977
13816
  script: null
@@ -9334,7 +14173,7 @@ var script$1 = {
9334
14173
  rule: {},
9335
14174
  mode: 'ins'
9336
14175
  },
9337
- rulesHash: UtRandom.getRandomString(10)
14176
+ rulesHash: uuid_1.v4()
9338
14177
  };
9339
14178
  },
9340
14179
  computed: {
@@ -9426,7 +14265,7 @@ var script$1 = {
9426
14265
  this.field.rules.push({
9427
14266
  ...rule
9428
14267
  });
9429
- this.rulesHash = UtRandom.getRandomString(10);
14268
+ this.rulesHash = uuid_1.v4();
9430
14269
  }
9431
14270
  };
9432
14271
  this.$bvModal.show(this.modalId);
@@ -9451,7 +14290,7 @@ var script$1 = {
9451
14290
  if (index >= 0) {
9452
14291
  this.field.rules.splice(index, 1);
9453
14292
  }
9454
- this.rulesHash = UtRandom.getRandomString(10);
14293
+ this.rulesHash = uuid_1.v4();
9455
14294
  }
9456
14295
  },
9457
14296
  created() {