index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.Token = void 0;
  6. var N = _interopRequireWildcard(require("../types"));
  7. var _identifier = require("../util/identifier");
  8. var _types2 = require("./types");
  9. var _context = require("./context");
  10. var _error = _interopRequireWildcard(require("../parser/error"));
  11. var _location = require("../util/location");
  12. var _whitespace = require("../util/whitespace");
  13. var _state = _interopRequireDefault(require("./state"));
  14. var _isDigit = function isDigit(code) {
  15. return code >= 48 && code <= 57;
  16. };
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  19. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  20. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  21. const forbiddenNumericSeparatorSiblings = {
  22. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  23. hex: [46, 88, 95, 120]
  24. };
  25. const allowedNumericSeparatorSiblings = {};
  26. allowedNumericSeparatorSiblings.bin = [48, 49];
  27. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  28. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  29. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  30. class Token {
  31. constructor(state) {
  32. this.type = state.type;
  33. this.value = state.value;
  34. this.start = state.start;
  35. this.end = state.end;
  36. this.loc = new _location.SourceLocation(state.startLoc, state.endLoc);
  37. }
  38. }
  39. exports.Token = Token;
  40. class Tokenizer extends _error.default {
  41. constructor(options, input) {
  42. super();
  43. this.isLookahead = void 0;
  44. this.tokens = [];
  45. this.state = new _state.default();
  46. this.state.init(options);
  47. this.input = input;
  48. this.length = input.length;
  49. this.isLookahead = false;
  50. }
  51. pushToken(token) {
  52. this.tokens.length = this.state.tokensLength;
  53. this.tokens.push(token);
  54. ++this.state.tokensLength;
  55. }
  56. next() {
  57. if (!this.isLookahead) {
  58. this.checkKeywordEscapes();
  59. if (this.options.tokens) {
  60. this.pushToken(new Token(this.state));
  61. }
  62. }
  63. this.state.lastTokEnd = this.state.end;
  64. this.state.lastTokStart = this.state.start;
  65. this.state.lastTokEndLoc = this.state.endLoc;
  66. this.state.lastTokStartLoc = this.state.startLoc;
  67. this.nextToken();
  68. }
  69. eat(type) {
  70. if (this.match(type)) {
  71. this.next();
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. match(type) {
  78. return this.state.type === type;
  79. }
  80. lookahead() {
  81. const old = this.state;
  82. this.state = old.clone(true);
  83. this.isLookahead = true;
  84. this.next();
  85. this.isLookahead = false;
  86. const curr = this.state;
  87. this.state = old;
  88. return curr;
  89. }
  90. nextTokenStart() {
  91. return this.nextTokenStartSince(this.state.pos);
  92. }
  93. nextTokenStartSince(pos) {
  94. _whitespace.skipWhiteSpace.lastIndex = pos;
  95. const skip = _whitespace.skipWhiteSpace.exec(this.input);
  96. return pos + skip[0].length;
  97. }
  98. lookaheadCharCode() {
  99. return this.input.charCodeAt(this.nextTokenStart());
  100. }
  101. setStrict(strict) {
  102. this.state.strict = strict;
  103. if (!this.match(_types2.types.num) && !this.match(_types2.types.string)) return;
  104. this.state.pos = this.state.start;
  105. while (this.state.pos < this.state.lineStart) {
  106. this.state.lineStart = this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
  107. --this.state.curLine;
  108. }
  109. this.nextToken();
  110. }
  111. curContext() {
  112. return this.state.context[this.state.context.length - 1];
  113. }
  114. nextToken() {
  115. const curContext = this.curContext();
  116. if (!curContext?.preserveSpace) this.skipSpace();
  117. this.state.octalPositions = [];
  118. this.state.start = this.state.pos;
  119. this.state.startLoc = this.state.curPosition();
  120. if (this.state.pos >= this.length) {
  121. this.finishToken(_types2.types.eof);
  122. return;
  123. }
  124. const override = curContext?.override;
  125. if (override) {
  126. override(this);
  127. } else {
  128. this.getTokenFromCode(this.input.codePointAt(this.state.pos));
  129. }
  130. }
  131. pushComment(block, text, start, end, startLoc, endLoc) {
  132. const comment = {
  133. type: block ? "CommentBlock" : "CommentLine",
  134. value: text,
  135. start: start,
  136. end: end,
  137. loc: new _location.SourceLocation(startLoc, endLoc)
  138. };
  139. if (this.options.tokens) this.pushToken(comment);
  140. this.state.comments.push(comment);
  141. this.addComment(comment);
  142. }
  143. skipBlockComment() {
  144. const startLoc = this.state.curPosition();
  145. const start = this.state.pos;
  146. const end = this.input.indexOf("*/", this.state.pos + 2);
  147. if (end === -1) throw this.raise(start, _error.Errors.UnterminatedComment);
  148. this.state.pos = end + 2;
  149. _whitespace.lineBreakG.lastIndex = start;
  150. let match;
  151. while ((match = _whitespace.lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  152. ++this.state.curLine;
  153. this.state.lineStart = match.index + match[0].length;
  154. }
  155. if (this.isLookahead) return;
  156. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  157. }
  158. skipLineComment(startSkip) {
  159. const start = this.state.pos;
  160. const startLoc = this.state.curPosition();
  161. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  162. if (this.state.pos < this.length) {
  163. while (!(0, _whitespace.isNewLine)(ch) && ++this.state.pos < this.length) {
  164. ch = this.input.charCodeAt(this.state.pos);
  165. }
  166. }
  167. if (this.isLookahead) return;
  168. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  169. }
  170. skipSpace() {
  171. loop: while (this.state.pos < this.length) {
  172. const ch = this.input.charCodeAt(this.state.pos);
  173. switch (ch) {
  174. case 32:
  175. case 160:
  176. case 9:
  177. ++this.state.pos;
  178. break;
  179. case 13:
  180. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  181. ++this.state.pos;
  182. }
  183. case 10:
  184. case 8232:
  185. case 8233:
  186. ++this.state.pos;
  187. ++this.state.curLine;
  188. this.state.lineStart = this.state.pos;
  189. break;
  190. case 47:
  191. switch (this.input.charCodeAt(this.state.pos + 1)) {
  192. case 42:
  193. this.skipBlockComment();
  194. break;
  195. case 47:
  196. this.skipLineComment(2);
  197. break;
  198. default:
  199. break loop;
  200. }
  201. break;
  202. default:
  203. if ((0, _whitespace.isWhitespace)(ch)) {
  204. ++this.state.pos;
  205. } else {
  206. break loop;
  207. }
  208. }
  209. }
  210. }
  211. finishToken(type, val) {
  212. this.state.end = this.state.pos;
  213. this.state.endLoc = this.state.curPosition();
  214. const prevType = this.state.type;
  215. this.state.type = type;
  216. this.state.value = val;
  217. if (!this.isLookahead) this.updateContext(prevType);
  218. }
  219. readToken_numberSign() {
  220. if (this.state.pos === 0 && this.readToken_interpreter()) {
  221. return;
  222. }
  223. const nextPos = this.state.pos + 1;
  224. const next = this.input.charCodeAt(nextPos);
  225. if (next >= 48 && next <= 57) {
  226. throw this.raise(this.state.pos, _error.Errors.UnexpectedDigitAfterHash);
  227. }
  228. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  229. this.expectPlugin("recordAndTuple");
  230. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  231. throw this.raise(this.state.pos, next === 123 ? _error.Errors.RecordExpressionHashIncorrectStartSyntaxType : _error.Errors.TupleExpressionHashIncorrectStartSyntaxType);
  232. }
  233. if (next === 123) {
  234. this.finishToken(_types2.types.braceHashL);
  235. } else {
  236. this.finishToken(_types2.types.bracketHashL);
  237. }
  238. this.state.pos += 2;
  239. } else {
  240. this.finishOp(_types2.types.hash, 1);
  241. }
  242. }
  243. readToken_dot() {
  244. const next = this.input.charCodeAt(this.state.pos + 1);
  245. if (next >= 48 && next <= 57) {
  246. this.readNumber(true);
  247. return;
  248. }
  249. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  250. this.state.pos += 3;
  251. this.finishToken(_types2.types.ellipsis);
  252. } else {
  253. ++this.state.pos;
  254. this.finishToken(_types2.types.dot);
  255. }
  256. }
  257. readToken_slash() {
  258. if (this.state.exprAllowed && !this.state.inType) {
  259. ++this.state.pos;
  260. this.readRegexp();
  261. return;
  262. }
  263. const next = this.input.charCodeAt(this.state.pos + 1);
  264. if (next === 61) {
  265. this.finishOp(_types2.types.assign, 2);
  266. } else {
  267. this.finishOp(_types2.types.slash, 1);
  268. }
  269. }
  270. readToken_interpreter() {
  271. if (this.state.pos !== 0 || this.length < 2) return false;
  272. let ch = this.input.charCodeAt(this.state.pos + 1);
  273. if (ch !== 33) return false;
  274. const start = this.state.pos;
  275. this.state.pos += 1;
  276. while (!(0, _whitespace.isNewLine)(ch) && ++this.state.pos < this.length) {
  277. ch = this.input.charCodeAt(this.state.pos);
  278. }
  279. const value = this.input.slice(start + 2, this.state.pos);
  280. this.finishToken(_types2.types.interpreterDirective, value);
  281. return true;
  282. }
  283. readToken_mult_modulo(code) {
  284. let type = code === 42 ? _types2.types.star : _types2.types.modulo;
  285. let width = 1;
  286. let next = this.input.charCodeAt(this.state.pos + 1);
  287. const exprAllowed = this.state.exprAllowed;
  288. if (code === 42 && next === 42) {
  289. width++;
  290. next = this.input.charCodeAt(this.state.pos + 2);
  291. type = _types2.types.exponent;
  292. }
  293. if (next === 61 && !exprAllowed) {
  294. width++;
  295. type = _types2.types.assign;
  296. }
  297. this.finishOp(type, width);
  298. }
  299. readToken_pipe_amp(code) {
  300. const next = this.input.charCodeAt(this.state.pos + 1);
  301. if (next === code) {
  302. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  303. this.finishOp(_types2.types.assign, 3);
  304. } else {
  305. this.finishOp(code === 124 ? _types2.types.logicalOR : _types2.types.logicalAND, 2);
  306. }
  307. return;
  308. }
  309. if (code === 124) {
  310. if (next === 62) {
  311. this.finishOp(_types2.types.pipeline, 2);
  312. return;
  313. }
  314. if (this.hasPlugin("recordAndTuple") && next === 125) {
  315. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  316. throw this.raise(this.state.pos, _error.Errors.RecordExpressionBarIncorrectEndSyntaxType);
  317. }
  318. this.finishOp(_types2.types.braceBarR, 2);
  319. return;
  320. }
  321. if (this.hasPlugin("recordAndTuple") && next === 93) {
  322. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  323. throw this.raise(this.state.pos, _error.Errors.TupleExpressionBarIncorrectEndSyntaxType);
  324. }
  325. this.finishOp(_types2.types.bracketBarR, 2);
  326. return;
  327. }
  328. }
  329. if (next === 61) {
  330. this.finishOp(_types2.types.assign, 2);
  331. return;
  332. }
  333. this.finishOp(code === 124 ? _types2.types.bitwiseOR : _types2.types.bitwiseAND, 1);
  334. }
  335. readToken_caret() {
  336. const next = this.input.charCodeAt(this.state.pos + 1);
  337. if (next === 61) {
  338. this.finishOp(_types2.types.assign, 2);
  339. } else {
  340. this.finishOp(_types2.types.bitwiseXOR, 1);
  341. }
  342. }
  343. readToken_plus_min(code) {
  344. const next = this.input.charCodeAt(this.state.pos + 1);
  345. if (next === code) {
  346. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || this.hasPrecedingLineBreak())) {
  347. this.skipLineComment(3);
  348. this.skipSpace();
  349. this.nextToken();
  350. return;
  351. }
  352. this.finishOp(_types2.types.incDec, 2);
  353. return;
  354. }
  355. if (next === 61) {
  356. this.finishOp(_types2.types.assign, 2);
  357. } else {
  358. this.finishOp(_types2.types.plusMin, 1);
  359. }
  360. }
  361. readToken_lt_gt(code) {
  362. const next = this.input.charCodeAt(this.state.pos + 1);
  363. let size = 1;
  364. if (next === code) {
  365. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  366. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  367. this.finishOp(_types2.types.assign, size + 1);
  368. return;
  369. }
  370. this.finishOp(_types2.types.bitShift, size);
  371. return;
  372. }
  373. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  374. this.skipLineComment(4);
  375. this.skipSpace();
  376. this.nextToken();
  377. return;
  378. }
  379. if (next === 61) {
  380. size = 2;
  381. }
  382. this.finishOp(_types2.types.relational, size);
  383. }
  384. readToken_eq_excl(code) {
  385. const next = this.input.charCodeAt(this.state.pos + 1);
  386. if (next === 61) {
  387. this.finishOp(_types2.types.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  388. return;
  389. }
  390. if (code === 61 && next === 62) {
  391. this.state.pos += 2;
  392. this.finishToken(_types2.types.arrow);
  393. return;
  394. }
  395. this.finishOp(code === 61 ? _types2.types.eq : _types2.types.bang, 1);
  396. }
  397. readToken_question() {
  398. const next = this.input.charCodeAt(this.state.pos + 1);
  399. const next2 = this.input.charCodeAt(this.state.pos + 2);
  400. if (next === 63) {
  401. if (next2 === 61) {
  402. this.finishOp(_types2.types.assign, 3);
  403. } else {
  404. this.finishOp(_types2.types.nullishCoalescing, 2);
  405. }
  406. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  407. this.state.pos += 2;
  408. this.finishToken(_types2.types.questionDot);
  409. } else {
  410. ++this.state.pos;
  411. this.finishToken(_types2.types.question);
  412. }
  413. }
  414. getTokenFromCode(code) {
  415. switch (code) {
  416. case 46:
  417. this.readToken_dot();
  418. return;
  419. case 40:
  420. ++this.state.pos;
  421. this.finishToken(_types2.types.parenL);
  422. return;
  423. case 41:
  424. ++this.state.pos;
  425. this.finishToken(_types2.types.parenR);
  426. return;
  427. case 59:
  428. ++this.state.pos;
  429. this.finishToken(_types2.types.semi);
  430. return;
  431. case 44:
  432. ++this.state.pos;
  433. this.finishToken(_types2.types.comma);
  434. return;
  435. case 91:
  436. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  437. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  438. throw this.raise(this.state.pos, _error.Errors.TupleExpressionBarIncorrectStartSyntaxType);
  439. }
  440. this.finishToken(_types2.types.bracketBarL);
  441. this.state.pos += 2;
  442. } else {
  443. ++this.state.pos;
  444. this.finishToken(_types2.types.bracketL);
  445. }
  446. return;
  447. case 93:
  448. ++this.state.pos;
  449. this.finishToken(_types2.types.bracketR);
  450. return;
  451. case 123:
  452. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  453. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  454. throw this.raise(this.state.pos, _error.Errors.RecordExpressionBarIncorrectStartSyntaxType);
  455. }
  456. this.finishToken(_types2.types.braceBarL);
  457. this.state.pos += 2;
  458. } else {
  459. ++this.state.pos;
  460. this.finishToken(_types2.types.braceL);
  461. }
  462. return;
  463. case 125:
  464. ++this.state.pos;
  465. this.finishToken(_types2.types.braceR);
  466. return;
  467. case 58:
  468. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  469. this.finishOp(_types2.types.doubleColon, 2);
  470. } else {
  471. ++this.state.pos;
  472. this.finishToken(_types2.types.colon);
  473. }
  474. return;
  475. case 63:
  476. this.readToken_question();
  477. return;
  478. case 96:
  479. ++this.state.pos;
  480. this.finishToken(_types2.types.backQuote);
  481. return;
  482. case 48:
  483. {
  484. const next = this.input.charCodeAt(this.state.pos + 1);
  485. if (next === 120 || next === 88) {
  486. this.readRadixNumber(16);
  487. return;
  488. }
  489. if (next === 111 || next === 79) {
  490. this.readRadixNumber(8);
  491. return;
  492. }
  493. if (next === 98 || next === 66) {
  494. this.readRadixNumber(2);
  495. return;
  496. }
  497. }
  498. case 49:
  499. case 50:
  500. case 51:
  501. case 52:
  502. case 53:
  503. case 54:
  504. case 55:
  505. case 56:
  506. case 57:
  507. this.readNumber(false);
  508. return;
  509. case 34:
  510. case 39:
  511. this.readString(code);
  512. return;
  513. case 47:
  514. this.readToken_slash();
  515. return;
  516. case 37:
  517. case 42:
  518. this.readToken_mult_modulo(code);
  519. return;
  520. case 124:
  521. case 38:
  522. this.readToken_pipe_amp(code);
  523. return;
  524. case 94:
  525. this.readToken_caret();
  526. return;
  527. case 43:
  528. case 45:
  529. this.readToken_plus_min(code);
  530. return;
  531. case 60:
  532. case 62:
  533. this.readToken_lt_gt(code);
  534. return;
  535. case 61:
  536. case 33:
  537. this.readToken_eq_excl(code);
  538. return;
  539. case 126:
  540. this.finishOp(_types2.types.tilde, 1);
  541. return;
  542. case 64:
  543. ++this.state.pos;
  544. this.finishToken(_types2.types.at);
  545. return;
  546. case 35:
  547. this.readToken_numberSign();
  548. return;
  549. case 92:
  550. this.readWord();
  551. return;
  552. default:
  553. if ((0, _identifier.isIdentifierStart)(code)) {
  554. this.readWord();
  555. return;
  556. }
  557. }
  558. throw this.raise(this.state.pos, _error.Errors.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  559. }
  560. finishOp(type, size) {
  561. const str = this.input.slice(this.state.pos, this.state.pos + size);
  562. this.state.pos += size;
  563. this.finishToken(type, str);
  564. }
  565. readRegexp() {
  566. const start = this.state.pos;
  567. let escaped, inClass;
  568. for (;;) {
  569. if (this.state.pos >= this.length) {
  570. throw this.raise(start, _error.Errors.UnterminatedRegExp);
  571. }
  572. const ch = this.input.charAt(this.state.pos);
  573. if (_whitespace.lineBreak.test(ch)) {
  574. throw this.raise(start, _error.Errors.UnterminatedRegExp);
  575. }
  576. if (escaped) {
  577. escaped = false;
  578. } else {
  579. if (ch === "[") {
  580. inClass = true;
  581. } else if (ch === "]" && inClass) {
  582. inClass = false;
  583. } else if (ch === "/" && !inClass) {
  584. break;
  585. }
  586. escaped = ch === "\\";
  587. }
  588. ++this.state.pos;
  589. }
  590. const content = this.input.slice(start, this.state.pos);
  591. ++this.state.pos;
  592. let mods = "";
  593. while (this.state.pos < this.length) {
  594. const char = this.input[this.state.pos];
  595. const charCode = this.input.codePointAt(this.state.pos);
  596. if (VALID_REGEX_FLAGS.has(char)) {
  597. if (mods.indexOf(char) > -1) {
  598. this.raise(this.state.pos + 1, _error.Errors.DuplicateRegExpFlags);
  599. }
  600. } else if ((0, _identifier.isIdentifierChar)(charCode) || charCode === 92) {
  601. this.raise(this.state.pos + 1, _error.Errors.MalformedRegExpFlags);
  602. } else {
  603. break;
  604. }
  605. ++this.state.pos;
  606. mods += char;
  607. }
  608. this.finishToken(_types2.types.regexp, {
  609. pattern: content,
  610. flags: mods
  611. });
  612. }
  613. readInt(radix, len, forceLen, allowNumSeparator = true) {
  614. const start = this.state.pos;
  615. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  616. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  617. let invalid = false;
  618. let total = 0;
  619. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  620. const code = this.input.charCodeAt(this.state.pos);
  621. let val;
  622. if (code === 95) {
  623. const prev = this.input.charCodeAt(this.state.pos - 1);
  624. const next = this.input.charCodeAt(this.state.pos + 1);
  625. if (allowedSiblings.indexOf(next) === -1) {
  626. this.raise(this.state.pos, _error.Errors.UnexpectedNumericSeparator);
  627. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  628. this.raise(this.state.pos, _error.Errors.UnexpectedNumericSeparator);
  629. }
  630. if (!allowNumSeparator) {
  631. this.raise(this.state.pos, _error.Errors.NumericSeparatorInEscapeSequence);
  632. }
  633. ++this.state.pos;
  634. continue;
  635. }
  636. if (code >= 97) {
  637. val = code - 97 + 10;
  638. } else if (code >= 65) {
  639. val = code - 65 + 10;
  640. } else if (_isDigit(code)) {
  641. val = code - 48;
  642. } else {
  643. val = Infinity;
  644. }
  645. if (val >= radix) {
  646. if (this.options.errorRecovery && val <= 9) {
  647. val = 0;
  648. this.raise(this.state.start + i + 2, _error.Errors.InvalidDigit, radix);
  649. } else if (forceLen) {
  650. val = 0;
  651. invalid = true;
  652. } else {
  653. break;
  654. }
  655. }
  656. ++this.state.pos;
  657. total = total * radix + val;
  658. }
  659. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  660. return null;
  661. }
  662. return total;
  663. }
  664. readRadixNumber(radix) {
  665. const start = this.state.pos;
  666. let isBigInt = false;
  667. this.state.pos += 2;
  668. const val = this.readInt(radix);
  669. if (val == null) {
  670. this.raise(this.state.start + 2, _error.Errors.InvalidDigit, radix);
  671. }
  672. const next = this.input.charCodeAt(this.state.pos);
  673. if (next === 110) {
  674. ++this.state.pos;
  675. isBigInt = true;
  676. } else if (next === 109) {
  677. throw this.raise(start, _error.Errors.InvalidDecimal);
  678. }
  679. if ((0, _identifier.isIdentifierStart)(this.input.codePointAt(this.state.pos))) {
  680. throw this.raise(this.state.pos, _error.Errors.NumberIdentifier);
  681. }
  682. if (isBigInt) {
  683. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  684. this.finishToken(_types2.types.bigint, str);
  685. return;
  686. }
  687. this.finishToken(_types2.types.num, val);
  688. }
  689. readNumber(startsWithDot) {
  690. const start = this.state.pos;
  691. let isFloat = false;
  692. let isBigInt = false;
  693. let isDecimal = false;
  694. let hasExponent = false;
  695. let isOctal = false;
  696. if (!startsWithDot && this.readInt(10) === null) {
  697. this.raise(start, _error.Errors.InvalidNumber);
  698. }
  699. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  700. if (hasLeadingZero) {
  701. const integer = this.input.slice(start, this.state.pos);
  702. if (this.state.strict) {
  703. this.raise(start, _error.Errors.StrictOctalLiteral);
  704. } else {
  705. const underscorePos = integer.indexOf("_");
  706. if (underscorePos > 0) {
  707. this.raise(underscorePos + start, _error.Errors.ZeroDigitNumericSeparator);
  708. }
  709. }
  710. isOctal = hasLeadingZero && !/[89]/.test(integer);
  711. }
  712. let next = this.input.charCodeAt(this.state.pos);
  713. if (next === 46 && !isOctal) {
  714. ++this.state.pos;
  715. this.readInt(10);
  716. isFloat = true;
  717. next = this.input.charCodeAt(this.state.pos);
  718. }
  719. if ((next === 69 || next === 101) && !isOctal) {
  720. next = this.input.charCodeAt(++this.state.pos);
  721. if (next === 43 || next === 45) {
  722. ++this.state.pos;
  723. }
  724. if (this.readInt(10) === null) {
  725. this.raise(start, _error.Errors.InvalidOrMissingExponent);
  726. }
  727. isFloat = true;
  728. hasExponent = true;
  729. next = this.input.charCodeAt(this.state.pos);
  730. }
  731. if (next === 110) {
  732. if (isFloat || hasLeadingZero) {
  733. this.raise(start, _error.Errors.InvalidBigIntLiteral);
  734. }
  735. ++this.state.pos;
  736. isBigInt = true;
  737. }
  738. if (next === 109) {
  739. this.expectPlugin("decimal", this.state.pos);
  740. if (hasExponent || hasLeadingZero) {
  741. this.raise(start, _error.Errors.InvalidDecimal);
  742. }
  743. ++this.state.pos;
  744. isDecimal = true;
  745. }
  746. if ((0, _identifier.isIdentifierStart)(this.input.codePointAt(this.state.pos))) {
  747. throw this.raise(this.state.pos, _error.Errors.NumberIdentifier);
  748. }
  749. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  750. if (isBigInt) {
  751. this.finishToken(_types2.types.bigint, str);
  752. return;
  753. }
  754. if (isDecimal) {
  755. this.finishToken(_types2.types.decimal, str);
  756. return;
  757. }
  758. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  759. this.finishToken(_types2.types.num, val);
  760. }
  761. readCodePoint(throwOnInvalid) {
  762. const ch = this.input.charCodeAt(this.state.pos);
  763. let code;
  764. if (ch === 123) {
  765. const codePos = ++this.state.pos;
  766. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  767. ++this.state.pos;
  768. if (code !== null && code > 0x10ffff) {
  769. if (throwOnInvalid) {
  770. this.raise(codePos, _error.Errors.InvalidCodePoint);
  771. } else {
  772. return null;
  773. }
  774. }
  775. } else {
  776. code = this.readHexChar(4, false, throwOnInvalid);
  777. }
  778. return code;
  779. }
  780. readString(quote) {
  781. let out = "",
  782. chunkStart = ++this.state.pos;
  783. for (;;) {
  784. if (this.state.pos >= this.length) {
  785. throw this.raise(this.state.start, _error.Errors.UnterminatedString);
  786. }
  787. const ch = this.input.charCodeAt(this.state.pos);
  788. if (ch === quote) break;
  789. if (ch === 92) {
  790. out += this.input.slice(chunkStart, this.state.pos);
  791. out += this.readEscapedChar(false);
  792. chunkStart = this.state.pos;
  793. } else if (ch === 8232 || ch === 8233) {
  794. ++this.state.pos;
  795. ++this.state.curLine;
  796. this.state.lineStart = this.state.pos;
  797. } else if ((0, _whitespace.isNewLine)(ch)) {
  798. throw this.raise(this.state.start, _error.Errors.UnterminatedString);
  799. } else {
  800. ++this.state.pos;
  801. }
  802. }
  803. out += this.input.slice(chunkStart, this.state.pos++);
  804. this.finishToken(_types2.types.string, out);
  805. }
  806. readTmplToken() {
  807. let out = "",
  808. chunkStart = this.state.pos,
  809. containsInvalid = false;
  810. for (;;) {
  811. if (this.state.pos >= this.length) {
  812. throw this.raise(this.state.start, _error.Errors.UnterminatedTemplate);
  813. }
  814. const ch = this.input.charCodeAt(this.state.pos);
  815. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  816. if (this.state.pos === this.state.start && this.match(_types2.types.template)) {
  817. if (ch === 36) {
  818. this.state.pos += 2;
  819. this.finishToken(_types2.types.dollarBraceL);
  820. return;
  821. } else {
  822. ++this.state.pos;
  823. this.finishToken(_types2.types.backQuote);
  824. return;
  825. }
  826. }
  827. out += this.input.slice(chunkStart, this.state.pos);
  828. this.finishToken(_types2.types.template, containsInvalid ? null : out);
  829. return;
  830. }
  831. if (ch === 92) {
  832. out += this.input.slice(chunkStart, this.state.pos);
  833. const escaped = this.readEscapedChar(true);
  834. if (escaped === null) {
  835. containsInvalid = true;
  836. } else {
  837. out += escaped;
  838. }
  839. chunkStart = this.state.pos;
  840. } else if ((0, _whitespace.isNewLine)(ch)) {
  841. out += this.input.slice(chunkStart, this.state.pos);
  842. ++this.state.pos;
  843. switch (ch) {
  844. case 13:
  845. if (this.input.charCodeAt(this.state.pos) === 10) {
  846. ++this.state.pos;
  847. }
  848. case 10:
  849. out += "\n";
  850. break;
  851. default:
  852. out += String.fromCharCode(ch);
  853. break;
  854. }
  855. ++this.state.curLine;
  856. this.state.lineStart = this.state.pos;
  857. chunkStart = this.state.pos;
  858. } else {
  859. ++this.state.pos;
  860. }
  861. }
  862. }
  863. readEscapedChar(inTemplate) {
  864. const throwOnInvalid = !inTemplate;
  865. const ch = this.input.charCodeAt(++this.state.pos);
  866. ++this.state.pos;
  867. switch (ch) {
  868. case 110:
  869. return "\n";
  870. case 114:
  871. return "\r";
  872. case 120:
  873. {
  874. const code = this.readHexChar(2, false, throwOnInvalid);
  875. return code === null ? null : String.fromCharCode(code);
  876. }
  877. case 117:
  878. {
  879. const code = this.readCodePoint(throwOnInvalid);
  880. return code === null ? null : String.fromCodePoint(code);
  881. }
  882. case 116:
  883. return "\t";
  884. case 98:
  885. return "\b";
  886. case 118:
  887. return "\u000b";
  888. case 102:
  889. return "\f";
  890. case 13:
  891. if (this.input.charCodeAt(this.state.pos) === 10) {
  892. ++this.state.pos;
  893. }
  894. case 10:
  895. this.state.lineStart = this.state.pos;
  896. ++this.state.curLine;
  897. case 8232:
  898. case 8233:
  899. return "";
  900. case 56:
  901. case 57:
  902. if (inTemplate) {
  903. return null;
  904. } else if (this.state.strict) {
  905. this.raise(this.state.pos - 1, _error.Errors.StrictNumericEscape);
  906. }
  907. default:
  908. if (ch >= 48 && ch <= 55) {
  909. const codePos = this.state.pos - 1;
  910. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  911. let octalStr = match[0];
  912. let octal = parseInt(octalStr, 8);
  913. if (octal > 255) {
  914. octalStr = octalStr.slice(0, -1);
  915. octal = parseInt(octalStr, 8);
  916. }
  917. this.state.pos += octalStr.length - 1;
  918. const next = this.input.charCodeAt(this.state.pos);
  919. if (octalStr !== "0" || next === 56 || next === 57) {
  920. if (inTemplate) {
  921. return null;
  922. } else if (this.state.strict) {
  923. this.raise(codePos, _error.Errors.StrictNumericEscape);
  924. } else {
  925. this.state.octalPositions.push(codePos);
  926. }
  927. }
  928. return String.fromCharCode(octal);
  929. }
  930. return String.fromCharCode(ch);
  931. }
  932. }
  933. readHexChar(len, forceLen, throwOnInvalid) {
  934. const codePos = this.state.pos;
  935. const n = this.readInt(16, len, forceLen, false);
  936. if (n === null) {
  937. if (throwOnInvalid) {
  938. this.raise(codePos, _error.Errors.InvalidEscapeSequence);
  939. } else {
  940. this.state.pos = codePos - 1;
  941. }
  942. }
  943. return n;
  944. }
  945. readWord1() {
  946. let word = "";
  947. this.state.containsEsc = false;
  948. const start = this.state.pos;
  949. let chunkStart = this.state.pos;
  950. while (this.state.pos < this.length) {
  951. const ch = this.input.codePointAt(this.state.pos);
  952. if ((0, _identifier.isIdentifierChar)(ch)) {
  953. this.state.pos += ch <= 0xffff ? 1 : 2;
  954. } else if (this.state.isIterator && ch === 64) {
  955. ++this.state.pos;
  956. } else if (ch === 92) {
  957. this.state.containsEsc = true;
  958. word += this.input.slice(chunkStart, this.state.pos);
  959. const escStart = this.state.pos;
  960. const identifierCheck = this.state.pos === start ? _identifier.isIdentifierStart : _identifier.isIdentifierChar;
  961. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  962. this.raise(this.state.pos, _error.Errors.MissingUnicodeEscape);
  963. continue;
  964. }
  965. ++this.state.pos;
  966. const esc = this.readCodePoint(true);
  967. if (esc !== null) {
  968. if (!identifierCheck(esc)) {
  969. this.raise(escStart, _error.Errors.EscapedCharNotAnIdentifier);
  970. }
  971. word += String.fromCodePoint(esc);
  972. }
  973. chunkStart = this.state.pos;
  974. } else {
  975. break;
  976. }
  977. }
  978. return word + this.input.slice(chunkStart, this.state.pos);
  979. }
  980. isIterator(word) {
  981. return word === "@@iterator" || word === "@@asyncIterator";
  982. }
  983. readWord() {
  984. const word = this.readWord1();
  985. const type = _types2.keywords.get(word) || _types2.types.name;
  986. if (this.state.isIterator && (!this.isIterator(word) || !this.state.inType)) {
  987. this.raise(this.state.pos, _error.Errors.InvalidIdentifier, word);
  988. }
  989. this.finishToken(type, word);
  990. }
  991. checkKeywordEscapes() {
  992. const kw = this.state.type.keyword;
  993. if (kw && this.state.containsEsc) {
  994. this.raise(this.state.start, _error.Errors.InvalidEscapedReservedWord, kw);
  995. }
  996. }
  997. braceIsBlock(prevType) {
  998. const parent = this.curContext();
  999. if (parent === _context.types.functionExpression || parent === _context.types.functionStatement) {
  1000. return true;
  1001. }
  1002. if (prevType === _types2.types.colon && (parent === _context.types.braceStatement || parent === _context.types.braceExpression)) {
  1003. return !parent.isExpr;
  1004. }
  1005. if (prevType === _types2.types._return || prevType === _types2.types.name && this.state.exprAllowed) {
  1006. return this.hasPrecedingLineBreak();
  1007. }
  1008. if (prevType === _types2.types._else || prevType === _types2.types.semi || prevType === _types2.types.eof || prevType === _types2.types.parenR || prevType === _types2.types.arrow) {
  1009. return true;
  1010. }
  1011. if (prevType === _types2.types.braceL) {
  1012. return parent === _context.types.braceStatement;
  1013. }
  1014. if (prevType === _types2.types._var || prevType === _types2.types._const || prevType === _types2.types.name) {
  1015. return false;
  1016. }
  1017. if (prevType === _types2.types.relational) {
  1018. return true;
  1019. }
  1020. return !this.state.exprAllowed;
  1021. }
  1022. updateContext(prevType) {
  1023. const type = this.state.type;
  1024. let update;
  1025. if (type.keyword && (prevType === _types2.types.dot || prevType === _types2.types.questionDot)) {
  1026. this.state.exprAllowed = false;
  1027. } else if (update = type.updateContext) {
  1028. update.call(this, prevType);
  1029. } else {
  1030. this.state.exprAllowed = type.beforeExpr;
  1031. }
  1032. }
  1033. }
  1034. exports.default = Tokenizer;