treat trailing backslashes in multi-line strings

This commit is contained in:
Christian Flothmann 2017-10-03 17:15:12 +02:00
parent b87a395025
commit 80af9b8562
2 changed files with 28 additions and 4 deletions

View File

@ -384,6 +384,7 @@ class Parser
if (0 === $this->currentLineNb) {
$parseError = false;
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = false;
$value = '';
foreach ($this->lines as $line) {
@ -401,13 +402,25 @@ class Parser
if ('' === trim($parsedLine)) {
$value .= "\n";
$previousLineWasNewline = true;
} elseif ($previousLineWasNewline) {
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
$value .= ' ';
}
if ('' !== trim($parsedLine) && '\\' === substr($parsedLine, -1)) {
$value .= ltrim(substr($parsedLine, 0, -1));
} elseif ('' !== trim($parsedLine)) {
$value .= trim($parsedLine);
}
if ('' === trim($parsedLine)) {
$previousLineWasNewline = true;
$previousLineWasTerminatedWithBackslash = false;
} elseif ('\\' === substr($parsedLine, -1)) {
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = true;
} else {
$value .= ' '.trim($parsedLine);
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = false;
}
} catch (ParseException $e) {
$parseError = true;
@ -416,7 +429,7 @@ class Parser
}
if (!$parseError) {
return trim($value);
return Inline::parse(trim($value));
}
}

View File

@ -1543,6 +1543,17 @@ EOT;
$this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml));
}
public function testMultiLineQuotedStringWithTrailingBackslash()
{
$yaml = <<<YAML
foobar:
"foo\
bar"
YAML;
$this->assertSame(array('foobar' => 'foobar'), $this->parser->parse($yaml));
}
public function testParseMultiLineUnquotedString()
{
$yaml = <<<EOT