readWord.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {input, state} from "../traverser/base";
  2. import {charCodes} from "../util/charcodes";
  3. import {IS_IDENTIFIER_CHAR} from "../util/identifier";
  4. import {finishToken} from "./index";
  5. import {READ_WORD_TREE} from "./readWordTree";
  6. import {TokenType as tt} from "./types";
  7. /**
  8. * Read an identifier, producing either a name token or matching on one of the existing keywords.
  9. * For performance, we pre-generate big decision tree that we traverse. Each node represents a
  10. * prefix and has 27 values, where the first value is the token or contextual token, if any (-1 if
  11. * not), and the other 26 values are the transitions to other nodes, or -1 to stop.
  12. */
  13. export default function readWord() {
  14. let treePos = 0;
  15. let code = 0;
  16. let pos = state.pos;
  17. while (pos < input.length) {
  18. code = input.charCodeAt(pos);
  19. if (code < charCodes.lowercaseA || code > charCodes.lowercaseZ) {
  20. break;
  21. }
  22. const next = READ_WORD_TREE[treePos + (code - charCodes.lowercaseA) + 1];
  23. if (next === -1) {
  24. break;
  25. } else {
  26. treePos = next;
  27. pos++;
  28. }
  29. }
  30. const keywordValue = READ_WORD_TREE[treePos];
  31. if (keywordValue > -1 && !IS_IDENTIFIER_CHAR[code]) {
  32. state.pos = pos;
  33. if (keywordValue & 1) {
  34. finishToken(keywordValue >>> 1);
  35. } else {
  36. finishToken(tt.name, keywordValue >>> 1);
  37. }
  38. return;
  39. }
  40. while (pos < input.length) {
  41. const ch = input.charCodeAt(pos);
  42. if (IS_IDENTIFIER_CHAR[ch]) {
  43. pos++;
  44. } else if (ch === charCodes.backslash) {
  45. // \u
  46. pos += 2;
  47. if (input.charCodeAt(pos) === charCodes.leftCurlyBrace) {
  48. while (pos < input.length && input.charCodeAt(pos) !== charCodes.rightCurlyBrace) {
  49. pos++;
  50. }
  51. pos++;
  52. }
  53. } else if (ch === charCodes.atSign && input.charCodeAt(pos + 1) === charCodes.atSign) {
  54. pos += 2;
  55. } else {
  56. break;
  57. }
  58. }
  59. state.pos = pos;
  60. finishToken(tt.name);
  61. }