From 522df2a355f8bdc4602039b1163b40f58d64ee66 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 8 Feb 2010 23:51:34 -0500 Subject: [PATCH] CoffeeScript-in-CoffeeScript can compile @property references. --- lib/coffee_script/nodes.js | 11 ++++++++++- lib/coffee_script/parser.js | 9 +++++++++ src/nodes.coffee | 12 ++++++++++++ src/parser.coffee | 7 +++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 495f0dcb..42ad97d7 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -1,5 +1,5 @@ (function(){ - var AccessorNode, CallNode, CommentNode, Expressions, ExtendsNode, IndexNode, LiteralNode, Node, ReturnNode, TAB, TRAILING_WHITESPACE, ValueNode, any, compact, del, dup, flatten, inherit, merge, statement; + var AccessorNode, CallNode, CommentNode, Expressions, ExtendsNode, IndexNode, LiteralNode, Node, ReturnNode, TAB, TRAILING_WHITESPACE, ThisNode, ValueNode, any, compact, del, dup, flatten, inherit, merge, statement; var __hasProp = Object.prototype.hasOwnProperty; process.mixin(require('./scope')); // The abstract base class for all CoffeeScript nodes. @@ -744,4 +744,13 @@ return '[' + this.index.compile(o) + ']'; } })); + // A this-reference, using '@'. + ThisNode = (exports.ThisNode = inherit(Node, { + constructor: function constructor(property) { + return this.property = property || null; + }, + compile_node: function compile_node(o) { + return 'this' + (this.property ? '.' + this.property : ''); + } + })); })(); \ No newline at end of file diff --git a/lib/coffee_script/parser.js b/lib/coffee_script/parser.js index 03848cfd..0872d1fc 100644 --- a/lib/coffee_script/parser.js +++ b/lib/coffee_script/parser.js @@ -212,6 +212,8 @@ return new ValueNode($1); }), o("Range", function() { return new ValueNode($1); + }), o("This", function() { + return new ValueNode($1); }), o("Value Accessor", function() { return $1.push($2); }), o("Invocation Accessor", function() { @@ -285,6 +287,13 @@ return new CallNode('super', $3); }) ], + // This references, either naked or to a property. + This: [o("@", function() { + return new ThisNode(); + }), o("@ IDENTIFIER", function() { + return new ThisNode(yytext); + }) + ], // The range literal. Range: [o("[ Expression . . Expression ]", function() { return new RangeNode($2, $5); diff --git a/src/nodes.coffee b/src/nodes.coffee index 4b5034b0..9517b573 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -473,6 +473,18 @@ IndexNode: exports.IndexNode: inherit Node, { } +# A this-reference, using '@'. +ThisNode: exports.ThisNode: inherit Node, { + + constructor: (property) -> + @property: property or null + + compile_node: (o) -> + 'this' + (if @property then '.' + @property else '') + +} + + diff --git a/src/parser.coffee b/src/parser.coffee index 2c0e2ec8..2b44d209 100644 --- a/src/parser.coffee +++ b/src/parser.coffee @@ -230,6 +230,7 @@ grammar: { o "Object", -> new ValueNode($1) o "Parenthetical", -> new ValueNode($1) o "Range", -> new ValueNode($1) + o "This", -> new ValueNode($1) o "Value Accessor", -> $1.push($2) o "Invocation Accessor", -> new ValueNode($1, [$2]) ] @@ -291,6 +292,12 @@ grammar: { o "SUPER CALL_START ArgList CALL_END", -> new CallNode('super', $3) ] + # This references, either naked or to a property. + This: [ + o "@", -> new ThisNode() + o "@ IDENTIFIER", -> new ThisNode(yytext) + ] + # The range literal. Range: [ o "[ Expression . . Expression ]", -> new RangeNode($2, $5)