UNPKG

19.3 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.6.0
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 })
354 ],
355 Throw: [
356 o('THROW Expression', function() {
357 return new Throw($2);
358 })
359 ],
360 Parenthetical: [
361 o('( Body )', function() {
362 return new Parens($2);
363 }), o('( INDENT Body OUTDENT )', function() {
364 return new Parens($3);
365 })
366 ],
367 WhileSource: [
368 o('WHILE Expression', function() {
369 return new While($2);
370 }), o('WHILE Expression WHEN Expression', function() {
371 return new While($2, {
372 guard: $4
373 });
374 }), o('UNTIL Expression', function() {
375 return new While($2, {
376 invert: true
377 });
378 }), o('UNTIL Expression WHEN Expression', function() {
379 return new While($2, {
380 invert: true,
381 guard: $4
382 });
383 })
384 ],
385 While: [
386 o('WhileSource Block', function() {
387 return $1.addBody($2);
388 }), o('Statement WhileSource', function() {
389 return $2.addBody(LOC(1)(Block.wrap([$1])));
390 }), o('Expression WhileSource', function() {
391 return $2.addBody(LOC(1)(Block.wrap([$1])));
392 }), o('Loop', function() {
393 return $1;
394 })
395 ],
396 Loop: [
397 o('LOOP Block', function() {
398 return new While(LOC(1)(new Literal('true'))).addBody($2);
399 }), o('LOOP Expression', function() {
400 return new While(LOC(1)(new Literal('true'))).addBody(LOC(2)(Block.wrap([$2])));
401 })
402 ],
403 For: [
404 o('Statement ForBody', function() {
405 return new For($1, $2);
406 }), o('Expression ForBody', function() {
407 return new For($1, $2);
408 }), o('ForBody Block', function() {
409 return new For($2, $1);
410 })
411 ],
412 ForBody: [
413 o('FOR Range', function() {
414 return {
415 source: LOC(2)(new Value($2))
416 };
417 }), o('ForStart ForSource', function() {
418 $2.own = $1.own;
419 $2.name = $1[0];
420 $2.index = $1[1];
421 return $2;
422 })
423 ],
424 ForStart: [
425 o('FOR ForVariables', function() {
426 return $2;
427 }), o('FOR OWN ForVariables', function() {
428 $3.own = true;
429 return $3;
430 })
431 ],
432 ForValue: [
433 o('Identifier'), o('ThisProperty'), o('Array', function() {
434 return new Value($1);
435 }), o('Object', function() {
436 return new Value($1);
437 })
438 ],
439 ForVariables: [
440 o('ForValue', function() {
441 return [$1];
442 }), o('ForValue , ForValue', function() {
443 return [$1, $3];
444 })
445 ],
446 ForSource: [
447 o('FORIN Expression', function() {
448 return {
449 source: $2
450 };
451 }), o('FOROF Expression', function() {
452 return {
453 source: $2,
454 object: true
455 };
456 }), o('FORIN Expression WHEN Expression', function() {
457 return {
458 source: $2,
459 guard: $4
460 };
461 }), o('FOROF Expression WHEN Expression', function() {
462 return {
463 source: $2,
464 guard: $4,
465 object: true
466 };
467 }), o('FORIN Expression BY Expression', function() {
468 return {
469 source: $2,
470 step: $4
471 };
472 }), o('FORIN Expression WHEN Expression BY Expression', function() {
473 return {
474 source: $2,
475 guard: $4,
476 step: $6
477 };
478 }), o('FORIN Expression BY Expression WHEN Expression', function() {
479 return {
480 source: $2,
481 step: $4,
482 guard: $6
483 };
484 })
485 ],
486 Switch: [
487 o('SWITCH Expression INDENT Whens OUTDENT', function() {
488 return new Switch($2, $4);
489 }), o('SWITCH Expression INDENT Whens ELSE Block OUTDENT', function() {
490 return new Switch($2, $4, $6);
491 }), o('SWITCH INDENT Whens OUTDENT', function() {
492 return new Switch(null, $3);
493 }), o('SWITCH INDENT Whens ELSE Block OUTDENT', function() {
494 return new Switch(null, $3, $5);
495 })
496 ],
497 Whens: [
498 o('When'), o('Whens When', function() {
499 return $1.concat($2);
500 })
501 ],
502 When: [
503 o('LEADING_WHEN SimpleArgs Block', function() {
504 return [[$2, $3]];
505 }), o('LEADING_WHEN SimpleArgs Block TERMINATOR', function() {
506 return [[$2, $3]];
507 })
508 ],
509 IfBlock: [
510 o('IF Expression Block', function() {
511 return new If($2, $3, {
512 type: $1
513 });
514 }), o('IfBlock ELSE IF Expression Block', function() {
515 return $1.addElse(new If($4, $5, {
516 type: $3
517 }));
518 })
519 ],
520 If: [
521 o('IfBlock'), o('IfBlock ELSE Block', function() {
522 return $1.addElse($3);
523 }), o('Statement POST_IF Expression', function() {
524 return new If($3, LOC(1)(Block.wrap([$1])), {
525 type: $2,
526 statement: true
527 });
528 }), o('Expression POST_IF Expression', function() {
529 return new If($3, LOC(1)(Block.wrap([$1])), {
530 type: $2,
531 statement: true
532 });
533 })
534 ],
535 Operation: [
536 o('UNARY Expression', function() {
537 return new Op($1, $2);
538 }), o('- Expression', (function() {
539 return new Op('-', $2);
540 }), {
541 prec: 'UNARY'
542 }), o('+ Expression', (function() {
543 return new Op('+', $2);
544 }), {
545 prec: 'UNARY'
546 }), o('-- SimpleAssignable', function() {
547 return new Op('--', $2);
548 }), o('++ SimpleAssignable', function() {
549 return new Op('++', $2);
550 }), o('SimpleAssignable --', function() {
551 return new Op('--', $1, null, true);
552 }), o('SimpleAssignable ++', function() {
553 return new Op('++', $1, null, true);
554 }), o('Expression ?', function() {
555 return new Existence($1);
556 }), o('Expression + Expression', function() {
557 return new Op('+', $1, $3);
558 }), o('Expression - Expression', function() {
559 return new Op('-', $1, $3);
560 }), o('Expression MATH Expression', function() {
561 return new Op($2, $1, $3);
562 }), o('Expression SHIFT Expression', function() {
563 return new Op($2, $1, $3);
564 }), o('Expression COMPARE Expression', function() {
565 return new Op($2, $1, $3);
566 }), o('Expression LOGIC Expression', function() {
567 return new Op($2, $1, $3);
568 }), o('Expression RELATION Expression', function() {
569 if ($2.charAt(0) === '!') {
570 return new Op($2.slice(1), $1, $3).invert();
571 } else {
572 return new Op($2, $1, $3);
573 }
574 }), o('SimpleAssignable COMPOUND_ASSIGN\
575 Expression', function() {
576 return new Assign($1, $3, $2);
577 }), o('SimpleAssignable COMPOUND_ASSIGN\
578 INDENT Expression OUTDENT', function() {
579 return new Assign($1, $4, $2);
580 }), o('SimpleAssignable COMPOUND_ASSIGN TERMINATOR\
581 Expression', function() {
582 return new Assign($1, $4, $2);
583 }), o('SimpleAssignable EXTENDS Expression', function() {
584 return new Extends($1, $3);
585 })
586 ]
587 };
588
589 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']];
590
591 tokens = [];
592
593 for (name in grammar) {
594 alternatives = grammar[name];
595 grammar[name] = (function() {
596 var _i, _j, _len, _len1, _ref, _results;
597 _results = [];
598 for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
599 alt = alternatives[_i];
600 _ref = alt[0].split(' ');
601 for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
602 token = _ref[_j];
603 if (!grammar[token]) {
604 tokens.push(token);
605 }
606 }
607 if (name === 'Root') {
608 alt[1] = "return " + alt[1];
609 }
610 _results.push(alt);
611 }
612 return _results;
613 })();
614 }
615
616 exports.parser = new Parser({
617 tokens: tokens.join(' '),
618 bnf: grammar,
619 operators: operators.reverse(),
620 startSymbol: 'Root'
621 });
622
623}).call(this);