lexer.js 26 KB

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