lexer.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. let source, pos, end,
  2. openTokenDepth,
  3. lastTokenPos,
  4. openTokenPosStack,
  5. openClassPosStack,
  6. curDynamicImport,
  7. templateStackDepth,
  8. facade,
  9. lastSlashWasDivision,
  10. nextBraceIsClass,
  11. templateDepth,
  12. templateStack,
  13. imports,
  14. exports,
  15. exportStatementStart,
  16. name;
  17. function addImport (ss, s, e, d) {
  18. const impt = { ss, se: d === -2 ? e : d === -1 ? e + 1 : 0, s, e, d, a: -1, n: undefined, at: null };
  19. imports.push(impt);
  20. return impt;
  21. }
  22. function addExport (s, e, ls, le) {
  23. exports.push({
  24. s,
  25. e,
  26. ls,
  27. le,
  28. ss: exportStatementStart,
  29. n: s[0] === '"' ? readString(s, '"') : s[0] === "'" ? readString(s, "'") : source.slice(s, e),
  30. ln: ls[0] === '"' ? readString(ls, '"') : ls[0] === "'" ? readString(ls, "'") : source.slice(ls, le)
  31. });
  32. }
  33. function readName (impt) {
  34. let { d, s } = impt;
  35. if (d !== -1)
  36. s++;
  37. impt.n = readString(s, source.charCodeAt(s - 1));
  38. }
  39. // Note: parsing is based on the _assumption_ that the source is already valid
  40. export function parse (_source, _name) {
  41. openTokenDepth = 0;
  42. curDynamicImport = null;
  43. templateDepth = -1;
  44. lastTokenPos = -1;
  45. lastSlashWasDivision = false;
  46. templateStack = Array(1024);
  47. templateStackDepth = 0;
  48. openTokenPosStack = Array(1024);
  49. openClassPosStack = Array(1024);
  50. nextBraceIsClass = false;
  51. facade = true;
  52. name = _name || '@';
  53. imports = [];
  54. exports = [];
  55. source = _source;
  56. pos = -1;
  57. end = source.length - 1;
  58. let ch = 0;
  59. // start with a pure "module-only" parser
  60. m: while (pos++ < end) {
  61. ch = source.charCodeAt(pos);
  62. if (ch === 32 || ch < 14 && ch > 8)
  63. continue;
  64. switch (ch) {
  65. case 101/*e*/:
  66. if (openTokenDepth === 0 && keywordStart(pos) && source.startsWith('xport', pos + 1)) {
  67. tryParseExportStatement();
  68. // export might have been a non-pure declaration
  69. if (!facade) {
  70. lastTokenPos = pos;
  71. break m;
  72. }
  73. }
  74. break;
  75. case 105/*i*/:
  76. if (keywordStart(pos) && source.startsWith('mport', pos + 1))
  77. tryParseImportStatement();
  78. break;
  79. case 59/*;*/:
  80. break;
  81. case 47/*/*/: {
  82. const next_ch = source.charCodeAt(pos + 1);
  83. if (next_ch === 47/*/*/) {
  84. lineComment();
  85. // dont update lastToken
  86. continue;
  87. }
  88. else if (next_ch === 42/***/) {
  89. blockComment(true);
  90. // dont update lastToken
  91. continue;
  92. }
  93. // fallthrough
  94. }
  95. default:
  96. // as soon as we hit a non-module token, we go to main parser
  97. facade = false;
  98. pos--;
  99. break m;
  100. }
  101. lastTokenPos = pos;
  102. }
  103. while (pos++ < end) {
  104. ch = source.charCodeAt(pos);
  105. if (ch === 32 || ch < 14 && ch > 8)
  106. continue;
  107. switch (ch) {
  108. case 101/*e*/:
  109. if (openTokenDepth === 0 && keywordStart(pos) && source.startsWith('xport', pos + 1))
  110. tryParseExportStatement();
  111. break;
  112. case 105/*i*/:
  113. if (keywordStart(pos) && source.startsWith('mport', pos + 1))
  114. tryParseImportStatement();
  115. break;
  116. case 99/*c*/:
  117. if (keywordStart(pos) && source.startsWith('lass', pos + 1) && isBrOrWs(source.charCodeAt(pos + 5)))
  118. nextBraceIsClass = true;
  119. break;
  120. case 40/*(*/:
  121. openTokenPosStack[openTokenDepth++] = lastTokenPos;
  122. break;
  123. case 41/*)*/:
  124. if (openTokenDepth === 0)
  125. syntaxError();
  126. openTokenDepth--;
  127. if (curDynamicImport && curDynamicImport.d === openTokenPosStack[openTokenDepth]) {
  128. if (curDynamicImport.e === 0)
  129. curDynamicImport.e = pos;
  130. curDynamicImport.se = pos;
  131. curDynamicImport = null;
  132. }
  133. break;
  134. case 91/*[*/:
  135. openTokenPosStack[openTokenDepth++] = lastTokenPos;
  136. break;
  137. case 93/*]*/:
  138. if (openTokenDepth === 0)
  139. syntaxError();
  140. openTokenDepth--;
  141. break;
  142. case 44/*,*/:
  143. if (curDynamicImport && curDynamicImport.e === 0 && curDynamicImport.d === openTokenPosStack[openTokenDepth - 1]) {
  144. curDynamicImport.e = lastTokenPos + 1;
  145. pos++;
  146. commentWhitespace(true);
  147. curDynamicImport.a = pos;
  148. pos--;
  149. }
  150. break;
  151. case 123/*{*/:
  152. // dynamic import followed by { is not a dynamic import (so remove)
  153. // this is a sneaky way to get around { import () {} } v { import () }
  154. // block / object ambiguity without a parser (assuming source is valid)
  155. // se marks the closing paren; e is moved before the first comma for import(a, b)
  156. if (source.charCodeAt(lastTokenPos) === 41/*)*/ && imports.length && imports[imports.length - 1].se === lastTokenPos) {
  157. imports.pop();
  158. }
  159. openClassPosStack[openTokenDepth] = nextBraceIsClass;
  160. nextBraceIsClass = false;
  161. openTokenPosStack[openTokenDepth++] = lastTokenPos;
  162. break;
  163. case 125/*}*/:
  164. if (openTokenDepth === 0)
  165. syntaxError();
  166. if (openTokenDepth-- === templateDepth) {
  167. templateDepth = templateStack[--templateStackDepth];
  168. templateString();
  169. }
  170. else {
  171. if (templateDepth !== -1 && openTokenDepth < templateDepth)
  172. syntaxError();
  173. }
  174. break;
  175. case 39/*'*/:
  176. case 34/*"*/:
  177. stringLiteral(ch);
  178. break;
  179. case 47/*/*/: {
  180. const next_ch = source.charCodeAt(pos + 1);
  181. if (next_ch === 47/*/*/) {
  182. lineComment();
  183. // dont update lastToken
  184. continue;
  185. }
  186. else if (next_ch === 42/***/) {
  187. blockComment(true);
  188. // dont update lastToken
  189. continue;
  190. }
  191. else {
  192. // Division / regex ambiguity handling based on checking backtrack analysis of:
  193. // - what token came previously (lastToken)
  194. // - if a closing brace or paren, what token came before the corresponding
  195. // opening brace or paren (lastOpenTokenIndex)
  196. const lastToken = source.charCodeAt(lastTokenPos);
  197. const lastExport = exports[exports.length - 1];
  198. if (isExpressionPunctuator(lastToken) &&
  199. !(lastToken === 46/*.*/ && (source.charCodeAt(lastTokenPos - 1) >= 48/*0*/ && source.charCodeAt(lastTokenPos - 1) <= 57/*9*/)) &&
  200. !(lastToken === 43/*+*/ && source.charCodeAt(lastTokenPos - 1) === 43/*+*/) && !(lastToken === 45/*-*/ && source.charCodeAt(lastTokenPos - 1) === 45/*-*/) ||
  201. lastToken === 41/*)*/ && isParenKeyword(openTokenPosStack[openTokenDepth]) ||
  202. openTokenDepth > 0 && lastToken === 102/*f*/ && source.charCodeAt(lastTokenPos - 1) === 111/*o*/ && isForOfBinding(lastTokenPos - 2) && isForParen(openTokenPosStack[openTokenDepth - 1]) ||
  203. lastToken === 125/*}*/ && (isExpressionTerminator(openTokenPosStack[openTokenDepth]) || openClassPosStack[openTokenDepth]) ||
  204. lastToken === 47/*/*/ && lastSlashWasDivision ||
  205. isExpressionKeyword(lastTokenPos) ||
  206. !lastToken) {
  207. regularExpression();
  208. lastSlashWasDivision = false;
  209. }
  210. else if (lastExport && lastTokenPos >= lastExport.s && lastTokenPos <= lastExport.e) {
  211. // export default /some-regexp/
  212. regularExpression();
  213. lastSlashWasDivision = false;
  214. }
  215. else {
  216. lastSlashWasDivision = true;
  217. }
  218. }
  219. break;
  220. }
  221. case 96/*`*/:
  222. templateString();
  223. break;
  224. }
  225. lastTokenPos = pos;
  226. }
  227. if (templateDepth !== -1 || openTokenDepth)
  228. syntaxError();
  229. return [imports, exports, facade];
  230. }
  231. function tryParseImportStatement () {
  232. const startPos = pos;
  233. pos += 6;
  234. let ch = commentWhitespace(true);
  235. switch (ch) {
  236. // dynamic import
  237. case 40/*(*/:
  238. openTokenPosStack[openTokenDepth++] = startPos;
  239. if (source.charCodeAt(lastTokenPos) === 46/*.*/)
  240. return;
  241. // dynamic import indicated by positive d
  242. // try parse a string, to record a safe dynamic import string
  243. pos++;
  244. ch = commentWhitespace(true);
  245. // The specifier start is recorded after leading whitespace/comments so it
  246. // points at the literal, matching the C lexer (src/lexer.c).
  247. const impt = addImport(startPos, pos, 0, startPos);
  248. curDynamicImport = impt;
  249. if (ch === 39/*'*/ || ch === 34/*"*/) {
  250. stringLiteral(ch);
  251. }
  252. else if (ch === 96/*`*/ && noSubstitutionTemplate()) {
  253. // A no-substitution template literal is a constant string, so it is a
  254. // safe specifier exactly like a quoted one. An interpolated template
  255. // leaves noSubstitutionTemplate() false and falls through to the open-
  256. // token machinery, which records the import as unsafe (n stays unset).
  257. }
  258. else {
  259. pos--;
  260. return;
  261. }
  262. pos++;
  263. ch = commentWhitespace(true);
  264. if (ch === 44/*,*/) {
  265. impt.e = pos;
  266. pos++;
  267. ch = commentWhitespace(true);
  268. impt.a = pos;
  269. readName(impt);
  270. pos--;
  271. }
  272. else if (ch === 41/*)*/) {
  273. openTokenDepth--;
  274. impt.e = pos;
  275. impt.se = pos;
  276. readName(impt);
  277. }
  278. else {
  279. pos--;
  280. }
  281. return;
  282. // import.meta
  283. case 46/*.*/:
  284. pos++;
  285. ch = commentWhitespace(true);
  286. // import.meta indicated by d === -2
  287. if (ch === 109/*m*/ && source.startsWith('eta', pos + 1) && source.charCodeAt(lastTokenPos) !== 46/*.*/)
  288. addImport(startPos, startPos, pos + 4, -2);
  289. return;
  290. default:
  291. // no space after "import" -> not an import keyword
  292. if (pos === startPos + 6)
  293. break;
  294. case 34/*"*/:
  295. case 39/*'*/:
  296. case 123/*{*/:
  297. case 42/***/:
  298. // import statement only permitted at base-level
  299. if (openTokenDepth !== 0) {
  300. pos--;
  301. return;
  302. }
  303. while (pos < end) {
  304. ch = source.charCodeAt(pos);
  305. if (ch === 39/*'*/ || ch === 34/*"*/) {
  306. readImportString(startPos, ch);
  307. return;
  308. }
  309. pos++;
  310. }
  311. syntaxError();
  312. }
  313. }
  314. function tryParseExportStatement () {
  315. const sStartPos = pos;
  316. const prevExport = exports.length;
  317. pos += 6;
  318. const curPos = pos;
  319. let ch = commentWhitespace(true);
  320. // Only commit the statement start once this is a real export: skipExpression
  321. // re-enters here for an `export`-prefixed identifier (e.g. `exports`) in an
  322. // initializer, which would otherwise clobber the start for later bindings.
  323. if (pos === curPos && !isPunctuator(ch))
  324. return;
  325. exportStatementStart = sStartPos;
  326. switch (ch) {
  327. // export default ...
  328. case 100/*d*/:
  329. addExport(pos, pos + 7, -1, -1);
  330. return;
  331. // export async? function*? name () {
  332. case 97/*a*/:
  333. pos += 5;
  334. commentWhitespace(true);
  335. // fallthrough
  336. case 102/*f*/:
  337. pos += 8;
  338. ch = commentWhitespace(true);
  339. if (ch === 42/***/) {
  340. pos++;
  341. ch = commentWhitespace(true);
  342. }
  343. const startPos = pos;
  344. ch = readToWsOrPunctuator(ch);
  345. addExport(startPos, pos, startPos, pos);
  346. pos--;
  347. return;
  348. // export class name ...
  349. case 99/*c*/:
  350. if (source.startsWith('lass', pos + 1) && isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos + 5))) {
  351. pos += 5;
  352. ch = commentWhitespace(true);
  353. const startPos = pos;
  354. ch = readToWsOrPunctuator(ch);
  355. addExport(startPos, pos, startPos, pos);
  356. pos--;
  357. return;
  358. }
  359. pos += 2;
  360. // fallthrough
  361. // export var/let/const name = ...(, name = ...)+
  362. case 118/*v*/:
  363. case 109/*l*/:
  364. // destructured initializations not currently supported (skipped for { or [)
  365. // also, lexing names after variable equals is skipped (export var p = function () { ... }, q = 5 skips "q")
  366. pos += 2;
  367. facade = false;
  368. do {
  369. pos++;
  370. ch = commentWhitespace(true);
  371. const startPos = pos;
  372. ch = readToWsOrPunctuator(ch);
  373. // dont yet handle [ { destructurings
  374. if (ch === 123/*{*/ || ch === 91/*[*/) {
  375. pos--;
  376. return;
  377. }
  378. if (pos === startPos)
  379. return;
  380. addExport(startPos, pos, startPos, pos);
  381. ch = commentWhitespace(true);
  382. if (ch === 61/*=*/) {
  383. pos--;
  384. return;
  385. }
  386. } while (ch === 44/*,*/);
  387. pos--;
  388. return;
  389. // export {...}
  390. case 123/*{*/:
  391. pos++;
  392. ch = commentWhitespace(true);
  393. while (true) {
  394. const startPos = pos;
  395. readToWsOrPunctuator(ch);
  396. const endPos = pos;
  397. commentWhitespace(true);
  398. ch = readExportAs(startPos, endPos);
  399. // ,
  400. if (ch === 44/*,*/) {
  401. pos++;
  402. ch = commentWhitespace(true);
  403. }
  404. if (ch === 125/*}*/)
  405. break;
  406. if (pos === startPos)
  407. return syntaxError();
  408. if (pos > end)
  409. return syntaxError();
  410. }
  411. pos++;
  412. ch = commentWhitespace(true);
  413. break;
  414. // export *
  415. // export * as X
  416. case 42/***/:
  417. pos++;
  418. commentWhitespace(true);
  419. ch = readExportAs(pos, pos);
  420. ch = commentWhitespace(true);
  421. break;
  422. }
  423. // from ...
  424. if (ch === 102/*f*/ && source.startsWith('rom', pos + 1)) {
  425. pos += 4;
  426. readImportString(sStartPos, commentWhitespace(true));
  427. // There were no local names.
  428. for (let i = prevExport; i < exports.length; ++i) {
  429. exports[i].ls = exports[i].le = -1;
  430. exports[i].ln = undefined;
  431. }
  432. }
  433. else {
  434. pos--;
  435. }
  436. }
  437. /*
  438. * Ported from Acorn
  439. *
  440. * MIT License
  441. * Copyright (C) 2012-2020 by various contributors (see AUTHORS)
  442. * Permission is hereby granted, free of charge, to any person obtaining a copy
  443. * of this software and associated documentation files (the "Software"), to deal
  444. * in the Software without restriction, including without limitation the rights
  445. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  446. * copies of the Software, and to permit persons to whom the Software is
  447. * furnished to do so, subject to the following conditions:
  448. * The above copyright notice and this permission notice shall be included in
  449. * all copies or substantial portions of the Software.
  450. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  451. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  452. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  453. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  454. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  455. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  456. * THE SOFTWARE.
  457. */
  458. let acornPos;
  459. function readString (start, quote) {
  460. acornPos = start;
  461. let out = '', chunkStart = acornPos;
  462. for (;;) {
  463. if (acornPos >= source.length) syntaxError();
  464. const ch = source.charCodeAt(acornPos);
  465. if (ch === quote) break;
  466. if (ch === 92) { // '\'
  467. out += source.slice(chunkStart, acornPos);
  468. out += readEscapedChar();
  469. chunkStart = acornPos;
  470. }
  471. else if (ch === 0x2028 || ch === 0x2029) {
  472. ++acornPos;
  473. }
  474. else {
  475. // Template literals (backtick quote) permit raw line breaks; string
  476. // literals do not.
  477. if (isBr(ch) && quote !== 96/*`*/) syntaxError();
  478. ++acornPos;
  479. }
  480. }
  481. out += source.slice(chunkStart, acornPos++);
  482. return out;
  483. }
  484. // Used to read escaped characters
  485. function readEscapedChar () {
  486. let ch = source.charCodeAt(++acornPos);
  487. ++acornPos;
  488. switch (ch) {
  489. case 110: return '\n'; // 'n' -> '\n'
  490. case 114: return '\r'; // 'r' -> '\r'
  491. case 120: return String.fromCharCode(readHexChar(2)); // 'x'
  492. case 117: return readCodePointToString(); // 'u'
  493. case 116: return '\t'; // 't' -> '\t'
  494. case 98: return '\b'; // 'b' -> '\b'
  495. case 118: return '\u000b'; // 'v' -> '\u000b'
  496. case 102: return '\f'; // 'f' -> '\f'
  497. case 13: if (source.charCodeAt(acornPos) === 10) ++acornPos; // '\r\n'
  498. case 10: // ' \n'
  499. return '';
  500. case 56:
  501. case 57:
  502. syntaxError();
  503. default:
  504. if (ch >= 48 && ch <= 55) {
  505. let octalStr = source.substr(acornPos - 1, 3).match(/^[0-7]+/)[0];
  506. let octal = parseInt(octalStr, 8);
  507. if (octal > 255) {
  508. octalStr = octalStr.slice(0, -1);
  509. octal = parseInt(octalStr, 8);
  510. }
  511. acornPos += octalStr.length - 1;
  512. ch = source.charCodeAt(acornPos);
  513. if (octalStr !== '0' || ch === 56 || ch === 57)
  514. syntaxError();
  515. return String.fromCharCode(octal);
  516. }
  517. if (isBr(ch)) {
  518. // Unicode new line characters after \ get removed from output in both
  519. // template literals and strings
  520. return '';
  521. }
  522. return String.fromCharCode(ch);
  523. }
  524. }
  525. // Used to read character escape sequences ('\x', '\u', '\U').
  526. function readHexChar (len) {
  527. const start = acornPos;
  528. let total = 0, lastCode = 0;
  529. for (let i = 0; i < len; ++i, ++acornPos) {
  530. let code = source.charCodeAt(acornPos), val;
  531. if (code === 95) {
  532. if (lastCode === 95 || i === 0) syntaxError();
  533. lastCode = code;
  534. continue;
  535. }
  536. if (code >= 97) val = code - 97 + 10; // a
  537. else if (code >= 65) val = code - 65 + 10; // A
  538. else if (code >= 48 && code <= 57) val = code - 48; // 0-9
  539. else break;
  540. if (val >= 16) break;
  541. lastCode = code;
  542. total = total * 16 + val;
  543. }
  544. if (lastCode === 95 || acornPos - start !== len) syntaxError();
  545. return total;
  546. }
  547. // Read a string value, interpreting backslash-escapes.
  548. function readCodePointToString () {
  549. const ch = source.charCodeAt(acornPos);
  550. let code;
  551. if (ch === 123) { // '{'
  552. ++acornPos;
  553. code = readHexChar(source.indexOf('}', acornPos) - acornPos);
  554. ++acornPos;
  555. if (code > 0x10FFFF) syntaxError();
  556. } else {
  557. code = readHexChar(4);
  558. }
  559. // UTF-16 Decoding
  560. if (code <= 0xFFFF) return String.fromCharCode(code);
  561. code -= 0x10000;
  562. return String.fromCharCode((code >> 10) + 0xD800, (code & 1023) + 0xDC00);
  563. }
  564. /*
  565. * </ Acorn Port>
  566. */
  567. function readExportAs (startPos, endPos) {
  568. let ch = source.charCodeAt(pos);
  569. let ls = startPos, le = endPos;
  570. if (ch === 97 /*a*/) {
  571. pos += 2;
  572. ch = commentWhitespace(true);
  573. startPos = pos;
  574. readToWsOrPunctuator(ch);
  575. endPos = pos;
  576. ch = commentWhitespace(true);
  577. }
  578. if (pos !== startPos)
  579. addExport(startPos, endPos, ls, le);
  580. return ch;
  581. }
  582. function readImportString (ss, ch) {
  583. const startPos = pos + 1;
  584. if (ch === 39/*'*/ || ch === 34/*"*/) {
  585. stringLiteral(ch);
  586. }
  587. else {
  588. syntaxError();
  589. return;
  590. }
  591. const impt = addImport(ss, startPos, pos, -1);
  592. readName(impt);
  593. pos++;
  594. ch = commentWhitespace(false);
  595. if (ch !== 119/*w*/ || !source.startsWith('ith', pos + 1)) {
  596. pos--;
  597. return;
  598. }
  599. const attrIndex = pos;
  600. pos += 4;
  601. ch = commentWhitespace(true);
  602. if (ch !== 123/*{*/) {
  603. pos = attrIndex;
  604. return;
  605. }
  606. const attrStart = pos;
  607. const attrs = [];
  608. do {
  609. pos++;
  610. ch = commentWhitespace(true);
  611. let key, keyStart, keyEnd;
  612. if (ch === 39/*'*/ || ch === 34/*"*/) {
  613. keyStart = pos;
  614. stringLiteral(ch);
  615. keyEnd = pos + 1;
  616. key = readString(keyStart, ch);
  617. pos++;
  618. ch = commentWhitespace(true);
  619. }
  620. else {
  621. keyStart = pos;
  622. ch = readToWsOrPunctuator(ch);
  623. keyEnd = pos;
  624. key = source.slice(keyStart, keyEnd);
  625. }
  626. if (ch !== 58/*:*/) {
  627. pos = attrIndex;
  628. return;
  629. }
  630. pos++;
  631. ch = commentWhitespace(true);
  632. let value, valueStart;
  633. if (ch === 39/*'*/ || ch === 34/*"*/) {
  634. valueStart = pos;
  635. stringLiteral(ch);
  636. value = readString(valueStart, ch);
  637. }
  638. else {
  639. pos = attrIndex;
  640. return;
  641. }
  642. attrs.push([key, value]);
  643. pos++;
  644. ch = commentWhitespace(true);
  645. if (ch === 44/*,*/) {
  646. pos++;
  647. continue;
  648. }
  649. if (ch === 125/*}*/)
  650. break;
  651. pos = attrIndex;
  652. return;
  653. } while (true);
  654. impt.a = attrStart;
  655. impt.at = attrs;
  656. impt.se = pos + 1;
  657. }
  658. function commentWhitespace (br) {
  659. let ch;
  660. do {
  661. ch = source.charCodeAt(pos);
  662. if (ch === 47/*/*/) {
  663. const next_ch = source.charCodeAt(pos + 1);
  664. if (next_ch === 47/*/*/)
  665. lineComment();
  666. else if (next_ch === 42/***/)
  667. blockComment(br);
  668. else
  669. return ch;
  670. }
  671. else if (br ? !isBrOrWs(ch): !isWsNotBr(ch)) {
  672. return ch;
  673. }
  674. } while (pos++ < end);
  675. return ch;
  676. }
  677. function templateString () {
  678. while (pos++ < end) {
  679. const ch = source.charCodeAt(pos);
  680. if (ch === 36/*$*/ && source.charCodeAt(pos + 1) === 123/*{*/) {
  681. pos++;
  682. templateStack[templateStackDepth++] = templateDepth;
  683. templateDepth = ++openTokenDepth;
  684. return;
  685. }
  686. if (ch === 96/*`*/)
  687. return;
  688. if (ch === 92/*\*/)
  689. pos++;
  690. }
  691. syntaxError();
  692. }
  693. // pos AT the opening backtick. A no-substitution template literal (no ${...})
  694. // is a constant string, so a dynamic import can record it as a safe specifier.
  695. // On success consumes it, leaves pos AT the closing backtick and returns true.
  696. // On a substitution or EOF restores pos and returns false, leaving the literal
  697. // to the main loop's template handling.
  698. function noSubstitutionTemplate () {
  699. const startPos = pos;
  700. while (pos++ < end) {
  701. const ch = source.charCodeAt(pos);
  702. if (ch === 96/*`*/)
  703. return true;
  704. if (ch === 92/*\*/) {
  705. pos++;
  706. continue;
  707. }
  708. if (ch === 36/*$*/ && source.charCodeAt(pos + 1) === 123/*{*/)
  709. break;
  710. }
  711. pos = startPos;
  712. return false;
  713. }
  714. function blockComment (br) {
  715. pos++;
  716. while (pos++ < end) {
  717. const ch = source.charCodeAt(pos);
  718. if (!br && isBr(ch))
  719. return;
  720. if (ch === 42/***/ && source.charCodeAt(pos + 1) === 47/*/*/) {
  721. pos++;
  722. return;
  723. }
  724. }
  725. }
  726. function lineComment () {
  727. while (pos++ < end) {
  728. const ch = source.charCodeAt(pos);
  729. if (ch === 10/*\n*/ || ch === 13/*\r*/)
  730. return;
  731. }
  732. }
  733. function stringLiteral (quote) {
  734. while (pos++ < end) {
  735. let ch = source.charCodeAt(pos);
  736. if (ch === quote)
  737. return;
  738. if (ch === 92/*\*/) {
  739. ch = source.charCodeAt(++pos);
  740. if (ch === 13/*\r*/ && source.charCodeAt(pos + 1) === 10/*\n*/)
  741. pos++;
  742. }
  743. else if (isBr(ch))
  744. break;
  745. }
  746. syntaxError();
  747. }
  748. function regexCharacterClass () {
  749. while (pos++ < end) {
  750. let ch = source.charCodeAt(pos);
  751. if (ch === 93/*]*/)
  752. return ch;
  753. if (ch === 92/*\*/)
  754. pos++;
  755. else if (ch === 10/*\n*/ || ch === 13/*\r*/)
  756. break;
  757. }
  758. syntaxError();
  759. }
  760. function regularExpression () {
  761. while (pos++ < end) {
  762. let ch = source.charCodeAt(pos);
  763. if (ch === 47/*/*/)
  764. return;
  765. if (ch === 91/*[*/)
  766. ch = regexCharacterClass();
  767. else if (ch === 92/*\*/)
  768. pos++;
  769. else if (ch === 10/*\n*/ || ch === 13/*\r*/)
  770. break;
  771. }
  772. syntaxError();
  773. }
  774. function readToWsOrPunctuator (ch) {
  775. do {
  776. if (isBrOrWs(ch) || isPunctuator(ch))
  777. return ch;
  778. } while (ch = source.charCodeAt(++pos));
  779. return ch;
  780. }
  781. // Note: non-asii BR and whitespace checks omitted for perf / footprint
  782. // if there is a significant user need this can be reconsidered
  783. function isBr (c) {
  784. return c === 13/*\r*/ || c === 10/*\n*/;
  785. }
  786. function isWsNotBr (c) {
  787. return c === 9 || c === 11 || c === 12 || c === 32 || c === 160;
  788. }
  789. function isBrOrWs (c) {
  790. return c > 8 && c < 14 || c === 32 || c === 160;
  791. }
  792. function isBrOrWsOrPunctuatorNotDot (c) {
  793. return c > 8 && c < 14 || c === 32 || c === 160 || isPunctuator(c) && c !== 46/*.*/;
  794. }
  795. function keywordStart (pos) {
  796. return pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1));
  797. }
  798. function readPrecedingKeyword (pos, match) {
  799. if (pos < match.length - 1)
  800. return false;
  801. return source.startsWith(match, pos - match.length + 1) && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length)));
  802. }
  803. function readPrecedingKeyword1 (pos, ch) {
  804. return source.charCodeAt(pos) === ch && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - 1)));
  805. }
  806. // Detects one of case, debugger, delete, do, else, in, instanceof, new,
  807. // return, throw, typeof, void, yield, await
  808. function isExpressionKeyword (pos) {
  809. switch (source.charCodeAt(pos)) {
  810. case 100/*d*/:
  811. switch (source.charCodeAt(pos - 1)) {
  812. case 105/*i*/:
  813. // void
  814. return readPrecedingKeyword(pos - 2, 'vo');
  815. case 108/*l*/:
  816. // yield
  817. return readPrecedingKeyword(pos - 2, 'yie');
  818. default:
  819. return false;
  820. }
  821. case 101/*e*/:
  822. switch (source.charCodeAt(pos - 1)) {
  823. case 115/*s*/:
  824. switch (source.charCodeAt(pos - 2)) {
  825. case 108/*l*/:
  826. // else
  827. return readPrecedingKeyword1(pos - 3, 101/*e*/);
  828. case 97/*a*/:
  829. // case
  830. return readPrecedingKeyword1(pos - 3, 99/*c*/);
  831. default:
  832. return false;
  833. }
  834. case 116/*t*/:
  835. // delete
  836. return readPrecedingKeyword(pos - 2, 'dele');
  837. default:
  838. return false;
  839. }
  840. case 102/*f*/:
  841. if (source.charCodeAt(pos - 1) !== 111/*o*/ || source.charCodeAt(pos - 2) !== 101/*e*/)
  842. return false;
  843. switch (source.charCodeAt(pos - 3)) {
  844. case 99/*c*/:
  845. // instanceof
  846. return readPrecedingKeyword(pos - 4, 'instan');
  847. case 112/*p*/:
  848. // typeof
  849. return readPrecedingKeyword(pos - 4, 'ty');
  850. default:
  851. return false;
  852. }
  853. case 110/*n*/:
  854. // in, return
  855. return readPrecedingKeyword1(pos - 1, 105/*i*/) || readPrecedingKeyword(pos - 1, 'retur');
  856. case 111/*o*/:
  857. // do
  858. return readPrecedingKeyword1(pos - 1, 100/*d*/);
  859. case 114/*r*/:
  860. // debugger
  861. return readPrecedingKeyword(pos - 1, 'debugge');
  862. case 116/*t*/:
  863. // await
  864. return readPrecedingKeyword(pos - 1, 'awai');
  865. case 119/*w*/:
  866. switch (source.charCodeAt(pos - 1)) {
  867. case 101/*e*/:
  868. // new
  869. return readPrecedingKeyword1(pos - 2, 110/*n*/);
  870. case 111/*o*/:
  871. // throw
  872. return readPrecedingKeyword(pos - 2, 'thr');
  873. default:
  874. return false;
  875. }
  876. }
  877. return false;
  878. }
  879. function isParenKeyword (curPos) {
  880. return source.charCodeAt(curPos) === 101/*e*/ && source.startsWith('whil', curPos - 4) ||
  881. source.charCodeAt(curPos) === 114/*r*/ && source.startsWith('fo', curPos - 2) ||
  882. source.charCodeAt(curPos - 1) === 105/*i*/ && source.charCodeAt(curPos) === 102/*f*/;
  883. }
  884. function isForParen (curPos) {
  885. return source.charCodeAt(curPos) === 114/*r*/ && source.startsWith('fo', curPos - 2);
  886. }
  887. // In valid JS, the for-of `of` keyword always follows a binding,
  888. // which ends with an identifier-tail char, ']', '}', or ')'.
  889. function isForOfBinding (pos) {
  890. const ch = source.charCodeAt(pos);
  891. if (!isBrOrWs(ch) && ch !== 93/*]*/ && ch !== 125/*}*/ && ch !== 41/*)*/)
  892. return false;
  893. while (pos > 0 && isBrOrWs(source.charCodeAt(pos)))
  894. pos--;
  895. const c = source.charCodeAt(pos);
  896. return c === 93/*]*/ || c === 125/*}*/ || c === 41/*)*/ || !isPunctuator(c);
  897. }
  898. function isPunctuator (ch) {
  899. // 23 possible punctuator endings: !%&()*+,-./:;<=>?[]^{}|~
  900. return ch === 33/*!*/ || ch === 37/*%*/ || ch === 38/*&*/ ||
  901. ch > 39 && ch < 48 || ch > 57 && ch < 64 ||
  902. ch === 91/*[*/ || ch === 93/*]*/ || ch === 94/*^*/ ||
  903. ch > 122 && ch < 127;
  904. }
  905. function isExpressionPunctuator (ch) {
  906. // 20 possible expression endings: !%&(*+,-.:;<=>?[^{|~
  907. return ch === 33/*!*/ || ch === 37/*%*/ || ch === 38/*&*/ ||
  908. ch > 39 && ch < 47 && ch !== 41 || ch > 57 && ch < 64 ||
  909. ch === 91/*[*/ || ch === 94/*^*/ || ch > 122 && ch < 127 && ch !== 125/*}*/;
  910. }
  911. function isExpressionTerminator (curPos) {
  912. // detects:
  913. // => ; ) finally catch else
  914. // as all of these followed by a { will indicate a statement brace
  915. switch (source.charCodeAt(curPos)) {
  916. case 62/*>*/:
  917. return source.charCodeAt(curPos - 1) === 61/*=*/;
  918. case 59/*;*/:
  919. case 41/*)*/:
  920. return true;
  921. case 104/*h*/:
  922. return source.startsWith('catc', curPos - 4);
  923. case 121/*y*/:
  924. return source.startsWith('finall', curPos - 6);
  925. case 101/*e*/:
  926. return source.startsWith('els', curPos - 3);
  927. }
  928. return false;
  929. }
  930. function syntaxError () {
  931. throw Object.assign(new Error(`Parse error ${name}:${source.slice(0, pos).split('\n').length}:${pos - source.lastIndexOf('\n', pos - 1)}`), { idx: pos });
  932. }