-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.js
264 lines (231 loc) · 8.09 KB
/
lexer.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Removes comments from the given
// source string.
let remove_comments = source => {
// Using [^\n] as per
// https://stackoverflow.com/a/3850095/1371131.
// That will match up to but not including
// the line break.
return source.replace(/;.*[^\n]*/g, "");
};
let last = a => a[a.length - 1];
let isNumber = a => /\d/.test(a);
let isDelimiter = a => /[{}\[\]()""]/.test(a)
let delimiterPairs = {
"{": "}",
"[": "]",
"(": ")",
"\"": "\""
}
let checkDelimiters = (tokenContainer) => {
let delimiters = [];
tokenContainer.result.forEach(token => {
if (isDelimiter(token[0])) {
if (token[0] === delimiterPairs[last(tokenContainer.unclosedDelimiters)]) {
tokenContainer.unclosedDelimiters.pop();
} else {
tokenContainer.unclosedDelimiters.push(token[0]);
}
}
});
}
// Takes a source string and turns
// it into a sequence of tokens.
let lex = source => {
// Remove comments
source = remove_comments(source);
let tokenContainer = source
.split("")
.reduce((acc, curr, i) => {
switch (acc.mode) {
case "number": {
// We want to close the number and ignore
// the current character
if (curr === " " || curr === "\n" || curr === ",") {
acc.mode = null;
return acc;
// We want to ignore underscores (they serve
// merely as optional thousand separators)
} else if (curr === "_") {
return acc;
// A number must not contain the following
// special characters. For example, in "foo(1 2 3)",
// the last parenthesis is not part of the number 3,
// even though they are not separated by a
// whitespace.
} else if (/[{}\[\]()#~]/.test(curr)) {
acc.mode = null;
acc.result.push([curr, curr]);
return acc;
// Consume the next character as part
// of the number
} else {
let resultCount = acc.result.length;
let currResultCount = acc.result[resultCount - 1].length;
acc.result[resultCount - 1][currResultCount - 1] += curr;
return acc;
}
}
case "symbol": {
// We want to close the symbol and ignore
// the current character
if (curr === " " || curr === "\n" || curr === ",") {
acc.mode = null;
return acc;
// A symbol must not contain the following
// special characters. For example, in "foo()",
// the parentheses are not part of the symbol,
// even though they are not separated by a
// whitespace.
} else if (/[{}\[\]()#~]/.test(curr)) {
acc.mode = null;
acc.result.push([curr, curr]);
return acc;
// Consume the next character as part
// of the symbol
} else {
let resultCount = acc.result.length;
let currResultCount = acc.result[resultCount - 1].length;
acc.result[resultCount - 1][currResultCount - 1] += curr;
return acc;
}
}
case "string": {
// We want to close the string
if (curr === "\"") {
acc.mode = null;
acc.result.push(["\"", "\""]);
return acc;
// Consume the next character as part
// of the string
} else {
let resultCount = acc.result.length;
let currResultCount = acc.result[resultCount - 1].length;
acc.result[resultCount - 1][currResultCount - 1] += curr;
return acc;
}
}
case "keyword": {
// We want to close the keyword
if (curr === " " || curr === "\n" || curr === ",") {
acc.mode = null;
return acc;
// A keyword must not contain the following
// special characters. For example, in "... :foo)" --
// perhaps as the last element of an argument list --
// the closing parenthesis is not part of the keyword,
// even though they are not separated by a
// whitespace.
} else if (/[{}\[\]()#~]/.test(curr)) {
acc.mode = null;
acc.result.push([curr, curr]);
return acc;
// Consume the next character as part
// of the keyword
} else {
let resultCount = acc.result.length;
let currResultCount = acc.result[resultCount - 1].length;
acc.result[resultCount - 1][currResultCount - 1] += curr;
return acc;
}
}
default: {
// We want to open a string
if (curr === "\"") {
acc.result.push(["\"", "\""]);
acc.mode = "string";
acc.result.push(["string", ""]);
return acc;
// We want to open a keyword
} else if (curr === ":") {
acc.mode = "keyword";
acc.result.push(["keyword", ""]);
return acc;
// We want to open a number
} else if (
isNumber(curr) ||
(isNumber(source[i + 1]) && (curr === "-" || curr === "+"))
) {
acc.mode = "number";
// The current character is already the first
// character in the result
acc.result.push(["number", curr]);
return acc;
// All other cases: open a symbol
// (or it's a character to be ignored)
} else {
if (curr === " " || curr === "\n" || curr === ",") {
return acc;
// Turn special characters for maps,
// sets, function invocations etc into
// tokens of their own.
} else if (/[{}\[\]()#~]/.test(curr)) {
acc.result.push([curr, curr]);
return acc;
}
acc.mode = "symbol";
// The current character is already the first
// character in the result
acc.result.push(["symbol", curr]);
return acc;
}
}
}
}, {
mode: null,
result: [],
unclosedDelimiters: []
});
checkDelimiters(tokenContainer);
return tokenContainer;
};
// Next up: handle comments in lex fn instead of
// removing them beforehand. That should allow me
// to display error messages with accurate line
// and character numbers later on.
module.exports = { lex };
// The types of things the parser needs to recognize:
// Function/macro calls
// ✓ Primitives
// Special forms (which look like function calls, and
// maybe they can just *be* function calls (or macro calls), at least
// as far as the consumer of the language is concerned):
// ✓ - def
// ✓ - let (this should be achievable solely in terms of a
// function inside a macro:
// let([a 1
// b (+ a 2)])
// =
// ((fn [a]
// ((fn [b]) (+ a 2))) 1)
// ...and so on, ever more deeply nested.
// ✓ - fn
// ✓ (- maybe later: fn! for functions that can only access
// the values of symbols they are explicitly given in the
// form of arguments)
// ✓ Blocks
// ✓ Maps, Arrays, Sets
// ✓ Booleans (which look like symbols)
// Destructuring
// ✓ JS interop with .fn, .-attr, fn. for constructors, and set! like in ClojureScript
// ✓ How to use JS operators like +, -, etc?
// ✓ def (as a single statement, no block)
// ✓ if (takes at least one block, optionally a second one)
//
// No statements, no expressions. Only functions.
//
// Closing brackets and braces on same line, not next line
// (as a convention).
//
// Do I need to escape single and double quotes and
// backticks that are part of symbols, keywords, and
// strings? Is that a security issue or only a compile-time
// issue?
//
// ✓ Assume that opening parenthesis means we are invoking
// a function.
// ✓ Special functions that are guaranteed to be pure because
// the compiler raises an exception when the function body
// accesses a symbol the function wasn't explicitly passed
// (I got this idea from Brian Will, he briefly mentioned it
// in one of his videos, but I can't remember which one:
// https://www.youtube.com/user/briantwill/)