-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtokenizer.ts
156 lines (134 loc) · 3.14 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
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
export function tokenize(input: string): Token[] {
let current = 0;
const tokens = [];
function finishIdentifier() {
let name = "";
while (isAlpha(input[current])) {
name += input[current];
current++;
}
const builder = keywords.get(name);
if (builder) {
return builder();
}
return token.identifier(name);
}
function finishStringLiteral() {
let value = "";
while (input[current] && input[current] !== "'") {
value += input[current];
current++;
}
if (input[current] === "'") {
// consume the closing tick
current++;
return token.stringLiteral(value);
}
throw new Error(`Unterminated string, expected a closing '`);
}
while (current < input.length) {
const currentChar = input[current];
if (isWhitespace(currentChar)) {
current++;
continue;
}
if (isAlpha(currentChar)) {
tokens.push(finishIdentifier());
} else if (isSingleCharacter(currentChar)) {
tokens.push(getCharToken(currentChar));
current++;
} else if (currentChar === "'") {
// consume the first tick
current++;
tokens.push(finishStringLiteral());
} else {
throw new Error(`Unknown character: ${currentChar}`);
}
}
return tokens;
}
// --
export enum TokenType {
Function = "Function",
Identifier = "Identifier",
LeftParen = "LeftParen",
RightParen = "RightParen",
LeftCurly = "LeftCurly",
RightCurly = "RightCurly",
Dot = "Dot",
Semicolon = "Semicolon",
StringLiteral = "StringLiteral",
}
export type Token =
| {
type: TokenType;
}
| {
type: TokenType.Identifier;
name: string;
}
| {
type: TokenType.StringLiteral;
value: string;
};
export const token = {
function() {
return {
type: TokenType.Function,
};
},
identifier(name: string) {
return {
type: TokenType.Identifier,
name,
};
},
leftParen() {
return { type: TokenType.LeftParen };
},
rightParen() {
return { type: TokenType.RightParen };
},
leftCurly() {
return { type: TokenType.LeftCurly };
},
rightCurly() {
return { type: TokenType.RightCurly };
},
dot() {
return { type: TokenType.Dot };
},
semicolon() {
return { type: TokenType.Semicolon };
},
stringLiteral(value: string) {
return {
type: TokenType.StringLiteral,
value,
};
},
};
const keywords = new Map([["function", token.function]]);
// --
function isAlpha(char: string) {
return /[a-zA-Z]/.test(char);
}
function isWhitespace(char: string) {
return /\s/.test(char);
}
type SingleCharacterToken = "(" | ")" | "{" | "}" | "." | ";";
const knownSingleCharacters = new Map<SingleCharacterToken, () => Token>([
["(", token.leftParen],
[")", token.rightParen],
["{", token.leftCurly],
["}", token.rightCurly],
[".", token.dot],
[";", token.semicolon],
]);
function isSingleCharacter(char: string): char is SingleCharacterToken {
return knownSingleCharacters.has(char as SingleCharacterToken);
}
function getCharToken(char: SingleCharacterToken) {
const builder = knownSingleCharacters.get(char);
return builder!();
}