1 | // This file was generated. Do not modify manually!
|
---|
2 | 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, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 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, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 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, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 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, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
|
---|
3 |
|
---|
4 | // This file was generated. Do not modify manually!
|
---|
5 | 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, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 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, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 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, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 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, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 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, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 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, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 4191];
|
---|
6 |
|
---|
7 | // This file was generated. Do not modify manually!
|
---|
8 | 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\u0898-\u089f\u08ca-\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\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\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\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\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-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\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-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\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\uff65";
|
---|
9 |
|
---|
10 | // This file was generated. Do not modify manually!
|
---|
11 | 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\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\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\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\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-\u1711\u171f-\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-\u1b4c\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-\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-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\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-\uab69\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";
|
---|
12 |
|
---|
13 | // These are a run-length and offset encoded representation of the
|
---|
14 | // >0xffff code points that are a valid part of identifiers. The
|
---|
15 | // offset starts at 0x10000, and each pair of numbers represents an
|
---|
16 | // offset to the next range, and then a size of the range.
|
---|
17 |
|
---|
18 | // Reserved word lists for various dialects of the language
|
---|
19 |
|
---|
20 | var reservedWords = {
|
---|
21 | 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",
|
---|
22 | 5: "class enum extends super const export import",
|
---|
23 | 6: "enum",
|
---|
24 | strict: "implements interface let package private protected public static yield",
|
---|
25 | strictBind: "eval arguments"
|
---|
26 | };
|
---|
27 |
|
---|
28 | // And the keywords
|
---|
29 |
|
---|
30 | 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";
|
---|
31 |
|
---|
32 | var keywords$1 = {
|
---|
33 | 5: ecma5AndLessKeywords,
|
---|
34 | "5module": ecma5AndLessKeywords + " export import",
|
---|
35 | 6: ecma5AndLessKeywords + " const class extends export import super"
|
---|
36 | };
|
---|
37 |
|
---|
38 | var keywordRelationalOperator = /^in(stanceof)?$/;
|
---|
39 |
|
---|
40 | // ## Character categories
|
---|
41 |
|
---|
42 | var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
|
---|
43 | var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
|
---|
44 |
|
---|
45 | // This has a complexity linear to the value of the code. The
|
---|
46 | // assumption is that looking up astral identifier characters is
|
---|
47 | // rare.
|
---|
48 | function isInAstralSet(code, set) {
|
---|
49 | var pos = 0x10000;
|
---|
50 | for (var i = 0; i < set.length; i += 2) {
|
---|
51 | pos += set[i];
|
---|
52 | if (pos > code) { return false }
|
---|
53 | pos += set[i + 1];
|
---|
54 | if (pos >= code) { return true }
|
---|
55 | }
|
---|
56 | return false
|
---|
57 | }
|
---|
58 |
|
---|
59 | // Test whether a given character code starts an identifier.
|
---|
60 |
|
---|
61 | function isIdentifierStart(code, astral) {
|
---|
62 | if (code < 65) { return code === 36 }
|
---|
63 | if (code < 91) { return true }
|
---|
64 | if (code < 97) { return code === 95 }
|
---|
65 | if (code < 123) { return true }
|
---|
66 | if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code)) }
|
---|
67 | if (astral === false) { return false }
|
---|
68 | return isInAstralSet(code, astralIdentifierStartCodes)
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Test whether a given character is part of an identifier.
|
---|
72 |
|
---|
73 | function isIdentifierChar(code, astral) {
|
---|
74 | if (code < 48) { return code === 36 }
|
---|
75 | if (code < 58) { return true }
|
---|
76 | if (code < 65) { return false }
|
---|
77 | if (code < 91) { return true }
|
---|
78 | if (code < 97) { return code === 95 }
|
---|
79 | if (code < 123) { return true }
|
---|
80 | if (code <= 0xffff) { return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code)) }
|
---|
81 | if (astral === false) { return false }
|
---|
82 | return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes)
|
---|
83 | }
|
---|
84 |
|
---|
85 | // ## Token types
|
---|
86 |
|
---|
87 | // The assignment of fine-grained, information-carrying type objects
|
---|
88 | // allows the tokenizer to store the information it has about a
|
---|
89 | // token in a way that is very cheap for the parser to look up.
|
---|
90 |
|
---|
91 | // All token type variables start with an underscore, to make them
|
---|
92 | // easy to recognize.
|
---|
93 |
|
---|
94 | // The `beforeExpr` property is used to disambiguate between regular
|
---|
95 | // expressions and divisions. It is set on all token types that can
|
---|
96 | // be followed by an expression (thus, a slash after them would be a
|
---|
97 | // regular expression).
|
---|
98 | //
|
---|
99 | // The `startsExpr` property is used to check if the token ends a
|
---|
100 | // `yield` expression. It is set on all token types that either can
|
---|
101 | // directly start an expression (like a quotation mark) or can
|
---|
102 | // continue an expression (like the body of a string).
|
---|
103 | //
|
---|
104 | // `isLoop` marks a keyword as starting a loop, which is important
|
---|
105 | // to know when parsing a label, in order to allow or disallow
|
---|
106 | // continue jumps to that label.
|
---|
107 |
|
---|
108 | var TokenType = function TokenType(label, conf) {
|
---|
109 | if ( conf === void 0 ) conf = {};
|
---|
110 |
|
---|
111 | this.label = label;
|
---|
112 | this.keyword = conf.keyword;
|
---|
113 | this.beforeExpr = !!conf.beforeExpr;
|
---|
114 | this.startsExpr = !!conf.startsExpr;
|
---|
115 | this.isLoop = !!conf.isLoop;
|
---|
116 | this.isAssign = !!conf.isAssign;
|
---|
117 | this.prefix = !!conf.prefix;
|
---|
118 | this.postfix = !!conf.postfix;
|
---|
119 | this.binop = conf.binop || null;
|
---|
120 | this.updateContext = null;
|
---|
121 | };
|
---|
122 |
|
---|
123 | function binop(name, prec) {
|
---|
124 | return new TokenType(name, {beforeExpr: true, binop: prec})
|
---|
125 | }
|
---|
126 | var beforeExpr = {beforeExpr: true}, startsExpr = {startsExpr: true};
|
---|
127 |
|
---|
128 | // Map keyword names to token types.
|
---|
129 |
|
---|
130 | var keywords = {};
|
---|
131 |
|
---|
132 | // Succinct definitions of keyword token types
|
---|
133 | function kw(name, options) {
|
---|
134 | if ( options === void 0 ) options = {};
|
---|
135 |
|
---|
136 | options.keyword = name;
|
---|
137 | return keywords[name] = new TokenType(name, options)
|
---|
138 | }
|
---|
139 |
|
---|
140 | var types$1 = {
|
---|
141 | num: new TokenType("num", startsExpr),
|
---|
142 | regexp: new TokenType("regexp", startsExpr),
|
---|
143 | string: new TokenType("string", startsExpr),
|
---|
144 | name: new TokenType("name", startsExpr),
|
---|
145 | privateId: new TokenType("privateId", startsExpr),
|
---|
146 | eof: new TokenType("eof"),
|
---|
147 |
|
---|
148 | // Punctuation token types.
|
---|
149 | bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
|
---|
150 | bracketR: new TokenType("]"),
|
---|
151 | braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
|
---|
152 | braceR: new TokenType("}"),
|
---|
153 | parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
|
---|
154 | parenR: new TokenType(")"),
|
---|
155 | comma: new TokenType(",", beforeExpr),
|
---|
156 | semi: new TokenType(";", beforeExpr),
|
---|
157 | colon: new TokenType(":", beforeExpr),
|
---|
158 | dot: new TokenType("."),
|
---|
159 | question: new TokenType("?", beforeExpr),
|
---|
160 | questionDot: new TokenType("?."),
|
---|
161 | arrow: new TokenType("=>", beforeExpr),
|
---|
162 | template: new TokenType("template"),
|
---|
163 | invalidTemplate: new TokenType("invalidTemplate"),
|
---|
164 | ellipsis: new TokenType("...", beforeExpr),
|
---|
165 | backQuote: new TokenType("`", startsExpr),
|
---|
166 | dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
|
---|
167 |
|
---|
168 | // Operators. These carry several kinds of properties to help the
|
---|
169 | // parser use them properly (the presence of these properties is
|
---|
170 | // what categorizes them as operators).
|
---|
171 | //
|
---|
172 | // `binop`, when present, specifies that this operator is a binary
|
---|
173 | // operator, and will refer to its precedence.
|
---|
174 | //
|
---|
175 | // `prefix` and `postfix` mark the operator as a prefix or postfix
|
---|
176 | // unary operator.
|
---|
177 | //
|
---|
178 | // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
|
---|
179 | // binary operators with a very low precedence, that should result
|
---|
180 | // in AssignmentExpression nodes.
|
---|
181 |
|
---|
182 | eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
|
---|
183 | assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
|
---|
184 | incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
|
---|
185 | prefix: new TokenType("!/~", {beforeExpr: true, prefix: true, startsExpr: true}),
|
---|
186 | logicalOR: binop("||", 1),
|
---|
187 | logicalAND: binop("&&", 2),
|
---|
188 | bitwiseOR: binop("|", 3),
|
---|
189 | bitwiseXOR: binop("^", 4),
|
---|
190 | bitwiseAND: binop("&", 5),
|
---|
191 | equality: binop("==/!=/===/!==", 6),
|
---|
192 | relational: binop("</>/<=/>=", 7),
|
---|
193 | bitShift: binop("<</>>/>>>", 8),
|
---|
194 | plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
|
---|
195 | modulo: binop("%", 10),
|
---|
196 | star: binop("*", 10),
|
---|
197 | slash: binop("/", 10),
|
---|
198 | starstar: new TokenType("**", {beforeExpr: true}),
|
---|
199 | coalesce: binop("??", 1),
|
---|
200 |
|
---|
201 | // Keyword token types.
|
---|
202 | _break: kw("break"),
|
---|
203 | _case: kw("case", beforeExpr),
|
---|
204 | _catch: kw("catch"),
|
---|
205 | _continue: kw("continue"),
|
---|
206 | _debugger: kw("debugger"),
|
---|
207 | _default: kw("default", beforeExpr),
|
---|
208 | _do: kw("do", {isLoop: true, beforeExpr: true}),
|
---|
209 | _else: kw("else", beforeExpr),
|
---|
210 | _finally: kw("finally"),
|
---|
211 | _for: kw("for", {isLoop: true}),
|
---|
212 | _function: kw("function", startsExpr),
|
---|
213 | _if: kw("if"),
|
---|
214 | _return: kw("return", beforeExpr),
|
---|
215 | _switch: kw("switch"),
|
---|
216 | _throw: kw("throw", beforeExpr),
|
---|
217 | _try: kw("try"),
|
---|
218 | _var: kw("var"),
|
---|
219 | _const: kw("const"),
|
---|
220 | _while: kw("while", {isLoop: true}),
|
---|
221 | _with: kw("with"),
|
---|
222 | _new: kw("new", {beforeExpr: true, startsExpr: true}),
|
---|
223 | _this: kw("this", startsExpr),
|
---|
224 | _super: kw("super", startsExpr),
|
---|
225 | _class: kw("class", startsExpr),
|
---|
226 | _extends: kw("extends", beforeExpr),
|
---|
227 | _export: kw("export"),
|
---|
228 | _import: kw("import", startsExpr),
|
---|
229 | _null: kw("null", startsExpr),
|
---|
230 | _true: kw("true", startsExpr),
|
---|
231 | _false: kw("false", startsExpr),
|
---|
232 | _in: kw("in", {beforeExpr: true, binop: 7}),
|
---|
233 | _instanceof: kw("instanceof", {beforeExpr: true, binop: 7}),
|
---|
234 | _typeof: kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true}),
|
---|
235 | _void: kw("void", {beforeExpr: true, prefix: true, startsExpr: true}),
|
---|
236 | _delete: kw("delete", {beforeExpr: true, prefix: true, startsExpr: true})
|
---|
237 | };
|
---|
238 |
|
---|
239 | // Matches a whole line break (where CRLF is considered a single
|
---|
240 | // line break). Used to count lines.
|
---|
241 |
|
---|
242 | var lineBreak = /\r\n?|\n|\u2028|\u2029/;
|
---|
243 | var lineBreakG = new RegExp(lineBreak.source, "g");
|
---|
244 |
|
---|
245 | function isNewLine(code) {
|
---|
246 | return code === 10 || code === 13 || code === 0x2028 || code === 0x2029
|
---|
247 | }
|
---|
248 |
|
---|
249 | function nextLineBreak(code, from, end) {
|
---|
250 | if ( end === void 0 ) end = code.length;
|
---|
251 |
|
---|
252 | for (var i = from; i < end; i++) {
|
---|
253 | var next = code.charCodeAt(i);
|
---|
254 | if (isNewLine(next))
|
---|
255 | { return i < end - 1 && next === 13 && code.charCodeAt(i + 1) === 10 ? i + 2 : i + 1 }
|
---|
256 | }
|
---|
257 | return -1
|
---|
258 | }
|
---|
259 |
|
---|
260 | var nonASCIIwhitespace = /[\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
|
---|
261 |
|
---|
262 | var skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
|
---|
263 |
|
---|
264 | var ref = Object.prototype;
|
---|
265 | var hasOwnProperty = ref.hasOwnProperty;
|
---|
266 | var toString = ref.toString;
|
---|
267 |
|
---|
268 | var hasOwn = Object.hasOwn || (function (obj, propName) { return (
|
---|
269 | hasOwnProperty.call(obj, propName)
|
---|
270 | ); });
|
---|
271 |
|
---|
272 | var isArray = Array.isArray || (function (obj) { return (
|
---|
273 | toString.call(obj) === "[object Array]"
|
---|
274 | ); });
|
---|
275 |
|
---|
276 | var regexpCache = Object.create(null);
|
---|
277 |
|
---|
278 | function wordsRegexp(words) {
|
---|
279 | return regexpCache[words] || (regexpCache[words] = new RegExp("^(?:" + words.replace(/ /g, "|") + ")$"))
|
---|
280 | }
|
---|
281 |
|
---|
282 | function codePointToString(code) {
|
---|
283 | // UTF-16 Decoding
|
---|
284 | if (code <= 0xFFFF) { return String.fromCharCode(code) }
|
---|
285 | code -= 0x10000;
|
---|
286 | return String.fromCharCode((code >> 10) + 0xD800, (code & 1023) + 0xDC00)
|
---|
287 | }
|
---|
288 |
|
---|
289 | var loneSurrogate = /(?:[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/;
|
---|
290 |
|
---|
291 | // These are used when `options.locations` is on, for the
|
---|
292 | // `startLoc` and `endLoc` properties.
|
---|
293 |
|
---|
294 | var Position = function Position(line, col) {
|
---|
295 | this.line = line;
|
---|
296 | this.column = col;
|
---|
297 | };
|
---|
298 |
|
---|
299 | Position.prototype.offset = function offset (n) {
|
---|
300 | return new Position(this.line, this.column + n)
|
---|
301 | };
|
---|
302 |
|
---|
303 | var SourceLocation = function SourceLocation(p, start, end) {
|
---|
304 | this.start = start;
|
---|
305 | this.end = end;
|
---|
306 | if (p.sourceFile !== null) { this.source = p.sourceFile; }
|
---|
307 | };
|
---|
308 |
|
---|
309 | // The `getLineInfo` function is mostly useful when the
|
---|
310 | // `locations` option is off (for performance reasons) and you
|
---|
311 | // want to find the line/column position for a given character
|
---|
312 | // offset. `input` should be the code string that the offset refers
|
---|
313 | // into.
|
---|
314 |
|
---|
315 | function getLineInfo(input, offset) {
|
---|
316 | for (var line = 1, cur = 0;;) {
|
---|
317 | var nextBreak = nextLineBreak(input, cur, offset);
|
---|
318 | if (nextBreak < 0) { return new Position(line, offset - cur) }
|
---|
319 | ++line;
|
---|
320 | cur = nextBreak;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | // A second argument must be given to configure the parser process.
|
---|
325 | // These options are recognized (only `ecmaVersion` is required):
|
---|
326 |
|
---|
327 | var defaultOptions = {
|
---|
328 | // `ecmaVersion` indicates the ECMAScript version to parse. Must be
|
---|
329 | // either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10
|
---|
330 | // (2019), 11 (2020), 12 (2021), 13 (2022), 14 (2023), or `"latest"`
|
---|
331 | // (the latest version the library supports). This influences
|
---|
332 | // support for strict mode, the set of reserved words, and support
|
---|
333 | // for new syntax features.
|
---|
334 | ecmaVersion: null,
|
---|
335 | // `sourceType` indicates the mode the code should be parsed in.
|
---|
336 | // Can be either `"script"` or `"module"`. This influences global
|
---|
337 | // strict mode and parsing of `import` and `export` declarations.
|
---|
338 | sourceType: "script",
|
---|
339 | // `onInsertedSemicolon` can be a callback that will be called when
|
---|
340 | // a semicolon is automatically inserted. It will be passed the
|
---|
341 | // position of the inserted semicolon as an offset, and if
|
---|
342 | // `locations` is enabled, it is given the location as a `{line,
|
---|
343 | // column}` object as second argument.
|
---|
344 | onInsertedSemicolon: null,
|
---|
345 | // `onTrailingComma` is similar to `onInsertedSemicolon`, but for
|
---|
346 | // trailing commas.
|
---|
347 | onTrailingComma: null,
|
---|
348 | // By default, reserved words are only enforced if ecmaVersion >= 5.
|
---|
349 | // Set `allowReserved` to a boolean value to explicitly turn this on
|
---|
350 | // an off. When this option has the value "never", reserved words
|
---|
351 | // and keywords can also not be used as property names.
|
---|
352 | allowReserved: null,
|
---|
353 | // When enabled, a return at the top level is not considered an
|
---|
354 | // error.
|
---|
355 | allowReturnOutsideFunction: false,
|
---|
356 | // When enabled, import/export statements are not constrained to
|
---|
357 | // appearing at the top of the program, and an import.meta expression
|
---|
358 | // in a script isn't considered an error.
|
---|
359 | allowImportExportEverywhere: false,
|
---|
360 | // By default, await identifiers are allowed to appear at the top-level scope only if ecmaVersion >= 2022.
|
---|
361 | // When enabled, await identifiers are allowed to appear at the top-level scope,
|
---|
362 | // but they are still not allowed in non-async functions.
|
---|
363 | allowAwaitOutsideFunction: null,
|
---|
364 | // When enabled, super identifiers are not constrained to
|
---|
365 | // appearing in methods and do not raise an error when they appear elsewhere.
|
---|
366 | allowSuperOutsideMethod: null,
|
---|
367 | // When enabled, hashbang directive in the beginning of file is
|
---|
368 | // allowed and treated as a line comment. Enabled by default when
|
---|
369 | // `ecmaVersion` >= 2023.
|
---|
370 | allowHashBang: false,
|
---|
371 | // By default, the parser will verify that private properties are
|
---|
372 | // only used in places where they are valid and have been declared.
|
---|
373 | // Set this to false to turn such checks off.
|
---|
374 | checkPrivateFields: true,
|
---|
375 | // When `locations` is on, `loc` properties holding objects with
|
---|
376 | // `start` and `end` properties in `{line, column}` form (with
|
---|
377 | // line being 1-based and column 0-based) will be attached to the
|
---|
378 | // nodes.
|
---|
379 | locations: false,
|
---|
380 | // A function can be passed as `onToken` option, which will
|
---|
381 | // cause Acorn to call that function with object in the same
|
---|
382 | // format as tokens returned from `tokenizer().getToken()`. Note
|
---|
383 | // that you are not allowed to call the parser from the
|
---|
384 | // callback—that will corrupt its internal state.
|
---|
385 | onToken: null,
|
---|
386 | // A function can be passed as `onComment` option, which will
|
---|
387 | // cause Acorn to call that function with `(block, text, start,
|
---|
388 | // end)` parameters whenever a comment is skipped. `block` is a
|
---|
389 | // boolean indicating whether this is a block (`/* */`) comment,
|
---|
390 | // `text` is the content of the comment, and `start` and `end` are
|
---|
391 | // character offsets that denote the start and end of the comment.
|
---|
392 | // When the `locations` option is on, two more parameters are
|
---|
393 | // passed, the full `{line, column}` locations of the start and
|
---|
394 | // end of the comments. Note that you are not allowed to call the
|
---|
395 | // parser from the callback—that will corrupt its internal state.
|
---|
396 | // When this option has an array as value, objects representing the
|
---|
397 | // comments are pushed to it.
|
---|
398 | onComment: null,
|
---|
399 | // Nodes have their start and end characters offsets recorded in
|
---|
400 | // `start` and `end` properties (directly on the node, rather than
|
---|
401 | // the `loc` object, which holds line/column data. To also add a
|
---|
402 | // [semi-standardized][range] `range` property holding a `[start,
|
---|
403 | // end]` array with the same numbers, set the `ranges` option to
|
---|
404 | // `true`.
|
---|
405 | //
|
---|
406 | // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
|
---|
407 | ranges: false,
|
---|
408 | // It is possible to parse multiple files into a single AST by
|
---|
409 | // passing the tree produced by parsing the first file as
|
---|
410 | // `program` option in subsequent parses. This will add the
|
---|
411 | // toplevel forms of the parsed file to the `Program` (top) node
|
---|
412 | // of an existing parse tree.
|
---|
413 | program: null,
|
---|
414 | // When `locations` is on, you can pass this to record the source
|
---|
415 | // file in every node's `loc` object.
|
---|
416 | sourceFile: null,
|
---|
417 | // This value, if given, is stored in every node, whether
|
---|
418 | // `locations` is on or off.
|
---|
419 | directSourceFile: null,
|
---|
420 | // When enabled, parenthesized expressions are represented by
|
---|
421 | // (non-standard) ParenthesizedExpression nodes
|
---|
422 | preserveParens: false
|
---|
423 | };
|
---|
424 |
|
---|
425 | // Interpret and default an options object
|
---|
426 |
|
---|
427 | var warnedAboutEcmaVersion = false;
|
---|
428 |
|
---|
429 | function getOptions(opts) {
|
---|
430 | var options = {};
|
---|
431 |
|
---|
432 | for (var opt in defaultOptions)
|
---|
433 | { options[opt] = opts && hasOwn(opts, opt) ? opts[opt] : defaultOptions[opt]; }
|
---|
434 |
|
---|
435 | if (options.ecmaVersion === "latest") {
|
---|
436 | options.ecmaVersion = 1e8;
|
---|
437 | } else if (options.ecmaVersion == null) {
|
---|
438 | if (!warnedAboutEcmaVersion && typeof console === "object" && console.warn) {
|
---|
439 | warnedAboutEcmaVersion = true;
|
---|
440 | console.warn("Since Acorn 8.0.0, options.ecmaVersion is required.\nDefaulting to 2020, but this will stop working in the future.");
|
---|
441 | }
|
---|
442 | options.ecmaVersion = 11;
|
---|
443 | } else if (options.ecmaVersion >= 2015) {
|
---|
444 | options.ecmaVersion -= 2009;
|
---|
445 | }
|
---|
446 |
|
---|
447 | if (options.allowReserved == null)
|
---|
448 | { options.allowReserved = options.ecmaVersion < 5; }
|
---|
449 |
|
---|
450 | if (!opts || opts.allowHashBang == null)
|
---|
451 | { options.allowHashBang = options.ecmaVersion >= 14; }
|
---|
452 |
|
---|
453 | if (isArray(options.onToken)) {
|
---|
454 | var tokens = options.onToken;
|
---|
455 | options.onToken = function (token) { return tokens.push(token); };
|
---|
456 | }
|
---|
457 | if (isArray(options.onComment))
|
---|
458 | { options.onComment = pushComment(options, options.onComment); }
|
---|
459 |
|
---|
460 | return options
|
---|
461 | }
|
---|
462 |
|
---|
463 | function pushComment(options, array) {
|
---|
464 | return function(block, text, start, end, startLoc, endLoc) {
|
---|
465 | var comment = {
|
---|
466 | type: block ? "Block" : "Line",
|
---|
467 | value: text,
|
---|
468 | start: start,
|
---|
469 | end: end
|
---|
470 | };
|
---|
471 | if (options.locations)
|
---|
472 | { comment.loc = new SourceLocation(this, startLoc, endLoc); }
|
---|
473 | if (options.ranges)
|
---|
474 | { comment.range = [start, end]; }
|
---|
475 | array.push(comment);
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | // Each scope gets a bitset that may contain these flags
|
---|
480 | var
|
---|
481 | SCOPE_TOP = 1,
|
---|
482 | SCOPE_FUNCTION = 2,
|
---|
483 | SCOPE_ASYNC = 4,
|
---|
484 | SCOPE_GENERATOR = 8,
|
---|
485 | SCOPE_ARROW = 16,
|
---|
486 | SCOPE_SIMPLE_CATCH = 32,
|
---|
487 | SCOPE_SUPER = 64,
|
---|
488 | SCOPE_DIRECT_SUPER = 128,
|
---|
489 | SCOPE_CLASS_STATIC_BLOCK = 256,
|
---|
490 | SCOPE_VAR = SCOPE_TOP | SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK;
|
---|
491 |
|
---|
492 | function functionFlags(async, generator) {
|
---|
493 | return SCOPE_FUNCTION | (async ? SCOPE_ASYNC : 0) | (generator ? SCOPE_GENERATOR : 0)
|
---|
494 | }
|
---|
495 |
|
---|
496 | // Used in checkLVal* and declareName to determine the type of a binding
|
---|
497 | var
|
---|
498 | BIND_NONE = 0, // Not a binding
|
---|
499 | BIND_VAR = 1, // Var-style binding
|
---|
500 | BIND_LEXICAL = 2, // Let- or const-style binding
|
---|
501 | BIND_FUNCTION = 3, // Function declaration
|
---|
502 | BIND_SIMPLE_CATCH = 4, // Simple (identifier pattern) catch binding
|
---|
503 | BIND_OUTSIDE = 5; // Special case for function names as bound inside the function
|
---|
504 |
|
---|
505 | var Parser = function Parser(options, input, startPos) {
|
---|
506 | this.options = options = getOptions(options);
|
---|
507 | this.sourceFile = options.sourceFile;
|
---|
508 | this.keywords = wordsRegexp(keywords$1[options.ecmaVersion >= 6 ? 6 : options.sourceType === "module" ? "5module" : 5]);
|
---|
509 | var reserved = "";
|
---|
510 | if (options.allowReserved !== true) {
|
---|
511 | reserved = reservedWords[options.ecmaVersion >= 6 ? 6 : options.ecmaVersion === 5 ? 5 : 3];
|
---|
512 | if (options.sourceType === "module") { reserved += " await"; }
|
---|
513 | }
|
---|
514 | this.reservedWords = wordsRegexp(reserved);
|
---|
515 | var reservedStrict = (reserved ? reserved + " " : "") + reservedWords.strict;
|
---|
516 | this.reservedWordsStrict = wordsRegexp(reservedStrict);
|
---|
517 | this.reservedWordsStrictBind = wordsRegexp(reservedStrict + " " + reservedWords.strictBind);
|
---|
518 | this.input = String(input);
|
---|
519 |
|
---|
520 | // Used to signal to callers of `readWord1` whether the word
|
---|
521 | // contained any escape sequences. This is needed because words with
|
---|
522 | // escape sequences must not be interpreted as keywords.
|
---|
523 | this.containsEsc = false;
|
---|
524 |
|
---|
525 | // Set up token state
|
---|
526 |
|
---|
527 | // The current position of the tokenizer in the input.
|
---|
528 | if (startPos) {
|
---|
529 | this.pos = startPos;
|
---|
530 | this.lineStart = this.input.lastIndexOf("\n", startPos - 1) + 1;
|
---|
531 | this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length;
|
---|
532 | } else {
|
---|
533 | this.pos = this.lineStart = 0;
|
---|
534 | this.curLine = 1;
|
---|
535 | }
|
---|
536 |
|
---|
537 | // Properties of the current token:
|
---|
538 | // Its type
|
---|
539 | this.type = types$1.eof;
|
---|
540 | // For tokens that include more information than their type, the value
|
---|
541 | this.value = null;
|
---|
542 | // Its start and end offset
|
---|
543 | this.start = this.end = this.pos;
|
---|
544 | // And, if locations are used, the {line, column} object
|
---|
545 | // corresponding to those offsets
|
---|
546 | this.startLoc = this.endLoc = this.curPosition();
|
---|
547 |
|
---|
548 | // Position information for the previous token
|
---|
549 | this.lastTokEndLoc = this.lastTokStartLoc = null;
|
---|
550 | this.lastTokStart = this.lastTokEnd = this.pos;
|
---|
551 |
|
---|
552 | // The context stack is used to superficially track syntactic
|
---|
553 | // context to predict whether a regular expression is allowed in a
|
---|
554 | // given position.
|
---|
555 | this.context = this.initialContext();
|
---|
556 | this.exprAllowed = true;
|
---|
557 |
|
---|
558 | // Figure out if it's a module code.
|
---|
559 | this.inModule = options.sourceType === "module";
|
---|
560 | this.strict = this.inModule || this.strictDirective(this.pos);
|
---|
561 |
|
---|
562 | // Used to signify the start of a potential arrow function
|
---|
563 | this.potentialArrowAt = -1;
|
---|
564 | this.potentialArrowInForAwait = false;
|
---|
565 |
|
---|
566 | // Positions to delayed-check that yield/await does not exist in default parameters.
|
---|
567 | this.yieldPos = this.awaitPos = this.awaitIdentPos = 0;
|
---|
568 | // Labels in scope.
|
---|
569 | this.labels = [];
|
---|
570 | // Thus-far undefined exports.
|
---|
571 | this.undefinedExports = Object.create(null);
|
---|
572 |
|
---|
573 | // If enabled, skip leading hashbang line.
|
---|
574 | if (this.pos === 0 && options.allowHashBang && this.input.slice(0, 2) === "#!")
|
---|
575 | { this.skipLineComment(2); }
|
---|
576 |
|
---|
577 | // Scope tracking for duplicate variable names (see scope.js)
|
---|
578 | this.scopeStack = [];
|
---|
579 | this.enterScope(SCOPE_TOP);
|
---|
580 |
|
---|
581 | // For RegExp validation
|
---|
582 | this.regexpState = null;
|
---|
583 |
|
---|
584 | // The stack of private names.
|
---|
585 | // Each element has two properties: 'declared' and 'used'.
|
---|
586 | // When it exited from the outermost class definition, all used private names must be declared.
|
---|
587 | this.privateNameStack = [];
|
---|
588 | };
|
---|
589 |
|
---|
590 | var prototypeAccessors = { inFunction: { configurable: true },inGenerator: { configurable: true },inAsync: { configurable: true },canAwait: { configurable: true },allowSuper: { configurable: true },allowDirectSuper: { configurable: true },treatFunctionsAsVar: { configurable: true },allowNewDotTarget: { configurable: true },inClassStaticBlock: { configurable: true } };
|
---|
591 |
|
---|
592 | Parser.prototype.parse = function parse () {
|
---|
593 | var node = this.options.program || this.startNode();
|
---|
594 | this.nextToken();
|
---|
595 | return this.parseTopLevel(node)
|
---|
596 | };
|
---|
597 |
|
---|
598 | prototypeAccessors.inFunction.get = function () { return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0 };
|
---|
599 |
|
---|
600 | prototypeAccessors.inGenerator.get = function () { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0 && !this.currentVarScope().inClassFieldInit };
|
---|
601 |
|
---|
602 | prototypeAccessors.inAsync.get = function () { return (this.currentVarScope().flags & SCOPE_ASYNC) > 0 && !this.currentVarScope().inClassFieldInit };
|
---|
603 |
|
---|
604 | prototypeAccessors.canAwait.get = function () {
|
---|
605 | for (var i = this.scopeStack.length - 1; i >= 0; i--) {
|
---|
606 | var scope = this.scopeStack[i];
|
---|
607 | if (scope.inClassFieldInit || scope.flags & SCOPE_CLASS_STATIC_BLOCK) { return false }
|
---|
608 | if (scope.flags & SCOPE_FUNCTION) { return (scope.flags & SCOPE_ASYNC) > 0 }
|
---|
609 | }
|
---|
610 | return (this.inModule && this.options.ecmaVersion >= 13) || this.options.allowAwaitOutsideFunction
|
---|
611 | };
|
---|
612 |
|
---|
613 | prototypeAccessors.allowSuper.get = function () {
|
---|
614 | var ref = this.currentThisScope();
|
---|
615 | var flags = ref.flags;
|
---|
616 | var inClassFieldInit = ref.inClassFieldInit;
|
---|
617 | return (flags & SCOPE_SUPER) > 0 || inClassFieldInit || this.options.allowSuperOutsideMethod
|
---|
618 | };
|
---|
619 |
|
---|
620 | prototypeAccessors.allowDirectSuper.get = function () { return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0 };
|
---|
621 |
|
---|
622 | prototypeAccessors.treatFunctionsAsVar.get = function () { return this.treatFunctionsAsVarInScope(this.currentScope()) };
|
---|
623 |
|
---|
624 | prototypeAccessors.allowNewDotTarget.get = function () {
|
---|
625 | var ref = this.currentThisScope();
|
---|
626 | var flags = ref.flags;
|
---|
627 | var inClassFieldInit = ref.inClassFieldInit;
|
---|
628 | return (flags & (SCOPE_FUNCTION | SCOPE_CLASS_STATIC_BLOCK)) > 0 || inClassFieldInit
|
---|
629 | };
|
---|
630 |
|
---|
631 | prototypeAccessors.inClassStaticBlock.get = function () {
|
---|
632 | return (this.currentVarScope().flags & SCOPE_CLASS_STATIC_BLOCK) > 0
|
---|
633 | };
|
---|
634 |
|
---|
635 | Parser.extend = function extend () {
|
---|
636 | var plugins = [], len = arguments.length;
|
---|
637 | while ( len-- ) plugins[ len ] = arguments[ len ];
|
---|
638 |
|
---|
639 | var cls = this;
|
---|
640 | for (var i = 0; i < plugins.length; i++) { cls = plugins[i](cls); }
|
---|
641 | return cls
|
---|
642 | };
|
---|
643 |
|
---|
644 | Parser.parse = function parse (input, options) {
|
---|
645 | return new this(options, input).parse()
|
---|
646 | };
|
---|
647 |
|
---|
648 | Parser.parseExpressionAt = function parseExpressionAt (input, pos, options) {
|
---|
649 | var parser = new this(options, input, pos);
|
---|
650 | parser.nextToken();
|
---|
651 | return parser.parseExpression()
|
---|
652 | };
|
---|
653 |
|
---|
654 | Parser.tokenizer = function tokenizer (input, options) {
|
---|
655 | return new this(options, input)
|
---|
656 | };
|
---|
657 |
|
---|
658 | Object.defineProperties( Parser.prototype, prototypeAccessors );
|
---|
659 |
|
---|
660 | var pp$9 = Parser.prototype;
|
---|
661 |
|
---|
662 | // ## Parser utilities
|
---|
663 |
|
---|
664 | var literal = /^(?:'((?:\\[^]|[^'\\])*?)'|"((?:\\[^]|[^"\\])*?)")/;
|
---|
665 | pp$9.strictDirective = function(start) {
|
---|
666 | if (this.options.ecmaVersion < 5) { return false }
|
---|
667 | for (;;) {
|
---|
668 | // Try to find string literal.
|
---|
669 | skipWhiteSpace.lastIndex = start;
|
---|
670 | start += skipWhiteSpace.exec(this.input)[0].length;
|
---|
671 | var match = literal.exec(this.input.slice(start));
|
---|
672 | if (!match) { return false }
|
---|
673 | if ((match[1] || match[2]) === "use strict") {
|
---|
674 | skipWhiteSpace.lastIndex = start + match[0].length;
|
---|
675 | var spaceAfter = skipWhiteSpace.exec(this.input), end = spaceAfter.index + spaceAfter[0].length;
|
---|
676 | var next = this.input.charAt(end);
|
---|
677 | return next === ";" || next === "}" ||
|
---|
678 | (lineBreak.test(spaceAfter[0]) &&
|
---|
679 | !(/[(`.[+\-/*%<>=,?^&]/.test(next) || next === "!" && this.input.charAt(end + 1) === "="))
|
---|
680 | }
|
---|
681 | start += match[0].length;
|
---|
682 |
|
---|
683 | // Skip semicolon, if any.
|
---|
684 | skipWhiteSpace.lastIndex = start;
|
---|
685 | start += skipWhiteSpace.exec(this.input)[0].length;
|
---|
686 | if (this.input[start] === ";")
|
---|
687 | { start++; }
|
---|
688 | }
|
---|
689 | };
|
---|
690 |
|
---|
691 | // Predicate that tests whether the next token is of the given
|
---|
692 | // type, and if yes, consumes it as a side effect.
|
---|
693 |
|
---|
694 | pp$9.eat = function(type) {
|
---|
695 | if (this.type === type) {
|
---|
696 | this.next();
|
---|
697 | return true
|
---|
698 | } else {
|
---|
699 | return false
|
---|
700 | }
|
---|
701 | };
|
---|
702 |
|
---|
703 | // Tests whether parsed token is a contextual keyword.
|
---|
704 |
|
---|
705 | pp$9.isContextual = function(name) {
|
---|
706 | return this.type === types$1.name && this.value === name && !this.containsEsc
|
---|
707 | };
|
---|
708 |
|
---|
709 | // Consumes contextual keyword if possible.
|
---|
710 |
|
---|
711 | pp$9.eatContextual = function(name) {
|
---|
712 | if (!this.isContextual(name)) { return false }
|
---|
713 | this.next();
|
---|
714 | return true
|
---|
715 | };
|
---|
716 |
|
---|
717 | // Asserts that following token is given contextual keyword.
|
---|
718 |
|
---|
719 | pp$9.expectContextual = function(name) {
|
---|
720 | if (!this.eatContextual(name)) { this.unexpected(); }
|
---|
721 | };
|
---|
722 |
|
---|
723 | // Test whether a semicolon can be inserted at the current position.
|
---|
724 |
|
---|
725 | pp$9.canInsertSemicolon = function() {
|
---|
726 | return this.type === types$1.eof ||
|
---|
727 | this.type === types$1.braceR ||
|
---|
728 | lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
|
---|
729 | };
|
---|
730 |
|
---|
731 | pp$9.insertSemicolon = function() {
|
---|
732 | if (this.canInsertSemicolon()) {
|
---|
733 | if (this.options.onInsertedSemicolon)
|
---|
734 | { this.options.onInsertedSemicolon(this.lastTokEnd, this.lastTokEndLoc); }
|
---|
735 | return true
|
---|
736 | }
|
---|
737 | };
|
---|
738 |
|
---|
739 | // Consume a semicolon, or, failing that, see if we are allowed to
|
---|
740 | // pretend that there is a semicolon at this position.
|
---|
741 |
|
---|
742 | pp$9.semicolon = function() {
|
---|
743 | if (!this.eat(types$1.semi) && !this.insertSemicolon()) { this.unexpected(); }
|
---|
744 | };
|
---|
745 |
|
---|
746 | pp$9.afterTrailingComma = function(tokType, notNext) {
|
---|
747 | if (this.type === tokType) {
|
---|
748 | if (this.options.onTrailingComma)
|
---|
749 | { this.options.onTrailingComma(this.lastTokStart, this.lastTokStartLoc); }
|
---|
750 | if (!notNext)
|
---|
751 | { this.next(); }
|
---|
752 | return true
|
---|
753 | }
|
---|
754 | };
|
---|
755 |
|
---|
756 | // Expect a token of a given type. If found, consume it, otherwise,
|
---|
757 | // raise an unexpected token error.
|
---|
758 |
|
---|
759 | pp$9.expect = function(type) {
|
---|
760 | this.eat(type) || this.unexpected();
|
---|
761 | };
|
---|
762 |
|
---|
763 | // Raise an unexpected token error.
|
---|
764 |
|
---|
765 | pp$9.unexpected = function(pos) {
|
---|
766 | this.raise(pos != null ? pos : this.start, "Unexpected token");
|
---|
767 | };
|
---|
768 |
|
---|
769 | var DestructuringErrors = function DestructuringErrors() {
|
---|
770 | this.shorthandAssign =
|
---|
771 | this.trailingComma =
|
---|
772 | this.parenthesizedAssign =
|
---|
773 | this.parenthesizedBind =
|
---|
774 | this.doubleProto =
|
---|
775 | -1;
|
---|
776 | };
|
---|
777 |
|
---|
778 | pp$9.checkPatternErrors = function(refDestructuringErrors, isAssign) {
|
---|
779 | if (!refDestructuringErrors) { return }
|
---|
780 | if (refDestructuringErrors.trailingComma > -1)
|
---|
781 | { this.raiseRecoverable(refDestructuringErrors.trailingComma, "Comma is not permitted after the rest element"); }
|
---|
782 | var parens = isAssign ? refDestructuringErrors.parenthesizedAssign : refDestructuringErrors.parenthesizedBind;
|
---|
783 | if (parens > -1) { this.raiseRecoverable(parens, isAssign ? "Assigning to rvalue" : "Parenthesized pattern"); }
|
---|
784 | };
|
---|
785 |
|
---|
786 | pp$9.checkExpressionErrors = function(refDestructuringErrors, andThrow) {
|
---|
787 | if (!refDestructuringErrors) { return false }
|
---|
788 | var shorthandAssign = refDestructuringErrors.shorthandAssign;
|
---|
789 | var doubleProto = refDestructuringErrors.doubleProto;
|
---|
790 | if (!andThrow) { return shorthandAssign >= 0 || doubleProto >= 0 }
|
---|
791 | if (shorthandAssign >= 0)
|
---|
792 | { this.raise(shorthandAssign, "Shorthand property assignments are valid only in destructuring patterns"); }
|
---|
793 | if (doubleProto >= 0)
|
---|
794 | { this.raiseRecoverable(doubleProto, "Redefinition of __proto__ property"); }
|
---|
795 | };
|
---|
796 |
|
---|
797 | pp$9.checkYieldAwaitInDefaultParams = function() {
|
---|
798 | if (this.yieldPos && (!this.awaitPos || this.yieldPos < this.awaitPos))
|
---|
799 | { this.raise(this.yieldPos, "Yield expression cannot be a default value"); }
|
---|
800 | if (this.awaitPos)
|
---|
801 | { this.raise(this.awaitPos, "Await expression cannot be a default value"); }
|
---|
802 | };
|
---|
803 |
|
---|
804 | pp$9.isSimpleAssignTarget = function(expr) {
|
---|
805 | if (expr.type === "ParenthesizedExpression")
|
---|
806 | { return this.isSimpleAssignTarget(expr.expression) }
|
---|
807 | return expr.type === "Identifier" || expr.type === "MemberExpression"
|
---|
808 | };
|
---|
809 |
|
---|
810 | var pp$8 = Parser.prototype;
|
---|
811 |
|
---|
812 | // ### Statement parsing
|
---|
813 |
|
---|
814 | // Parse a program. Initializes the parser, reads any number of
|
---|
815 | // statements, and wraps them in a Program node. Optionally takes a
|
---|
816 | // `program` argument. If present, the statements will be appended
|
---|
817 | // to its body instead of creating a new node.
|
---|
818 |
|
---|
819 | pp$8.parseTopLevel = function(node) {
|
---|
820 | var exports = Object.create(null);
|
---|
821 | if (!node.body) { node.body = []; }
|
---|
822 | while (this.type !== types$1.eof) {
|
---|
823 | var stmt = this.parseStatement(null, true, exports);
|
---|
824 | node.body.push(stmt);
|
---|
825 | }
|
---|
826 | if (this.inModule)
|
---|
827 | { for (var i = 0, list = Object.keys(this.undefinedExports); i < list.length; i += 1)
|
---|
828 | {
|
---|
829 | var name = list[i];
|
---|
830 |
|
---|
831 | this.raiseRecoverable(this.undefinedExports[name].start, ("Export '" + name + "' is not defined"));
|
---|
832 | } }
|
---|
833 | this.adaptDirectivePrologue(node.body);
|
---|
834 | this.next();
|
---|
835 | node.sourceType = this.options.sourceType;
|
---|
836 | return this.finishNode(node, "Program")
|
---|
837 | };
|
---|
838 |
|
---|
839 | var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
|
---|
840 |
|
---|
841 | pp$8.isLet = function(context) {
|
---|
842 | if (this.options.ecmaVersion < 6 || !this.isContextual("let")) { return false }
|
---|
843 | skipWhiteSpace.lastIndex = this.pos;
|
---|
844 | var skip = skipWhiteSpace.exec(this.input);
|
---|
845 | var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
|
---|
846 | // For ambiguous cases, determine if a LexicalDeclaration (or only a
|
---|
847 | // Statement) is allowed here. If context is not empty then only a Statement
|
---|
848 | // is allowed. However, `let [` is an explicit negative lookahead for
|
---|
849 | // ExpressionStatement, so special-case it first.
|
---|
850 | if (nextCh === 91 || nextCh === 92) { return true } // '[', '\'
|
---|
851 | if (context) { return false }
|
---|
852 |
|
---|
853 | if (nextCh === 123 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true } // '{', astral
|
---|
854 | if (isIdentifierStart(nextCh, true)) {
|
---|
855 | var pos = next + 1;
|
---|
856 | while (isIdentifierChar(nextCh = this.input.charCodeAt(pos), true)) { ++pos; }
|
---|
857 | if (nextCh === 92 || nextCh > 0xd7ff && nextCh < 0xdc00) { return true }
|
---|
858 | var ident = this.input.slice(next, pos);
|
---|
859 | if (!keywordRelationalOperator.test(ident)) { return true }
|
---|
860 | }
|
---|
861 | return false
|
---|
862 | };
|
---|
863 |
|
---|
864 | // check 'async [no LineTerminator here] function'
|
---|
865 | // - 'async /*foo*/ function' is OK.
|
---|
866 | // - 'async /*\n*/ function' is invalid.
|
---|
867 | pp$8.isAsyncFunction = function() {
|
---|
868 | if (this.options.ecmaVersion < 8 || !this.isContextual("async"))
|
---|
869 | { return false }
|
---|
870 |
|
---|
871 | skipWhiteSpace.lastIndex = this.pos;
|
---|
872 | var skip = skipWhiteSpace.exec(this.input);
|
---|
873 | var next = this.pos + skip[0].length, after;
|
---|
874 | return !lineBreak.test(this.input.slice(this.pos, next)) &&
|
---|
875 | this.input.slice(next, next + 8) === "function" &&
|
---|
876 | (next + 8 === this.input.length ||
|
---|
877 | !(isIdentifierChar(after = this.input.charCodeAt(next + 8)) || after > 0xd7ff && after < 0xdc00))
|
---|
878 | };
|
---|
879 |
|
---|
880 | // Parse a single statement.
|
---|
881 | //
|
---|
882 | // If expecting a statement and finding a slash operator, parse a
|
---|
883 | // regular expression literal. This is to handle cases like
|
---|
884 | // `if (foo) /blah/.exec(foo)`, where looking at the previous token
|
---|
885 | // does not help.
|
---|
886 |
|
---|
887 | pp$8.parseStatement = function(context, topLevel, exports) {
|
---|
888 | var starttype = this.type, node = this.startNode(), kind;
|
---|
889 |
|
---|
890 | if (this.isLet(context)) {
|
---|
891 | starttype = types$1._var;
|
---|
892 | kind = "let";
|
---|
893 | }
|
---|
894 |
|
---|
895 | // Most types of statements are recognized by the keyword they
|
---|
896 | // start with. Many are trivial to parse, some require a bit of
|
---|
897 | // complexity.
|
---|
898 |
|
---|
899 | switch (starttype) {
|
---|
900 | case types$1._break: case types$1._continue: return this.parseBreakContinueStatement(node, starttype.keyword)
|
---|
901 | case types$1._debugger: return this.parseDebuggerStatement(node)
|
---|
902 | case types$1._do: return this.parseDoStatement(node)
|
---|
903 | case types$1._for: return this.parseForStatement(node)
|
---|
904 | case types$1._function:
|
---|
905 | // Function as sole body of either an if statement or a labeled statement
|
---|
906 | // works, but not when it is part of a labeled statement that is the sole
|
---|
907 | // body of an if statement.
|
---|
908 | if ((context && (this.strict || context !== "if" && context !== "label")) && this.options.ecmaVersion >= 6) { this.unexpected(); }
|
---|
909 | return this.parseFunctionStatement(node, false, !context)
|
---|
910 | case types$1._class:
|
---|
911 | if (context) { this.unexpected(); }
|
---|
912 | return this.parseClass(node, true)
|
---|
913 | case types$1._if: return this.parseIfStatement(node)
|
---|
914 | case types$1._return: return this.parseReturnStatement(node)
|
---|
915 | case types$1._switch: return this.parseSwitchStatement(node)
|
---|
916 | case types$1._throw: return this.parseThrowStatement(node)
|
---|
917 | case types$1._try: return this.parseTryStatement(node)
|
---|
918 | case types$1._const: case types$1._var:
|
---|
919 | kind = kind || this.value;
|
---|
920 | if (context && kind !== "var") { this.unexpected(); }
|
---|
921 | return this.parseVarStatement(node, kind)
|
---|
922 | case types$1._while: return this.parseWhileStatement(node)
|
---|
923 | case types$1._with: return this.parseWithStatement(node)
|
---|
924 | case types$1.braceL: return this.parseBlock(true, node)
|
---|
925 | case types$1.semi: return this.parseEmptyStatement(node)
|
---|
926 | case types$1._export:
|
---|
927 | case types$1._import:
|
---|
928 | if (this.options.ecmaVersion > 10 && starttype === types$1._import) {
|
---|
929 | skipWhiteSpace.lastIndex = this.pos;
|
---|
930 | var skip = skipWhiteSpace.exec(this.input);
|
---|
931 | var next = this.pos + skip[0].length, nextCh = this.input.charCodeAt(next);
|
---|
932 | if (nextCh === 40 || nextCh === 46) // '(' or '.'
|
---|
933 | { return this.parseExpressionStatement(node, this.parseExpression()) }
|
---|
934 | }
|
---|
935 |
|
---|
936 | if (!this.options.allowImportExportEverywhere) {
|
---|
937 | if (!topLevel)
|
---|
938 | { this.raise(this.start, "'import' and 'export' may only appear at the top level"); }
|
---|
939 | if (!this.inModule)
|
---|
940 | { this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'"); }
|
---|
941 | }
|
---|
942 | return starttype === types$1._import ? this.parseImport(node) : this.parseExport(node, exports)
|
---|
943 |
|
---|
944 | // If the statement does not start with a statement keyword or a
|
---|
945 | // brace, it's an ExpressionStatement or LabeledStatement. We
|
---|
946 | // simply start parsing an expression, and afterwards, if the
|
---|
947 | // next token is a colon and the expression was a simple
|
---|
948 | // Identifier node, we switch to interpreting it as a label.
|
---|
949 | default:
|
---|
950 | if (this.isAsyncFunction()) {
|
---|
951 | if (context) { this.unexpected(); }
|
---|
952 | this.next();
|
---|
953 | return this.parseFunctionStatement(node, true, !context)
|
---|
954 | }
|
---|
955 |
|
---|
956 | var maybeName = this.value, expr = this.parseExpression();
|
---|
957 | if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon))
|
---|
958 | { return this.parseLabeledStatement(node, maybeName, expr, context) }
|
---|
959 | else { return this.parseExpressionStatement(node, expr) }
|
---|
960 | }
|
---|
961 | };
|
---|
962 |
|
---|
963 | pp$8.parseBreakContinueStatement = function(node, keyword) {
|
---|
964 | var isBreak = keyword === "break";
|
---|
965 | this.next();
|
---|
966 | if (this.eat(types$1.semi) || this.insertSemicolon()) { node.label = null; }
|
---|
967 | else if (this.type !== types$1.name) { this.unexpected(); }
|
---|
968 | else {
|
---|
969 | node.label = this.parseIdent();
|
---|
970 | this.semicolon();
|
---|
971 | }
|
---|
972 |
|
---|
973 | // Verify that there is an actual destination to break or
|
---|
974 | // continue to.
|
---|
975 | var i = 0;
|
---|
976 | for (; i < this.labels.length; ++i) {
|
---|
977 | var lab = this.labels[i];
|
---|
978 | if (node.label == null || lab.name === node.label.name) {
|
---|
979 | if (lab.kind != null && (isBreak || lab.kind === "loop")) { break }
|
---|
980 | if (node.label && isBreak) { break }
|
---|
981 | }
|
---|
982 | }
|
---|
983 | if (i === this.labels.length) { this.raise(node.start, "Unsyntactic " + keyword); }
|
---|
984 | return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
|
---|
985 | };
|
---|
986 |
|
---|
987 | pp$8.parseDebuggerStatement = function(node) {
|
---|
988 | this.next();
|
---|
989 | this.semicolon();
|
---|
990 | return this.finishNode(node, "DebuggerStatement")
|
---|
991 | };
|
---|
992 |
|
---|
993 | pp$8.parseDoStatement = function(node) {
|
---|
994 | this.next();
|
---|
995 | this.labels.push(loopLabel);
|
---|
996 | node.body = this.parseStatement("do");
|
---|
997 | this.labels.pop();
|
---|
998 | this.expect(types$1._while);
|
---|
999 | node.test = this.parseParenExpression();
|
---|
1000 | if (this.options.ecmaVersion >= 6)
|
---|
1001 | { this.eat(types$1.semi); }
|
---|
1002 | else
|
---|
1003 | { this.semicolon(); }
|
---|
1004 | return this.finishNode(node, "DoWhileStatement")
|
---|
1005 | };
|
---|
1006 |
|
---|
1007 | // Disambiguating between a `for` and a `for`/`in` or `for`/`of`
|
---|
1008 | // loop is non-trivial. Basically, we have to parse the init `var`
|
---|
1009 | // statement or expression, disallowing the `in` operator (see
|
---|
1010 | // the second parameter to `parseExpression`), and then check
|
---|
1011 | // whether the next token is `in` or `of`. When there is no init
|
---|
1012 | // part (semicolon immediately after the opening parenthesis), it
|
---|
1013 | // is a regular `for` loop.
|
---|
1014 |
|
---|
1015 | pp$8.parseForStatement = function(node) {
|
---|
1016 | this.next();
|
---|
1017 | var awaitAt = (this.options.ecmaVersion >= 9 && this.canAwait && this.eatContextual("await")) ? this.lastTokStart : -1;
|
---|
1018 | this.labels.push(loopLabel);
|
---|
1019 | this.enterScope(0);
|
---|
1020 | this.expect(types$1.parenL);
|
---|
1021 | if (this.type === types$1.semi) {
|
---|
1022 | if (awaitAt > -1) { this.unexpected(awaitAt); }
|
---|
1023 | return this.parseFor(node, null)
|
---|
1024 | }
|
---|
1025 | var isLet = this.isLet();
|
---|
1026 | if (this.type === types$1._var || this.type === types$1._const || isLet) {
|
---|
1027 | var init$1 = this.startNode(), kind = isLet ? "let" : this.value;
|
---|
1028 | this.next();
|
---|
1029 | this.parseVar(init$1, true, kind);
|
---|
1030 | this.finishNode(init$1, "VariableDeclaration");
|
---|
1031 | if ((this.type === types$1._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) && init$1.declarations.length === 1) {
|
---|
1032 | if (this.options.ecmaVersion >= 9) {
|
---|
1033 | if (this.type === types$1._in) {
|
---|
1034 | if (awaitAt > -1) { this.unexpected(awaitAt); }
|
---|
1035 | } else { node.await = awaitAt > -1; }
|
---|
1036 | }
|
---|
1037 | return this.parseForIn(node, init$1)
|
---|
1038 | }
|
---|
1039 | if (awaitAt > -1) { this.unexpected(awaitAt); }
|
---|
1040 | return this.parseFor(node, init$1)
|
---|
1041 | }
|
---|
1042 | var startsWithLet = this.isContextual("let"), isForOf = false;
|
---|
1043 | var containsEsc = this.containsEsc;
|
---|
1044 | var refDestructuringErrors = new DestructuringErrors;
|
---|
1045 | var initPos = this.start;
|
---|
1046 | var init = awaitAt > -1
|
---|
1047 | ? this.parseExprSubscripts(refDestructuringErrors, "await")
|
---|
1048 | : this.parseExpression(true, refDestructuringErrors);
|
---|
1049 | if (this.type === types$1._in || (isForOf = this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
|
---|
1050 | if (awaitAt > -1) { // implies `ecmaVersion >= 9` (see declaration of awaitAt)
|
---|
1051 | if (this.type === types$1._in) { this.unexpected(awaitAt); }
|
---|
1052 | node.await = true;
|
---|
1053 | } else if (isForOf && this.options.ecmaVersion >= 8) {
|
---|
1054 | if (init.start === initPos && !containsEsc && init.type === "Identifier" && init.name === "async") { this.unexpected(); }
|
---|
1055 | else if (this.options.ecmaVersion >= 9) { node.await = false; }
|
---|
1056 | }
|
---|
1057 | if (startsWithLet && isForOf) { this.raise(init.start, "The left-hand side of a for-of loop may not start with 'let'."); }
|
---|
1058 | this.toAssignable(init, false, refDestructuringErrors);
|
---|
1059 | this.checkLValPattern(init);
|
---|
1060 | return this.parseForIn(node, init)
|
---|
1061 | } else {
|
---|
1062 | this.checkExpressionErrors(refDestructuringErrors, true);
|
---|
1063 | }
|
---|
1064 | if (awaitAt > -1) { this.unexpected(awaitAt); }
|
---|
1065 | return this.parseFor(node, init)
|
---|
1066 | };
|
---|
1067 |
|
---|
1068 | pp$8.parseFunctionStatement = function(node, isAsync, declarationPosition) {
|
---|
1069 | this.next();
|
---|
1070 | return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), false, isAsync)
|
---|
1071 | };
|
---|
1072 |
|
---|
1073 | pp$8.parseIfStatement = function(node) {
|
---|
1074 | this.next();
|
---|
1075 | node.test = this.parseParenExpression();
|
---|
1076 | // allow function declarations in branches, but only in non-strict mode
|
---|
1077 | node.consequent = this.parseStatement("if");
|
---|
1078 | node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
|
---|
1079 | return this.finishNode(node, "IfStatement")
|
---|
1080 | };
|
---|
1081 |
|
---|
1082 | pp$8.parseReturnStatement = function(node) {
|
---|
1083 | if (!this.inFunction && !this.options.allowReturnOutsideFunction)
|
---|
1084 | { this.raise(this.start, "'return' outside of function"); }
|
---|
1085 | this.next();
|
---|
1086 |
|
---|
1087 | // In `return` (and `break`/`continue`), the keywords with
|
---|
1088 | // optional arguments, we eagerly look for a semicolon or the
|
---|
1089 | // possibility to insert one.
|
---|
1090 |
|
---|
1091 | if (this.eat(types$1.semi) || this.insertSemicolon()) { node.argument = null; }
|
---|
1092 | else { node.argument = this.parseExpression(); this.semicolon(); }
|
---|
1093 | return this.finishNode(node, "ReturnStatement")
|
---|
1094 | };
|
---|
1095 |
|
---|
1096 | pp$8.parseSwitchStatement = function(node) {
|
---|
1097 | this.next();
|
---|
1098 | node.discriminant = this.parseParenExpression();
|
---|
1099 | node.cases = [];
|
---|
1100 | this.expect(types$1.braceL);
|
---|
1101 | this.labels.push(switchLabel);
|
---|
1102 | this.enterScope(0);
|
---|
1103 |
|
---|
1104 | // Statements under must be grouped (by label) in SwitchCase
|
---|
1105 | // nodes. `cur` is used to keep the node that we are currently
|
---|
1106 | // adding statements to.
|
---|
1107 |
|
---|
1108 | var cur;
|
---|
1109 | for (var sawDefault = false; this.type !== types$1.braceR;) {
|
---|
1110 | if (this.type === types$1._case || this.type === types$1._default) {
|
---|
1111 | var isCase = this.type === types$1._case;
|
---|
1112 | if (cur) { this.finishNode(cur, "SwitchCase"); }
|
---|
1113 | node.cases.push(cur = this.startNode());
|
---|
1114 | cur.consequent = [];
|
---|
1115 | this.next();
|
---|
1116 | if (isCase) {
|
---|
1117 | cur.test = this.parseExpression();
|
---|
1118 | } else {
|
---|
1119 | if (sawDefault) { this.raiseRecoverable(this.lastTokStart, "Multiple default clauses"); }
|
---|
1120 | sawDefault = true;
|
---|
1121 | cur.test = null;
|
---|
1122 | }
|
---|
1123 | this.expect(types$1.colon);
|
---|
1124 | } else {
|
---|
1125 | if (!cur) { this.unexpected(); }
|
---|
1126 | cur.consequent.push(this.parseStatement(null));
|
---|
1127 | }
|
---|
1128 | }
|
---|
1129 | this.exitScope();
|
---|
1130 | if (cur) { this.finishNode(cur, "SwitchCase"); }
|
---|
1131 | this.next(); // Closing brace
|
---|
1132 | this.labels.pop();
|
---|
1133 | return this.finishNode(node, "SwitchStatement")
|
---|
1134 | };
|
---|
1135 |
|
---|
1136 | pp$8.parseThrowStatement = function(node) {
|
---|
1137 | this.next();
|
---|
1138 | if (lineBreak.test(this.input.slice(this.lastTokEnd, this.start)))
|
---|
1139 | { this.raise(this.lastTokEnd, "Illegal newline after throw"); }
|
---|
1140 | node.argument = this.parseExpression();
|
---|
1141 | this.semicolon();
|
---|
1142 | return this.finishNode(node, "ThrowStatement")
|
---|
1143 | };
|
---|
1144 |
|
---|
1145 | // Reused empty array added for node fields that are always empty.
|
---|
1146 |
|
---|
1147 | var empty$1 = [];
|
---|
1148 |
|
---|
1149 | pp$8.parseCatchClauseParam = function() {
|
---|
1150 | var param = this.parseBindingAtom();
|
---|
1151 | var simple = param.type === "Identifier";
|
---|
1152 | this.enterScope(simple ? SCOPE_SIMPLE_CATCH : 0);
|
---|
1153 | this.checkLValPattern(param, simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL);
|
---|
1154 | this.expect(types$1.parenR);
|
---|
1155 |
|
---|
1156 | return param
|
---|
1157 | };
|
---|
1158 |
|
---|
1159 | pp$8.parseTryStatement = function(node) {
|
---|
1160 | this.next();
|
---|
1161 | node.block = this.parseBlock();
|
---|
1162 | node.handler = null;
|
---|
1163 | if (this.type === types$1._catch) {
|
---|
1164 | var clause = this.startNode();
|
---|
1165 | this.next();
|
---|
1166 | if (this.eat(types$1.parenL)) {
|
---|
1167 | clause.param = this.parseCatchClauseParam();
|
---|
1168 | } else {
|
---|
1169 | if (this.options.ecmaVersion < 10) { this.unexpected(); }
|
---|
1170 | clause.param = null;
|
---|
1171 | this.enterScope(0);
|
---|
1172 | }
|
---|
1173 | clause.body = this.parseBlock(false);
|
---|
1174 | this.exitScope();
|
---|
1175 | node.handler = this.finishNode(clause, "CatchClause");
|
---|
1176 | }
|
---|
1177 | node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
|
---|
1178 | if (!node.handler && !node.finalizer)
|
---|
1179 | { this.raise(node.start, "Missing catch or finally clause"); }
|
---|
1180 | return this.finishNode(node, "TryStatement")
|
---|
1181 | };
|
---|
1182 |
|
---|
1183 | pp$8.parseVarStatement = function(node, kind, allowMissingInitializer) {
|
---|
1184 | this.next();
|
---|
1185 | this.parseVar(node, false, kind, allowMissingInitializer);
|
---|
1186 | this.semicolon();
|
---|
1187 | return this.finishNode(node, "VariableDeclaration")
|
---|
1188 | };
|
---|
1189 |
|
---|
1190 | pp$8.parseWhileStatement = function(node) {
|
---|
1191 | this.next();
|
---|
1192 | node.test = this.parseParenExpression();
|
---|
1193 | this.labels.push(loopLabel);
|
---|
1194 | node.body = this.parseStatement("while");
|
---|
1195 | this.labels.pop();
|
---|
1196 | return this.finishNode(node, "WhileStatement")
|
---|
1197 | };
|
---|
1198 |
|
---|
1199 | pp$8.parseWithStatement = function(node) {
|
---|
1200 | if (this.strict) { this.raise(this.start, "'with' in strict mode"); }
|
---|
1201 | this.next();
|
---|
1202 | node.object = this.parseParenExpression();
|
---|
1203 | node.body = this.parseStatement("with");
|
---|
1204 | return this.finishNode(node, "WithStatement")
|
---|
1205 | };
|
---|
1206 |
|
---|
1207 | pp$8.parseEmptyStatement = function(node) {
|
---|
1208 | this.next();
|
---|
1209 | return this.finishNode(node, "EmptyStatement")
|
---|
1210 | };
|
---|
1211 |
|
---|
1212 | pp$8.parseLabeledStatement = function(node, maybeName, expr, context) {
|
---|
1213 | for (var i$1 = 0, list = this.labels; i$1 < list.length; i$1 += 1)
|
---|
1214 | {
|
---|
1215 | var label = list[i$1];
|
---|
1216 |
|
---|
1217 | if (label.name === maybeName)
|
---|
1218 | { this.raise(expr.start, "Label '" + maybeName + "' is already declared");
|
---|
1219 | } }
|
---|
1220 | var kind = this.type.isLoop ? "loop" : this.type === types$1._switch ? "switch" : null;
|
---|
1221 | for (var i = this.labels.length - 1; i >= 0; i--) {
|
---|
1222 | var label$1 = this.labels[i];
|
---|
1223 | if (label$1.statementStart === node.start) {
|
---|
1224 | // Update information about previous labels on this node
|
---|
1225 | label$1.statementStart = this.start;
|
---|
1226 | label$1.kind = kind;
|
---|
1227 | } else { break }
|
---|
1228 | }
|
---|
1229 | this.labels.push({name: maybeName, kind: kind, statementStart: this.start});
|
---|
1230 | node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
|
---|
1231 | this.labels.pop();
|
---|
1232 | node.label = expr;
|
---|
1233 | return this.finishNode(node, "LabeledStatement")
|
---|
1234 | };
|
---|
1235 |
|
---|
1236 | pp$8.parseExpressionStatement = function(node, expr) {
|
---|
1237 | node.expression = expr;
|
---|
1238 | this.semicolon();
|
---|
1239 | return this.finishNode(node, "ExpressionStatement")
|
---|
1240 | };
|
---|
1241 |
|
---|
1242 | // Parse a semicolon-enclosed block of statements, handling `"use
|
---|
1243 | // strict"` declarations when `allowStrict` is true (used for
|
---|
1244 | // function bodies).
|
---|
1245 |
|
---|
1246 | pp$8.parseBlock = function(createNewLexicalScope, node, exitStrict) {
|
---|
1247 | if ( createNewLexicalScope === void 0 ) createNewLexicalScope = true;
|
---|
1248 | if ( node === void 0 ) node = this.startNode();
|
---|
1249 |
|
---|
1250 | node.body = [];
|
---|
1251 | this.expect(types$1.braceL);
|
---|
1252 | if (createNewLexicalScope) { this.enterScope(0); }
|
---|
1253 | while (this.type !== types$1.braceR) {
|
---|
1254 | var stmt = this.parseStatement(null);
|
---|
1255 | node.body.push(stmt);
|
---|
1256 | }
|
---|
1257 | if (exitStrict) { this.strict = false; }
|
---|
1258 | this.next();
|
---|
1259 | if (createNewLexicalScope) { this.exitScope(); }
|
---|
1260 | return this.finishNode(node, "BlockStatement")
|
---|
1261 | };
|
---|
1262 |
|
---|
1263 | // Parse a regular `for` loop. The disambiguation code in
|
---|
1264 | // `parseStatement` will already have parsed the init statement or
|
---|
1265 | // expression.
|
---|
1266 |
|
---|
1267 | pp$8.parseFor = function(node, init) {
|
---|
1268 | node.init = init;
|
---|
1269 | this.expect(types$1.semi);
|
---|
1270 | node.test = this.type === types$1.semi ? null : this.parseExpression();
|
---|
1271 | this.expect(types$1.semi);
|
---|
1272 | node.update = this.type === types$1.parenR ? null : this.parseExpression();
|
---|
1273 | this.expect(types$1.parenR);
|
---|
1274 | node.body = this.parseStatement("for");
|
---|
1275 | this.exitScope();
|
---|
1276 | this.labels.pop();
|
---|
1277 | return this.finishNode(node, "ForStatement")
|
---|
1278 | };
|
---|
1279 |
|
---|
1280 | // Parse a `for`/`in` and `for`/`of` loop, which are almost
|
---|
1281 | // same from parser's perspective.
|
---|
1282 |
|
---|
1283 | pp$8.parseForIn = function(node, init) {
|
---|
1284 | var isForIn = this.type === types$1._in;
|
---|
1285 | this.next();
|
---|
1286 |
|
---|
1287 | if (
|
---|
1288 | init.type === "VariableDeclaration" &&
|
---|
1289 | init.declarations[0].init != null &&
|
---|
1290 | (
|
---|
1291 | !isForIn ||
|
---|
1292 | this.options.ecmaVersion < 8 ||
|
---|
1293 | this.strict ||
|
---|
1294 | init.kind !== "var" ||
|
---|
1295 | init.declarations[0].id.type !== "Identifier"
|
---|
1296 | )
|
---|
1297 | ) {
|
---|
1298 | this.raise(
|
---|
1299 | init.start,
|
---|
1300 | ((isForIn ? "for-in" : "for-of") + " loop variable declaration may not have an initializer")
|
---|
1301 | );
|
---|
1302 | }
|
---|
1303 | node.left = init;
|
---|
1304 | node.right = isForIn ? this.parseExpression() : this.parseMaybeAssign();
|
---|
1305 | this.expect(types$1.parenR);
|
---|
1306 | node.body = this.parseStatement("for");
|
---|
1307 | this.exitScope();
|
---|
1308 | this.labels.pop();
|
---|
1309 | return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement")
|
---|
1310 | };
|
---|
1311 |
|
---|
1312 | // Parse a list of variable declarations.
|
---|
1313 |
|
---|
1314 | pp$8.parseVar = function(node, isFor, kind, allowMissingInitializer) {
|
---|
1315 | node.declarations = [];
|
---|
1316 | node.kind = kind;
|
---|
1317 | for (;;) {
|
---|
1318 | var decl = this.startNode();
|
---|
1319 | this.parseVarId(decl, kind);
|
---|
1320 | if (this.eat(types$1.eq)) {
|
---|
1321 | decl.init = this.parseMaybeAssign(isFor);
|
---|
1322 | } else if (!allowMissingInitializer && kind === "const" && !(this.type === types$1._in || (this.options.ecmaVersion >= 6 && this.isContextual("of")))) {
|
---|
1323 | this.unexpected();
|
---|
1324 | } else if (!allowMissingInitializer && decl.id.type !== "Identifier" && !(isFor && (this.type === types$1._in || this.isContextual("of")))) {
|
---|
1325 | this.raise(this.lastTokEnd, "Complex binding patterns require an initialization value");
|
---|
1326 | } else {
|
---|
1327 | decl.init = null;
|
---|
1328 | }
|
---|
1329 | node.declarations.push(this.finishNode(decl, "VariableDeclarator"));
|
---|
1330 | if (!this.eat(types$1.comma)) { break }
|
---|
1331 | }
|
---|
1332 | return node
|
---|
1333 | };
|
---|
1334 |
|
---|
1335 | pp$8.parseVarId = function(decl, kind) {
|
---|
1336 | decl.id = this.parseBindingAtom();
|
---|
1337 | this.checkLValPattern(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false);
|
---|
1338 | };
|
---|
1339 |
|
---|
1340 | var FUNC_STATEMENT = 1, FUNC_HANGING_STATEMENT = 2, FUNC_NULLABLE_ID = 4;
|
---|
1341 |
|
---|
1342 | // Parse a function declaration or literal (depending on the
|
---|
1343 | // `statement & FUNC_STATEMENT`).
|
---|
1344 |
|
---|
1345 | // Remove `allowExpressionBody` for 7.0.0, as it is only called with false
|
---|
1346 | pp$8.parseFunction = function(node, statement, allowExpressionBody, isAsync, forInit) {
|
---|
1347 | this.initFunction(node);
|
---|
1348 | if (this.options.ecmaVersion >= 9 || this.options.ecmaVersion >= 6 && !isAsync) {
|
---|
1349 | if (this.type === types$1.star && (statement & FUNC_HANGING_STATEMENT))
|
---|
1350 | { this.unexpected(); }
|
---|
1351 | node.generator = this.eat(types$1.star);
|
---|
1352 | }
|
---|
1353 | if (this.options.ecmaVersion >= 8)
|
---|
1354 | { node.async = !!isAsync; }
|
---|
1355 |
|
---|
1356 | if (statement & FUNC_STATEMENT) {
|
---|
1357 | node.id = (statement & FUNC_NULLABLE_ID) && this.type !== types$1.name ? null : this.parseIdent();
|
---|
1358 | if (node.id && !(statement & FUNC_HANGING_STATEMENT))
|
---|
1359 | // If it is a regular function declaration in sloppy mode, then it is
|
---|
1360 | // subject to Annex B semantics (BIND_FUNCTION). Otherwise, the binding
|
---|
1361 | // mode depends on properties of the current scope (see
|
---|
1362 | // treatFunctionsAsVar).
|
---|
1363 | { this.checkLValSimple(node.id, (this.strict || node.generator || node.async) ? this.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION); }
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
|
---|
1367 | this.yieldPos = 0;
|
---|
1368 | this.awaitPos = 0;
|
---|
1369 | this.awaitIdentPos = 0;
|
---|
1370 | this.enterScope(functionFlags(node.async, node.generator));
|
---|
1371 |
|
---|
1372 | if (!(statement & FUNC_STATEMENT))
|
---|
1373 | { node.id = this.type === types$1.name ? this.parseIdent() : null; }
|
---|
1374 |
|
---|
1375 | this.parseFunctionParams(node);
|
---|
1376 | this.parseFunctionBody(node, allowExpressionBody, false, forInit);
|
---|
1377 |
|
---|
1378 | this.yieldPos = oldYieldPos;
|
---|
1379 | this.awaitPos = oldAwaitPos;
|
---|
1380 | this.awaitIdentPos = oldAwaitIdentPos;
|
---|
1381 | return this.finishNode(node, (statement & FUNC_STATEMENT) ? "FunctionDeclaration" : "FunctionExpression")
|
---|
1382 | };
|
---|
1383 |
|
---|
1384 | pp$8.parseFunctionParams = function(node) {
|
---|
1385 | this.expect(types$1.parenL);
|
---|
1386 | node.params = this.parseBindingList(types$1.parenR, false, this.options.ecmaVersion >= 8);
|
---|
1387 | this.checkYieldAwaitInDefaultParams();
|
---|
1388 | };
|
---|
1389 |
|
---|
1390 | // Parse a class declaration or literal (depending on the
|
---|
1391 | // `isStatement` parameter).
|
---|
1392 |
|
---|
1393 | pp$8.parseClass = function(node, isStatement) {
|
---|
1394 | this.next();
|
---|
1395 |
|
---|
1396 | // ecma-262 14.6 Class Definitions
|
---|
1397 | // A class definition is always strict mode code.
|
---|
1398 | var oldStrict = this.strict;
|
---|
1399 | this.strict = true;
|
---|
1400 |
|
---|
1401 | this.parseClassId(node, isStatement);
|
---|
1402 | this.parseClassSuper(node);
|
---|
1403 | var privateNameMap = this.enterClassBody();
|
---|
1404 | var classBody = this.startNode();
|
---|
1405 | var hadConstructor = false;
|
---|
1406 | classBody.body = [];
|
---|
1407 | this.expect(types$1.braceL);
|
---|
1408 | while (this.type !== types$1.braceR) {
|
---|
1409 | var element = this.parseClassElement(node.superClass !== null);
|
---|
1410 | if (element) {
|
---|
1411 | classBody.body.push(element);
|
---|
1412 | if (element.type === "MethodDefinition" && element.kind === "constructor") {
|
---|
1413 | if (hadConstructor) { this.raiseRecoverable(element.start, "Duplicate constructor in the same class"); }
|
---|
1414 | hadConstructor = true;
|
---|
1415 | } else if (element.key && element.key.type === "PrivateIdentifier" && isPrivateNameConflicted(privateNameMap, element)) {
|
---|
1416 | this.raiseRecoverable(element.key.start, ("Identifier '#" + (element.key.name) + "' has already been declared"));
|
---|
1417 | }
|
---|
1418 | }
|
---|
1419 | }
|
---|
1420 | this.strict = oldStrict;
|
---|
1421 | this.next();
|
---|
1422 | node.body = this.finishNode(classBody, "ClassBody");
|
---|
1423 | this.exitClassBody();
|
---|
1424 | return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
|
---|
1425 | };
|
---|
1426 |
|
---|
1427 | pp$8.parseClassElement = function(constructorAllowsSuper) {
|
---|
1428 | if (this.eat(types$1.semi)) { return null }
|
---|
1429 |
|
---|
1430 | var ecmaVersion = this.options.ecmaVersion;
|
---|
1431 | var node = this.startNode();
|
---|
1432 | var keyName = "";
|
---|
1433 | var isGenerator = false;
|
---|
1434 | var isAsync = false;
|
---|
1435 | var kind = "method";
|
---|
1436 | var isStatic = false;
|
---|
1437 |
|
---|
1438 | if (this.eatContextual("static")) {
|
---|
1439 | // Parse static init block
|
---|
1440 | if (ecmaVersion >= 13 && this.eat(types$1.braceL)) {
|
---|
1441 | this.parseClassStaticBlock(node);
|
---|
1442 | return node
|
---|
1443 | }
|
---|
1444 | if (this.isClassElementNameStart() || this.type === types$1.star) {
|
---|
1445 | isStatic = true;
|
---|
1446 | } else {
|
---|
1447 | keyName = "static";
|
---|
1448 | }
|
---|
1449 | }
|
---|
1450 | node.static = isStatic;
|
---|
1451 | if (!keyName && ecmaVersion >= 8 && this.eatContextual("async")) {
|
---|
1452 | if ((this.isClassElementNameStart() || this.type === types$1.star) && !this.canInsertSemicolon()) {
|
---|
1453 | isAsync = true;
|
---|
1454 | } else {
|
---|
1455 | keyName = "async";
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 | if (!keyName && (ecmaVersion >= 9 || !isAsync) && this.eat(types$1.star)) {
|
---|
1459 | isGenerator = true;
|
---|
1460 | }
|
---|
1461 | if (!keyName && !isAsync && !isGenerator) {
|
---|
1462 | var lastValue = this.value;
|
---|
1463 | if (this.eatContextual("get") || this.eatContextual("set")) {
|
---|
1464 | if (this.isClassElementNameStart()) {
|
---|
1465 | kind = lastValue;
|
---|
1466 | } else {
|
---|
1467 | keyName = lastValue;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | // Parse element name
|
---|
1473 | if (keyName) {
|
---|
1474 | // 'async', 'get', 'set', or 'static' were not a keyword contextually.
|
---|
1475 | // The last token is any of those. Make it the element name.
|
---|
1476 | node.computed = false;
|
---|
1477 | node.key = this.startNodeAt(this.lastTokStart, this.lastTokStartLoc);
|
---|
1478 | node.key.name = keyName;
|
---|
1479 | this.finishNode(node.key, "Identifier");
|
---|
1480 | } else {
|
---|
1481 | this.parseClassElementName(node);
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | // Parse element value
|
---|
1485 | if (ecmaVersion < 13 || this.type === types$1.parenL || kind !== "method" || isGenerator || isAsync) {
|
---|
1486 | var isConstructor = !node.static && checkKeyName(node, "constructor");
|
---|
1487 | var allowsDirectSuper = isConstructor && constructorAllowsSuper;
|
---|
1488 | // Couldn't move this check into the 'parseClassMethod' method for backward compatibility.
|
---|
1489 | if (isConstructor && kind !== "method") { this.raise(node.key.start, "Constructor can't have get/set modifier"); }
|
---|
1490 | node.kind = isConstructor ? "constructor" : kind;
|
---|
1491 | this.parseClassMethod(node, isGenerator, isAsync, allowsDirectSuper);
|
---|
1492 | } else {
|
---|
1493 | this.parseClassField(node);
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | return node
|
---|
1497 | };
|
---|
1498 |
|
---|
1499 | pp$8.isClassElementNameStart = function() {
|
---|
1500 | return (
|
---|
1501 | this.type === types$1.name ||
|
---|
1502 | this.type === types$1.privateId ||
|
---|
1503 | this.type === types$1.num ||
|
---|
1504 | this.type === types$1.string ||
|
---|
1505 | this.type === types$1.bracketL ||
|
---|
1506 | this.type.keyword
|
---|
1507 | )
|
---|
1508 | };
|
---|
1509 |
|
---|
1510 | pp$8.parseClassElementName = function(element) {
|
---|
1511 | if (this.type === types$1.privateId) {
|
---|
1512 | if (this.value === "constructor") {
|
---|
1513 | this.raise(this.start, "Classes can't have an element named '#constructor'");
|
---|
1514 | }
|
---|
1515 | element.computed = false;
|
---|
1516 | element.key = this.parsePrivateIdent();
|
---|
1517 | } else {
|
---|
1518 | this.parsePropertyName(element);
|
---|
1519 | }
|
---|
1520 | };
|
---|
1521 |
|
---|
1522 | pp$8.parseClassMethod = function(method, isGenerator, isAsync, allowsDirectSuper) {
|
---|
1523 | // Check key and flags
|
---|
1524 | var key = method.key;
|
---|
1525 | if (method.kind === "constructor") {
|
---|
1526 | if (isGenerator) { this.raise(key.start, "Constructor can't be a generator"); }
|
---|
1527 | if (isAsync) { this.raise(key.start, "Constructor can't be an async method"); }
|
---|
1528 | } else if (method.static && checkKeyName(method, "prototype")) {
|
---|
1529 | this.raise(key.start, "Classes may not have a static property named prototype");
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | // Parse value
|
---|
1533 | var value = method.value = this.parseMethod(isGenerator, isAsync, allowsDirectSuper);
|
---|
1534 |
|
---|
1535 | // Check value
|
---|
1536 | if (method.kind === "get" && value.params.length !== 0)
|
---|
1537 | { this.raiseRecoverable(value.start, "getter should have no params"); }
|
---|
1538 | if (method.kind === "set" && value.params.length !== 1)
|
---|
1539 | { this.raiseRecoverable(value.start, "setter should have exactly one param"); }
|
---|
1540 | if (method.kind === "set" && value.params[0].type === "RestElement")
|
---|
1541 | { this.raiseRecoverable(value.params[0].start, "Setter cannot use rest params"); }
|
---|
1542 |
|
---|
1543 | return this.finishNode(method, "MethodDefinition")
|
---|
1544 | };
|
---|
1545 |
|
---|
1546 | pp$8.parseClassField = function(field) {
|
---|
1547 | if (checkKeyName(field, "constructor")) {
|
---|
1548 | this.raise(field.key.start, "Classes can't have a field named 'constructor'");
|
---|
1549 | } else if (field.static && checkKeyName(field, "prototype")) {
|
---|
1550 | this.raise(field.key.start, "Classes can't have a static field named 'prototype'");
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | if (this.eat(types$1.eq)) {
|
---|
1554 | // To raise SyntaxError if 'arguments' exists in the initializer.
|
---|
1555 | var scope = this.currentThisScope();
|
---|
1556 | var inClassFieldInit = scope.inClassFieldInit;
|
---|
1557 | scope.inClassFieldInit = true;
|
---|
1558 | field.value = this.parseMaybeAssign();
|
---|
1559 | scope.inClassFieldInit = inClassFieldInit;
|
---|
1560 | } else {
|
---|
1561 | field.value = null;
|
---|
1562 | }
|
---|
1563 | this.semicolon();
|
---|
1564 |
|
---|
1565 | return this.finishNode(field, "PropertyDefinition")
|
---|
1566 | };
|
---|
1567 |
|
---|
1568 | pp$8.parseClassStaticBlock = function(node) {
|
---|
1569 | node.body = [];
|
---|
1570 |
|
---|
1571 | var oldLabels = this.labels;
|
---|
1572 | this.labels = [];
|
---|
1573 | this.enterScope(SCOPE_CLASS_STATIC_BLOCK | SCOPE_SUPER);
|
---|
1574 | while (this.type !== types$1.braceR) {
|
---|
1575 | var stmt = this.parseStatement(null);
|
---|
1576 | node.body.push(stmt);
|
---|
1577 | }
|
---|
1578 | this.next();
|
---|
1579 | this.exitScope();
|
---|
1580 | this.labels = oldLabels;
|
---|
1581 |
|
---|
1582 | return this.finishNode(node, "StaticBlock")
|
---|
1583 | };
|
---|
1584 |
|
---|
1585 | pp$8.parseClassId = function(node, isStatement) {
|
---|
1586 | if (this.type === types$1.name) {
|
---|
1587 | node.id = this.parseIdent();
|
---|
1588 | if (isStatement)
|
---|
1589 | { this.checkLValSimple(node.id, BIND_LEXICAL, false); }
|
---|
1590 | } else {
|
---|
1591 | if (isStatement === true)
|
---|
1592 | { this.unexpected(); }
|
---|
1593 | node.id = null;
|
---|
1594 | }
|
---|
1595 | };
|
---|
1596 |
|
---|
1597 | pp$8.parseClassSuper = function(node) {
|
---|
1598 | node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts(null, false) : null;
|
---|
1599 | };
|
---|
1600 |
|
---|
1601 | pp$8.enterClassBody = function() {
|
---|
1602 | var element = {declared: Object.create(null), used: []};
|
---|
1603 | this.privateNameStack.push(element);
|
---|
1604 | return element.declared
|
---|
1605 | };
|
---|
1606 |
|
---|
1607 | pp$8.exitClassBody = function() {
|
---|
1608 | var ref = this.privateNameStack.pop();
|
---|
1609 | var declared = ref.declared;
|
---|
1610 | var used = ref.used;
|
---|
1611 | if (!this.options.checkPrivateFields) { return }
|
---|
1612 | var len = this.privateNameStack.length;
|
---|
1613 | var parent = len === 0 ? null : this.privateNameStack[len - 1];
|
---|
1614 | for (var i = 0; i < used.length; ++i) {
|
---|
1615 | var id = used[i];
|
---|
1616 | if (!hasOwn(declared, id.name)) {
|
---|
1617 | if (parent) {
|
---|
1618 | parent.used.push(id);
|
---|
1619 | } else {
|
---|
1620 | this.raiseRecoverable(id.start, ("Private field '#" + (id.name) + "' must be declared in an enclosing class"));
|
---|
1621 | }
|
---|
1622 | }
|
---|
1623 | }
|
---|
1624 | };
|
---|
1625 |
|
---|
1626 | function isPrivateNameConflicted(privateNameMap, element) {
|
---|
1627 | var name = element.key.name;
|
---|
1628 | var curr = privateNameMap[name];
|
---|
1629 |
|
---|
1630 | var next = "true";
|
---|
1631 | if (element.type === "MethodDefinition" && (element.kind === "get" || element.kind === "set")) {
|
---|
1632 | next = (element.static ? "s" : "i") + element.kind;
|
---|
1633 | }
|
---|
1634 |
|
---|
1635 | // `class { get #a(){}; static set #a(_){} }` is also conflict.
|
---|
1636 | if (
|
---|
1637 | curr === "iget" && next === "iset" ||
|
---|
1638 | curr === "iset" && next === "iget" ||
|
---|
1639 | curr === "sget" && next === "sset" ||
|
---|
1640 | curr === "sset" && next === "sget"
|
---|
1641 | ) {
|
---|
1642 | privateNameMap[name] = "true";
|
---|
1643 | return false
|
---|
1644 | } else if (!curr) {
|
---|
1645 | privateNameMap[name] = next;
|
---|
1646 | return false
|
---|
1647 | } else {
|
---|
1648 | return true
|
---|
1649 | }
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | function checkKeyName(node, name) {
|
---|
1653 | var computed = node.computed;
|
---|
1654 | var key = node.key;
|
---|
1655 | return !computed && (
|
---|
1656 | key.type === "Identifier" && key.name === name ||
|
---|
1657 | key.type === "Literal" && key.value === name
|
---|
1658 | )
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | // Parses module export declaration.
|
---|
1662 |
|
---|
1663 | pp$8.parseExportAllDeclaration = function(node, exports) {
|
---|
1664 | if (this.options.ecmaVersion >= 11) {
|
---|
1665 | if (this.eatContextual("as")) {
|
---|
1666 | node.exported = this.parseModuleExportName();
|
---|
1667 | this.checkExport(exports, node.exported, this.lastTokStart);
|
---|
1668 | } else {
|
---|
1669 | node.exported = null;
|
---|
1670 | }
|
---|
1671 | }
|
---|
1672 | this.expectContextual("from");
|
---|
1673 | if (this.type !== types$1.string) { this.unexpected(); }
|
---|
1674 | node.source = this.parseExprAtom();
|
---|
1675 | this.semicolon();
|
---|
1676 | return this.finishNode(node, "ExportAllDeclaration")
|
---|
1677 | };
|
---|
1678 |
|
---|
1679 | pp$8.parseExport = function(node, exports) {
|
---|
1680 | this.next();
|
---|
1681 | // export * from '...'
|
---|
1682 | if (this.eat(types$1.star)) {
|
---|
1683 | return this.parseExportAllDeclaration(node, exports)
|
---|
1684 | }
|
---|
1685 | if (this.eat(types$1._default)) { // export default ...
|
---|
1686 | this.checkExport(exports, "default", this.lastTokStart);
|
---|
1687 | node.declaration = this.parseExportDefaultDeclaration();
|
---|
1688 | return this.finishNode(node, "ExportDefaultDeclaration")
|
---|
1689 | }
|
---|
1690 | // export var|const|let|function|class ...
|
---|
1691 | if (this.shouldParseExportStatement()) {
|
---|
1692 | node.declaration = this.parseExportDeclaration(node);
|
---|
1693 | if (node.declaration.type === "VariableDeclaration")
|
---|
1694 | { this.checkVariableExport(exports, node.declaration.declarations); }
|
---|
1695 | else
|
---|
1696 | { this.checkExport(exports, node.declaration.id, node.declaration.id.start); }
|
---|
1697 | node.specifiers = [];
|
---|
1698 | node.source = null;
|
---|
1699 | } else { // export { x, y as z } [from '...']
|
---|
1700 | node.declaration = null;
|
---|
1701 | node.specifiers = this.parseExportSpecifiers(exports);
|
---|
1702 | if (this.eatContextual("from")) {
|
---|
1703 | if (this.type !== types$1.string) { this.unexpected(); }
|
---|
1704 | node.source = this.parseExprAtom();
|
---|
1705 | } else {
|
---|
1706 | for (var i = 0, list = node.specifiers; i < list.length; i += 1) {
|
---|
1707 | // check for keywords used as local names
|
---|
1708 | var spec = list[i];
|
---|
1709 |
|
---|
1710 | this.checkUnreserved(spec.local);
|
---|
1711 | // check if export is defined
|
---|
1712 | this.checkLocalExport(spec.local);
|
---|
1713 |
|
---|
1714 | if (spec.local.type === "Literal") {
|
---|
1715 | this.raise(spec.local.start, "A string literal cannot be used as an exported binding without `from`.");
|
---|
1716 | }
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | node.source = null;
|
---|
1720 | }
|
---|
1721 | this.semicolon();
|
---|
1722 | }
|
---|
1723 | return this.finishNode(node, "ExportNamedDeclaration")
|
---|
1724 | };
|
---|
1725 |
|
---|
1726 | pp$8.parseExportDeclaration = function(node) {
|
---|
1727 | return this.parseStatement(null)
|
---|
1728 | };
|
---|
1729 |
|
---|
1730 | pp$8.parseExportDefaultDeclaration = function() {
|
---|
1731 | var isAsync;
|
---|
1732 | if (this.type === types$1._function || (isAsync = this.isAsyncFunction())) {
|
---|
1733 | var fNode = this.startNode();
|
---|
1734 | this.next();
|
---|
1735 | if (isAsync) { this.next(); }
|
---|
1736 | return this.parseFunction(fNode, FUNC_STATEMENT | FUNC_NULLABLE_ID, false, isAsync)
|
---|
1737 | } else if (this.type === types$1._class) {
|
---|
1738 | var cNode = this.startNode();
|
---|
1739 | return this.parseClass(cNode, "nullableID")
|
---|
1740 | } else {
|
---|
1741 | var declaration = this.parseMaybeAssign();
|
---|
1742 | this.semicolon();
|
---|
1743 | return declaration
|
---|
1744 | }
|
---|
1745 | };
|
---|
1746 |
|
---|
1747 | pp$8.checkExport = function(exports, name, pos) {
|
---|
1748 | if (!exports) { return }
|
---|
1749 | if (typeof name !== "string")
|
---|
1750 | { name = name.type === "Identifier" ? name.name : name.value; }
|
---|
1751 | if (hasOwn(exports, name))
|
---|
1752 | { this.raiseRecoverable(pos, "Duplicate export '" + name + "'"); }
|
---|
1753 | exports[name] = true;
|
---|
1754 | };
|
---|
1755 |
|
---|
1756 | pp$8.checkPatternExport = function(exports, pat) {
|
---|
1757 | var type = pat.type;
|
---|
1758 | if (type === "Identifier")
|
---|
1759 | { this.checkExport(exports, pat, pat.start); }
|
---|
1760 | else if (type === "ObjectPattern")
|
---|
1761 | { for (var i = 0, list = pat.properties; i < list.length; i += 1)
|
---|
1762 | {
|
---|
1763 | var prop = list[i];
|
---|
1764 |
|
---|
1765 | this.checkPatternExport(exports, prop);
|
---|
1766 | } }
|
---|
1767 | else if (type === "ArrayPattern")
|
---|
1768 | { for (var i$1 = 0, list$1 = pat.elements; i$1 < list$1.length; i$1 += 1) {
|
---|
1769 | var elt = list$1[i$1];
|
---|
1770 |
|
---|
1771 | if (elt) { this.checkPatternExport(exports, elt); }
|
---|
1772 | } }
|
---|
1773 | else if (type === "Property")
|
---|
1774 | { this.checkPatternExport(exports, pat.value); }
|
---|
1775 | else if (type === "AssignmentPattern")
|
---|
1776 | { this.checkPatternExport(exports, pat.left); }
|
---|
1777 | else if (type === "RestElement")
|
---|
1778 | { this.checkPatternExport(exports, pat.argument); }
|
---|
1779 | };
|
---|
1780 |
|
---|
1781 | pp$8.checkVariableExport = function(exports, decls) {
|
---|
1782 | if (!exports) { return }
|
---|
1783 | for (var i = 0, list = decls; i < list.length; i += 1)
|
---|
1784 | {
|
---|
1785 | var decl = list[i];
|
---|
1786 |
|
---|
1787 | this.checkPatternExport(exports, decl.id);
|
---|
1788 | }
|
---|
1789 | };
|
---|
1790 |
|
---|
1791 | pp$8.shouldParseExportStatement = function() {
|
---|
1792 | return this.type.keyword === "var" ||
|
---|
1793 | this.type.keyword === "const" ||
|
---|
1794 | this.type.keyword === "class" ||
|
---|
1795 | this.type.keyword === "function" ||
|
---|
1796 | this.isLet() ||
|
---|
1797 | this.isAsyncFunction()
|
---|
1798 | };
|
---|
1799 |
|
---|
1800 | // Parses a comma-separated list of module exports.
|
---|
1801 |
|
---|
1802 | pp$8.parseExportSpecifier = function(exports) {
|
---|
1803 | var node = this.startNode();
|
---|
1804 | node.local = this.parseModuleExportName();
|
---|
1805 |
|
---|
1806 | node.exported = this.eatContextual("as") ? this.parseModuleExportName() : node.local;
|
---|
1807 | this.checkExport(
|
---|
1808 | exports,
|
---|
1809 | node.exported,
|
---|
1810 | node.exported.start
|
---|
1811 | );
|
---|
1812 |
|
---|
1813 | return this.finishNode(node, "ExportSpecifier")
|
---|
1814 | };
|
---|
1815 |
|
---|
1816 | pp$8.parseExportSpecifiers = function(exports) {
|
---|
1817 | var nodes = [], first = true;
|
---|
1818 | // export { x, y as z } [from '...']
|
---|
1819 | this.expect(types$1.braceL);
|
---|
1820 | while (!this.eat(types$1.braceR)) {
|
---|
1821 | if (!first) {
|
---|
1822 | this.expect(types$1.comma);
|
---|
1823 | if (this.afterTrailingComma(types$1.braceR)) { break }
|
---|
1824 | } else { first = false; }
|
---|
1825 |
|
---|
1826 | nodes.push(this.parseExportSpecifier(exports));
|
---|
1827 | }
|
---|
1828 | return nodes
|
---|
1829 | };
|
---|
1830 |
|
---|
1831 | // Parses import declaration.
|
---|
1832 |
|
---|
1833 | pp$8.parseImport = function(node) {
|
---|
1834 | this.next();
|
---|
1835 |
|
---|
1836 | // import '...'
|
---|
1837 | if (this.type === types$1.string) {
|
---|
1838 | node.specifiers = empty$1;
|
---|
1839 | node.source = this.parseExprAtom();
|
---|
1840 | } else {
|
---|
1841 | node.specifiers = this.parseImportSpecifiers();
|
---|
1842 | this.expectContextual("from");
|
---|
1843 | node.source = this.type === types$1.string ? this.parseExprAtom() : this.unexpected();
|
---|
1844 | }
|
---|
1845 | this.semicolon();
|
---|
1846 | return this.finishNode(node, "ImportDeclaration")
|
---|
1847 | };
|
---|
1848 |
|
---|
1849 | // Parses a comma-separated list of module imports.
|
---|
1850 |
|
---|
1851 | pp$8.parseImportSpecifier = function() {
|
---|
1852 | var node = this.startNode();
|
---|
1853 | node.imported = this.parseModuleExportName();
|
---|
1854 |
|
---|
1855 | if (this.eatContextual("as")) {
|
---|
1856 | node.local = this.parseIdent();
|
---|
1857 | } else {
|
---|
1858 | this.checkUnreserved(node.imported);
|
---|
1859 | node.local = node.imported;
|
---|
1860 | }
|
---|
1861 | this.checkLValSimple(node.local, BIND_LEXICAL);
|
---|
1862 |
|
---|
1863 | return this.finishNode(node, "ImportSpecifier")
|
---|
1864 | };
|
---|
1865 |
|
---|
1866 | pp$8.parseImportDefaultSpecifier = function() {
|
---|
1867 | // import defaultObj, { x, y as z } from '...'
|
---|
1868 | var node = this.startNode();
|
---|
1869 | node.local = this.parseIdent();
|
---|
1870 | this.checkLValSimple(node.local, BIND_LEXICAL);
|
---|
1871 | return this.finishNode(node, "ImportDefaultSpecifier")
|
---|
1872 | };
|
---|
1873 |
|
---|
1874 | pp$8.parseImportNamespaceSpecifier = function() {
|
---|
1875 | var node = this.startNode();
|
---|
1876 | this.next();
|
---|
1877 | this.expectContextual("as");
|
---|
1878 | node.local = this.parseIdent();
|
---|
1879 | this.checkLValSimple(node.local, BIND_LEXICAL);
|
---|
1880 | return this.finishNode(node, "ImportNamespaceSpecifier")
|
---|
1881 | };
|
---|
1882 |
|
---|
1883 | pp$8.parseImportSpecifiers = function() {
|
---|
1884 | var nodes = [], first = true;
|
---|
1885 | if (this.type === types$1.name) {
|
---|
1886 | nodes.push(this.parseImportDefaultSpecifier());
|
---|
1887 | if (!this.eat(types$1.comma)) { return nodes }
|
---|
1888 | }
|
---|
1889 | if (this.type === types$1.star) {
|
---|
1890 | nodes.push(this.parseImportNamespaceSpecifier());
|
---|
1891 | return nodes
|
---|
1892 | }
|
---|
1893 | this.expect(types$1.braceL);
|
---|
1894 | while (!this.eat(types$1.braceR)) {
|
---|
1895 | if (!first) {
|
---|
1896 | this.expect(types$1.comma);
|
---|
1897 | if (this.afterTrailingComma(types$1.braceR)) { break }
|
---|
1898 | } else { first = false; }
|
---|
1899 |
|
---|
1900 | nodes.push(this.parseImportSpecifier());
|
---|
1901 | }
|
---|
1902 | return nodes
|
---|
1903 | };
|
---|
1904 |
|
---|
1905 | pp$8.parseModuleExportName = function() {
|
---|
1906 | if (this.options.ecmaVersion >= 13 && this.type === types$1.string) {
|
---|
1907 | var stringLiteral = this.parseLiteral(this.value);
|
---|
1908 | if (loneSurrogate.test(stringLiteral.value)) {
|
---|
1909 | this.raise(stringLiteral.start, "An export name cannot include a lone surrogate.");
|
---|
1910 | }
|
---|
1911 | return stringLiteral
|
---|
1912 | }
|
---|
1913 | return this.parseIdent(true)
|
---|
1914 | };
|
---|
1915 |
|
---|
1916 | // Set `ExpressionStatement#directive` property for directive prologues.
|
---|
1917 | pp$8.adaptDirectivePrologue = function(statements) {
|
---|
1918 | for (var i = 0; i < statements.length && this.isDirectiveCandidate(statements[i]); ++i) {
|
---|
1919 | statements[i].directive = statements[i].expression.raw.slice(1, -1);
|
---|
1920 | }
|
---|
1921 | };
|
---|
1922 | pp$8.isDirectiveCandidate = function(statement) {
|
---|
1923 | return (
|
---|
1924 | this.options.ecmaVersion >= 5 &&
|
---|
1925 | statement.type === "ExpressionStatement" &&
|
---|
1926 | statement.expression.type === "Literal" &&
|
---|
1927 | typeof statement.expression.value === "string" &&
|
---|
1928 | // Reject parenthesized strings.
|
---|
1929 | (this.input[statement.start] === "\"" || this.input[statement.start] === "'")
|
---|
1930 | )
|
---|
1931 | };
|
---|
1932 |
|
---|
1933 | var pp$7 = Parser.prototype;
|
---|
1934 |
|
---|
1935 | // Convert existing expression atom to assignable pattern
|
---|
1936 | // if possible.
|
---|
1937 |
|
---|
1938 | pp$7.toAssignable = function(node, isBinding, refDestructuringErrors) {
|
---|
1939 | if (this.options.ecmaVersion >= 6 && node) {
|
---|
1940 | switch (node.type) {
|
---|
1941 | case "Identifier":
|
---|
1942 | if (this.inAsync && node.name === "await")
|
---|
1943 | { this.raise(node.start, "Cannot use 'await' as identifier inside an async function"); }
|
---|
1944 | break
|
---|
1945 |
|
---|
1946 | case "ObjectPattern":
|
---|
1947 | case "ArrayPattern":
|
---|
1948 | case "AssignmentPattern":
|
---|
1949 | case "RestElement":
|
---|
1950 | break
|
---|
1951 |
|
---|
1952 | case "ObjectExpression":
|
---|
1953 | node.type = "ObjectPattern";
|
---|
1954 | if (refDestructuringErrors) { this.checkPatternErrors(refDestructuringErrors, true); }
|
---|
1955 | for (var i = 0, list = node.properties; i < list.length; i += 1) {
|
---|
1956 | var prop = list[i];
|
---|
1957 |
|
---|
1958 | this.toAssignable(prop, isBinding);
|
---|
1959 | // Early error:
|
---|
1960 | // AssignmentRestProperty[Yield, Await] :
|
---|
1961 | // `...` DestructuringAssignmentTarget[Yield, Await]
|
---|
1962 | //
|
---|
1963 | // It is a Syntax Error if |DestructuringAssignmentTarget| is an |ArrayLiteral| or an |ObjectLiteral|.
|
---|
1964 | if (
|
---|
1965 | prop.type === "RestElement" &&
|
---|
1966 | (prop.argument.type === "ArrayPattern" || prop.argument.type === "ObjectPattern")
|
---|
1967 | ) {
|
---|
1968 | this.raise(prop.argument.start, "Unexpected token");
|
---|
1969 | }
|
---|
1970 | }
|
---|
1971 | break
|
---|
1972 |
|
---|
1973 | case "Property":
|
---|
1974 | // AssignmentProperty has type === "Property"
|
---|
1975 | if (node.kind !== "init") { this.raise(node.key.start, "Object pattern can't contain getter or setter"); }
|
---|
1976 | this.toAssignable(node.value, isBinding);
|
---|
1977 | break
|
---|
1978 |
|
---|
1979 | case "ArrayExpression":
|
---|
1980 | node.type = "ArrayPattern";
|
---|
1981 | if (refDestructuringErrors) { this.checkPatternErrors(refDestructuringErrors, true); }
|
---|
1982 | this.toAssignableList(node.elements, isBinding);
|
---|
1983 | break
|
---|
1984 |
|
---|
1985 | case "SpreadElement":
|
---|
1986 | node.type = "RestElement";
|
---|
1987 | this.toAssignable(node.argument, isBinding);
|
---|
1988 | if (node.argument.type === "AssignmentPattern")
|
---|
1989 | { this.raise(node.argument.start, "Rest elements cannot have a default value"); }
|
---|
1990 | break
|
---|
1991 |
|
---|
1992 | case "AssignmentExpression":
|
---|
1993 | if (node.operator !== "=") { this.raise(node.left.end, "Only '=' operator can be used for specifying default value."); }
|
---|
1994 | node.type = "AssignmentPattern";
|
---|
1995 | delete node.operator;
|
---|
1996 | this.toAssignable(node.left, isBinding);
|
---|
1997 | break
|
---|
1998 |
|
---|
1999 | case "ParenthesizedExpression":
|
---|
2000 | this.toAssignable(node.expression, isBinding, refDestructuringErrors);
|
---|
2001 | break
|
---|
2002 |
|
---|
2003 | case "ChainExpression":
|
---|
2004 | this.raiseRecoverable(node.start, "Optional chaining cannot appear in left-hand side");
|
---|
2005 | break
|
---|
2006 |
|
---|
2007 | case "MemberExpression":
|
---|
2008 | if (!isBinding) { break }
|
---|
2009 |
|
---|
2010 | default:
|
---|
2011 | this.raise(node.start, "Assigning to rvalue");
|
---|
2012 | }
|
---|
2013 | } else if (refDestructuringErrors) { this.checkPatternErrors(refDestructuringErrors, true); }
|
---|
2014 | return node
|
---|
2015 | };
|
---|
2016 |
|
---|
2017 | // Convert list of expression atoms to binding list.
|
---|
2018 |
|
---|
2019 | pp$7.toAssignableList = function(exprList, isBinding) {
|
---|
2020 | var end = exprList.length;
|
---|
2021 | for (var i = 0; i < end; i++) {
|
---|
2022 | var elt = exprList[i];
|
---|
2023 | if (elt) { this.toAssignable(elt, isBinding); }
|
---|
2024 | }
|
---|
2025 | if (end) {
|
---|
2026 | var last = exprList[end - 1];
|
---|
2027 | if (this.options.ecmaVersion === 6 && isBinding && last && last.type === "RestElement" && last.argument.type !== "Identifier")
|
---|
2028 | { this.unexpected(last.argument.start); }
|
---|
2029 | }
|
---|
2030 | return exprList
|
---|
2031 | };
|
---|
2032 |
|
---|
2033 | // Parses spread element.
|
---|
2034 |
|
---|
2035 | pp$7.parseSpread = function(refDestructuringErrors) {
|
---|
2036 | var node = this.startNode();
|
---|
2037 | this.next();
|
---|
2038 | node.argument = this.parseMaybeAssign(false, refDestructuringErrors);
|
---|
2039 | return this.finishNode(node, "SpreadElement")
|
---|
2040 | };
|
---|
2041 |
|
---|
2042 | pp$7.parseRestBinding = function() {
|
---|
2043 | var node = this.startNode();
|
---|
2044 | this.next();
|
---|
2045 |
|
---|
2046 | // RestElement inside of a function parameter must be an identifier
|
---|
2047 | if (this.options.ecmaVersion === 6 && this.type !== types$1.name)
|
---|
2048 | { this.unexpected(); }
|
---|
2049 |
|
---|
2050 | node.argument = this.parseBindingAtom();
|
---|
2051 |
|
---|
2052 | return this.finishNode(node, "RestElement")
|
---|
2053 | };
|
---|
2054 |
|
---|
2055 | // Parses lvalue (assignable) atom.
|
---|
2056 |
|
---|
2057 | pp$7.parseBindingAtom = function() {
|
---|
2058 | if (this.options.ecmaVersion >= 6) {
|
---|
2059 | switch (this.type) {
|
---|
2060 | case types$1.bracketL:
|
---|
2061 | var node = this.startNode();
|
---|
2062 | this.next();
|
---|
2063 | node.elements = this.parseBindingList(types$1.bracketR, true, true);
|
---|
2064 | return this.finishNode(node, "ArrayPattern")
|
---|
2065 |
|
---|
2066 | case types$1.braceL:
|
---|
2067 | return this.parseObj(true)
|
---|
2068 | }
|
---|
2069 | }
|
---|
2070 | return this.parseIdent()
|
---|
2071 | };
|
---|
2072 |
|
---|
2073 | pp$7.parseBindingList = function(close, allowEmpty, allowTrailingComma, allowModifiers) {
|
---|
2074 | var elts = [], first = true;
|
---|
2075 | while (!this.eat(close)) {
|
---|
2076 | if (first) { first = false; }
|
---|
2077 | else { this.expect(types$1.comma); }
|
---|
2078 | if (allowEmpty && this.type === types$1.comma) {
|
---|
2079 | elts.push(null);
|
---|
2080 | } else if (allowTrailingComma && this.afterTrailingComma(close)) {
|
---|
2081 | break
|
---|
2082 | } else if (this.type === types$1.ellipsis) {
|
---|
2083 | var rest = this.parseRestBinding();
|
---|
2084 | this.parseBindingListItem(rest);
|
---|
2085 | elts.push(rest);
|
---|
2086 | if (this.type === types$1.comma) { this.raiseRecoverable(this.start, "Comma is not permitted after the rest element"); }
|
---|
2087 | this.expect(close);
|
---|
2088 | break
|
---|
2089 | } else {
|
---|
2090 | elts.push(this.parseAssignableListItem(allowModifiers));
|
---|
2091 | }
|
---|
2092 | }
|
---|
2093 | return elts
|
---|
2094 | };
|
---|
2095 |
|
---|
2096 | pp$7.parseAssignableListItem = function(allowModifiers) {
|
---|
2097 | var elem = this.parseMaybeDefault(this.start, this.startLoc);
|
---|
2098 | this.parseBindingListItem(elem);
|
---|
2099 | return elem
|
---|
2100 | };
|
---|
2101 |
|
---|
2102 | pp$7.parseBindingListItem = function(param) {
|
---|
2103 | return param
|
---|
2104 | };
|
---|
2105 |
|
---|
2106 | // Parses assignment pattern around given atom if possible.
|
---|
2107 |
|
---|
2108 | pp$7.parseMaybeDefault = function(startPos, startLoc, left) {
|
---|
2109 | left = left || this.parseBindingAtom();
|
---|
2110 | if (this.options.ecmaVersion < 6 || !this.eat(types$1.eq)) { return left }
|
---|
2111 | var node = this.startNodeAt(startPos, startLoc);
|
---|
2112 | node.left = left;
|
---|
2113 | node.right = this.parseMaybeAssign();
|
---|
2114 | return this.finishNode(node, "AssignmentPattern")
|
---|
2115 | };
|
---|
2116 |
|
---|
2117 | // The following three functions all verify that a node is an lvalue —
|
---|
2118 | // something that can be bound, or assigned to. In order to do so, they perform
|
---|
2119 | // a variety of checks:
|
---|
2120 | //
|
---|
2121 | // - Check that none of the bound/assigned-to identifiers are reserved words.
|
---|
2122 | // - Record name declarations for bindings in the appropriate scope.
|
---|
2123 | // - Check duplicate argument names, if checkClashes is set.
|
---|
2124 | //
|
---|
2125 | // If a complex binding pattern is encountered (e.g., object and array
|
---|
2126 | // destructuring), the entire pattern is recursively checked.
|
---|
2127 | //
|
---|
2128 | // There are three versions of checkLVal*() appropriate for different
|
---|
2129 | // circumstances:
|
---|
2130 | //
|
---|
2131 | // - checkLValSimple() shall be used if the syntactic construct supports
|
---|
2132 | // nothing other than identifiers and member expressions. Parenthesized
|
---|
2133 | // expressions are also correctly handled. This is generally appropriate for
|
---|
2134 | // constructs for which the spec says
|
---|
2135 | //
|
---|
2136 | // > It is a Syntax Error if AssignmentTargetType of [the production] is not
|
---|
2137 | // > simple.
|
---|
2138 | //
|
---|
2139 | // It is also appropriate for checking if an identifier is valid and not
|
---|
2140 | // defined elsewhere, like import declarations or function/class identifiers.
|
---|
2141 | //
|
---|
2142 | // Examples where this is used include:
|
---|
2143 | // a += …;
|
---|
2144 | // import a from '…';
|
---|
2145 | // where a is the node to be checked.
|
---|
2146 | //
|
---|
2147 | // - checkLValPattern() shall be used if the syntactic construct supports
|
---|
2148 | // anything checkLValSimple() supports, as well as object and array
|
---|
2149 | // destructuring patterns. This is generally appropriate for constructs for
|
---|
2150 | // which the spec says
|
---|
2151 | //
|
---|
2152 | // > It is a Syntax Error if [the production] is neither an ObjectLiteral nor
|
---|
2153 | // > an ArrayLiteral and AssignmentTargetType of [the production] is not
|
---|
2154 | // > simple.
|
---|
2155 | //
|
---|
2156 | // Examples where this is used include:
|
---|
2157 | // (a = …);
|
---|
2158 | // const a = …;
|
---|
2159 | // try { … } catch (a) { … }
|
---|
2160 | // where a is the node to be checked.
|
---|
2161 | //
|
---|
2162 | // - checkLValInnerPattern() shall be used if the syntactic construct supports
|
---|
2163 | // anything checkLValPattern() supports, as well as default assignment
|
---|
2164 | // patterns, rest elements, and other constructs that may appear within an
|
---|
2165 | // object or array destructuring pattern.
|
---|
2166 | //
|
---|
2167 | // As a special case, function parameters also use checkLValInnerPattern(),
|
---|
2168 | // as they also support defaults and rest constructs.
|
---|
2169 | //
|
---|
2170 | // These functions deliberately support both assignment and binding constructs,
|
---|
2171 | // as the logic for both is exceedingly similar. If the node is the target of
|
---|
2172 | // an assignment, then bindingType should be set to BIND_NONE. Otherwise, it
|
---|
2173 | // should be set to the appropriate BIND_* constant, like BIND_VAR or
|
---|
2174 | // BIND_LEXICAL.
|
---|
2175 | //
|
---|
2176 | // If the function is called with a non-BIND_NONE bindingType, then
|
---|
2177 | // additionally a checkClashes object may be specified to allow checking for
|
---|
2178 | // duplicate argument names. checkClashes is ignored if the provided construct
|
---|
2179 | // is an assignment (i.e., bindingType is BIND_NONE).
|
---|
2180 |
|
---|
2181 | pp$7.checkLValSimple = function(expr, bindingType, checkClashes) {
|
---|
2182 | if ( bindingType === void 0 ) bindingType = BIND_NONE;
|
---|
2183 |
|
---|
2184 | var isBind = bindingType !== BIND_NONE;
|
---|
2185 |
|
---|
2186 | switch (expr.type) {
|
---|
2187 | case "Identifier":
|
---|
2188 | if (this.strict && this.reservedWordsStrictBind.test(expr.name))
|
---|
2189 | { this.raiseRecoverable(expr.start, (isBind ? "Binding " : "Assigning to ") + expr.name + " in strict mode"); }
|
---|
2190 | if (isBind) {
|
---|
2191 | if (bindingType === BIND_LEXICAL && expr.name === "let")
|
---|
2192 | { this.raiseRecoverable(expr.start, "let is disallowed as a lexically bound name"); }
|
---|
2193 | if (checkClashes) {
|
---|
2194 | if (hasOwn(checkClashes, expr.name))
|
---|
2195 | { this.raiseRecoverable(expr.start, "Argument name clash"); }
|
---|
2196 | checkClashes[expr.name] = true;
|
---|
2197 | }
|
---|
2198 | if (bindingType !== BIND_OUTSIDE) { this.declareName(expr.name, bindingType, expr.start); }
|
---|
2199 | }
|
---|
2200 | break
|
---|
2201 |
|
---|
2202 | case "ChainExpression":
|
---|
2203 | this.raiseRecoverable(expr.start, "Optional chaining cannot appear in left-hand side");
|
---|
2204 | break
|
---|
2205 |
|
---|
2206 | case "MemberExpression":
|
---|
2207 | if (isBind) { this.raiseRecoverable(expr.start, "Binding member expression"); }
|
---|
2208 | break
|
---|
2209 |
|
---|
2210 | case "ParenthesizedExpression":
|
---|
2211 | if (isBind) { this.raiseRecoverable(expr.start, "Binding parenthesized expression"); }
|
---|
2212 | return this.checkLValSimple(expr.expression, bindingType, checkClashes)
|
---|
2213 |
|
---|
2214 | default:
|
---|
2215 | this.raise(expr.start, (isBind ? "Binding" : "Assigning to") + " rvalue");
|
---|
2216 | }
|
---|
2217 | };
|
---|
2218 |
|
---|
2219 | pp$7.checkLValPattern = function(expr, bindingType, checkClashes) {
|
---|
2220 | if ( bindingType === void 0 ) bindingType = BIND_NONE;
|
---|
2221 |
|
---|
2222 | switch (expr.type) {
|
---|
2223 | case "ObjectPattern":
|
---|
2224 | for (var i = 0, list = expr.properties; i < list.length; i += 1) {
|
---|
2225 | var prop = list[i];
|
---|
2226 |
|
---|
2227 | this.checkLValInnerPattern(prop, bindingType, checkClashes);
|
---|
2228 | }
|
---|
2229 | break
|
---|
2230 |
|
---|
2231 | case "ArrayPattern":
|
---|
2232 | for (var i$1 = 0, list$1 = expr.elements; i$1 < list$1.length; i$1 += 1) {
|
---|
2233 | var elem = list$1[i$1];
|
---|
2234 |
|
---|
2235 | if (elem) { this.checkLValInnerPattern(elem, bindingType, checkClashes); }
|
---|
2236 | }
|
---|
2237 | break
|
---|
2238 |
|
---|
2239 | default:
|
---|
2240 | this.checkLValSimple(expr, bindingType, checkClashes);
|
---|
2241 | }
|
---|
2242 | };
|
---|
2243 |
|
---|
2244 | pp$7.checkLValInnerPattern = function(expr, bindingType, checkClashes) {
|
---|
2245 | if ( bindingType === void 0 ) bindingType = BIND_NONE;
|
---|
2246 |
|
---|
2247 | switch (expr.type) {
|
---|
2248 | case "Property":
|
---|
2249 | // AssignmentProperty has type === "Property"
|
---|
2250 | this.checkLValInnerPattern(expr.value, bindingType, checkClashes);
|
---|
2251 | break
|
---|
2252 |
|
---|
2253 | case "AssignmentPattern":
|
---|
2254 | this.checkLValPattern(expr.left, bindingType, checkClashes);
|
---|
2255 | break
|
---|
2256 |
|
---|
2257 | case "RestElement":
|
---|
2258 | this.checkLValPattern(expr.argument, bindingType, checkClashes);
|
---|
2259 | break
|
---|
2260 |
|
---|
2261 | default:
|
---|
2262 | this.checkLValPattern(expr, bindingType, checkClashes);
|
---|
2263 | }
|
---|
2264 | };
|
---|
2265 |
|
---|
2266 | // The algorithm used to determine whether a regexp can appear at a
|
---|
2267 | // given point in the program is loosely based on sweet.js' approach.
|
---|
2268 | // See https://github.com/mozilla/sweet.js/wiki/design
|
---|
2269 |
|
---|
2270 |
|
---|
2271 | var TokContext = function TokContext(token, isExpr, preserveSpace, override, generator) {
|
---|
2272 | this.token = token;
|
---|
2273 | this.isExpr = !!isExpr;
|
---|
2274 | this.preserveSpace = !!preserveSpace;
|
---|
2275 | this.override = override;
|
---|
2276 | this.generator = !!generator;
|
---|
2277 | };
|
---|
2278 |
|
---|
2279 | var types = {
|
---|
2280 | b_stat: new TokContext("{", false),
|
---|
2281 | b_expr: new TokContext("{", true),
|
---|
2282 | b_tmpl: new TokContext("${", false),
|
---|
2283 | p_stat: new TokContext("(", false),
|
---|
2284 | p_expr: new TokContext("(", true),
|
---|
2285 | q_tmpl: new TokContext("`", true, true, function (p) { return p.tryReadTemplateToken(); }),
|
---|
2286 | f_stat: new TokContext("function", false),
|
---|
2287 | f_expr: new TokContext("function", true),
|
---|
2288 | f_expr_gen: new TokContext("function", true, false, null, true),
|
---|
2289 | f_gen: new TokContext("function", false, false, null, true)
|
---|
2290 | };
|
---|
2291 |
|
---|
2292 | var pp$6 = Parser.prototype;
|
---|
2293 |
|
---|
2294 | pp$6.initialContext = function() {
|
---|
2295 | return [types.b_stat]
|
---|
2296 | };
|
---|
2297 |
|
---|
2298 | pp$6.curContext = function() {
|
---|
2299 | return this.context[this.context.length - 1]
|
---|
2300 | };
|
---|
2301 |
|
---|
2302 | pp$6.braceIsBlock = function(prevType) {
|
---|
2303 | var parent = this.curContext();
|
---|
2304 | if (parent === types.f_expr || parent === types.f_stat)
|
---|
2305 | { return true }
|
---|
2306 | if (prevType === types$1.colon && (parent === types.b_stat || parent === types.b_expr))
|
---|
2307 | { return !parent.isExpr }
|
---|
2308 |
|
---|
2309 | // The check for `tt.name && exprAllowed` detects whether we are
|
---|
2310 | // after a `yield` or `of` construct. See the `updateContext` for
|
---|
2311 | // `tt.name`.
|
---|
2312 | if (prevType === types$1._return || prevType === types$1.name && this.exprAllowed)
|
---|
2313 | { return lineBreak.test(this.input.slice(this.lastTokEnd, this.start)) }
|
---|
2314 | if (prevType === types$1._else || prevType === types$1.semi || prevType === types$1.eof || prevType === types$1.parenR || prevType === types$1.arrow)
|
---|
2315 | { return true }
|
---|
2316 | if (prevType === types$1.braceL)
|
---|
2317 | { return parent === types.b_stat }
|
---|
2318 | if (prevType === types$1._var || prevType === types$1._const || prevType === types$1.name)
|
---|
2319 | { return false }
|
---|
2320 | return !this.exprAllowed
|
---|
2321 | };
|
---|
2322 |
|
---|
2323 | pp$6.inGeneratorContext = function() {
|
---|
2324 | for (var i = this.context.length - 1; i >= 1; i--) {
|
---|
2325 | var context = this.context[i];
|
---|
2326 | if (context.token === "function")
|
---|
2327 | { return context.generator }
|
---|
2328 | }
|
---|
2329 | return false
|
---|
2330 | };
|
---|
2331 |
|
---|
2332 | pp$6.updateContext = function(prevType) {
|
---|
2333 | var update, type = this.type;
|
---|
2334 | if (type.keyword && prevType === types$1.dot)
|
---|
2335 | { this.exprAllowed = false; }
|
---|
2336 | else if (update = type.updateContext)
|
---|
2337 | { update.call(this, prevType); }
|
---|
2338 | else
|
---|
2339 | { this.exprAllowed = type.beforeExpr; }
|
---|
2340 | };
|
---|
2341 |
|
---|
2342 | // Used to handle edge cases when token context could not be inferred correctly during tokenization phase
|
---|
2343 |
|
---|
2344 | pp$6.overrideContext = function(tokenCtx) {
|
---|
2345 | if (this.curContext() !== tokenCtx) {
|
---|
2346 | this.context[this.context.length - 1] = tokenCtx;
|
---|
2347 | }
|
---|
2348 | };
|
---|
2349 |
|
---|
2350 | // Token-specific context update code
|
---|
2351 |
|
---|
2352 | types$1.parenR.updateContext = types$1.braceR.updateContext = function() {
|
---|
2353 | if (this.context.length === 1) {
|
---|
2354 | this.exprAllowed = true;
|
---|
2355 | return
|
---|
2356 | }
|
---|
2357 | var out = this.context.pop();
|
---|
2358 | if (out === types.b_stat && this.curContext().token === "function") {
|
---|
2359 | out = this.context.pop();
|
---|
2360 | }
|
---|
2361 | this.exprAllowed = !out.isExpr;
|
---|
2362 | };
|
---|
2363 |
|
---|
2364 | types$1.braceL.updateContext = function(prevType) {
|
---|
2365 | this.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr);
|
---|
2366 | this.exprAllowed = true;
|
---|
2367 | };
|
---|
2368 |
|
---|
2369 | types$1.dollarBraceL.updateContext = function() {
|
---|
2370 | this.context.push(types.b_tmpl);
|
---|
2371 | this.exprAllowed = true;
|
---|
2372 | };
|
---|
2373 |
|
---|
2374 | types$1.parenL.updateContext = function(prevType) {
|
---|
2375 | var statementParens = prevType === types$1._if || prevType === types$1._for || prevType === types$1._with || prevType === types$1._while;
|
---|
2376 | this.context.push(statementParens ? types.p_stat : types.p_expr);
|
---|
2377 | this.exprAllowed = true;
|
---|
2378 | };
|
---|
2379 |
|
---|
2380 | types$1.incDec.updateContext = function() {
|
---|
2381 | // tokExprAllowed stays unchanged
|
---|
2382 | };
|
---|
2383 |
|
---|
2384 | types$1._function.updateContext = types$1._class.updateContext = function(prevType) {
|
---|
2385 | if (prevType.beforeExpr && prevType !== types$1._else &&
|
---|
2386 | !(prevType === types$1.semi && this.curContext() !== types.p_stat) &&
|
---|
2387 | !(prevType === types$1._return && lineBreak.test(this.input.slice(this.lastTokEnd, this.start))) &&
|
---|
2388 | !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types.b_stat))
|
---|
2389 | { this.context.push(types.f_expr); }
|
---|
2390 | else
|
---|
2391 | { this.context.push(types.f_stat); }
|
---|
2392 | this.exprAllowed = false;
|
---|
2393 | };
|
---|
2394 |
|
---|
2395 | types$1.colon.updateContext = function() {
|
---|
2396 | if (this.curContext().token === "function") { this.context.pop(); }
|
---|
2397 | this.exprAllowed = true;
|
---|
2398 | };
|
---|
2399 |
|
---|
2400 | types$1.backQuote.updateContext = function() {
|
---|
2401 | if (this.curContext() === types.q_tmpl)
|
---|
2402 | { this.context.pop(); }
|
---|
2403 | else
|
---|
2404 | { this.context.push(types.q_tmpl); }
|
---|
2405 | this.exprAllowed = false;
|
---|
2406 | };
|
---|
2407 |
|
---|
2408 | types$1.star.updateContext = function(prevType) {
|
---|
2409 | if (prevType === types$1._function) {
|
---|
2410 | var index = this.context.length - 1;
|
---|
2411 | if (this.context[index] === types.f_expr)
|
---|
2412 | { this.context[index] = types.f_expr_gen; }
|
---|
2413 | else
|
---|
2414 | { this.context[index] = types.f_gen; }
|
---|
2415 | }
|
---|
2416 | this.exprAllowed = true;
|
---|
2417 | };
|
---|
2418 |
|
---|
2419 | types$1.name.updateContext = function(prevType) {
|
---|
2420 | var allowed = false;
|
---|
2421 | if (this.options.ecmaVersion >= 6 && prevType !== types$1.dot) {
|
---|
2422 | if (this.value === "of" && !this.exprAllowed ||
|
---|
2423 | this.value === "yield" && this.inGeneratorContext())
|
---|
2424 | { allowed = true; }
|
---|
2425 | }
|
---|
2426 | this.exprAllowed = allowed;
|
---|
2427 | };
|
---|
2428 |
|
---|
2429 | // A recursive descent parser operates by defining functions for all
|
---|
2430 | // syntactic elements, and recursively calling those, each function
|
---|
2431 | // advancing the input stream and returning an AST node. Precedence
|
---|
2432 | // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
|
---|
2433 | // instead of `(!x)[1]` is handled by the fact that the parser
|
---|
2434 | // function that parses unary prefix operators is called first, and
|
---|
2435 | // in turn calls the function that parses `[]` subscripts — that
|
---|
2436 | // way, it'll receive the node for `x[1]` already parsed, and wraps
|
---|
2437 | // *that* in the unary operator node.
|
---|
2438 | //
|
---|
2439 | // Acorn uses an [operator precedence parser][opp] to handle binary
|
---|
2440 | // operator precedence, because it is much more compact than using
|
---|
2441 | // the technique outlined above, which uses different, nesting
|
---|
2442 | // functions to specify precedence, for all of the ten binary
|
---|
2443 | // precedence levels that JavaScript defines.
|
---|
2444 | //
|
---|
2445 | // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
|
---|
2446 |
|
---|
2447 |
|
---|
2448 | var pp$5 = Parser.prototype;
|
---|
2449 |
|
---|
2450 | // Check if property name clashes with already added.
|
---|
2451 | // Object/class getters and setters are not allowed to clash —
|
---|
2452 | // either with each other or with an init property — and in
|
---|
2453 | // strict mode, init properties are also not allowed to be repeated.
|
---|
2454 |
|
---|
2455 | pp$5.checkPropClash = function(prop, propHash, refDestructuringErrors) {
|
---|
2456 | if (this.options.ecmaVersion >= 9 && prop.type === "SpreadElement")
|
---|
2457 | { return }
|
---|
2458 | if (this.options.ecmaVersion >= 6 && (prop.computed || prop.method || prop.shorthand))
|
---|
2459 | { return }
|
---|
2460 | var key = prop.key;
|
---|
2461 | var name;
|
---|
2462 | switch (key.type) {
|
---|
2463 | case "Identifier": name = key.name; break
|
---|
2464 | case "Literal": name = String(key.value); break
|
---|
2465 | default: return
|
---|
2466 | }
|
---|
2467 | var kind = prop.kind;
|
---|
2468 | if (this.options.ecmaVersion >= 6) {
|
---|
2469 | if (name === "__proto__" && kind === "init") {
|
---|
2470 | if (propHash.proto) {
|
---|
2471 | if (refDestructuringErrors) {
|
---|
2472 | if (refDestructuringErrors.doubleProto < 0) {
|
---|
2473 | refDestructuringErrors.doubleProto = key.start;
|
---|
2474 | }
|
---|
2475 | } else {
|
---|
2476 | this.raiseRecoverable(key.start, "Redefinition of __proto__ property");
|
---|
2477 | }
|
---|
2478 | }
|
---|
2479 | propHash.proto = true;
|
---|
2480 | }
|
---|
2481 | return
|
---|
2482 | }
|
---|
2483 | name = "$" + name;
|
---|
2484 | var other = propHash[name];
|
---|
2485 | if (other) {
|
---|
2486 | var redefinition;
|
---|
2487 | if (kind === "init") {
|
---|
2488 | redefinition = this.strict && other.init || other.get || other.set;
|
---|
2489 | } else {
|
---|
2490 | redefinition = other.init || other[kind];
|
---|
2491 | }
|
---|
2492 | if (redefinition)
|
---|
2493 | { this.raiseRecoverable(key.start, "Redefinition of property"); }
|
---|
2494 | } else {
|
---|
2495 | other = propHash[name] = {
|
---|
2496 | init: false,
|
---|
2497 | get: false,
|
---|
2498 | set: false
|
---|
2499 | };
|
---|
2500 | }
|
---|
2501 | other[kind] = true;
|
---|
2502 | };
|
---|
2503 |
|
---|
2504 | // ### Expression parsing
|
---|
2505 |
|
---|
2506 | // These nest, from the most general expression type at the top to
|
---|
2507 | // 'atomic', nondivisible expression types at the bottom. Most of
|
---|
2508 | // the functions will simply let the function(s) below them parse,
|
---|
2509 | // and, *if* the syntactic construct they handle is present, wrap
|
---|
2510 | // the AST node that the inner parser gave them in another node.
|
---|
2511 |
|
---|
2512 | // Parse a full expression. The optional arguments are used to
|
---|
2513 | // forbid the `in` operator (in for loops initalization expressions)
|
---|
2514 | // and provide reference for storing '=' operator inside shorthand
|
---|
2515 | // property assignment in contexts where both object expression
|
---|
2516 | // and object pattern might appear (so it's possible to raise
|
---|
2517 | // delayed syntax error at correct position).
|
---|
2518 |
|
---|
2519 | pp$5.parseExpression = function(forInit, refDestructuringErrors) {
|
---|
2520 | var startPos = this.start, startLoc = this.startLoc;
|
---|
2521 | var expr = this.parseMaybeAssign(forInit, refDestructuringErrors);
|
---|
2522 | if (this.type === types$1.comma) {
|
---|
2523 | var node = this.startNodeAt(startPos, startLoc);
|
---|
2524 | node.expressions = [expr];
|
---|
2525 | while (this.eat(types$1.comma)) { node.expressions.push(this.parseMaybeAssign(forInit, refDestructuringErrors)); }
|
---|
2526 | return this.finishNode(node, "SequenceExpression")
|
---|
2527 | }
|
---|
2528 | return expr
|
---|
2529 | };
|
---|
2530 |
|
---|
2531 | // Parse an assignment expression. This includes applications of
|
---|
2532 | // operators like `+=`.
|
---|
2533 |
|
---|
2534 | pp$5.parseMaybeAssign = function(forInit, refDestructuringErrors, afterLeftParse) {
|
---|
2535 | if (this.isContextual("yield")) {
|
---|
2536 | if (this.inGenerator) { return this.parseYield(forInit) }
|
---|
2537 | // The tokenizer will assume an expression is allowed after
|
---|
2538 | // `yield`, but this isn't that kind of yield
|
---|
2539 | else { this.exprAllowed = false; }
|
---|
2540 | }
|
---|
2541 |
|
---|
2542 | var ownDestructuringErrors = false, oldParenAssign = -1, oldTrailingComma = -1, oldDoubleProto = -1;
|
---|
2543 | if (refDestructuringErrors) {
|
---|
2544 | oldParenAssign = refDestructuringErrors.parenthesizedAssign;
|
---|
2545 | oldTrailingComma = refDestructuringErrors.trailingComma;
|
---|
2546 | oldDoubleProto = refDestructuringErrors.doubleProto;
|
---|
2547 | refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = -1;
|
---|
2548 | } else {
|
---|
2549 | refDestructuringErrors = new DestructuringErrors;
|
---|
2550 | ownDestructuringErrors = true;
|
---|
2551 | }
|
---|
2552 |
|
---|
2553 | var startPos = this.start, startLoc = this.startLoc;
|
---|
2554 | if (this.type === types$1.parenL || this.type === types$1.name) {
|
---|
2555 | this.potentialArrowAt = this.start;
|
---|
2556 | this.potentialArrowInForAwait = forInit === "await";
|
---|
2557 | }
|
---|
2558 | var left = this.parseMaybeConditional(forInit, refDestructuringErrors);
|
---|
2559 | if (afterLeftParse) { left = afterLeftParse.call(this, left, startPos, startLoc); }
|
---|
2560 | if (this.type.isAssign) {
|
---|
2561 | var node = this.startNodeAt(startPos, startLoc);
|
---|
2562 | node.operator = this.value;
|
---|
2563 | if (this.type === types$1.eq)
|
---|
2564 | { left = this.toAssignable(left, false, refDestructuringErrors); }
|
---|
2565 | if (!ownDestructuringErrors) {
|
---|
2566 | refDestructuringErrors.parenthesizedAssign = refDestructuringErrors.trailingComma = refDestructuringErrors.doubleProto = -1;
|
---|
2567 | }
|
---|
2568 | if (refDestructuringErrors.shorthandAssign >= left.start)
|
---|
2569 | { refDestructuringErrors.shorthandAssign = -1; } // reset because shorthand default was used correctly
|
---|
2570 | if (this.type === types$1.eq)
|
---|
2571 | { this.checkLValPattern(left); }
|
---|
2572 | else
|
---|
2573 | { this.checkLValSimple(left); }
|
---|
2574 | node.left = left;
|
---|
2575 | this.next();
|
---|
2576 | node.right = this.parseMaybeAssign(forInit);
|
---|
2577 | if (oldDoubleProto > -1) { refDestructuringErrors.doubleProto = oldDoubleProto; }
|
---|
2578 | return this.finishNode(node, "AssignmentExpression")
|
---|
2579 | } else {
|
---|
2580 | if (ownDestructuringErrors) { this.checkExpressionErrors(refDestructuringErrors, true); }
|
---|
2581 | }
|
---|
2582 | if (oldParenAssign > -1) { refDestructuringErrors.parenthesizedAssign = oldParenAssign; }
|
---|
2583 | if (oldTrailingComma > -1) { refDestructuringErrors.trailingComma = oldTrailingComma; }
|
---|
2584 | return left
|
---|
2585 | };
|
---|
2586 |
|
---|
2587 | // Parse a ternary conditional (`?:`) operator.
|
---|
2588 |
|
---|
2589 | pp$5.parseMaybeConditional = function(forInit, refDestructuringErrors) {
|
---|
2590 | var startPos = this.start, startLoc = this.startLoc;
|
---|
2591 | var expr = this.parseExprOps(forInit, refDestructuringErrors);
|
---|
2592 | if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
|
---|
2593 | if (this.eat(types$1.question)) {
|
---|
2594 | var node = this.startNodeAt(startPos, startLoc);
|
---|
2595 | node.test = expr;
|
---|
2596 | node.consequent = this.parseMaybeAssign();
|
---|
2597 | this.expect(types$1.colon);
|
---|
2598 | node.alternate = this.parseMaybeAssign(forInit);
|
---|
2599 | return this.finishNode(node, "ConditionalExpression")
|
---|
2600 | }
|
---|
2601 | return expr
|
---|
2602 | };
|
---|
2603 |
|
---|
2604 | // Start the precedence parser.
|
---|
2605 |
|
---|
2606 | pp$5.parseExprOps = function(forInit, refDestructuringErrors) {
|
---|
2607 | var startPos = this.start, startLoc = this.startLoc;
|
---|
2608 | var expr = this.parseMaybeUnary(refDestructuringErrors, false, false, forInit);
|
---|
2609 | if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
|
---|
2610 | return expr.start === startPos && expr.type === "ArrowFunctionExpression" ? expr : this.parseExprOp(expr, startPos, startLoc, -1, forInit)
|
---|
2611 | };
|
---|
2612 |
|
---|
2613 | // Parse binary operators with the operator precedence parsing
|
---|
2614 | // algorithm. `left` is the left-hand side of the operator.
|
---|
2615 | // `minPrec` provides context that allows the function to stop and
|
---|
2616 | // defer further parser to one of its callers when it encounters an
|
---|
2617 | // operator that has a lower precedence than the set it is parsing.
|
---|
2618 |
|
---|
2619 | pp$5.parseExprOp = function(left, leftStartPos, leftStartLoc, minPrec, forInit) {
|
---|
2620 | var prec = this.type.binop;
|
---|
2621 | if (prec != null && (!forInit || this.type !== types$1._in)) {
|
---|
2622 | if (prec > minPrec) {
|
---|
2623 | var logical = this.type === types$1.logicalOR || this.type === types$1.logicalAND;
|
---|
2624 | var coalesce = this.type === types$1.coalesce;
|
---|
2625 | if (coalesce) {
|
---|
2626 | // Handle the precedence of `tt.coalesce` as equal to the range of logical expressions.
|
---|
2627 | // In other words, `node.right` shouldn't contain logical expressions in order to check the mixed error.
|
---|
2628 | prec = types$1.logicalAND.binop;
|
---|
2629 | }
|
---|
2630 | var op = this.value;
|
---|
2631 | this.next();
|
---|
2632 | var startPos = this.start, startLoc = this.startLoc;
|
---|
2633 | var right = this.parseExprOp(this.parseMaybeUnary(null, false, false, forInit), startPos, startLoc, prec, forInit);
|
---|
2634 | var node = this.buildBinary(leftStartPos, leftStartLoc, left, right, op, logical || coalesce);
|
---|
2635 | if ((logical && this.type === types$1.coalesce) || (coalesce && (this.type === types$1.logicalOR || this.type === types$1.logicalAND))) {
|
---|
2636 | this.raiseRecoverable(this.start, "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses");
|
---|
2637 | }
|
---|
2638 | return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, forInit)
|
---|
2639 | }
|
---|
2640 | }
|
---|
2641 | return left
|
---|
2642 | };
|
---|
2643 |
|
---|
2644 | pp$5.buildBinary = function(startPos, startLoc, left, right, op, logical) {
|
---|
2645 | if (right.type === "PrivateIdentifier") { this.raise(right.start, "Private identifier can only be left side of binary expression"); }
|
---|
2646 | var node = this.startNodeAt(startPos, startLoc);
|
---|
2647 | node.left = left;
|
---|
2648 | node.operator = op;
|
---|
2649 | node.right = right;
|
---|
2650 | return this.finishNode(node, logical ? "LogicalExpression" : "BinaryExpression")
|
---|
2651 | };
|
---|
2652 |
|
---|
2653 | // Parse unary operators, both prefix and postfix.
|
---|
2654 |
|
---|
2655 | pp$5.parseMaybeUnary = function(refDestructuringErrors, sawUnary, incDec, forInit) {
|
---|
2656 | var startPos = this.start, startLoc = this.startLoc, expr;
|
---|
2657 | if (this.isContextual("await") && this.canAwait) {
|
---|
2658 | expr = this.parseAwait(forInit);
|
---|
2659 | sawUnary = true;
|
---|
2660 | } else if (this.type.prefix) {
|
---|
2661 | var node = this.startNode(), update = this.type === types$1.incDec;
|
---|
2662 | node.operator = this.value;
|
---|
2663 | node.prefix = true;
|
---|
2664 | this.next();
|
---|
2665 | node.argument = this.parseMaybeUnary(null, true, update, forInit);
|
---|
2666 | this.checkExpressionErrors(refDestructuringErrors, true);
|
---|
2667 | if (update) { this.checkLValSimple(node.argument); }
|
---|
2668 | else if (this.strict && node.operator === "delete" && isLocalVariableAccess(node.argument))
|
---|
2669 | { this.raiseRecoverable(node.start, "Deleting local variable in strict mode"); }
|
---|
2670 | else if (node.operator === "delete" && isPrivateFieldAccess(node.argument))
|
---|
2671 | { this.raiseRecoverable(node.start, "Private fields can not be deleted"); }
|
---|
2672 | else { sawUnary = true; }
|
---|
2673 | expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
|
---|
2674 | } else if (!sawUnary && this.type === types$1.privateId) {
|
---|
2675 | if ((forInit || this.privateNameStack.length === 0) && this.options.checkPrivateFields) { this.unexpected(); }
|
---|
2676 | expr = this.parsePrivateIdent();
|
---|
2677 | // only could be private fields in 'in', such as #x in obj
|
---|
2678 | if (this.type !== types$1._in) { this.unexpected(); }
|
---|
2679 | } else {
|
---|
2680 | expr = this.parseExprSubscripts(refDestructuringErrors, forInit);
|
---|
2681 | if (this.checkExpressionErrors(refDestructuringErrors)) { return expr }
|
---|
2682 | while (this.type.postfix && !this.canInsertSemicolon()) {
|
---|
2683 | var node$1 = this.startNodeAt(startPos, startLoc);
|
---|
2684 | node$1.operator = this.value;
|
---|
2685 | node$1.prefix = false;
|
---|
2686 | node$1.argument = expr;
|
---|
2687 | this.checkLValSimple(expr);
|
---|
2688 | this.next();
|
---|
2689 | expr = this.finishNode(node$1, "UpdateExpression");
|
---|
2690 | }
|
---|
2691 | }
|
---|
2692 |
|
---|
2693 | if (!incDec && this.eat(types$1.starstar)) {
|
---|
2694 | if (sawUnary)
|
---|
2695 | { this.unexpected(this.lastTokStart); }
|
---|
2696 | else
|
---|
2697 | { return this.buildBinary(startPos, startLoc, expr, this.parseMaybeUnary(null, false, false, forInit), "**", false) }
|
---|
2698 | } else {
|
---|
2699 | return expr
|
---|
2700 | }
|
---|
2701 | };
|
---|
2702 |
|
---|
2703 | function isLocalVariableAccess(node) {
|
---|
2704 | return (
|
---|
2705 | node.type === "Identifier" ||
|
---|
2706 | node.type === "ParenthesizedExpression" && isLocalVariableAccess(node.expression)
|
---|
2707 | )
|
---|
2708 | }
|
---|
2709 |
|
---|
2710 | function isPrivateFieldAccess(node) {
|
---|
2711 | return (
|
---|
2712 | node.type === "MemberExpression" && node.property.type === "PrivateIdentifier" ||
|
---|
2713 | node.type === "ChainExpression" && isPrivateFieldAccess(node.expression) ||
|
---|
2714 | node.type === "ParenthesizedExpression" && isPrivateFieldAccess(node.expression)
|
---|
2715 | )
|
---|
2716 | }
|
---|
2717 |
|
---|
2718 | // Parse call, dot, and `[]`-subscript expressions.
|
---|
2719 |
|
---|
2720 | pp$5.parseExprSubscripts = function(refDestructuringErrors, forInit) {
|
---|
2721 | var startPos = this.start, startLoc = this.startLoc;
|
---|
2722 | var expr = this.parseExprAtom(refDestructuringErrors, forInit);
|
---|
2723 | if (expr.type === "ArrowFunctionExpression" && this.input.slice(this.lastTokStart, this.lastTokEnd) !== ")")
|
---|
2724 | { return expr }
|
---|
2725 | var result = this.parseSubscripts(expr, startPos, startLoc, false, forInit);
|
---|
2726 | if (refDestructuringErrors && result.type === "MemberExpression") {
|
---|
2727 | if (refDestructuringErrors.parenthesizedAssign >= result.start) { refDestructuringErrors.parenthesizedAssign = -1; }
|
---|
2728 | if (refDestructuringErrors.parenthesizedBind >= result.start) { refDestructuringErrors.parenthesizedBind = -1; }
|
---|
2729 | if (refDestructuringErrors.trailingComma >= result.start) { refDestructuringErrors.trailingComma = -1; }
|
---|
2730 | }
|
---|
2731 | return result
|
---|
2732 | };
|
---|
2733 |
|
---|
2734 | pp$5.parseSubscripts = function(base, startPos, startLoc, noCalls, forInit) {
|
---|
2735 | var maybeAsyncArrow = this.options.ecmaVersion >= 8 && base.type === "Identifier" && base.name === "async" &&
|
---|
2736 | this.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 &&
|
---|
2737 | this.potentialArrowAt === base.start;
|
---|
2738 | var optionalChained = false;
|
---|
2739 |
|
---|
2740 | while (true) {
|
---|
2741 | var element = this.parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit);
|
---|
2742 |
|
---|
2743 | if (element.optional) { optionalChained = true; }
|
---|
2744 | if (element === base || element.type === "ArrowFunctionExpression") {
|
---|
2745 | if (optionalChained) {
|
---|
2746 | var chainNode = this.startNodeAt(startPos, startLoc);
|
---|
2747 | chainNode.expression = element;
|
---|
2748 | element = this.finishNode(chainNode, "ChainExpression");
|
---|
2749 | }
|
---|
2750 | return element
|
---|
2751 | }
|
---|
2752 |
|
---|
2753 | base = element;
|
---|
2754 | }
|
---|
2755 | };
|
---|
2756 |
|
---|
2757 | pp$5.shouldParseAsyncArrow = function() {
|
---|
2758 | return !this.canInsertSemicolon() && this.eat(types$1.arrow)
|
---|
2759 | };
|
---|
2760 |
|
---|
2761 | pp$5.parseSubscriptAsyncArrow = function(startPos, startLoc, exprList, forInit) {
|
---|
2762 | return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList, true, forInit)
|
---|
2763 | };
|
---|
2764 |
|
---|
2765 | pp$5.parseSubscript = function(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
|
---|
2766 | var optionalSupported = this.options.ecmaVersion >= 11;
|
---|
2767 | var optional = optionalSupported && this.eat(types$1.questionDot);
|
---|
2768 | if (noCalls && optional) { this.raise(this.lastTokStart, "Optional chaining cannot appear in the callee of new expressions"); }
|
---|
2769 |
|
---|
2770 | var computed = this.eat(types$1.bracketL);
|
---|
2771 | if (computed || (optional && this.type !== types$1.parenL && this.type !== types$1.backQuote) || this.eat(types$1.dot)) {
|
---|
2772 | var node = this.startNodeAt(startPos, startLoc);
|
---|
2773 | node.object = base;
|
---|
2774 | if (computed) {
|
---|
2775 | node.property = this.parseExpression();
|
---|
2776 | this.expect(types$1.bracketR);
|
---|
2777 | } else if (this.type === types$1.privateId && base.type !== "Super") {
|
---|
2778 | node.property = this.parsePrivateIdent();
|
---|
2779 | } else {
|
---|
2780 | node.property = this.parseIdent(this.options.allowReserved !== "never");
|
---|
2781 | }
|
---|
2782 | node.computed = !!computed;
|
---|
2783 | if (optionalSupported) {
|
---|
2784 | node.optional = optional;
|
---|
2785 | }
|
---|
2786 | base = this.finishNode(node, "MemberExpression");
|
---|
2787 | } else if (!noCalls && this.eat(types$1.parenL)) {
|
---|
2788 | var refDestructuringErrors = new DestructuringErrors, oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
|
---|
2789 | this.yieldPos = 0;
|
---|
2790 | this.awaitPos = 0;
|
---|
2791 | this.awaitIdentPos = 0;
|
---|
2792 | var exprList = this.parseExprList(types$1.parenR, this.options.ecmaVersion >= 8, false, refDestructuringErrors);
|
---|
2793 | if (maybeAsyncArrow && !optional && this.shouldParseAsyncArrow()) {
|
---|
2794 | this.checkPatternErrors(refDestructuringErrors, false);
|
---|
2795 | this.checkYieldAwaitInDefaultParams();
|
---|
2796 | if (this.awaitIdentPos > 0)
|
---|
2797 | { this.raise(this.awaitIdentPos, "Cannot use 'await' as identifier inside an async function"); }
|
---|
2798 | this.yieldPos = oldYieldPos;
|
---|
2799 | this.awaitPos = oldAwaitPos;
|
---|
2800 | this.awaitIdentPos = oldAwaitIdentPos;
|
---|
2801 | return this.parseSubscriptAsyncArrow(startPos, startLoc, exprList, forInit)
|
---|
2802 | }
|
---|
2803 | this.checkExpressionErrors(refDestructuringErrors, true);
|
---|
2804 | this.yieldPos = oldYieldPos || this.yieldPos;
|
---|
2805 | this.awaitPos = oldAwaitPos || this.awaitPos;
|
---|
2806 | this.awaitIdentPos = oldAwaitIdentPos || this.awaitIdentPos;
|
---|
2807 | var node$1 = this.startNodeAt(startPos, startLoc);
|
---|
2808 | node$1.callee = base;
|
---|
2809 | node$1.arguments = exprList;
|
---|
2810 | if (optionalSupported) {
|
---|
2811 | node$1.optional = optional;
|
---|
2812 | }
|
---|
2813 | base = this.finishNode(node$1, "CallExpression");
|
---|
2814 | } else if (this.type === types$1.backQuote) {
|
---|
2815 | if (optional || optionalChained) {
|
---|
2816 | this.raise(this.start, "Optional chaining cannot appear in the tag of tagged template expressions");
|
---|
2817 | }
|
---|
2818 | var node$2 = this.startNodeAt(startPos, startLoc);
|
---|
2819 | node$2.tag = base;
|
---|
2820 | node$2.quasi = this.parseTemplate({isTagged: true});
|
---|
2821 | base = this.finishNode(node$2, "TaggedTemplateExpression");
|
---|
2822 | }
|
---|
2823 | return base
|
---|
2824 | };
|
---|
2825 |
|
---|
2826 | // Parse an atomic expression — either a single token that is an
|
---|
2827 | // expression, an expression started by a keyword like `function` or
|
---|
2828 | // `new`, or an expression wrapped in punctuation like `()`, `[]`,
|
---|
2829 | // or `{}`.
|
---|
2830 |
|
---|
2831 | pp$5.parseExprAtom = function(refDestructuringErrors, forInit, forNew) {
|
---|
2832 | // If a division operator appears in an expression position, the
|
---|
2833 | // tokenizer got confused, and we force it to read a regexp instead.
|
---|
2834 | if (this.type === types$1.slash) { this.readRegexp(); }
|
---|
2835 |
|
---|
2836 | var node, canBeArrow = this.potentialArrowAt === this.start;
|
---|
2837 | switch (this.type) {
|
---|
2838 | case types$1._super:
|
---|
2839 | if (!this.allowSuper)
|
---|
2840 | { this.raise(this.start, "'super' keyword outside a method"); }
|
---|
2841 | node = this.startNode();
|
---|
2842 | this.next();
|
---|
2843 | if (this.type === types$1.parenL && !this.allowDirectSuper)
|
---|
2844 | { this.raise(node.start, "super() call outside constructor of a subclass"); }
|
---|
2845 | // The `super` keyword can appear at below:
|
---|
2846 | // SuperProperty:
|
---|
2847 | // super [ Expression ]
|
---|
2848 | // super . IdentifierName
|
---|
2849 | // SuperCall:
|
---|
2850 | // super ( Arguments )
|
---|
2851 | if (this.type !== types$1.dot && this.type !== types$1.bracketL && this.type !== types$1.parenL)
|
---|
2852 | { this.unexpected(); }
|
---|
2853 | return this.finishNode(node, "Super")
|
---|
2854 |
|
---|
2855 | case types$1._this:
|
---|
2856 | node = this.startNode();
|
---|
2857 | this.next();
|
---|
2858 | return this.finishNode(node, "ThisExpression")
|
---|
2859 |
|
---|
2860 | case types$1.name:
|
---|
2861 | var startPos = this.start, startLoc = this.startLoc, containsEsc = this.containsEsc;
|
---|
2862 | var id = this.parseIdent(false);
|
---|
2863 | if (this.options.ecmaVersion >= 8 && !containsEsc && id.name === "async" && !this.canInsertSemicolon() && this.eat(types$1._function)) {
|
---|
2864 | this.overrideContext(types.f_expr);
|
---|
2865 | return this.parseFunction(this.startNodeAt(startPos, startLoc), 0, false, true, forInit)
|
---|
2866 | }
|
---|
2867 | if (canBeArrow && !this.canInsertSemicolon()) {
|
---|
2868 | if (this.eat(types$1.arrow))
|
---|
2869 | { return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], false, forInit) }
|
---|
2870 | if (this.options.ecmaVersion >= 8 && id.name === "async" && this.type === types$1.name && !containsEsc &&
|
---|
2871 | (!this.potentialArrowInForAwait || this.value !== "of" || this.containsEsc)) {
|
---|
2872 | id = this.parseIdent(false);
|
---|
2873 | if (this.canInsertSemicolon() || !this.eat(types$1.arrow))
|
---|
2874 | { this.unexpected(); }
|
---|
2875 | return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), [id], true, forInit)
|
---|
2876 | }
|
---|
2877 | }
|
---|
2878 | return id
|
---|
2879 |
|
---|
2880 | case types$1.regexp:
|
---|
2881 | var value = this.value;
|
---|
2882 | node = this.parseLiteral(value.value);
|
---|
2883 | node.regex = {pattern: value.pattern, flags: value.flags};
|
---|
2884 | return node
|
---|
2885 |
|
---|
2886 | case types$1.num: case types$1.string:
|
---|
2887 | return this.parseLiteral(this.value)
|
---|
2888 |
|
---|
2889 | case types$1._null: case types$1._true: case types$1._false:
|
---|
2890 | node = this.startNode();
|
---|
2891 | node.value = this.type === types$1._null ? null : this.type === types$1._true;
|
---|
2892 | node.raw = this.type.keyword;
|
---|
2893 | this.next();
|
---|
2894 | return this.finishNode(node, "Literal")
|
---|
2895 |
|
---|
2896 | case types$1.parenL:
|
---|
2897 | var start = this.start, expr = this.parseParenAndDistinguishExpression(canBeArrow, forInit);
|
---|
2898 | if (refDestructuringErrors) {
|
---|
2899 | if (refDestructuringErrors.parenthesizedAssign < 0 && !this.isSimpleAssignTarget(expr))
|
---|
2900 | { refDestructuringErrors.parenthesizedAssign = start; }
|
---|
2901 | if (refDestructuringErrors.parenthesizedBind < 0)
|
---|
2902 | { refDestructuringErrors.parenthesizedBind = start; }
|
---|
2903 | }
|
---|
2904 | return expr
|
---|
2905 |
|
---|
2906 | case types$1.bracketL:
|
---|
2907 | node = this.startNode();
|
---|
2908 | this.next();
|
---|
2909 | node.elements = this.parseExprList(types$1.bracketR, true, true, refDestructuringErrors);
|
---|
2910 | return this.finishNode(node, "ArrayExpression")
|
---|
2911 |
|
---|
2912 | case types$1.braceL:
|
---|
2913 | this.overrideContext(types.b_expr);
|
---|
2914 | return this.parseObj(false, refDestructuringErrors)
|
---|
2915 |
|
---|
2916 | case types$1._function:
|
---|
2917 | node = this.startNode();
|
---|
2918 | this.next();
|
---|
2919 | return this.parseFunction(node, 0)
|
---|
2920 |
|
---|
2921 | case types$1._class:
|
---|
2922 | return this.parseClass(this.startNode(), false)
|
---|
2923 |
|
---|
2924 | case types$1._new:
|
---|
2925 | return this.parseNew()
|
---|
2926 |
|
---|
2927 | case types$1.backQuote:
|
---|
2928 | return this.parseTemplate()
|
---|
2929 |
|
---|
2930 | case types$1._import:
|
---|
2931 | if (this.options.ecmaVersion >= 11) {
|
---|
2932 | return this.parseExprImport(forNew)
|
---|
2933 | } else {
|
---|
2934 | return this.unexpected()
|
---|
2935 | }
|
---|
2936 |
|
---|
2937 | default:
|
---|
2938 | return this.parseExprAtomDefault()
|
---|
2939 | }
|
---|
2940 | };
|
---|
2941 |
|
---|
2942 | pp$5.parseExprAtomDefault = function() {
|
---|
2943 | this.unexpected();
|
---|
2944 | };
|
---|
2945 |
|
---|
2946 | pp$5.parseExprImport = function(forNew) {
|
---|
2947 | var node = this.startNode();
|
---|
2948 |
|
---|
2949 | // Consume `import` as an identifier for `import.meta`.
|
---|
2950 | // Because `this.parseIdent(true)` doesn't check escape sequences, it needs the check of `this.containsEsc`.
|
---|
2951 | if (this.containsEsc) { this.raiseRecoverable(this.start, "Escape sequence in keyword import"); }
|
---|
2952 | this.next();
|
---|
2953 |
|
---|
2954 | if (this.type === types$1.parenL && !forNew) {
|
---|
2955 | return this.parseDynamicImport(node)
|
---|
2956 | } else if (this.type === types$1.dot) {
|
---|
2957 | var meta = this.startNodeAt(node.start, node.loc && node.loc.start);
|
---|
2958 | meta.name = "import";
|
---|
2959 | node.meta = this.finishNode(meta, "Identifier");
|
---|
2960 | return this.parseImportMeta(node)
|
---|
2961 | } else {
|
---|
2962 | this.unexpected();
|
---|
2963 | }
|
---|
2964 | };
|
---|
2965 |
|
---|
2966 | pp$5.parseDynamicImport = function(node) {
|
---|
2967 | this.next(); // skip `(`
|
---|
2968 |
|
---|
2969 | // Parse node.source.
|
---|
2970 | node.source = this.parseMaybeAssign();
|
---|
2971 |
|
---|
2972 | // Verify ending.
|
---|
2973 | if (!this.eat(types$1.parenR)) {
|
---|
2974 | var errorPos = this.start;
|
---|
2975 | if (this.eat(types$1.comma) && this.eat(types$1.parenR)) {
|
---|
2976 | this.raiseRecoverable(errorPos, "Trailing comma is not allowed in import()");
|
---|
2977 | } else {
|
---|
2978 | this.unexpected(errorPos);
|
---|
2979 | }
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | return this.finishNode(node, "ImportExpression")
|
---|
2983 | };
|
---|
2984 |
|
---|
2985 | pp$5.parseImportMeta = function(node) {
|
---|
2986 | this.next(); // skip `.`
|
---|
2987 |
|
---|
2988 | var containsEsc = this.containsEsc;
|
---|
2989 | node.property = this.parseIdent(true);
|
---|
2990 |
|
---|
2991 | if (node.property.name !== "meta")
|
---|
2992 | { this.raiseRecoverable(node.property.start, "The only valid meta property for import is 'import.meta'"); }
|
---|
2993 | if (containsEsc)
|
---|
2994 | { this.raiseRecoverable(node.start, "'import.meta' must not contain escaped characters"); }
|
---|
2995 | if (this.options.sourceType !== "module" && !this.options.allowImportExportEverywhere)
|
---|
2996 | { this.raiseRecoverable(node.start, "Cannot use 'import.meta' outside a module"); }
|
---|
2997 |
|
---|
2998 | return this.finishNode(node, "MetaProperty")
|
---|
2999 | };
|
---|
3000 |
|
---|
3001 | pp$5.parseLiteral = function(value) {
|
---|
3002 | var node = this.startNode();
|
---|
3003 | node.value = value;
|
---|
3004 | node.raw = this.input.slice(this.start, this.end);
|
---|
3005 | if (node.raw.charCodeAt(node.raw.length - 1) === 110) { node.bigint = node.raw.slice(0, -1).replace(/_/g, ""); }
|
---|
3006 | this.next();
|
---|
3007 | return this.finishNode(node, "Literal")
|
---|
3008 | };
|
---|
3009 |
|
---|
3010 | pp$5.parseParenExpression = function() {
|
---|
3011 | this.expect(types$1.parenL);
|
---|
3012 | var val = this.parseExpression();
|
---|
3013 | this.expect(types$1.parenR);
|
---|
3014 | return val
|
---|
3015 | };
|
---|
3016 |
|
---|
3017 | pp$5.shouldParseArrow = function(exprList) {
|
---|
3018 | return !this.canInsertSemicolon()
|
---|
3019 | };
|
---|
3020 |
|
---|
3021 | pp$5.parseParenAndDistinguishExpression = function(canBeArrow, forInit) {
|
---|
3022 | var startPos = this.start, startLoc = this.startLoc, val, allowTrailingComma = this.options.ecmaVersion >= 8;
|
---|
3023 | if (this.options.ecmaVersion >= 6) {
|
---|
3024 | this.next();
|
---|
3025 |
|
---|
3026 | var innerStartPos = this.start, innerStartLoc = this.startLoc;
|
---|
3027 | var exprList = [], first = true, lastIsComma = false;
|
---|
3028 | var refDestructuringErrors = new DestructuringErrors, oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, spreadStart;
|
---|
3029 | this.yieldPos = 0;
|
---|
3030 | this.awaitPos = 0;
|
---|
3031 | // Do not save awaitIdentPos to allow checking awaits nested in parameters
|
---|
3032 | while (this.type !== types$1.parenR) {
|
---|
3033 | first ? first = false : this.expect(types$1.comma);
|
---|
3034 | if (allowTrailingComma && this.afterTrailingComma(types$1.parenR, true)) {
|
---|
3035 | lastIsComma = true;
|
---|
3036 | break
|
---|
3037 | } else if (this.type === types$1.ellipsis) {
|
---|
3038 | spreadStart = this.start;
|
---|
3039 | exprList.push(this.parseParenItem(this.parseRestBinding()));
|
---|
3040 | if (this.type === types$1.comma) {
|
---|
3041 | this.raiseRecoverable(
|
---|
3042 | this.start,
|
---|
3043 | "Comma is not permitted after the rest element"
|
---|
3044 | );
|
---|
3045 | }
|
---|
3046 | break
|
---|
3047 | } else {
|
---|
3048 | exprList.push(this.parseMaybeAssign(false, refDestructuringErrors, this.parseParenItem));
|
---|
3049 | }
|
---|
3050 | }
|
---|
3051 | var innerEndPos = this.lastTokEnd, innerEndLoc = this.lastTokEndLoc;
|
---|
3052 | this.expect(types$1.parenR);
|
---|
3053 |
|
---|
3054 | if (canBeArrow && this.shouldParseArrow(exprList) && this.eat(types$1.arrow)) {
|
---|
3055 | this.checkPatternErrors(refDestructuringErrors, false);
|
---|
3056 | this.checkYieldAwaitInDefaultParams();
|
---|
3057 | this.yieldPos = oldYieldPos;
|
---|
3058 | this.awaitPos = oldAwaitPos;
|
---|
3059 | return this.parseParenArrowList(startPos, startLoc, exprList, forInit)
|
---|
3060 | }
|
---|
3061 |
|
---|
3062 | if (!exprList.length || lastIsComma) { this.unexpected(this.lastTokStart); }
|
---|
3063 | if (spreadStart) { this.unexpected(spreadStart); }
|
---|
3064 | this.checkExpressionErrors(refDestructuringErrors, true);
|
---|
3065 | this.yieldPos = oldYieldPos || this.yieldPos;
|
---|
3066 | this.awaitPos = oldAwaitPos || this.awaitPos;
|
---|
3067 |
|
---|
3068 | if (exprList.length > 1) {
|
---|
3069 | val = this.startNodeAt(innerStartPos, innerStartLoc);
|
---|
3070 | val.expressions = exprList;
|
---|
3071 | this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
|
---|
3072 | } else {
|
---|
3073 | val = exprList[0];
|
---|
3074 | }
|
---|
3075 | } else {
|
---|
3076 | val = this.parseParenExpression();
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | if (this.options.preserveParens) {
|
---|
3080 | var par = this.startNodeAt(startPos, startLoc);
|
---|
3081 | par.expression = val;
|
---|
3082 | return this.finishNode(par, "ParenthesizedExpression")
|
---|
3083 | } else {
|
---|
3084 | return val
|
---|
3085 | }
|
---|
3086 | };
|
---|
3087 |
|
---|
3088 | pp$5.parseParenItem = function(item) {
|
---|
3089 | return item
|
---|
3090 | };
|
---|
3091 |
|
---|
3092 | pp$5.parseParenArrowList = function(startPos, startLoc, exprList, forInit) {
|
---|
3093 | return this.parseArrowExpression(this.startNodeAt(startPos, startLoc), exprList, false, forInit)
|
---|
3094 | };
|
---|
3095 |
|
---|
3096 | // New's precedence is slightly tricky. It must allow its argument to
|
---|
3097 | // be a `[]` or dot subscript expression, but not a call — at least,
|
---|
3098 | // not without wrapping it in parentheses. Thus, it uses the noCalls
|
---|
3099 | // argument to parseSubscripts to prevent it from consuming the
|
---|
3100 | // argument list.
|
---|
3101 |
|
---|
3102 | var empty = [];
|
---|
3103 |
|
---|
3104 | pp$5.parseNew = function() {
|
---|
3105 | if (this.containsEsc) { this.raiseRecoverable(this.start, "Escape sequence in keyword new"); }
|
---|
3106 | var node = this.startNode();
|
---|
3107 | this.next();
|
---|
3108 | if (this.options.ecmaVersion >= 6 && this.type === types$1.dot) {
|
---|
3109 | var meta = this.startNodeAt(node.start, node.loc && node.loc.start);
|
---|
3110 | meta.name = "new";
|
---|
3111 | node.meta = this.finishNode(meta, "Identifier");
|
---|
3112 | this.next();
|
---|
3113 | var containsEsc = this.containsEsc;
|
---|
3114 | node.property = this.parseIdent(true);
|
---|
3115 | if (node.property.name !== "target")
|
---|
3116 | { this.raiseRecoverable(node.property.start, "The only valid meta property for new is 'new.target'"); }
|
---|
3117 | if (containsEsc)
|
---|
3118 | { this.raiseRecoverable(node.start, "'new.target' must not contain escaped characters"); }
|
---|
3119 | if (!this.allowNewDotTarget)
|
---|
3120 | { this.raiseRecoverable(node.start, "'new.target' can only be used in functions and class static block"); }
|
---|
3121 | return this.finishNode(node, "MetaProperty")
|
---|
3122 | }
|
---|
3123 | var startPos = this.start, startLoc = this.startLoc;
|
---|
3124 | node.callee = this.parseSubscripts(this.parseExprAtom(null, false, true), startPos, startLoc, true, false);
|
---|
3125 | if (this.eat(types$1.parenL)) { node.arguments = this.parseExprList(types$1.parenR, this.options.ecmaVersion >= 8, false); }
|
---|
3126 | else { node.arguments = empty; }
|
---|
3127 | return this.finishNode(node, "NewExpression")
|
---|
3128 | };
|
---|
3129 |
|
---|
3130 | // Parse template expression.
|
---|
3131 |
|
---|
3132 | pp$5.parseTemplateElement = function(ref) {
|
---|
3133 | var isTagged = ref.isTagged;
|
---|
3134 |
|
---|
3135 | var elem = this.startNode();
|
---|
3136 | if (this.type === types$1.invalidTemplate) {
|
---|
3137 | if (!isTagged) {
|
---|
3138 | this.raiseRecoverable(this.start, "Bad escape sequence in untagged template literal");
|
---|
3139 | }
|
---|
3140 | elem.value = {
|
---|
3141 | raw: this.value.replace(/\r\n?/g, "\n"),
|
---|
3142 | cooked: null
|
---|
3143 | };
|
---|
3144 | } else {
|
---|
3145 | elem.value = {
|
---|
3146 | raw: this.input.slice(this.start, this.end).replace(/\r\n?/g, "\n"),
|
---|
3147 | cooked: this.value
|
---|
3148 | };
|
---|
3149 | }
|
---|
3150 | this.next();
|
---|
3151 | elem.tail = this.type === types$1.backQuote;
|
---|
3152 | return this.finishNode(elem, "TemplateElement")
|
---|
3153 | };
|
---|
3154 |
|
---|
3155 | pp$5.parseTemplate = function(ref) {
|
---|
3156 | if ( ref === void 0 ) ref = {};
|
---|
3157 | var isTagged = ref.isTagged; if ( isTagged === void 0 ) isTagged = false;
|
---|
3158 |
|
---|
3159 | var node = this.startNode();
|
---|
3160 | this.next();
|
---|
3161 | node.expressions = [];
|
---|
3162 | var curElt = this.parseTemplateElement({isTagged: isTagged});
|
---|
3163 | node.quasis = [curElt];
|
---|
3164 | while (!curElt.tail) {
|
---|
3165 | if (this.type === types$1.eof) { this.raise(this.pos, "Unterminated template literal"); }
|
---|
3166 | this.expect(types$1.dollarBraceL);
|
---|
3167 | node.expressions.push(this.parseExpression());
|
---|
3168 | this.expect(types$1.braceR);
|
---|
3169 | node.quasis.push(curElt = this.parseTemplateElement({isTagged: isTagged}));
|
---|
3170 | }
|
---|
3171 | this.next();
|
---|
3172 | return this.finishNode(node, "TemplateLiteral")
|
---|
3173 | };
|
---|
3174 |
|
---|
3175 | pp$5.isAsyncProp = function(prop) {
|
---|
3176 | return !prop.computed && prop.key.type === "Identifier" && prop.key.name === "async" &&
|
---|
3177 | (this.type === types$1.name || this.type === types$1.num || this.type === types$1.string || this.type === types$1.bracketL || this.type.keyword || (this.options.ecmaVersion >= 9 && this.type === types$1.star)) &&
|
---|
3178 | !lineBreak.test(this.input.slice(this.lastTokEnd, this.start))
|
---|
3179 | };
|
---|
3180 |
|
---|
3181 | // Parse an object literal or binding pattern.
|
---|
3182 |
|
---|
3183 | pp$5.parseObj = function(isPattern, refDestructuringErrors) {
|
---|
3184 | var node = this.startNode(), first = true, propHash = {};
|
---|
3185 | node.properties = [];
|
---|
3186 | this.next();
|
---|
3187 | while (!this.eat(types$1.braceR)) {
|
---|
3188 | if (!first) {
|
---|
3189 | this.expect(types$1.comma);
|
---|
3190 | if (this.options.ecmaVersion >= 5 && this.afterTrailingComma(types$1.braceR)) { break }
|
---|
3191 | } else { first = false; }
|
---|
3192 |
|
---|
3193 | var prop = this.parseProperty(isPattern, refDestructuringErrors);
|
---|
3194 | if (!isPattern) { this.checkPropClash(prop, propHash, refDestructuringErrors); }
|
---|
3195 | node.properties.push(prop);
|
---|
3196 | }
|
---|
3197 | return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression")
|
---|
3198 | };
|
---|
3199 |
|
---|
3200 | pp$5.parseProperty = function(isPattern, refDestructuringErrors) {
|
---|
3201 | var prop = this.startNode(), isGenerator, isAsync, startPos, startLoc;
|
---|
3202 | if (this.options.ecmaVersion >= 9 && this.eat(types$1.ellipsis)) {
|
---|
3203 | if (isPattern) {
|
---|
3204 | prop.argument = this.parseIdent(false);
|
---|
3205 | if (this.type === types$1.comma) {
|
---|
3206 | this.raiseRecoverable(this.start, "Comma is not permitted after the rest element");
|
---|
3207 | }
|
---|
3208 | return this.finishNode(prop, "RestElement")
|
---|
3209 | }
|
---|
3210 | // Parse argument.
|
---|
3211 | prop.argument = this.parseMaybeAssign(false, refDestructuringErrors);
|
---|
3212 | // To disallow trailing comma via `this.toAssignable()`.
|
---|
3213 | if (this.type === types$1.comma && refDestructuringErrors && refDestructuringErrors.trailingComma < 0) {
|
---|
3214 | refDestructuringErrors.trailingComma = this.start;
|
---|
3215 | }
|
---|
3216 | // Finish
|
---|
3217 | return this.finishNode(prop, "SpreadElement")
|
---|
3218 | }
|
---|
3219 | if (this.options.ecmaVersion >= 6) {
|
---|
3220 | prop.method = false;
|
---|
3221 | prop.shorthand = false;
|
---|
3222 | if (isPattern || refDestructuringErrors) {
|
---|
3223 | startPos = this.start;
|
---|
3224 | startLoc = this.startLoc;
|
---|
3225 | }
|
---|
3226 | if (!isPattern)
|
---|
3227 | { isGenerator = this.eat(types$1.star); }
|
---|
3228 | }
|
---|
3229 | var containsEsc = this.containsEsc;
|
---|
3230 | this.parsePropertyName(prop);
|
---|
3231 | if (!isPattern && !containsEsc && this.options.ecmaVersion >= 8 && !isGenerator && this.isAsyncProp(prop)) {
|
---|
3232 | isAsync = true;
|
---|
3233 | isGenerator = this.options.ecmaVersion >= 9 && this.eat(types$1.star);
|
---|
3234 | this.parsePropertyName(prop);
|
---|
3235 | } else {
|
---|
3236 | isAsync = false;
|
---|
3237 | }
|
---|
3238 | this.parsePropertyValue(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors, containsEsc);
|
---|
3239 | return this.finishNode(prop, "Property")
|
---|
3240 | };
|
---|
3241 |
|
---|
3242 | pp$5.parseGetterSetter = function(prop) {
|
---|
3243 | prop.kind = prop.key.name;
|
---|
3244 | this.parsePropertyName(prop);
|
---|
3245 | prop.value = this.parseMethod(false);
|
---|
3246 | var paramCount = prop.kind === "get" ? 0 : 1;
|
---|
3247 | if (prop.value.params.length !== paramCount) {
|
---|
3248 | var start = prop.value.start;
|
---|
3249 | if (prop.kind === "get")
|
---|
3250 | { this.raiseRecoverable(start, "getter should have no params"); }
|
---|
3251 | else
|
---|
3252 | { this.raiseRecoverable(start, "setter should have exactly one param"); }
|
---|
3253 | } else {
|
---|
3254 | if (prop.kind === "set" && prop.value.params[0].type === "RestElement")
|
---|
3255 | { this.raiseRecoverable(prop.value.params[0].start, "Setter cannot use rest params"); }
|
---|
3256 | }
|
---|
3257 | };
|
---|
3258 |
|
---|
3259 | pp$5.parsePropertyValue = function(prop, isPattern, isGenerator, isAsync, startPos, startLoc, refDestructuringErrors, containsEsc) {
|
---|
3260 | if ((isGenerator || isAsync) && this.type === types$1.colon)
|
---|
3261 | { this.unexpected(); }
|
---|
3262 |
|
---|
3263 | if (this.eat(types$1.colon)) {
|
---|
3264 | prop.value = isPattern ? this.parseMaybeDefault(this.start, this.startLoc) : this.parseMaybeAssign(false, refDestructuringErrors);
|
---|
3265 | prop.kind = "init";
|
---|
3266 | } else if (this.options.ecmaVersion >= 6 && this.type === types$1.parenL) {
|
---|
3267 | if (isPattern) { this.unexpected(); }
|
---|
3268 | prop.kind = "init";
|
---|
3269 | prop.method = true;
|
---|
3270 | prop.value = this.parseMethod(isGenerator, isAsync);
|
---|
3271 | } else if (!isPattern && !containsEsc &&
|
---|
3272 | this.options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
|
---|
3273 | (prop.key.name === "get" || prop.key.name === "set") &&
|
---|
3274 | (this.type !== types$1.comma && this.type !== types$1.braceR && this.type !== types$1.eq)) {
|
---|
3275 | if (isGenerator || isAsync) { this.unexpected(); }
|
---|
3276 | this.parseGetterSetter(prop);
|
---|
3277 | } else if (this.options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
|
---|
3278 | if (isGenerator || isAsync) { this.unexpected(); }
|
---|
3279 | this.checkUnreserved(prop.key);
|
---|
3280 | if (prop.key.name === "await" && !this.awaitIdentPos)
|
---|
3281 | { this.awaitIdentPos = startPos; }
|
---|
3282 | prop.kind = "init";
|
---|
3283 | if (isPattern) {
|
---|
3284 | prop.value = this.parseMaybeDefault(startPos, startLoc, this.copyNode(prop.key));
|
---|
3285 | } else if (this.type === types$1.eq && refDestructuringErrors) {
|
---|
3286 | if (refDestructuringErrors.shorthandAssign < 0)
|
---|
3287 | { refDestructuringErrors.shorthandAssign = this.start; }
|
---|
3288 | prop.value = this.parseMaybeDefault(startPos, startLoc, this.copyNode(prop.key));
|
---|
3289 | } else {
|
---|
3290 | prop.value = this.copyNode(prop.key);
|
---|
3291 | }
|
---|
3292 | prop.shorthand = true;
|
---|
3293 | } else { this.unexpected(); }
|
---|
3294 | };
|
---|
3295 |
|
---|
3296 | pp$5.parsePropertyName = function(prop) {
|
---|
3297 | if (this.options.ecmaVersion >= 6) {
|
---|
3298 | if (this.eat(types$1.bracketL)) {
|
---|
3299 | prop.computed = true;
|
---|
3300 | prop.key = this.parseMaybeAssign();
|
---|
3301 | this.expect(types$1.bracketR);
|
---|
3302 | return prop.key
|
---|
3303 | } else {
|
---|
3304 | prop.computed = false;
|
---|
3305 | }
|
---|
3306 | }
|
---|
3307 | return prop.key = this.type === types$1.num || this.type === types$1.string ? this.parseExprAtom() : this.parseIdent(this.options.allowReserved !== "never")
|
---|
3308 | };
|
---|
3309 |
|
---|
3310 | // Initialize empty function node.
|
---|
3311 |
|
---|
3312 | pp$5.initFunction = function(node) {
|
---|
3313 | node.id = null;
|
---|
3314 | if (this.options.ecmaVersion >= 6) { node.generator = node.expression = false; }
|
---|
3315 | if (this.options.ecmaVersion >= 8) { node.async = false; }
|
---|
3316 | };
|
---|
3317 |
|
---|
3318 | // Parse object or class method.
|
---|
3319 |
|
---|
3320 | pp$5.parseMethod = function(isGenerator, isAsync, allowDirectSuper) {
|
---|
3321 | var node = this.startNode(), oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
|
---|
3322 |
|
---|
3323 | this.initFunction(node);
|
---|
3324 | if (this.options.ecmaVersion >= 6)
|
---|
3325 | { node.generator = isGenerator; }
|
---|
3326 | if (this.options.ecmaVersion >= 8)
|
---|
3327 | { node.async = !!isAsync; }
|
---|
3328 |
|
---|
3329 | this.yieldPos = 0;
|
---|
3330 | this.awaitPos = 0;
|
---|
3331 | this.awaitIdentPos = 0;
|
---|
3332 | this.enterScope(functionFlags(isAsync, node.generator) | SCOPE_SUPER | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
|
---|
3333 |
|
---|
3334 | this.expect(types$1.parenL);
|
---|
3335 | node.params = this.parseBindingList(types$1.parenR, false, this.options.ecmaVersion >= 8);
|
---|
3336 | this.checkYieldAwaitInDefaultParams();
|
---|
3337 | this.parseFunctionBody(node, false, true, false);
|
---|
3338 |
|
---|
3339 | this.yieldPos = oldYieldPos;
|
---|
3340 | this.awaitPos = oldAwaitPos;
|
---|
3341 | this.awaitIdentPos = oldAwaitIdentPos;
|
---|
3342 | return this.finishNode(node, "FunctionExpression")
|
---|
3343 | };
|
---|
3344 |
|
---|
3345 | // Parse arrow function expression with given parameters.
|
---|
3346 |
|
---|
3347 | pp$5.parseArrowExpression = function(node, params, isAsync, forInit) {
|
---|
3348 | var oldYieldPos = this.yieldPos, oldAwaitPos = this.awaitPos, oldAwaitIdentPos = this.awaitIdentPos;
|
---|
3349 |
|
---|
3350 | this.enterScope(functionFlags(isAsync, false) | SCOPE_ARROW);
|
---|
3351 | this.initFunction(node);
|
---|
3352 | if (this.options.ecmaVersion >= 8) { node.async = !!isAsync; }
|
---|
3353 |
|
---|
3354 | this.yieldPos = 0;
|
---|
3355 | this.awaitPos = 0;
|
---|
3356 | this.awaitIdentPos = 0;
|
---|
3357 |
|
---|
3358 | node.params = this.toAssignableList(params, true);
|
---|
3359 | this.parseFunctionBody(node, true, false, forInit);
|
---|
3360 |
|
---|
3361 | this.yieldPos = oldYieldPos;
|
---|
3362 | this.awaitPos = oldAwaitPos;
|
---|
3363 | this.awaitIdentPos = oldAwaitIdentPos;
|
---|
3364 | return this.finishNode(node, "ArrowFunctionExpression")
|
---|
3365 | };
|
---|
3366 |
|
---|
3367 | // Parse function body and check parameters.
|
---|
3368 |
|
---|
3369 | pp$5.parseFunctionBody = function(node, isArrowFunction, isMethod, forInit) {
|
---|
3370 | var isExpression = isArrowFunction && this.type !== types$1.braceL;
|
---|
3371 | var oldStrict = this.strict, useStrict = false;
|
---|
3372 |
|
---|
3373 | if (isExpression) {
|
---|
3374 | node.body = this.parseMaybeAssign(forInit);
|
---|
3375 | node.expression = true;
|
---|
3376 | this.checkParams(node, false);
|
---|
3377 | } else {
|
---|
3378 | var nonSimple = this.options.ecmaVersion >= 7 && !this.isSimpleParamList(node.params);
|
---|
3379 | if (!oldStrict || nonSimple) {
|
---|
3380 | useStrict = this.strictDirective(this.end);
|
---|
3381 | // If this is a strict mode function, verify that argument names
|
---|
3382 | // are not repeated, and it does not try to bind the words `eval`
|
---|
3383 | // or `arguments`.
|
---|
3384 | if (useStrict && nonSimple)
|
---|
3385 | { this.raiseRecoverable(node.start, "Illegal 'use strict' directive in function with non-simple parameter list"); }
|
---|
3386 | }
|
---|
3387 | // Start a new scope with regard to labels and the `inFunction`
|
---|
3388 | // flag (restore them to their old value afterwards).
|
---|
3389 | var oldLabels = this.labels;
|
---|
3390 | this.labels = [];
|
---|
3391 | if (useStrict) { this.strict = true; }
|
---|
3392 |
|
---|
3393 | // Add the params to varDeclaredNames to ensure that an error is thrown
|
---|
3394 | // if a let/const declaration in the function clashes with one of the params.
|
---|
3395 | this.checkParams(node, !oldStrict && !useStrict && !isArrowFunction && !isMethod && this.isSimpleParamList(node.params));
|
---|
3396 | // Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
|
---|
3397 | if (this.strict && node.id) { this.checkLValSimple(node.id, BIND_OUTSIDE); }
|
---|
3398 | node.body = this.parseBlock(false, undefined, useStrict && !oldStrict);
|
---|
3399 | node.expression = false;
|
---|
3400 | this.adaptDirectivePrologue(node.body.body);
|
---|
3401 | this.labels = oldLabels;
|
---|
3402 | }
|
---|
3403 | this.exitScope();
|
---|
3404 | };
|
---|
3405 |
|
---|
3406 | pp$5.isSimpleParamList = function(params) {
|
---|
3407 | for (var i = 0, list = params; i < list.length; i += 1)
|
---|
3408 | {
|
---|
3409 | var param = list[i];
|
---|
3410 |
|
---|
3411 | if (param.type !== "Identifier") { return false
|
---|
3412 | } }
|
---|
3413 | return true
|
---|
3414 | };
|
---|
3415 |
|
---|
3416 | // Checks function params for various disallowed patterns such as using "eval"
|
---|
3417 | // or "arguments" and duplicate parameters.
|
---|
3418 |
|
---|
3419 | pp$5.checkParams = function(node, allowDuplicates) {
|
---|
3420 | var nameHash = Object.create(null);
|
---|
3421 | for (var i = 0, list = node.params; i < list.length; i += 1)
|
---|
3422 | {
|
---|
3423 | var param = list[i];
|
---|
3424 |
|
---|
3425 | this.checkLValInnerPattern(param, BIND_VAR, allowDuplicates ? null : nameHash);
|
---|
3426 | }
|
---|
3427 | };
|
---|
3428 |
|
---|
3429 | // Parses a comma-separated list of expressions, and returns them as
|
---|
3430 | // an array. `close` is the token type that ends the list, and
|
---|
3431 | // `allowEmpty` can be turned on to allow subsequent commas with
|
---|
3432 | // nothing in between them to be parsed as `null` (which is needed
|
---|
3433 | // for array literals).
|
---|
3434 |
|
---|
3435 | pp$5.parseExprList = function(close, allowTrailingComma, allowEmpty, refDestructuringErrors) {
|
---|
3436 | var elts = [], first = true;
|
---|
3437 | while (!this.eat(close)) {
|
---|
3438 | if (!first) {
|
---|
3439 | this.expect(types$1.comma);
|
---|
3440 | if (allowTrailingComma && this.afterTrailingComma(close)) { break }
|
---|
3441 | } else { first = false; }
|
---|
3442 |
|
---|
3443 | var elt = (void 0);
|
---|
3444 | if (allowEmpty && this.type === types$1.comma)
|
---|
3445 | { elt = null; }
|
---|
3446 | else if (this.type === types$1.ellipsis) {
|
---|
3447 | elt = this.parseSpread(refDestructuringErrors);
|
---|
3448 | if (refDestructuringErrors && this.type === types$1.comma && refDestructuringErrors.trailingComma < 0)
|
---|
3449 | { refDestructuringErrors.trailingComma = this.start; }
|
---|
3450 | } else {
|
---|
3451 | elt = this.parseMaybeAssign(false, refDestructuringErrors);
|
---|
3452 | }
|
---|
3453 | elts.push(elt);
|
---|
3454 | }
|
---|
3455 | return elts
|
---|
3456 | };
|
---|
3457 |
|
---|
3458 | pp$5.checkUnreserved = function(ref) {
|
---|
3459 | var start = ref.start;
|
---|
3460 | var end = ref.end;
|
---|
3461 | var name = ref.name;
|
---|
3462 |
|
---|
3463 | if (this.inGenerator && name === "yield")
|
---|
3464 | { this.raiseRecoverable(start, "Cannot use 'yield' as identifier inside a generator"); }
|
---|
3465 | if (this.inAsync && name === "await")
|
---|
3466 | { this.raiseRecoverable(start, "Cannot use 'await' as identifier inside an async function"); }
|
---|
3467 | if (this.currentThisScope().inClassFieldInit && name === "arguments")
|
---|
3468 | { this.raiseRecoverable(start, "Cannot use 'arguments' in class field initializer"); }
|
---|
3469 | if (this.inClassStaticBlock && (name === "arguments" || name === "await"))
|
---|
3470 | { this.raise(start, ("Cannot use " + name + " in class static initialization block")); }
|
---|
3471 | if (this.keywords.test(name))
|
---|
3472 | { this.raise(start, ("Unexpected keyword '" + name + "'")); }
|
---|
3473 | if (this.options.ecmaVersion < 6 &&
|
---|
3474 | this.input.slice(start, end).indexOf("\\") !== -1) { return }
|
---|
3475 | var re = this.strict ? this.reservedWordsStrict : this.reservedWords;
|
---|
3476 | if (re.test(name)) {
|
---|
3477 | if (!this.inAsync && name === "await")
|
---|
3478 | { this.raiseRecoverable(start, "Cannot use keyword 'await' outside an async function"); }
|
---|
3479 | this.raiseRecoverable(start, ("The keyword '" + name + "' is reserved"));
|
---|
3480 | }
|
---|
3481 | };
|
---|
3482 |
|
---|
3483 | // Parse the next token as an identifier. If `liberal` is true (used
|
---|
3484 | // when parsing properties), it will also convert keywords into
|
---|
3485 | // identifiers.
|
---|
3486 |
|
---|
3487 | pp$5.parseIdent = function(liberal) {
|
---|
3488 | var node = this.parseIdentNode();
|
---|
3489 | this.next(!!liberal);
|
---|
3490 | this.finishNode(node, "Identifier");
|
---|
3491 | if (!liberal) {
|
---|
3492 | this.checkUnreserved(node);
|
---|
3493 | if (node.name === "await" && !this.awaitIdentPos)
|
---|
3494 | { this.awaitIdentPos = node.start; }
|
---|
3495 | }
|
---|
3496 | return node
|
---|
3497 | };
|
---|
3498 |
|
---|
3499 | pp$5.parseIdentNode = function() {
|
---|
3500 | var node = this.startNode();
|
---|
3501 | if (this.type === types$1.name) {
|
---|
3502 | node.name = this.value;
|
---|
3503 | } else if (this.type.keyword) {
|
---|
3504 | node.name = this.type.keyword;
|
---|
3505 |
|
---|
3506 | // To fix https://github.com/acornjs/acorn/issues/575
|
---|
3507 | // `class` and `function` keywords push new context into this.context.
|
---|
3508 | // But there is no chance to pop the context if the keyword is consumed as an identifier such as a property name.
|
---|
3509 | // If the previous token is a dot, this does not apply because the context-managing code already ignored the keyword
|
---|
3510 | if ((node.name === "class" || node.name === "function") &&
|
---|
3511 | (this.lastTokEnd !== this.lastTokStart + 1 || this.input.charCodeAt(this.lastTokStart) !== 46)) {
|
---|
3512 | this.context.pop();
|
---|
3513 | }
|
---|
3514 | this.type = types$1.name;
|
---|
3515 | } else {
|
---|
3516 | this.unexpected();
|
---|
3517 | }
|
---|
3518 | return node
|
---|
3519 | };
|
---|
3520 |
|
---|
3521 | pp$5.parsePrivateIdent = function() {
|
---|
3522 | var node = this.startNode();
|
---|
3523 | if (this.type === types$1.privateId) {
|
---|
3524 | node.name = this.value;
|
---|
3525 | } else {
|
---|
3526 | this.unexpected();
|
---|
3527 | }
|
---|
3528 | this.next();
|
---|
3529 | this.finishNode(node, "PrivateIdentifier");
|
---|
3530 |
|
---|
3531 | // For validating existence
|
---|
3532 | if (this.options.checkPrivateFields) {
|
---|
3533 | if (this.privateNameStack.length === 0) {
|
---|
3534 | this.raise(node.start, ("Private field '#" + (node.name) + "' must be declared in an enclosing class"));
|
---|
3535 | } else {
|
---|
3536 | this.privateNameStack[this.privateNameStack.length - 1].used.push(node);
|
---|
3537 | }
|
---|
3538 | }
|
---|
3539 |
|
---|
3540 | return node
|
---|
3541 | };
|
---|
3542 |
|
---|
3543 | // Parses yield expression inside generator.
|
---|
3544 |
|
---|
3545 | pp$5.parseYield = function(forInit) {
|
---|
3546 | if (!this.yieldPos) { this.yieldPos = this.start; }
|
---|
3547 |
|
---|
3548 | var node = this.startNode();
|
---|
3549 | this.next();
|
---|
3550 | if (this.type === types$1.semi || this.canInsertSemicolon() || (this.type !== types$1.star && !this.type.startsExpr)) {
|
---|
3551 | node.delegate = false;
|
---|
3552 | node.argument = null;
|
---|
3553 | } else {
|
---|
3554 | node.delegate = this.eat(types$1.star);
|
---|
3555 | node.argument = this.parseMaybeAssign(forInit);
|
---|
3556 | }
|
---|
3557 | return this.finishNode(node, "YieldExpression")
|
---|
3558 | };
|
---|
3559 |
|
---|
3560 | pp$5.parseAwait = function(forInit) {
|
---|
3561 | if (!this.awaitPos) { this.awaitPos = this.start; }
|
---|
3562 |
|
---|
3563 | var node = this.startNode();
|
---|
3564 | this.next();
|
---|
3565 | node.argument = this.parseMaybeUnary(null, true, false, forInit);
|
---|
3566 | return this.finishNode(node, "AwaitExpression")
|
---|
3567 | };
|
---|
3568 |
|
---|
3569 | var pp$4 = Parser.prototype;
|
---|
3570 |
|
---|
3571 | // This function is used to raise exceptions on parse errors. It
|
---|
3572 | // takes an offset integer (into the current `input`) to indicate
|
---|
3573 | // the location of the error, attaches the position to the end
|
---|
3574 | // of the error message, and then raises a `SyntaxError` with that
|
---|
3575 | // message.
|
---|
3576 |
|
---|
3577 | pp$4.raise = function(pos, message) {
|
---|
3578 | var loc = getLineInfo(this.input, pos);
|
---|
3579 | message += " (" + loc.line + ":" + loc.column + ")";
|
---|
3580 | var err = new SyntaxError(message);
|
---|
3581 | err.pos = pos; err.loc = loc; err.raisedAt = this.pos;
|
---|
3582 | throw err
|
---|
3583 | };
|
---|
3584 |
|
---|
3585 | pp$4.raiseRecoverable = pp$4.raise;
|
---|
3586 |
|
---|
3587 | pp$4.curPosition = function() {
|
---|
3588 | if (this.options.locations) {
|
---|
3589 | return new Position(this.curLine, this.pos - this.lineStart)
|
---|
3590 | }
|
---|
3591 | };
|
---|
3592 |
|
---|
3593 | var pp$3 = Parser.prototype;
|
---|
3594 |
|
---|
3595 | var Scope = function Scope(flags) {
|
---|
3596 | this.flags = flags;
|
---|
3597 | // A list of var-declared names in the current lexical scope
|
---|
3598 | this.var = [];
|
---|
3599 | // A list of lexically-declared names in the current lexical scope
|
---|
3600 | this.lexical = [];
|
---|
3601 | // A list of lexically-declared FunctionDeclaration names in the current lexical scope
|
---|
3602 | this.functions = [];
|
---|
3603 | // A switch to disallow the identifier reference 'arguments'
|
---|
3604 | this.inClassFieldInit = false;
|
---|
3605 | };
|
---|
3606 |
|
---|
3607 | // The functions in this module keep track of declared variables in the current scope in order to detect duplicate variable names.
|
---|
3608 |
|
---|
3609 | pp$3.enterScope = function(flags) {
|
---|
3610 | this.scopeStack.push(new Scope(flags));
|
---|
3611 | };
|
---|
3612 |
|
---|
3613 | pp$3.exitScope = function() {
|
---|
3614 | this.scopeStack.pop();
|
---|
3615 | };
|
---|
3616 |
|
---|
3617 | // The spec says:
|
---|
3618 | // > At the top level of a function, or script, function declarations are
|
---|
3619 | // > treated like var declarations rather than like lexical declarations.
|
---|
3620 | pp$3.treatFunctionsAsVarInScope = function(scope) {
|
---|
3621 | return (scope.flags & SCOPE_FUNCTION) || !this.inModule && (scope.flags & SCOPE_TOP)
|
---|
3622 | };
|
---|
3623 |
|
---|
3624 | pp$3.declareName = function(name, bindingType, pos) {
|
---|
3625 | var redeclared = false;
|
---|
3626 | if (bindingType === BIND_LEXICAL) {
|
---|
3627 | var scope = this.currentScope();
|
---|
3628 | redeclared = scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
|
---|
3629 | scope.lexical.push(name);
|
---|
3630 | if (this.inModule && (scope.flags & SCOPE_TOP))
|
---|
3631 | { delete this.undefinedExports[name]; }
|
---|
3632 | } else if (bindingType === BIND_SIMPLE_CATCH) {
|
---|
3633 | var scope$1 = this.currentScope();
|
---|
3634 | scope$1.lexical.push(name);
|
---|
3635 | } else if (bindingType === BIND_FUNCTION) {
|
---|
3636 | var scope$2 = this.currentScope();
|
---|
3637 | if (this.treatFunctionsAsVar)
|
---|
3638 | { redeclared = scope$2.lexical.indexOf(name) > -1; }
|
---|
3639 | else
|
---|
3640 | { redeclared = scope$2.lexical.indexOf(name) > -1 || scope$2.var.indexOf(name) > -1; }
|
---|
3641 | scope$2.functions.push(name);
|
---|
3642 | } else {
|
---|
3643 | for (var i = this.scopeStack.length - 1; i >= 0; --i) {
|
---|
3644 | var scope$3 = this.scopeStack[i];
|
---|
3645 | if (scope$3.lexical.indexOf(name) > -1 && !((scope$3.flags & SCOPE_SIMPLE_CATCH) && scope$3.lexical[0] === name) ||
|
---|
3646 | !this.treatFunctionsAsVarInScope(scope$3) && scope$3.functions.indexOf(name) > -1) {
|
---|
3647 | redeclared = true;
|
---|
3648 | break
|
---|
3649 | }
|
---|
3650 | scope$3.var.push(name);
|
---|
3651 | if (this.inModule && (scope$3.flags & SCOPE_TOP))
|
---|
3652 | { delete this.undefinedExports[name]; }
|
---|
3653 | if (scope$3.flags & SCOPE_VAR) { break }
|
---|
3654 | }
|
---|
3655 | }
|
---|
3656 | if (redeclared) { this.raiseRecoverable(pos, ("Identifier '" + name + "' has already been declared")); }
|
---|
3657 | };
|
---|
3658 |
|
---|
3659 | pp$3.checkLocalExport = function(id) {
|
---|
3660 | // scope.functions must be empty as Module code is always strict.
|
---|
3661 | if (this.scopeStack[0].lexical.indexOf(id.name) === -1 &&
|
---|
3662 | this.scopeStack[0].var.indexOf(id.name) === -1) {
|
---|
3663 | this.undefinedExports[id.name] = id;
|
---|
3664 | }
|
---|
3665 | };
|
---|
3666 |
|
---|
3667 | pp$3.currentScope = function() {
|
---|
3668 | return this.scopeStack[this.scopeStack.length - 1]
|
---|
3669 | };
|
---|
3670 |
|
---|
3671 | pp$3.currentVarScope = function() {
|
---|
3672 | for (var i = this.scopeStack.length - 1;; i--) {
|
---|
3673 | var scope = this.scopeStack[i];
|
---|
3674 | if (scope.flags & SCOPE_VAR) { return scope }
|
---|
3675 | }
|
---|
3676 | };
|
---|
3677 |
|
---|
3678 | // Could be useful for `this`, `new.target`, `super()`, `super.property`, and `super[property]`.
|
---|
3679 | pp$3.currentThisScope = function() {
|
---|
3680 | for (var i = this.scopeStack.length - 1;; i--) {
|
---|
3681 | var scope = this.scopeStack[i];
|
---|
3682 | if (scope.flags & SCOPE_VAR && !(scope.flags & SCOPE_ARROW)) { return scope }
|
---|
3683 | }
|
---|
3684 | };
|
---|
3685 |
|
---|
3686 | var Node = function Node(parser, pos, loc) {
|
---|
3687 | this.type = "";
|
---|
3688 | this.start = pos;
|
---|
3689 | this.end = 0;
|
---|
3690 | if (parser.options.locations)
|
---|
3691 | { this.loc = new SourceLocation(parser, loc); }
|
---|
3692 | if (parser.options.directSourceFile)
|
---|
3693 | { this.sourceFile = parser.options.directSourceFile; }
|
---|
3694 | if (parser.options.ranges)
|
---|
3695 | { this.range = [pos, 0]; }
|
---|
3696 | };
|
---|
3697 |
|
---|
3698 | // Start an AST node, attaching a start offset.
|
---|
3699 |
|
---|
3700 | var pp$2 = Parser.prototype;
|
---|
3701 |
|
---|
3702 | pp$2.startNode = function() {
|
---|
3703 | return new Node(this, this.start, this.startLoc)
|
---|
3704 | };
|
---|
3705 |
|
---|
3706 | pp$2.startNodeAt = function(pos, loc) {
|
---|
3707 | return new Node(this, pos, loc)
|
---|
3708 | };
|
---|
3709 |
|
---|
3710 | // Finish an AST node, adding `type` and `end` properties.
|
---|
3711 |
|
---|
3712 | function finishNodeAt(node, type, pos, loc) {
|
---|
3713 | node.type = type;
|
---|
3714 | node.end = pos;
|
---|
3715 | if (this.options.locations)
|
---|
3716 | { node.loc.end = loc; }
|
---|
3717 | if (this.options.ranges)
|
---|
3718 | { node.range[1] = pos; }
|
---|
3719 | return node
|
---|
3720 | }
|
---|
3721 |
|
---|
3722 | pp$2.finishNode = function(node, type) {
|
---|
3723 | return finishNodeAt.call(this, node, type, this.lastTokEnd, this.lastTokEndLoc)
|
---|
3724 | };
|
---|
3725 |
|
---|
3726 | // Finish node at given position
|
---|
3727 |
|
---|
3728 | pp$2.finishNodeAt = function(node, type, pos, loc) {
|
---|
3729 | return finishNodeAt.call(this, node, type, pos, loc)
|
---|
3730 | };
|
---|
3731 |
|
---|
3732 | pp$2.copyNode = function(node) {
|
---|
3733 | var newNode = new Node(this, node.start, this.startLoc);
|
---|
3734 | for (var prop in node) { newNode[prop] = node[prop]; }
|
---|
3735 | return newNode
|
---|
3736 | };
|
---|
3737 |
|
---|
3738 | // This file contains Unicode properties extracted from the ECMAScript specification.
|
---|
3739 | // The lists are extracted like so:
|
---|
3740 | // $$('#table-binary-unicode-properties > figure > table > tbody > tr > td:nth-child(1) code').map(el => el.innerText)
|
---|
3741 |
|
---|
3742 | // #table-binary-unicode-properties
|
---|
3743 | var ecma9BinaryProperties = "ASCII ASCII_Hex_Digit AHex Alphabetic Alpha Any Assigned Bidi_Control Bidi_C Bidi_Mirrored Bidi_M Case_Ignorable CI Cased Changes_When_Casefolded CWCF Changes_When_Casemapped CWCM Changes_When_Lowercased CWL Changes_When_NFKC_Casefolded CWKCF Changes_When_Titlecased CWT Changes_When_Uppercased CWU Dash Default_Ignorable_Code_Point DI Deprecated Dep Diacritic Dia Emoji Emoji_Component Emoji_Modifier Emoji_Modifier_Base Emoji_Presentation Extender Ext Grapheme_Base Gr_Base Grapheme_Extend Gr_Ext Hex_Digit Hex IDS_Binary_Operator IDSB IDS_Trinary_Operator IDST ID_Continue IDC ID_Start IDS Ideographic Ideo Join_Control Join_C Logical_Order_Exception LOE Lowercase Lower Math Noncharacter_Code_Point NChar Pattern_Syntax Pat_Syn Pattern_White_Space Pat_WS Quotation_Mark QMark Radical Regional_Indicator RI Sentence_Terminal STerm Soft_Dotted SD Terminal_Punctuation Term Unified_Ideograph UIdeo Uppercase Upper Variation_Selector VS White_Space space XID_Continue XIDC XID_Start XIDS";
|
---|
3744 | var ecma10BinaryProperties = ecma9BinaryProperties + " Extended_Pictographic";
|
---|
3745 | var ecma11BinaryProperties = ecma10BinaryProperties;
|
---|
3746 | var ecma12BinaryProperties = ecma11BinaryProperties + " EBase EComp EMod EPres ExtPict";
|
---|
3747 | var ecma13BinaryProperties = ecma12BinaryProperties;
|
---|
3748 | var ecma14BinaryProperties = ecma13BinaryProperties;
|
---|
3749 |
|
---|
3750 | var unicodeBinaryProperties = {
|
---|
3751 | 9: ecma9BinaryProperties,
|
---|
3752 | 10: ecma10BinaryProperties,
|
---|
3753 | 11: ecma11BinaryProperties,
|
---|
3754 | 12: ecma12BinaryProperties,
|
---|
3755 | 13: ecma13BinaryProperties,
|
---|
3756 | 14: ecma14BinaryProperties
|
---|
3757 | };
|
---|
3758 |
|
---|
3759 | // #table-binary-unicode-properties-of-strings
|
---|
3760 | var ecma14BinaryPropertiesOfStrings = "Basic_Emoji Emoji_Keycap_Sequence RGI_Emoji_Modifier_Sequence RGI_Emoji_Flag_Sequence RGI_Emoji_Tag_Sequence RGI_Emoji_ZWJ_Sequence RGI_Emoji";
|
---|
3761 |
|
---|
3762 | var unicodeBinaryPropertiesOfStrings = {
|
---|
3763 | 9: "",
|
---|
3764 | 10: "",
|
---|
3765 | 11: "",
|
---|
3766 | 12: "",
|
---|
3767 | 13: "",
|
---|
3768 | 14: ecma14BinaryPropertiesOfStrings
|
---|
3769 | };
|
---|
3770 |
|
---|
3771 | // #table-unicode-general-category-values
|
---|
3772 | var unicodeGeneralCategoryValues = "Cased_Letter LC Close_Punctuation Pe Connector_Punctuation Pc Control Cc cntrl Currency_Symbol Sc Dash_Punctuation Pd Decimal_Number Nd digit Enclosing_Mark Me Final_Punctuation Pf Format Cf Initial_Punctuation Pi Letter L Letter_Number Nl Line_Separator Zl Lowercase_Letter Ll Mark M Combining_Mark Math_Symbol Sm Modifier_Letter Lm Modifier_Symbol Sk Nonspacing_Mark Mn Number N Open_Punctuation Ps Other C Other_Letter Lo Other_Number No Other_Punctuation Po Other_Symbol So Paragraph_Separator Zp Private_Use Co Punctuation P punct Separator Z Space_Separator Zs Spacing_Mark Mc Surrogate Cs Symbol S Titlecase_Letter Lt Unassigned Cn Uppercase_Letter Lu";
|
---|
3773 |
|
---|
3774 | // #table-unicode-script-values
|
---|
3775 | var ecma9ScriptValues = "Adlam Adlm Ahom Anatolian_Hieroglyphs Hluw Arabic Arab Armenian Armn Avestan Avst Balinese Bali Bamum Bamu Bassa_Vah Bass Batak Batk Bengali Beng Bhaiksuki Bhks Bopomofo Bopo Brahmi Brah Braille Brai Buginese Bugi Buhid Buhd Canadian_Aboriginal Cans Carian Cari Caucasian_Albanian Aghb Chakma Cakm Cham Cham Cherokee Cher Common Zyyy Coptic Copt Qaac Cuneiform Xsux Cypriot Cprt Cyrillic Cyrl Deseret Dsrt Devanagari Deva Duployan Dupl Egyptian_Hieroglyphs Egyp Elbasan Elba Ethiopic Ethi Georgian Geor Glagolitic Glag Gothic Goth Grantha Gran Greek Grek Gujarati Gujr Gurmukhi Guru Han Hani Hangul Hang Hanunoo Hano Hatran Hatr Hebrew Hebr Hiragana Hira Imperial_Aramaic Armi Inherited Zinh Qaai Inscriptional_Pahlavi Phli Inscriptional_Parthian Prti Javanese Java Kaithi Kthi Kannada Knda Katakana Kana Kayah_Li Kali Kharoshthi Khar Khmer Khmr Khojki Khoj Khudawadi Sind Lao Laoo Latin Latn Lepcha Lepc Limbu Limb Linear_A Lina Linear_B Linb Lisu Lisu Lycian Lyci Lydian Lydi Mahajani Mahj Malayalam Mlym Mandaic Mand Manichaean Mani Marchen Marc Masaram_Gondi Gonm Meetei_Mayek Mtei Mende_Kikakui Mend Meroitic_Cursive Merc Meroitic_Hieroglyphs Mero Miao Plrd Modi Mongolian Mong Mro Mroo Multani Mult Myanmar Mymr Nabataean Nbat New_Tai_Lue Talu Newa Newa Nko Nkoo Nushu Nshu Ogham Ogam Ol_Chiki Olck Old_Hungarian Hung Old_Italic Ital Old_North_Arabian Narb Old_Permic Perm Old_Persian Xpeo Old_South_Arabian Sarb Old_Turkic Orkh Oriya Orya Osage Osge Osmanya Osma Pahawh_Hmong Hmng Palmyrene Palm Pau_Cin_Hau Pauc Phags_Pa Phag Phoenician Phnx Psalter_Pahlavi Phlp Rejang Rjng Runic Runr Samaritan Samr Saurashtra Saur Sharada Shrd Shavian Shaw Siddham Sidd SignWriting Sgnw Sinhala Sinh Sora_Sompeng Sora Soyombo Soyo Sundanese Sund Syloti_Nagri Sylo Syriac Syrc Tagalog Tglg Tagbanwa Tagb Tai_Le Tale Tai_Tham Lana Tai_Viet Tavt Takri Takr Tamil Taml Tangut Tang Telugu Telu Thaana Thaa Thai Thai Tibetan Tibt Tifinagh Tfng Tirhuta Tirh Ugaritic Ugar Vai Vaii Warang_Citi Wara Yi Yiii Zanabazar_Square Zanb";
|
---|
3776 | var ecma10ScriptValues = ecma9ScriptValues + " Dogra Dogr Gunjala_Gondi Gong Hanifi_Rohingya Rohg Makasar Maka Medefaidrin Medf Old_Sogdian Sogo Sogdian Sogd";
|
---|
3777 | var ecma11ScriptValues = ecma10ScriptValues + " Elymaic Elym Nandinagari Nand Nyiakeng_Puachue_Hmong Hmnp Wancho Wcho";
|
---|
3778 | var ecma12ScriptValues = ecma11ScriptValues + " Chorasmian Chrs Diak Dives_Akuru Khitan_Small_Script Kits Yezi Yezidi";
|
---|
3779 | var ecma13ScriptValues = ecma12ScriptValues + " Cypro_Minoan Cpmn Old_Uyghur Ougr Tangsa Tnsa Toto Vithkuqi Vith";
|
---|
3780 | var ecma14ScriptValues = ecma13ScriptValues + " Hrkt Katakana_Or_Hiragana Kawi Nag_Mundari Nagm Unknown Zzzz";
|
---|
3781 |
|
---|
3782 | var unicodeScriptValues = {
|
---|
3783 | 9: ecma9ScriptValues,
|
---|
3784 | 10: ecma10ScriptValues,
|
---|
3785 | 11: ecma11ScriptValues,
|
---|
3786 | 12: ecma12ScriptValues,
|
---|
3787 | 13: ecma13ScriptValues,
|
---|
3788 | 14: ecma14ScriptValues
|
---|
3789 | };
|
---|
3790 |
|
---|
3791 | var data = {};
|
---|
3792 | function buildUnicodeData(ecmaVersion) {
|
---|
3793 | var d = data[ecmaVersion] = {
|
---|
3794 | binary: wordsRegexp(unicodeBinaryProperties[ecmaVersion] + " " + unicodeGeneralCategoryValues),
|
---|
3795 | binaryOfStrings: wordsRegexp(unicodeBinaryPropertiesOfStrings[ecmaVersion]),
|
---|
3796 | nonBinary: {
|
---|
3797 | General_Category: wordsRegexp(unicodeGeneralCategoryValues),
|
---|
3798 | Script: wordsRegexp(unicodeScriptValues[ecmaVersion])
|
---|
3799 | }
|
---|
3800 | };
|
---|
3801 | d.nonBinary.Script_Extensions = d.nonBinary.Script;
|
---|
3802 |
|
---|
3803 | d.nonBinary.gc = d.nonBinary.General_Category;
|
---|
3804 | d.nonBinary.sc = d.nonBinary.Script;
|
---|
3805 | d.nonBinary.scx = d.nonBinary.Script_Extensions;
|
---|
3806 | }
|
---|
3807 |
|
---|
3808 | for (var i = 0, list = [9, 10, 11, 12, 13, 14]; i < list.length; i += 1) {
|
---|
3809 | var ecmaVersion = list[i];
|
---|
3810 |
|
---|
3811 | buildUnicodeData(ecmaVersion);
|
---|
3812 | }
|
---|
3813 |
|
---|
3814 | var pp$1 = Parser.prototype;
|
---|
3815 |
|
---|
3816 | // Track disjunction structure to determine whether a duplicate
|
---|
3817 | // capture group name is allowed because it is in a separate branch.
|
---|
3818 | var BranchID = function BranchID(parent, base) {
|
---|
3819 | // Parent disjunction branch
|
---|
3820 | this.parent = parent;
|
---|
3821 | // Identifies this set of sibling branches
|
---|
3822 | this.base = base || this;
|
---|
3823 | };
|
---|
3824 |
|
---|
3825 | BranchID.prototype.separatedFrom = function separatedFrom (alt) {
|
---|
3826 | // A branch is separate from another branch if they or any of
|
---|
3827 | // their parents are siblings in a given disjunction
|
---|
3828 | for (var self = this; self; self = self.parent) {
|
---|
3829 | for (var other = alt; other; other = other.parent) {
|
---|
3830 | if (self.base === other.base && self !== other) { return true }
|
---|
3831 | }
|
---|
3832 | }
|
---|
3833 | return false
|
---|
3834 | };
|
---|
3835 |
|
---|
3836 | BranchID.prototype.sibling = function sibling () {
|
---|
3837 | return new BranchID(this.parent, this.base)
|
---|
3838 | };
|
---|
3839 |
|
---|
3840 | var RegExpValidationState = function RegExpValidationState(parser) {
|
---|
3841 | this.parser = parser;
|
---|
3842 | this.validFlags = "gim" + (parser.options.ecmaVersion >= 6 ? "uy" : "") + (parser.options.ecmaVersion >= 9 ? "s" : "") + (parser.options.ecmaVersion >= 13 ? "d" : "") + (parser.options.ecmaVersion >= 15 ? "v" : "");
|
---|
3843 | this.unicodeProperties = data[parser.options.ecmaVersion >= 14 ? 14 : parser.options.ecmaVersion];
|
---|
3844 | this.source = "";
|
---|
3845 | this.flags = "";
|
---|
3846 | this.start = 0;
|
---|
3847 | this.switchU = false;
|
---|
3848 | this.switchV = false;
|
---|
3849 | this.switchN = false;
|
---|
3850 | this.pos = 0;
|
---|
3851 | this.lastIntValue = 0;
|
---|
3852 | this.lastStringValue = "";
|
---|
3853 | this.lastAssertionIsQuantifiable = false;
|
---|
3854 | this.numCapturingParens = 0;
|
---|
3855 | this.maxBackReference = 0;
|
---|
3856 | this.groupNames = Object.create(null);
|
---|
3857 | this.backReferenceNames = [];
|
---|
3858 | this.branchID = null;
|
---|
3859 | };
|
---|
3860 |
|
---|
3861 | RegExpValidationState.prototype.reset = function reset (start, pattern, flags) {
|
---|
3862 | var unicodeSets = flags.indexOf("v") !== -1;
|
---|
3863 | var unicode = flags.indexOf("u") !== -1;
|
---|
3864 | this.start = start | 0;
|
---|
3865 | this.source = pattern + "";
|
---|
3866 | this.flags = flags;
|
---|
3867 | if (unicodeSets && this.parser.options.ecmaVersion >= 15) {
|
---|
3868 | this.switchU = true;
|
---|
3869 | this.switchV = true;
|
---|
3870 | this.switchN = true;
|
---|
3871 | } else {
|
---|
3872 | this.switchU = unicode && this.parser.options.ecmaVersion >= 6;
|
---|
3873 | this.switchV = false;
|
---|
3874 | this.switchN = unicode && this.parser.options.ecmaVersion >= 9;
|
---|
3875 | }
|
---|
3876 | };
|
---|
3877 |
|
---|
3878 | RegExpValidationState.prototype.raise = function raise (message) {
|
---|
3879 | this.parser.raiseRecoverable(this.start, ("Invalid regular expression: /" + (this.source) + "/: " + message));
|
---|
3880 | };
|
---|
3881 |
|
---|
3882 | // If u flag is given, this returns the code point at the index (it combines a surrogate pair).
|
---|
3883 | // Otherwise, this returns the code unit of the index (can be a part of a surrogate pair).
|
---|
3884 | RegExpValidationState.prototype.at = function at (i, forceU) {
|
---|
3885 | if ( forceU === void 0 ) forceU = false;
|
---|
3886 |
|
---|
3887 | var s = this.source;
|
---|
3888 | var l = s.length;
|
---|
3889 | if (i >= l) {
|
---|
3890 | return -1
|
---|
3891 | }
|
---|
3892 | var c = s.charCodeAt(i);
|
---|
3893 | if (!(forceU || this.switchU) || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l) {
|
---|
3894 | return c
|
---|
3895 | }
|
---|
3896 | var next = s.charCodeAt(i + 1);
|
---|
3897 | return next >= 0xDC00 && next <= 0xDFFF ? (c << 10) + next - 0x35FDC00 : c
|
---|
3898 | };
|
---|
3899 |
|
---|
3900 | RegExpValidationState.prototype.nextIndex = function nextIndex (i, forceU) {
|
---|
3901 | if ( forceU === void 0 ) forceU = false;
|
---|
3902 |
|
---|
3903 | var s = this.source;
|
---|
3904 | var l = s.length;
|
---|
3905 | if (i >= l) {
|
---|
3906 | return l
|
---|
3907 | }
|
---|
3908 | var c = s.charCodeAt(i), next;
|
---|
3909 | if (!(forceU || this.switchU) || c <= 0xD7FF || c >= 0xE000 || i + 1 >= l ||
|
---|
3910 | (next = s.charCodeAt(i + 1)) < 0xDC00 || next > 0xDFFF) {
|
---|
3911 | return i + 1
|
---|
3912 | }
|
---|
3913 | return i + 2
|
---|
3914 | };
|
---|
3915 |
|
---|
3916 | RegExpValidationState.prototype.current = function current (forceU) {
|
---|
3917 | if ( forceU === void 0 ) forceU = false;
|
---|
3918 |
|
---|
3919 | return this.at(this.pos, forceU)
|
---|
3920 | };
|
---|
3921 |
|
---|
3922 | RegExpValidationState.prototype.lookahead = function lookahead (forceU) {
|
---|
3923 | if ( forceU === void 0 ) forceU = false;
|
---|
3924 |
|
---|
3925 | return this.at(this.nextIndex(this.pos, forceU), forceU)
|
---|
3926 | };
|
---|
3927 |
|
---|
3928 | RegExpValidationState.prototype.advance = function advance (forceU) {
|
---|
3929 | if ( forceU === void 0 ) forceU = false;
|
---|
3930 |
|
---|
3931 | this.pos = this.nextIndex(this.pos, forceU);
|
---|
3932 | };
|
---|
3933 |
|
---|
3934 | RegExpValidationState.prototype.eat = function eat (ch, forceU) {
|
---|
3935 | if ( forceU === void 0 ) forceU = false;
|
---|
3936 |
|
---|
3937 | if (this.current(forceU) === ch) {
|
---|
3938 | this.advance(forceU);
|
---|
3939 | return true
|
---|
3940 | }
|
---|
3941 | return false
|
---|
3942 | };
|
---|
3943 |
|
---|
3944 | RegExpValidationState.prototype.eatChars = function eatChars (chs, forceU) {
|
---|
3945 | if ( forceU === void 0 ) forceU = false;
|
---|
3946 |
|
---|
3947 | var pos = this.pos;
|
---|
3948 | for (var i = 0, list = chs; i < list.length; i += 1) {
|
---|
3949 | var ch = list[i];
|
---|
3950 |
|
---|
3951 | var current = this.at(pos, forceU);
|
---|
3952 | if (current === -1 || current !== ch) {
|
---|
3953 | return false
|
---|
3954 | }
|
---|
3955 | pos = this.nextIndex(pos, forceU);
|
---|
3956 | }
|
---|
3957 | this.pos = pos;
|
---|
3958 | return true
|
---|
3959 | };
|
---|
3960 |
|
---|
3961 | /**
|
---|
3962 | * Validate the flags part of a given RegExpLiteral.
|
---|
3963 | *
|
---|
3964 | * @param {RegExpValidationState} state The state to validate RegExp.
|
---|
3965 | * @returns {void}
|
---|
3966 | */
|
---|
3967 | pp$1.validateRegExpFlags = function(state) {
|
---|
3968 | var validFlags = state.validFlags;
|
---|
3969 | var flags = state.flags;
|
---|
3970 |
|
---|
3971 | var u = false;
|
---|
3972 | var v = false;
|
---|
3973 |
|
---|
3974 | for (var i = 0; i < flags.length; i++) {
|
---|
3975 | var flag = flags.charAt(i);
|
---|
3976 | if (validFlags.indexOf(flag) === -1) {
|
---|
3977 | this.raise(state.start, "Invalid regular expression flag");
|
---|
3978 | }
|
---|
3979 | if (flags.indexOf(flag, i + 1) > -1) {
|
---|
3980 | this.raise(state.start, "Duplicate regular expression flag");
|
---|
3981 | }
|
---|
3982 | if (flag === "u") { u = true; }
|
---|
3983 | if (flag === "v") { v = true; }
|
---|
3984 | }
|
---|
3985 | if (this.options.ecmaVersion >= 15 && u && v) {
|
---|
3986 | this.raise(state.start, "Invalid regular expression flag");
|
---|
3987 | }
|
---|
3988 | };
|
---|
3989 |
|
---|
3990 | function hasProp(obj) {
|
---|
3991 | for (var _ in obj) { return true }
|
---|
3992 | return false
|
---|
3993 | }
|
---|
3994 |
|
---|
3995 | /**
|
---|
3996 | * Validate the pattern part of a given RegExpLiteral.
|
---|
3997 | *
|
---|
3998 | * @param {RegExpValidationState} state The state to validate RegExp.
|
---|
3999 | * @returns {void}
|
---|
4000 | */
|
---|
4001 | pp$1.validateRegExpPattern = function(state) {
|
---|
4002 | this.regexp_pattern(state);
|
---|
4003 |
|
---|
4004 | // The goal symbol for the parse is |Pattern[~U, ~N]|. If the result of
|
---|
4005 | // parsing contains a |GroupName|, reparse with the goal symbol
|
---|
4006 | // |Pattern[~U, +N]| and use this result instead. Throw a *SyntaxError*
|
---|
4007 | // exception if _P_ did not conform to the grammar, if any elements of _P_
|
---|
4008 | // were not matched by the parse, or if any Early Error conditions exist.
|
---|
4009 | if (!state.switchN && this.options.ecmaVersion >= 9 && hasProp(state.groupNames)) {
|
---|
4010 | state.switchN = true;
|
---|
4011 | this.regexp_pattern(state);
|
---|
4012 | }
|
---|
4013 | };
|
---|
4014 |
|
---|
4015 | // https://www.ecma-international.org/ecma-262/8.0/#prod-Pattern
|
---|
4016 | pp$1.regexp_pattern = function(state) {
|
---|
4017 | state.pos = 0;
|
---|
4018 | state.lastIntValue = 0;
|
---|
4019 | state.lastStringValue = "";
|
---|
4020 | state.lastAssertionIsQuantifiable = false;
|
---|
4021 | state.numCapturingParens = 0;
|
---|
4022 | state.maxBackReference = 0;
|
---|
4023 | state.groupNames = Object.create(null);
|
---|
4024 | state.backReferenceNames.length = 0;
|
---|
4025 | state.branchID = null;
|
---|
4026 |
|
---|
4027 | this.regexp_disjunction(state);
|
---|
4028 |
|
---|
4029 | if (state.pos !== state.source.length) {
|
---|
4030 | // Make the same messages as V8.
|
---|
4031 | if (state.eat(0x29 /* ) */)) {
|
---|
4032 | state.raise("Unmatched ')'");
|
---|
4033 | }
|
---|
4034 | if (state.eat(0x5D /* ] */) || state.eat(0x7D /* } */)) {
|
---|
4035 | state.raise("Lone quantifier brackets");
|
---|
4036 | }
|
---|
4037 | }
|
---|
4038 | if (state.maxBackReference > state.numCapturingParens) {
|
---|
4039 | state.raise("Invalid escape");
|
---|
4040 | }
|
---|
4041 | for (var i = 0, list = state.backReferenceNames; i < list.length; i += 1) {
|
---|
4042 | var name = list[i];
|
---|
4043 |
|
---|
4044 | if (!state.groupNames[name]) {
|
---|
4045 | state.raise("Invalid named capture referenced");
|
---|
4046 | }
|
---|
4047 | }
|
---|
4048 | };
|
---|
4049 |
|
---|
4050 | // https://www.ecma-international.org/ecma-262/8.0/#prod-Disjunction
|
---|
4051 | pp$1.regexp_disjunction = function(state) {
|
---|
4052 | var trackDisjunction = this.options.ecmaVersion >= 16;
|
---|
4053 | if (trackDisjunction) { state.branchID = new BranchID(state.branchID, null); }
|
---|
4054 | this.regexp_alternative(state);
|
---|
4055 | while (state.eat(0x7C /* | */)) {
|
---|
4056 | if (trackDisjunction) { state.branchID = state.branchID.sibling(); }
|
---|
4057 | this.regexp_alternative(state);
|
---|
4058 | }
|
---|
4059 | if (trackDisjunction) { state.branchID = state.branchID.parent; }
|
---|
4060 |
|
---|
4061 | // Make the same message as V8.
|
---|
4062 | if (this.regexp_eatQuantifier(state, true)) {
|
---|
4063 | state.raise("Nothing to repeat");
|
---|
4064 | }
|
---|
4065 | if (state.eat(0x7B /* { */)) {
|
---|
4066 | state.raise("Lone quantifier brackets");
|
---|
4067 | }
|
---|
4068 | };
|
---|
4069 |
|
---|
4070 | // https://www.ecma-international.org/ecma-262/8.0/#prod-Alternative
|
---|
4071 | pp$1.regexp_alternative = function(state) {
|
---|
4072 | while (state.pos < state.source.length && this.regexp_eatTerm(state)) {}
|
---|
4073 | };
|
---|
4074 |
|
---|
4075 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-Term
|
---|
4076 | pp$1.regexp_eatTerm = function(state) {
|
---|
4077 | if (this.regexp_eatAssertion(state)) {
|
---|
4078 | // Handle `QuantifiableAssertion Quantifier` alternative.
|
---|
4079 | // `state.lastAssertionIsQuantifiable` is true if the last eaten Assertion
|
---|
4080 | // is a QuantifiableAssertion.
|
---|
4081 | if (state.lastAssertionIsQuantifiable && this.regexp_eatQuantifier(state)) {
|
---|
4082 | // Make the same message as V8.
|
---|
4083 | if (state.switchU) {
|
---|
4084 | state.raise("Invalid quantifier");
|
---|
4085 | }
|
---|
4086 | }
|
---|
4087 | return true
|
---|
4088 | }
|
---|
4089 |
|
---|
4090 | if (state.switchU ? this.regexp_eatAtom(state) : this.regexp_eatExtendedAtom(state)) {
|
---|
4091 | this.regexp_eatQuantifier(state);
|
---|
4092 | return true
|
---|
4093 | }
|
---|
4094 |
|
---|
4095 | return false
|
---|
4096 | };
|
---|
4097 |
|
---|
4098 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-Assertion
|
---|
4099 | pp$1.regexp_eatAssertion = function(state) {
|
---|
4100 | var start = state.pos;
|
---|
4101 | state.lastAssertionIsQuantifiable = false;
|
---|
4102 |
|
---|
4103 | // ^, $
|
---|
4104 | if (state.eat(0x5E /* ^ */) || state.eat(0x24 /* $ */)) {
|
---|
4105 | return true
|
---|
4106 | }
|
---|
4107 |
|
---|
4108 | // \b \B
|
---|
4109 | if (state.eat(0x5C /* \ */)) {
|
---|
4110 | if (state.eat(0x42 /* B */) || state.eat(0x62 /* b */)) {
|
---|
4111 | return true
|
---|
4112 | }
|
---|
4113 | state.pos = start;
|
---|
4114 | }
|
---|
4115 |
|
---|
4116 | // Lookahead / Lookbehind
|
---|
4117 | if (state.eat(0x28 /* ( */) && state.eat(0x3F /* ? */)) {
|
---|
4118 | var lookbehind = false;
|
---|
4119 | if (this.options.ecmaVersion >= 9) {
|
---|
4120 | lookbehind = state.eat(0x3C /* < */);
|
---|
4121 | }
|
---|
4122 | if (state.eat(0x3D /* = */) || state.eat(0x21 /* ! */)) {
|
---|
4123 | this.regexp_disjunction(state);
|
---|
4124 | if (!state.eat(0x29 /* ) */)) {
|
---|
4125 | state.raise("Unterminated group");
|
---|
4126 | }
|
---|
4127 | state.lastAssertionIsQuantifiable = !lookbehind;
|
---|
4128 | return true
|
---|
4129 | }
|
---|
4130 | }
|
---|
4131 |
|
---|
4132 | state.pos = start;
|
---|
4133 | return false
|
---|
4134 | };
|
---|
4135 |
|
---|
4136 | // https://www.ecma-international.org/ecma-262/8.0/#prod-Quantifier
|
---|
4137 | pp$1.regexp_eatQuantifier = function(state, noError) {
|
---|
4138 | if ( noError === void 0 ) noError = false;
|
---|
4139 |
|
---|
4140 | if (this.regexp_eatQuantifierPrefix(state, noError)) {
|
---|
4141 | state.eat(0x3F /* ? */);
|
---|
4142 | return true
|
---|
4143 | }
|
---|
4144 | return false
|
---|
4145 | };
|
---|
4146 |
|
---|
4147 | // https://www.ecma-international.org/ecma-262/8.0/#prod-QuantifierPrefix
|
---|
4148 | pp$1.regexp_eatQuantifierPrefix = function(state, noError) {
|
---|
4149 | return (
|
---|
4150 | state.eat(0x2A /* * */) ||
|
---|
4151 | state.eat(0x2B /* + */) ||
|
---|
4152 | state.eat(0x3F /* ? */) ||
|
---|
4153 | this.regexp_eatBracedQuantifier(state, noError)
|
---|
4154 | )
|
---|
4155 | };
|
---|
4156 | pp$1.regexp_eatBracedQuantifier = function(state, noError) {
|
---|
4157 | var start = state.pos;
|
---|
4158 | if (state.eat(0x7B /* { */)) {
|
---|
4159 | var min = 0, max = -1;
|
---|
4160 | if (this.regexp_eatDecimalDigits(state)) {
|
---|
4161 | min = state.lastIntValue;
|
---|
4162 | if (state.eat(0x2C /* , */) && this.regexp_eatDecimalDigits(state)) {
|
---|
4163 | max = state.lastIntValue;
|
---|
4164 | }
|
---|
4165 | if (state.eat(0x7D /* } */)) {
|
---|
4166 | // SyntaxError in https://www.ecma-international.org/ecma-262/8.0/#sec-term
|
---|
4167 | if (max !== -1 && max < min && !noError) {
|
---|
4168 | state.raise("numbers out of order in {} quantifier");
|
---|
4169 | }
|
---|
4170 | return true
|
---|
4171 | }
|
---|
4172 | }
|
---|
4173 | if (state.switchU && !noError) {
|
---|
4174 | state.raise("Incomplete quantifier");
|
---|
4175 | }
|
---|
4176 | state.pos = start;
|
---|
4177 | }
|
---|
4178 | return false
|
---|
4179 | };
|
---|
4180 |
|
---|
4181 | // https://www.ecma-international.org/ecma-262/8.0/#prod-Atom
|
---|
4182 | pp$1.regexp_eatAtom = function(state) {
|
---|
4183 | return (
|
---|
4184 | this.regexp_eatPatternCharacters(state) ||
|
---|
4185 | state.eat(0x2E /* . */) ||
|
---|
4186 | this.regexp_eatReverseSolidusAtomEscape(state) ||
|
---|
4187 | this.regexp_eatCharacterClass(state) ||
|
---|
4188 | this.regexp_eatUncapturingGroup(state) ||
|
---|
4189 | this.regexp_eatCapturingGroup(state)
|
---|
4190 | )
|
---|
4191 | };
|
---|
4192 | pp$1.regexp_eatReverseSolidusAtomEscape = function(state) {
|
---|
4193 | var start = state.pos;
|
---|
4194 | if (state.eat(0x5C /* \ */)) {
|
---|
4195 | if (this.regexp_eatAtomEscape(state)) {
|
---|
4196 | return true
|
---|
4197 | }
|
---|
4198 | state.pos = start;
|
---|
4199 | }
|
---|
4200 | return false
|
---|
4201 | };
|
---|
4202 | pp$1.regexp_eatUncapturingGroup = function(state) {
|
---|
4203 | var start = state.pos;
|
---|
4204 | if (state.eat(0x28 /* ( */)) {
|
---|
4205 | if (state.eat(0x3F /* ? */) && state.eat(0x3A /* : */)) {
|
---|
4206 | this.regexp_disjunction(state);
|
---|
4207 | if (state.eat(0x29 /* ) */)) {
|
---|
4208 | return true
|
---|
4209 | }
|
---|
4210 | state.raise("Unterminated group");
|
---|
4211 | }
|
---|
4212 | state.pos = start;
|
---|
4213 | }
|
---|
4214 | return false
|
---|
4215 | };
|
---|
4216 | pp$1.regexp_eatCapturingGroup = function(state) {
|
---|
4217 | if (state.eat(0x28 /* ( */)) {
|
---|
4218 | if (this.options.ecmaVersion >= 9) {
|
---|
4219 | this.regexp_groupSpecifier(state);
|
---|
4220 | } else if (state.current() === 0x3F /* ? */) {
|
---|
4221 | state.raise("Invalid group");
|
---|
4222 | }
|
---|
4223 | this.regexp_disjunction(state);
|
---|
4224 | if (state.eat(0x29 /* ) */)) {
|
---|
4225 | state.numCapturingParens += 1;
|
---|
4226 | return true
|
---|
4227 | }
|
---|
4228 | state.raise("Unterminated group");
|
---|
4229 | }
|
---|
4230 | return false
|
---|
4231 | };
|
---|
4232 |
|
---|
4233 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ExtendedAtom
|
---|
4234 | pp$1.regexp_eatExtendedAtom = function(state) {
|
---|
4235 | return (
|
---|
4236 | state.eat(0x2E /* . */) ||
|
---|
4237 | this.regexp_eatReverseSolidusAtomEscape(state) ||
|
---|
4238 | this.regexp_eatCharacterClass(state) ||
|
---|
4239 | this.regexp_eatUncapturingGroup(state) ||
|
---|
4240 | this.regexp_eatCapturingGroup(state) ||
|
---|
4241 | this.regexp_eatInvalidBracedQuantifier(state) ||
|
---|
4242 | this.regexp_eatExtendedPatternCharacter(state)
|
---|
4243 | )
|
---|
4244 | };
|
---|
4245 |
|
---|
4246 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-InvalidBracedQuantifier
|
---|
4247 | pp$1.regexp_eatInvalidBracedQuantifier = function(state) {
|
---|
4248 | if (this.regexp_eatBracedQuantifier(state, true)) {
|
---|
4249 | state.raise("Nothing to repeat");
|
---|
4250 | }
|
---|
4251 | return false
|
---|
4252 | };
|
---|
4253 |
|
---|
4254 | // https://www.ecma-international.org/ecma-262/8.0/#prod-SyntaxCharacter
|
---|
4255 | pp$1.regexp_eatSyntaxCharacter = function(state) {
|
---|
4256 | var ch = state.current();
|
---|
4257 | if (isSyntaxCharacter(ch)) {
|
---|
4258 | state.lastIntValue = ch;
|
---|
4259 | state.advance();
|
---|
4260 | return true
|
---|
4261 | }
|
---|
4262 | return false
|
---|
4263 | };
|
---|
4264 | function isSyntaxCharacter(ch) {
|
---|
4265 | return (
|
---|
4266 | ch === 0x24 /* $ */ ||
|
---|
4267 | ch >= 0x28 /* ( */ && ch <= 0x2B /* + */ ||
|
---|
4268 | ch === 0x2E /* . */ ||
|
---|
4269 | ch === 0x3F /* ? */ ||
|
---|
4270 | ch >= 0x5B /* [ */ && ch <= 0x5E /* ^ */ ||
|
---|
4271 | ch >= 0x7B /* { */ && ch <= 0x7D /* } */
|
---|
4272 | )
|
---|
4273 | }
|
---|
4274 |
|
---|
4275 | // https://www.ecma-international.org/ecma-262/8.0/#prod-PatternCharacter
|
---|
4276 | // But eat eager.
|
---|
4277 | pp$1.regexp_eatPatternCharacters = function(state) {
|
---|
4278 | var start = state.pos;
|
---|
4279 | var ch = 0;
|
---|
4280 | while ((ch = state.current()) !== -1 && !isSyntaxCharacter(ch)) {
|
---|
4281 | state.advance();
|
---|
4282 | }
|
---|
4283 | return state.pos !== start
|
---|
4284 | };
|
---|
4285 |
|
---|
4286 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ExtendedPatternCharacter
|
---|
4287 | pp$1.regexp_eatExtendedPatternCharacter = function(state) {
|
---|
4288 | var ch = state.current();
|
---|
4289 | if (
|
---|
4290 | ch !== -1 &&
|
---|
4291 | ch !== 0x24 /* $ */ &&
|
---|
4292 | !(ch >= 0x28 /* ( */ && ch <= 0x2B /* + */) &&
|
---|
4293 | ch !== 0x2E /* . */ &&
|
---|
4294 | ch !== 0x3F /* ? */ &&
|
---|
4295 | ch !== 0x5B /* [ */ &&
|
---|
4296 | ch !== 0x5E /* ^ */ &&
|
---|
4297 | ch !== 0x7C /* | */
|
---|
4298 | ) {
|
---|
4299 | state.advance();
|
---|
4300 | return true
|
---|
4301 | }
|
---|
4302 | return false
|
---|
4303 | };
|
---|
4304 |
|
---|
4305 | // GroupSpecifier ::
|
---|
4306 | // [empty]
|
---|
4307 | // `?` GroupName
|
---|
4308 | pp$1.regexp_groupSpecifier = function(state) {
|
---|
4309 | if (state.eat(0x3F /* ? */)) {
|
---|
4310 | if (!this.regexp_eatGroupName(state)) { state.raise("Invalid group"); }
|
---|
4311 | var trackDisjunction = this.options.ecmaVersion >= 16;
|
---|
4312 | var known = state.groupNames[state.lastStringValue];
|
---|
4313 | if (known) {
|
---|
4314 | if (trackDisjunction) {
|
---|
4315 | for (var i = 0, list = known; i < list.length; i += 1) {
|
---|
4316 | var altID = list[i];
|
---|
4317 |
|
---|
4318 | if (!altID.separatedFrom(state.branchID))
|
---|
4319 | { state.raise("Duplicate capture group name"); }
|
---|
4320 | }
|
---|
4321 | } else {
|
---|
4322 | state.raise("Duplicate capture group name");
|
---|
4323 | }
|
---|
4324 | }
|
---|
4325 | if (trackDisjunction) {
|
---|
4326 | (known || (state.groupNames[state.lastStringValue] = [])).push(state.branchID);
|
---|
4327 | } else {
|
---|
4328 | state.groupNames[state.lastStringValue] = true;
|
---|
4329 | }
|
---|
4330 | }
|
---|
4331 | };
|
---|
4332 |
|
---|
4333 | // GroupName ::
|
---|
4334 | // `<` RegExpIdentifierName `>`
|
---|
4335 | // Note: this updates `state.lastStringValue` property with the eaten name.
|
---|
4336 | pp$1.regexp_eatGroupName = function(state) {
|
---|
4337 | state.lastStringValue = "";
|
---|
4338 | if (state.eat(0x3C /* < */)) {
|
---|
4339 | if (this.regexp_eatRegExpIdentifierName(state) && state.eat(0x3E /* > */)) {
|
---|
4340 | return true
|
---|
4341 | }
|
---|
4342 | state.raise("Invalid capture group name");
|
---|
4343 | }
|
---|
4344 | return false
|
---|
4345 | };
|
---|
4346 |
|
---|
4347 | // RegExpIdentifierName ::
|
---|
4348 | // RegExpIdentifierStart
|
---|
4349 | // RegExpIdentifierName RegExpIdentifierPart
|
---|
4350 | // Note: this updates `state.lastStringValue` property with the eaten name.
|
---|
4351 | pp$1.regexp_eatRegExpIdentifierName = function(state) {
|
---|
4352 | state.lastStringValue = "";
|
---|
4353 | if (this.regexp_eatRegExpIdentifierStart(state)) {
|
---|
4354 | state.lastStringValue += codePointToString(state.lastIntValue);
|
---|
4355 | while (this.regexp_eatRegExpIdentifierPart(state)) {
|
---|
4356 | state.lastStringValue += codePointToString(state.lastIntValue);
|
---|
4357 | }
|
---|
4358 | return true
|
---|
4359 | }
|
---|
4360 | return false
|
---|
4361 | };
|
---|
4362 |
|
---|
4363 | // RegExpIdentifierStart ::
|
---|
4364 | // UnicodeIDStart
|
---|
4365 | // `$`
|
---|
4366 | // `_`
|
---|
4367 | // `\` RegExpUnicodeEscapeSequence[+U]
|
---|
4368 | pp$1.regexp_eatRegExpIdentifierStart = function(state) {
|
---|
4369 | var start = state.pos;
|
---|
4370 | var forceU = this.options.ecmaVersion >= 11;
|
---|
4371 | var ch = state.current(forceU);
|
---|
4372 | state.advance(forceU);
|
---|
4373 |
|
---|
4374 | if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state, forceU)) {
|
---|
4375 | ch = state.lastIntValue;
|
---|
4376 | }
|
---|
4377 | if (isRegExpIdentifierStart(ch)) {
|
---|
4378 | state.lastIntValue = ch;
|
---|
4379 | return true
|
---|
4380 | }
|
---|
4381 |
|
---|
4382 | state.pos = start;
|
---|
4383 | return false
|
---|
4384 | };
|
---|
4385 | function isRegExpIdentifierStart(ch) {
|
---|
4386 | return isIdentifierStart(ch, true) || ch === 0x24 /* $ */ || ch === 0x5F /* _ */
|
---|
4387 | }
|
---|
4388 |
|
---|
4389 | // RegExpIdentifierPart ::
|
---|
4390 | // UnicodeIDContinue
|
---|
4391 | // `$`
|
---|
4392 | // `_`
|
---|
4393 | // `\` RegExpUnicodeEscapeSequence[+U]
|
---|
4394 | // <ZWNJ>
|
---|
4395 | // <ZWJ>
|
---|
4396 | pp$1.regexp_eatRegExpIdentifierPart = function(state) {
|
---|
4397 | var start = state.pos;
|
---|
4398 | var forceU = this.options.ecmaVersion >= 11;
|
---|
4399 | var ch = state.current(forceU);
|
---|
4400 | state.advance(forceU);
|
---|
4401 |
|
---|
4402 | if (ch === 0x5C /* \ */ && this.regexp_eatRegExpUnicodeEscapeSequence(state, forceU)) {
|
---|
4403 | ch = state.lastIntValue;
|
---|
4404 | }
|
---|
4405 | if (isRegExpIdentifierPart(ch)) {
|
---|
4406 | state.lastIntValue = ch;
|
---|
4407 | return true
|
---|
4408 | }
|
---|
4409 |
|
---|
4410 | state.pos = start;
|
---|
4411 | return false
|
---|
4412 | };
|
---|
4413 | function isRegExpIdentifierPart(ch) {
|
---|
4414 | return isIdentifierChar(ch, true) || ch === 0x24 /* $ */ || ch === 0x5F /* _ */ || ch === 0x200C /* <ZWNJ> */ || ch === 0x200D /* <ZWJ> */
|
---|
4415 | }
|
---|
4416 |
|
---|
4417 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-AtomEscape
|
---|
4418 | pp$1.regexp_eatAtomEscape = function(state) {
|
---|
4419 | if (
|
---|
4420 | this.regexp_eatBackReference(state) ||
|
---|
4421 | this.regexp_eatCharacterClassEscape(state) ||
|
---|
4422 | this.regexp_eatCharacterEscape(state) ||
|
---|
4423 | (state.switchN && this.regexp_eatKGroupName(state))
|
---|
4424 | ) {
|
---|
4425 | return true
|
---|
4426 | }
|
---|
4427 | if (state.switchU) {
|
---|
4428 | // Make the same message as V8.
|
---|
4429 | if (state.current() === 0x63 /* c */) {
|
---|
4430 | state.raise("Invalid unicode escape");
|
---|
4431 | }
|
---|
4432 | state.raise("Invalid escape");
|
---|
4433 | }
|
---|
4434 | return false
|
---|
4435 | };
|
---|
4436 | pp$1.regexp_eatBackReference = function(state) {
|
---|
4437 | var start = state.pos;
|
---|
4438 | if (this.regexp_eatDecimalEscape(state)) {
|
---|
4439 | var n = state.lastIntValue;
|
---|
4440 | if (state.switchU) {
|
---|
4441 | // For SyntaxError in https://www.ecma-international.org/ecma-262/8.0/#sec-atomescape
|
---|
4442 | if (n > state.maxBackReference) {
|
---|
4443 | state.maxBackReference = n;
|
---|
4444 | }
|
---|
4445 | return true
|
---|
4446 | }
|
---|
4447 | if (n <= state.numCapturingParens) {
|
---|
4448 | return true
|
---|
4449 | }
|
---|
4450 | state.pos = start;
|
---|
4451 | }
|
---|
4452 | return false
|
---|
4453 | };
|
---|
4454 | pp$1.regexp_eatKGroupName = function(state) {
|
---|
4455 | if (state.eat(0x6B /* k */)) {
|
---|
4456 | if (this.regexp_eatGroupName(state)) {
|
---|
4457 | state.backReferenceNames.push(state.lastStringValue);
|
---|
4458 | return true
|
---|
4459 | }
|
---|
4460 | state.raise("Invalid named reference");
|
---|
4461 | }
|
---|
4462 | return false
|
---|
4463 | };
|
---|
4464 |
|
---|
4465 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-CharacterEscape
|
---|
4466 | pp$1.regexp_eatCharacterEscape = function(state) {
|
---|
4467 | return (
|
---|
4468 | this.regexp_eatControlEscape(state) ||
|
---|
4469 | this.regexp_eatCControlLetter(state) ||
|
---|
4470 | this.regexp_eatZero(state) ||
|
---|
4471 | this.regexp_eatHexEscapeSequence(state) ||
|
---|
4472 | this.regexp_eatRegExpUnicodeEscapeSequence(state, false) ||
|
---|
4473 | (!state.switchU && this.regexp_eatLegacyOctalEscapeSequence(state)) ||
|
---|
4474 | this.regexp_eatIdentityEscape(state)
|
---|
4475 | )
|
---|
4476 | };
|
---|
4477 | pp$1.regexp_eatCControlLetter = function(state) {
|
---|
4478 | var start = state.pos;
|
---|
4479 | if (state.eat(0x63 /* c */)) {
|
---|
4480 | if (this.regexp_eatControlLetter(state)) {
|
---|
4481 | return true
|
---|
4482 | }
|
---|
4483 | state.pos = start;
|
---|
4484 | }
|
---|
4485 | return false
|
---|
4486 | };
|
---|
4487 | pp$1.regexp_eatZero = function(state) {
|
---|
4488 | if (state.current() === 0x30 /* 0 */ && !isDecimalDigit(state.lookahead())) {
|
---|
4489 | state.lastIntValue = 0;
|
---|
4490 | state.advance();
|
---|
4491 | return true
|
---|
4492 | }
|
---|
4493 | return false
|
---|
4494 | };
|
---|
4495 |
|
---|
4496 | // https://www.ecma-international.org/ecma-262/8.0/#prod-ControlEscape
|
---|
4497 | pp$1.regexp_eatControlEscape = function(state) {
|
---|
4498 | var ch = state.current();
|
---|
4499 | if (ch === 0x74 /* t */) {
|
---|
4500 | state.lastIntValue = 0x09; /* \t */
|
---|
4501 | state.advance();
|
---|
4502 | return true
|
---|
4503 | }
|
---|
4504 | if (ch === 0x6E /* n */) {
|
---|
4505 | state.lastIntValue = 0x0A; /* \n */
|
---|
4506 | state.advance();
|
---|
4507 | return true
|
---|
4508 | }
|
---|
4509 | if (ch === 0x76 /* v */) {
|
---|
4510 | state.lastIntValue = 0x0B; /* \v */
|
---|
4511 | state.advance();
|
---|
4512 | return true
|
---|
4513 | }
|
---|
4514 | if (ch === 0x66 /* f */) {
|
---|
4515 | state.lastIntValue = 0x0C; /* \f */
|
---|
4516 | state.advance();
|
---|
4517 | return true
|
---|
4518 | }
|
---|
4519 | if (ch === 0x72 /* r */) {
|
---|
4520 | state.lastIntValue = 0x0D; /* \r */
|
---|
4521 | state.advance();
|
---|
4522 | return true
|
---|
4523 | }
|
---|
4524 | return false
|
---|
4525 | };
|
---|
4526 |
|
---|
4527 | // https://www.ecma-international.org/ecma-262/8.0/#prod-ControlLetter
|
---|
4528 | pp$1.regexp_eatControlLetter = function(state) {
|
---|
4529 | var ch = state.current();
|
---|
4530 | if (isControlLetter(ch)) {
|
---|
4531 | state.lastIntValue = ch % 0x20;
|
---|
4532 | state.advance();
|
---|
4533 | return true
|
---|
4534 | }
|
---|
4535 | return false
|
---|
4536 | };
|
---|
4537 | function isControlLetter(ch) {
|
---|
4538 | return (
|
---|
4539 | (ch >= 0x41 /* A */ && ch <= 0x5A /* Z */) ||
|
---|
4540 | (ch >= 0x61 /* a */ && ch <= 0x7A /* z */)
|
---|
4541 | )
|
---|
4542 | }
|
---|
4543 |
|
---|
4544 | // https://www.ecma-international.org/ecma-262/8.0/#prod-RegExpUnicodeEscapeSequence
|
---|
4545 | pp$1.regexp_eatRegExpUnicodeEscapeSequence = function(state, forceU) {
|
---|
4546 | if ( forceU === void 0 ) forceU = false;
|
---|
4547 |
|
---|
4548 | var start = state.pos;
|
---|
4549 | var switchU = forceU || state.switchU;
|
---|
4550 |
|
---|
4551 | if (state.eat(0x75 /* u */)) {
|
---|
4552 | if (this.regexp_eatFixedHexDigits(state, 4)) {
|
---|
4553 | var lead = state.lastIntValue;
|
---|
4554 | if (switchU && lead >= 0xD800 && lead <= 0xDBFF) {
|
---|
4555 | var leadSurrogateEnd = state.pos;
|
---|
4556 | if (state.eat(0x5C /* \ */) && state.eat(0x75 /* u */) && this.regexp_eatFixedHexDigits(state, 4)) {
|
---|
4557 | var trail = state.lastIntValue;
|
---|
4558 | if (trail >= 0xDC00 && trail <= 0xDFFF) {
|
---|
4559 | state.lastIntValue = (lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000;
|
---|
4560 | return true
|
---|
4561 | }
|
---|
4562 | }
|
---|
4563 | state.pos = leadSurrogateEnd;
|
---|
4564 | state.lastIntValue = lead;
|
---|
4565 | }
|
---|
4566 | return true
|
---|
4567 | }
|
---|
4568 | if (
|
---|
4569 | switchU &&
|
---|
4570 | state.eat(0x7B /* { */) &&
|
---|
4571 | this.regexp_eatHexDigits(state) &&
|
---|
4572 | state.eat(0x7D /* } */) &&
|
---|
4573 | isValidUnicode(state.lastIntValue)
|
---|
4574 | ) {
|
---|
4575 | return true
|
---|
4576 | }
|
---|
4577 | if (switchU) {
|
---|
4578 | state.raise("Invalid unicode escape");
|
---|
4579 | }
|
---|
4580 | state.pos = start;
|
---|
4581 | }
|
---|
4582 |
|
---|
4583 | return false
|
---|
4584 | };
|
---|
4585 | function isValidUnicode(ch) {
|
---|
4586 | return ch >= 0 && ch <= 0x10FFFF
|
---|
4587 | }
|
---|
4588 |
|
---|
4589 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-IdentityEscape
|
---|
4590 | pp$1.regexp_eatIdentityEscape = function(state) {
|
---|
4591 | if (state.switchU) {
|
---|
4592 | if (this.regexp_eatSyntaxCharacter(state)) {
|
---|
4593 | return true
|
---|
4594 | }
|
---|
4595 | if (state.eat(0x2F /* / */)) {
|
---|
4596 | state.lastIntValue = 0x2F; /* / */
|
---|
4597 | return true
|
---|
4598 | }
|
---|
4599 | return false
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | var ch = state.current();
|
---|
4603 | if (ch !== 0x63 /* c */ && (!state.switchN || ch !== 0x6B /* k */)) {
|
---|
4604 | state.lastIntValue = ch;
|
---|
4605 | state.advance();
|
---|
4606 | return true
|
---|
4607 | }
|
---|
4608 |
|
---|
4609 | return false
|
---|
4610 | };
|
---|
4611 |
|
---|
4612 | // https://www.ecma-international.org/ecma-262/8.0/#prod-DecimalEscape
|
---|
4613 | pp$1.regexp_eatDecimalEscape = function(state) {
|
---|
4614 | state.lastIntValue = 0;
|
---|
4615 | var ch = state.current();
|
---|
4616 | if (ch >= 0x31 /* 1 */ && ch <= 0x39 /* 9 */) {
|
---|
4617 | do {
|
---|
4618 | state.lastIntValue = 10 * state.lastIntValue + (ch - 0x30 /* 0 */);
|
---|
4619 | state.advance();
|
---|
4620 | } while ((ch = state.current()) >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */)
|
---|
4621 | return true
|
---|
4622 | }
|
---|
4623 | return false
|
---|
4624 | };
|
---|
4625 |
|
---|
4626 | // Return values used by character set parsing methods, needed to
|
---|
4627 | // forbid negation of sets that can match strings.
|
---|
4628 | var CharSetNone = 0; // Nothing parsed
|
---|
4629 | var CharSetOk = 1; // Construct parsed, cannot contain strings
|
---|
4630 | var CharSetString = 2; // Construct parsed, can contain strings
|
---|
4631 |
|
---|
4632 | // https://www.ecma-international.org/ecma-262/8.0/#prod-CharacterClassEscape
|
---|
4633 | pp$1.regexp_eatCharacterClassEscape = function(state) {
|
---|
4634 | var ch = state.current();
|
---|
4635 |
|
---|
4636 | if (isCharacterClassEscape(ch)) {
|
---|
4637 | state.lastIntValue = -1;
|
---|
4638 | state.advance();
|
---|
4639 | return CharSetOk
|
---|
4640 | }
|
---|
4641 |
|
---|
4642 | var negate = false;
|
---|
4643 | if (
|
---|
4644 | state.switchU &&
|
---|
4645 | this.options.ecmaVersion >= 9 &&
|
---|
4646 | ((negate = ch === 0x50 /* P */) || ch === 0x70 /* p */)
|
---|
4647 | ) {
|
---|
4648 | state.lastIntValue = -1;
|
---|
4649 | state.advance();
|
---|
4650 | var result;
|
---|
4651 | if (
|
---|
4652 | state.eat(0x7B /* { */) &&
|
---|
4653 | (result = this.regexp_eatUnicodePropertyValueExpression(state)) &&
|
---|
4654 | state.eat(0x7D /* } */)
|
---|
4655 | ) {
|
---|
4656 | if (negate && result === CharSetString) { state.raise("Invalid property name"); }
|
---|
4657 | return result
|
---|
4658 | }
|
---|
4659 | state.raise("Invalid property name");
|
---|
4660 | }
|
---|
4661 |
|
---|
4662 | return CharSetNone
|
---|
4663 | };
|
---|
4664 |
|
---|
4665 | function isCharacterClassEscape(ch) {
|
---|
4666 | return (
|
---|
4667 | ch === 0x64 /* d */ ||
|
---|
4668 | ch === 0x44 /* D */ ||
|
---|
4669 | ch === 0x73 /* s */ ||
|
---|
4670 | ch === 0x53 /* S */ ||
|
---|
4671 | ch === 0x77 /* w */ ||
|
---|
4672 | ch === 0x57 /* W */
|
---|
4673 | )
|
---|
4674 | }
|
---|
4675 |
|
---|
4676 | // UnicodePropertyValueExpression ::
|
---|
4677 | // UnicodePropertyName `=` UnicodePropertyValue
|
---|
4678 | // LoneUnicodePropertyNameOrValue
|
---|
4679 | pp$1.regexp_eatUnicodePropertyValueExpression = function(state) {
|
---|
4680 | var start = state.pos;
|
---|
4681 |
|
---|
4682 | // UnicodePropertyName `=` UnicodePropertyValue
|
---|
4683 | if (this.regexp_eatUnicodePropertyName(state) && state.eat(0x3D /* = */)) {
|
---|
4684 | var name = state.lastStringValue;
|
---|
4685 | if (this.regexp_eatUnicodePropertyValue(state)) {
|
---|
4686 | var value = state.lastStringValue;
|
---|
4687 | this.regexp_validateUnicodePropertyNameAndValue(state, name, value);
|
---|
4688 | return CharSetOk
|
---|
4689 | }
|
---|
4690 | }
|
---|
4691 | state.pos = start;
|
---|
4692 |
|
---|
4693 | // LoneUnicodePropertyNameOrValue
|
---|
4694 | if (this.regexp_eatLoneUnicodePropertyNameOrValue(state)) {
|
---|
4695 | var nameOrValue = state.lastStringValue;
|
---|
4696 | return this.regexp_validateUnicodePropertyNameOrValue(state, nameOrValue)
|
---|
4697 | }
|
---|
4698 | return CharSetNone
|
---|
4699 | };
|
---|
4700 |
|
---|
4701 | pp$1.regexp_validateUnicodePropertyNameAndValue = function(state, name, value) {
|
---|
4702 | if (!hasOwn(state.unicodeProperties.nonBinary, name))
|
---|
4703 | { state.raise("Invalid property name"); }
|
---|
4704 | if (!state.unicodeProperties.nonBinary[name].test(value))
|
---|
4705 | { state.raise("Invalid property value"); }
|
---|
4706 | };
|
---|
4707 |
|
---|
4708 | pp$1.regexp_validateUnicodePropertyNameOrValue = function(state, nameOrValue) {
|
---|
4709 | if (state.unicodeProperties.binary.test(nameOrValue)) { return CharSetOk }
|
---|
4710 | if (state.switchV && state.unicodeProperties.binaryOfStrings.test(nameOrValue)) { return CharSetString }
|
---|
4711 | state.raise("Invalid property name");
|
---|
4712 | };
|
---|
4713 |
|
---|
4714 | // UnicodePropertyName ::
|
---|
4715 | // UnicodePropertyNameCharacters
|
---|
4716 | pp$1.regexp_eatUnicodePropertyName = function(state) {
|
---|
4717 | var ch = 0;
|
---|
4718 | state.lastStringValue = "";
|
---|
4719 | while (isUnicodePropertyNameCharacter(ch = state.current())) {
|
---|
4720 | state.lastStringValue += codePointToString(ch);
|
---|
4721 | state.advance();
|
---|
4722 | }
|
---|
4723 | return state.lastStringValue !== ""
|
---|
4724 | };
|
---|
4725 |
|
---|
4726 | function isUnicodePropertyNameCharacter(ch) {
|
---|
4727 | return isControlLetter(ch) || ch === 0x5F /* _ */
|
---|
4728 | }
|
---|
4729 |
|
---|
4730 | // UnicodePropertyValue ::
|
---|
4731 | // UnicodePropertyValueCharacters
|
---|
4732 | pp$1.regexp_eatUnicodePropertyValue = function(state) {
|
---|
4733 | var ch = 0;
|
---|
4734 | state.lastStringValue = "";
|
---|
4735 | while (isUnicodePropertyValueCharacter(ch = state.current())) {
|
---|
4736 | state.lastStringValue += codePointToString(ch);
|
---|
4737 | state.advance();
|
---|
4738 | }
|
---|
4739 | return state.lastStringValue !== ""
|
---|
4740 | };
|
---|
4741 | function isUnicodePropertyValueCharacter(ch) {
|
---|
4742 | return isUnicodePropertyNameCharacter(ch) || isDecimalDigit(ch)
|
---|
4743 | }
|
---|
4744 |
|
---|
4745 | // LoneUnicodePropertyNameOrValue ::
|
---|
4746 | // UnicodePropertyValueCharacters
|
---|
4747 | pp$1.regexp_eatLoneUnicodePropertyNameOrValue = function(state) {
|
---|
4748 | return this.regexp_eatUnicodePropertyValue(state)
|
---|
4749 | };
|
---|
4750 |
|
---|
4751 | // https://www.ecma-international.org/ecma-262/8.0/#prod-CharacterClass
|
---|
4752 | pp$1.regexp_eatCharacterClass = function(state) {
|
---|
4753 | if (state.eat(0x5B /* [ */)) {
|
---|
4754 | var negate = state.eat(0x5E /* ^ */);
|
---|
4755 | var result = this.regexp_classContents(state);
|
---|
4756 | if (!state.eat(0x5D /* ] */))
|
---|
4757 | { state.raise("Unterminated character class"); }
|
---|
4758 | if (negate && result === CharSetString)
|
---|
4759 | { state.raise("Negated character class may contain strings"); }
|
---|
4760 | return true
|
---|
4761 | }
|
---|
4762 | return false
|
---|
4763 | };
|
---|
4764 |
|
---|
4765 | // https://tc39.es/ecma262/#prod-ClassContents
|
---|
4766 | // https://www.ecma-international.org/ecma-262/8.0/#prod-ClassRanges
|
---|
4767 | pp$1.regexp_classContents = function(state) {
|
---|
4768 | if (state.current() === 0x5D /* ] */) { return CharSetOk }
|
---|
4769 | if (state.switchV) { return this.regexp_classSetExpression(state) }
|
---|
4770 | this.regexp_nonEmptyClassRanges(state);
|
---|
4771 | return CharSetOk
|
---|
4772 | };
|
---|
4773 |
|
---|
4774 | // https://www.ecma-international.org/ecma-262/8.0/#prod-NonemptyClassRanges
|
---|
4775 | // https://www.ecma-international.org/ecma-262/8.0/#prod-NonemptyClassRangesNoDash
|
---|
4776 | pp$1.regexp_nonEmptyClassRanges = function(state) {
|
---|
4777 | while (this.regexp_eatClassAtom(state)) {
|
---|
4778 | var left = state.lastIntValue;
|
---|
4779 | if (state.eat(0x2D /* - */) && this.regexp_eatClassAtom(state)) {
|
---|
4780 | var right = state.lastIntValue;
|
---|
4781 | if (state.switchU && (left === -1 || right === -1)) {
|
---|
4782 | state.raise("Invalid character class");
|
---|
4783 | }
|
---|
4784 | if (left !== -1 && right !== -1 && left > right) {
|
---|
4785 | state.raise("Range out of order in character class");
|
---|
4786 | }
|
---|
4787 | }
|
---|
4788 | }
|
---|
4789 | };
|
---|
4790 |
|
---|
4791 | // https://www.ecma-international.org/ecma-262/8.0/#prod-ClassAtom
|
---|
4792 | // https://www.ecma-international.org/ecma-262/8.0/#prod-ClassAtomNoDash
|
---|
4793 | pp$1.regexp_eatClassAtom = function(state) {
|
---|
4794 | var start = state.pos;
|
---|
4795 |
|
---|
4796 | if (state.eat(0x5C /* \ */)) {
|
---|
4797 | if (this.regexp_eatClassEscape(state)) {
|
---|
4798 | return true
|
---|
4799 | }
|
---|
4800 | if (state.switchU) {
|
---|
4801 | // Make the same message as V8.
|
---|
4802 | var ch$1 = state.current();
|
---|
4803 | if (ch$1 === 0x63 /* c */ || isOctalDigit(ch$1)) {
|
---|
4804 | state.raise("Invalid class escape");
|
---|
4805 | }
|
---|
4806 | state.raise("Invalid escape");
|
---|
4807 | }
|
---|
4808 | state.pos = start;
|
---|
4809 | }
|
---|
4810 |
|
---|
4811 | var ch = state.current();
|
---|
4812 | if (ch !== 0x5D /* ] */) {
|
---|
4813 | state.lastIntValue = ch;
|
---|
4814 | state.advance();
|
---|
4815 | return true
|
---|
4816 | }
|
---|
4817 |
|
---|
4818 | return false
|
---|
4819 | };
|
---|
4820 |
|
---|
4821 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ClassEscape
|
---|
4822 | pp$1.regexp_eatClassEscape = function(state) {
|
---|
4823 | var start = state.pos;
|
---|
4824 |
|
---|
4825 | if (state.eat(0x62 /* b */)) {
|
---|
4826 | state.lastIntValue = 0x08; /* <BS> */
|
---|
4827 | return true
|
---|
4828 | }
|
---|
4829 |
|
---|
4830 | if (state.switchU && state.eat(0x2D /* - */)) {
|
---|
4831 | state.lastIntValue = 0x2D; /* - */
|
---|
4832 | return true
|
---|
4833 | }
|
---|
4834 |
|
---|
4835 | if (!state.switchU && state.eat(0x63 /* c */)) {
|
---|
4836 | if (this.regexp_eatClassControlLetter(state)) {
|
---|
4837 | return true
|
---|
4838 | }
|
---|
4839 | state.pos = start;
|
---|
4840 | }
|
---|
4841 |
|
---|
4842 | return (
|
---|
4843 | this.regexp_eatCharacterClassEscape(state) ||
|
---|
4844 | this.regexp_eatCharacterEscape(state)
|
---|
4845 | )
|
---|
4846 | };
|
---|
4847 |
|
---|
4848 | // https://tc39.es/ecma262/#prod-ClassSetExpression
|
---|
4849 | // https://tc39.es/ecma262/#prod-ClassUnion
|
---|
4850 | // https://tc39.es/ecma262/#prod-ClassIntersection
|
---|
4851 | // https://tc39.es/ecma262/#prod-ClassSubtraction
|
---|
4852 | pp$1.regexp_classSetExpression = function(state) {
|
---|
4853 | var result = CharSetOk, subResult;
|
---|
4854 | if (this.regexp_eatClassSetRange(state)) ; else if (subResult = this.regexp_eatClassSetOperand(state)) {
|
---|
4855 | if (subResult === CharSetString) { result = CharSetString; }
|
---|
4856 | // https://tc39.es/ecma262/#prod-ClassIntersection
|
---|
4857 | var start = state.pos;
|
---|
4858 | while (state.eatChars([0x26, 0x26] /* && */)) {
|
---|
4859 | if (
|
---|
4860 | state.current() !== 0x26 /* & */ &&
|
---|
4861 | (subResult = this.regexp_eatClassSetOperand(state))
|
---|
4862 | ) {
|
---|
4863 | if (subResult !== CharSetString) { result = CharSetOk; }
|
---|
4864 | continue
|
---|
4865 | }
|
---|
4866 | state.raise("Invalid character in character class");
|
---|
4867 | }
|
---|
4868 | if (start !== state.pos) { return result }
|
---|
4869 | // https://tc39.es/ecma262/#prod-ClassSubtraction
|
---|
4870 | while (state.eatChars([0x2D, 0x2D] /* -- */)) {
|
---|
4871 | if (this.regexp_eatClassSetOperand(state)) { continue }
|
---|
4872 | state.raise("Invalid character in character class");
|
---|
4873 | }
|
---|
4874 | if (start !== state.pos) { return result }
|
---|
4875 | } else {
|
---|
4876 | state.raise("Invalid character in character class");
|
---|
4877 | }
|
---|
4878 | // https://tc39.es/ecma262/#prod-ClassUnion
|
---|
4879 | for (;;) {
|
---|
4880 | if (this.regexp_eatClassSetRange(state)) { continue }
|
---|
4881 | subResult = this.regexp_eatClassSetOperand(state);
|
---|
4882 | if (!subResult) { return result }
|
---|
4883 | if (subResult === CharSetString) { result = CharSetString; }
|
---|
4884 | }
|
---|
4885 | };
|
---|
4886 |
|
---|
4887 | // https://tc39.es/ecma262/#prod-ClassSetRange
|
---|
4888 | pp$1.regexp_eatClassSetRange = function(state) {
|
---|
4889 | var start = state.pos;
|
---|
4890 | if (this.regexp_eatClassSetCharacter(state)) {
|
---|
4891 | var left = state.lastIntValue;
|
---|
4892 | if (state.eat(0x2D /* - */) && this.regexp_eatClassSetCharacter(state)) {
|
---|
4893 | var right = state.lastIntValue;
|
---|
4894 | if (left !== -1 && right !== -1 && left > right) {
|
---|
4895 | state.raise("Range out of order in character class");
|
---|
4896 | }
|
---|
4897 | return true
|
---|
4898 | }
|
---|
4899 | state.pos = start;
|
---|
4900 | }
|
---|
4901 | return false
|
---|
4902 | };
|
---|
4903 |
|
---|
4904 | // https://tc39.es/ecma262/#prod-ClassSetOperand
|
---|
4905 | pp$1.regexp_eatClassSetOperand = function(state) {
|
---|
4906 | if (this.regexp_eatClassSetCharacter(state)) { return CharSetOk }
|
---|
4907 | return this.regexp_eatClassStringDisjunction(state) || this.regexp_eatNestedClass(state)
|
---|
4908 | };
|
---|
4909 |
|
---|
4910 | // https://tc39.es/ecma262/#prod-NestedClass
|
---|
4911 | pp$1.regexp_eatNestedClass = function(state) {
|
---|
4912 | var start = state.pos;
|
---|
4913 | if (state.eat(0x5B /* [ */)) {
|
---|
4914 | var negate = state.eat(0x5E /* ^ */);
|
---|
4915 | var result = this.regexp_classContents(state);
|
---|
4916 | if (state.eat(0x5D /* ] */)) {
|
---|
4917 | if (negate && result === CharSetString) {
|
---|
4918 | state.raise("Negated character class may contain strings");
|
---|
4919 | }
|
---|
4920 | return result
|
---|
4921 | }
|
---|
4922 | state.pos = start;
|
---|
4923 | }
|
---|
4924 | if (state.eat(0x5C /* \ */)) {
|
---|
4925 | var result$1 = this.regexp_eatCharacterClassEscape(state);
|
---|
4926 | if (result$1) {
|
---|
4927 | return result$1
|
---|
4928 | }
|
---|
4929 | state.pos = start;
|
---|
4930 | }
|
---|
4931 | return null
|
---|
4932 | };
|
---|
4933 |
|
---|
4934 | // https://tc39.es/ecma262/#prod-ClassStringDisjunction
|
---|
4935 | pp$1.regexp_eatClassStringDisjunction = function(state) {
|
---|
4936 | var start = state.pos;
|
---|
4937 | if (state.eatChars([0x5C, 0x71] /* \q */)) {
|
---|
4938 | if (state.eat(0x7B /* { */)) {
|
---|
4939 | var result = this.regexp_classStringDisjunctionContents(state);
|
---|
4940 | if (state.eat(0x7D /* } */)) {
|
---|
4941 | return result
|
---|
4942 | }
|
---|
4943 | } else {
|
---|
4944 | // Make the same message as V8.
|
---|
4945 | state.raise("Invalid escape");
|
---|
4946 | }
|
---|
4947 | state.pos = start;
|
---|
4948 | }
|
---|
4949 | return null
|
---|
4950 | };
|
---|
4951 |
|
---|
4952 | // https://tc39.es/ecma262/#prod-ClassStringDisjunctionContents
|
---|
4953 | pp$1.regexp_classStringDisjunctionContents = function(state) {
|
---|
4954 | var result = this.regexp_classString(state);
|
---|
4955 | while (state.eat(0x7C /* | */)) {
|
---|
4956 | if (this.regexp_classString(state) === CharSetString) { result = CharSetString; }
|
---|
4957 | }
|
---|
4958 | return result
|
---|
4959 | };
|
---|
4960 |
|
---|
4961 | // https://tc39.es/ecma262/#prod-ClassString
|
---|
4962 | // https://tc39.es/ecma262/#prod-NonEmptyClassString
|
---|
4963 | pp$1.regexp_classString = function(state) {
|
---|
4964 | var count = 0;
|
---|
4965 | while (this.regexp_eatClassSetCharacter(state)) { count++; }
|
---|
4966 | return count === 1 ? CharSetOk : CharSetString
|
---|
4967 | };
|
---|
4968 |
|
---|
4969 | // https://tc39.es/ecma262/#prod-ClassSetCharacter
|
---|
4970 | pp$1.regexp_eatClassSetCharacter = function(state) {
|
---|
4971 | var start = state.pos;
|
---|
4972 | if (state.eat(0x5C /* \ */)) {
|
---|
4973 | if (
|
---|
4974 | this.regexp_eatCharacterEscape(state) ||
|
---|
4975 | this.regexp_eatClassSetReservedPunctuator(state)
|
---|
4976 | ) {
|
---|
4977 | return true
|
---|
4978 | }
|
---|
4979 | if (state.eat(0x62 /* b */)) {
|
---|
4980 | state.lastIntValue = 0x08; /* <BS> */
|
---|
4981 | return true
|
---|
4982 | }
|
---|
4983 | state.pos = start;
|
---|
4984 | return false
|
---|
4985 | }
|
---|
4986 | var ch = state.current();
|
---|
4987 | if (ch < 0 || ch === state.lookahead() && isClassSetReservedDoublePunctuatorCharacter(ch)) { return false }
|
---|
4988 | if (isClassSetSyntaxCharacter(ch)) { return false }
|
---|
4989 | state.advance();
|
---|
4990 | state.lastIntValue = ch;
|
---|
4991 | return true
|
---|
4992 | };
|
---|
4993 |
|
---|
4994 | // https://tc39.es/ecma262/#prod-ClassSetReservedDoublePunctuator
|
---|
4995 | function isClassSetReservedDoublePunctuatorCharacter(ch) {
|
---|
4996 | return (
|
---|
4997 | ch === 0x21 /* ! */ ||
|
---|
4998 | ch >= 0x23 /* # */ && ch <= 0x26 /* & */ ||
|
---|
4999 | ch >= 0x2A /* * */ && ch <= 0x2C /* , */ ||
|
---|
5000 | ch === 0x2E /* . */ ||
|
---|
5001 | ch >= 0x3A /* : */ && ch <= 0x40 /* @ */ ||
|
---|
5002 | ch === 0x5E /* ^ */ ||
|
---|
5003 | ch === 0x60 /* ` */ ||
|
---|
5004 | ch === 0x7E /* ~ */
|
---|
5005 | )
|
---|
5006 | }
|
---|
5007 |
|
---|
5008 | // https://tc39.es/ecma262/#prod-ClassSetSyntaxCharacter
|
---|
5009 | function isClassSetSyntaxCharacter(ch) {
|
---|
5010 | return (
|
---|
5011 | ch === 0x28 /* ( */ ||
|
---|
5012 | ch === 0x29 /* ) */ ||
|
---|
5013 | ch === 0x2D /* - */ ||
|
---|
5014 | ch === 0x2F /* / */ ||
|
---|
5015 | ch >= 0x5B /* [ */ && ch <= 0x5D /* ] */ ||
|
---|
5016 | ch >= 0x7B /* { */ && ch <= 0x7D /* } */
|
---|
5017 | )
|
---|
5018 | }
|
---|
5019 |
|
---|
5020 | // https://tc39.es/ecma262/#prod-ClassSetReservedPunctuator
|
---|
5021 | pp$1.regexp_eatClassSetReservedPunctuator = function(state) {
|
---|
5022 | var ch = state.current();
|
---|
5023 | if (isClassSetReservedPunctuator(ch)) {
|
---|
5024 | state.lastIntValue = ch;
|
---|
5025 | state.advance();
|
---|
5026 | return true
|
---|
5027 | }
|
---|
5028 | return false
|
---|
5029 | };
|
---|
5030 |
|
---|
5031 | // https://tc39.es/ecma262/#prod-ClassSetReservedPunctuator
|
---|
5032 | function isClassSetReservedPunctuator(ch) {
|
---|
5033 | return (
|
---|
5034 | ch === 0x21 /* ! */ ||
|
---|
5035 | ch === 0x23 /* # */ ||
|
---|
5036 | ch === 0x25 /* % */ ||
|
---|
5037 | ch === 0x26 /* & */ ||
|
---|
5038 | ch === 0x2C /* , */ ||
|
---|
5039 | ch === 0x2D /* - */ ||
|
---|
5040 | ch >= 0x3A /* : */ && ch <= 0x3E /* > */ ||
|
---|
5041 | ch === 0x40 /* @ */ ||
|
---|
5042 | ch === 0x60 /* ` */ ||
|
---|
5043 | ch === 0x7E /* ~ */
|
---|
5044 | )
|
---|
5045 | }
|
---|
5046 |
|
---|
5047 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-ClassControlLetter
|
---|
5048 | pp$1.regexp_eatClassControlLetter = function(state) {
|
---|
5049 | var ch = state.current();
|
---|
5050 | if (isDecimalDigit(ch) || ch === 0x5F /* _ */) {
|
---|
5051 | state.lastIntValue = ch % 0x20;
|
---|
5052 | state.advance();
|
---|
5053 | return true
|
---|
5054 | }
|
---|
5055 | return false
|
---|
5056 | };
|
---|
5057 |
|
---|
5058 | // https://www.ecma-international.org/ecma-262/8.0/#prod-HexEscapeSequence
|
---|
5059 | pp$1.regexp_eatHexEscapeSequence = function(state) {
|
---|
5060 | var start = state.pos;
|
---|
5061 | if (state.eat(0x78 /* x */)) {
|
---|
5062 | if (this.regexp_eatFixedHexDigits(state, 2)) {
|
---|
5063 | return true
|
---|
5064 | }
|
---|
5065 | if (state.switchU) {
|
---|
5066 | state.raise("Invalid escape");
|
---|
5067 | }
|
---|
5068 | state.pos = start;
|
---|
5069 | }
|
---|
5070 | return false
|
---|
5071 | };
|
---|
5072 |
|
---|
5073 | // https://www.ecma-international.org/ecma-262/8.0/#prod-DecimalDigits
|
---|
5074 | pp$1.regexp_eatDecimalDigits = function(state) {
|
---|
5075 | var start = state.pos;
|
---|
5076 | var ch = 0;
|
---|
5077 | state.lastIntValue = 0;
|
---|
5078 | while (isDecimalDigit(ch = state.current())) {
|
---|
5079 | state.lastIntValue = 10 * state.lastIntValue + (ch - 0x30 /* 0 */);
|
---|
5080 | state.advance();
|
---|
5081 | }
|
---|
5082 | return state.pos !== start
|
---|
5083 | };
|
---|
5084 | function isDecimalDigit(ch) {
|
---|
5085 | return ch >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */
|
---|
5086 | }
|
---|
5087 |
|
---|
5088 | // https://www.ecma-international.org/ecma-262/8.0/#prod-HexDigits
|
---|
5089 | pp$1.regexp_eatHexDigits = function(state) {
|
---|
5090 | var start = state.pos;
|
---|
5091 | var ch = 0;
|
---|
5092 | state.lastIntValue = 0;
|
---|
5093 | while (isHexDigit(ch = state.current())) {
|
---|
5094 | state.lastIntValue = 16 * state.lastIntValue + hexToInt(ch);
|
---|
5095 | state.advance();
|
---|
5096 | }
|
---|
5097 | return state.pos !== start
|
---|
5098 | };
|
---|
5099 | function isHexDigit(ch) {
|
---|
5100 | return (
|
---|
5101 | (ch >= 0x30 /* 0 */ && ch <= 0x39 /* 9 */) ||
|
---|
5102 | (ch >= 0x41 /* A */ && ch <= 0x46 /* F */) ||
|
---|
5103 | (ch >= 0x61 /* a */ && ch <= 0x66 /* f */)
|
---|
5104 | )
|
---|
5105 | }
|
---|
5106 | function hexToInt(ch) {
|
---|
5107 | if (ch >= 0x41 /* A */ && ch <= 0x46 /* F */) {
|
---|
5108 | return 10 + (ch - 0x41 /* A */)
|
---|
5109 | }
|
---|
5110 | if (ch >= 0x61 /* a */ && ch <= 0x66 /* f */) {
|
---|
5111 | return 10 + (ch - 0x61 /* a */)
|
---|
5112 | }
|
---|
5113 | return ch - 0x30 /* 0 */
|
---|
5114 | }
|
---|
5115 |
|
---|
5116 | // https://www.ecma-international.org/ecma-262/8.0/#prod-annexB-LegacyOctalEscapeSequence
|
---|
5117 | // Allows only 0-377(octal) i.e. 0-255(decimal).
|
---|
5118 | pp$1.regexp_eatLegacyOctalEscapeSequence = function(state) {
|
---|
5119 | if (this.regexp_eatOctalDigit(state)) {
|
---|
5120 | var n1 = state.lastIntValue;
|
---|
5121 | if (this.regexp_eatOctalDigit(state)) {
|
---|
5122 | var n2 = state.lastIntValue;
|
---|
5123 | if (n1 <= 3 && this.regexp_eatOctalDigit(state)) {
|
---|
5124 | state.lastIntValue = n1 * 64 + n2 * 8 + state.lastIntValue;
|
---|
5125 | } else {
|
---|
5126 | state.lastIntValue = n1 * 8 + n2;
|
---|
5127 | }
|
---|
5128 | } else {
|
---|
5129 | state.lastIntValue = n1;
|
---|
5130 | }
|
---|
5131 | return true
|
---|
5132 | }
|
---|
5133 | return false
|
---|
5134 | };
|
---|
5135 |
|
---|
5136 | // https://www.ecma-international.org/ecma-262/8.0/#prod-OctalDigit
|
---|
5137 | pp$1.regexp_eatOctalDigit = function(state) {
|
---|
5138 | var ch = state.current();
|
---|
5139 | if (isOctalDigit(ch)) {
|
---|
5140 | state.lastIntValue = ch - 0x30; /* 0 */
|
---|
5141 | state.advance();
|
---|
5142 | return true
|
---|
5143 | }
|
---|
5144 | state.lastIntValue = 0;
|
---|
5145 | return false
|
---|
5146 | };
|
---|
5147 | function isOctalDigit(ch) {
|
---|
5148 | return ch >= 0x30 /* 0 */ && ch <= 0x37 /* 7 */
|
---|
5149 | }
|
---|
5150 |
|
---|
5151 | // https://www.ecma-international.org/ecma-262/8.0/#prod-Hex4Digits
|
---|
5152 | // https://www.ecma-international.org/ecma-262/8.0/#prod-HexDigit
|
---|
5153 | // And HexDigit HexDigit in https://www.ecma-international.org/ecma-262/8.0/#prod-HexEscapeSequence
|
---|
5154 | pp$1.regexp_eatFixedHexDigits = function(state, length) {
|
---|
5155 | var start = state.pos;
|
---|
5156 | state.lastIntValue = 0;
|
---|
5157 | for (var i = 0; i < length; ++i) {
|
---|
5158 | var ch = state.current();
|
---|
5159 | if (!isHexDigit(ch)) {
|
---|
5160 | state.pos = start;
|
---|
5161 | return false
|
---|
5162 | }
|
---|
5163 | state.lastIntValue = 16 * state.lastIntValue + hexToInt(ch);
|
---|
5164 | state.advance();
|
---|
5165 | }
|
---|
5166 | return true
|
---|
5167 | };
|
---|
5168 |
|
---|
5169 | // Object type used to represent tokens. Note that normally, tokens
|
---|
5170 | // simply exist as properties on the parser object. This is only
|
---|
5171 | // used for the onToken callback and the external tokenizer.
|
---|
5172 |
|
---|
5173 | var Token = function Token(p) {
|
---|
5174 | this.type = p.type;
|
---|
5175 | this.value = p.value;
|
---|
5176 | this.start = p.start;
|
---|
5177 | this.end = p.end;
|
---|
5178 | if (p.options.locations)
|
---|
5179 | { this.loc = new SourceLocation(p, p.startLoc, p.endLoc); }
|
---|
5180 | if (p.options.ranges)
|
---|
5181 | { this.range = [p.start, p.end]; }
|
---|
5182 | };
|
---|
5183 |
|
---|
5184 | // ## Tokenizer
|
---|
5185 |
|
---|
5186 | var pp = Parser.prototype;
|
---|
5187 |
|
---|
5188 | // Move to the next token
|
---|
5189 |
|
---|
5190 | pp.next = function(ignoreEscapeSequenceInKeyword) {
|
---|
5191 | if (!ignoreEscapeSequenceInKeyword && this.type.keyword && this.containsEsc)
|
---|
5192 | { this.raiseRecoverable(this.start, "Escape sequence in keyword " + this.type.keyword); }
|
---|
5193 | if (this.options.onToken)
|
---|
5194 | { this.options.onToken(new Token(this)); }
|
---|
5195 |
|
---|
5196 | this.lastTokEnd = this.end;
|
---|
5197 | this.lastTokStart = this.start;
|
---|
5198 | this.lastTokEndLoc = this.endLoc;
|
---|
5199 | this.lastTokStartLoc = this.startLoc;
|
---|
5200 | this.nextToken();
|
---|
5201 | };
|
---|
5202 |
|
---|
5203 | pp.getToken = function() {
|
---|
5204 | this.next();
|
---|
5205 | return new Token(this)
|
---|
5206 | };
|
---|
5207 |
|
---|
5208 | // If we're in an ES6 environment, make parsers iterable
|
---|
5209 | if (typeof Symbol !== "undefined")
|
---|
5210 | { pp[Symbol.iterator] = function() {
|
---|
5211 | var this$1$1 = this;
|
---|
5212 |
|
---|
5213 | return {
|
---|
5214 | next: function () {
|
---|
5215 | var token = this$1$1.getToken();
|
---|
5216 | return {
|
---|
5217 | done: token.type === types$1.eof,
|
---|
5218 | value: token
|
---|
5219 | }
|
---|
5220 | }
|
---|
5221 | }
|
---|
5222 | }; }
|
---|
5223 |
|
---|
5224 | // Toggle strict mode. Re-reads the next number or string to please
|
---|
5225 | // pedantic tests (`"use strict"; 010;` should fail).
|
---|
5226 |
|
---|
5227 | // Read a single token, updating the parser object's token-related
|
---|
5228 | // properties.
|
---|
5229 |
|
---|
5230 | pp.nextToken = function() {
|
---|
5231 | var curContext = this.curContext();
|
---|
5232 | if (!curContext || !curContext.preserveSpace) { this.skipSpace(); }
|
---|
5233 |
|
---|
5234 | this.start = this.pos;
|
---|
5235 | if (this.options.locations) { this.startLoc = this.curPosition(); }
|
---|
5236 | if (this.pos >= this.input.length) { return this.finishToken(types$1.eof) }
|
---|
5237 |
|
---|
5238 | if (curContext.override) { return curContext.override(this) }
|
---|
5239 | else { this.readToken(this.fullCharCodeAtPos()); }
|
---|
5240 | };
|
---|
5241 |
|
---|
5242 | pp.readToken = function(code) {
|
---|
5243 | // Identifier or keyword. '\uXXXX' sequences are allowed in
|
---|
5244 | // identifiers, so '\' also dispatches to that.
|
---|
5245 | if (isIdentifierStart(code, this.options.ecmaVersion >= 6) || code === 92 /* '\' */)
|
---|
5246 | { return this.readWord() }
|
---|
5247 |
|
---|
5248 | return this.getTokenFromCode(code)
|
---|
5249 | };
|
---|
5250 |
|
---|
5251 | pp.fullCharCodeAtPos = function() {
|
---|
5252 | var code = this.input.charCodeAt(this.pos);
|
---|
5253 | if (code <= 0xd7ff || code >= 0xdc00) { return code }
|
---|
5254 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5255 | return next <= 0xdbff || next >= 0xe000 ? code : (code << 10) + next - 0x35fdc00
|
---|
5256 | };
|
---|
5257 |
|
---|
5258 | pp.skipBlockComment = function() {
|
---|
5259 | var startLoc = this.options.onComment && this.curPosition();
|
---|
5260 | var start = this.pos, end = this.input.indexOf("*/", this.pos += 2);
|
---|
5261 | if (end === -1) { this.raise(this.pos - 2, "Unterminated comment"); }
|
---|
5262 | this.pos = end + 2;
|
---|
5263 | if (this.options.locations) {
|
---|
5264 | for (var nextBreak = (void 0), pos = start; (nextBreak = nextLineBreak(this.input, pos, this.pos)) > -1;) {
|
---|
5265 | ++this.curLine;
|
---|
5266 | pos = this.lineStart = nextBreak;
|
---|
5267 | }
|
---|
5268 | }
|
---|
5269 | if (this.options.onComment)
|
---|
5270 | { this.options.onComment(true, this.input.slice(start + 2, end), start, this.pos,
|
---|
5271 | startLoc, this.curPosition()); }
|
---|
5272 | };
|
---|
5273 |
|
---|
5274 | pp.skipLineComment = function(startSkip) {
|
---|
5275 | var start = this.pos;
|
---|
5276 | var startLoc = this.options.onComment && this.curPosition();
|
---|
5277 | var ch = this.input.charCodeAt(this.pos += startSkip);
|
---|
5278 | while (this.pos < this.input.length && !isNewLine(ch)) {
|
---|
5279 | ch = this.input.charCodeAt(++this.pos);
|
---|
5280 | }
|
---|
5281 | if (this.options.onComment)
|
---|
5282 | { this.options.onComment(false, this.input.slice(start + startSkip, this.pos), start, this.pos,
|
---|
5283 | startLoc, this.curPosition()); }
|
---|
5284 | };
|
---|
5285 |
|
---|
5286 | // Called at the start of the parse and after every token. Skips
|
---|
5287 | // whitespace and comments, and.
|
---|
5288 |
|
---|
5289 | pp.skipSpace = function() {
|
---|
5290 | loop: while (this.pos < this.input.length) {
|
---|
5291 | var ch = this.input.charCodeAt(this.pos);
|
---|
5292 | switch (ch) {
|
---|
5293 | case 32: case 160: // ' '
|
---|
5294 | ++this.pos;
|
---|
5295 | break
|
---|
5296 | case 13:
|
---|
5297 | if (this.input.charCodeAt(this.pos + 1) === 10) {
|
---|
5298 | ++this.pos;
|
---|
5299 | }
|
---|
5300 | case 10: case 8232: case 8233:
|
---|
5301 | ++this.pos;
|
---|
5302 | if (this.options.locations) {
|
---|
5303 | ++this.curLine;
|
---|
5304 | this.lineStart = this.pos;
|
---|
5305 | }
|
---|
5306 | break
|
---|
5307 | case 47: // '/'
|
---|
5308 | switch (this.input.charCodeAt(this.pos + 1)) {
|
---|
5309 | case 42: // '*'
|
---|
5310 | this.skipBlockComment();
|
---|
5311 | break
|
---|
5312 | case 47:
|
---|
5313 | this.skipLineComment(2);
|
---|
5314 | break
|
---|
5315 | default:
|
---|
5316 | break loop
|
---|
5317 | }
|
---|
5318 | break
|
---|
5319 | default:
|
---|
5320 | if (ch > 8 && ch < 14 || ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
|
---|
5321 | ++this.pos;
|
---|
5322 | } else {
|
---|
5323 | break loop
|
---|
5324 | }
|
---|
5325 | }
|
---|
5326 | }
|
---|
5327 | };
|
---|
5328 |
|
---|
5329 | // Called at the end of every token. Sets `end`, `val`, and
|
---|
5330 | // maintains `context` and `exprAllowed`, and skips the space after
|
---|
5331 | // the token, so that the next one's `start` will point at the
|
---|
5332 | // right position.
|
---|
5333 |
|
---|
5334 | pp.finishToken = function(type, val) {
|
---|
5335 | this.end = this.pos;
|
---|
5336 | if (this.options.locations) { this.endLoc = this.curPosition(); }
|
---|
5337 | var prevType = this.type;
|
---|
5338 | this.type = type;
|
---|
5339 | this.value = val;
|
---|
5340 |
|
---|
5341 | this.updateContext(prevType);
|
---|
5342 | };
|
---|
5343 |
|
---|
5344 | // ### Token reading
|
---|
5345 |
|
---|
5346 | // This is the function that is called to fetch the next token. It
|
---|
5347 | // is somewhat obscure, because it works in character codes rather
|
---|
5348 | // than characters, and because operator parsing has been inlined
|
---|
5349 | // into it.
|
---|
5350 | //
|
---|
5351 | // All in the name of speed.
|
---|
5352 | //
|
---|
5353 | pp.readToken_dot = function() {
|
---|
5354 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5355 | if (next >= 48 && next <= 57) { return this.readNumber(true) }
|
---|
5356 | var next2 = this.input.charCodeAt(this.pos + 2);
|
---|
5357 | if (this.options.ecmaVersion >= 6 && next === 46 && next2 === 46) { // 46 = dot '.'
|
---|
5358 | this.pos += 3;
|
---|
5359 | return this.finishToken(types$1.ellipsis)
|
---|
5360 | } else {
|
---|
5361 | ++this.pos;
|
---|
5362 | return this.finishToken(types$1.dot)
|
---|
5363 | }
|
---|
5364 | };
|
---|
5365 |
|
---|
5366 | pp.readToken_slash = function() { // '/'
|
---|
5367 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5368 | if (this.exprAllowed) { ++this.pos; return this.readRegexp() }
|
---|
5369 | if (next === 61) { return this.finishOp(types$1.assign, 2) }
|
---|
5370 | return this.finishOp(types$1.slash, 1)
|
---|
5371 | };
|
---|
5372 |
|
---|
5373 | pp.readToken_mult_modulo_exp = function(code) { // '%*'
|
---|
5374 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5375 | var size = 1;
|
---|
5376 | var tokentype = code === 42 ? types$1.star : types$1.modulo;
|
---|
5377 |
|
---|
5378 | // exponentiation operator ** and **=
|
---|
5379 | if (this.options.ecmaVersion >= 7 && code === 42 && next === 42) {
|
---|
5380 | ++size;
|
---|
5381 | tokentype = types$1.starstar;
|
---|
5382 | next = this.input.charCodeAt(this.pos + 2);
|
---|
5383 | }
|
---|
5384 |
|
---|
5385 | if (next === 61) { return this.finishOp(types$1.assign, size + 1) }
|
---|
5386 | return this.finishOp(tokentype, size)
|
---|
5387 | };
|
---|
5388 |
|
---|
5389 | pp.readToken_pipe_amp = function(code) { // '|&'
|
---|
5390 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5391 | if (next === code) {
|
---|
5392 | if (this.options.ecmaVersion >= 12) {
|
---|
5393 | var next2 = this.input.charCodeAt(this.pos + 2);
|
---|
5394 | if (next2 === 61) { return this.finishOp(types$1.assign, 3) }
|
---|
5395 | }
|
---|
5396 | return this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2)
|
---|
5397 | }
|
---|
5398 | if (next === 61) { return this.finishOp(types$1.assign, 2) }
|
---|
5399 | return this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1)
|
---|
5400 | };
|
---|
5401 |
|
---|
5402 | pp.readToken_caret = function() { // '^'
|
---|
5403 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5404 | if (next === 61) { return this.finishOp(types$1.assign, 2) }
|
---|
5405 | return this.finishOp(types$1.bitwiseXOR, 1)
|
---|
5406 | };
|
---|
5407 |
|
---|
5408 | pp.readToken_plus_min = function(code) { // '+-'
|
---|
5409 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5410 | if (next === code) {
|
---|
5411 | if (next === 45 && !this.inModule && this.input.charCodeAt(this.pos + 2) === 62 &&
|
---|
5412 | (this.lastTokEnd === 0 || lineBreak.test(this.input.slice(this.lastTokEnd, this.pos)))) {
|
---|
5413 | // A `-->` line comment
|
---|
5414 | this.skipLineComment(3);
|
---|
5415 | this.skipSpace();
|
---|
5416 | return this.nextToken()
|
---|
5417 | }
|
---|
5418 | return this.finishOp(types$1.incDec, 2)
|
---|
5419 | }
|
---|
5420 | if (next === 61) { return this.finishOp(types$1.assign, 2) }
|
---|
5421 | return this.finishOp(types$1.plusMin, 1)
|
---|
5422 | };
|
---|
5423 |
|
---|
5424 | pp.readToken_lt_gt = function(code) { // '<>'
|
---|
5425 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5426 | var size = 1;
|
---|
5427 | if (next === code) {
|
---|
5428 | size = code === 62 && this.input.charCodeAt(this.pos + 2) === 62 ? 3 : 2;
|
---|
5429 | if (this.input.charCodeAt(this.pos + size) === 61) { return this.finishOp(types$1.assign, size + 1) }
|
---|
5430 | return this.finishOp(types$1.bitShift, size)
|
---|
5431 | }
|
---|
5432 | if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.pos + 2) === 45 &&
|
---|
5433 | this.input.charCodeAt(this.pos + 3) === 45) {
|
---|
5434 | // `<!--`, an XML-style comment that should be interpreted as a line comment
|
---|
5435 | this.skipLineComment(4);
|
---|
5436 | this.skipSpace();
|
---|
5437 | return this.nextToken()
|
---|
5438 | }
|
---|
5439 | if (next === 61) { size = 2; }
|
---|
5440 | return this.finishOp(types$1.relational, size)
|
---|
5441 | };
|
---|
5442 |
|
---|
5443 | pp.readToken_eq_excl = function(code) { // '=!'
|
---|
5444 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5445 | if (next === 61) { return this.finishOp(types$1.equality, this.input.charCodeAt(this.pos + 2) === 61 ? 3 : 2) }
|
---|
5446 | if (code === 61 && next === 62 && this.options.ecmaVersion >= 6) { // '=>'
|
---|
5447 | this.pos += 2;
|
---|
5448 | return this.finishToken(types$1.arrow)
|
---|
5449 | }
|
---|
5450 | return this.finishOp(code === 61 ? types$1.eq : types$1.prefix, 1)
|
---|
5451 | };
|
---|
5452 |
|
---|
5453 | pp.readToken_question = function() { // '?'
|
---|
5454 | var ecmaVersion = this.options.ecmaVersion;
|
---|
5455 | if (ecmaVersion >= 11) {
|
---|
5456 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5457 | if (next === 46) {
|
---|
5458 | var next2 = this.input.charCodeAt(this.pos + 2);
|
---|
5459 | if (next2 < 48 || next2 > 57) { return this.finishOp(types$1.questionDot, 2) }
|
---|
5460 | }
|
---|
5461 | if (next === 63) {
|
---|
5462 | if (ecmaVersion >= 12) {
|
---|
5463 | var next2$1 = this.input.charCodeAt(this.pos + 2);
|
---|
5464 | if (next2$1 === 61) { return this.finishOp(types$1.assign, 3) }
|
---|
5465 | }
|
---|
5466 | return this.finishOp(types$1.coalesce, 2)
|
---|
5467 | }
|
---|
5468 | }
|
---|
5469 | return this.finishOp(types$1.question, 1)
|
---|
5470 | };
|
---|
5471 |
|
---|
5472 | pp.readToken_numberSign = function() { // '#'
|
---|
5473 | var ecmaVersion = this.options.ecmaVersion;
|
---|
5474 | var code = 35; // '#'
|
---|
5475 | if (ecmaVersion >= 13) {
|
---|
5476 | ++this.pos;
|
---|
5477 | code = this.fullCharCodeAtPos();
|
---|
5478 | if (isIdentifierStart(code, true) || code === 92 /* '\' */) {
|
---|
5479 | return this.finishToken(types$1.privateId, this.readWord1())
|
---|
5480 | }
|
---|
5481 | }
|
---|
5482 |
|
---|
5483 | this.raise(this.pos, "Unexpected character '" + codePointToString(code) + "'");
|
---|
5484 | };
|
---|
5485 |
|
---|
5486 | pp.getTokenFromCode = function(code) {
|
---|
5487 | switch (code) {
|
---|
5488 | // The interpretation of a dot depends on whether it is followed
|
---|
5489 | // by a digit or another two dots.
|
---|
5490 | case 46: // '.'
|
---|
5491 | return this.readToken_dot()
|
---|
5492 |
|
---|
5493 | // Punctuation tokens.
|
---|
5494 | case 40: ++this.pos; return this.finishToken(types$1.parenL)
|
---|
5495 | case 41: ++this.pos; return this.finishToken(types$1.parenR)
|
---|
5496 | case 59: ++this.pos; return this.finishToken(types$1.semi)
|
---|
5497 | case 44: ++this.pos; return this.finishToken(types$1.comma)
|
---|
5498 | case 91: ++this.pos; return this.finishToken(types$1.bracketL)
|
---|
5499 | case 93: ++this.pos; return this.finishToken(types$1.bracketR)
|
---|
5500 | case 123: ++this.pos; return this.finishToken(types$1.braceL)
|
---|
5501 | case 125: ++this.pos; return this.finishToken(types$1.braceR)
|
---|
5502 | case 58: ++this.pos; return this.finishToken(types$1.colon)
|
---|
5503 |
|
---|
5504 | case 96: // '`'
|
---|
5505 | if (this.options.ecmaVersion < 6) { break }
|
---|
5506 | ++this.pos;
|
---|
5507 | return this.finishToken(types$1.backQuote)
|
---|
5508 |
|
---|
5509 | case 48: // '0'
|
---|
5510 | var next = this.input.charCodeAt(this.pos + 1);
|
---|
5511 | if (next === 120 || next === 88) { return this.readRadixNumber(16) } // '0x', '0X' - hex number
|
---|
5512 | if (this.options.ecmaVersion >= 6) {
|
---|
5513 | if (next === 111 || next === 79) { return this.readRadixNumber(8) } // '0o', '0O' - octal number
|
---|
5514 | if (next === 98 || next === 66) { return this.readRadixNumber(2) } // '0b', '0B' - binary number
|
---|
5515 | }
|
---|
5516 |
|
---|
5517 | // Anything else beginning with a digit is an integer, octal
|
---|
5518 | // number, or float.
|
---|
5519 | case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
|
---|
5520 | return this.readNumber(false)
|
---|
5521 |
|
---|
5522 | // Quotes produce strings.
|
---|
5523 | case 34: case 39: // '"', "'"
|
---|
5524 | return this.readString(code)
|
---|
5525 |
|
---|
5526 | // Operators are parsed inline in tiny state machines. '=' (61) is
|
---|
5527 | // often referred to. `finishOp` simply skips the amount of
|
---|
5528 | // characters it is given as second argument, and returns a token
|
---|
5529 | // of the type given by its first argument.
|
---|
5530 | case 47: // '/'
|
---|
5531 | return this.readToken_slash()
|
---|
5532 |
|
---|
5533 | case 37: case 42: // '%*'
|
---|
5534 | return this.readToken_mult_modulo_exp(code)
|
---|
5535 |
|
---|
5536 | case 124: case 38: // '|&'
|
---|
5537 | return this.readToken_pipe_amp(code)
|
---|
5538 |
|
---|
5539 | case 94: // '^'
|
---|
5540 | return this.readToken_caret()
|
---|
5541 |
|
---|
5542 | case 43: case 45: // '+-'
|
---|
5543 | return this.readToken_plus_min(code)
|
---|
5544 |
|
---|
5545 | case 60: case 62: // '<>'
|
---|
5546 | return this.readToken_lt_gt(code)
|
---|
5547 |
|
---|
5548 | case 61: case 33: // '=!'
|
---|
5549 | return this.readToken_eq_excl(code)
|
---|
5550 |
|
---|
5551 | case 63: // '?'
|
---|
5552 | return this.readToken_question()
|
---|
5553 |
|
---|
5554 | case 126: // '~'
|
---|
5555 | return this.finishOp(types$1.prefix, 1)
|
---|
5556 |
|
---|
5557 | case 35: // '#'
|
---|
5558 | return this.readToken_numberSign()
|
---|
5559 | }
|
---|
5560 |
|
---|
5561 | this.raise(this.pos, "Unexpected character '" + codePointToString(code) + "'");
|
---|
5562 | };
|
---|
5563 |
|
---|
5564 | pp.finishOp = function(type, size) {
|
---|
5565 | var str = this.input.slice(this.pos, this.pos + size);
|
---|
5566 | this.pos += size;
|
---|
5567 | return this.finishToken(type, str)
|
---|
5568 | };
|
---|
5569 |
|
---|
5570 | pp.readRegexp = function() {
|
---|
5571 | var escaped, inClass, start = this.pos;
|
---|
5572 | for (;;) {
|
---|
5573 | if (this.pos >= this.input.length) { this.raise(start, "Unterminated regular expression"); }
|
---|
5574 | var ch = this.input.charAt(this.pos);
|
---|
5575 | if (lineBreak.test(ch)) { this.raise(start, "Unterminated regular expression"); }
|
---|
5576 | if (!escaped) {
|
---|
5577 | if (ch === "[") { inClass = true; }
|
---|
5578 | else if (ch === "]" && inClass) { inClass = false; }
|
---|
5579 | else if (ch === "/" && !inClass) { break }
|
---|
5580 | escaped = ch === "\\";
|
---|
5581 | } else { escaped = false; }
|
---|
5582 | ++this.pos;
|
---|
5583 | }
|
---|
5584 | var pattern = this.input.slice(start, this.pos);
|
---|
5585 | ++this.pos;
|
---|
5586 | var flagsStart = this.pos;
|
---|
5587 | var flags = this.readWord1();
|
---|
5588 | if (this.containsEsc) { this.unexpected(flagsStart); }
|
---|
5589 |
|
---|
5590 | // Validate pattern
|
---|
5591 | var state = this.regexpState || (this.regexpState = new RegExpValidationState(this));
|
---|
5592 | state.reset(start, pattern, flags);
|
---|
5593 | this.validateRegExpFlags(state);
|
---|
5594 | this.validateRegExpPattern(state);
|
---|
5595 |
|
---|
5596 | // Create Literal#value property value.
|
---|
5597 | var value = null;
|
---|
5598 | try {
|
---|
5599 | value = new RegExp(pattern, flags);
|
---|
5600 | } catch (e) {
|
---|
5601 | // ESTree requires null if it failed to instantiate RegExp object.
|
---|
5602 | // https://github.com/estree/estree/blob/a27003adf4fd7bfad44de9cef372a2eacd527b1c/es5.md#regexpliteral
|
---|
5603 | }
|
---|
5604 |
|
---|
5605 | return this.finishToken(types$1.regexp, {pattern: pattern, flags: flags, value: value})
|
---|
5606 | };
|
---|
5607 |
|
---|
5608 | // Read an integer in the given radix. Return null if zero digits
|
---|
5609 | // were read, the integer value otherwise. When `len` is given, this
|
---|
5610 | // will return `null` unless the integer has exactly `len` digits.
|
---|
5611 |
|
---|
5612 | pp.readInt = function(radix, len, maybeLegacyOctalNumericLiteral) {
|
---|
5613 | // `len` is used for character escape sequences. In that case, disallow separators.
|
---|
5614 | var allowSeparators = this.options.ecmaVersion >= 12 && len === undefined;
|
---|
5615 |
|
---|
5616 | // `maybeLegacyOctalNumericLiteral` is true if it doesn't have prefix (0x,0o,0b)
|
---|
5617 | // and isn't fraction part nor exponent part. In that case, if the first digit
|
---|
5618 | // is zero then disallow separators.
|
---|
5619 | var isLegacyOctalNumericLiteral = maybeLegacyOctalNumericLiteral && this.input.charCodeAt(this.pos) === 48;
|
---|
5620 |
|
---|
5621 | var start = this.pos, total = 0, lastCode = 0;
|
---|
5622 | for (var i = 0, e = len == null ? Infinity : len; i < e; ++i, ++this.pos) {
|
---|
5623 | var code = this.input.charCodeAt(this.pos), val = (void 0);
|
---|
5624 |
|
---|
5625 | if (allowSeparators && code === 95) {
|
---|
5626 | if (isLegacyOctalNumericLiteral) { this.raiseRecoverable(this.pos, "Numeric separator is not allowed in legacy octal numeric literals"); }
|
---|
5627 | if (lastCode === 95) { this.raiseRecoverable(this.pos, "Numeric separator must be exactly one underscore"); }
|
---|
5628 | if (i === 0) { this.raiseRecoverable(this.pos, "Numeric separator is not allowed at the first of digits"); }
|
---|
5629 | lastCode = code;
|
---|
5630 | continue
|
---|
5631 | }
|
---|
5632 |
|
---|
5633 | if (code >= 97) { val = code - 97 + 10; } // a
|
---|
5634 | else if (code >= 65) { val = code - 65 + 10; } // A
|
---|
5635 | else if (code >= 48 && code <= 57) { val = code - 48; } // 0-9
|
---|
5636 | else { val = Infinity; }
|
---|
5637 | if (val >= radix) { break }
|
---|
5638 | lastCode = code;
|
---|
5639 | total = total * radix + val;
|
---|
5640 | }
|
---|
5641 |
|
---|
5642 | if (allowSeparators && lastCode === 95) { this.raiseRecoverable(this.pos - 1, "Numeric separator is not allowed at the last of digits"); }
|
---|
5643 | if (this.pos === start || len != null && this.pos - start !== len) { return null }
|
---|
5644 |
|
---|
5645 | return total
|
---|
5646 | };
|
---|
5647 |
|
---|
5648 | function stringToNumber(str, isLegacyOctalNumericLiteral) {
|
---|
5649 | if (isLegacyOctalNumericLiteral) {
|
---|
5650 | return parseInt(str, 8)
|
---|
5651 | }
|
---|
5652 |
|
---|
5653 | // `parseFloat(value)` stops parsing at the first numeric separator then returns a wrong value.
|
---|
5654 | return parseFloat(str.replace(/_/g, ""))
|
---|
5655 | }
|
---|
5656 |
|
---|
5657 | function stringToBigInt(str) {
|
---|
5658 | if (typeof BigInt !== "function") {
|
---|
5659 | return null
|
---|
5660 | }
|
---|
5661 |
|
---|
5662 | // `BigInt(value)` throws syntax error if the string contains numeric separators.
|
---|
5663 | return BigInt(str.replace(/_/g, ""))
|
---|
5664 | }
|
---|
5665 |
|
---|
5666 | pp.readRadixNumber = function(radix) {
|
---|
5667 | var start = this.pos;
|
---|
5668 | this.pos += 2; // 0x
|
---|
5669 | var val = this.readInt(radix);
|
---|
5670 | if (val == null) { this.raise(this.start + 2, "Expected number in radix " + radix); }
|
---|
5671 | if (this.options.ecmaVersion >= 11 && this.input.charCodeAt(this.pos) === 110) {
|
---|
5672 | val = stringToBigInt(this.input.slice(start, this.pos));
|
---|
5673 | ++this.pos;
|
---|
5674 | } else if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
|
---|
5675 | return this.finishToken(types$1.num, val)
|
---|
5676 | };
|
---|
5677 |
|
---|
5678 | // Read an integer, octal integer, or floating-point number.
|
---|
5679 |
|
---|
5680 | pp.readNumber = function(startsWithDot) {
|
---|
5681 | var start = this.pos;
|
---|
5682 | if (!startsWithDot && this.readInt(10, undefined, true) === null) { this.raise(start, "Invalid number"); }
|
---|
5683 | var octal = this.pos - start >= 2 && this.input.charCodeAt(start) === 48;
|
---|
5684 | if (octal && this.strict) { this.raise(start, "Invalid number"); }
|
---|
5685 | var next = this.input.charCodeAt(this.pos);
|
---|
5686 | if (!octal && !startsWithDot && this.options.ecmaVersion >= 11 && next === 110) {
|
---|
5687 | var val$1 = stringToBigInt(this.input.slice(start, this.pos));
|
---|
5688 | ++this.pos;
|
---|
5689 | if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
|
---|
5690 | return this.finishToken(types$1.num, val$1)
|
---|
5691 | }
|
---|
5692 | if (octal && /[89]/.test(this.input.slice(start, this.pos))) { octal = false; }
|
---|
5693 | if (next === 46 && !octal) { // '.'
|
---|
5694 | ++this.pos;
|
---|
5695 | this.readInt(10);
|
---|
5696 | next = this.input.charCodeAt(this.pos);
|
---|
5697 | }
|
---|
5698 | if ((next === 69 || next === 101) && !octal) { // 'eE'
|
---|
5699 | next = this.input.charCodeAt(++this.pos);
|
---|
5700 | if (next === 43 || next === 45) { ++this.pos; } // '+-'
|
---|
5701 | if (this.readInt(10) === null) { this.raise(start, "Invalid number"); }
|
---|
5702 | }
|
---|
5703 | if (isIdentifierStart(this.fullCharCodeAtPos())) { this.raise(this.pos, "Identifier directly after number"); }
|
---|
5704 |
|
---|
5705 | var val = stringToNumber(this.input.slice(start, this.pos), octal);
|
---|
5706 | return this.finishToken(types$1.num, val)
|
---|
5707 | };
|
---|
5708 |
|
---|
5709 | // Read a string value, interpreting backslash-escapes.
|
---|
5710 |
|
---|
5711 | pp.readCodePoint = function() {
|
---|
5712 | var ch = this.input.charCodeAt(this.pos), code;
|
---|
5713 |
|
---|
5714 | if (ch === 123) { // '{'
|
---|
5715 | if (this.options.ecmaVersion < 6) { this.unexpected(); }
|
---|
5716 | var codePos = ++this.pos;
|
---|
5717 | code = this.readHexChar(this.input.indexOf("}", this.pos) - this.pos);
|
---|
5718 | ++this.pos;
|
---|
5719 | if (code > 0x10FFFF) { this.invalidStringToken(codePos, "Code point out of bounds"); }
|
---|
5720 | } else {
|
---|
5721 | code = this.readHexChar(4);
|
---|
5722 | }
|
---|
5723 | return code
|
---|
5724 | };
|
---|
5725 |
|
---|
5726 | pp.readString = function(quote) {
|
---|
5727 | var out = "", chunkStart = ++this.pos;
|
---|
5728 | for (;;) {
|
---|
5729 | if (this.pos >= this.input.length) { this.raise(this.start, "Unterminated string constant"); }
|
---|
5730 | var ch = this.input.charCodeAt(this.pos);
|
---|
5731 | if (ch === quote) { break }
|
---|
5732 | if (ch === 92) { // '\'
|
---|
5733 | out += this.input.slice(chunkStart, this.pos);
|
---|
5734 | out += this.readEscapedChar(false);
|
---|
5735 | chunkStart = this.pos;
|
---|
5736 | } else if (ch === 0x2028 || ch === 0x2029) {
|
---|
5737 | if (this.options.ecmaVersion < 10) { this.raise(this.start, "Unterminated string constant"); }
|
---|
5738 | ++this.pos;
|
---|
5739 | if (this.options.locations) {
|
---|
5740 | this.curLine++;
|
---|
5741 | this.lineStart = this.pos;
|
---|
5742 | }
|
---|
5743 | } else {
|
---|
5744 | if (isNewLine(ch)) { this.raise(this.start, "Unterminated string constant"); }
|
---|
5745 | ++this.pos;
|
---|
5746 | }
|
---|
5747 | }
|
---|
5748 | out += this.input.slice(chunkStart, this.pos++);
|
---|
5749 | return this.finishToken(types$1.string, out)
|
---|
5750 | };
|
---|
5751 |
|
---|
5752 | // Reads template string tokens.
|
---|
5753 |
|
---|
5754 | var INVALID_TEMPLATE_ESCAPE_ERROR = {};
|
---|
5755 |
|
---|
5756 | pp.tryReadTemplateToken = function() {
|
---|
5757 | this.inTemplateElement = true;
|
---|
5758 | try {
|
---|
5759 | this.readTmplToken();
|
---|
5760 | } catch (err) {
|
---|
5761 | if (err === INVALID_TEMPLATE_ESCAPE_ERROR) {
|
---|
5762 | this.readInvalidTemplateToken();
|
---|
5763 | } else {
|
---|
5764 | throw err
|
---|
5765 | }
|
---|
5766 | }
|
---|
5767 |
|
---|
5768 | this.inTemplateElement = false;
|
---|
5769 | };
|
---|
5770 |
|
---|
5771 | pp.invalidStringToken = function(position, message) {
|
---|
5772 | if (this.inTemplateElement && this.options.ecmaVersion >= 9) {
|
---|
5773 | throw INVALID_TEMPLATE_ESCAPE_ERROR
|
---|
5774 | } else {
|
---|
5775 | this.raise(position, message);
|
---|
5776 | }
|
---|
5777 | };
|
---|
5778 |
|
---|
5779 | pp.readTmplToken = function() {
|
---|
5780 | var out = "", chunkStart = this.pos;
|
---|
5781 | for (;;) {
|
---|
5782 | if (this.pos >= this.input.length) { this.raise(this.start, "Unterminated template"); }
|
---|
5783 | var ch = this.input.charCodeAt(this.pos);
|
---|
5784 | if (ch === 96 || ch === 36 && this.input.charCodeAt(this.pos + 1) === 123) { // '`', '${'
|
---|
5785 | if (this.pos === this.start && (this.type === types$1.template || this.type === types$1.invalidTemplate)) {
|
---|
5786 | if (ch === 36) {
|
---|
5787 | this.pos += 2;
|
---|
5788 | return this.finishToken(types$1.dollarBraceL)
|
---|
5789 | } else {
|
---|
5790 | ++this.pos;
|
---|
5791 | return this.finishToken(types$1.backQuote)
|
---|
5792 | }
|
---|
5793 | }
|
---|
5794 | out += this.input.slice(chunkStart, this.pos);
|
---|
5795 | return this.finishToken(types$1.template, out)
|
---|
5796 | }
|
---|
5797 | if (ch === 92) { // '\'
|
---|
5798 | out += this.input.slice(chunkStart, this.pos);
|
---|
5799 | out += this.readEscapedChar(true);
|
---|
5800 | chunkStart = this.pos;
|
---|
5801 | } else if (isNewLine(ch)) {
|
---|
5802 | out += this.input.slice(chunkStart, this.pos);
|
---|
5803 | ++this.pos;
|
---|
5804 | switch (ch) {
|
---|
5805 | case 13:
|
---|
5806 | if (this.input.charCodeAt(this.pos) === 10) { ++this.pos; }
|
---|
5807 | case 10:
|
---|
5808 | out += "\n";
|
---|
5809 | break
|
---|
5810 | default:
|
---|
5811 | out += String.fromCharCode(ch);
|
---|
5812 | break
|
---|
5813 | }
|
---|
5814 | if (this.options.locations) {
|
---|
5815 | ++this.curLine;
|
---|
5816 | this.lineStart = this.pos;
|
---|
5817 | }
|
---|
5818 | chunkStart = this.pos;
|
---|
5819 | } else {
|
---|
5820 | ++this.pos;
|
---|
5821 | }
|
---|
5822 | }
|
---|
5823 | };
|
---|
5824 |
|
---|
5825 | // Reads a template token to search for the end, without validating any escape sequences
|
---|
5826 | pp.readInvalidTemplateToken = function() {
|
---|
5827 | for (; this.pos < this.input.length; this.pos++) {
|
---|
5828 | switch (this.input[this.pos]) {
|
---|
5829 | case "\\":
|
---|
5830 | ++this.pos;
|
---|
5831 | break
|
---|
5832 |
|
---|
5833 | case "$":
|
---|
5834 | if (this.input[this.pos + 1] !== "{") { break }
|
---|
5835 | // fall through
|
---|
5836 | case "`":
|
---|
5837 | return this.finishToken(types$1.invalidTemplate, this.input.slice(this.start, this.pos))
|
---|
5838 |
|
---|
5839 | case "\r":
|
---|
5840 | if (this.input[this.pos + 1] === "\n") { ++this.pos; }
|
---|
5841 | // fall through
|
---|
5842 | case "\n": case "\u2028": case "\u2029":
|
---|
5843 | ++this.curLine;
|
---|
5844 | this.lineStart = this.pos + 1;
|
---|
5845 | break
|
---|
5846 | }
|
---|
5847 | }
|
---|
5848 | this.raise(this.start, "Unterminated template");
|
---|
5849 | };
|
---|
5850 |
|
---|
5851 | // Used to read escaped characters
|
---|
5852 |
|
---|
5853 | pp.readEscapedChar = function(inTemplate) {
|
---|
5854 | var ch = this.input.charCodeAt(++this.pos);
|
---|
5855 | ++this.pos;
|
---|
5856 | switch (ch) {
|
---|
5857 | case 110: return "\n" // 'n' -> '\n'
|
---|
5858 | case 114: return "\r" // 'r' -> '\r'
|
---|
5859 | case 120: return String.fromCharCode(this.readHexChar(2)) // 'x'
|
---|
5860 | case 117: return codePointToString(this.readCodePoint()) // 'u'
|
---|
5861 | case 116: return "\t" // 't' -> '\t'
|
---|
5862 | case 98: return "\b" // 'b' -> '\b'
|
---|
5863 | case 118: return "\u000b" // 'v' -> '\u000b'
|
---|
5864 | case 102: return "\f" // 'f' -> '\f'
|
---|
5865 | case 13: if (this.input.charCodeAt(this.pos) === 10) { ++this.pos; } // '\r\n'
|
---|
5866 | case 10: // ' \n'
|
---|
5867 | if (this.options.locations) { this.lineStart = this.pos; ++this.curLine; }
|
---|
5868 | return ""
|
---|
5869 | case 56:
|
---|
5870 | case 57:
|
---|
5871 | if (this.strict) {
|
---|
5872 | this.invalidStringToken(
|
---|
5873 | this.pos - 1,
|
---|
5874 | "Invalid escape sequence"
|
---|
5875 | );
|
---|
5876 | }
|
---|
5877 | if (inTemplate) {
|
---|
5878 | var codePos = this.pos - 1;
|
---|
5879 |
|
---|
5880 | this.invalidStringToken(
|
---|
5881 | codePos,
|
---|
5882 | "Invalid escape sequence in template string"
|
---|
5883 | );
|
---|
5884 | }
|
---|
5885 | default:
|
---|
5886 | if (ch >= 48 && ch <= 55) {
|
---|
5887 | var octalStr = this.input.substr(this.pos - 1, 3).match(/^[0-7]+/)[0];
|
---|
5888 | var octal = parseInt(octalStr, 8);
|
---|
5889 | if (octal > 255) {
|
---|
5890 | octalStr = octalStr.slice(0, -1);
|
---|
5891 | octal = parseInt(octalStr, 8);
|
---|
5892 | }
|
---|
5893 | this.pos += octalStr.length - 1;
|
---|
5894 | ch = this.input.charCodeAt(this.pos);
|
---|
5895 | if ((octalStr !== "0" || ch === 56 || ch === 57) && (this.strict || inTemplate)) {
|
---|
5896 | this.invalidStringToken(
|
---|
5897 | this.pos - 1 - octalStr.length,
|
---|
5898 | inTemplate
|
---|
5899 | ? "Octal literal in template string"
|
---|
5900 | : "Octal literal in strict mode"
|
---|
5901 | );
|
---|
5902 | }
|
---|
5903 | return String.fromCharCode(octal)
|
---|
5904 | }
|
---|
5905 | if (isNewLine(ch)) {
|
---|
5906 | // Unicode new line characters after \ get removed from output in both
|
---|
5907 | // template literals and strings
|
---|
5908 | if (this.options.locations) { this.lineStart = this.pos; ++this.curLine; }
|
---|
5909 | return ""
|
---|
5910 | }
|
---|
5911 | return String.fromCharCode(ch)
|
---|
5912 | }
|
---|
5913 | };
|
---|
5914 |
|
---|
5915 | // Used to read character escape sequences ('\x', '\u', '\U').
|
---|
5916 |
|
---|
5917 | pp.readHexChar = function(len) {
|
---|
5918 | var codePos = this.pos;
|
---|
5919 | var n = this.readInt(16, len);
|
---|
5920 | if (n === null) { this.invalidStringToken(codePos, "Bad character escape sequence"); }
|
---|
5921 | return n
|
---|
5922 | };
|
---|
5923 |
|
---|
5924 | // Read an identifier, and return it as a string. Sets `this.containsEsc`
|
---|
5925 | // to whether the word contained a '\u' escape.
|
---|
5926 | //
|
---|
5927 | // Incrementally adds only escaped chars, adding other chunks as-is
|
---|
5928 | // as a micro-optimization.
|
---|
5929 |
|
---|
5930 | pp.readWord1 = function() {
|
---|
5931 | this.containsEsc = false;
|
---|
5932 | var word = "", first = true, chunkStart = this.pos;
|
---|
5933 | var astral = this.options.ecmaVersion >= 6;
|
---|
5934 | while (this.pos < this.input.length) {
|
---|
5935 | var ch = this.fullCharCodeAtPos();
|
---|
5936 | if (isIdentifierChar(ch, astral)) {
|
---|
5937 | this.pos += ch <= 0xffff ? 1 : 2;
|
---|
5938 | } else if (ch === 92) { // "\"
|
---|
5939 | this.containsEsc = true;
|
---|
5940 | word += this.input.slice(chunkStart, this.pos);
|
---|
5941 | var escStart = this.pos;
|
---|
5942 | if (this.input.charCodeAt(++this.pos) !== 117) // "u"
|
---|
5943 | { this.invalidStringToken(this.pos, "Expecting Unicode escape sequence \\uXXXX"); }
|
---|
5944 | ++this.pos;
|
---|
5945 | var esc = this.readCodePoint();
|
---|
5946 | if (!(first ? isIdentifierStart : isIdentifierChar)(esc, astral))
|
---|
5947 | { this.invalidStringToken(escStart, "Invalid Unicode escape"); }
|
---|
5948 | word += codePointToString(esc);
|
---|
5949 | chunkStart = this.pos;
|
---|
5950 | } else {
|
---|
5951 | break
|
---|
5952 | }
|
---|
5953 | first = false;
|
---|
5954 | }
|
---|
5955 | return word + this.input.slice(chunkStart, this.pos)
|
---|
5956 | };
|
---|
5957 |
|
---|
5958 | // Read an identifier or keyword token. Will check for reserved
|
---|
5959 | // words when necessary.
|
---|
5960 |
|
---|
5961 | pp.readWord = function() {
|
---|
5962 | var word = this.readWord1();
|
---|
5963 | var type = types$1.name;
|
---|
5964 | if (this.keywords.test(word)) {
|
---|
5965 | type = keywords[word];
|
---|
5966 | }
|
---|
5967 | return this.finishToken(type, word)
|
---|
5968 | };
|
---|
5969 |
|
---|
5970 | // Acorn is a tiny, fast JavaScript parser written in JavaScript.
|
---|
5971 | //
|
---|
5972 | // Acorn was written by Marijn Haverbeke, Ingvar Stepanyan, and
|
---|
5973 | // various contributors and released under an MIT license.
|
---|
5974 | //
|
---|
5975 | // Git repositories for Acorn are available at
|
---|
5976 | //
|
---|
5977 | // http://marijnhaverbeke.nl/git/acorn
|
---|
5978 | // https://github.com/acornjs/acorn.git
|
---|
5979 | //
|
---|
5980 | // Please use the [github bug tracker][ghbt] to report issues.
|
---|
5981 | //
|
---|
5982 | // [ghbt]: https://github.com/acornjs/acorn/issues
|
---|
5983 | //
|
---|
5984 | // [walk]: util/walk.js
|
---|
5985 |
|
---|
5986 |
|
---|
5987 | var version = "8.12.1";
|
---|
5988 |
|
---|
5989 | Parser.acorn = {
|
---|
5990 | Parser: Parser,
|
---|
5991 | version: version,
|
---|
5992 | defaultOptions: defaultOptions,
|
---|
5993 | Position: Position,
|
---|
5994 | SourceLocation: SourceLocation,
|
---|
5995 | getLineInfo: getLineInfo,
|
---|
5996 | Node: Node,
|
---|
5997 | TokenType: TokenType,
|
---|
5998 | tokTypes: types$1,
|
---|
5999 | keywordTypes: keywords,
|
---|
6000 | TokContext: TokContext,
|
---|
6001 | tokContexts: types,
|
---|
6002 | isIdentifierChar: isIdentifierChar,
|
---|
6003 | isIdentifierStart: isIdentifierStart,
|
---|
6004 | Token: Token,
|
---|
6005 | isNewLine: isNewLine,
|
---|
6006 | lineBreak: lineBreak,
|
---|
6007 | lineBreakG: lineBreakG,
|
---|
6008 | nonASCIIwhitespace: nonASCIIwhitespace
|
---|
6009 | };
|
---|
6010 |
|
---|
6011 | // The main exported interface (under `self.acorn` when in the
|
---|
6012 | // browser) is a `parse` function that takes a code string and returns
|
---|
6013 | // an abstract syntax tree as specified by the [ESTree spec][estree].
|
---|
6014 | //
|
---|
6015 | // [estree]: https://github.com/estree/estree
|
---|
6016 |
|
---|
6017 | function parse(input, options) {
|
---|
6018 | return Parser.parse(input, options)
|
---|
6019 | }
|
---|
6020 |
|
---|
6021 | // This function tries to parse a single expression at a given
|
---|
6022 | // offset in a string. Useful for parsing mixed-language formats
|
---|
6023 | // that embed JavaScript expressions.
|
---|
6024 |
|
---|
6025 | function parseExpressionAt(input, pos, options) {
|
---|
6026 | return Parser.parseExpressionAt(input, pos, options)
|
---|
6027 | }
|
---|
6028 |
|
---|
6029 | // Acorn is organized as a tokenizer and a recursive-descent parser.
|
---|
6030 | // The `tokenizer` export provides an interface to the tokenizer.
|
---|
6031 |
|
---|
6032 | function tokenizer(input, options) {
|
---|
6033 | return Parser.tokenizer(input, options)
|
---|
6034 | }
|
---|
6035 |
|
---|
6036 | export { Node, Parser, Position, SourceLocation, TokContext, Token, TokenType, defaultOptions, getLineInfo, isIdentifierChar, isIdentifierStart, isNewLine, keywords as keywordTypes, lineBreak, lineBreakG, nonASCIIwhitespace, parse, parseExpressionAt, types as tokContexts, types$1 as tokTypes, tokenizer, version };
|
---|