1 |
|
(function (global, factory) {
|
2 |
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
3 |
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
4 |
|
(global = global || self, factory(global.acorn = {}));
|
5 |
|
}(this, function (exports) { 'use strict';
|
6 |
|
|
7 |
|
// Reserved word lists for various dialects of the language
|
8 |
|
|
9 |
|
var reservedWords = {
|
10 |
|
3: "abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile",
|
11 |
|
5: "class enum extends super const export import",
|
12 |
|
6: "enum",
|
13 |
|
strict: "implements interface let package private protected public static yield",
|
14 |
|
strictBind: "eval arguments"
|
15 |
|
};
|
16 |
|
|
17 |
|
// And the keywords
|
18 |
|
|
19 |
|
var ecma5AndLessKeywords = "break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this";
|
20 |
|
|
21 |
|
var keywords = {
|
22 |
|
5: ecma5AndLessKeywords,
|
23 |
|
"5module": ecma5AndLessKeywords + " export import",
|
24 |
|
6: ecma5AndLessKeywords + " const class extends export import super"
|
25 |
|
};
|
26 |
|
|
27 |
|
var keywordRelationalOperator = /^in(stanceof)?$/;
|
28 |
|
|
29 |
|
// ## Character categories
|
30 |
|
|
31 |
|
// Big ugly regular expressions that match characters in the
|
32 |
|
// whitespace, identifier, and identifier-start categories. These
|
33 |
|
// are only applied when a character is found to actually have a
|
34 |
|
// code point above 128.
|
35 |
|
// Generated by `bin/generate-identifier-regex.js`.
|
36 |
|
var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\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\u0860-\u086a\u08a0-\u08b4\u08b6-\u08bd\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\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\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\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-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\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\u2118-\u211d\u2124\u2126\u2128\u212a-\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\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab67\uab70-\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";
|
37 |
|
var nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
|
38 |
|
|
39 |
|
var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
|
40 |
|
var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
|
41 |
|
|
42 |
|
nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
|
43 |
|
|
44 |
|
// These are a run-length and offset encoded representation of the
|
45 |
|
// >0xffff code points that are a valid part of identifiers. The
|
46 |
|
// offset starts at 0x10000, and each pair of numbers represents an
|
47 |
|
// offset to the next range, and then a size of the range. They were
|
48 |
|
// generated by bin/generate-identifier-regex.js
|
49 |
|
|
50 |
|
// eslint-disable-next-line comma-spacing
|
51 |
|
var astralIdentifierStartCodes = [0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,14,29,6,37,11,29,3,35,5,7,2,4,43,157,19,35,5,35,5,39,9,51,157,310,10,21,11,7,153,5,3,0,2,43,2,1,4,0,3,22,11,22,10,30,66,18,2,1,11,21,11,25,71,55,7,1,65,0,16,3,2,2,2,28,43,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,56,50,14,50,14,35,477,28,11,0,9,21,155,22,13,52,76,44,33,24,27,35,30,0,12,34,4,0,13,47,15,3,22,0,2,0,36,17,2,24,85,6,2,0,2,3,2,14,2,9,8,46,39,7,3,1,3,21,2,6,2,1,2,4,4,0,19,0,13,4,159,52,19,3,21,0,33,47,21,1,2,0,185,46,42,3,37,47,21,0,60,42,14,0,72,26,230,43,117,63,32,0,161,7,3,38,17,0,2,0,29,0,11,39,8,0,22,0,12,45,20,0,35,56,264,8,2,36,18,0,50,29,113,6,2,1,2,37,22,0,26,5,2,1,2,31,15,0,328,18,270,921,103,110,18,195,2749,1070,4050,582,8634,568,8,30,114,29,19,47,17,3,32,20,6,18,689,63,129,74,6,0,67,12,65,1,2,0,29,6135,9,754,9486,286,50,2,18,3,9,395,2309,106,6,12,4,8,8,9,5991,84,2,70,2,1,3,0,3,1,3,3,2,11,2,0,2,6,2,64,2,3,3,7,2,6,2,27,2,3,2,4,2,0,4,6,2,339,3,24,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,7,2357,44,11,6,17,0,370,43,1301,196,60,67,8,0,1205,3,2,26,2,1,2,0,3,0,2,9,2,3,2,0,2,0,7,0,5,0,2,0,2,0,2,2,2,1,2,0,3,0,2,0,2,0,2,0,2,0,2,1,2,0,3,3,2,6,2,3,2,3,2,0,2,9,2,16,6,2,2,4,2,16,4421,42710,42,4148,12,221,3,5761,15,7472,3104,541];
|
52 |
|
|
53 |
|
// eslint-disable-next-line comma-spacing
|
54 |
|
var astralIdentifierCodes = [509,0,227,0,150,4,294,9,1368,2,2,1,6,3,41,2,5,0,166,1,574,3,9,9,525,10,176,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,6,1,45,0,13,2,49,13,9,3,4,9,83,11,7,0,161,11,6,9,7,3,56,1,2,6,3,1,3,2,10,0,11,1,3,6,4,4,193,17,10,9,5,0,82,19,13,9,214,6,3,8,28,1,83,16,16,9,82,12,9,9,84,14,5,9,243,14,166,9,232,6,3,6,4,0,29,9,41,6,2,3,9,0,10,10,47,15,406,7,2,7,17,9,57,21,2,13,123,5,4,0,2,1,2,6,2,0,9,9,49,4,2,1,2,4,9,9,330,3,19306,9,135,4,60,6,26,9,1014,0,2,54,8,3,19723,1,5319,4,4,5,9,7,3,6,31,3,149,2,1418,49,513,54,5,49,9,0,15,0,23,4,2,14,1361,6,2,16,3,6,2,1,2,4,262,6,10,9,419,13,1495,6,110,6,6,9,792487,239];
|
55 |
|
|
56 |
|
// This has a complexity linear to the value of the code. The
|
57 |
|
// assumption is that looking up astral identifier characters is
|
58 |
|
// rare.
|
59 |
|
function isInAstralSet(code, set) {
|
60 |
|
var pos = 0x10000;
|
61 |
|
for (var i = 0; i < set.length; i += 2) {
|
62 |
|
pos += set[i];
|
63 |
|
if (pos > code) { return false }
|
64 |
|
pos += set[i + 1];
|
65 |
|
if (pos >= code) { return true }
|
66 |
|
}
|
67 |
|
}
|
68 |
|
|
69 |
|
// Test whether a given character code starts an identifier.
|
70 |
|
|
71 |
|
function isIdentifierStart(code, astral) {
|
72 |
|
if (code < 65) { return code === 36 }
|
73 |
|
if (code < 91) { return true }
|
74 |
|
if (code < 97) { return code === 95 }
|
75 |
|
if (code < 123) { return true }
|
76 |
|
if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) }
|
77 |
|
if (astral === false) { return false }
|
78 |
|
return isInAstralSet(code, astralIdentifierStartCodes)
|
79 |
|
}
|
80 |
|
|
81 |
|
// Test whether a given character is part of an identifier.
|
82 |
|
|
83 |
|
function isIdentifierChar(code, astral) {
|
84 |
|
if (code < 48) { return code === 36 }
|
85 |
|
if (code < 58) { return true }
|
86 |
|
if (code < 65) { return false }
|
87 |
|
if (code < 91) { return true }
|
88 |
|
if (code < 97) { return code === 95 }
|
89 |
|
if (code < 123) { return true }
|
90 |
|
if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)) }
|
91 |
|
if (astral === false) { return false }
|
92 |
|
return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes)
|
93 |
|
}
|
94 |
|
|
95 |
|
// ## Token types
|
96 |
|
|
97 |
|
// The assignment of fine-grained, information-carrying type objects
|
98 |
|
// allows the tokenizer to store the information it has about a
|
99 |
|
// token in a way that is very cheap for the parser to look up.
|
100 |
|
|
101 |
|
// All token type variables start with an underscore, to make them
|
102 |
|
// easy to recognize.
|
103 |
|
|
104 |
|
// The `beforeExpr` property is used to disambiguate between regular
|
105 |
|
// expressions and divisions. It is set on all token types that can
|
106 |
|
// be followed by an expression (thus, a slash after them would be a
|
107 |
|
// regular expression).
|
108 |
|
//
|
109 |
|
// The `startsExpr` property is used to check if the token ends a
|
110 |
|
// `yield` expression. It is set on all token types that either can
|
111 |
|
// directly start an expression (like a quotation mark) or can
|
112 |
|
// continue an expression (like the body of a string).
|
113 |
|
//
|
114 |
|
// `isLoop` marks a keyword as starting a loop, which is important
|
115 |
|
// to know when parsing a label, in order to allow or disallow
|
116 |
|
// continue jumps to that label.
|
117 |
|
|
118 |
|
var TokenType = function TokenType(label, conf) {
|
119 |
|
if ( conf === void 0 ) conf = {};
|
120 |
|
|
121 |
|
this.label = label;
|
122 |
|
this.keyword = conf.keyword;
|
123 |
|
this.beforeExpr = !!conf.beforeExpr;
|
124 |
|
this.startsExpr = !!conf.startsExpr;
|
125 |
|
this.isLoop = !!conf.isLoop;
|
126 |
|
this.isAssign = !!conf.isAssign;
|
127 |
|
this.prefix = !!conf.prefix;
|
128 |
|
this.postfix = !!conf.postfix;
|
129 |
|
this.binop = conf.binop || null;
|
130 |
|
this.updateContext = null;
|
131 |
|
};
|
132 |
|
|
133 |
|
function binop(name, prec) {
|
134 |
|
return new TokenType(name, {beforeExpr: true, binop: prec})
|
135 |
|
}
|
136 |
|
var beforeExpr = {beforeExpr: true}, startsExpr = {startsExpr: true};
|
137 |
|
|
138 |
|
// Map keyword names to token types.
|
139 |
|
|
140 |
|
var keywords$1 = {};
|
141 |
|
|
142 |
|
// Succinct definitions of keyword token types
|
143 |
|
function kw(name, options) {
|
144 |
|
if ( options === void 0 ) options = {};
|
145 |
|
|
146 |
|
options.keyword = name;
|
147 |
|
return keywords$1[name] = new TokenType(name, options)
|
148 |
|
}
|
149 |
|
|
150 |
|
var types = {
|
151 |
|
num: new TokenType("num", startsExpr),
|
152 |
|
regexp: new TokenType("regexp", startsExpr),
|
153 |
|
string: new TokenType("string", startsExpr),
|
154 |
|
name: new TokenType("name", startsExpr),
|
155 |
|
eof: new TokenType("eof"),
|
156 |
|
|
157 |
|
// Punctuation token types.
|
158 |
|
bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
|
159 |
|
bracketR: new TokenType("]"),
|
160 |
|
braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
|
161 |
|
braceR: new TokenType("}"),
|
162 |
|
parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
|
163 |
|
parenR: new TokenType(")"),
|
164 |
|
comma: new TokenType(",", beforeExpr),
|
165 |
|
semi: new TokenType(";", beforeExpr),
|
166 |
|
colon: new TokenType(":", beforeExpr),
|
167 |
|
dot: new TokenType("."),
|
168 |
|
question: new TokenType("?", beforeExpr),
|
169 |
|
arrow: new TokenType("=>", beforeExpr),
|
170 |
|
template: new TokenType("template"),
|
171 |
|
invalidTemplate: new TokenType("invalidTemplate"),
|
172 |
|
ellipsis: new TokenType("...", beforeExpr),
|
173 |
|
backQuote: new TokenType("`", startsExpr),
|
174 |
|
dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
|
175 |
|
|
176 |
|
// Operators. These carry several kinds of properties to help the
|
177 |
|
// parser use them properly (the presence of these properties is
|
178 |
|
// what categorizes them as operators).
|
179 |
|
//
|
180 |
|
// `binop`, when present, specifies that this operator is a binary
|
181 |
|
// operator, and will refer to its precedence.
|
182 |
|
//
|
183 |
|
// `prefix` and `postfix` mark the operator as a prefix or postfix
|
184 |
|
// unary operator.
|
185 |
|
//
|
186 |
|
// `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
|
187 |
|
// binary operators with a very low precedence, that should result
|
188 |
|
// in AssignmentExpression nodes.
|
189 |
|
|
190 |
|
eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
|
191 |
|
assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
|
192 |
|
incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
|
193 |
|
prefix: new TokenType("!/~", {beforeExpr: true, prefix: true, startsExpr: true}),
|
194 |
|
logicalOR: binop("||", 1),
|
195 |
|
logicalAND: binop("&&", 2),
|
196 |
|
bitwiseOR: binop("|", 3),
|
197 |
|
bitwiseXOR: binop("^", 4),
|
198 |
|
bitwiseAND: binop("&", 5),
|
199 |
|
equality: binop("==/!=/===/!==", 6),
|
200 |
|
relational: binop("</>/<=/>=", 7),
|
201 |
|
bitShift: binop("<</>>/>>>", 8),
|
202 |
|
plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
|
203 |
|
modulo: binop("%", 10),
|
204 |
|
star: binop("*", 10),
|
205 |
|
slash: binop("/", 10),
|
206 |
|
starstar: new TokenType("**", {beforeExpr: true}),
|
207 |
|
|
208 |
|
// Keyword token types.
|
209 |
|
_break: kw("break"),
|
210 |
|
_case: kw("case", beforeExpr),
|
211 |
|
_catch: kw("catch"),
|
212 |
|
_continue: kw("continue"),
|
213 |
|
_debugger: kw("debugger"),
|
214 |
|
_default: kw("default", beforeExpr),
|
215 |
|
_do: kw("do", {isLoop: true, beforeExpr: true}),
|
216 |
|
_else: kw("else", beforeExpr),
|
217 |
|
_finally: kw("finally"),
|
218 |
|
_for: kw("for", {isLoop: true}),
|
219 |
|
_function: kw("function", startsExpr),
|
220 |
|
_if: kw("if"),
|
221 |
|
_return: kw("return", beforeExpr),
|
222 |
|
_switch: kw("switch"),
|
223 |
|
_throw: kw("throw", beforeExpr),
|
224 |
|
_try: kw("try"),
|
225 |
|
_var: kw("var"),
|
226 |
|
_const: kw("const"),
|
227 |
|
_while: kw("while", {isLoop: true}),
|
228 |
|
_with: kw("with"),
|
229 |
|
_new: kw("new", {beforeExpr: true, startsExpr: true}),
|
230 |
|
_this: kw("this", startsExpr),
|
231 |
|
_super: kw("super", startsExpr),
|
232 |
|
_class: kw("class", startsExpr),
|
233 |
|
_extends: kw("extends", beforeExpr),
|
234 |
|
_export: kw("export"),
|
235 |
|
_import: kw("import", startsExpr),
|
236 |
|
_null: kw("null", startsExpr),
|
237 |
|
_true: kw("true", startsExpr),
|
238 |
|
_false: kw("false", startsExpr),
|
239 |
|
_in: kw("in", {beforeExpr: true, binop: 7}),
|
240 |
|
_instanceof: kw("instanceof", {beforeExpr: true, binop: 7}),
|
241 |
|
_typeof: kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true}),
|
242 |
|
_void: kw("void", {beforeExpr: true, prefix: true, startsExpr: true}),
|
243 |
|
_delete: kw("delete", {beforeExpr: true, prefix: true, startsExpr: true})
|
244 |
|
};
|
245 |
|
|
246 |
|
// Matches a whole line break (where CRLF is considered a single
|
247 |
|
// line break). Used to count lines.
|
248 |
|
|
249 |
|
var lineBreak = /\r\n?|\n|\u2028|\u2029/;
|
250 |
|
var lineBreakG = new RegExp(lineBreak.source, "g");
|
251 |
|
|
252 |
|
function isNewLine(code, ecma2019String) {
|
253 |
|
return code === 10 || code === 13 || (!ecma2019String && (code === 0x2028 || code === 0x2029))
|
254 |
|
}
|
255 |
|
|
256 |
|
var nonASCIIwhitespace = /[\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
|
257 |
|
|
258 |
|
var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
259 |
|
|
260 |
|
var ref = Object.prototype;
|
261 |
|
var hasOwnProperty = ref.hasOwnProperty;
|
262 |
|
var toString = ref.toString;
|
263 |
|
|
264 |
|
// Checks if an object has a property.
|
265 |
|
|
266 |
|
function has(obj, propName) {
|
267 |
|
return hasOwnProperty.call(obj, propName)
|
268 |
|
}
|
269 |
|
|
270 |
|
var isArray = Array.isArray || (function (obj) { return (
|
271 |
|
toString.call(obj) === "[object Array]"
|
272 |
|
); });
|
273 |
|
|
274 |
|
function wordsRegexp(words) {
|
275 |
|
return new RegExp("^(?:" + words.replace(/ /g, "|") + ")$")
|
276 |
|
}
|
277 |
|
|
278 |
|
// These are used when `options.locations` is on, for the
|
279 |
|
// `startLoc` and `endLoc` properties.
|
280 |
|
|
281 |
|
var Position = function Position(line, col) {
|
282 |
|
this.line = line;
|
283 |
|
this.column = col;
|
284 |
|
};
|
285 |
|
|
286 |
|
Position.prototype.offset = function offset (n) {
|
287 |
|
return new Position(this.line, this.column + n)
|
288 |
|
};
|
289 |
|
|
290 |
|
var SourceLocation = function SourceLocation(p, start, end) {
|
291 |
|
this.start = start;
|
292 |
|
this.end = end;
|
293 |
|
if (p.sourceFile !== null) { this.source = p.sourceFile; }
|
294 |
|
};
|
295 |
|
|
296 |
|
// The `getLineInfo` function is mostly useful when the
|
297 |
|
// `locations` option is off (for performance reasons) and you
|
298 |
|
// want to find the line/column position for a given character
|
299 |
|
// offset. `input` should be the code string that the offset refers
|
300 |
|
// into.
|
301 |
|
|
302 |
|
function getLineInfo(input, offset) {
|
303 |
|
for (var line = 1, cur = 0;;) {
|
304 |
|
lineBreakG.lastIndex = cur;
|
305 |
|
var match = lineBreakG.exec(input);
|
306 |
|
if (match && match.index < offset) {
|
307 |
|
++line;
|
308 |
|
cur = match.index + match[0].length;
|
309 |
|
} else {
|
310 |
|
return new Position(line, offset - cur)
|
311 |
|
}
|
312 |
|
}
|
313 |
|
}
|
314 |
|
|
315 |
|
// A second optional argument can be given to further configure
|
316 |
|
// the parser process. These options are recognized:
|
317 |
|
|
318 |
|
var defaultOptions = {
|
319 |
|
// `ecmaVersion` indicates the ECMAScript version to parse. Must be
|
320 |
|
// either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018), or 10
|
321 |
|
// (2019). This influences support for strict mode, the set of
|
322 |
|
// reserved words, and support for new syntax features. The default
|
323 |
|
// is 9.
|
324 |
|
ecmaVersion: 9,
|
325 |
|
// `sourceType` indicates the mode the code should be parsed in.
|
326 |
|
// Can be either `"script"` or `"module"`. This influences global
|
327 |
|
// strict mode and parsing of `import` and `export` declarations.
|
328 |
|
sourceType: "script",
|
329 |
|
// `onInsertedSemicolon` can be a callback that will be called
|
330 |
|
// when a semicolon is automatically inserted. It will be passed
|
331 |
|
// the position of the comma as an offset, and if `locations` is
|
332 |
|
// enabled, it is given the location as a `{line, column}` object
|
333 |
|
// as second argument.
|
334 |
|
onInsertedSemicolon: null,
|
335 |
|
// `onTrailingComma` is similar to `onInsertedSemicolon`, but for
|
336 |
|
// trailing commas.
|
337 |
|
onTrailingComma: null,
|
338 |
|
// By default, reserved words are only enforced if ecmaVersion >= 5.
|
339 |
|
// Set `allowReserved` to a boolean value to explicitly turn this on
|
340 |
|
// an off. When this option has the value "never", reserved words
|
341 |
|
// and keywords can also not be used as property names.
|
342 |
|
allowReserved: null,
|
343 |
|
// When enabled, a return at the top level is not considered an
|
344 |
|
// error.
|
345 |
|
allowReturnOutsideFunction: false,
|
346 |
|
// When enabled, import/export statements are not constrained to
|
347 |
|
// appearing at the top of the program.
|
348 |
|
allowImportExportEverywhere: false,
|
349 |
|
// When enabled, await identifiers are allowed to appear at the top-level scope,
|
350 |
|
// but they are still not allowed in non-async functions.
|
351 |
|
allowAwaitOutsideFunction: false,
|
352 |
|
// When enabled, hashbang directive in the beginning of file
|
353 |
|
// is allowed and treated as a line comment.
|
354 |
|
allowHashBang: false,
|
355 |
|
// When `locations` is on, `loc` properties holding objects with
|
356 |
|
// `start` and `end` properties in `{line, column}` form (with
|
357 |
|
// line being 1-based and column 0-based) will be attached to the
|
358 |
|
// nodes.
|
359 |
|
locations: false,
|
360 |
|
// A function can be passed as `onToken` option, which will
|
361 |
|
// cause Acorn to call that function with object in the same
|
362 |
|
// format as tokens returned from `tokenizer().getToken()`. Note
|
363 |
|
// that you are not allowed to call the parser from the
|
364 |
|
// callback—that will corrupt its internal state.
|
365 |
|
onToken: null,
|
366 |
|
// A function can be passed as `onComment` option, which will
|
367 |
|
// cause Acorn to call that function with `(block, text, start,
|
368 |
|
// end)` parameters whenever a comment is skipped. `block` is a
|
369 |
|
// boolean indicating whether this is a block (`/* */`) comment,
|
370 |
|
// `text` is the content of the comment, and `start` and `end` are
|
371 |
|
// character offsets that denote the start and end of the comment.
|
372 |
|
// When the `locations` option is on, two more parameters are
|
373 |
|
// passed, the full `{line, column}` locations of the start and
|
374 |
|
// end of the comments. Note that you are not allowed to call the
|
375 |
|
// parser from the callback—that will corrupt its internal state.
|
376 |
|
onComment: null,
|
377 |
|
// Nodes have their start and end characters offsets recorded in
|
378 |
|
// `start` and `end` properties (directly on the node, rather than
|
379 |
|
// the `loc` object, which holds line/column data. To also add a
|
380 |
|
// [semi-standardized][range] `range` property holding a `[start,
|
381 |
|
// end]` array with the same numbers, set the `ranges` option to
|
382 |
|
// `true`.
|
383 |
|
//
|
384 |
|
// [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
|
385 |
|
ranges: false,
|
386 |
|
// It is possible to parse multiple files into a single AST by
|
387 |
|
// passing the tree produced by parsing the first file as
|
388 |
|
// `program` option in subsequent parses. This will add the
|
389 |
|
// toplevel forms of the parsed file to the `Program` (top) node
|
390 |
|
// of an existing parse tree.
|
391 |
|
program: null,
|
392 |
|
// When `locations` is on, you can pass this to record the source
|
393 |
|
// file in every node's `loc` object.
|
394 |
|
sourceFile: null,
|
395 |
|
// This value, if given, is stored in every node, whether
|
396 |
|
// `locations` is on or off.
|
397 |
|
directSourceFile: null,
|
398 |
|
// When enabled, parenthesized expressions are represented by
|
399 |
|
// (non-standard) ParenthesizedExpression nodes
|
400 |
|
preserveParens: false
|
401 |
|
};
|
402 |
|
|
403 |
|
// Interpret and default an options object
|
404 |
|
|
405 |
|
function getOptions(opts) {
|
406 |
|
var options = {};
|
407 |
|
|
408 |
|
for (var opt in defaultOptions)
|
409 |
|
{ options[opt] = opts && has(opts, opt) ? opts[opt] : defaultOptions[opt]; }
|
410 |
|
|
411 |
|
if (options.ecmaVersion >= 2015)
|
412 |
|
{ options.ecmaVersion -= 2009; }
|
413 |
|
|
414 |
|
if (options.allowReserved == null)
|
415 |
|
{ options.allowReserved = options.ecmaVersion < 5; }
|
416 |
|
|
417 |
|
if (isArray(options.onToken)) {
|
418 |
|
var tokens = options.onToken;
|
419 |
|
options.onToken = function (token) { return tokens.push(token); };
|
420 |
|
}
|
421 |
|
if (isArray(options.onComment))
|
422 |
|
{ options.onComment = pushComment(options, options.onComment); }
|
423 |
|
|
424 |
|
return options
|
425 |
|
}
|
426 |
|
|
427 |
|
function pushComment(options, array) {
|
428 |
|
return function(block, text, start, end, startLoc, endLoc) {
|
429 |
|
var comment = {
|
430 |
|
type: block ? "Block" : "Line",
|
431 |
|
value: text,
|
432 |
|
start: start,
|
433 |
|
end: end
|
434 |
|
};
|
435 |
|
if (options.locations)
|
436 |
|
{ comment.loc = new SourceLocation(this, startLoc, endLoc); }
|
437 |
|
if (options.ranges)
|
438 |
|
{ comment.range = [start, end]; }
|
439 |
|
array.push(comment);
|
440 |
|
}
|
441 |
|
}
|
442 |
|
|
443 |
|
// Each scope gets a bitset that may contain these flags
|
444 |
|
var
|
445 |
|
SCOPE_TOP = 1,
|
446 |
|
SCOPE_FUNCTION = 2,
|
447 |
|
SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION,
|
448 |
|
SCOPE_ASYNC = 4,
|
449 |
|
SCOPE_GENERATOR = 8,
|
450 |
|
SCOPE_ARROW = 16,
|
451 |
|
SCOPE_SIMPLE_CATCH = 32,
|
452 |
|
SCOPE_SUPER = 64,
|
453 |
|
SCOPE_DIRECT_SUPER = 128;
|
454 |
|
|
455 |
|
function functionFlags(async, generator) {
|
456 |
|
return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0)
|
457 |
|
}
|
458 |
|
|
459 |
|
// Used in checkLVal and declareName to determine the type of a binding
|
460 |
|
var
|
461 |
|
BIND_NONE = 0, // Not a binding
|
462 |
|
BIND_VAR = 1, // Var-style binding
|
463 |
|
BIND_LEXICAL = 2, // Let- or const-style binding
|
464 |
|
BIND_FUNCTION = 3, // Function declaration
|
465 |
|
BIND_SIMPLE_CATCH = 4, // Simple (identifier pattern) catch binding
|
466 |
|
BIND_OUTSIDE = 5; // Special case for function names as bound inside the function
|
467 |
|
|
468 |
|
var Parser = function Parser(options, input, startPos) {
|
469 |
|
this.options = options = getOptions(options);
|
470 |
|
this.sourceFile = options.sourceFile;
|
471 |
|
this.keywords = wordsRegexp(keywords[options.ecmaVersion >= 6 ? 6 : options.sourceType === "module" ? "5module" : 5]);
|
472 |
|
var reserved = "";
|
473 |
|
if (options.allowReserved !== true) {
|
474 |
|
for (var v = options.ecmaVersion;; v--)
|
475 |
|
{ if (reserved = reservedWords[v]) { break } }
|
476 |
|
if (options.sourceType === "module") { reserved += " await"; }
|
477 |
|
}
|
478 |
|
this.reservedWords = wordsRegexp(reserved);
|
479 |
|
var reservedStrict = (reserved ? reserved + " " : "") + reservedWords.strict;
|
480 |
|
this.reservedWordsStrict = wordsRegexp(reservedStrict);
|
481 |
|
this.reservedWordsStrictBind = wordsRegexp(reservedStrict + " " + reservedWords.strictBind);
|
482 |
|
this.input = String(input);
|
483 |
|
|
484 |
|
// Used to signal to callers of `readWord1` whether the word
|
485 |
|
// contained any escape sequences. This is needed because words with
|
486 |
|
// escape sequences must not be interpreted as keywords.
|
487 |
|
this.containsEsc = false;
|
488 |
|
|
489 |
|
// Set up token state
|
490 |
|
|
491 |
|
// The current position of the tokenizer in the input.
|
492 |
|
if (startPos) {
|
493 |
|
this.pos = startPos;
|
494 |
|
this.lineStart = this.input.lastIndexOf("\n", startPos - 1) + 1;
|
495 |
|
this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length;
|
496 |
|
} else {
|
497 |
|
this.pos = this.lineStart = 0;
|
498 |
|
this.curLine = 1;
|
499 |
|
}
|
500 |
|
|
501 |
|
// Properties of the current token:
|
502 |
|
// Its type
|
503 |
|
this.type = types.eof;
|
504 |
|
// For tokens that include more information than their type, the value
|
505 |
|
this.value = null;
|
506 |
|
// Its start and end offset
|
507 |
|
this.start = this.end = this.pos;
|
508 |
|
// And, if locations are used, the {line, column} object
|
509 |
|
// corresponding to those offsets
|
510 |
|
this.startLoc = this.endLoc = this.curPosition();
|
511 |
|
|
512 |
|
// Position information for the previous token
|
513 |
|
this.lastTokEndLoc = this.lastTokStartLoc = null;
|
514 |
|
this.lastTokStart = this.lastTokEnd = this.pos;
|
515 |
|
|
516 |
|
// The context stack is used to superficially track syntactic
|
517 |
|
// context to predict whether a regular expression is allowed in a
|
518 |
|
// given position.
|
519 |
|
this.context = this.initialContext();
|
520 |
|
this.exprAllowed = true;
|
521 |
|
|
522 |
|
// Figure out if it's a module code.
|
523 |
|
this.inModule = options.sourceType === "module";
|
524 |
|
this.strict = this.inModule || this.strictDirective(this.pos);
|
525 |
|
|
526 |
|
// Used to signify the start of a potential arrow function
|
527 |
|
this.potentialArrowAt = -1;
|
528 |
|
|
529 |
|
// Positions to delayed-check that yield/await does not exist in default parameters.
|
530 |
|
this.yieldPos = this.awaitPos = this.awaitIdentPos = 0;
|
531 |
|
// Labels in scope.
|
532 |
|
this.labels = [];
|
533 |
|
// Thus-far undefined exports.
|
534 |
|
this.undefinedExports = {};
|
535 |
|
|
536 |
|
// If enabled, skip leading hashbang line.
|
537 |
|
if (this.pos === 0 && options.allowHashBang && this.input.slice(0, 2) === "#!")
|
538 |
|
{ this.skipLineComment(2); }
|
539 |
|
|
540 |
|
// Scope tracking for duplicate variable names (see scope.js)
|
541 |
|
this.scopeStack = [];
|
542 |
|
this.enterScope(SCOPE_TOP);
|
543 |
|
|
544 |
|
// For RegExp validation
|
545 |
|
this.regexpState = null;
|
546 |
|
};
|
547 |
|
|
548 |
|
var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true } };
|
549 |
|
|
550 |
|
Parser.prototype.parse = function parse () {
|
551 |
|
var node = this.options.program || this.startNode();
|
552 |
|
this.nextToken();
|
553 |
|
return this.parseTopLevel(node)
|
554 |
|
};
|
555 |
|
|
556 |
|
prototypeAccessors.inFunction.get = function () { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 };
|
557 |
|
prototypeAccessors.inGenerator.get = function () { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 };
|
558 |
|
prototypeAccessors.inAsync.get = function () { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 };
|
559 |
|
prototypeAccessors.allowSuper.get = function () { return (this.currentThisScope().flags & SCOPE_SUPER) > 0 };
|
560 |
|
prototypeAccessors.allowDirectSuper.get = function () { return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0 };
|
561 |
|
prototypeAccessors.treatFunctionsAsVar.get = function () { return this.treatFunctionsAsVarInScope(this.currentScope()) };
|
562 |
|
|
563 |
|
// Switch to a getter for 7.0.0.
|
564 |
|
Parser.prototype.inNonArrowFunction = function inNonArrowFunction () { return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0 };
|
565 |
|
|
566 |
|
Parser.extend = function extend () {
|
567 |
|
var plugins = [], len = arguments.length;
|
568 |
|
while ( len-- ) plugins[ len ] = arguments[ len ];
|
569 |
|
|
570 |
|
var cls = this;
|
571 |
|
for (var i = 0; i < plugins.length; i++) { cls = plugins[i](cls); }
|
572 |
|
return cls
|
573 |
|
};
|
574 |
|
|
575 |
|
Parser.parse = function parse (input, options) {
|
576 |
|
return new this(options, input).parse()
|
577 |
|
};
|
578 |
|
|
579 |
|
Parser.parseExpressionAt = function parseExpressionAt (input, pos, options) {
|
580 |
|
var parser = new this(options, input, pos);
|
581 |
|
parser.nextToken();
|
582 |
|
return parser.parseExpression()
|
583 |
|
};
|
584 |
|
|
585 |
|
Parser.tokenizer = function tokenizer (input, options) {
|
586 |
|
return new this(options, input)
|
587 |
|
};
|
588 |
|
|
589 |
|
Object.defineProperties( Parser.prototype, prototypeAccessors );
|
590 |
|
|
591 |
|
var pp = Parser.prototype;
|
592 |
|
|
593 |
|
// ## Parser utilities
|
594 |
|
|
595 |
|
var literal = /^(?:'((?:\\.|[^'])*?)'|"((?:\\.|[^"])*?)")/;
|
596 |
|
pp.strictDirective = function(start) {
|
597 |
|
for (;;) {
|
598 |
|
// Try to find string literal.
|
599 |
|
skipWhiteSpace.lastIndex = start;
|
600 |
|
start += skipWhiteSpace.exec(this.input)[0].length;
|
601 |
|
var match = literal.exec(this.input.slice(start));
|
602 |
|
if (!match) { return false }
|
603 |
|
if ((match[1] || match[2]) === "use strict") { return true }
|
604 |
|
start += match[0].length;
|
605 |
|
|
606 |
|
// Skip semicolon, if any.
|
607 |
|
skipWhiteSpace.lastIndex = start;
|
608 |
|
start += skipWhiteSpace.exec(this.input)[0].length;
|
609 |
|
if (this.input[start] === ";")
|
610 |
|
{ start++; }
|
611 |
|
}
|
612 |
|
};
|
613 |
|
|
614 |
|
// Predicate that tests whether the next token is of the given
|
615 |
|
// type, and if yes, consumes it as a side effect.
|
616 |
|
|
617 |
|
pp.eat = function(type) {
|
618 |
|
if (this.type === type) {
|
619 |
|
this.next();
|
620 |
|
return true
|
621 |
|
} else {
|
622 |
|
return false
|
623 |
|
}
|
624 |
|
};
|
625 |
|
|
626 |
|
// Tests whether parsed token is a contextual keyword.
|
627 |
|
|
628 |
|
pp.isContextual = function(name) {
|
629 |
|
return this.type === types.name && this.value === name && !this.containsEsc
|
630 |
|
};
|
631 |
|
|
632 |
|
// Consumes contextual keyword if possible.
|
633 |
|
|
634 |
|
pp.eatContextual = function(name) {
|
635 |
|
if (!this.isContextual(name)) { return false }
|
636 |
|
this.next();
|
637 |
|
return true
|
638 |
|
};
|
639 |
|
|
640 |
|
// Asserts that following token is given contextual keyword.
|
641 |
|
|
642 |
|
pp.expectContextual = function(name) {
|
643 |
|
if (!this.eatContextual(name)) { this.unexpected(); }
|
644 |
|
};
|
645 |
|
|
646 |
|
// Test whether a semicolon can be inserted at the current position.
|
647 |
|
|
648 |
|
pp.canInsertSemicolon = function() {
|
649 |
|
return this.type === types.eof ||
|
650 |
|
this.type === types.braceR ||
|
651 |
|
lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
|
652 |
|
};
|
653 |
|
|
654 |
|
pp.insertSemicolon = function() {
|
655 |
|
if (this.canInsertSemicolon()) {
|
656 |
|
if (this.options.onInsertedSemicolon)
|
657 |
|
{ this.options.onInsertedSemicolon(this.lastTokEnd, this.lastTokEndLoc); }
|
658 |
|
return true
|
659 |
|
}
|
660 |
|
};
|
661 |
|
|
662 |
|
// Consume a semicolon, or, failing that, see if we are allowed to
|
663 |
|
// pretend that there is a semicolon at this position.
|
664 |
|
|
665 |
|
pp.semicolon = function() {
|
666 |
|
if (!this.eat(types.semi) && !this.insertSemicolon()) { this.unexpected(); }
|
667 |
|
};
|
668 |
|
|
669 |
|
pp.afterTrailingComma = function(tokType, notNext) {
|
670 |
|
if (this.type === tokType) {
|
671 |
|
if (this.options.onTrailingComma)
|
672 |
|
{ this.options.onTrailingComma(this.lastTokStart, this.lastTokStartLoc); }
|
673 |
|
if (!notNext)
|
674 |
|
{ this.next(); }
|
675 |
|
return true
|
676 |
|
}
|
677 |
|
};
|
678 |
|
|
679 |
|
// Expect a token of a given type. If found, consume it, otherwise,
|
680 |
|
// raise an unexpected token error.
|
681 |
|
|
682 |
|
pp.expect = function(type) {
|
683 |
|
this.eat(type) || this.unexpected();
|
684 |
|
};
|
685 |
|
|
686 |
|
// Raise an unexpected token error.
|
687 |
|
|
688 |
|
pp.unexpected = function(pos) {
|
689 |
|
this.raise(pos != null ? pos : this.start, "Unexpected token");
|
690 |
|
};
|
691 |
|
|
692 |
|
function DestructuringErrors() {
|
693 |
|
this.shorthandAssign =
|
694 |
|
this.trailingComma =
|
695 |
|
this.parenthesizedAssign =
|
696 |
|
this.parenthesizedBind =
|
697 |
|
this.doubleProto =
|
698 |
|
-1;
|
699 |
|
}
|
700 |
|
|
701 |
|
pp.checkPatternErrors = function(refDestructuringErrors, isAssign) {
|
702 |
|
if (!refDestructuringErrors) { return }
|
703 |
|
if (refDestructuringErrors.trailingComma > -1)
|
704 |
|
{ this.raiseRecoverable(refDestructuringErrors.trailingComma, "Comma is not permitted after the rest element"); }
|
705 |
|
var parens = isAssign ? refDestructuringErrors.parenthesizedAssign : refDestructuringErrors.parenthesizedBind;
|
706 |
|
if (parens > -1) { this.raiseRecoverable(parens, "Parenthesized pattern"); }
|
707 |
|
};
|
708 |
|
|
709 |
|
pp.checkExpressionErrors = function(refDestructuringErrors, andThrow) {
|
710 |
|
if (!refDestructuringErrors) { return false }
|
711 |
|
var shorthandAssign = refDestructuringErrors.shorthandAssign;
|
712 |
|
var doubleProto = refDestructuringErrors.doubleProto;
|
713 |
|
if (!andThrow) { return shorthandAssign >= 0 || doubleProto >= 0 }
|
714 |
|
if (shorthandAssign >= 0)
|
715 |
|
{ this.raise(shorthandAssign, "Shorthand property assignments are valid only in destructuring patterns"); }
|
716 |
|
if (doubleProto >= 0)
|
717 |
|
{ this.raiseRecoverable(doubleProto, "Redefinition of __proto__ property"); }
|
718 |
|
};
|
719 |
|
|
720 |
|
pp.checkYieldAwaitInDefaultParams = function() {
|
721 |
|
if (this.yieldPos && (!this.awaitPos || this.yieldPos < this.awaitPos))
|
722 |
|
{ this.raise(this.yieldPos, "Yield expression cannot be a default value"); }
|
723 |
|
if (this.awaitPos)
|
724 |
|
{ this.raise(this.awaitPos, "Await expression cannot be a default value"); }
|
725 |
|
};
|
726 |
|
|
727 |
|
pp.isSimpleAssignTarget = function(expr) {
|
728 |
|
if (expr.type === "ParenthesizedExpression")
|
729 |
|
{ return this.isSimpleAssignTarget(expr.expression) }
|
730 |
|
return expr.type === "Identifier" || expr.type === "MemberExpression"
|
731 |
|
};
|
732 |
|
|
733 |
|
var pp$1 = Parser.prototype;
|
734 |
|
|
735 |
|
// ### Statement parsing
|
736 |
|
|
737 |
|
// Parse a program. Initializes the parser, reads any number of
|
738 |
|
// statements, and wraps them in a Program node. Optionally takes a
|
739 |
|
// `program` argument. If present, the statements will be appended
|
740 |
|
// to its body instead of creating a new node.
|
741 |
|
|
742 |
|
pp$1.parseTopLevel = function(node) {
|
743 |
|
var exports = {};
|
744 |
|
if (!node.body) { node.body = []; }
|
745 |
|
while (this.type !== types.eof) {
|
746 |
|
var stmt = this.parseStatement(null, true, exports);
|
747 |
|
node.body.push(stmt);
|
748 |
|
}
|
749 |
|
if (this.inModule)
|
750 |
|
{ for (var i = 0, list = Object.keys(this.undefinedExports); i < list.length; i += 1)
|
751 |
|
{
|
752 |
|
var name = list[i];
|
753 |
|
|
754 |
|
this.raiseRecoverable(this.undefinedExports[name].start, ("Export '" + name + "' is not defined"));
|
755 |
|
} }
|
756 |
|
this.adaptDirectivePrologue(node.body);
|
757 |
|
this.next();
|
758 |
|
node.sourceType = this.options.sourceType;
|
759 |
|
return this.finishNode(node, "Program")
|
760 |
|
};
|
761 |
|
|
762 |
|
var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
|
763 |
|
|
764 |
|
pp$1.isLet = function(context) {
|
765 |
|
if (this.options.ecmaVersion < 6 || !this.isContextual("let")) { return false }
|
766 |
|
skipWhiteSpace.lastIndex = this.pos;
|
767 |
|
var skip = skipWhiteSpace.exec(this.input);
|
768 |
|
var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
|
769 |
|
// For ambiguous cases, determine if a LexicalDeclaration (or only a
|
770 |
|
// Statement) is allowed here. If context is not empty then only a Statement
|
771 |
|
// is allowed. However, `let [` is an explicit negative lookahead for
|
772 |
|
// ExpressionStatement, so special-case it first.
|
773 |
|
if (nextCh === 91) { return true } // '['
|
774 |
|
if (context) { return false }
|
775 |
|
|
776 |
|
if (nextCh === 123) { return true } // '{'
|
777 |
|
if (isIdentifierStart(nextCh, true)) {
|
778 |
|
var pos = next + 1;
|
779 |
|
while (isIdentifierChar(this.input.charCodeAt(pos), true)) { ++pos; }
|
780 |
|
var ident = this.input.slice(next, pos);
|
781 |
|
if (!keywordRelationalOperator.test(ident)) { return true }
|
782 |
|
}
|
783 |
|
return false
|
784 |
|
};
|
785 |
|
|
786 |
|
// check 'async [no LineTerminator here] function'
|
787 |
|
// - 'async /*foo*/ function' is OK.
|
788 |
|
// - 'async /*\n*/ function' is invalid.
|
789 |
|
pp$1.isAsyncFunction = function() {
|
790 |
|
if (this.options.ecmaVersion < 8 || !this.isContextual("async"))
|
791 |
|
{ return false }
|
792 |
|
|
793 |
|
skipWhiteSpace.lastIndex = this.pos;
|
794 |
|
var skip = skipWhiteSpace.exec(this.input);
|
795 |
|
var next = this.pos + skip[0].length;
|
796 |
|
return !lineBreak.test(this.input.slice(this.pos, next)) &&
|
797 |
|
this.input.slice(next, next + 8) === "function" &&
|
798 |
|
(next + 8 === this.input.length || !isIdentifierChar(this.input.charAt(next + 8)))
|
799 |
|
};
|
800 |
|
|
801 |
|
// Parse a single statement.
|
802 |
|
//
|
803 |
|
// If expecting a statement and finding a slash operator, parse a
|
804 |
|
// regular expression literal. This is to handle cases like
|
805 |
|
// `if (foo) /blah/.exec(foo)`, where looking at the previous token
|
806 |
|
// does not help.
|
807 |
|
|
808 |
|
pp$1.parseStatement = function(context, topLevel, exports) {
|
809 |
|
var starttype = this.type, node = this.startNode(), kind;
|
810 |
|
|
811 |
|
if (this.isLet(context)) {
|
812 |
|
starttype = types._var;
|
813 |
|
kind = "let";
|
814 |
|
}
|
815 |
|
|
816 |
|
// Most types of statements are recognized by the keyword they
|
817 |
|
// start with. Many are trivial to parse, some require a bit of
|
818 |
|
// complexity.
|
819 |
|
|
820 |
|
switch (starttype) {
|
821 |
|
case types._break: case types._continue: return this.parseBreakContinueStatement(node, starttype.keyword)
|
822 |
|
case types._debugger: return this.parseDebuggerStatement(node)
|
823 |
|
case types._do: return this.parseDoStatement(node)
|
824 |
|
case types._for: return this.parseForStatement(node)
|
825 |
|
case types._function:
|
826 |
|
// Function as sole body of either an if statement or a labeled statement
|
827 |
|
// works, but not when it is part of a labeled statement that is the sole
|
828 |
|
// body of an if statement.
|
829 |
|
if ((context && (this.strict || context !== "if" && context !== "label")) && this.options.ecmaVersion >= 6) { this.unexpected(); }
|
830 |
|
return this.parseFunctionStatement(node, false, !context)
|
831 |
|
case types._class:
|
832 |
|
if (context) { this.unexpected(); }
|
833 |
|
return this.parseClass(node, true)
|
834 |
|
case types._if: return this.parseIfStatement(node)
|
835 |
|
case types._return: return this.parseReturnStatement(node)
|
836 |
|
case types._switch: return this.parseSwitchStatement(node)
|
837 |
|
case types._throw: return this.parseThrowStatement(node)
|
838 |
|
case types._try: return this.parseTryStatement(node)
|
839 |
|
case types._const: case types._var:
|
840 |
|
kind = kind || this.value;
|
841 |
|
if (context && kind !== "var") { this.unexpected(); }
|
842 |
|
return this.parseVarStatement(node, kind)
|
843 |
|
case types._while: return this.parseWhileStatement(node)
|
844 |
|
case types._with: return this.parseWithStatement(node)
|
845 |
|
case types.braceL: return this.parseBlock(true, node)
|
846 |
|
case types.semi: return this.parseEmptyStatement(node)
|
847 |
|
case types._export:
|
848 |
|
case types._import:
|
849 |
|
if (this.options.ecmaVersion > 10 && starttype === types._import) {
|
850 |
|
skipWhiteSpace.lastIndex = this.pos;
|
851 |
|
var skip = skipWhiteSpace.exec(this.input);
|
852 |
|
var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
|
853 |
|
if (nextCh === 40) // '('
|
854 |
|
{ return this.parseExpressionStatement(node, this.parseExpression()) }
|
855 |
|
}
|
856 |
|
|
857 |
|
if (!this.options.allowImportExportEverywhere) {
|
858 |
|
if (!topLevel)
|
859 |
|
{ this.raise(this.start, "'import' and 'export' may only appear at the top level"); }
|
860 |
|
if (!this.inModule)
|
861 |
|
{ this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'"); }
|
862 |
|
}
|
863 |
|
return starttype === types._import ? this.parseImport(node) : this.parseExport(node, exports)
|
864 |
|
|
865 |
|
// If the statement does not start with a statement keyword or a
|
866 |
|
// brace, it's an ExpressionStatement or LabeledStatement. We
|
867 |
|
// simply start parsing an expression, and afterwards, if the
|
868 |
|
// next token is a colon and the expression was a simple
|
869 |
|
// Identifier node, we switch to interpreting it as a label.
|
870 |
|
default:
|
871 |
|
if (this.isAsyncFunction()) {
|
872 |
|
if (context) { this.unexpected(); }
|
873 |
|
this.next();
|
874 |
|
return this.parseFunctionStatement(node, true, !context)
|
875 |
|
}
|
876 |
|
|
877 |
|
var maybeName = this.value, expr = this.parseExpression();
|
878 |
|
if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon))
|
879 |
|
{ return this.parseLabeledStatement(node, maybeName, expr, context) }
|
880 |
|
else { return this.parseExpressionStatement(node, expr) }
|
881 |
|
}
|
882 |
|
};
|
883 |
|
|
884 |
|
pp$1.parseBreakContinueStatement = function(node, keyword) {
|
885 |
|
var isBreak = keyword === "break";
|
886 |
|
this.next();
|
887 |
|
if (this.eat(types.semi) || this.insertSemicolon()) { node.label = null; }
|
888 |
|
else if (this.type !== types.name) { this.unexpected(); }
|
889 |
|
else {
|
890 |
|
node.label = this.parseIdent();
|
891 |
|
this.semicolon();
|
892 |
|
}
|
893 |
|
|
894 |
|
// Verify that there is an actual destination to break or
|
895 |
|
// continue to.
|
896 |
|
var i = 0;
|
897 |
|
for (; i < this.labels.length; ++i) {
|
898 |
|
var lab = this.labels[i];
|
899 |
|
if (node.label == null || lab.name === node.label.name) {
|
900 |
|
if (lab.kind != null && (isBreak || lab.kind === "loop")) { break }
|
901 |
|
if (node.label && isBreak) { break }
|
902 |
|
}
|
903 |
|
}
|
904 |
|
if (i === this.labels.length) { this.raise(node.start, "Unsyntactic " + keyword); }
|
905 |
|
return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
|
906 |
|
};
|
907 |
|
|
908 |
|
pp$1.parseDebuggerStatement = function(node) {
|
909 |
|
this.next();
|
910 |
|
this.semicolon();
|
911 |
|
return this.finishNode(node, "DebuggerStatement")
|
912 |
|
};
|
913 |
|
|
914 |
|
pp$1.parseDoStatement = function(node) {
|
915 |
|
this.next();
|
916 |
|
this.labels.push(loopLabel);
|
917 |
|
node.body = this.parseStatement("do");
|
918 |
|
this.labels.pop();
|
919 |
|
this.expect(types._while);
|
920 |
|
node.test = this.parseParenExpression();
|
921 |
|
if (this.options.ecmaVersion >= 6)
|
922 |
|
{ this.eat(types.semi); }
|
923 |
|
else
|
924 |
|
{ this.semicolon(); }
|
925 |
|
return this.finishNode(node, "DoWhileStatement")
|
926 |
|
};
|
927 |
|
|
928 |
|
// Disambiguating between a `for` and a `for`/`in` or `for`/`of`
|
929 |
|
// loop is non-trivial. Basically, we have to parse the init `var`
|
930 |
|
// statement or expression, disallowing the `in` operator (see
|
931 |
|
// the second parameter to `parseExpression`), and then check
|
932 |
|
// whether the next token is `in` or `of`. When there is no init
|
933 |
|
// part (semicolon immediately after the opening parenthesis), it
|
934 |
|
// is a regular `for` loop.
|
935 |
|
|
936 |
|
pp$1.parseForStatement = function(node) {
|
937 |
|
this.next();
|
938 |
|
var awaitAt = (this.options.ecmaVersion >= 9 && (this.inAsync || (!this.inFunction && this.options.allowAwaitOutsideFunction)) && this.eatContextual("await")) ? this.lastTokStart : -1;
|
939 |
|
this.labels.push(loopLabel);
|
940 |
|
this.enterScope(0);
|
941 |
|
this.expect(types.parenL);
|
942 |
|
if (this.type === types.semi) {
|
943 |
|
if (awaitAt > -1) { this.unexpected(awaitAt); }
|
944 |
|
return this.parseFor(node, null)
|
945 |
|
}
|
946 |
|
var isLet = this.isLet();
|
947 |
|
if (this.type === types._var || this.type === types._const || isLet) {
|
948 |
|
var init$1 = this.startNode(), kind = isLet ? "let" : this.value;
|
949 |
|
this.next();
|
950 |
|
this.parseVar(init$1, true, kind);
|
951 |
|
this.finishNode(init$1, "VariableDeclaration");
|
952 |
|
if ((this.type === types._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) && init$1.declarations.length === 1) {
|
953 |
|
if (this.options.ecmaVersion >= 9) {
|
954 |
|
if (this.type === types._in) {
|
955 |
|
if (awaitAt > -1) { this.unexpected(awaitAt); }
|
956 |
|
} else { node.await = awaitAt > -1; }
|
957 |
|
}
|
958 |
|
return this.parseForIn(node, init$1)
|
959 |
|
}
|
960 |
|
if (awaitAt > -1) { this.unexpected(awaitAt); }
|
961 |
|
return this.parseFor(node, init$1)
|
962 |
|
}
|
963 |
|
var refDestructuringErrors = new DestructuringErrors;
|
964 |
|
var init = this.parseExpression(true, refDestructuringErrors);
|
965 |
|
if (this.type === types._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
|
966 |
|
if (this.options.ecmaVersion >= 9) {
|
967 |
|
if (this.type === types._in) {
|
968 |
|
if (awaitAt > -1) { this.unexpected(awaitAt); }
|
969 |
|
} else { node.await = awaitAt > -1; }
|
970 |
|
}
|
971 |
|
this.toAssignable(init, false, refDestructuringErrors);
|
972 |
|
this.checkLVal(init);
|
973 |
|
return this.parseForIn(node, init)
|
974 |
|
} else {
|
975 |
|
this.checkExpressionErrors(refDestructuringErrors, true);
|
976 |
|
}
|
977 |
|
if (awaitAt > -1) { this.unexpected(awaitAt); }
|
978 |
|
return this.parseFor(node, init)
|
979 |
|
};
|
980 |
|
|
981 |
|
pp$1.parseFunctionStatement = function(node, isAsync, declarationPosition) {
|
982 |
|
this.next();
|
983 |
|
return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), false, isAsync)
|
984 |
|
};
|
985 |
|
|
986 |
|
pp$1.parseIfStatement = function(node) {
|
987 |
|
this.next();
|
988 |
|
node.test = this.parseParenExpression();
|
989 |
|
// allow function declarations in branches, but only in non-strict mode
|
990 |
|
node.consequent = this.parseStatement("if");
|
991 |
|
node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
|
992 |
|
return this.finishNode(node, "IfStatement")
|
993 |
|
};
|
994 |
|
|
995 |
|
pp$1.parseReturnStatement = function(node) {
|
996 |
|
if (!this.inFunction && !this.options.allowReturnOutsideFunction)
|
997 |
|
{ this.raise(this.start, "'return' outside of function"); }
|
998 |
|
this.next();
|
999 |
|
|
1000 |
|
// In `return` (and `break`/`continue`), the keywords with
|
1001 |
|
// optional arguments, we eagerly look for a semicolon or the
|
1002 |
|
// possibility to insert one.
|
1003 |
|
|
1004 |
|
if (this.eat(types.semi) || this.insertSemicolon()) { node.argument = null; }
|
1005 |
|
else { node.argument = this.parseExpression(); this.semicolon(); }
|
1006 |
|
return this.finishNode(node, "ReturnStatement")
|
1007 |
|
};
|
1008 |
|
|
1009 |
|
pp$1.parseSwitchStatement = function(node) {
|
1010 |
|
this.next();
|
1011 |
|
node.discriminant = this.parseParenExpression();
|
1012 |
|
node.cases = [];
|
1013 |
|
this.expect(types.braceL);
|
1014 |
|
this.labels.push(switchLabel);
|
1015 |
|
this.enterScope(0);
|
1016 |
|
|
1017 |
|
// Statements under must be grouped (by label) in SwitchCase
|
1018 |
|
// nodes. `cur` is used to keep the node that we are currently
|
1019 |
|
// adding statements to.
|
1020 |
|
|
1021 |
|
var cur;
|
1022 |
|
for (var sawDefault = false; this.type !== types.braceR;) {
|
1023 |
|
if (this.type === types._case || this.type === types._default) {
|
1024 |
|
var isCase = this.type === types._case;
|
1025 |
|
if (cur) { this.finishNode(cur, "SwitchCase"); }
|
1026 |
|
node.cases.push(cur = this.startNode());
|
1027 |
|
cur.consequent = [];
|
1028 |
|
this.next();
|
1029 |
|
if (isCase) {
|
1030 |
|
cur.test = this.parseExpression();
|
1031 |
|
} else {
|
1032 |
|
if (sawDefault) { this.raiseRecoverable(this.lastTokStart, "Multiple default clauses"); }
|
1033 |
|
sawDefault = true;
|
1034 |
|
cur.test = null;
|
1035 |
|
}
|
1036 |
|
this.expect(types.colon);
|
1037 |
|
} else {
|
1038 |
|
if (!cur) { this.unexpected(); }
|
1039 |
|
cur.consequent.push(this.parseStatement(null));
|
1040 |
|
}
|
1041 |
|
}
|
1042 |
|
this.exitScope();
|
1043 |
|
if (cur) { this.finishNode(cur, "SwitchCase"); }
|
1044 |
|
this.next(); // Closing brace
|
1045 |
|
this.labels.pop();
|
1046 |
|
return this.finishNode(node, "SwitchStatement")
|
1047 |
|
};
|
1048 |
|
|
1049 |
|
pp$1.parseThrowStatement = function(node) {
|
1050 |
|
this.next();
|
1051 |
|
if (lineBreak.test(this.input.slice(this.lastTokEnd, this.start)))
|
1052 |
|
{ this.raise(this.lastTokEnd, "Illegal newline after throw"); }
|
1053 |
|
node.argument = this.parseExpression();
|
1054 |
|
this.semicolon();
|
1055 |
|
return this.finishNode(node, "ThrowStatement")
|
1056 |
|
};
|
1057 |
|
|
1058 |
|
// Reused empty array added for node fields that are always empty.
|
1059 |
|
|
1060 |
|
var empty = [];
|
1061 |
|
|
1062 |
|
pp$1.parseTryStatement = function(node) {
|
1063 |
|
this.next();
|
1064 |
|
node.block = this.parseBlock();
|
1065 |
|
node.handler = null;
|
1066 |
|
if (this.type === types._catch) {
|
1067 |
|
var clause = this.startNode();
|
1068 |
|
this.next();
|
1069 |
|
if (this.eat(types.parenL)) {
|
1070 |
|
clause.param = this.parseBindingAtom();
|
1071 |
|
var simple = clause.param.type === "Identifier";
|
1072 |
|
this.enterScope(simple ? SCOPE_SIMPLE_CATCH : 0);
|
1073 |
|
this.checkLVal(clause.param, simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL);
|
1074 |
|
this.expect(types.parenR);
|
1075 |
|
} else {
|
1076 |
|
if (this.options.ecmaVersion < 10) { this.unexpected(); }
|
1077 |
|
clause.param = null;
|
1078 |
|
this.enterScope(0);
|
1079 |
|
}
|
1080 |
|
clause.body = this.parseBlock(false);
|
1081 |
|
this.exitScope();
|
1082 |
|
node.handler = this.finishNode(clause, "CatchClause");
|
1083 |
|
}
|
1084 |
|
node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
|
1085 |
|
if (!node.handler && !node.finalizer)
|
1086 |
|
{ this.raise(node.start, "Missing catch or finally clause"); }
|
1087 |
|
return this.finishNode(node, "TryStatement")
|
1088 |
|
};
|
1089 |
|
|
1090 |
|
pp$1.parseVarStatement = function(node, kind) {
|
1091 |
|
this.next();
|
1092 |
|
this.parseVar(node, false, kind);
|
1093 |
|
this.semicolon();
|
1094 |
|
return this.finishNode(node, "VariableDeclaration")
|
1095 |
|
};
|
1096 |
|
|
1097 |
|
pp$1.parseWhileStatement = function(node) {
|
1098 |
|
this.next();
|
1099 |
|
node.test = this.parseParenExpression();
|
1100 |
|
this.labels.push(loopLabel);
|
1101 |
|
node.body = this.parseStatement("while");
|
1102 |
|
this.labels.pop();
|
1103 |
|
return this.finishNode(node, "WhileStatement")
|
1104 |
|
};
|
1105 |
|
|
1106 |
|
pp$1.parseWithStatement = function(node) {
|
1107 |
|
if (this.strict) { this.raise(this.start, "'with' in strict mode"); }
|
1108 |
|
this.next();
|
1109 |
|
node.object = this.parseParenExpression();
|
1110 |
|
node.body = this.parseStatement("with");
|
1111 |
|
return this.finishNode(node, "WithStatement")
|
1112 |
|
};
|
1113 |
|
|
1114 |
|
pp$1.parseEmptyStatement = function(node) {
|
1115 |
|
this.next();
|
1116 |
|
return this.finishNode(node, "EmptyStatement")
|
1117 |
|
};
|
1118 |
|
|
1119 |
|
pp$1.parseLabeledStatement = function(node, maybeName, expr, context) {
|
1120 |
|
for (var i$1 = 0, list = this.labels; i$1 < list.length; i$1 += 1)
|
1121 |
|
{
|
1122 |
|
var label = list[i$1];
|
1123 |
|
|
1124 |
|
if (label.name === maybeName)
|
1125 |
|
{ this.raise(expr.start, "Label '" + maybeName + "' is already declared");
|
1126 |
|
} }
|
1127 |
|
var kind = this.type.isLoop ? "loop" : this.type === types._switch ? "switch" : null;
|
1128 |
|
for (var i = this.labels.length - 1; i >= 0; i--) {
|
1129 |
|
var label$1 = this.labels[i];
|
1130 |
|
if (label$1.statementStart === node.start) {
|
1131 |
|
// Update information about previous labels on this node
|
1132 |
|
label$1.statementStart = this.start;
|
1133 |
|
label$1.kind = kind;
|
1134 |
|
} else { break }
|
1135 |
|
}
|
1136 |
|
this.labels.push({name: maybeName, kind: kind, statementStart: this.start});
|
1137 |
|
node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
|
1138 |
|
this.labels.pop();
|
1139 |
|
node.label = expr;
|
1140 |
|
return this.finishNode(node, "LabeledStatement")
|
1141 |
|
};
|
1142 |
|
|
1143 |
|
pp$1.parseExpressionStatement = function(node, expr) {
|
1144 |
|
node.expression = expr;
|
1145 |
|
this.semicolon();
|
1146 |
|
return this.finishNode(node, "ExpressionStatement")
|
1147 |
|
};
|
1148 |
|
|
1149 |
|
// Parse a semicolon-enclosed block of statements, handling `"use
|
1150 |
|
// strict"` declarations when `allowStrict` is true (used for
|
1151 |
|
// function bodies).
|
1152 |
|
|
1153 |
|
pp$1.parseBlock = function(createNewLexicalScope, node) {
|
1154 |
|
if ( createNewLexicalScope === void 0 ) createNewLexicalScope = true;
|
1155 |
|
if ( node === void 0 ) node = this.startNode();
|
1156 |
|
|
1157 |
|
node.body = [];
|
1158 |
|
this.expect(types.braceL);
|
1159 |
|
if (createNewLexicalScope) { this.enterScope(0); }
|
1160 |
|
while (!this.eat(types.braceR)) {
|
1161 |
|
var stmt = this.parseStatement(null);
|
1162 |
|
node.body.push(stmt);
|
1163 |
|
}
|
1164 |
|
if (createNewLexicalScope) { this.exitScope(); }
|
1165 |
|
return this.finishNode(node, "BlockStatement")
|
1166 |
|
};
|
1167 |
|
|
1168 |
|
// Parse a regular `for` loop. The disambiguation code in
|
1169 |
|
// `parseStatement` will already have parsed the init statement or
|
1170 |
|
// expression.
|
1171 |
|
|
1172 |
|
pp$1.parseFor = function(node, init) {
|
1173 |
|
node.init = init;
|
1174 |
|
this.expect(types.semi);
|
1175 |
|
node.test = this.type === types.semi ? null : this.parseExpression();
|
1176 |
|
this.expect(types.semi);
|
1177 |
|
node.update = this.type === types.parenR ? null : this.parseExpression();
|
1178 |
|
this.expect(types.parenR);
|
1179 |
|
node.body = this.parseStatement("for");
|
1180 |
|
this.exitScope();
|
1181 |
|
this.labels.pop();
|
1182 |
|
return this.finishNode(node, "ForStatement")
|
1183 |
|
};
|
1184 |
|
|
1185 |
|
// Parse a `for`/`in` and `for`/`of` loop, which are almost
|
1186 |
|
// same from parser's perspective.
|
1187 |
|
|
1188 |
|
pp$1.parseForIn = function(node, init) {
|
1189 |
|
var isForIn = this.type === types._in;
|
1190 |
|
this.next();
|
1191 |
|
|
1192 |
|
if (
|
1193 |
|
init.type === "VariableDeclaration" &&
|
1194 |
|
init.declarations[0].init != null &&
|
1195 |
|
(
|
1196 |
|
!isForIn ||
|
1197 |
|
this.options.ecmaVersion < 8 ||
|
1198 |
|
this.strict ||
|
1199 |
|
init.kind !== "var" ||
|
1200 |
|
init.declarations[0].id.type !== "Identifier"
|
1201 |
|
)
|
1202 |
|
) {
|
1203 |
|
this.raise(
|
1204 |
|
init.start,
|
1205 |
|
((isForIn ? "for-in" : "for-of") + " loop variable declaration may not have an initializer")
|
1206 |
|
);
|
1207 |
|
} else if (init.type === "AssignmentPattern") {
|
1208 |
|
this.raise(init.start, "Invalid left-hand side in for-loop");
|
1209 |
|
}
|
1210 |
|
node.left = init;
|
1211 |
|
node.right = isForIn ? this.parseExpression() : this.parseMaybeAssign();
|
1212 |
|
this.expect(types.parenR);
|
1213 |
|
node.body = this.parseStatement("for");
|
1214 |
|
this.exitScope();
|
1215 |
|
this.labels.pop();
|
1216 |
|
return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement")
|
1217 |
|
};
|
1218 |
|
|
1219 |
|
// Parse a list of variable declarations.
|
1220 |
|
|
1221 |
|
pp$1.parseVar = function(node, isFor, kind) {
|
1222 |
|
node.declarations = [];
|
1223 |
|
node.kind = kind;
|
1224 |
|
for (;;) {
|
1225 |
|
var decl = this.startNode();
|
1226 |
|
this.parseVarId(decl, kind);
|
1227 |
|
if (this.eat(types.eq)) {
|
1228 |
|
decl.init = this.parseMaybeAssign(isFor);
|
1229 |
|
} else if (kind === "const" && !(this.type === types._in || (this.options.ecmaVersion >= 6 && this.isContextual("of")))) {
|
1230 |
|
this.unexpected();
|
1231 |
|
} else if (decl.id.type !== "Identifier" && !(isFor && (this.type === types._in || this.isContextual("of")))) {
|
1232 |
|
this.raise(this.lastTokEnd, "Complex binding patterns require an initialization value");
|
1233 |
|
} else {
|
1234 |
|
decl.init = null;
|
1235 |
|
}
|
1236 |
|
node.declarations.push(this.finishNode(decl, "VariableDeclarator"));
|
1237 |
|
if (!this.eat(types.comma)) { break }
|
1238 |
|
}
|
1239 |
|
return node
|
1240 |
|
};
|
1241 |
|
|
1242 |
|
pp$1.parseVarId = function(decl, kind) {
|
1243 |
|
decl.id = this.parseBindingAtom();
|
1244 |
|
this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false);
|
1245 |
|
};
|
1246 |
|
|
1247 |
|
var FUNC_STATEMENT = 1, FUNC_HANGING_STATEMENT = 2, FUNC_NULLABLE_ID = 4;
|
1248 |
|
|
1249 |
|
// Parse a function declaration or literal (depending on the
|
1250 |
|
// `statement & FUNC_STATEMENT`).
|
1251 |
|
|
1252 |
|
// Remove `allowExpressionBody` for 7.0.0, as it is only called with false
|
1253 |
|
pp$1.parseFunction = function(node, statement, allowExpressionBody, isAsync) {
|
1254 |
|
this.initFunction(node);
|
1255 |
|
if (this.options.ecmaVersion >= 9 || this.options.ecmaVersion >= 6 && !isAsync) {
|
1256 |
|
if (this.type === types.star && (statement & FUNC_HANGING_STATEMENT))
|
re #8149 - odstranění šablon 1 část