-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.js
More file actions
50 lines (44 loc) · 1.42 KB
/
tokenizer.js
File metadata and controls
50 lines (44 loc) · 1.42 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
function tokenizer(script) {
const reMap = {
blank: /\s+/y,
paren: /[()]/y,
symbol: /[_$A-Za-z\u4e00-\u9fa5][\w\u4e00-\u9fa5]*/y,
number: /\d+/y,
}
let res
let resTag = false
let lastIndex = 0
const maxLen = script.length
const tokens = []
do {
// 用于判断正则是否全部遍历过,并且没有找到相应的表达式,而导致的死循环
resTag = false
for (const key in reMap) {
if (reMap.hasOwnProperty(key)) {
const re = reMap[key];
re.lastIndex = lastIndex
res = re.exec(script)
if (res) {
lastIndex = re.lastIndex
// TODO: token的分支
resTag = true
if (key === 'blank') {
break
} else {
tokens.push({
type: key,
value: res[0]
})
// res.lastIndex = 0
break
}
}
}
}
if (!resTag && lastIndex !== maxLen) {
throw new Error('token resolved failed')
}
} while (res && lastIndex !== maxLen)
return tokens
}
module.exports = tokenizer