UNPKG

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