heregex: now allows bare slashes and empty body

This commit is contained in:
satyr
2010-10-04 11:55:49 +09:00
parent c605b3e232
commit 493fa7d8fe
3 changed files with 8 additions and 5 deletions

View File

@@ -205,11 +205,12 @@
return true; return true;
}; };
Lexer.prototype.heregexToken = function(match) { Lexer.prototype.heregexToken = function(match) {
var _ref2, body, flags, heregex; var _ref2, body, flags, heregex, re;
_ref2 = match, heregex = _ref2[0], body = _ref2[1], flags = _ref2[2]; _ref2 = match, heregex = _ref2[0], body = _ref2[1], flags = _ref2[2];
this.i += heregex.length; this.i += heregex.length;
if (!(~body.indexOf('#{'))) { if (!(~body.indexOf('#{'))) {
this.token('REGEX', '/' + body.replace(HEREGEX_OMIT, '') + '/' + flags); re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/');
this.token('REGEX', "/" + (re || '(?:)') + "/" + (flags));
return true; return true;
} }
this.token('IDENTIFIER', 'RegExp'); this.token('IDENTIFIER', 'RegExp');

View File

@@ -198,7 +198,8 @@ exports.Lexer = class Lexer
[heregex, body, flags] = match [heregex, body, flags] = match
@i += heregex.length @i += heregex.length
unless ~body.indexOf '#{' unless ~body.indexOf '#{'
@token 'REGEX', '/' + body.replace(HEREGEX_OMIT, '') + '/' + flags re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/')
@token 'REGEX', "/#{ re or '(?:)' }/#{flags}"
return true return true
@token 'IDENTIFIER', 'RegExp' @token 'IDENTIFIER', 'RegExp'
@tokens.push ['CALL_START', '('] @tokens.push ['CALL_START', '(']

View File

@@ -27,8 +27,8 @@ ok ' '.match regexp
ok (obj.width()/id - obj.height()/id) is -5 ok (obj.width()/id - obj.height()/id) is -5
eq /^I'm\s+Heregex?/gim + '', /// eq /^I'm\s+Heregex?\/\/\//gim + '', ///
^ I'm \s+ Heregex? # or not ^ I'm \s+ Heregex? / // # or not
///gim + '' ///gim + ''
eq '\\\\#{}', /// eq '\\\\#{}', ///
#{ #{
@@ -36,3 +36,4 @@ eq '\\\\#{}', ///
} }
\#{} \#{}
///.source ///.source
eq /// /// + '', '/(?:)/'