UNPKG

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