diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index f5649a3d13..f022de68e3 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -174,37 +174,48 @@ }; Lexer.prototype.numberToken = function() { - var binaryLiteral, lexedLength, match, number, numberValue, octalLiteral, tag; + var base, lexedLength, match, number, numberValue, ref2, tag; if (!(match = NUMBER.exec(this.chunk))) { return 0; } number = match[0]; lexedLength = number.length; - if (/^0[BOX]/.test(number)) { - this.error("radix prefix in '" + number + "' must be lowercase", { - offset: 1 - }); - } else if (/E/.test(number) && !/^0x/.test(number)) { - this.error("exponential notation in '" + number + "' must be indicated with a lowercase 'e'", { - offset: number.indexOf('E') - }); - } else if (/^0\d*[89]/.test(number)) { - this.error("decimal literal '" + number + "' must not be prefixed with '0'", { - length: lexedLength - }); - } else if (/^0\d+/.test(number)) { - this.error("octal literal '" + number + "' must be prefixed with '0o'", { - length: lexedLength - }); + switch (false) { + case !/^0[BOX]/.test(number): + this.error("radix prefix in '" + number + "' must be lowercase", { + offset: 1 + }); + break; + case !/^(?!0x).*E/.test(number): + this.error("exponential notation in '" + number + "' must be indicated with a lowercase 'e'", { + offset: number.indexOf('E') + }); + break; + case !/^0\d*[89]/.test(number): + this.error("decimal literal '" + number + "' must not be prefixed with '0'", { + length: lexedLength + }); + break; + case !/^0\d+/.test(number): + this.error("octal literal '" + number + "' must be prefixed with '0o'", { + length: lexedLength + }); } - if (octalLiteral = /^0o([0-7]+)/.exec(number)) { - numberValue = parseInt(octalLiteral[1], 8); - number = "0x" + (numberValue.toString(16)); - } else if (binaryLiteral = /^0b([01]+)/.exec(number)) { - numberValue = parseInt(binaryLiteral[1], 2); + base = (function() { + switch (number.charAt(1)) { + case 'b': + return 2; + case 'o': + return 8; + case 'x': + return 16; + default: + return null; + } + })(); + numberValue = base != null ? parseInt(number.slice(2), base) : parseFloat(number); + if ((ref2 = number.charAt(1)) === 'b' || ref2 === 'o') { number = "0x" + (numberValue.toString(16)); - } else { - numberValue = parseFloat(number); } tag = numberValue === 2e308 ? 'INFINITY' : 'NUMBER'; this.token(tag, number, 0, lexedLength);