Fixing Issue #744 -- you can now use reserved words as static properties of a class.

This commit is contained in:
Jeremy Ashkenas 2010-10-06 20:07:19 -04:00
parent af2e866947
commit 69b901a5b6
5 changed files with 26 additions and 25 deletions

View File

@ -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)) {

View File

@ -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 + '"';

View File

@ -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'

View File

@ -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 + '"'

View File

@ -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