-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtokenizer.js
184 lines (152 loc) · 3.73 KB
/
tokenizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { tokenTypeMap } from './specs';
class Tokenizer {
constructor(input) {
if (typeof input !== 'string') {
throw new Error('Tokenizer input is not a string');
}
this.input = input;
this.index = 0;
this.context = null;
}
getTokens = () => {
const tokens = [];
while (this.index < this.input.length) {
tokens.push(this.getCurToken());
this.index++;
}
return tokens;
}
getCurToken = () => {
this.eatSpaces();
return (
this.readColon()
|| this.readComma()
|| this.readParens()
|| this.readBrace()
|| this.readOperator()
|| this.readString()
|| this.readNumber()
|| this.readIdentifier()
|| this.readEqual()
|| this.error()
);
}
readColon = () => {
return this.readChar('colon');
}
readComma = () => {
return this.readChar('comma');
}
readParens = () => {
return this.readChar('parens');
}
readBrace = () => {
return this.readChar('brace');
}
readOperator = () => {
return this.readChar('operator');
}
readEqual = () => {
return this.readChar('equal');
}
readString = () => {
const curChar = this.char();
const token = tokenTypeMap.string;
if (token.chars.includes(curChar)) {
const result = {
type: token.type,
value: curChar,
};
const closer = curChar;
this.index++;
// 找到字符串结尾,暂时不处理字符串中有转义的情况
while (this.char() !== closer) {
// 加上字符串中的内容
result.value += this.char();
this.index++;
}
result.value += this.char(); // 加上字符串的结尾
this.setContext(result.type);
return result;
}
}
readNumber = () => {
const curChar = this.char();
const token = tokenTypeMap.number;
if (token.regx.test(curChar)) {
const result = {
type: token.type,
value: curChar,
};
this.index++;
// TODO: process eslint error
// eslint-disable-next-line no-useless-escape
while (/[0-9\.]/.test(this.char())) { // 这里也没有处理 3.4.1 类似数字格式的情况
result.value += this.char();
this.index++;
}
this.index--;
this.setContext(result.type);
return result;
}
}
readIdentifier = () => {
const curChar = this.char();
const token = tokenTypeMap.identifier;
if (token.regx.test(curChar)) {
const result = {
type: token.type,
value: curChar,
};
this.index++;
while (token.regx.test(this.char())) {
result.value += this.char();
this.index++;
}
this.index--;
this.setContext(result.type);
return result;
}
}
readChar = type => {
const curChar = this.char();
const token = tokenTypeMap[type];
if (token.chars.includes(curChar)) {
// 处理箭头函数的情况
if (curChar === '=' && this.input[this.index + 1] === '>') {
this.setContext(tokenTypeMap.arrow.type);
this.index++; // 向前走一位
return {
type: tokenTypeMap.arrow.type,
value: tokenTypeMap.arrow.chars[0],
};
}
this.setContext(token.type);
return {
type: token.type,
value: curChar,
};
}
}
isEOF = () => {
return this.index >= this.input.length;
}
eatSpaces = () => {
while (/\s/.test(this.char())) {
this.index++;
}
}
setContext = type => {
this.context = type;
}
inContext = type => {
return this.context = type;
}
char = () => {
return this.input[this.index];
}
error = () => {
throw new Error(`Unexpected token: ${this.char()}`);
}
}
export default Tokenizer;