Improve HEREDOC regexp

* Exclude trailing blank line from the match group
  * Fix backslash handling
This commit is contained in:
Marc Häfner 2013-11-28 16:46:00 +01:00
parent 13f205404c
commit 26200f4640
4 changed files with 7 additions and 8 deletions

View File

@ -193,14 +193,13 @@
};
Lexer.prototype.heredocToken = function() {
var doc, heredoc, match, quote, trimmed;
var doc, heredoc, match, quote;
if (!(match = HEREDOC.exec(this.chunk))) {
return 0;
}
heredoc = match[0];
quote = heredoc.charAt(0);
trimmed = match[2].replace(/(([^\\]|\\\\)\s*)\n[^\n\S]*$/, '$1');
doc = this.sanitizeHeredoc(trimmed, {
doc = this.sanitizeHeredoc(match[2], {
quote: quote,
indent: null
});
@ -847,7 +846,7 @@
NUMBER = /^0b[01]+|^0o[0-7]+|^0x[\da-f]+|^\d*\.?\d+(?:e[+-]?\d+)?/i;
HEREDOC = /^("""|''')(([\s\S]*?([^\\]|\\\\))?)\1/;
HEREDOC = /^("""|''')((?:\\[\s\S]|[^\\])*?)(?:\n[^\n\S]*)?\1/;
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?(\.|::)|\.{2,3})/;

View File

@ -205,9 +205,7 @@ exports.Lexer = class Lexer
return 0 unless match = HEREDOC.exec @chunk
heredoc = match[0]
quote = heredoc.charAt 0
# Trim last newline if it's not escaped
trimmed = match[2].replace /(([^\\]|\\\\)\s*)\n[^\n\S]*$/, '$1'
doc = @sanitizeHeredoc trimmed, quote: quote, indent: null
doc = @sanitizeHeredoc match[2], quote: quote, indent: null
if quote is '"' and 0 <= doc.indexOf '#{'
@interpolateString doc, heredoc: yes, strOffset: 3, lexedLength: heredoc.length
else
@ -772,7 +770,7 @@ NUMBER = ///
^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal
///i
HEREDOC = /// ^ ("""|''') (( [\s\S]*? ([^\\]|\\\\) )?) \1 ///
HEREDOC = /// ^ ("""|''') ((?: \\[\s\S] | [^\\] )*?) (?:\n[^\n\S]*)? \1 ///
OPERATOR = /// ^ (
?: [-=]> # function

View File

@ -70,6 +70,7 @@ test "#1050", ->
test "#1273: escaping quotes at the end of heredocs", ->
cantCompile '"""\\"""' # """\"""
cantCompile '"""\\\\\\"""' # """\\\"""
test "#1106: __proto__ compilation", ->
object = eq

View File

@ -207,6 +207,7 @@ eq '""Hello, World\\""', """
test "#1273, escaping quotes at the end of heredocs.", ->
# """\""" no longer compiles
eq """\\""", '\\'
eq """\\\"""", '\\\"'
a = """
basic heredoc