-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.js
320 lines (297 loc) · 11.1 KB
/
parser.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function parseFunk(code) {
var offset = 0;
var currentLine = 1;
var bracketStack = ['{'];
function current() {
return code.charAt(offset) || '\n';
}
function ahead() {
return code.charAt(offset + 1) || '\n';
}
function next(skipSpace) {
if(offset > code.length) throw 'Unexpected end of file at line ' + currentLine;
offset += 1;
if(skipSpace) skipWhitespace();
return current();
}
function skipWhitespace() {
if(bracketStack[bracketStack.length - 1] != '{') return skipLineEnd();
var c = current();
while(offset < code.length && (c == ' ' || c == '\t')) {
c = next();
}
}
function skipLineEnd() {
var c = current();
while(offset < code.length && (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == ';')) {
if(c == ';') while(c != '\n') c = next();
if(c == '\n') currentLine += 1;
c = next();
}
}
function node(skipSpace, tag, line, extra) {
extra = extra || {};
extra.tag = tag;
extra.line = line;
if(skipSpace) skipWhitespace();
return extra;
}
function parseLower() {
var c = current();
var start = offset;
var line = currentLine;
if(!(c >= 'a' && c <= 'z')) return null;
while((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) c = next();
return node(true, 'Lower', line, {value: code.substring(start, offset)});
}
function parseUpper() {
var c = current();
var start = offset;
var line = currentLine;
if(!(c >= 'A' && c <= 'Z')) return null;
while((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) c = next();
return node(true, 'String', line, {value: code.substring(start, offset)});
}
function parseNumber() {
var c = current();
var start = offset;
var line = currentLine;
if(!(c >= '0' && c <= '9')) return null;
while(c >= '0' && c <= '9') c = next();
return node(true, 'Number', line, {value: code.substring(start, offset)});
}
function parseString() {
var c = current();
var start = offset;
var line = currentLine;
if(!(c == '"')) return null;
c = next();
while(c != '"') c = next();
c = next();
return node(true, 'String', line, {value: code.substring(start + 1, offset - 1)});
}
function parseLambda() {
var c = current();
var start = offset;
var line = currentLine;
if(!(c == '{')) return null;
bracketStack.push('{');
next();
skipLineEnd();
c = current();
if(!(c == '|')) {
var statements = parseStatements();
c = current();
if(!(c == '}')) throw 'Expected "}" after function at line ' + currentLine;
bracketStack.pop();
next(true);
return node(false, 'Function', line, {cases: [node(false, 'Case', line, {pattern: node(false, 'Placeholder', line), body: statements})]});
}
var cases = [];
while(c == '|') {
next(true);
var primaryPattern = parseUpper() || parseNumber() || parseString() || parseLower();
if(primaryPattern == null) {
c = current();
if(!(c == '_')) throw 'Expected "_" wildcard pattern at line ' + currentLine;
next(true);
}
var extraPatterns = [];
var extraPattern = parseLower();
c = current();
while(extraPattern != null || c == '_') {
if(extraPattern == null) next(true);
extraPatterns.push(extraPattern);
extraPattern = parseLower();
c = current();
}
c = current();
if(!(c == '|')) throw 'Expected "|" after pattern at line ' + currentLine;
next();
skipLineEnd();
var statements = parseStatements();
for(var i = extraPatterns.length - 1; i >= 0; i--) {
statements = [node(false, 'Function', line, {cases: [node(false, 'Case', line, {pattern: extraPatterns[i], body: statements})]})];
}
cases.push(node(false, 'Case', line, {pattern: primaryPattern, body: statements}));
c = current();
}
c = current();
if(!(c == '}')) throw 'Expected "}" after function at line ' + currentLine;
bracketStack.pop();
next(true);
return node(false, 'Function', line, {cases: cases});
}
function parseList() {
var c = current();
var start = offset;
var line = currentLine;
if(!(c == '[')) return null;
next(true);
bracketStack.push('[');
var elements = [];
var term = parseTerm();
c = current();
while(c == ',' && term != null) {
next(true);
elements.push(term);
term = parseTerm();
c = current();
}
if(term != null) elements.push(term);
var rest = null;
c = current();
if(c == '|') {
next(true);
rest = parseTerm();
if(rest == null) throw 'Expected term after "|" at line ' + currentLine;
}
c = current();
if(!(c == ']')) throw 'Expected "]" after "[" at line ' + currentLine;
bracketStack.pop();
next(true);
return node(false, 'List', line, {elements: elements, rest: rest});
}
function parseAtom() {
var c = current();
var start = offset;
var line = currentLine;
if(c == '(') {
next(true);
bracketStack.push('(');
var term = parseTerm();
if(term == null) term = node(false, 'Unit', line);
c = current();
if(!(c == ')')) throw 'Expected ")" after "(" at line ' + currentLine;
bracketStack.pop();
next(true);
return term;
} else if(c == '_') {
next(true);
return node(false, 'Placeholder', line);
} else {
return parseLower() || parseUpper() || parseNumber() || parseString() || parseList() || parseLambda();
}
}
function parseApply() {
var term = parseAtom();
if(term == null) return null;
var c = current();
var start = offset;
var argument = null;
while(c == '.' || c == '(' || (argument = parseAtom()) != null) {
var line = currentLine;
if(c == '.') {
next(true);
var method = parseUpper();
if(method == null) throw 'Method name expected after "." at line ' + line;
term = node(false, 'Apply', line, {left: term, right: method});
} else if(c == '(') {
next(true);
bracketStack.push('(');
var right = parseTerm();
if(right == null) {
term = node(false, 'Apply', line, {left: term, right: node(false, 'Unit', line)});
} else {
term = node(false, 'Apply', line, {left: term, right: right});
while(current() == ',') {
next(true);
var right = parseTerm();
if(right == null) throw 'Term expected after "," at line ' + line;
term = node(false, 'Apply', line, {left: term, right: right});
}
}
c = current();
if(c != ')') throw '")" expected after "(" at line ' + line;
bracketStack.pop();
next(true);
} else {
if(argument == null) throw 'Argument expected at line ' + line;
term = node(false, 'Apply', line, {left: term, right: argument});
}
c = current();
}
return term;
}
function parseUnaryOperator() {
var c = current();
var start = offset;
var line = currentLine;
while('@!#$%/=?^~*<>+-'.indexOf(c) != -1) {
c = next();
}
var operator = code.substring(start, offset);
skipWhitespace();
var term = parseApply();
if(operator.length == 0) return term;
else if(term == null) throw 'Term expected after "' + operator + '" at line ' + line;
else return node(false, 'Apply', line, {left: term, right: node(false, 'String', line, {value: operator + '_'})});
}
function parseBinaryOperator() {
var left = parseUnaryOperator();
if(left == null) return null;
var c = current();
var start = offset;
var line = currentLine;
while('@!#$%/=?^~*<>+-'.indexOf(c) != -1) {
c = next();
}
var operator = code.substring(start, offset);
skipWhitespace();
if(operator.length == 0) return left;
var right = parseBinaryOperator();
if(right == null) throw 'Term expected after "' + operator + '" at line ' + line;
else return node(false, 'Apply', line, {
left: node(false, 'Apply', line, {left: left, right: node(false, 'String', line, {value: operator})}),
right: right
});
}
function parseAndOr() {
var left = parseBinaryOperator();
if(left == null) return null;
var c = current();
var n = ahead();
var start = offset;
var line = currentLine;
var isAnd = c == '&' && n == '&';
var isOr = c == '|' && n == '|';
if(!(isAnd || isOr)) return left;
next();
next(true);
var right = parseAndOr();
if(right == null) throw 'Term expected after "' + (isAnd ? '&&' : '||') + '" at line ' + line;
else return node(false, isAnd ? 'And' : 'Or', line, {left: left, right: right});
}
var parseTerm = parseAndOr;
function parseInitialization() {
var left = parseTerm();
var c = current();
var start = offset;
var line = currentLine;
if(!(c == ':')) return left;
next();
c = current();
if(!(c == '=')) throw '"=" expected after ":" at line ' + currentLine;
next(true);
if(left.tag != 'Lower') throw 'Lower case identifier expected before ":=" at line ' + line;
var term = parseTerm();
if(term == null) throw 'Value expected after ":' + left.value + '" at line ' + line;
return node(false, 'Initialize', line, {name: left.value, value: term});
}
function parseStatements() {
var statements = [];
skipLineEnd();
var statement = parseInitialization();
if(statement == null) return statements;
skipLineEnd();
while(statement != null) {
statements.push(statement);
statement = parseInitialization();
skipLineEnd();
}
return statements;
}
var programStatements = parseStatements();
if(offset < code.length) throw 'Unexpected character "' + current() + '" at line ' + currentLine;
return programStatements;
}