-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.ts
69 lines (64 loc) · 1.51 KB
/
tokenizer.ts
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
import { LETTER, NUMBERS, WHITESPACE } from './utils/reg'
import type { Token } from './utils/type'
import { TokenType } from './utils/type'
// 将字符串代码转换成tokens数组
export function tokenizer(input: string) {
// 当前index
let current = 0
// tokens数组
const tokens: Token[] = []
// 遍历字符串
while (current < input.length) {
let char = input[current]
if (char === '(') {
tokens.push({
type: TokenType.Paren,
value: '(',
})
// 🤔 current 和 continue的位置
current++
continue
}
if (char === ')') {
tokens.push({
type: TokenType.Paren,
value: ')',
})
current++
continue
}
// 跳过空格
if (WHITESPACE.test(char)) {
current++
continue
}
// 匹配字母 忽略大小写 并且只匹配一个
if (LETTER.test(char)) {
let value = ''
// current < input.length 不加此判断会报错 Uncaught RangeError: Invalid string length
while (LETTER.test(char) && current < input.length) {
value += char
char = input[++current]
}
tokens.push({
type: TokenType.Name,
value,
})
continue
}
// 匹配数字 并且只匹配一个
if (NUMBERS.test(char)) {
let value = ''
while (NUMBERS.test(char)) {
value += char
char = input[++current]
}
tokens.push({
type: TokenType.Number,
value,
})
continue
}
}
return tokens
}