PageRenderTime 52ms CodeModel.GetById 11ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/coffee-script/grammar.js

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