readWord.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _base = require('../traverser/base');
  2. var _charcodes = require('../util/charcodes');
  3. var _identifier = require('../util/identifier');
  4. var _index = require('./index');
  5. var _readWordTree = require('./readWordTree');
  6. var _types = require('./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. function readWord() {
  14. let treePos = 0;
  15. let code = 0;
  16. let pos = _base.state.pos;
  17. while (pos < _base.input.length) {
  18. code = _base.input.charCodeAt(pos);
  19. if (code < _charcodes.charCodes.lowercaseA || code > _charcodes.charCodes.lowercaseZ) {
  20. break;
  21. }
  22. const next = _readWordTree.READ_WORD_TREE[treePos + (code - _charcodes.charCodes.lowercaseA) + 1];
  23. if (next === -1) {
  24. break;
  25. } else {
  26. treePos = next;
  27. pos++;
  28. }
  29. }
  30. const keywordValue = _readWordTree.READ_WORD_TREE[treePos];
  31. if (keywordValue > -1 && !_identifier.IS_IDENTIFIER_CHAR[code]) {
  32. _base.state.pos = pos;
  33. if (keywordValue & 1) {
  34. _index.finishToken.call(void 0, keywordValue >>> 1);
  35. } else {
  36. _index.finishToken.call(void 0, _types.TokenType.name, keywordValue >>> 1);
  37. }
  38. return;
  39. }
  40. while (pos < _base.input.length) {
  41. const ch = _base.input.charCodeAt(pos);
  42. if (_identifier.IS_IDENTIFIER_CHAR[ch]) {
  43. pos++;
  44. } else if (ch === _charcodes.charCodes.backslash) {
  45. // \u
  46. pos += 2;
  47. if (_base.input.charCodeAt(pos) === _charcodes.charCodes.leftCurlyBrace) {
  48. while (pos < _base.input.length && _base.input.charCodeAt(pos) !== _charcodes.charCodes.rightCurlyBrace) {
  49. pos++;
  50. }
  51. pos++;
  52. }
  53. } else if (ch === _charcodes.charCodes.atSign && _base.input.charCodeAt(pos + 1) === _charcodes.charCodes.atSign) {
  54. pos += 2;
  55. } else {
  56. break;
  57. }
  58. }
  59. _base.state.pos = pos;
  60. _index.finishToken.call(void 0, _types.TokenType.name);
  61. } exports.default = readWord;