diff --git a/lib/lexer.js b/lib/lexer.js index 7aaa6847..92aa8510 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -29,7 +29,7 @@ return (new Rewriter).rewrite(this.tokens); }; Lexer.prototype.identifierToken = function() { - var closeIndex, forcedIdentifier, id, match, tag; + var forcedIdentifier, id, match, tag; if (!(match = IDENTIFIER.exec(this.chunk))) { return false; } @@ -64,15 +64,9 @@ } if (include(JS_FORBIDDEN, id)) { if (forcedIdentifier) { - tag = 'STRING'; - id = ("\"" + id + "\""); - if (forcedIdentifier === 'accessor') { - closeIndex = true; - if (this.tag() !== '@') { - this.tokens.pop(); - } - this.token('INDEX_START', '['); - } + tag = 'IDENTIFIER'; + id = new String(id); + id.reserved = true; } else if (include(RESERVED, id)) { this.identifierError(id); } @@ -88,9 +82,6 @@ } } this.token(tag, id); - if (closeIndex) { - this.token(']', ']'); - } return true; }; Lexer.prototype.numberToken = function() { @@ -335,7 +326,8 @@ this.i += value.length; tag = value; if (value === '=') { - if (include(JS_FORBIDDEN, pval = this.value())) { + pval = this.value(); + if (!pval.reserved && include(JS_FORBIDDEN, pval)) { this.assignmentError(); } if (('or' === pval || 'and' === pval)) { diff --git a/lib/nodes.js b/lib/nodes.js index 5a054909..38544074 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -263,11 +263,15 @@ }; LiteralNode.prototype.isPureStatement = LiteralNode.prototype.isStatement; LiteralNode.prototype.isComplex = NO; + LiteralNode.prototype.isReserved = function() { + return !!this.value.reserved; + }; LiteralNode.prototype.compileNode = function(o) { - var end, idt; + var end, idt, val; idt = this.isStatement(o) ? this.idt() : ''; end = this.isStatement(o) ? ';' : ''; - return idt + this.value + end; + val = this.isReserved() ? ("\"" + (this.value) + "\"") : this.value; + return idt + val + end; }; LiteralNode.prototype.toString = function() { return ' "' + this.value + '"'; diff --git a/src/lexer.coffee b/src/lexer.coffee index 17a3f9c9..cfccad80 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -100,12 +100,9 @@ exports.Lexer = class Lexer id = '!' + id if include JS_FORBIDDEN, id if forcedIdentifier - tag = 'STRING' - id = "\"#{id}\"" - if forcedIdentifier is 'accessor' - closeIndex = on - @tokens.pop() if @tag() isnt '@' - @token 'INDEX_START', '[' + tag = 'IDENTIFIER' + id = new String id + id.reserved = yes else if include(RESERVED, id) @identifierError id unless forcedIdentifier @@ -115,7 +112,6 @@ exports.Lexer = class Lexer else if include LOGIC, id tag = 'LOGIC' @token tag, id - @token ']', ']' if closeIndex true # Matches numbers, including decimals, hex, and exponential notation. @@ -312,7 +308,8 @@ exports.Lexer = class Lexer @i += value.length tag = value if value is '=' - @assignmentError() if include JS_FORBIDDEN, pval = @value() + pval = @value() + @assignmentError() if not pval.reserved and include JS_FORBIDDEN, pval if pval in ['or', 'and'] prev = last @tokens prev[0] = 'COMPOUND_ASSIGN' diff --git a/src/nodes.coffee b/src/nodes.coffee index b13b3650..5c7fa373 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -248,10 +248,14 @@ exports.LiteralNode = class LiteralNode extends BaseNode isComplex: NO + isReserved: -> + !!@value.reserved + compileNode: (o) -> idt = if @isStatement(o) then @idt() else '' end = if @isStatement(o) then ';' else '' - idt + @value + end + val = if @isReserved() then "\"#{@value}\"" else @value + idt + val + end toString: -> ' "' + @value + '"' diff --git a/test/test_classes.coffee b/test/test_classes.coffee index 5b0bfd4c..0b0727c8 100644 --- a/test/test_classes.coffee +++ b/test/test_classes.coffee @@ -51,6 +51,8 @@ ok (new SubClass).prop is 'top-super-sub' class OneClass + @new: 'new' + function: 'function' constructor: (name) -> @name = name class TwoClass extends OneClass @@ -58,6 +60,8 @@ class TwoClass extends OneClass Function.prototype.new = -> new this arguments... ok (TwoClass.new('three')).name is 'three' +ok (new OneClass).function is 'function' +ok OneClass.new is 'new' delete Function.prototype.new