lexer.js 24 KB

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