Repository navigation
Expand file tree
/
Copy pathtokenizer.ts
More file actions
227 lines (203 loc) · 7.24 KB
/
Copy pathtokenizer.ts
File metadata and controls
227 lines (203 loc) · 7.24 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* A real single-pass JS lexer producing a token stream, shared by anything
* that needs to reason about REPL input structurally instead of guessing
* from raw substrings (`code.includes('(')` and friends are exactly the
* kind of brittle, false-positive-prone checks this exists to replace -
* a string literal containing "(" or a `=>` arrow both defeat a naive
* `includes` check, but not a real token stream).
*
* This is a lexer, not a parser: it has no grammar/AST, so it can't answer
* "is this valid JS" or "what does this expression evaluate to" - only
* "what are the tokens, in order, ignoring string/comment/regex-shaped
* traps". That's enough for the two things this project needs it for:
* syntax highlighting (syntax-highlighter.ts) and lightweight structural
* checks on REPL input (repl.ts) - e.g. "is the first real token `{`" or
* "is this `(` a call, based on what token precedes it".
*/
export type TokenType =
| 'string'
| 'comment'
| 'number'
| 'keyword'
| 'boolean'
| 'identifier'
| 'operator'
| 'punctuation'
| 'whitespace';
export interface Token {
type: TokenType;
value: string;
}
const KEYWORDS = new Set([
'var', 'let', 'const', 'if', 'else', 'for', 'while', 'do', 'return',
'function', 'class', 'new', 'try', 'catch', 'finally', 'switch', 'case',
'break', 'continue', 'default', 'import', 'export', 'from', 'async',
'await', 'this', 'typeof', 'void', 'delete', 'in', 'of', 'instanceof',
'extends', 'super', 'throw', 'yield', 'debugger'
]);
const BOOLEANS = new Set(['true', 'false', 'null', 'undefined']);
const PUNCTUATION = new Set(['(', ')', '{', '}', '[', ']', ',', ';', ':', '.']);
// Longest-match-first so e.g. `>>>=` isn't split into `>>>` + `=`, and `=>`
// isn't mistaken for an assignment by anything consuming this token stream.
const MULTI_CHAR_OPERATORS = [
'>>>=', '**=', '<<=', '>>=', '&&=', '||=', '??=', '>>>',
'===', '!==', '...', '?.',
'=>', '==', '!=', '<=', '>=', '&&', '||', '??', '**', '++', '--',
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<', '>>'
];
const SINGLE_CHAR_OPERATORS = new Set(['+', '-', '*', '/', '%', '=', '&', '|', '<', '>', '!', '^', '~', '?']);
/**
* The most recent token that carries structural meaning, skipping back over
* trivia. Cheap in practice: a whitespace run is already collapsed into a
* single token, so this walks past a handful of entries at most, and since
* it stops at the first significant token the runs never overlap.
*/
function lastSignificantToken(tokens: Token[]): Token | null {
for (let i = tokens.length - 1; i >= 0; i--) {
const token = tokens[i];
if (token.type !== 'whitespace' && token.type !== 'comment') {
return token;
}
}
return null;
}
export function tokenize(code: string): Token[] {
const tokens: Token[] = [];
let i = 0;
const len = code.length;
while (i < len) {
const char = code[i];
if (/\s/.test(char)) {
let value = char;
i++;
while (i < len && /\s/.test(code[i])) {
value += code[i];
i++;
}
tokens.push({ type: 'whitespace', value });
continue;
}
if (char === '"' || char === "'") {
const quote = char;
let value = quote;
i++;
while (i < len) {
const c = code[i];
value += c;
i++;
if (c === '\\') {
if (i < len) {
value += code[i];
i++;
}
} else if (c === quote) {
break;
}
}
tokens.push({ type: 'string', value });
continue;
}
if (char === '`') {
let value = char;
i++;
while (i < len) {
const c = code[i];
value += c;
i++;
if (c === '\\') {
if (i < len) {
value += code[i];
i++;
}
} else if (c === '`') {
break;
}
}
tokens.push({ type: 'string', value });
continue;
}
if (char === '/' && code[i + 1] === '/') {
let value = '//';
i += 2;
while (i < len && code[i] !== '\n') {
value += code[i];
i++;
}
tokens.push({ type: 'comment', value });
continue;
}
if (char === '/' && code[i + 1] === '*') {
let value = '/*';
i += 2;
while (i < len) {
const c = code[i];
value += c;
i++;
if (c === '*' && code[i] === '/') {
value += '/';
i++;
break;
}
}
tokens.push({ type: 'comment', value });
continue;
}
if (/\d/.test(char)) {
let value = char;
i++;
while (i < len && /[\d.]/.test(code[i])) {
value += code[i];
i++;
}
tokens.push({ type: 'number', value });
continue;
}
if (/[a-zA-Z_$]/.test(char)) {
let value = char;
i++;
while (i < len && /[a-zA-Z0-9_$]/.test(code[i])) {
value += code[i];
i++;
}
// A word directly after `.` / `?.` is a property name, never a
// keyword - JS allows reserved words there (`gen.return()`,
// `Array.from`, `mod.default`). Classifying those as keywords
// both mis-colors them and, worse, hides the call from
// repl.ts's "is the token before `(` callable" check.
const prev = lastSignificantToken(tokens);
const afterMemberAccess = prev !== null && (
(prev.type === 'punctuation' && prev.value === '.') ||
(prev.type === 'operator' && prev.value === '?.')
);
if (afterMemberAccess) {
tokens.push({ type: 'identifier', value });
} else if (KEYWORDS.has(value)) {
tokens.push({ type: 'keyword', value });
} else if (BOOLEANS.has(value)) {
tokens.push({ type: 'boolean', value });
} else {
tokens.push({ type: 'identifier', value });
}
continue;
}
const multiCharMatch = MULTI_CHAR_OPERATORS.find((op) => code.startsWith(op, i));
if (multiCharMatch) {
tokens.push({ type: 'operator', value: multiCharMatch });
i += multiCharMatch.length;
continue;
}
if (PUNCTUATION.has(char)) {
tokens.push({ type: 'punctuation', value: char });
i++;
continue;
}
if (SINGLE_CHAR_OPERATORS.has(char)) {
tokens.push({ type: 'operator', value: char });
i++;
continue;
}
tokens.push({ type: 'punctuation', value: char });
i++;
}
return tokens;
}