-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.ts
149 lines (139 loc) · 3.52 KB
/
lexer.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
import { DiscriminateUnion } from "./utils.ts";
type Token =
| { tag: "LPAREN" }
| { tag: "RPAREN" }
| { tag: "COLON" }
| { tag: "ARROW" }
| { tag: "LET" }
| { tag: "IF" }
| { tag: "AND" }
| { tag: "OR" }
| { tag: "LAMBDA" }
// | { tag: "EMPTY" }
| { tag: "BOOL"; val: boolean }
| { tag: "INT"; val: number }
| { tag: "STR"; val: string }
| { tag: "IDEN"; name: string };
export class Lexer {
private program: string;
private i = 0;
constructor(program: string) {
this.program = program.trim();
}
public peek() {
return this.calculateNextToken()[0];
}
public nextToken() {
const [result, endIdx] = this.calculateNextToken();
if (result) {
this.i = endIdx;
}
return result;
}
public consumeToken<T extends Token["tag"]>(
expected: T,
): DiscriminateUnion<Token, "tag", T> {
const token = this.nextToken();
if (token === null) {
throw new Error("eof");
}
function assertToken(t: Token): t is DiscriminateUnion<Token, "tag", T> {
return t.tag === expected;
}
if (!assertToken(token)) {
throw new Error(
`Unexpected token: expected ${expected} but got ${token.tag}`,
);
}
return token;
}
private calculateNextToken(): [Token | null, number] {
let j = this.i;
let input = this.program;
// Eat up extra whitespace before token
while (j < input.length && /\s/.test(input[j])) {
j++;
}
if (j >= input.length) {
return [null, j];
}
let char = "";
if (input[j] === '"') {
// Handle strings
char += input[j];
j++;
while (j < input.length && input[j] !== '"') {
char += input[j];
j++;
}
char += input[j];
j++;
} else if (input[j] === "(" || input[j] === ")") {
// Handle parens
char += input[j];
j++;
} else if (input[j] === ":") {
// Handle colon
char += input[j];
j++;
} else if (input[j] === "-" && input[j + 1] === ">") {
// Handle arrow
char += input[j];
j++;
char += input[j];
j++;
} else {
// Handle all other tokens
// chars which signal end of token:
// - whitespace
// - parens
// - colon
// don't need arrow since it can only come after paren when used in type ann
while (j < input.length && !/(\s|\(|\)|\:)/.test(input[j])) {
char += input[j];
j++;
}
}
function charToToken(): Token | null {
if (!char) {
return null;
}
switch (char) {
case "(":
return { tag: "LPAREN" };
case ")":
return { tag: "RPAREN" };
case ":":
return { tag: "COLON" };
case "->":
return { tag: "ARROW" };
case "let":
return { tag: "LET" };
case "if":
return { tag: "IF" };
case "and":
return { tag: "AND" };
case "or":
return { tag: "OR" };
case "lambda":
return { tag: "LAMBDA" };
case "#t":
return { tag: "BOOL", val: true };
case "#f":
return { tag: "BOOL", val: false };
}
if (parseInt(char).toString() === char) {
return { tag: "INT", val: parseInt(char) };
}
if (char[0] === '"' && char[char.length - 1] === '"') {
return { tag: "STR", val: char.slice(1, char.length - 1) };
}
return { tag: "IDEN", name: char };
}
const token = charToToken();
if (!token) {
return [null, j];
}
return [token, j];
}
}