-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
637 lines (543 loc) · 14.4 KB
/
parser.js
File metadata and controls
637 lines (543 loc) · 14.4 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
var printObj = require('./utils/print-object');
var utils = require("./utils/utils");
var bareObject = utils.bareObject;
var generateVarName = utils.generateVarName;
module.exports = function (tokens) {
var parsed = parseBlock(tokens[0], parseExpression);
return parsed.exp;
};
function parseBlock(token, elementParser) {
if (token.isIndent) {
var parseResult = parseSequence({
of: elementParser,
stopWhen: indentLessThan(token.width)
})(token);
return {
exp: parseResult.exp,
token: parseResult.token.previous()
};
}
else token.error("Block must start on a new line");
}
function indentLessThan(blockIndent) {
return function (token) {
// checks if next token is also an indent,
// so empty lines can be skipped
return token.is("End of File") ||
(
token.isIndent &&
token.width < blockIndent &&
!token.next().isIndent
);
};
}
// Parse repeatedly using some parse function
// until the isEnd predicate returns true
function parseSequence(options) {
var parseNext = options.of;
return function (token, altIsEnd) {
var isEnd = altIsEnd || options.stopWhen;
var parseResult;
var expressions = [];
token = advanceToken(token);
while (!isEnd(token)) {
parseResult = parseNext(token);
expressions.push(parseResult.exp);
token = advanceToNextExpression(parseResult.token, isEnd);
}
return {
exp: expressions,
token: token.next()
};
};
}
function parseExpression(token, precedence) {
precedence = precedence || 0;
var parseResult;
var exp = null;
// Parse initial atom of the expression:
// try to find specific parse function for token, else
// find generic parse function for the token type
var parser = prefixOperators[token.string] || tokenTypeParsers[token.type];
if (!parser) {
throw SyntaxError("Unknown token type " + token);
}
// Parse expression fragments:
do {
parseResult = parser(token, precedence, exp);
exp = parseResult.exp;
// get the infix parse function for the next token
token = advanceToken(parseResult.token);
parser = infixOperators[token.string];
// Check that the operator has a precedence
if (parser && parser.precedence === undefined) {
throw Error(
"Undefined precedence for infix operator: " + token.string);
}
// break to stop function calls being split over line breaks
if (isBracketAtStartOfLine(token)) break;
// if the token is an operator with higher precedence than
// the current master operator, loop to add the partial expression
} while (parser && parser.precedence > precedence);
return parseResult;
}
function parseLiteral(token, precedence) {
return {
exp: {
type: "Literal",
value: token.value
},
token: token.next()
};
}
function parseNumber(token, precedence) {
// lookahead to detect decimals
if (token.next().is(".") && token.move(2).isNumber) {
var value = Number(token.value + "." + token.move(2).string);
if (String(value) === "NaN") {
token.error("Invalid number");
}
return {
exp: {
type: "Literal",
value: value
},
token: token.move(3)
};
}
else {
return parseLiteral(token, precedence);
}
}
function ignoreToken(token, precedence) {
return parseExpression(token.next(), precedence);
}
function parseIdentifier(token) {
return {exp: makeIdentifier(token), token: token.next()};
}
// Should never be reached by a valid program
function parsePunctuation(token) {
throw token.error("Unexpected character: " + token.string);
}
function unaryOp(unaryOpPrecedence) {
function parseUnary (token, precedence) {
var operandResult = parseExpression(token.next(), unaryOpPrecedence);
return {
exp: [makeIdentifier(token), operandResult.exp],
token: operandResult.token
};
}
return parseUnary;
}
function binaryOp(binaryOpPrecedence, rightAssociative) {
function parseBinary(token, precedence, leftExp) {
var rightResult = parseExpression(
token.next(),
binaryOpPrecedence - (rightAssociative ? 0.01 : 0)
);
return {
exp: [makeIdentifier(token), leftExp, rightResult.exp],
token: rightResult.token
};
}
// set the precedence to a property of the function, so
// parseExpression() can see it to know whether to include the op
parseBinary.precedence = binaryOpPrecedence;
return parseBinary;
}
var matchToken = bareObject({
"(": ")",
"[": "]",
"{": "}"
});
function parseCall(token, precedence, callee) {
return withPrefix([callee], parseArguments(token.next()));
}
var parseArguments = parseSequence({
of: parseExpression,
stopWhen: onToken(")")
});
function parseCallWithObject(token, precedence, callee) {
var objResult = parseObjLiteral(token);
return {
exp: [callee, objResult.exp],
token: objResult.token
};
}
function parseMemberAccess(token, precedence, parent) {
if (token.next().type !== "Identifier") {
token.error("the property of an object must be an identifier");
}
var property = makeIdentifier(token.next());
return {
exp: [makeIdentifier("."), parent, property],
token: token.move(2)
};
}
function parseDataStructureGetter(token, precedence, dataStruct) {
var memberRes = parseExpression(token.next());
checkToken(memberRes.token, "]");
return {
exp: [
[makeIdentifier("."), dataStruct, makeIdentifier("get")],
memberRes.exp
],
token: memberRes.token.next()
};
}
function parseGrouped(token) {
var grouped = parseArguments(token.next());
if (grouped.exp.length === 1) return {
exp: grouped.exp[0],
token: grouped.token
};
else return withPrefix([":seq"], grouped);
}
function parseLambda(token, precedence, paramExp) {
var params = isCallTo(":seq", paramExp) ?
paramExp.slice(1) :
[paramExp];
params.forEach(function (param, i) {
if (!isValidParam(param)) {
token.error("Parameter number " + i +
" is invalid in a lambda function");
}
});
var body = parseBody(token.next(), "colon optional");
return {
exp: [makeIdentifier("fn"), params, body.exp],
token: body.token
};
}
function parseFn(token) {
return withPrefix(["fn"], (
fnAnon(token.next()) ||
fnNamed(token.next()) ||
fnMethod(token.next()) ||
token.error("Invalid method")
));
}
function fnAnon(token) {
if (token.isNot("(")) return false;
var params = parseParams(token.next());
var body = parseBody(params.token, "colon optional");
return {
exp: [params.exp, body.exp],
token: body.token
};
}
function fnNamed(token) {
if (!token.isIdentifier)
token.error("Expected function name, but found " + token.string);
return withPrefix([makeIdentifier(token)], fnAnon(token.next()));
}
function fnMethod(token) {
var methodNameToken = token.move(2);
if (!token.isIdentifier &&
!checkToken(token.next(), ".") &&
!methodNameToken.isIdentifier)
return false;
var selfName = makeIdentifier(token);
var methodName = makeIdentifier(methodNameToken);
return withPrefix([selfName, methodName], fnAnon(methodNameToken.next()));
}
var parseParams = parseSequence({
of: parseParam,
stopWhen: onToken(")")
});
function parseParam(token) {
var param = parseExpression(token);
return isValidParam(param.exp) ?
param : token.error("invalid parameter name in function");
}
function isValidParam(exp) {
return exp.isIdentifier ||
// check for default parameter assignment
(isCallTo("=", exp) && isValidParam(exp[1])) ||
// check for rest parameters
(isCallTo("...", exp) && exp[1].isIdentifier);
}
function parseIf(token) {
var test = parseExpression(token.next());
var ifBody = parseBody(test.token);
var elseBody = parseElse(ifBody.token);
//elseBody.exp will be an empty array if there is no else part
return {
exp: [makeIdentifier("if"), test.exp, ifBody.exp].concat(elseBody.exp),
token: elseBody.token
};
}
function parseElse(token) {
advToken = advanceToken(token);
if (advToken.is("else")) {
var elseBody = parseBody(advToken.next(), "colon optional");
return {
exp: [elseBody.exp],
token: elseBody.token
};
}
else return {exp: [], token: token};
}
function parseBody(token, colonOptional) {
while (token.isComment) {
token = token.next();
}
if (colonOptional) {
if (token.is(":")) {
token = token.next();
}
}
else {
checkToken(token, ":");
token = token.next();
}
if (token.isIndent) {
return parseBlock(token, parseExpression);
}
else {
var expResult = parseExpression(token);
return {exp: [expResult.exp], token: expResult.token};
}
}
function parseType(token) {
var behaviour;
token = token.next();
if (!token.isIdentifier) token.error(
"Expected a type name, but found " + token.string);
var typeName = parseIdentifier(token).exp;
checkToken(token.next(), "(");
var params = parseParams(token.move(2));
// optional behaviour block
token = params.token;
if (token.is(":")) {
behaviour = parseBlock(token.next(), parseObjProperty);
token = behaviour.token;
}
return {
exp: [makeIdentifier("type"), typeName, params.exp]
.concat(behaviour ? [behaviour.exp] : []),
token: token
};
}
function parseDoBlock(token) {
var blockBody = parseBody(token.next());
return {
exp: [makeIdentifier("do"), blockBody.exp],
token: blockBody.token
};
}
function parseWithBlock(token) {
var controller = parseExpression(token.next());
var blockBody = parseBody(controller.token);
return {
exp: [makeIdentifier("with"), controller.exp, blockBody.exp],
token: blockBody.token
};
}
function parseSetExpression(token) {
var precedence = 8;
var assignResult = parseExpression(token.next(), precedence);
if (isCallTo("=", assignResult.exp)) {
assignResult.exp[0] = makeIdentifier(token);
return assignResult;
}
else token.error("invalid variable reassignment");
}
function parseObjLiteral(token, precedence) {
return withPrefix([":object"], parseObjLiteralBody(token.next()));
}
var parseObjLiteralBody = parseSequence({
of: parseObjProperty,
stopWhen: onToken("}")
});
function parseObjProperty(token) {
var key, value;
if (token.is("fn")) {
return parseMethod(token);
}
else if (token.isIdentifier) {
key = parseIdentifier(token);
}
else if (token.isString || token.isNumber) {
key = parseLiteral(token);
}
else token.error("Invalid object property key");
checkToken(key.token, ":");
value = parseExpression(key.token.next());
return objProperty(key.exp, value);
}
function objProperty(keyExp, valueRes) {
return {
exp: [makeIdentifier(":"), keyExp, valueRes.exp],
token: valueRes.token
};
}
function parseMethod(token) {
var method = fnNamed(token.next());
if (method) {
methodName = method.exp[0];
}
else {
method = fnMethod(token.next());
if (method) {
methodName = method.exp[1];
method.exp[1] = null;
}
else token.error("Invalid method declaration");
}
return objProperty(methodName, withPrefix(["fn"], method));
}
// Not yet working
function curriedOperator(operator) {
return function (token) {
var arg = makeIdentifier(generateVarName());
var body = infixOperators[operator](token, 0, arg);
return {
exp: [makeIdentifier("fn"), [arg], [body.exp]],
token: body.token,
}
}
}
var prefixOperators = bareObject({
"...": unaryOp(5),
"-": unaryOp(60),
"not": unaryOp(23),
"throw": unaryOp(14),
"@": unaryOp(65),
"(": parseGrouped,
"[": function (token) {
return withPrefix(["Vector"],
parseArguments(token.next(), onToken("]")));
},
"{": parseObjLiteral,
"fn": parseFn,
"if": parseIf,
"type": parseType,
"do": parseDoBlock,
"with": parseWithBlock,
"set!": parseSetExpression,
});
var infixOperators = bareObject({
".": withPrecedence(80, parseMemberAccess),
"[": withPrecedence(75, parseDataStructureGetter),
"(": withPrecedence(70, parseCall),
"{": withPrecedence(70, parseCallWithObject),
"^": binaryOp(52),
"*": binaryOp(50),
"/": binaryOp(50),
"%": binaryOp(50),
"+": binaryOp(40),
"-": binaryOp(40),
"++": binaryOp(55),
"<": binaryOp(30),
">": binaryOp(30),
"<=": binaryOp(30),
">=": binaryOp(30),
"::": binaryOp(30),
"===": binaryOp(25),
"==": binaryOp(25),
"!=": binaryOp(25),
"and": binaryOp(20),
"&": binaryOp(20),
"or": binaryOp(15),
"|": binaryOp(15),
"=>": binaryOp(12),
"->": withPrecedence(11, parseLambda),
"=": binaryOp(10, "right associative"),
"<-": binaryOp(10, "right associative"),
});
var tokenTypeParsers = bareObject({
"Number": parseNumber,
"String": parseLiteral,
"Regex": parseLiteral,
"Boolean": parseLiteral,
"Indent": ignoreToken,
"Comment": ignoreToken,
"Identifier": parseIdentifier,
"Punctuation": parsePunctuation,
});
// --------------------HELPER FUNCTIONS-----------------------
function lispString(ast) {
return (ast instanceof Array) ?
"(" + ast.map(lispString).join(" ") + ")"
: (typeof(ast.value) === "string") ?
"'" + ast.value + "'"
: ast.value || ast.name;
}
function isBracketAtStartOfLine(token) {
return token.string in matchToken && isSkippable(token.previous());
}
function makeIdentifier(name) {
return {
type: "Identifier",
name: (typeof(name) === "object") ? name.string : name,
isIdentifier: true,
};
}
function isCallTo(identifier, node) {
return node instanceof Array && node[0].name === identifier;
}
function onToken(stopToken) {
var f = function (token) {
return token.is("End of File") || token.is(stopToken);
};
f.toString = function () {
return "on token: " + stopToken;
};
return f;
}
function advanceToken(token) {
while (isSkippable(token)) {
token = token.next();
}
return token;
}
function advanceToNextExpression(token, isEnd) {
var foundDivider = false;
while (isSkippable(token) && !isEnd(token)) {
token = token.next();
foundDivider = true;
}
// throw an error if two expressions are
// on the same line without a comma
if (!(foundDivider || isEnd(token))) {
token.error("Unexpected start of expression");
}
return token;
}
function isSkippable(token) {
return token.is(",") ||
token.is(";") ||
token.isIndent ||
token.isComment;
}
function checkToken(token, expectedToken) {
if (token.isNot(expectedToken)) {
token.error("Expected \"" + expectedToken +
"\", but found \"" + token.string + "\"");
}
return true;
}
function withPrecedence(precedence, func) {
func.precedence = precedence;
return func;
}
function prependCallee(callee, parseResult) {
if (typeof(callee) === "string") callee = makeIdentifier(callee);
return {
exp: [callee].concat(parseResult.exp),
token: parseResult.token
};
}
function withPrefix(prefix, parseResult) {
// propagate failure
if (!parseResult) return false;
return {
exp: prefix.map(wrapIdentifier).concat(parseResult.exp),
token: parseResult.token
};
}
function wrapIdentifier(identifier) {
return typeof(identifier) === "string" ?
makeIdentifier(identifier) : identifier;
}