Permalink
Browse files

Add support for @foo a la CoffeeScript and add a bunch of tests

  • Loading branch information...
1 parent 9f495b5 commit 88e8cd97200672925040755e66197259c83925e9 @wycats committed Oct 15, 2011
Showing with 35 additions and 4 deletions.
  1. +4 −0 lib/grammar.y
  2. +1 −0 lib/lexer.l
  3. +2 −2 lib/stringify.js
  4. +28 −2 test/stringify-test.js
View
@@ -241,6 +241,8 @@ MemberExpr
| FunctionExpr
| MemberExpr '[' Expr ']'
{ $$ = yy.Node('MemberExpression',$1,$3,true,yy.loc([@$,@4])); }
+ | AT_THISTOKEN IdentifierName
+ { $$ = yy.Node('MemberExpression',yy.Node('ThisExpression'),yy.Node('Identifier', String($2)),false,yy.loc([@$,@2])); }
| MemberExpr '.' IdentifierName
{ $$ = yy.Node('MemberExpression',$1,yy.Node('Identifier', String($3)),false,yy.loc([@$,@3])); }
| NEW MemberExpr Arguments
@@ -251,6 +253,8 @@ MemberExprNoBF
: PrimaryExprNoBrace
| MemberExprNoBF '[' Expr ']'
{ $$ = yy.Node('MemberExpression',$1,$3,true,yy.loc([@$,@4])); }
+ | AT_THISTOKEN IdentifierName
+ { $$ = yy.Node('MemberExpression',yy.Node('ThisExpression'),yy.Node('Identifier', String($2)),false,yy.loc([@$,@2])); }
| MemberExprNoBF '.' IdentifierName
{ $$ = yy.Node('MemberExpression',$1,yy.Node('Identifier', String($3)),false,yy.loc([@$,@3])); }
| NEW MemberExpr Arguments
View
@@ -142,6 +142,7 @@ RCLASS "["({BSL}|[^\\\]])*"]"
"static" return 'STATIC'
"yield" return 'YIELD'
"this" return 'THISTOKEN'
+"@" return 'AT_THISTOKEN'
"true" return 'TRUETOKEN'
"false" return 'FALSETOKEN'
"null" return 'NULLTOKEN'
View
@@ -634,7 +634,7 @@ function sourceElement(n, indent) {
function decaf_handleFunctionDeclSpread(n) {
var len = n.params.length - 1, params = n.params, spread, ast;
- if (params[len].type === 'Spread') {
+ if (params[len] && params[len].type === 'Spread') {
spread = params.pop();
// TODO: save off AST
ast = Reflect.parse("var " + spread.name + " = " + len + " < arguments.length ? [].slice.call(arguments, " + len + ") : []");
@@ -654,7 +654,7 @@ function decaf_handleCallSpread(n, indent, callback) {
// more parameters than just the spread
if (params.length) {
- var argString = "[" + argBody(params, indent) + "].concat([].slice.call(" + spreadName + ")";
+ var argString = "[" + argBody(params, indent) + "].concat([].slice.call(" + spreadName + "))";
} else {
var argString = spreadName;
}
@@ -271,12 +271,38 @@ function runUnitTests() {
" }\n" +
" new test3;\n" +
"}\n"),
+
+ // --- decaf extensions ---
+
+ // @foo
+ ["@foo", "this.foo;\n"],
+ ["@foo.bar", "this.foo.bar;\n"],
+ ["x.y(@foo.bar(baz))", "x.y(this.foo.bar(baz));\n"],
+
+ // spread
+ ["x(...y)", "x.apply(undefined, y);\n"],
+ ["x(y, ...z)", "x.apply(undefined, [y].concat([].slice.call(z)));\n"],
+ ["x(y, z, ...a)", "x.apply(undefined, [y, z].concat([].slice.call(a)));\n"],
+ ["x.y(...z)", "x.y.apply(x, z);\n"],
+ ["x.y(z, ...a)", "x.y.apply(x, [z].concat([].slice.call(a)));\n"],
+ ["x.y(z, a, ...b)", "x.y.apply(x, [z, a].concat([].slice.call(b)));\n"],
+ ["x.y.z(...a)", "(var _ref=x.y).z.apply(_ref, a);\n"],
+ ["x.y.z(a, ...b)", "(var _ref=x.y).z.apply(_ref, [a].concat([].slice.call(b)));\n"],
+ ["x.y.z(a, b, ...c)", "(var _ref=x.y).z.apply(_ref, [a, b].concat([].slice.call(c)));\n"],
+ ["@x.y.z(a, b, ...c)", "(var _ref=this.x.y).z.apply(_ref, [a, b].concat([].slice.call(c)));\n"],
+ ["x.y.z(a, @b, ...c)", "(var _ref=x.y).z.apply(_ref, [a, this.b].concat([].slice.call(c)));\n"],
];
for (var i = 0; i < tests.length; i++) {
- var b = tests[i], a;
try {
- a = Reflect.stringify(Reflect.parse(b, {loc: false}));
+ if (typeof tests[i] === "string") {
+ var b = tests[i], a;
+ a = Reflect.stringify(Reflect.parse(b, {loc: false}));
+ } else {
+ var b = tests[i][1], a;
+ a = Reflect.stringify(Reflect.parse(tests[i][0], {loc: false}));
+ }
+
if (typeof a !== "string") {
throw new TypeError("Reflect.stringify returned " +
(a !== null && typeof a === "object"

0 comments on commit 88e8cd9

Please sign in to comment.