Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80677 views
1
// Generated by CoffeeScript 1.9.3
2
(function() {
3
var Parser, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap;
4
5
Parser = require('jison').Parser;
6
7
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
8
9
o = function(patternString, action, options) {
10
var addLocationDataFn, match, patternCount;
11
patternString = patternString.replace(/\s{2,}/g, ' ');
12
patternCount = patternString.split(' ').length;
13
if (!action) {
14
return [patternString, '$$ = $1;', options];
15
}
16
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
17
action = action.replace(/\bnew /g, '$&yy.');
18
action = action.replace(/\b(?:Block\.wrap|extend)\b/g, 'yy.$&');
19
addLocationDataFn = function(first, last) {
20
if (!last) {
21
return "yy.addLocationDataFn(@" + first + ")";
22
} else {
23
return "yy.addLocationDataFn(@" + first + ", @" + last + ")";
24
}
25
};
26
action = action.replace(/LOC\(([0-9]*)\)/g, addLocationDataFn('$1'));
27
action = action.replace(/LOC\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2'));
28
return [patternString, "$$ = " + (addLocationDataFn(1, patternCount)) + "(" + action + ");", options];
29
};
30
31
grammar = {
32
Root: [
33
o('', function() {
34
return new Block;
35
}), o('Body')
36
],
37
Body: [
38
o('Line', function() {
39
return Block.wrap([$1]);
40
}), o('Body TERMINATOR Line', function() {
41
return $1.push($3);
42
}), o('Body TERMINATOR')
43
],
44
Line: [o('Expression'), o('Statement')],
45
Statement: [
46
o('Return'), o('Comment'), o('STATEMENT', function() {
47
return new Literal($1);
48
})
49
],
50
Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Class'), o('Throw')],
51
Block: [
52
o('INDENT OUTDENT', function() {
53
return new Block;
54
}), o('INDENT Body OUTDENT', function() {
55
return $2;
56
})
57
],
58
Identifier: [
59
o('IDENTIFIER', function() {
60
return new Literal($1);
61
})
62
],
63
AlphaNumeric: [
64
o('NUMBER', function() {
65
return new Literal($1);
66
}), o('String')
67
],
68
String: [
69
o('STRING', function() {
70
return new Literal($1);
71
}), o('STRING_START Body STRING_END', function() {
72
return new Parens($2);
73
})
74
],
75
Regex: [
76
o('REGEX', function() {
77
return new Literal($1);
78
}), o('REGEX_START Invocation REGEX_END', function() {
79
return $2;
80
})
81
],
82
Literal: [
83
o('AlphaNumeric'), o('JS', function() {
84
return new Literal($1);
85
}), o('Regex'), o('DEBUGGER', function() {
86
return new Literal($1);
87
}), o('UNDEFINED', function() {
88
return new Undefined;
89
}), o('NULL', function() {
90
return new Null;
91
}), o('BOOL', function() {
92
return new Bool($1);
93
})
94
],
95
Assign: [
96
o('Assignable = Expression', function() {
97
return new Assign($1, $3);
98
}), o('Assignable = TERMINATOR Expression', function() {
99
return new Assign($1, $4);
100
}), o('Assignable = INDENT Expression OUTDENT', function() {
101
return new Assign($1, $4);
102
})
103
],
104
AssignObj: [
105
o('ObjAssignable', function() {
106
return new Value($1);
107
}), o('ObjAssignable : Expression', function() {
108
return new Assign(LOC(1)(new Value($1)), $3, 'object');
109
}), o('ObjAssignable : INDENT Expression OUTDENT', function() {
110
return new Assign(LOC(1)(new Value($1)), $4, 'object');
111
}), o('Comment')
112
],
113
ObjAssignable: [o('Identifier'), o('AlphaNumeric'), o('ThisProperty')],
114
Return: [
115
o('RETURN Expression', function() {
116
return new Return($2);
117
}), o('RETURN', function() {
118
return new Return;
119
})
120
],
121
Comment: [
122
o('HERECOMMENT', function() {
123
return new Comment($1);
124
})
125
],
126
Code: [
127
o('PARAM_START ParamList PARAM_END FuncGlyph Block', function() {
128
return new Code($2, $5, $4);
129
}), o('FuncGlyph Block', function() {
130
return new Code([], $2, $1);
131
})
132
],
133
FuncGlyph: [
134
o('->', function() {
135
return 'func';
136
}), o('=>', function() {
137
return 'boundfunc';
138
})
139
],
140
OptComma: [o(''), o(',')],
141
ParamList: [
142
o('', function() {
143
return [];
144
}), o('Param', function() {
145
return [$1];
146
}), o('ParamList , Param', function() {
147
return $1.concat($3);
148
}), o('ParamList OptComma TERMINATOR Param', function() {
149
return $1.concat($4);
150
}), o('ParamList OptComma INDENT ParamList OptComma OUTDENT', function() {
151
return $1.concat($4);
152
})
153
],
154
Param: [
155
o('ParamVar', function() {
156
return new Param($1);
157
}), o('ParamVar ...', function() {
158
return new Param($1, null, true);
159
}), o('ParamVar = Expression', function() {
160
return new Param($1, $3);
161
}), o('...', function() {
162
return new Expansion;
163
})
164
],
165
ParamVar: [o('Identifier'), o('ThisProperty'), o('Array'), o('Object')],
166
Splat: [
167
o('Expression ...', function() {
168
return new Splat($1);
169
})
170
],
171
SimpleAssignable: [
172
o('Identifier', function() {
173
return new Value($1);
174
}), o('Value Accessor', function() {
175
return $1.add($2);
176
}), o('Invocation Accessor', function() {
177
return new Value($1, [].concat($2));
178
}), o('ThisProperty')
179
],
180
Assignable: [
181
o('SimpleAssignable'), o('Array', function() {
182
return new Value($1);
183
}), o('Object', function() {
184
return new Value($1);
185
})
186
],
187
Value: [
188
o('Assignable'), o('Literal', function() {
189
return new Value($1);
190
}), o('Parenthetical', function() {
191
return new Value($1);
192
}), o('Range', function() {
193
return new Value($1);
194
}), o('This')
195
],
196
Accessor: [
197
o('. Identifier', function() {
198
return new Access($2);
199
}), o('?. Identifier', function() {
200
return new Access($2, 'soak');
201
}), o(':: Identifier', function() {
202
return [LOC(1)(new Access(new Literal('prototype'))), LOC(2)(new Access($2))];
203
}), o('?:: Identifier', function() {
204
return [LOC(1)(new Access(new Literal('prototype'), 'soak')), LOC(2)(new Access($2))];
205
}), o('::', function() {
206
return new Access(new Literal('prototype'));
207
}), o('Index')
208
],
209
Index: [
210
o('INDEX_START IndexValue INDEX_END', function() {
211
return $2;
212
}), o('INDEX_SOAK Index', function() {
213
return extend($2, {
214
soak: true
215
});
216
})
217
],
218
IndexValue: [
219
o('Expression', function() {
220
return new Index($1);
221
}), o('Slice', function() {
222
return new Slice($1);
223
})
224
],
225
Object: [
226
o('{ AssignList OptComma }', function() {
227
return new Obj($2, $1.generated);
228
})
229
],
230
AssignList: [
231
o('', function() {
232
return [];
233
}), o('AssignObj', function() {
234
return [$1];
235
}), o('AssignList , AssignObj', function() {
236
return $1.concat($3);
237
}), o('AssignList OptComma TERMINATOR AssignObj', function() {
238
return $1.concat($4);
239
}), o('AssignList OptComma INDENT AssignList OptComma OUTDENT', function() {
240
return $1.concat($4);
241
})
242
],
243
Class: [
244
o('CLASS', function() {
245
return new Class;
246
}), o('CLASS Block', function() {
247
return new Class(null, null, $2);
248
}), o('CLASS EXTENDS Expression', function() {
249
return new Class(null, $3);
250
}), o('CLASS EXTENDS Expression Block', function() {
251
return new Class(null, $3, $4);
252
}), o('CLASS SimpleAssignable', function() {
253
return new Class($2);
254
}), o('CLASS SimpleAssignable Block', function() {
255
return new Class($2, null, $3);
256
}), o('CLASS SimpleAssignable EXTENDS Expression', function() {
257
return new Class($2, $4);
258
}), o('CLASS SimpleAssignable EXTENDS Expression Block', function() {
259
return new Class($2, $4, $5);
260
})
261
],
262
Invocation: [
263
o('Value OptFuncExist Arguments', function() {
264
return new Call($1, $3, $2);
265
}), o('Invocation OptFuncExist Arguments', function() {
266
return new Call($1, $3, $2);
267
}), o('SUPER', function() {
268
return new Call('super', [new Splat(new Literal('arguments'))]);
269
}), o('SUPER Arguments', function() {
270
return new Call('super', $2);
271
})
272
],
273
OptFuncExist: [
274
o('', function() {
275
return false;
276
}), o('FUNC_EXIST', function() {
277
return true;
278
})
279
],
280
Arguments: [
281
o('CALL_START CALL_END', function() {
282
return [];
283
}), o('CALL_START ArgList OptComma CALL_END', function() {
284
return $2;
285
})
286
],
287
This: [
288
o('THIS', function() {
289
return new Value(new Literal('this'));
290
}), o('@', function() {
291
return new Value(new Literal('this'));
292
})
293
],
294
ThisProperty: [
295
o('@ Identifier', function() {
296
return new Value(LOC(1)(new Literal('this')), [LOC(2)(new Access($2))], 'this');
297
})
298
],
299
Array: [
300
o('[ ]', function() {
301
return new Arr([]);
302
}), o('[ ArgList OptComma ]', function() {
303
return new Arr($2);
304
})
305
],
306
RangeDots: [
307
o('..', function() {
308
return 'inclusive';
309
}), o('...', function() {
310
return 'exclusive';
311
})
312
],
313
Range: [
314
o('[ Expression RangeDots Expression ]', function() {
315
return new Range($2, $4, $3);
316
})
317
],
318
Slice: [
319
o('Expression RangeDots Expression', function() {
320
return new Range($1, $3, $2);
321
}), o('Expression RangeDots', function() {
322
return new Range($1, null, $2);
323
}), o('RangeDots Expression', function() {
324
return new Range(null, $2, $1);
325
}), o('RangeDots', function() {
326
return new Range(null, null, $1);
327
})
328
],
329
ArgList: [
330
o('Arg', function() {
331
return [$1];
332
}), o('ArgList , Arg', function() {
333
return $1.concat($3);
334
}), o('ArgList OptComma TERMINATOR Arg', function() {
335
return $1.concat($4);
336
}), o('INDENT ArgList OptComma OUTDENT', function() {
337
return $2;
338
}), o('ArgList OptComma INDENT ArgList OptComma OUTDENT', function() {
339
return $1.concat($4);
340
})
341
],
342
Arg: [
343
o('Expression'), o('Splat'), o('...', function() {
344
return new Expansion;
345
})
346
],
347
SimpleArgs: [
348
o('Expression'), o('SimpleArgs , Expression', function() {
349
return [].concat($1, $3);
350
})
351
],
352
Try: [
353
o('TRY Block', function() {
354
return new Try($2);
355
}), o('TRY Block Catch', function() {
356
return new Try($2, $3[0], $3[1]);
357
}), o('TRY Block FINALLY Block', function() {
358
return new Try($2, null, null, $4);
359
}), o('TRY Block Catch FINALLY Block', function() {
360
return new Try($2, $3[0], $3[1], $5);
361
})
362
],
363
Catch: [
364
o('CATCH Identifier Block', function() {
365
return [$2, $3];
366
}), o('CATCH Object Block', function() {
367
return [LOC(2)(new Value($2)), $3];
368
}), o('CATCH Block', function() {
369
return [null, $2];
370
})
371
],
372
Throw: [
373
o('THROW Expression', function() {
374
return new Throw($2);
375
})
376
],
377
Parenthetical: [
378
o('( Body )', function() {
379
return new Parens($2);
380
}), o('( INDENT Body OUTDENT )', function() {
381
return new Parens($3);
382
})
383
],
384
WhileSource: [
385
o('WHILE Expression', function() {
386
return new While($2);
387
}), o('WHILE Expression WHEN Expression', function() {
388
return new While($2, {
389
guard: $4
390
});
391
}), o('UNTIL Expression', function() {
392
return new While($2, {
393
invert: true
394
});
395
}), o('UNTIL Expression WHEN Expression', function() {
396
return new While($2, {
397
invert: true,
398
guard: $4
399
});
400
})
401
],
402
While: [
403
o('WhileSource Block', function() {
404
return $1.addBody($2);
405
}), o('Statement WhileSource', function() {
406
return $2.addBody(LOC(1)(Block.wrap([$1])));
407
}), o('Expression WhileSource', function() {
408
return $2.addBody(LOC(1)(Block.wrap([$1])));
409
}), o('Loop', function() {
410
return $1;
411
})
412
],
413
Loop: [
414
o('LOOP Block', function() {
415
return new While(LOC(1)(new Literal('true'))).addBody($2);
416
}), o('LOOP Expression', function() {
417
return new While(LOC(1)(new Literal('true'))).addBody(LOC(2)(Block.wrap([$2])));
418
})
419
],
420
For: [
421
o('Statement ForBody', function() {
422
return new For($1, $2);
423
}), o('Expression ForBody', function() {
424
return new For($1, $2);
425
}), o('ForBody Block', function() {
426
return new For($2, $1);
427
})
428
],
429
ForBody: [
430
o('FOR Range', function() {
431
return {
432
source: LOC(2)(new Value($2))
433
};
434
}), o('FOR Range BY Expression', function() {
435
return {
436
source: LOC(2)(new Value($2)),
437
step: $4
438
};
439
}), o('ForStart ForSource', function() {
440
$2.own = $1.own;
441
$2.name = $1[0];
442
$2.index = $1[1];
443
return $2;
444
})
445
],
446
ForStart: [
447
o('FOR ForVariables', function() {
448
return $2;
449
}), o('FOR OWN ForVariables', function() {
450
$3.own = true;
451
return $3;
452
})
453
],
454
ForValue: [
455
o('Identifier'), o('ThisProperty'), o('Array', function() {
456
return new Value($1);
457
}), o('Object', function() {
458
return new Value($1);
459
})
460
],
461
ForVariables: [
462
o('ForValue', function() {
463
return [$1];
464
}), o('ForValue , ForValue', function() {
465
return [$1, $3];
466
})
467
],
468
ForSource: [
469
o('FORIN Expression', function() {
470
return {
471
source: $2
472
};
473
}), o('FOROF Expression', function() {
474
return {
475
source: $2,
476
object: true
477
};
478
}), o('FORIN Expression WHEN Expression', function() {
479
return {
480
source: $2,
481
guard: $4
482
};
483
}), o('FOROF Expression WHEN Expression', function() {
484
return {
485
source: $2,
486
guard: $4,
487
object: true
488
};
489
}), o('FORIN Expression BY Expression', function() {
490
return {
491
source: $2,
492
step: $4
493
};
494
}), o('FORIN Expression WHEN Expression BY Expression', function() {
495
return {
496
source: $2,
497
guard: $4,
498
step: $6
499
};
500
}), o('FORIN Expression BY Expression WHEN Expression', function() {
501
return {
502
source: $2,
503
step: $4,
504
guard: $6
505
};
506
})
507
],
508
Switch: [
509
o('SWITCH Expression INDENT Whens OUTDENT', function() {
510
return new Switch($2, $4);
511
}), o('SWITCH Expression INDENT Whens ELSE Block OUTDENT', function() {
512
return new Switch($2, $4, $6);
513
}), o('SWITCH INDENT Whens OUTDENT', function() {
514
return new Switch(null, $3);
515
}), o('SWITCH INDENT Whens ELSE Block OUTDENT', function() {
516
return new Switch(null, $3, $5);
517
})
518
],
519
Whens: [
520
o('When'), o('Whens When', function() {
521
return $1.concat($2);
522
})
523
],
524
When: [
525
o('LEADING_WHEN SimpleArgs Block', function() {
526
return [[$2, $3]];
527
}), o('LEADING_WHEN SimpleArgs Block TERMINATOR', function() {
528
return [[$2, $3]];
529
})
530
],
531
IfBlock: [
532
o('IF Expression Block', function() {
533
return new If($2, $3, {
534
type: $1
535
});
536
}), o('IfBlock ELSE IF Expression Block', function() {
537
return $1.addElse(LOC(3, 5)(new If($4, $5, {
538
type: $3
539
})));
540
})
541
],
542
If: [
543
o('IfBlock'), o('IfBlock ELSE Block', function() {
544
return $1.addElse($3);
545
}), o('Statement POST_IF Expression', function() {
546
return new If($3, LOC(1)(Block.wrap([$1])), {
547
type: $2,
548
statement: true
549
});
550
}), o('Expression POST_IF Expression', function() {
551
return new If($3, LOC(1)(Block.wrap([$1])), {
552
type: $2,
553
statement: true
554
});
555
})
556
],
557
Operation: [
558
o('UNARY Expression', function() {
559
return new Op($1, $2);
560
}), o('UNARY_MATH Expression', function() {
561
return new Op($1, $2);
562
}), o('- Expression', (function() {
563
return new Op('-', $2);
564
}), {
565
prec: 'UNARY_MATH'
566
}), o('+ Expression', (function() {
567
return new Op('+', $2);
568
}), {
569
prec: 'UNARY_MATH'
570
}), o('YIELD Statement', function() {
571
return new Op($1, $2);
572
}), o('YIELD Expression', function() {
573
return new Op($1, $2);
574
}), o('YIELD FROM Expression', function() {
575
return new Op($1.concat($2), $3);
576
}), o('-- SimpleAssignable', function() {
577
return new Op('--', $2);
578
}), o('++ SimpleAssignable', function() {
579
return new Op('++', $2);
580
}), o('SimpleAssignable --', function() {
581
return new Op('--', $1, null, true);
582
}), o('SimpleAssignable ++', function() {
583
return new Op('++', $1, null, true);
584
}), o('Expression ?', function() {
585
return new Existence($1);
586
}), o('Expression + Expression', function() {
587
return new Op('+', $1, $3);
588
}), o('Expression - Expression', function() {
589
return new Op('-', $1, $3);
590
}), o('Expression MATH Expression', function() {
591
return new Op($2, $1, $3);
592
}), o('Expression ** Expression', function() {
593
return new Op($2, $1, $3);
594
}), o('Expression SHIFT Expression', function() {
595
return new Op($2, $1, $3);
596
}), o('Expression COMPARE Expression', function() {
597
return new Op($2, $1, $3);
598
}), o('Expression LOGIC Expression', function() {
599
return new Op($2, $1, $3);
600
}), o('Expression RELATION Expression', function() {
601
if ($2.charAt(0) === '!') {
602
return new Op($2.slice(1), $1, $3).invert();
603
} else {
604
return new Op($2, $1, $3);
605
}
606
}), o('SimpleAssignable COMPOUND_ASSIGN Expression', function() {
607
return new Assign($1, $3, $2);
608
}), o('SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT', function() {
609
return new Assign($1, $4, $2);
610
}), o('SimpleAssignable COMPOUND_ASSIGN TERMINATOR Expression', function() {
611
return new Assign($1, $4, $2);
612
}), o('SimpleAssignable EXTENDS Expression', function() {
613
return new Extends($1, $3);
614
})
615
]
616
};
617
618
operators = [['left', '.', '?.', '::', '?::'], ['left', 'CALL_START', 'CALL_END'], ['nonassoc', '++', '--'], ['left', '?'], ['right', 'UNARY'], ['right', '**'], ['right', 'UNARY_MATH'], ['left', 'MATH'], ['left', '+', '-'], ['left', 'SHIFT'], ['left', 'RELATION'], ['left', 'COMPARE'], ['left', 'LOGIC'], ['nonassoc', 'INDENT', 'OUTDENT'], ['right', 'YIELD'], ['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'THROW', 'EXTENDS'], ['right', 'FORIN', 'FOROF', 'BY', 'WHEN'], ['right', 'IF', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS'], ['left', 'POST_IF']];
619
620
tokens = [];
621
622
for (name in grammar) {
623
alternatives = grammar[name];
624
grammar[name] = (function() {
625
var i, j, len, len1, ref, results;
626
results = [];
627
for (i = 0, len = alternatives.length; i < len; i++) {
628
alt = alternatives[i];
629
ref = alt[0].split(' ');
630
for (j = 0, len1 = ref.length; j < len1; j++) {
631
token = ref[j];
632
if (!grammar[token]) {
633
tokens.push(token);
634
}
635
}
636
if (name === 'Root') {
637
alt[1] = "return " + alt[1];
638
}
639
results.push(alt);
640
}
641
return results;
642
})();
643
}
644
645
exports.parser = new Parser({
646
tokens: tokens.join(' '),
647
bnf: grammar,
648
operators: operators.reverse(),
649
startSymbol: 'Root'
650
});
651
652
}).call(this);
653
654