Newer
Older
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
// YOU ARE NOT SUPPOSED TO MODIFY THIS FILE.
// An interface that represents what a token (lexeme) is.
// Examples of tokens: keyword `let`, opening parenthesis, closing parenthesis, number, identifier, etc.
export interface Token {
kind: TokenKind;
value: string;
}
// A type that represents possible kinds of tokens.
export type TokenKind =
| "OpMulDiv"
| "OpAddSub"
| "Equals"
| "OpeningBracket"
| "ClosingBracket"
| "Separator"
| "Number"
| "Identifier"
| "PhysicalUnit"
| "KeywordLet";
const units = [
"g",
"kg",
"m",
"km",
"min",
"s",
"h",
"km/h",
"km/s",
"km/min",
"m/h",
"m/s",
"m/min",
];
export class Tokenizer {
private tokens: Token[] = [];
private current = -1; // Have to start at -1 because first iteration advances
//
constructor(private source: string) {}
//
tokenize(): Token[] {
while (this.current < this.source.length - 1) {
this.scanToken();
}
return this.tokens;
}
//
private scanToken() {
const char = this.advance();
switch (char) {
case "+":
case "-":
this.consumeToken("OpAddSub", char);
break;
case "/":
case "*":
this.consumeToken("OpMulDiv", char);
break;
case "=":
this.consumeToken("Equals", char);
break;
case "[":
this.consumeUnit();
break;
case "(":
this.consumeToken("OpeningBracket", char);
break;
case ")":
this.consumeToken("ClosingBracket", char);
case " ":
case "\n":
break;
case ";":
this.consumeToken("Separator", ";");
break;
default:
if (this.isAlpha(char)) {
this.consumeAlpha();
} else if (this.isNumerical(char)) {
this.consumeNumber();
} else {
throw new Error("Invalid token");
}
break;
}
}
//
private consumeNumber() {
let numberWord = "";
while (true) {
numberWord += this.getCurrent();
if (!this.isNumerical(this.peek(1))) {
break;
}
this.current += 1;
}
this.consumeToken("Number", numberWord);
}
//
private consumeAlpha() {
let word = "";
while (true) {
word += this.getCurrent();
if (!this.isAlpha(this.peek(1))) {
break;
}
this.current += 1;
}
if (word === "let") {
this.consumeToken("KeywordLet", word);
} else {
this.consumeToken("Identifier", word);
}
}
//
private consumeUnit() {
this.current += 1;
let unit = "";
while (this.isAlpha(this.getCurrent()) || this.getCurrent() === "/") {
unit += this.getCurrent();
this.current += 1;
}
this.consumeToken("PhysicalUnit", unit);
}
//
private getCurrent() {
return this.source[this.current];
}
//
private peek(x: number) {
return this.source[this.current + x];
}
//
private consumeToken(tokenType: TokenKind, token: string) {
this.tokens.push({ kind: tokenType, value: token });
}
//
private advance(): string {
this.current += 1;
return this.source[this.current];
}
//
private isAlpha(val: string): boolean {
let alphabet = new Set(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_".split("")
);
return alphabet.has(val);
}
//
private isNumerical(char: string): boolean {
let numbers = new Set("1234567890".split(""));
return numbers.has(char);
}
}