Skip to content

Commit

Permalink
[Yaml] recognize when a block scalar is left
Browse files Browse the repository at this point in the history
The parser did not recognize when the block scalar was completely parsed
and thus treated following comments as they need to be kept leading to
parse errors on the following lines.
  • Loading branch information
xabbuh committed Dec 28, 2015
1 parent a89fe42 commit 8571810
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
27 changes: 20 additions & 7 deletions src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -303,7 +303,11 @@ private function getCurrentLineIndentation()
private function getNextEmbedBlock($indentation = null, $inSequence = false)
{
$oldLineIndentation = $this->getCurrentLineIndentation();
$insideBlockScalar = $this->isBlockScalarHeader();
$blockScalarIndentations = array();

if ($this->isBlockScalarHeader()) {
$blockScalarIndentations[] = $this->getCurrentLineIndentation();
}

if (!$this->moveToNextLine()) {
return;
Expand Down Expand Up @@ -340,17 +344,26 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)

$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();

if (!$insideBlockScalar) {
$insideBlockScalar = $this->isBlockScalarHeader();
if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
$blockScalarIndentations[] = $this->getCurrentLineIndentation();
}

$previousLineIndentation = $this->getCurrentLineIndentation();

while ($this->moveToNextLine()) {
$indent = $this->getCurrentLineIndentation();

if (!$insideBlockScalar && $indent === $previousLineIndentation) {
$insideBlockScalar = $this->isBlockScalarHeader();
// terminate all block scalars that are more indented than the current line
if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && trim($this->currentLine) !== '') {
foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) {
unset($blockScalarIndentations[$key]);
}
}
}

if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
$blockScalarIndentations[] = $this->getCurrentLineIndentation();
}

$previousLineIndentation = $indent;
Expand All @@ -366,7 +379,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
}

// we ignore "comment" lines only when we are not inside a scalar block
if (!$insideBlockScalar && $this->isCurrentLineComment()) {
if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
continue;
}

Expand Down Expand Up @@ -523,7 +536,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
$previousLineIndented = false;
$previousLineBlank = false;

for ($i = 0; $i < count($blockLines); $i++) {
for ($i = 0; $i < count($blockLines); ++$i) {
if ('' === $blockLines[$i]) {
$text .= "\n";
$previousLineIndented = false;
Expand Down
37 changes: 30 additions & 7 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -737,7 +737,9 @@ public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expec

public function getCommentLikeStringInScalarBlockData()
{
$yaml1 = <<<'EOT'
$tests = array();

$yaml = <<<'EOT'
pages:
-
title: some title
Expand All @@ -752,7 +754,7 @@ public function getCommentLikeStringInScalarBlockData()
footer # comment3
EOT;
$expected1 = array(
$expected = array(
'pages' => array(
array(
'title' => 'some title',
Expand All @@ -771,8 +773,9 @@ public function getCommentLikeStringInScalarBlockData()
),
),
);
$tests[] = array($yaml, $expected);

$yaml2 = <<<'EOT'
$yaml = <<<'EOT'
test: |
foo
# bar
Expand All @@ -787,7 +790,7 @@ public function getCommentLikeStringInScalarBlockData()
# bar
baz
EOT;
$expected2 = array(
$expected = array(
'test' => <<<'EOT'
foo
# bar
Expand All @@ -814,11 +817,31 @@ public function getCommentLikeStringInScalarBlockData()
),
),
);
$tests[] = array($yaml, $expected);

return array(
array($yaml1, $expected1),
array($yaml2, $expected2),
$yaml = <<<EOT
foo:
bar:
scalar-block: >
line1
line2>
baz:
# comment
foobar: ~
EOT;
$expected = array(
'foo' => array(
'bar' => array(
'scalar-block' => 'line1 line2>',
),
'baz' => array(
'foobar' => null,
),
),
);
$tests[] = array($yaml, $expected);

return $tests;
}

public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
Expand Down

0 comments on commit 8571810

Please sign in to comment.