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;
};
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];
this.i += heregex.length;
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;
}
this.token('IDENTIFIER', 'RegExp');

View File

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

View File

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