-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.js
74 lines (55 loc) · 1.1 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
const Spec = [
// White space
[/^\s+/, null],
// Symbols
[/^\[/, '['],
[/^\]/, ']'],
[/^\{/, '{'],
[/^\}/, '}'],
[/^\,/, ','],
[/^\:/, ':'],
// Keywords
[/^\btrue\b/, "TRUE"],
[/^\bfalse\b/, "FALSE"],
[/^\bnull\b/, "NULL"],
// Literals
[/^"[^"]*"/, 'STRING'],
[/^\d+/, 'NUMBER'],
]
class Tokenizer {
constructor(str) {
this._str = str;
this._cur = 0;
}
peek() {
if (this._cur == this._str.length) return null;
return this._str[this._cur];
}
next() {
if (!this.peek()) return null;
const str = this._str.substring(this._cur);
for (const [ptrn, type] of Spec) {
const token = this._match(ptrn, str);
if (token == null) {
continue;
}
if (type == null) {
return this.next();
}
return {
type,
value: token
}
}
throw new SyntaxError(`Unexpected token [${str[0]}]`);
}
_match(ptrn, str) {
const match = ptrn.exec(str);
if (match == null) return null;
this._cur += match[0].length;
return match[0];
}
}
module.exports = {
Tokenizer
}