-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.ts
153 lines (146 loc) · 3.73 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
150
151
152
153
export type SourceInfo = { startIdx: number; endIdx: number };
type Token =
& { info: SourceInfo }
& {
token: (
| { tag: "LPAREN" }
| { tag: "RPAREN" }
| { tag: "LCURLY" }
| { tag: "RCURLY" }
| { tag: "COLON" }
| { tag: "ARROW" }
| { tag: "LET" }
| { tag: "IF" }
| { tag: "AND" }
| { tag: "OR" }
| { tag: "LAMBDA" }
| { tag: "REF" }
| { tag: "PROJ" }
| { tag: "EMPTY" }
| { tag: "BOOL"; val: boolean }
| { tag: "INT"; val: number }
| { tag: "STR"; val: string }
| { tag: "IDEN"; name: string }
);
};
export type Lexer = { peek: () => Token | null; nextToken: () => Token | null };
export function createLexer(s: string): Lexer {
let i = 0;
const input = s.trim();
function calculateNextToken(): Token | null {
let j = i;
// Eat up extra whitespace before token
while (j < input.length && /\s/.test(input[j])) {
j++;
}
if (j >= input.length) {
return null;
}
let char = "";
let startIdx = j;
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] === "{" || input[j] === "}") {
// Handle curly braces
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
// - curly braces
// - 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["token"] | null {
if (!char) {
return null;
}
switch (char) {
case "(":
return { tag: "LPAREN" };
case ")":
return { tag: "RPAREN" };
case "{":
return { tag: "LCURLY" };
case "}":
return { tag: "RCURLY" };
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 "ref":
return { tag: "REF" };
case "get-field":
return { tag: "PROJ" };
case "empty":
return { tag: "EMPTY" };
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;
}
return { token, info: { startIdx, endIdx: j } };
}
return {
peek: () => {
return calculateNextToken();
},
nextToken: () => {
const result = calculateNextToken();
if (result) {
i = result.info.endIdx;
}
return result;
},
};
}