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