empty regular expressions with flags still need to be compiled to /(?:)/

This commit is contained in:
Michael Ficarra 2011-10-03 03:47:24 -04:00
parent 1627922060
commit 9fef66ffcf
3 changed files with 18 additions and 12 deletions

View File

@ -181,7 +181,7 @@
};
Lexer.prototype.regexToken = function() {
var length, match, prev, regex, _ref3;
var flags, length, match, prev, regex, _ref3, _ref4;
if (this.chunk.charAt(0) !== '/') return 0;
if (match = HEREGEX.exec(this.chunk)) {
length = this.heregexToken(match);
@ -193,13 +193,13 @@
return 0;
}
if (!(match = REGEX.exec(this.chunk))) return 0;
regex = match[0];
if (regex.match(/^\/\*/)) {
_ref4 = match, match = _ref4[0], regex = _ref4[1], flags = _ref4[2];
if (regex.slice(0, 2) === '/*') {
this.error('regular expressions cannot begin with `*`');
}
if (regex === '//') regex = '/(?:)/';
this.token('REGEX', regex);
return regex.length;
this.token('REGEX', "" + regex + flags);
return match.length;
};
Lexer.prototype.heregexToken = function(match) {
@ -638,7 +638,7 @@
JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/;
REGEX = /^\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/[imgy]{0,4}(?!\w)/;
REGEX = /^(\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/)([imgy]{0,4})(?!\w)/;
HEREGEX = /^\/{3}([\s\S]+?)\/{3}([imgy]{0,4})(?!\w)/;

View File

@ -198,11 +198,11 @@ exports.Lexer = class Lexer
prev = last @tokens
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
if regex.match /^\/\*/ then @error 'regular expressions cannot begin with `*`'
[match, regex, flags] = match
if regex[..1] is '/*' then @error 'regular expressions cannot begin with `*`'
if regex is '//' then regex = '/(?:)/'
@token 'REGEX', regex
regex.length
@token 'REGEX', "#{regex}#{flags}"
match.length
# Matches multiline extended regular expressions.
heregexToken: (match) ->
@ -607,7 +607,7 @@ JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/
# Regex-matching-regexes.
REGEX = /// ^
/ (?! [\s=] ) # disallow leading whitespace or equals signs
(/ (?! [\s=] ) # disallow leading whitespace or equals signs
[^ [ / \n \\ ]* # every other thing
(?:
(?: \\[\s\S] # anything escaped
@ -617,7 +617,7 @@ REGEX = /// ^
]
) [^ [ / \n \\ ]*
)*
/ [imgy]{0,4} (?!\w)
/) ([imgy]{0,4}) (?!\w)
///
HEREGEX = /// ^ /{3} ([\s\S]+?) /{3} ([imgy]{0,4}) (?!\w) ///

View File

@ -55,3 +55,9 @@ test "an empty heregex will compile to an empty, non-capturing group", ->
test "#1724: regular expressions beginning with `*`", ->
throws -> CoffeeScript.compile '/// * ///'
test "empty regular expressions with flags", ->
fn = (x) -> x
a = "" + //i
fn ""
eq '/(?:)/i', a