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