1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/grammar.js

587 lines
17 KiB
JavaScript
Raw Normal View History

2010-07-25 03:15:12 -04:00
(function() {
var Parser, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap;
2010-02-10 23:24:05 -05:00
Parser = require('jison').Parser;
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
o = function(patternString, action, options) {
2010-02-10 23:24:05 -05:00
var match;
patternString = patternString.replace(/\s{2,}/g, ' ');
2010-10-07 07:05:22 -04:00
if (!action) {
return [patternString, '$$ = $1;', options];
2010-02-10 23:24:05 -05:00
}
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
action = action.replace(/\bnew /g, '$&yy.');
2011-01-15 14:19:35 -05:00
action = action.replace(/\b(?:Block\.wrap|extend)\b/g, 'yy.$&');
return [patternString, "$$ = " + action + ";", options];
2010-02-10 23:24:05 -05:00
};
grammar = {
Root: [
o('', function() {
2011-01-15 14:19:35 -05:00
return new Block;
}), o('Body'), o('Block TERMINATOR')
2010-02-10 23:24:05 -05:00
],
Body: [
o('Line', function() {
2011-01-15 14:19:35 -05:00
return Block.wrap([$1]);
}), o('Body TERMINATOR Line', function() {
2010-02-10 23:24:05 -05:00
return $1.push($3);
}), o('Body TERMINATOR')
2010-04-05 10:26:23 -04:00
],
Line: [o('Expression'), o('Statement')],
Statement: [
o('Return'), o('Throw'), o('Comment'), o('STATEMENT', function() {
return new Literal($1);
})
2010-02-10 23:24:05 -05:00
],
2010-11-09 00:26:31 -05:00
Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Class')],
Block: [
o('INDENT OUTDENT', function() {
2011-01-15 14:19:35 -05:00
return new Block;
}), o('INDENT Body OUTDENT', function() {
2010-02-10 23:24:05 -05:00
return $2;
})
],
Identifier: [
o('IDENTIFIER', function() {
return new Literal($1);
2010-02-10 23:24:05 -05:00
})
],
AlphaNumeric: [
o('NUMBER', function() {
return new Literal($1);
}), o('STRING', function() {
return new Literal($1);
2010-02-10 23:24:05 -05:00
})
],
Literal: [
o('AlphaNumeric'), o('JS', function() {
return new Literal($1);
}), o('REGEX', function() {
return new Literal($1);
}), o('BOOL', function() {
var val;
val = new Literal($1);
if ($1 === 'undefined') {
val.isUndefined = true;
}
return val;
2010-02-10 23:24:05 -05:00
})
],
Assign: [
o('Assignable = Expression', function() {
return new Assign($1, $3);
}), o('Assignable = INDENT Expression OUTDENT', function() {
return new Assign($1, $4);
2010-02-10 23:24:05 -05:00
})
],
AssignObj: [
2010-10-24 01:05:31 -04:00
o('ObjAssignable', function() {
return new Value($1);
2010-10-24 01:05:31 -04:00
}), o('ObjAssignable : Expression', function() {
return new Assign(new Value($1), $3, 'object');
2010-10-24 01:05:31 -04:00
}), o('ObjAssignable :\
INDENT Expression OUTDENT', function() {
return new Assign(new Value($1), $4, 'object');
}), o('Comment')
2010-02-10 23:24:05 -05:00
],
2010-11-28 12:42:43 -05:00
ObjAssignable: [o('Identifier'), o('AlphaNumeric'), o('ThisProperty')],
Return: [
o('RETURN Expression', function() {
return new Return($2);
}), o('RETURN', function() {
return new Return;
2010-02-10 23:24:05 -05:00
})
],
Comment: [
o('HERECOMMENT', function() {
return new Comment($1);
})
],
Code: [
o('PARAM_START ParamList PARAM_END FuncGlyph Block', function() {
return new Code($2, $5, $4);
}), o('FuncGlyph Block', function() {
return new Code([], $2, $1);
2010-02-10 23:24:05 -05:00
})
],
FuncGlyph: [
o('->', function() {
2010-02-10 23:24:05 -05:00
return 'func';
}), o('=>', function() {
2010-02-10 23:24:05 -05:00
return 'boundfunc';
})
],
OptComma: [o(''), o(',')],
ParamList: [
o('', function() {
return [];
}), o('Param', function() {
2010-02-10 23:24:05 -05:00
return [$1];
}), o('ParamList , Param', function() {
2010-10-11 03:06:29 -04:00
return $1.concat($3);
2010-02-10 23:24:05 -05:00
})
],
Param: [
2010-10-26 06:08:01 -04:00
o('ParamVar', function() {
2010-10-26 00:09:46 -04:00
return new Param($1);
2010-10-26 06:08:01 -04:00
}), o('ParamVar ...', function() {
2010-10-26 00:09:46 -04:00
return new Param($1, null, true);
}), o('ParamVar = Expression', function() {
2010-10-26 00:09:46 -04:00
return new Param($1, $3);
2010-02-10 23:24:05 -05:00
})
],
ParamVar: [o('Identifier'), o('ThisProperty'), o('Array'), o('Object')],
Splat: [
o('Expression ...', function() {
return new Splat($1);
2010-02-10 23:24:05 -05:00
})
],
SimpleAssignable: [
o('Identifier', function() {
return new Value($1);
}), o('Value Accessor', function() {
2010-03-28 13:06:16 -04:00
return $1.push($2);
}), o('Invocation Accessor', function() {
return new Value($1, [$2]);
}), o('ThisProperty')
2010-03-28 13:06:16 -04:00
],
Assignable: [
2010-10-24 01:05:31 -04:00
o('SimpleAssignable'), o('Array', function() {
return new Value($1);
2010-10-24 01:05:31 -04:00
}), o('Object', function() {
return new Value($1);
})
],
Value: [
o('Assignable'), o('Literal', function() {
return new Value($1);
}), o('Parenthetical', function() {
return new Value($1);
2010-11-20 16:25:22 -05:00
}), o('Range', function() {
return new Value($1);
}), o('This')
2010-02-10 23:24:05 -05:00
],
Accessor: [
o('. Identifier', function() {
2010-11-20 20:09:36 -05:00
return new Access($2);
}), o('?. Identifier', function() {
2010-11-20 20:09:36 -05:00
return new Access($2, 'soak');
2010-10-25 14:56:02 -04:00
}), o(':: Identifier', function() {
2010-11-20 20:09:36 -05:00
return new Access($2, 'proto');
}), o('::', function() {
2010-11-20 20:09:36 -05:00
return new Access(new Literal('prototype'));
}), o('Index'), o('Slice', function() {
return new Slice($1);
})
2010-02-10 23:24:05 -05:00
],
Index: [
o('INDEX_START Expression INDEX_END', function() {
return new Index($2);
}), o('INDEX_SOAK Index', function() {
return extend($2, {
2010-10-25 09:31:52 -04:00
soak: true
});
}), o('INDEX_PROTO Index', function() {
return extend($2, {
proto: true
});
2010-02-10 23:24:05 -05:00
})
],
Object: [
o('{ AssignList OptComma }', function() {
return new Obj($2, $1.generated);
})
],
AssignList: [
o('', function() {
2010-02-10 23:24:05 -05:00
return [];
}), o('AssignObj', function() {
2010-02-10 23:24:05 -05:00
return [$1];
}), o('AssignList , AssignObj', function() {
2010-10-11 03:06:29 -04:00
return $1.concat($3);
}), o('AssignList OptComma TERMINATOR AssignObj', function() {
2010-10-11 03:06:29 -04:00
return $1.concat($4);
}), o('AssignList OptComma INDENT AssignList OptComma OUTDENT', function() {
return $1.concat($4);
2010-02-10 23:24:05 -05:00
})
],
Class: [
o('CLASS', function() {
return new Class;
}), o('CLASS Block', function() {
return new Class(null, null, $2);
}), o('CLASS EXTENDS Value', function() {
return new Class(null, $3);
}), o('CLASS EXTENDS Value Block', function() {
return new Class(null, $3, $4);
}), o('CLASS SimpleAssignable', function() {
return new Class($2);
}), o('CLASS SimpleAssignable Block', function() {
return new Class($2, null, $3);
}), o('CLASS SimpleAssignable EXTENDS Value', function() {
return new Class($2, $4);
}), o('CLASS SimpleAssignable EXTENDS Value Block', function() {
return new Class($2, $4, $5);
})
2010-02-10 23:24:05 -05:00
],
Invocation: [
o('Value OptFuncExist Arguments', function() {
return new Call($1, $3, $2);
}), o('Invocation OptFuncExist Arguments', function() {
return new Call($1, $3, $2);
}), o('SUPER', function() {
return new Call('super', [new Splat(new Literal('arguments'))]);
}), o('SUPER Arguments', function() {
return new Call('super', $2);
})
],
OptFuncExist: [
o('', function() {
return false;
}), o('FUNC_EXIST', function() {
return true;
2010-02-10 23:24:05 -05:00
})
],
Arguments: [
o('CALL_START CALL_END', function() {
return [];
}), o('CALL_START ArgList OptComma CALL_END', function() {
return $2;
2010-02-10 23:24:05 -05:00
})
],
This: [
o('THIS', function() {
return new Value(new Literal('this'));
}), o('@', function() {
return new Value(new Literal('this'));
2010-03-28 13:06:16 -04:00
})
],
ThisProperty: [
o('@ Identifier', function() {
2010-11-20 20:09:36 -05:00
return new Value(new Literal('this'), [new Access($2)], 'this');
2010-02-10 23:24:05 -05:00
})
],
Array: [
o('[ ]', function() {
return new Arr([]);
}), o('[ ArgList OptComma ]', function() {
return new Arr($2);
2010-02-10 23:24:05 -05:00
})
],
2010-11-20 16:25:22 -05:00
RangeDots: [
o('..', function() {
return 'inclusive';
}), o('...', function() {
return 'exclusive';
})
],
Range: [
o('[ Expression RangeDots Expression ]', function() {
return new Range($2, $4, $3);
})
],
Slice: [
o('INDEX_START Expression RangeDots Expression INDEX_END', function() {
return new Range($2, $4, $3);
}), o('INDEX_START Expression RangeDots INDEX_END', function() {
return new Range($2, null, $3);
}), o('INDEX_START RangeDots Expression INDEX_END', function() {
return new Range(null, $3, $2);
})
],
ArgList: [
o('Arg', function() {
2010-02-10 23:24:05 -05:00
return [$1];
}), o('ArgList , Arg', function() {
2010-10-11 03:06:29 -04:00
return $1.concat($3);
}), o('ArgList OptComma TERMINATOR Arg', function() {
2010-10-11 03:06:29 -04:00
return $1.concat($4);
}), o('INDENT ArgList OptComma OUTDENT', function() {
return $2;
}), o('ArgList OptComma INDENT ArgList OptComma OUTDENT', function() {
return $1.concat($4);
})
2010-02-10 23:24:05 -05:00
],
Arg: [o('Expression'), o('Splat')],
SimpleArgs: [
o('Expression'), o('SimpleArgs , Expression', function() {
2010-10-11 03:06:29 -04:00
return [].concat($1, $3);
2010-02-10 23:24:05 -05:00
})
],
Try: [
o('TRY Block', function() {
return new Try($2);
}), o('TRY Block Catch', function() {
return new Try($2, $3[0], $3[1]);
}), o('TRY Block FINALLY Block', function() {
return new Try($2, null, null, $4);
}), o('TRY Block Catch FINALLY Block', function() {
return new Try($2, $3[0], $3[1], $5);
2010-02-10 23:24:05 -05:00
})
],
Catch: [
o('CATCH Identifier Block', function() {
2010-02-10 23:24:05 -05:00
return [$2, $3];
})
],
Throw: [
o('THROW Expression', function() {
return new Throw($2);
2010-02-10 23:24:05 -05:00
})
],
Parenthetical: [
o('( Body )', function() {
2010-10-06 23:53:26 -04:00
return new Parens($2);
}), o('( INDENT Body OUTDENT )', function() {
return new Parens($3);
2010-02-10 23:24:05 -05:00
})
],
WhileSource: [
o('WHILE Expression', function() {
return new While($2);
}), o('WHILE Expression WHEN Expression', function() {
return new While($2, {
guard: $4
});
}), o('UNTIL Expression', function() {
return new While($2, {
invert: true
});
}), o('UNTIL Expression WHEN Expression', function() {
return new While($2, {
invert: true,
guard: $4
});
})
],
While: [
o('WhileSource Block', function() {
return $1.addBody($2);
}), o('Statement WhileSource', function() {
2011-01-15 14:19:35 -05:00
return $2.addBody(Block.wrap([$1]));
}), o('Expression WhileSource', function() {
2011-01-15 14:19:35 -05:00
return $2.addBody(Block.wrap([$1]));
}), o('Loop', function() {
return $1;
})
],
Loop: [
o('LOOP Block', function() {
return new While(new Literal('true')).addBody($2);
}), o('LOOP Expression', function() {
2011-01-15 14:19:35 -05:00
return new While(new Literal('true')).addBody(Block.wrap([$2]));
2010-02-10 23:24:05 -05:00
})
],
For: [
o('Statement ForBody', function() {
return new For($1, $2);
}), o('Expression ForBody', function() {
return new For($1, $2);
}), o('ForBody Block', function() {
return new For($2, $1);
2010-11-20 16:25:22 -05:00
})
],
ForBody: [
o('FOR Range', function() {
return {
source: new Value($2)
2010-11-20 16:25:22 -05:00
};
}), o('ForStart ForSource', function() {
2010-11-28 18:33:43 -05:00
$2.own = $1.own;
$2.name = $1[0];
$2.index = $1[1];
2010-11-20 16:25:22 -05:00
return $2;
})
],
ForStart: [
o('FOR ForVariables', function() {
return $2;
2010-11-28 18:33:43 -05:00
}), o('FOR OWN ForVariables', function() {
$3.own = true;
2010-11-20 16:25:22 -05:00
return $3;
2010-02-10 23:24:05 -05:00
})
],
ForValue: [
o('Identifier'), o('Array', function() {
return new Value($1);
}), o('Object', function() {
return new Value($1);
})
],
2010-11-20 16:25:22 -05:00
ForVariables: [
o('ForValue', function() {
return [$1];
}), o('ForValue , ForValue', function() {
return [$1, $3];
})
],
ForSource: [
o('FORIN Expression', function() {
2010-02-10 23:24:05 -05:00
return {
source: $2
};
2010-11-20 16:25:22 -05:00
}), o('FOROF Expression', function() {
2010-02-10 23:24:05 -05:00
return {
source: $2,
2010-11-20 16:25:22 -05:00
object: true
2010-02-10 23:24:05 -05:00
};
2010-11-20 16:25:22 -05:00
}), o('FORIN Expression WHEN Expression', function() {
return {
source: $2,
2010-11-20 16:25:22 -05:00
guard: $4
};
}), o('FOROF Expression WHEN Expression', function() {
return {
source: $2,
2010-11-20 16:25:22 -05:00
guard: $4,
object: true
};
2010-11-20 16:25:22 -05:00
}), o('FORIN Expression BY Expression', function() {
return {
2010-11-20 16:25:22 -05:00
source: $2,
step: $4
};
2010-11-20 16:25:22 -05:00
}), o('FORIN Expression WHEN Expression BY Expression', function() {
return {
2010-11-20 16:25:22 -05:00
source: $2,
guard: $4,
step: $6
};
2010-11-20 16:25:22 -05:00
}), o('FORIN Expression BY Expression WHEN Expression', function() {
return {
2010-11-20 16:25:22 -05:00
source: $2,
step: $4,
guard: $6
};
2010-02-10 23:24:05 -05:00
})
],
Switch: [
o('SWITCH Expression INDENT Whens OUTDENT', function() {
return new Switch($2, $4);
}), o('SWITCH Expression INDENT Whens ELSE Block OUTDENT', function() {
return new Switch($2, $4, $6);
}), o('SWITCH INDENT Whens OUTDENT', function() {
return new Switch(null, $3);
}), o('SWITCH INDENT Whens ELSE Block OUTDENT', function() {
return new Switch(null, $3, $5);
2010-02-10 23:24:05 -05:00
})
],
Whens: [
o('When'), o('Whens When', function() {
return $1.concat($2);
2010-02-10 23:24:05 -05:00
})
],
When: [
o('LEADING_WHEN SimpleArgs Block', function() {
return [[$2, $3]];
}), o('LEADING_WHEN SimpleArgs Block TERMINATOR', function() {
return [[$2, $3]];
2010-02-10 23:24:05 -05:00
})
],
IfBlock: [
o('IF Expression Block', function() {
return new If($2, $3, {
type: $1
2010-04-27 19:35:15 -04:00
});
}), o('IfBlock ELSE IF Expression Block', function() {
return $1.addElse(new If($4, $5, {
type: $3
}));
2010-02-10 23:24:05 -05:00
})
],
If: [
2011-01-10 23:19:31 -05:00
o('IfBlock'), o('IfBlock ELSE Block', function() {
return $1.addElse($3);
}), o('Statement POST_IF Expression', function() {
2011-01-15 14:19:35 -05:00
return new If($3, Block.wrap([$1]), {
type: $2,
2010-04-05 10:26:23 -04:00
statement: true
});
}), o('Expression POST_IF Expression', function() {
2011-01-15 14:19:35 -05:00
return new If($3, Block.wrap([$1]), {
type: $2,
2010-02-10 23:24:05 -05:00
statement: true
});
})
],
Operation: [
2010-10-20 20:16:17 -04:00
o('UNARY Expression', function() {
return new Op($1, $2);
}), o('- Expression', (function() {
return new Op('-', $2);
}), {
prec: 'UNARY'
}), o('+ Expression', (function() {
return new Op('+', $2);
}), {
prec: 'UNARY'
2010-10-20 20:16:17 -04:00
}), o('-- SimpleAssignable', function() {
return new Op('--', $2);
2010-10-20 20:16:17 -04:00
}), o('++ SimpleAssignable', function() {
return new Op('++', $2);
2010-10-20 20:16:17 -04:00
}), o('SimpleAssignable --', function() {
return new Op('--', $1, null, true);
2010-10-20 20:16:17 -04:00
}), o('SimpleAssignable ++', function() {
return new Op('++', $1, null, true);
}), o('Expression ?', function() {
return new Existence($1);
2010-10-20 20:16:17 -04:00
}), o('Expression + Expression', function() {
return new Op('+', $1, $3);
2010-10-20 20:16:17 -04:00
}), o('Expression - Expression', function() {
return new Op('-', $1, $3);
2010-10-20 20:16:17 -04:00
}), o('Expression MATH Expression', function() {
return new Op($2, $1, $3);
2010-10-20 20:16:17 -04:00
}), o('Expression SHIFT Expression', function() {
return new Op($2, $1, $3);
2010-10-20 20:16:17 -04:00
}), o('Expression COMPARE Expression', function() {
return new Op($2, $1, $3);
2010-10-20 20:16:17 -04:00
}), o('Expression LOGIC Expression', function() {
return new Op($2, $1, $3);
2010-10-20 20:16:17 -04:00
}), o('Expression RELATION Expression', function() {
if ($2.charAt(0) === '!') {
return new Op($2.slice(1), $1, $3).invert();
} else {
return new Op($2, $1, $3);
}
}), o('SimpleAssignable COMPOUND_ASSIGN\
Expression', function() {
return new Assign($1, $3, $2);
}), o('SimpleAssignable COMPOUND_ASSIGN\
INDENT Expression OUTDENT', function() {
return new Assign($1, $4, $2);
2010-11-09 00:26:31 -05:00
}), o('SimpleAssignable EXTENDS Expression', function() {
return new Extends($1, $3);
})
2010-02-10 23:24:05 -05:00
]
};
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', 'DO', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS'], ['right', 'POST_IF']];
2010-02-10 23:24:05 -05:00
tokens = [];
2010-10-01 18:26:37 -04:00
for (name in grammar) {
alternatives = grammar[name];
2010-12-23 13:50:52 -05:00
grammar[name] = (function() {
var _i, _j, _len, _len2, _ref, _results;
2010-11-09 00:17:08 -05:00
_results = [];
for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
alt = alternatives[_i];
_ref = alt[0].split(' ');
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
token = _ref[_j];
if (!grammar[token]) {
tokens.push(token);
}
}
if (name === 'Root') {
alt[1] = "return " + alt[1];
}
2010-11-09 00:17:08 -05:00
_results.push(alt);
}
2010-11-09 00:17:08 -05:00
return _results;
2010-12-23 13:50:52 -05:00
})();
}
2010-02-16 01:04:48 -05:00
exports.parser = new Parser({
tokens: tokens.join(' '),
bnf: grammar,
operators: operators.reverse(),
2010-02-10 23:24:05 -05:00
startSymbol: 'Root'
});
}).call(this);