Skip to content

Commit

Permalink
[Yaml] fixed embedded folded string parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Aug 24, 2013
1 parent 2165d5d commit 566d79c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -19,6 +19,8 @@
*/
class Parser
{
const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';

private $offset = 0;
private $lines = array();
private $currentLineNb = -1;
Expand Down Expand Up @@ -304,10 +306,15 @@ private function getNextEmbedBlock($indentation = null)

$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);

// We are in string block (ie. after a line ending with "|")
$removeComments = !preg_match('~(.*)\|[\s]*$~', $this->currentLine);
// Comments must not be removed inside a string block (ie. after a line ending with "|")
$removeCommentsPattern = '~'.self::FOLDED_SCALAR_PATTERN.'$~';
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);

while ($this->moveToNextLine()) {
if ($this->getCurrentLineIndentation() === $newIndent) {
$removeComments = !preg_match($removeCommentsPattern, $this->currentLine);
}

if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
$this->moveToPreviousLine();
break;
Expand Down Expand Up @@ -389,7 +396,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
return $this->refs[$value];
}

if (preg_match('/^(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?$/', $value, $matches)) {
if (preg_match('/^'.self::FOLDED_SCALAR_PATTERN.'$/', $value, $matches)) {
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';

return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));
Expand Down
34 changes: 33 additions & 1 deletion src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -551,7 +551,7 @@ public function testStringBlockWithComments()
));
}

public function testNestedStringBlockWithComments()
public function testFoldedStringBlockWithComments()
{
$this->assertEquals(array(array('content' => <<<EOT
# comment 1
Expand All @@ -576,6 +576,38 @@ public function testNestedStringBlockWithComments()
</body>
footer # comment3
EOF
));
}

public function testNestedFoldedStringBlockWithComments()
{
$this->assertEquals(array(array(
'title' => 'some title',
'content' => <<<EOT
# comment 1
header
# comment 2
<body>
<h1>title</h1>
</body>
footer # comment3
EOT
)), Yaml::parse(<<<EOF
-
title: some title
content: |
# comment 1
header
# comment 2
<body>
<h1>title</h1>
</body>
footer # comment3
EOF
));
}
Expand Down

0 comments on commit 566d79c

Please sign in to comment.