lexer.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. let source, pos, end;
  2. let openTokenDepth,
  3. templateDepth,
  4. lastTokenPos,
  5. lastSlashWasDivision,
  6. templateStack,
  7. templateStackDepth,
  8. openTokenPosStack,
  9. openClassPosStack,
  10. nextBraceIsClass,
  11. starExportMap,
  12. lastStarExportSpecifier,
  13. _exports,
  14. reexports;
  15. function resetState () {
  16. openTokenDepth = 0;
  17. templateDepth = -1;
  18. lastTokenPos = -1;
  19. lastSlashWasDivision = false;
  20. templateStack = new Array(1024);
  21. templateStackDepth = 0;
  22. openTokenPosStack = new Array(1024);
  23. openClassPosStack = new Array(1024);
  24. nextBraceIsClass = false;
  25. starExportMap = Object.create(null);
  26. lastStarExportSpecifier = null;
  27. _exports = new Set();
  28. reexports = new Set();
  29. }
  30. // RequireType
  31. const Import = 0;
  32. const ExportAssign = 1;
  33. const ExportStar = 2;
  34. const strictReserved = new Set(['implements', 'interface', 'let', 'package', 'private', 'protected', 'public', 'static', 'yield', 'enum']);
  35. function parseCJS (source, name = '@') {
  36. resetState();
  37. try {
  38. parseSource(source);
  39. }
  40. catch (e) {
  41. e.message += `\n at ${name}:${source.slice(0, pos).split('\n').length}:${pos - source.lastIndexOf('\n', pos - 1)}`;
  42. e.loc = pos;
  43. throw e;
  44. }
  45. const result = { exports: [..._exports], reexports: [...reexports] };
  46. resetState();
  47. return result;
  48. }
  49. function addExport (name) {
  50. if (!strictReserved.has(name))
  51. _exports.add(name);
  52. }
  53. function parseSource (cjsSource) {
  54. source = cjsSource;
  55. pos = -1;
  56. end = source.length - 1;
  57. let ch = 0;
  58. // Handle #!
  59. if (source.charCodeAt(0) === 35/*#*/ && source.charCodeAt(1) === 33/*!*/) {
  60. if (source.length === 2)
  61. return true;
  62. pos += 2;
  63. while (pos++ < end) {
  64. ch = source.charCodeAt(pos);
  65. if (ch === 10/*\n*/ || ch === 13/*\r*/)
  66. break;
  67. }
  68. }
  69. while (pos++ < end) {
  70. ch = source.charCodeAt(pos);
  71. if (ch === 32 || ch < 14 && ch > 8)
  72. continue;
  73. if (openTokenDepth === 0) {
  74. switch (ch) {
  75. case 105/*i*/:
  76. if (source.startsWith('mport', pos + 1) && keywordStart(pos))
  77. throwIfImportStatement();
  78. lastTokenPos = pos;
  79. continue;
  80. case 114/*r*/:
  81. const startPos = pos;
  82. if (tryParseRequire(Import) && keywordStart(startPos))
  83. tryBacktrackAddStarExportBinding(startPos - 1);
  84. lastTokenPos = pos;
  85. continue;
  86. case 95/*_*/:
  87. if (source.startsWith('_export', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) {
  88. pos += 8;
  89. if (source.startsWith('Star', pos))
  90. pos += 4;
  91. if (source.charCodeAt(pos) === 40/*(*/) {
  92. openTokenPosStack[openTokenDepth++] = lastTokenPos;
  93. if (source.charCodeAt(++pos) === 114/*r*/)
  94. tryParseRequire(ExportStar);
  95. }
  96. }
  97. lastTokenPos = pos;
  98. continue;
  99. }
  100. }
  101. switch (ch) {
  102. case 101/*e*/:
  103. if (source.startsWith('xport', pos + 1) && keywordStart(pos)) {
  104. if (source.charCodeAt(pos + 6) === 115/*s*/)
  105. tryParseExportsDotAssign(false);
  106. else if (openTokenDepth === 0)
  107. throwIfExportStatement();
  108. }
  109. break;
  110. case 99/*c*/:
  111. if (keywordStart(pos) && source.startsWith('lass', pos + 1) && isBrOrWs(source.charCodeAt(pos + 5)))
  112. nextBraceIsClass = true;
  113. break;
  114. case 109/*m*/:
  115. if (source.startsWith('odule', pos + 1) && keywordStart(pos))
  116. tryParseModuleExportsDotAssign();
  117. break;
  118. case 79/*O*/:
  119. if (source.startsWith('bject', pos + 1) && keywordStart(pos))
  120. tryParseObjectDefineOrKeys(openTokenDepth === 0);
  121. break;
  122. case 40/*(*/:
  123. openTokenPosStack[openTokenDepth++] = lastTokenPos;
  124. break;
  125. case 41/*)*/:
  126. if (openTokenDepth === 0)
  127. throw new Error('Unexpected closing bracket.');
  128. openTokenDepth--;
  129. break;
  130. case 123/*{*/:
  131. openClassPosStack[openTokenDepth] = nextBraceIsClass;
  132. nextBraceIsClass = false;
  133. openTokenPosStack[openTokenDepth++] = lastTokenPos;
  134. break;
  135. case 125/*}*/:
  136. if (openTokenDepth === 0)
  137. throw new Error('Unexpected closing brace.');
  138. if (openTokenDepth-- === templateDepth) {
  139. templateDepth = templateStack[--templateStackDepth];
  140. templateString();
  141. }
  142. else {
  143. if (templateDepth !== -1 && openTokenDepth < templateDepth)
  144. throw new Error('Unexpected closing brace.');
  145. }
  146. break;
  147. case 60/*>*/:
  148. // TODO: <!-- XML comment support
  149. break;
  150. case 39/*'*/:
  151. singleQuoteString();
  152. break;
  153. case 34/*"*/:
  154. doubleQuoteString();
  155. break;
  156. case 47/*/*/: {
  157. const next_ch = source.charCodeAt(pos + 1);
  158. if (next_ch === 47/*/*/) {
  159. lineComment();
  160. // dont update lastToken
  161. continue;
  162. }
  163. else if (next_ch === 42/***/) {
  164. blockComment();
  165. // dont update lastToken
  166. continue;
  167. }
  168. else {
  169. // Division / regex ambiguity handling based on checking backtrack analysis of:
  170. // - what token came previously (lastToken)
  171. // - if a closing brace or paren, what token came before the corresponding
  172. // opening brace or paren (lastOpenTokenIndex)
  173. const lastToken = source.charCodeAt(lastTokenPos);
  174. if (isExpressionPunctuator(lastToken) &&
  175. !(lastToken === 46/*.*/ && (source.charCodeAt(lastTokenPos - 1) >= 48/*0*/ && source.charCodeAt(lastTokenPos - 1) <= 57/*9*/)) &&
  176. !(lastToken === 43/*+*/ && source.charCodeAt(lastTokenPos - 1) === 43/*+*/) && !(lastToken === 45/*-*/ && source.charCodeAt(lastTokenPos - 1) === 45/*-*/) ||
  177. lastToken === 41/*)*/ && isParenKeyword(openTokenPosStack[openTokenDepth]) ||
  178. lastToken === 125/*}*/ && (isExpressionTerminator(openTokenPosStack[openTokenDepth]) || openClassPosStack[openTokenDepth]) ||
  179. lastToken === 47/*/*/ && lastSlashWasDivision ||
  180. isExpressionKeyword(lastTokenPos) ||
  181. !lastToken) {
  182. regularExpression();
  183. lastSlashWasDivision = false;
  184. }
  185. else {
  186. lastSlashWasDivision = true;
  187. }
  188. }
  189. break;
  190. }
  191. case 96/*`*/:
  192. templateString();
  193. break;
  194. }
  195. lastTokenPos = pos;
  196. }
  197. if (templateDepth !== -1)
  198. throw new Error('Unterminated template.');
  199. if (openTokenDepth)
  200. throw new Error('Unterminated braces.');
  201. }
  202. function tryBacktrackAddStarExportBinding (bPos) {
  203. while (source.charCodeAt(bPos) === 32/* */ && bPos >= 0)
  204. bPos--;
  205. if (source.charCodeAt(bPos) === 61/*=*/) {
  206. bPos--;
  207. while (source.charCodeAt(bPos) === 32/* */ && bPos >= 0)
  208. bPos--;
  209. let codePoint;
  210. const id_end = bPos;
  211. let identifierStart = false;
  212. while ((codePoint = codePointAtLast(bPos)) && bPos >= 0) {
  213. if (codePoint === 92/*\*/)
  214. return;
  215. if (!isIdentifierChar(codePoint, true))
  216. break;
  217. identifierStart = isIdentifierStart(codePoint, true);
  218. bPos -= codePointLen(codePoint);
  219. }
  220. if (identifierStart && source.charCodeAt(bPos) === 32/* */) {
  221. const starExportId = source.slice(bPos + 1, id_end + 1);
  222. while (source.charCodeAt(bPos) === 32/* */ && bPos >= 0)
  223. bPos--;
  224. switch (source.charCodeAt(bPos)) {
  225. case 114/*r*/:
  226. if (!source.startsWith('va', bPos - 2))
  227. return;
  228. break;
  229. case 116/*t*/:
  230. if (!source.startsWith('le', bPos - 2) && !source.startsWith('cons', bPos - 4))
  231. return;
  232. break;
  233. default: return;
  234. }
  235. starExportMap[starExportId] = lastStarExportSpecifier;
  236. }
  237. }
  238. }
  239. function tryParseObjectDefineOrKeys (keys) {
  240. pos += 6;
  241. let revertPos = pos - 1;
  242. let ch = commentWhitespace();
  243. if (ch === 46/*.*/) {
  244. pos++;
  245. ch = commentWhitespace();
  246. if (ch === 100/*d*/ && source.startsWith('efineProperty', pos + 1)) {
  247. while (true) {
  248. pos += 14;
  249. revertPos = pos - 1;
  250. ch = commentWhitespace();
  251. if (ch !== 40/*(*/) break;
  252. pos++;
  253. ch = commentWhitespace();
  254. if (!readExportsOrModuleDotExports(ch)) break;
  255. ch = commentWhitespace();
  256. if (ch !== 44/*,*/) break;
  257. pos++;
  258. ch = commentWhitespace();
  259. if (ch !== 39/*'*/ && ch !== 34/*"*/) break;
  260. let quot = ch;
  261. const exportPos = ++pos;
  262. if (!identifier() || source.charCodeAt(pos) !== quot) break;
  263. const expt = source.slice(exportPos, pos);
  264. pos++;
  265. ch = commentWhitespace();
  266. if (ch !== 44/*,*/) break;
  267. pos++;
  268. ch = commentWhitespace();
  269. if (ch !== 123/*{*/) break;
  270. pos++;
  271. ch = commentWhitespace();
  272. if (ch === 101/*e*/) {
  273. if (!source.startsWith('numerable', pos + 1)) break;
  274. pos += 10;
  275. ch = commentWhitespace();
  276. if (ch !== 58/*:*/) break;
  277. pos++;
  278. ch = commentWhitespace();
  279. if (ch !== 116/*t*/ || !source.startsWith('rue', pos + 1)) break;
  280. pos += 4;
  281. ch = commentWhitespace();
  282. if (ch !== 44) break;
  283. pos++;
  284. ch = commentWhitespace();
  285. }
  286. if (ch === 118/*v*/) {
  287. if (!source.startsWith('alue', pos + 1)) break;
  288. pos += 5;
  289. ch = commentWhitespace();
  290. if (ch !== 58/*:*/) break;
  291. pos++;
  292. addExport(expt);
  293. break;
  294. }
  295. else if (ch === 103/*g*/) {
  296. if (!source.startsWith('et', pos + 1)) break;
  297. pos += 3;
  298. ch = commentWhitespace();
  299. if (ch === 58/*:*/) {
  300. pos++;
  301. ch = commentWhitespace();
  302. if (ch !== 102/*f*/) break;
  303. if (!source.startsWith('unction', pos + 1)) break;
  304. pos += 8;
  305. let lastPos = pos;
  306. ch = commentWhitespace();
  307. if (ch !== 40 && (lastPos === pos || !identifier())) break;
  308. ch = commentWhitespace();
  309. }
  310. if (ch !== 40/*(*/) break;
  311. pos++;
  312. ch = commentWhitespace();
  313. if (ch !== 41/*)*/) break;
  314. pos++;
  315. ch = commentWhitespace();
  316. if (ch !== 123/*{*/) break;
  317. pos++;
  318. ch = commentWhitespace();
  319. if (ch !== 114/*r*/) break;
  320. if (!source.startsWith('eturn', pos + 1)) break;
  321. pos += 6;
  322. ch = commentWhitespace();
  323. if (!identifier()) break;
  324. ch = commentWhitespace();
  325. if (ch === 46/*.*/) {
  326. pos++;
  327. commentWhitespace();
  328. if (!identifier()) break;
  329. ch = commentWhitespace();
  330. }
  331. else if (ch === 91/*[*/) {
  332. pos++;
  333. ch = commentWhitespace();
  334. if (ch === 39/*'*/) singleQuoteString();
  335. else if (ch === 34/*"*/) doubleQuoteString();
  336. else break;
  337. pos++;
  338. ch = commentWhitespace();
  339. if (ch !== 93/*]*/) break;
  340. pos++;
  341. ch = commentWhitespace();
  342. }
  343. if (ch === 59/*;*/) {
  344. pos++;
  345. ch = commentWhitespace();
  346. }
  347. if (ch !== 125/*}*/) break;
  348. pos++;
  349. ch = commentWhitespace();
  350. if (ch !== 125/*}*/) break;
  351. pos++;
  352. ch = commentWhitespace();
  353. if (ch !== 41/*)*/) break;
  354. addExport(expt);
  355. return;
  356. }
  357. break;
  358. }
  359. }
  360. else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) {
  361. while (true) {
  362. pos += 4;
  363. revertPos = pos - 1;
  364. ch = commentWhitespace();
  365. if (ch !== 40/*(*/) break;
  366. pos++;
  367. ch = commentWhitespace();
  368. const id_start = pos;
  369. if (!identifier()) break;
  370. const id = source.slice(id_start, pos);
  371. ch = commentWhitespace();
  372. if (ch !== 41/*)*/) break;
  373. revertPos = pos++;
  374. ch = commentWhitespace();
  375. if (ch !== 46/*.*/) break;
  376. pos++;
  377. ch = commentWhitespace();
  378. if (ch !== 102/*f*/ || !source.startsWith('orEach', pos + 1)) break;
  379. pos += 7;
  380. ch = commentWhitespace();
  381. revertPos = pos - 1;
  382. if (ch !== 40/*(*/) break;
  383. pos++;
  384. ch = commentWhitespace();
  385. if (ch !== 102/*f*/ || !source.startsWith('unction', pos + 1)) break;
  386. pos += 8;
  387. ch = commentWhitespace();
  388. if (ch !== 40/*(*/) break;
  389. pos++;
  390. ch = commentWhitespace();
  391. const it_id_start = pos;
  392. if (!identifier()) break;
  393. const it_id = source.slice(it_id_start, pos);
  394. ch = commentWhitespace();
  395. if (ch !== 41/*)*/) break;
  396. pos++;
  397. ch = commentWhitespace();
  398. if (ch !== 123/*{*/) break;
  399. pos++;
  400. ch = commentWhitespace();
  401. if (ch !== 105/*i*/ || source.charCodeAt(pos + 1) !== 102/*f*/) break;
  402. pos += 2;
  403. ch = commentWhitespace();
  404. if (ch !== 40/*(*/) break;
  405. pos++;
  406. ch = commentWhitespace();
  407. if (!source.startsWith(it_id, pos)) break;
  408. pos += it_id.length;
  409. ch = commentWhitespace();
  410. // `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`? |
  411. if (ch === 61/*=*/) {
  412. if (!source.startsWith('==', pos + 1)) break;
  413. pos += 3;
  414. ch = commentWhitespace();
  415. if (ch !== 34/*"*/ && ch !== 39/*'*/) break;
  416. let quot = ch;
  417. if (!source.startsWith('default', pos + 1)) break;
  418. pos += 8;
  419. ch = commentWhitespace();
  420. if (ch !== quot) break;
  421. pos += 1;
  422. ch = commentWhitespace();
  423. if (ch !== 124/*|*/ || source.charCodeAt(pos + 1) !== 124/*|*/) break;
  424. pos += 2;
  425. ch = commentWhitespace();
  426. if (source.slice(pos, pos + it_id.length) !== it_id) break;
  427. pos += it_id.length;
  428. ch = commentWhitespace();
  429. if (ch !== 61/*=*/ || source.slice(pos + 1, pos + 3) !== '==') break;
  430. pos += 3;
  431. ch = commentWhitespace();
  432. if (ch !== 34/*"*/ && ch !== 39/*'*/) break;
  433. quot = ch;
  434. if (!source.startsWith('__esModule', pos + 1)) break;
  435. pos += 11;
  436. ch = commentWhitespace();
  437. if (ch !== quot) break;
  438. pos += 1;
  439. ch = commentWhitespace();
  440. if (ch !== 41/*)*/) break;
  441. pos += 1;
  442. ch = commentWhitespace();
  443. if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break;
  444. pos += 6;
  445. ch = commentWhitespace();
  446. if (ch === 59/*;*/)
  447. pos++;
  448. ch = commentWhitespace();
  449. }
  450. // `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) `)`
  451. else if (ch === 33/*!*/) {
  452. if (!source.startsWith('==', pos + 1)) break;
  453. pos += 3;
  454. ch = commentWhitespace();
  455. if (ch !== 34/*"*/ && ch !== 39/*'*/) break;
  456. const quot = ch;
  457. if (!source.startsWith('default', pos + 1)) break;
  458. pos += 8;
  459. ch = commentWhitespace();
  460. if (ch !== quot) break;
  461. pos += 1;
  462. ch = commentWhitespace();
  463. if (ch !== 41/*)*/) break;
  464. pos += 1;
  465. ch = commentWhitespace();
  466. }
  467. else break;
  468. // `if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`?
  469. if (ch === 105/*i*/ && source.charCodeAt(pos + 1) === 102/*f*/) {
  470. pos += 2;
  471. ch = commentWhitespace();
  472. if (ch !== 40/*(*/) break;
  473. pos++;
  474. ch = commentWhitespace();
  475. if (!source.startsWith(it_id, pos)) break;
  476. pos += it_id.length;
  477. ch = commentWhitespace();
  478. if (ch !== 105/*i*/ || !source.startsWith('n ', pos + 1)) break;
  479. pos += 3;
  480. ch = commentWhitespace();
  481. if (!readExportsOrModuleDotExports(ch)) break;
  482. ch = commentWhitespace();
  483. if (ch !== 38/*&*/ || source.charCodeAt(pos + 1) !== 38/*&*/) break;
  484. pos += 2;
  485. ch = commentWhitespace();
  486. if (!readExportsOrModuleDotExports(ch)) break;
  487. ch = commentWhitespace();
  488. if (ch !== 91/*[*/) break;
  489. pos++;
  490. ch = commentWhitespace();
  491. if (!source.startsWith(it_id, pos)) break;
  492. pos += it_id.length;
  493. ch = commentWhitespace();
  494. if (ch !== 93/*]*/) break;
  495. pos++;
  496. ch = commentWhitespace();
  497. if (ch !== 61/*=*/ || !source.startsWith('==', pos + 1)) break;
  498. pos += 3;
  499. ch = commentWhitespace();
  500. if (!source.startsWith(id, pos)) break;
  501. pos += id.length;
  502. ch = commentWhitespace();
  503. if (ch !== 91/*[*/) break;
  504. pos++;
  505. ch = commentWhitespace();
  506. if (!source.startsWith(it_id, pos)) break;
  507. pos += it_id.length;
  508. ch = commentWhitespace();
  509. if (ch !== 93/*]*/) break;
  510. pos++;
  511. ch = commentWhitespace();
  512. if (ch !== 41/*)*/) break;
  513. pos++;
  514. ch = commentWhitespace();
  515. if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break;
  516. pos += 6;
  517. ch = commentWhitespace();
  518. if (ch === 59/*;*/)
  519. pos++;
  520. ch = commentWhitespace();
  521. }
  522. // EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]`
  523. if (readExportsOrModuleDotExports(ch)) {
  524. ch = commentWhitespace();
  525. if (ch !== 91/*[*/) break;
  526. pos++;
  527. ch = commentWhitespace();
  528. if (source.slice(pos, pos + it_id.length) !== it_id) break;
  529. pos += it_id.length;
  530. ch = commentWhitespace();
  531. if (ch !== 93/*]*/) break;
  532. pos++;
  533. ch = commentWhitespace();
  534. if (ch !== 61/*=*/) break;
  535. pos++;
  536. ch = commentWhitespace();
  537. if (source.slice(pos, pos + id.length) !== id) break;
  538. pos += id.length;
  539. ch = commentWhitespace();
  540. if (ch !== 91/*[*/) break;
  541. pos++;
  542. ch = commentWhitespace();
  543. if (source.slice(pos, pos + it_id.length) !== it_id) break;
  544. pos += it_id.length;
  545. ch = commentWhitespace();
  546. if (ch !== 93/*]*/) break;
  547. pos++;
  548. ch = commentWhitespace();
  549. if (ch === 59/*;*/) {
  550. pos++;
  551. ch = commentWhitespace();
  552. }
  553. }
  554. // `Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get: function () { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]; } })`
  555. else if (ch === 79/*O*/) {
  556. if (source.slice(pos + 1, pos + 6) !== 'bject') break;
  557. pos += 6;
  558. ch = commentWhitespace();
  559. if (ch !== 46/*.*/) break;
  560. pos++;
  561. ch = commentWhitespace();
  562. if (ch !== 100/*d*/ || !source.startsWith('efineProperty', pos + 1)) break;
  563. pos += 14;
  564. ch = commentWhitespace();
  565. if (ch !== 40/*(*/) break;
  566. pos++;
  567. ch = commentWhitespace();
  568. if (!readExportsOrModuleDotExports(ch)) break;
  569. ch = commentWhitespace();
  570. if (ch !== 44/*,*/) break;
  571. pos++;
  572. ch = commentWhitespace();
  573. if (!source.startsWith(it_id, pos)) break;
  574. pos += it_id.length;
  575. ch = commentWhitespace();
  576. if (ch !== 44/*,*/) break;
  577. pos++;
  578. ch = commentWhitespace();
  579. if (ch !== 123/*{*/) break;
  580. pos++;
  581. ch = commentWhitespace();
  582. if (ch !== 101/*e*/ || !source.startsWith('numerable', pos + 1)) break;
  583. pos += 10;
  584. ch = commentWhitespace();
  585. if (ch !== 58/*:*/) break;
  586. pos++;
  587. ch = commentWhitespace();
  588. if (ch !== 116/*t*/ && !source.startsWith('rue', pos + 1)) break;
  589. pos += 4;
  590. ch = commentWhitespace();
  591. if (ch !== 44/*,*/) break;
  592. pos++;
  593. ch = commentWhitespace();
  594. if (ch !== 103/*g*/ || !source.startsWith('et', pos + 1)) break;
  595. pos += 3;
  596. ch = commentWhitespace();
  597. if (ch !== 58/*:*/) break;
  598. pos++;
  599. ch = commentWhitespace();
  600. if (ch !== 102/*f*/ || !source.startsWith('unction', pos + 1)) break;
  601. pos += 8;
  602. ch = commentWhitespace();
  603. if (ch !== 40/*(*/) break;
  604. pos++;
  605. ch = commentWhitespace();
  606. if (ch !== 41/*)*/) break;
  607. pos++;
  608. ch = commentWhitespace();
  609. if (ch !== 123/*{*/) break;
  610. pos++;
  611. ch = commentWhitespace();
  612. if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break;
  613. pos += 6;
  614. ch = commentWhitespace();
  615. if (!source.startsWith(id, pos)) break;
  616. pos += id.length;
  617. ch = commentWhitespace();
  618. if (ch !== 91/*[*/) break;
  619. pos++;
  620. ch = commentWhitespace();
  621. if (!source.startsWith(it_id, pos)) break;
  622. pos += it_id.length;
  623. ch = commentWhitespace();
  624. if (ch !== 93/*]*/) break;
  625. pos++;
  626. ch = commentWhitespace();
  627. if (ch === 59/*;*/) {
  628. pos++;
  629. ch = commentWhitespace();
  630. }
  631. if (ch !== 125/*}*/) break;
  632. pos++;
  633. ch = commentWhitespace();
  634. if (ch !== 125/*}*/) break;
  635. pos++;
  636. ch = commentWhitespace();
  637. if (ch !== 41/*)*/) break;
  638. pos++;
  639. ch = commentWhitespace();
  640. if (ch === 59/*;*/) {
  641. pos++;
  642. ch = commentWhitespace();
  643. }
  644. }
  645. else break;
  646. if (ch !== 125/*}*/) break;
  647. pos++;
  648. ch = commentWhitespace();
  649. if (ch !== 41/*)*/) break;
  650. const starExportSpecifier = starExportMap[id];
  651. if (starExportSpecifier) {
  652. reexports.add(starExportSpecifier);
  653. pos = revertPos;
  654. return;
  655. }
  656. return;
  657. }
  658. }
  659. }
  660. pos = revertPos;
  661. }
  662. function readExportsOrModuleDotExports (ch) {
  663. const revertPos = pos;
  664. if (ch === 109/*m*/ && source.startsWith('odule', pos + 1)) {
  665. pos += 6;
  666. ch = commentWhitespace();
  667. if (ch !== 46/*.*/) {
  668. pos = revertPos;
  669. return false;
  670. }
  671. pos++;
  672. ch = commentWhitespace();
  673. }
  674. if (ch === 101/*e*/ && source.startsWith('xports', pos + 1)) {
  675. pos += 7;
  676. return true;
  677. }
  678. else {
  679. pos = revertPos;
  680. return false;
  681. }
  682. }
  683. function tryParseModuleExportsDotAssign () {
  684. pos += 6;
  685. const revertPos = pos - 1;
  686. let ch = commentWhitespace();
  687. if (ch === 46/*.*/) {
  688. pos++;
  689. ch = commentWhitespace();
  690. if (ch === 101/*e*/ && source.startsWith('xports', pos + 1)) {
  691. tryParseExportsDotAssign(true);
  692. return;
  693. }
  694. }
  695. pos = revertPos;
  696. }
  697. function tryParseExportsDotAssign (assign) {
  698. pos += 7;
  699. const revertPos = pos - 1;
  700. let ch = commentWhitespace();
  701. switch (ch) {
  702. // exports.asdf
  703. case 46/*.*/: {
  704. pos++;
  705. ch = commentWhitespace();
  706. const startPos = pos;
  707. if (identifier()) {
  708. const endPos = pos;
  709. ch = commentWhitespace();
  710. if (ch === 61/*=*/) {
  711. addExport(source.slice(startPos, endPos));
  712. return;
  713. }
  714. }
  715. break;
  716. }
  717. // exports['asdf']
  718. case 91/*[*/: {
  719. pos++;
  720. ch = commentWhitespace();
  721. if (ch === 39/*'*/ || ch === 34/*"*/) {
  722. pos++;
  723. const startPos = pos;
  724. if (identifier() && source.charCodeAt(pos) === ch) {
  725. const endPos = pos++;
  726. ch = commentWhitespace();
  727. if (ch !== 93/*]*/)
  728. break;
  729. pos++;
  730. ch = commentWhitespace();
  731. if (ch !== 61/*=*/)
  732. break;
  733. addExport(source.slice(startPos, endPos));
  734. }
  735. }
  736. break;
  737. }
  738. // module.exports =
  739. case 61/*=*/: {
  740. if (assign) {
  741. if (reexports.size)
  742. reexports = new Set();
  743. pos++;
  744. ch = commentWhitespace();
  745. // { ... }
  746. if (ch === 123/*{*/) {
  747. tryParseLiteralExports();
  748. return;
  749. }
  750. // require('...')
  751. if (ch === 114/*r*/)
  752. tryParseRequire(ExportAssign);
  753. }
  754. }
  755. }
  756. pos = revertPos;
  757. }
  758. function tryParseRequire (requireType) {
  759. // require('...')
  760. const revertPos = pos;
  761. if (source.startsWith('equire', pos + 1)) {
  762. pos += 7;
  763. let ch = commentWhitespace();
  764. if (ch === 40/*(*/) {
  765. pos++;
  766. ch = commentWhitespace();
  767. const reexportStart = pos + 1;
  768. if (ch === 39/*'*/) {
  769. singleQuoteString();
  770. const reexportEnd = pos++;
  771. ch = commentWhitespace();
  772. if (ch === 41/*)*/) {
  773. switch (requireType) {
  774. case ExportAssign:
  775. reexports.add(source.slice(reexportStart, reexportEnd));
  776. return true;
  777. case ExportStar:
  778. reexports.add(source.slice(reexportStart, reexportEnd));
  779. return true;
  780. default:
  781. lastStarExportSpecifier = source.slice(reexportStart, reexportEnd);
  782. return true;
  783. }
  784. }
  785. }
  786. else if (ch === 34/*"*/) {
  787. doubleQuoteString();
  788. const reexportEnd = pos++;
  789. ch = commentWhitespace();
  790. if (ch === 41/*)*/) {
  791. switch (requireType) {
  792. case ExportAssign:
  793. reexports.add(source.slice(reexportStart, reexportEnd));
  794. return true;
  795. case ExportStar:
  796. reexports.add(source.slice(reexportStart, reexportEnd));
  797. return true;
  798. default:
  799. lastStarExportSpecifier = source.slice(reexportStart, reexportEnd);
  800. return true;
  801. }
  802. }
  803. }
  804. }
  805. pos = revertPos;
  806. }
  807. return false;
  808. }
  809. function tryParseLiteralExports () {
  810. const revertPos = pos - 1;
  811. while (pos++ < end) {
  812. let ch = commentWhitespace();
  813. const startPos = pos;
  814. if (identifier()) {
  815. const endPos = pos;
  816. ch = commentWhitespace();
  817. if (ch === 58/*:*/) {
  818. pos++;
  819. ch = commentWhitespace();
  820. // nothing more complex than identifier expressions for now
  821. if (!identifier()) {
  822. pos = revertPos;
  823. return;
  824. }
  825. ch = source.charCodeAt(pos);
  826. }
  827. addExport(source.slice(startPos, endPos));
  828. }
  829. else if (ch === 46/*.*/ && source.startsWith('..', pos + 1)) {
  830. pos += 3;
  831. if (source.charCodeAt(pos) === 114/*r*/ && tryParseRequire(ExportAssign)) {
  832. pos++;
  833. }
  834. else if (!identifier()) {
  835. pos = revertPos;
  836. return;
  837. }
  838. ch = commentWhitespace();
  839. }
  840. else if (ch === 39/*'*/ || ch === 34/*"*/) {
  841. const startPos = ++pos;
  842. if (identifier() && source.charCodeAt(pos) === ch) {
  843. const endPos = pos++;
  844. ch = commentWhitespace();
  845. if (ch === 58/*:*/) {
  846. pos++;
  847. ch = commentWhitespace();
  848. // nothing more complex than identifier expressions for now
  849. if (!identifier()) {
  850. pos = revertPos;
  851. return;
  852. }
  853. ch = source.charCodeAt(pos);
  854. addExport(source.slice(startPos, endPos));
  855. }
  856. }
  857. }
  858. else {
  859. pos = revertPos;
  860. return;
  861. }
  862. if (ch === 125/*}*/)
  863. return;
  864. if (ch !== 44/*,*/) {
  865. pos = revertPos;
  866. return;
  867. }
  868. }
  869. }
  870. // --- Extracted from AcornJS ---
  871. //(https://github.com/acornjs/acorn/blob/master/acorn/src/identifier.js#L23
  872. //
  873. // MIT License
  874. // Copyright (C) 2012-2018 by various contributors (see AUTHORS)
  875. // Permission is hereby granted, free of charge, to any person obtaining a copy
  876. // of this software and associated documentation files (the "Software"), to deal
  877. // in the Software without restriction, including without limitation the rights
  878. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  879. // copies of the Software, and to permit persons to whom the Software is
  880. // furnished to do so, subject to the following conditions:
  881. // The above copyright notice and this permission notice shall be included in
  882. // all copies or substantial portions of the Software.
  883. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  884. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  885. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  886. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  887. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  888. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  889. // THE SOFTWARE.
  890. // ## Character categories
  891. // Big ugly regular expressions that match characters in the
  892. // whitespace, identifier, and identifier-start categories. These
  893. // are only applied when a character is found to actually have a
  894. // code point above 128.
  895. // Generated by `bin/generate-identifier-regex.js`.
  896. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08c7\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\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-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\u9ffc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7ca\ua7f5-\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"
  897. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf\u1ac0\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\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"
  898. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]")
  899. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]")
  900. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null
  901. // These are a run-length and offset encoded representation of the
  902. // >0xffff code points that are a valid part of identifiers. The
  903. // offset starts at 0x10000, and each pair of numbers represents an
  904. // offset to the next range, and then a size of the range. They were
  905. // generated by bin/generate-identifier-regex.js
  906. // eslint-disable-next-line comma-spacing
  907. const astralIdentifierStartCodes = [0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,14,29,6,37,11,29,3,35,5,7,2,4,43,157,19,35,5,35,5,39,9,51,157,310,10,21,11,7,153,5,3,0,2,43,2,1,4,0,3,22,11,22,10,30,66,18,2,1,11,21,11,25,71,55,7,1,65,0,16,3,2,2,2,28,43,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,56,50,14,50,14,35,349,41,7,1,79,28,11,0,9,21,107,20,28,22,13,52,76,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,85,6,2,0,2,3,2,14,2,9,8,46,39,7,3,1,3,21,2,6,2,1,2,4,4,0,19,0,13,4,159,52,19,3,21,2,31,47,21,1,2,0,185,46,42,3,37,47,21,0,60,42,14,0,72,26,230,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,35,56,264,8,2,36,18,0,50,29,113,6,2,1,2,37,22,0,26,5,2,1,2,31,15,0,328,18,190,0,80,921,103,110,18,195,2749,1070,4050,582,8634,568,8,30,114,29,19,47,17,3,32,20,6,18,689,63,129,74,6,0,67,12,65,1,2,0,29,6135,9,1237,43,8,8952,286,50,2,18,3,9,395,2309,106,6,12,4,8,8,9,5991,84,2,70,2,1,3,0,3,1,3,3,2,11,2,0,2,6,2,64,2,3,3,7,2,6,2,27,2,3,2,4,2,0,4,6,2,339,3,24,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,7,2357,44,11,6,17,0,370,43,1301,196,60,67,8,0,1205,3,2,26,2,1,2,0,3,0,2,9,2,3,2,0,2,0,7,0,5,0,2,0,2,0,2,2,2,1,2,0,3,0,2,0,2,0,2,0,2,0,2,1,2,0,3,3,2,6,2,3,2,3,2,0,2,9,2,16,6,2,2,4,2,16,4421,42717,35,4148,12,221,3,5761,15,7472,3104,541,1507,4938]
  908. // eslint-disable-next-line comma-spacing
  909. const 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,154,10,176,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,6,1,45,0,13,2,49,13,9,3,2,11,83,11,7,0,161,11,6,9,7,3,56,1,2,6,3,1,3,2,10,0,11,1,3,6,4,4,193,17,10,9,5,0,82,19,13,9,214,6,3,8,28,1,83,16,16,9,82,12,9,9,84,14,5,9,243,14,166,9,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,19306,9,135,4,60,6,26,9,1014,0,2,54,8,3,82,0,12,1,19628,1,5319,4,4,5,9,7,3,6,31,3,149,2,1418,49,513,54,5,49,9,0,15,0,23,4,2,14,1361,6,2,16,3,6,2,1,2,4,262,6,10,9,419,13,1495,6,110,6,6,9,4759,9,787719,239]
  910. // This has a complexity linear to the value of the code. The
  911. // assumption is that looking up astral identifier characters is
  912. // rare.
  913. function isInAstralSet(code, set) {
  914. let pos = 0x10000
  915. for (let i = 0; i < set.length; i += 2) {
  916. pos += set[i]
  917. if (pos > code) return false
  918. pos += set[i + 1]
  919. if (pos >= code) return true
  920. }
  921. }
  922. // Test whether a given character code starts an identifier.
  923. function isIdentifierStart(code, astral) {
  924. if (code < 65) return code === 36
  925. if (code < 91) return true
  926. if (code < 97) return code === 95
  927. if (code < 123) return true
  928. if (code <= 0xffff) return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code))
  929. if (astral === false) return false
  930. return isInAstralSet(code, astralIdentifierStartCodes)
  931. }
  932. // Test whether a given character is part of an identifier.
  933. function isIdentifierChar(code, astral) {
  934. if (code < 48) return code === 36
  935. if (code < 58) return true
  936. if (code < 65) return false
  937. if (code < 91) return true
  938. if (code < 97) return code === 95
  939. if (code < 123) return true
  940. if (code <= 0xffff) return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code))
  941. if (astral === false) return false
  942. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes)
  943. }
  944. function identifier () {
  945. let ch = source.codePointAt(pos);
  946. if (!isIdentifierStart(ch, true) || ch === '\\')
  947. return false;
  948. pos += codePointLen(ch);
  949. while (ch = source.codePointAt(pos)) {
  950. if (isIdentifierChar(ch, true)) {
  951. pos += codePointLen(ch);
  952. }
  953. else if (ch === '\\') {
  954. // no identifier escapes support for now
  955. return false;
  956. }
  957. else {
  958. break;
  959. }
  960. }
  961. return true;
  962. }
  963. function codePointLen (ch) {
  964. if (ch < 0x10000) return 1;
  965. return 2;
  966. }
  967. function codePointAtLast (bPos) {
  968. // Gives the UTF char for backtracking surrogates
  969. const ch = source.charCodeAt(bPos);
  970. if ((ch & 0xFC00) === 0xDC00)
  971. return (((source.charCodeAt(bPos - 1) & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;
  972. return ch;
  973. }
  974. function throwIfImportStatement () {
  975. const startPos = pos;
  976. pos += 6;
  977. const ch = commentWhitespace();
  978. switch (ch) {
  979. // dynamic import
  980. case 40/*(*/:
  981. openTokenPosStack[openTokenDepth++] = startPos;
  982. return;
  983. // import.meta
  984. case 46/*.*/:
  985. throw new Error('Unexpected import.meta in CJS module.');
  986. return;
  987. default:
  988. // no space after "import" -> not an import keyword
  989. if (pos === startPos + 6)
  990. break;
  991. case 34/*"*/:
  992. case 39/*'*/:
  993. case 123/*{*/:
  994. case 42/***/:
  995. // import statement only permitted at base-level
  996. if (openTokenDepth !== 0) {
  997. pos--;
  998. return;
  999. }
  1000. // import statements are a syntax error in CommonJS
  1001. throw new Error('Unexpected import statement in CJS module.');
  1002. }
  1003. }
  1004. function throwIfExportStatement () {
  1005. pos += 6;
  1006. const curPos = pos;
  1007. const ch = commentWhitespace();
  1008. if (pos === curPos && !isPunctuator(ch))
  1009. return;
  1010. throw new Error('Unexpected export statement in CJS module.');
  1011. }
  1012. function commentWhitespace () {
  1013. let ch;
  1014. do {
  1015. ch = source.charCodeAt(pos);
  1016. if (ch === 47/*/*/) {
  1017. const next_ch = source.charCodeAt(pos + 1);
  1018. if (next_ch === 47/*/*/)
  1019. lineComment();
  1020. else if (next_ch === 42/***/)
  1021. blockComment();
  1022. else
  1023. return ch;
  1024. }
  1025. else if (!isBrOrWs(ch)) {
  1026. return ch;
  1027. }
  1028. } while (pos++ < end);
  1029. return ch;
  1030. }
  1031. function templateString () {
  1032. while (pos++ < end) {
  1033. const ch = source.charCodeAt(pos);
  1034. if (ch === 36/*$*/ && source.charCodeAt(pos + 1) === 123/*{*/) {
  1035. pos++;
  1036. templateStack[templateStackDepth++] = templateDepth;
  1037. templateDepth = ++openTokenDepth;
  1038. return;
  1039. }
  1040. if (ch === 96/*`*/)
  1041. return;
  1042. if (ch === 92/*\*/)
  1043. pos++;
  1044. }
  1045. syntaxError();
  1046. }
  1047. function blockComment () {
  1048. pos++;
  1049. while (pos++ < end) {
  1050. const ch = source.charCodeAt(pos);
  1051. if (ch === 42/***/ && source.charCodeAt(pos + 1) === 47/*/*/) {
  1052. pos++;
  1053. return;
  1054. }
  1055. }
  1056. }
  1057. function lineComment () {
  1058. while (pos++ < end) {
  1059. const ch = source.charCodeAt(pos);
  1060. if (ch === 10/*\n*/ || ch === 13/*\r*/)
  1061. return;
  1062. }
  1063. }
  1064. function singleQuoteString () {
  1065. while (pos++ < end) {
  1066. let ch = source.charCodeAt(pos);
  1067. if (ch === 39/*'*/)
  1068. return;
  1069. if (ch === 92/*\*/) {
  1070. ch = source.charCodeAt(++pos);
  1071. if (ch === 13/*\r*/ && source.charCodeAt(pos + 1) === 10/*\n*/)
  1072. pos++;
  1073. }
  1074. else if (isBr(ch))
  1075. break;
  1076. }
  1077. throw new Error('Unterminated string.');
  1078. }
  1079. function doubleQuoteString () {
  1080. while (pos++ < end) {
  1081. let ch = source.charCodeAt(pos);
  1082. if (ch === 34/*"*/)
  1083. return;
  1084. if (ch === 92/*\*/) {
  1085. ch = source.charCodeAt(++pos);
  1086. if (ch === 13/*\r*/ && source.charCodeAt(pos + 1) === 10/*\n*/)
  1087. pos++;
  1088. }
  1089. else if (isBr(ch))
  1090. break;
  1091. }
  1092. throw new Error('Unterminated string.');
  1093. }
  1094. function regexCharacterClass () {
  1095. while (pos++ < end) {
  1096. let ch = source.charCodeAt(pos);
  1097. if (ch === 93/*]*/)
  1098. return ch;
  1099. if (ch === 92/*\*/)
  1100. pos++;
  1101. else if (ch === 10/*\n*/ || ch === 13/*\r*/)
  1102. break;
  1103. }
  1104. throw new Error('Syntax error reading regular expression class.');
  1105. }
  1106. function regularExpression () {
  1107. while (pos++ < end) {
  1108. let ch = source.charCodeAt(pos);
  1109. if (ch === 47/*/*/)
  1110. return;
  1111. if (ch === 91/*[*/)
  1112. ch = regexCharacterClass();
  1113. else if (ch === 92/*\*/)
  1114. pos++;
  1115. else if (ch === 10/*\n*/ || ch === 13/*\r*/)
  1116. break;
  1117. }
  1118. throw new Error('Syntax error reading regular expression.');
  1119. }
  1120. // Note: non-asii BR and whitespace checks omitted for perf / footprint
  1121. // if there is a significant user need this can be reconsidered
  1122. function isBr (c) {
  1123. return c === 13/*\r*/ || c === 10/*\n*/;
  1124. }
  1125. function isBrOrWs (c) {
  1126. return c > 8 && c < 14 || c === 32 || c === 160;
  1127. }
  1128. function isBrOrWsOrPunctuatorNotDot (c) {
  1129. return c > 8 && c < 14 || c === 32 || c === 160 || isPunctuator(c) && c !== 46/*.*/;
  1130. }
  1131. function keywordStart (pos) {
  1132. return pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1));
  1133. }
  1134. function readPrecedingKeyword (pos, match) {
  1135. if (pos < match.length - 1)
  1136. return false;
  1137. return source.startsWith(match, pos - match.length + 1) && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length)));
  1138. }
  1139. function readPrecedingKeyword1 (pos, ch) {
  1140. return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
  1141. }
  1142. // Detects one of case, debugger, delete, do, else, in, instanceof, new,
  1143. // return, throw, typeof, void, yield, await
  1144. function isExpressionKeyword (pos) {
  1145. switch (source.charCodeAt(pos)) {
  1146. case 100/*d*/:
  1147. switch (source.charCodeAt(pos - 1)) {
  1148. case 105/*i*/:
  1149. // void
  1150. return readPrecedingKeyword(pos - 2, 'vo');
  1151. case 108/*l*/:
  1152. // yield
  1153. return readPrecedingKeyword(pos - 2, 'yie');
  1154. default:
  1155. return false;
  1156. }
  1157. case 101/*e*/:
  1158. switch (source.charCodeAt(pos - 1)) {
  1159. case 115/*s*/:
  1160. switch (source.charCodeAt(pos - 2)) {
  1161. case 108/*l*/:
  1162. // else
  1163. return readPrecedingKeyword1(pos - 3, 101/*e*/);
  1164. case 97/*a*/:
  1165. // case
  1166. return readPrecedingKeyword1(pos - 3, 99/*c*/);
  1167. default:
  1168. return false;
  1169. }
  1170. case 116/*t*/:
  1171. // delete
  1172. return readPrecedingKeyword(pos - 2, 'dele');
  1173. default:
  1174. return false;
  1175. }
  1176. case 102/*f*/:
  1177. if (source.charCodeAt(pos - 1) !== 111/*o*/ || source.charCodeAt(pos - 2) !== 101/*e*/)
  1178. return false;
  1179. switch (source.charCodeAt(pos - 3)) {
  1180. case 99/*c*/:
  1181. // instanceof
  1182. return readPrecedingKeyword(pos - 4, 'instan');
  1183. case 112/*p*/:
  1184. // typeof
  1185. return readPrecedingKeyword(pos - 4, 'ty');
  1186. default:
  1187. return false;
  1188. }
  1189. case 110/*n*/:
  1190. // in, return
  1191. return readPrecedingKeyword1(pos - 1, 105/*i*/) || readPrecedingKeyword(pos - 1, 'retur');
  1192. case 111/*o*/:
  1193. // do
  1194. return readPrecedingKeyword1(pos - 1, 100/*d*/);
  1195. case 114/*r*/:
  1196. // debugger
  1197. return readPrecedingKeyword(pos - 1, 'debugge');
  1198. case 116/*t*/:
  1199. // await
  1200. return readPrecedingKeyword(pos - 1, 'awai');
  1201. case 119/*w*/:
  1202. switch (source.charCodeAt(pos - 1)) {
  1203. case 101/*e*/:
  1204. // new
  1205. return readPrecedingKeyword1(pos - 2, 110/*n*/);
  1206. case 111/*o*/:
  1207. // throw
  1208. return readPrecedingKeyword(pos - 2, 'thr');
  1209. default:
  1210. return false;
  1211. }
  1212. }
  1213. return false;
  1214. }
  1215. function isParenKeyword (curPos) {
  1216. return source.charCodeAt(curPos) === 101/*e*/ && source.startsWith('whil', curPos - 4) ||
  1217. source.charCodeAt(curPos) === 114/*r*/ && source.startsWith('fo', curPos - 2) ||
  1218. source.charCodeAt(curPos - 1) === 105/*i*/ && source.charCodeAt(curPos) === 102/*f*/;
  1219. }
  1220. function isPunctuator (ch) {
  1221. // 23 possible punctuator endings: !%&()*+,-./:;<=>?[]^{}|~
  1222. return ch === 33/*!*/ || ch === 37/*%*/ || ch === 38/*&*/ ||
  1223. ch > 39 && ch < 48 || ch > 57 && ch < 64 ||
  1224. ch === 91/*[*/ || ch === 93/*]*/ || ch === 94/*^*/ ||
  1225. ch > 122 && ch < 127;
  1226. }
  1227. function isExpressionPunctuator (ch) {
  1228. // 20 possible expression endings: !%&(*+,-.:;<=>?[^{|~
  1229. return ch === 33/*!*/ || ch === 37/*%*/ || ch === 38/*&*/ ||
  1230. ch > 39 && ch < 47 && ch !== 41 || ch > 57 && ch < 64 ||
  1231. ch === 91/*[*/ || ch === 94/*^*/ || ch > 122 && ch < 127 && ch !== 125/*}*/;
  1232. }
  1233. function isExpressionTerminator (curPos) {
  1234. // detects:
  1235. // => ; ) finally catch else
  1236. // as all of these followed by a { will indicate a statement brace
  1237. switch (source.charCodeAt(curPos)) {
  1238. case 62/*>*/:
  1239. return source.charCodeAt(curPos - 1) === 61/*=*/;
  1240. case 59/*;*/:
  1241. case 41/*)*/:
  1242. return true;
  1243. case 104/*h*/:
  1244. return source.startsWith('catc', curPos - 4);
  1245. case 121/*y*/:
  1246. return source.startsWith('finall', curPos - 6);
  1247. case 101/*e*/:
  1248. return source.startsWith('els', curPos - 3);
  1249. }
  1250. return false;
  1251. }
  1252. const initPromise = Promise.resolve();
  1253. module.exports.init = () => initPromise;
  1254. module.exports.parse = parseCJS;