Escapable linebreaks in heregexes

This commit is contained in:
Marc Häfner 2013-10-24 00:36:46 +02:00
parent 91ac3fa031
commit fa76e2dd21
3 changed files with 9 additions and 4 deletions

View File

@ -275,7 +275,7 @@
var body, flags, flagsOffset, heregex, plusToken, prev, re, tag, token, tokens, value, _i, _len, _ref2, _ref3, _ref4;
heregex = match[0], body = match[1], flags = match[2];
if (0 > body.indexOf('#{')) {
re = body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/');
re = this.escapeLines(body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/'), true);
if (re.match(/^\*/)) {
this.error('regular expressions cannot begin with `*`');
}
@ -860,7 +860,7 @@
HEREGEX = /^\/{3}((?:\\?[\s\S])+?)\/{3}([imgy]{0,4})(?!\w)/;
HEREGEX_OMIT = /((?:\\\\)+)|\\([^\n\S]|\/)|\s+(?:#.*)?/g;
HEREGEX_OMIT = /((?:\\\\)+)|\\(\s|\/)|\s+(?:#.*)?/g;
MULTILINER = /\n/g;

View File

@ -255,7 +255,7 @@ exports.Lexer = class Lexer
heregexToken: (match) ->
[heregex, body, flags] = match
if 0 > body.indexOf '#{'
re = body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/')
re = @escapeLines body.replace(HEREGEX_OMIT, '$1$2').replace(/\//g, '\\/'), yes
if re.match /^\*/ then @error 'regular expressions cannot begin with `*`'
@token 'REGEX', "/#{ re or '(?:)' }/#{flags}", 0, heregex.length
return heregex.length
@ -810,7 +810,7 @@ HEREGEX = /// ^ /{3} ((?:\\?[\s\S])+?) /{3} ([imgy]{0,4}) (?!\w) ///
HEREGEX_OMIT = ///
((?:\\\\)+) # consume (and preserve) an even number of backslashes
| \\([^\n\S]|/) # preserve escaped spaces and "de-escape" slashes
| \\(\s|/) # preserve escaped whitespace and "de-escape" slashes
| \s+(?:#.*)? # remove whitespace and comments
///g

View File

@ -72,3 +72,8 @@ test "#2238: don't escape already escaped slashes", ->
test "escaped slashes don't close heregex", ->
eq /// \/// ///.source, /\/\/\//.source
eq /// \\\////.source, /\\\//.source
test "escaped linebreaks", ->
eq /// \n\
\
///.source, /\n\n\n/.source