From f4c1b20ec2a952c740b60cbed04d4a5c204b4538 Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Thu, 22 Sep 2011 04:09:58 -0400 Subject: [PATCH] fixes #1724: regular expressions beginning with `*` also normalised capitalisation in a few error messages --- lib/coffee-script/lexer.js | 10 +++++++--- src/lexer.coffee | 8 +++++--- test/regexps.coffee | 3 +++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/coffee-script/lexer.js b/lib/coffee-script/lexer.js index 4502f714..65a92901 100644 --- a/lib/coffee-script/lexer.js +++ b/lib/coffee-script/lexer.js @@ -72,7 +72,7 @@ id = new String(id); id.reserved = true; } else if (__indexOf.call(RESERVED, id) >= 0) { - this.error("Reserved word \"" + word + "\""); + this.error("reserved word \"" + word + "\""); } } if (!forcedIdentifier) { @@ -194,7 +194,11 @@ } if (!(match = REGEX.exec(this.chunk))) return 0; regex = match[0]; - this.token('REGEX', regex === '//' ? '/(?:)/' : regex); + if (regex.match(/^\/\*/)) { + this.error('regular expressions cannot begin with `*`'); + } + if (regex === '//') regex = '/(?:)/'; + this.token('REGEX', regex); return regex.length; }; @@ -338,7 +342,7 @@ prev = last(this.tokens); if (value === '=' && prev) { if (!prev[1].reserved && (_ref3 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref3) >= 0)) { - this.error("Reserved word \"" + (this.value()) + "\" can't be assigned"); + this.error("reserved word \"" + (this.value()) + "\" can't be assigned"); } if ((_ref4 = prev[1]) === '||' || _ref4 === '&&') { prev[0] = 'COMPOUND_ASSIGN'; diff --git a/src/lexer.coffee b/src/lexer.coffee index 401eef08..3c3b0d5c 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -112,7 +112,7 @@ exports.Lexer = class Lexer id = new String id id.reserved = yes else if id in RESERVED - @error "Reserved word \"#{word}\"" + @error "reserved word \"#{word}\"" unless forcedIdentifier id = COFFEE_ALIAS_MAP[id] if id in COFFEE_ALIASES @@ -199,7 +199,9 @@ exports.Lexer = class Lexer return 0 if prev and (prev[0] in (if prev.spaced then NOT_REGEX else NOT_SPACED_REGEX)) return 0 unless match = REGEX.exec @chunk [regex] = match - @token 'REGEX', if regex is '//' then '/(?:)/' else regex + if regex.match /^\/\*/ then @error 'regular expressions cannot begin with `*`' + if regex is '//' then regex = '/(?:)/' + @token 'REGEX', regex regex.length # Matches multiline extended regular expressions. @@ -324,7 +326,7 @@ exports.Lexer = class Lexer prev = last @tokens if value is '=' and prev if not prev[1].reserved and prev[1] in JS_FORBIDDEN - @error "Reserved word \"#{@value()}\" can't be assigned" + @error "reserved word \"#{@value()}\" can't be assigned" if prev[1] in ['||', '&&'] prev[0] = 'COMPOUND_ASSIGN' prev[1] += '=' diff --git a/test/regexps.coffee b/test/regexps.coffee index 6b50e54c..e15e8ef4 100644 --- a/test/regexps.coffee +++ b/test/regexps.coffee @@ -38,6 +38,9 @@ test "#764: regular expressions should be indexable", -> test "#584: slashes are allowed unescaped in character classes", -> ok /^a\/[/]b$/.test 'a//b' +test "#1724: regular expressions beginning with `*`", -> + throws -> CoffeeScript.compile '/*/' + # Heregexe(n|s)