index.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /* eslint max-len: 0 */
  2. import {input, isFlowEnabled, state} from "../traverser/base";
  3. import {unexpected} from "../traverser/util";
  4. import {charCodes} from "../util/charcodes";
  5. import {IS_IDENTIFIER_CHAR, IS_IDENTIFIER_START} from "../util/identifier";
  6. import {IS_WHITESPACE, skipWhiteSpace} from "../util/whitespace";
  7. import {ContextualKeyword} from "./keywords";
  8. import readWord from "./readWord";
  9. import { TokenType as tt} from "./types";
  10. export var IdentifierRole; (function (IdentifierRole) {
  11. const Access = 0; IdentifierRole[IdentifierRole["Access"] = Access] = "Access";
  12. const ExportAccess = Access + 1; IdentifierRole[IdentifierRole["ExportAccess"] = ExportAccess] = "ExportAccess";
  13. const TopLevelDeclaration = ExportAccess + 1; IdentifierRole[IdentifierRole["TopLevelDeclaration"] = TopLevelDeclaration] = "TopLevelDeclaration";
  14. const FunctionScopedDeclaration = TopLevelDeclaration + 1; IdentifierRole[IdentifierRole["FunctionScopedDeclaration"] = FunctionScopedDeclaration] = "FunctionScopedDeclaration";
  15. const BlockScopedDeclaration = FunctionScopedDeclaration + 1; IdentifierRole[IdentifierRole["BlockScopedDeclaration"] = BlockScopedDeclaration] = "BlockScopedDeclaration";
  16. const ObjectShorthandTopLevelDeclaration = BlockScopedDeclaration + 1; IdentifierRole[IdentifierRole["ObjectShorthandTopLevelDeclaration"] = ObjectShorthandTopLevelDeclaration] = "ObjectShorthandTopLevelDeclaration";
  17. const ObjectShorthandFunctionScopedDeclaration = ObjectShorthandTopLevelDeclaration + 1; IdentifierRole[IdentifierRole["ObjectShorthandFunctionScopedDeclaration"] = ObjectShorthandFunctionScopedDeclaration] = "ObjectShorthandFunctionScopedDeclaration";
  18. const ObjectShorthandBlockScopedDeclaration = ObjectShorthandFunctionScopedDeclaration + 1; IdentifierRole[IdentifierRole["ObjectShorthandBlockScopedDeclaration"] = ObjectShorthandBlockScopedDeclaration] = "ObjectShorthandBlockScopedDeclaration";
  19. const ObjectShorthand = ObjectShorthandBlockScopedDeclaration + 1; IdentifierRole[IdentifierRole["ObjectShorthand"] = ObjectShorthand] = "ObjectShorthand";
  20. // Any identifier bound in an import statement, e.g. both A and b from
  21. // `import A, * as b from 'A';`
  22. const ImportDeclaration = ObjectShorthand + 1; IdentifierRole[IdentifierRole["ImportDeclaration"] = ImportDeclaration] = "ImportDeclaration";
  23. const ObjectKey = ImportDeclaration + 1; IdentifierRole[IdentifierRole["ObjectKey"] = ObjectKey] = "ObjectKey";
  24. // The `foo` in `import {foo as bar} from "./abc";`.
  25. const ImportAccess = ObjectKey + 1; IdentifierRole[IdentifierRole["ImportAccess"] = ImportAccess] = "ImportAccess";
  26. })(IdentifierRole || (IdentifierRole = {}));
  27. /**
  28. * Extra information on jsxTagStart tokens, used to determine which of the three
  29. * jsx functions are called in the automatic transform.
  30. */
  31. export var JSXRole; (function (JSXRole) {
  32. // The element is self-closing or has a body that resolves to empty. We
  33. // shouldn't emit children at all in this case.
  34. const NoChildren = 0; JSXRole[JSXRole["NoChildren"] = NoChildren] = "NoChildren";
  35. // The element has a single explicit child, which might still be an arbitrary
  36. // expression like an array. We should emit that expression as the children.
  37. const OneChild = NoChildren + 1; JSXRole[JSXRole["OneChild"] = OneChild] = "OneChild";
  38. // The element has at least two explicitly-specified children or has spread
  39. // children, so child positions are assumed to be "static". We should wrap
  40. // these children in an array.
  41. const StaticChildren = OneChild + 1; JSXRole[JSXRole["StaticChildren"] = StaticChildren] = "StaticChildren";
  42. // The element has a prop named "key" after a prop spread, so we should fall
  43. // back to the createElement function.
  44. const KeyAfterPropSpread = StaticChildren + 1; JSXRole[JSXRole["KeyAfterPropSpread"] = KeyAfterPropSpread] = "KeyAfterPropSpread";
  45. })(JSXRole || (JSXRole = {}));
  46. export function isDeclaration(token) {
  47. const role = token.identifierRole;
  48. return (
  49. role === IdentifierRole.TopLevelDeclaration ||
  50. role === IdentifierRole.FunctionScopedDeclaration ||
  51. role === IdentifierRole.BlockScopedDeclaration ||
  52. role === IdentifierRole.ObjectShorthandTopLevelDeclaration ||
  53. role === IdentifierRole.ObjectShorthandFunctionScopedDeclaration ||
  54. role === IdentifierRole.ObjectShorthandBlockScopedDeclaration
  55. );
  56. }
  57. export function isNonTopLevelDeclaration(token) {
  58. const role = token.identifierRole;
  59. return (
  60. role === IdentifierRole.FunctionScopedDeclaration ||
  61. role === IdentifierRole.BlockScopedDeclaration ||
  62. role === IdentifierRole.ObjectShorthandFunctionScopedDeclaration ||
  63. role === IdentifierRole.ObjectShorthandBlockScopedDeclaration
  64. );
  65. }
  66. export function isTopLevelDeclaration(token) {
  67. const role = token.identifierRole;
  68. return (
  69. role === IdentifierRole.TopLevelDeclaration ||
  70. role === IdentifierRole.ObjectShorthandTopLevelDeclaration ||
  71. role === IdentifierRole.ImportDeclaration
  72. );
  73. }
  74. export function isBlockScopedDeclaration(token) {
  75. const role = token.identifierRole;
  76. // Treat top-level declarations as block scope since the distinction doesn't matter here.
  77. return (
  78. role === IdentifierRole.TopLevelDeclaration ||
  79. role === IdentifierRole.BlockScopedDeclaration ||
  80. role === IdentifierRole.ObjectShorthandTopLevelDeclaration ||
  81. role === IdentifierRole.ObjectShorthandBlockScopedDeclaration
  82. );
  83. }
  84. export function isFunctionScopedDeclaration(token) {
  85. const role = token.identifierRole;
  86. return (
  87. role === IdentifierRole.FunctionScopedDeclaration ||
  88. role === IdentifierRole.ObjectShorthandFunctionScopedDeclaration
  89. );
  90. }
  91. export function isObjectShorthandDeclaration(token) {
  92. return (
  93. token.identifierRole === IdentifierRole.ObjectShorthandTopLevelDeclaration ||
  94. token.identifierRole === IdentifierRole.ObjectShorthandBlockScopedDeclaration ||
  95. token.identifierRole === IdentifierRole.ObjectShorthandFunctionScopedDeclaration
  96. );
  97. }
  98. // Object type used to represent tokens. Note that normally, tokens
  99. // simply exist as properties on the parser object. This is only
  100. // used for the onToken callback and the external tokenizer.
  101. export class Token {
  102. constructor() {
  103. this.type = state.type;
  104. this.contextualKeyword = state.contextualKeyword;
  105. this.start = state.start;
  106. this.end = state.end;
  107. this.scopeDepth = state.scopeDepth;
  108. this.isType = state.isType;
  109. this.identifierRole = null;
  110. this.jsxRole = null;
  111. this.shadowsGlobal = false;
  112. this.isAsyncOperation = false;
  113. this.contextId = null;
  114. this.rhsEndIndex = null;
  115. this.isExpression = false;
  116. this.numNullishCoalesceStarts = 0;
  117. this.numNullishCoalesceEnds = 0;
  118. this.isOptionalChainStart = false;
  119. this.isOptionalChainEnd = false;
  120. this.subscriptStartIndex = null;
  121. this.nullishStartIndex = null;
  122. }
  123. // Initially false for all tokens, then may be computed in a follow-up step that does scope
  124. // analysis.
  125. // Initially false for all tokens, but may be set during transform to mark it as containing an
  126. // await operation.
  127. // For assignments, the index of the RHS. For export tokens, the end of the export.
  128. // For class tokens, records if the class is a class expression or a class statement.
  129. // Number of times to insert a `nullishCoalesce(` snippet before this token.
  130. // Number of times to insert a `)` snippet after this token.
  131. // If true, insert an `optionalChain([` snippet before this token.
  132. // If true, insert a `])` snippet after this token.
  133. // Tag for `.`, `?.`, `[`, `?.[`, `(`, and `?.(` to denote the "root" token for this
  134. // subscript chain. This can be used to determine if this chain is an optional chain.
  135. // Tag for `??` operators to denote the root token for this nullish coalescing call.
  136. }
  137. // ## Tokenizer
  138. // Move to the next token
  139. export function next() {
  140. state.tokens.push(new Token());
  141. nextToken();
  142. }
  143. // Call instead of next when inside a template, since that needs to be handled differently.
  144. export function nextTemplateToken() {
  145. state.tokens.push(new Token());
  146. state.start = state.pos;
  147. readTmplToken();
  148. }
  149. // The tokenizer never parses regexes by default. Instead, the parser is responsible for
  150. // instructing it to parse a regex when we see a slash at the start of an expression.
  151. export function retokenizeSlashAsRegex() {
  152. if (state.type === tt.assign) {
  153. --state.pos;
  154. }
  155. readRegexp();
  156. }
  157. export function pushTypeContext(existingTokensInType) {
  158. for (let i = state.tokens.length - existingTokensInType; i < state.tokens.length; i++) {
  159. state.tokens[i].isType = true;
  160. }
  161. const oldIsType = state.isType;
  162. state.isType = true;
  163. return oldIsType;
  164. }
  165. export function popTypeContext(oldIsType) {
  166. state.isType = oldIsType;
  167. }
  168. export function eat(type) {
  169. if (match(type)) {
  170. next();
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. }
  176. export function eatTypeToken(tokenType) {
  177. const oldIsType = state.isType;
  178. state.isType = true;
  179. eat(tokenType);
  180. state.isType = oldIsType;
  181. }
  182. export function match(type) {
  183. return state.type === type;
  184. }
  185. export function lookaheadType() {
  186. const snapshot = state.snapshot();
  187. next();
  188. const type = state.type;
  189. state.restoreFromSnapshot(snapshot);
  190. return type;
  191. }
  192. export class TypeAndKeyword {
  193. constructor(type, contextualKeyword) {
  194. this.type = type;
  195. this.contextualKeyword = contextualKeyword;
  196. }
  197. }
  198. export function lookaheadTypeAndKeyword() {
  199. const snapshot = state.snapshot();
  200. next();
  201. const type = state.type;
  202. const contextualKeyword = state.contextualKeyword;
  203. state.restoreFromSnapshot(snapshot);
  204. return new TypeAndKeyword(type, contextualKeyword);
  205. }
  206. export function nextTokenStart() {
  207. return nextTokenStartSince(state.pos);
  208. }
  209. export function nextTokenStartSince(pos) {
  210. skipWhiteSpace.lastIndex = pos;
  211. const skip = skipWhiteSpace.exec(input);
  212. return pos + skip[0].length;
  213. }
  214. export function lookaheadCharCode() {
  215. return input.charCodeAt(nextTokenStart());
  216. }
  217. // Read a single token, updating the parser object's token-related
  218. // properties.
  219. export function nextToken() {
  220. skipSpace();
  221. state.start = state.pos;
  222. if (state.pos >= input.length) {
  223. const tokens = state.tokens;
  224. // We normally run past the end a bit, but if we're way past the end, avoid an infinite loop.
  225. // Also check the token positions rather than the types since sometimes we rewrite the token
  226. // type to something else.
  227. if (
  228. tokens.length >= 2 &&
  229. tokens[tokens.length - 1].start >= input.length &&
  230. tokens[tokens.length - 2].start >= input.length
  231. ) {
  232. unexpected("Unexpectedly reached the end of input.");
  233. }
  234. finishToken(tt.eof);
  235. return;
  236. }
  237. readToken(input.charCodeAt(state.pos));
  238. }
  239. function readToken(code) {
  240. // Identifier or keyword. '\uXXXX' sequences are allowed in
  241. // identifiers, so '\' also dispatches to that.
  242. if (
  243. IS_IDENTIFIER_START[code] ||
  244. code === charCodes.backslash ||
  245. (code === charCodes.atSign && input.charCodeAt(state.pos + 1) === charCodes.atSign)
  246. ) {
  247. readWord();
  248. } else {
  249. getTokenFromCode(code);
  250. }
  251. }
  252. function skipBlockComment() {
  253. while (
  254. input.charCodeAt(state.pos) !== charCodes.asterisk ||
  255. input.charCodeAt(state.pos + 1) !== charCodes.slash
  256. ) {
  257. state.pos++;
  258. if (state.pos > input.length) {
  259. unexpected("Unterminated comment", state.pos - 2);
  260. return;
  261. }
  262. }
  263. state.pos += 2;
  264. }
  265. export function skipLineComment(startSkip) {
  266. let ch = input.charCodeAt((state.pos += startSkip));
  267. if (state.pos < input.length) {
  268. while (
  269. ch !== charCodes.lineFeed &&
  270. ch !== charCodes.carriageReturn &&
  271. ch !== charCodes.lineSeparator &&
  272. ch !== charCodes.paragraphSeparator &&
  273. ++state.pos < input.length
  274. ) {
  275. ch = input.charCodeAt(state.pos);
  276. }
  277. }
  278. }
  279. // Called at the start of the parse and after every token. Skips
  280. // whitespace and comments.
  281. export function skipSpace() {
  282. while (state.pos < input.length) {
  283. const ch = input.charCodeAt(state.pos);
  284. switch (ch) {
  285. case charCodes.carriageReturn:
  286. if (input.charCodeAt(state.pos + 1) === charCodes.lineFeed) {
  287. ++state.pos;
  288. }
  289. case charCodes.lineFeed:
  290. case charCodes.lineSeparator:
  291. case charCodes.paragraphSeparator:
  292. ++state.pos;
  293. break;
  294. case charCodes.slash:
  295. switch (input.charCodeAt(state.pos + 1)) {
  296. case charCodes.asterisk:
  297. state.pos += 2;
  298. skipBlockComment();
  299. break;
  300. case charCodes.slash:
  301. skipLineComment(2);
  302. break;
  303. default:
  304. return;
  305. }
  306. break;
  307. default:
  308. if (IS_WHITESPACE[ch]) {
  309. ++state.pos;
  310. } else {
  311. return;
  312. }
  313. }
  314. }
  315. }
  316. // Called at the end of every token. Sets various fields, and skips the space after the token, so
  317. // that the next one's `start` will point at the right position.
  318. export function finishToken(
  319. type,
  320. contextualKeyword = ContextualKeyword.NONE,
  321. ) {
  322. state.end = state.pos;
  323. state.type = type;
  324. state.contextualKeyword = contextualKeyword;
  325. }
  326. // ### Token reading
  327. // This is the function that is called to fetch the next token. It
  328. // is somewhat obscure, because it works in character codes rather
  329. // than characters, and because operator parsing has been inlined
  330. // into it.
  331. //
  332. // All in the name of speed.
  333. function readToken_dot() {
  334. const nextChar = input.charCodeAt(state.pos + 1);
  335. if (nextChar >= charCodes.digit0 && nextChar <= charCodes.digit9) {
  336. readNumber(true);
  337. return;
  338. }
  339. if (nextChar === charCodes.dot && input.charCodeAt(state.pos + 2) === charCodes.dot) {
  340. state.pos += 3;
  341. finishToken(tt.ellipsis);
  342. } else {
  343. ++state.pos;
  344. finishToken(tt.dot);
  345. }
  346. }
  347. function readToken_slash() {
  348. const nextChar = input.charCodeAt(state.pos + 1);
  349. if (nextChar === charCodes.equalsTo) {
  350. finishOp(tt.assign, 2);
  351. } else {
  352. finishOp(tt.slash, 1);
  353. }
  354. }
  355. function readToken_mult_modulo(code) {
  356. // '%*'
  357. let tokenType = code === charCodes.asterisk ? tt.star : tt.modulo;
  358. let width = 1;
  359. let nextChar = input.charCodeAt(state.pos + 1);
  360. // Exponentiation operator **
  361. if (code === charCodes.asterisk && nextChar === charCodes.asterisk) {
  362. width++;
  363. nextChar = input.charCodeAt(state.pos + 2);
  364. tokenType = tt.exponent;
  365. }
  366. // Match *= or %=, disallowing *=> which can be valid in flow.
  367. if (
  368. nextChar === charCodes.equalsTo &&
  369. input.charCodeAt(state.pos + 2) !== charCodes.greaterThan
  370. ) {
  371. width++;
  372. tokenType = tt.assign;
  373. }
  374. finishOp(tokenType, width);
  375. }
  376. function readToken_pipe_amp(code) {
  377. // '|&'
  378. const nextChar = input.charCodeAt(state.pos + 1);
  379. if (nextChar === code) {
  380. if (input.charCodeAt(state.pos + 2) === charCodes.equalsTo) {
  381. // ||= or &&=
  382. finishOp(tt.assign, 3);
  383. } else {
  384. // || or &&
  385. finishOp(code === charCodes.verticalBar ? tt.logicalOR : tt.logicalAND, 2);
  386. }
  387. return;
  388. }
  389. if (code === charCodes.verticalBar) {
  390. // '|>'
  391. if (nextChar === charCodes.greaterThan) {
  392. finishOp(tt.pipeline, 2);
  393. return;
  394. } else if (nextChar === charCodes.rightCurlyBrace && isFlowEnabled) {
  395. // '|}'
  396. finishOp(tt.braceBarR, 2);
  397. return;
  398. }
  399. }
  400. if (nextChar === charCodes.equalsTo) {
  401. finishOp(tt.assign, 2);
  402. return;
  403. }
  404. finishOp(code === charCodes.verticalBar ? tt.bitwiseOR : tt.bitwiseAND, 1);
  405. }
  406. function readToken_caret() {
  407. // '^'
  408. const nextChar = input.charCodeAt(state.pos + 1);
  409. if (nextChar === charCodes.equalsTo) {
  410. finishOp(tt.assign, 2);
  411. } else {
  412. finishOp(tt.bitwiseXOR, 1);
  413. }
  414. }
  415. function readToken_plus_min(code) {
  416. // '+-'
  417. const nextChar = input.charCodeAt(state.pos + 1);
  418. if (nextChar === code) {
  419. // Tentatively call this a prefix operator, but it might be changed to postfix later.
  420. finishOp(tt.preIncDec, 2);
  421. return;
  422. }
  423. if (nextChar === charCodes.equalsTo) {
  424. finishOp(tt.assign, 2);
  425. } else if (code === charCodes.plusSign) {
  426. finishOp(tt.plus, 1);
  427. } else {
  428. finishOp(tt.minus, 1);
  429. }
  430. }
  431. function readToken_lt() {
  432. const nextChar = input.charCodeAt(state.pos + 1);
  433. if (nextChar === charCodes.lessThan) {
  434. if (input.charCodeAt(state.pos + 2) === charCodes.equalsTo) {
  435. finishOp(tt.assign, 3);
  436. return;
  437. }
  438. // We see <<, but need to be really careful about whether to treat it as a
  439. // true left-shift or as two < tokens.
  440. if (state.isType) {
  441. // Within a type, << might come up in a snippet like `Array<<T>() => void>`,
  442. // so treat it as two < tokens. Importantly, this should only override <<
  443. // rather than other tokens like <= . If we treated <= as < in a type
  444. // context, then the snippet `a as T <= 1` would incorrectly start parsing
  445. // a type argument on T. We don't need to worry about `a as T << 1`
  446. // because TypeScript disallows that syntax.
  447. finishOp(tt.lessThan, 1);
  448. } else {
  449. // Outside a type, this might be a true left-shift operator, or it might
  450. // still be two open-type-arg tokens, such as in `f<<T>() => void>()`. We
  451. // look at the token while considering the `f`, so we don't yet know that
  452. // we're in a type context. In this case, we initially tokenize as a
  453. // left-shift and correct after-the-fact as necessary in
  454. // tsParseTypeArgumentsWithPossibleBitshift .
  455. finishOp(tt.bitShiftL, 2);
  456. }
  457. return;
  458. }
  459. if (nextChar === charCodes.equalsTo) {
  460. // <=
  461. finishOp(tt.relationalOrEqual, 2);
  462. } else {
  463. finishOp(tt.lessThan, 1);
  464. }
  465. }
  466. function readToken_gt() {
  467. if (state.isType) {
  468. // Avoid right-shift for things like `Array<Array<string>>` and
  469. // greater-than-or-equal for things like `const a: Array<number>=[];`.
  470. finishOp(tt.greaterThan, 1);
  471. return;
  472. }
  473. const nextChar = input.charCodeAt(state.pos + 1);
  474. if (nextChar === charCodes.greaterThan) {
  475. const size = input.charCodeAt(state.pos + 2) === charCodes.greaterThan ? 3 : 2;
  476. if (input.charCodeAt(state.pos + size) === charCodes.equalsTo) {
  477. finishOp(tt.assign, size + 1);
  478. return;
  479. }
  480. finishOp(tt.bitShiftR, size);
  481. return;
  482. }
  483. if (nextChar === charCodes.equalsTo) {
  484. // >=
  485. finishOp(tt.relationalOrEqual, 2);
  486. } else {
  487. finishOp(tt.greaterThan, 1);
  488. }
  489. }
  490. /**
  491. * Reinterpret a possible > token when transitioning from a type to a non-type
  492. * context.
  493. *
  494. * This comes up in two situations where >= needs to be treated as one token:
  495. * - After an `as` expression, like in the code `a as T >= 1`.
  496. * - In a type argument in an expression context, e.g. `f(a < b, c >= d)`, we
  497. * need to see the token as >= so that we get an error and backtrack to
  498. * normal expression parsing.
  499. *
  500. * Other situations require >= to be seen as two tokens, e.g.
  501. * `const x: Array<T>=[];`, so it's important to treat > as its own token in
  502. * typical type parsing situations.
  503. */
  504. export function rescan_gt() {
  505. if (state.type === tt.greaterThan) {
  506. state.pos -= 1;
  507. readToken_gt();
  508. }
  509. }
  510. function readToken_eq_excl(code) {
  511. // '=!'
  512. const nextChar = input.charCodeAt(state.pos + 1);
  513. if (nextChar === charCodes.equalsTo) {
  514. finishOp(tt.equality, input.charCodeAt(state.pos + 2) === charCodes.equalsTo ? 3 : 2);
  515. return;
  516. }
  517. if (code === charCodes.equalsTo && nextChar === charCodes.greaterThan) {
  518. // '=>'
  519. state.pos += 2;
  520. finishToken(tt.arrow);
  521. return;
  522. }
  523. finishOp(code === charCodes.equalsTo ? tt.eq : tt.bang, 1);
  524. }
  525. function readToken_question() {
  526. // '?'
  527. const nextChar = input.charCodeAt(state.pos + 1);
  528. const nextChar2 = input.charCodeAt(state.pos + 2);
  529. if (
  530. nextChar === charCodes.questionMark &&
  531. // In Flow (but not TypeScript), ??string is a valid type that should be
  532. // tokenized as two individual ? tokens.
  533. !(isFlowEnabled && state.isType)
  534. ) {
  535. if (nextChar2 === charCodes.equalsTo) {
  536. // '??='
  537. finishOp(tt.assign, 3);
  538. } else {
  539. // '??'
  540. finishOp(tt.nullishCoalescing, 2);
  541. }
  542. } else if (
  543. nextChar === charCodes.dot &&
  544. !(nextChar2 >= charCodes.digit0 && nextChar2 <= charCodes.digit9)
  545. ) {
  546. // '.' not followed by a number
  547. state.pos += 2;
  548. finishToken(tt.questionDot);
  549. } else {
  550. ++state.pos;
  551. finishToken(tt.question);
  552. }
  553. }
  554. export function getTokenFromCode(code) {
  555. switch (code) {
  556. case charCodes.numberSign:
  557. ++state.pos;
  558. finishToken(tt.hash);
  559. return;
  560. // The interpretation of a dot depends on whether it is followed
  561. // by a digit or another two dots.
  562. case charCodes.dot:
  563. readToken_dot();
  564. return;
  565. // Punctuation tokens.
  566. case charCodes.leftParenthesis:
  567. ++state.pos;
  568. finishToken(tt.parenL);
  569. return;
  570. case charCodes.rightParenthesis:
  571. ++state.pos;
  572. finishToken(tt.parenR);
  573. return;
  574. case charCodes.semicolon:
  575. ++state.pos;
  576. finishToken(tt.semi);
  577. return;
  578. case charCodes.comma:
  579. ++state.pos;
  580. finishToken(tt.comma);
  581. return;
  582. case charCodes.leftSquareBracket:
  583. ++state.pos;
  584. finishToken(tt.bracketL);
  585. return;
  586. case charCodes.rightSquareBracket:
  587. ++state.pos;
  588. finishToken(tt.bracketR);
  589. return;
  590. case charCodes.leftCurlyBrace:
  591. if (isFlowEnabled && input.charCodeAt(state.pos + 1) === charCodes.verticalBar) {
  592. finishOp(tt.braceBarL, 2);
  593. } else {
  594. ++state.pos;
  595. finishToken(tt.braceL);
  596. }
  597. return;
  598. case charCodes.rightCurlyBrace:
  599. ++state.pos;
  600. finishToken(tt.braceR);
  601. return;
  602. case charCodes.colon:
  603. if (input.charCodeAt(state.pos + 1) === charCodes.colon) {
  604. finishOp(tt.doubleColon, 2);
  605. } else {
  606. ++state.pos;
  607. finishToken(tt.colon);
  608. }
  609. return;
  610. case charCodes.questionMark:
  611. readToken_question();
  612. return;
  613. case charCodes.atSign:
  614. ++state.pos;
  615. finishToken(tt.at);
  616. return;
  617. case charCodes.graveAccent:
  618. ++state.pos;
  619. finishToken(tt.backQuote);
  620. return;
  621. case charCodes.digit0: {
  622. const nextChar = input.charCodeAt(state.pos + 1);
  623. // '0x', '0X', '0o', '0O', '0b', '0B'
  624. if (
  625. nextChar === charCodes.lowercaseX ||
  626. nextChar === charCodes.uppercaseX ||
  627. nextChar === charCodes.lowercaseO ||
  628. nextChar === charCodes.uppercaseO ||
  629. nextChar === charCodes.lowercaseB ||
  630. nextChar === charCodes.uppercaseB
  631. ) {
  632. readRadixNumber();
  633. return;
  634. }
  635. }
  636. // Anything else beginning with a digit is an integer, octal
  637. // number, or float.
  638. case charCodes.digit1:
  639. case charCodes.digit2:
  640. case charCodes.digit3:
  641. case charCodes.digit4:
  642. case charCodes.digit5:
  643. case charCodes.digit6:
  644. case charCodes.digit7:
  645. case charCodes.digit8:
  646. case charCodes.digit9:
  647. readNumber(false);
  648. return;
  649. // Quotes produce strings.
  650. case charCodes.quotationMark:
  651. case charCodes.apostrophe:
  652. readString(code);
  653. return;
  654. // Operators are parsed inline in tiny state machines. '=' (charCodes.equalsTo) is
  655. // often referred to. `finishOp` simply skips the amount of
  656. // characters it is given as second argument, and returns a token
  657. // of the type given by its first argument.
  658. case charCodes.slash:
  659. readToken_slash();
  660. return;
  661. case charCodes.percentSign:
  662. case charCodes.asterisk:
  663. readToken_mult_modulo(code);
  664. return;
  665. case charCodes.verticalBar:
  666. case charCodes.ampersand:
  667. readToken_pipe_amp(code);
  668. return;
  669. case charCodes.caret:
  670. readToken_caret();
  671. return;
  672. case charCodes.plusSign:
  673. case charCodes.dash:
  674. readToken_plus_min(code);
  675. return;
  676. case charCodes.lessThan:
  677. readToken_lt();
  678. return;
  679. case charCodes.greaterThan:
  680. readToken_gt();
  681. return;
  682. case charCodes.equalsTo:
  683. case charCodes.exclamationMark:
  684. readToken_eq_excl(code);
  685. return;
  686. case charCodes.tilde:
  687. finishOp(tt.tilde, 1);
  688. return;
  689. default:
  690. break;
  691. }
  692. unexpected(`Unexpected character '${String.fromCharCode(code)}'`, state.pos);
  693. }
  694. function finishOp(type, size) {
  695. state.pos += size;
  696. finishToken(type);
  697. }
  698. function readRegexp() {
  699. const start = state.pos;
  700. let escaped = false;
  701. let inClass = false;
  702. for (;;) {
  703. if (state.pos >= input.length) {
  704. unexpected("Unterminated regular expression", start);
  705. return;
  706. }
  707. const code = input.charCodeAt(state.pos);
  708. if (escaped) {
  709. escaped = false;
  710. } else {
  711. if (code === charCodes.leftSquareBracket) {
  712. inClass = true;
  713. } else if (code === charCodes.rightSquareBracket && inClass) {
  714. inClass = false;
  715. } else if (code === charCodes.slash && !inClass) {
  716. break;
  717. }
  718. escaped = code === charCodes.backslash;
  719. }
  720. ++state.pos;
  721. }
  722. ++state.pos;
  723. // Need to use `skipWord` because '\uXXXX' sequences are allowed here (don't ask).
  724. skipWord();
  725. finishToken(tt.regexp);
  726. }
  727. /**
  728. * Read a decimal integer. Note that this can't be unified with the similar code
  729. * in readRadixNumber (which also handles hex digits) because "e" needs to be
  730. * the end of the integer so that we can properly handle scientific notation.
  731. */
  732. function readInt() {
  733. while (true) {
  734. const code = input.charCodeAt(state.pos);
  735. if ((code >= charCodes.digit0 && code <= charCodes.digit9) || code === charCodes.underscore) {
  736. state.pos++;
  737. } else {
  738. break;
  739. }
  740. }
  741. }
  742. function readRadixNumber() {
  743. state.pos += 2; // 0x
  744. // Walk to the end of the number, allowing hex digits.
  745. while (true) {
  746. const code = input.charCodeAt(state.pos);
  747. if (
  748. (code >= charCodes.digit0 && code <= charCodes.digit9) ||
  749. (code >= charCodes.lowercaseA && code <= charCodes.lowercaseF) ||
  750. (code >= charCodes.uppercaseA && code <= charCodes.uppercaseF) ||
  751. code === charCodes.underscore
  752. ) {
  753. state.pos++;
  754. } else {
  755. break;
  756. }
  757. }
  758. const nextChar = input.charCodeAt(state.pos);
  759. if (nextChar === charCodes.lowercaseN) {
  760. ++state.pos;
  761. finishToken(tt.bigint);
  762. } else {
  763. finishToken(tt.num);
  764. }
  765. }
  766. // Read an integer, octal integer, or floating-point number.
  767. function readNumber(startsWithDot) {
  768. let isBigInt = false;
  769. let isDecimal = false;
  770. if (!startsWithDot) {
  771. readInt();
  772. }
  773. let nextChar = input.charCodeAt(state.pos);
  774. if (nextChar === charCodes.dot) {
  775. ++state.pos;
  776. readInt();
  777. nextChar = input.charCodeAt(state.pos);
  778. }
  779. if (nextChar === charCodes.uppercaseE || nextChar === charCodes.lowercaseE) {
  780. nextChar = input.charCodeAt(++state.pos);
  781. if (nextChar === charCodes.plusSign || nextChar === charCodes.dash) {
  782. ++state.pos;
  783. }
  784. readInt();
  785. nextChar = input.charCodeAt(state.pos);
  786. }
  787. if (nextChar === charCodes.lowercaseN) {
  788. ++state.pos;
  789. isBigInt = true;
  790. } else if (nextChar === charCodes.lowercaseM) {
  791. ++state.pos;
  792. isDecimal = true;
  793. }
  794. if (isBigInt) {
  795. finishToken(tt.bigint);
  796. return;
  797. }
  798. if (isDecimal) {
  799. finishToken(tt.decimal);
  800. return;
  801. }
  802. finishToken(tt.num);
  803. }
  804. function readString(quote) {
  805. state.pos++;
  806. for (;;) {
  807. if (state.pos >= input.length) {
  808. unexpected("Unterminated string constant");
  809. return;
  810. }
  811. const ch = input.charCodeAt(state.pos);
  812. if (ch === charCodes.backslash) {
  813. state.pos++;
  814. } else if (ch === quote) {
  815. break;
  816. }
  817. state.pos++;
  818. }
  819. state.pos++;
  820. finishToken(tt.string);
  821. }
  822. // Reads template string tokens.
  823. function readTmplToken() {
  824. for (;;) {
  825. if (state.pos >= input.length) {
  826. unexpected("Unterminated template");
  827. return;
  828. }
  829. const ch = input.charCodeAt(state.pos);
  830. if (
  831. ch === charCodes.graveAccent ||
  832. (ch === charCodes.dollarSign && input.charCodeAt(state.pos + 1) === charCodes.leftCurlyBrace)
  833. ) {
  834. if (state.pos === state.start && match(tt.template)) {
  835. if (ch === charCodes.dollarSign) {
  836. state.pos += 2;
  837. finishToken(tt.dollarBraceL);
  838. return;
  839. } else {
  840. ++state.pos;
  841. finishToken(tt.backQuote);
  842. return;
  843. }
  844. }
  845. finishToken(tt.template);
  846. return;
  847. }
  848. if (ch === charCodes.backslash) {
  849. state.pos++;
  850. }
  851. state.pos++;
  852. }
  853. }
  854. // Skip to the end of the current word. Note that this is the same as the snippet at the end of
  855. // readWord, but calling skipWord from readWord seems to slightly hurt performance from some rough
  856. // measurements.
  857. export function skipWord() {
  858. while (state.pos < input.length) {
  859. const ch = input.charCodeAt(state.pos);
  860. if (IS_IDENTIFIER_CHAR[ch]) {
  861. state.pos++;
  862. } else if (ch === charCodes.backslash) {
  863. // \u
  864. state.pos += 2;
  865. if (input.charCodeAt(state.pos) === charCodes.leftCurlyBrace) {
  866. while (
  867. state.pos < input.length &&
  868. input.charCodeAt(state.pos) !== charCodes.rightCurlyBrace
  869. ) {
  870. state.pos++;
  871. }
  872. state.pos++;
  873. }
  874. } else {
  875. break;
  876. }
  877. }
  878. }