diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index c45ba46b874fb..3a006e4736f02 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -448,7 +448,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false) } // we ignore "comment" lines only when we are not inside a scalar block - if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) { + if (empty($blockScalarIndentations) && $this->isCurrentLineComment() && false === $this->checkIfPreviousNonCommentLineIsCollectionItem()) { continue; } @@ -484,10 +484,18 @@ private function moveToNextLine() /** * Moves the parser to the previous line. + * + * @return bool */ private function moveToPreviousLine() { + if ($this->currentLineNb < 1) { + return false; + } + $this->currentLine = $this->lines[--$this->currentLineNb]; + + return true; } /** @@ -804,4 +812,44 @@ private function isBlockScalarHeader() { return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine); } + + /** + * Returns true if the current line is a collection item. + * + * @return bool + */ + private function isCurrentLineCollectionItem() + { + $ltrimmedLine = ltrim($this->currentLine, ' '); + + return '' !== $ltrimmedLine && '-' === $ltrimmedLine[0]; + } + + /** + * Tests whether the current comment line is in a collection. + * + * @return bool + */ + private function checkIfPreviousNonCommentLineIsCollectionItem() + { + $isCollectionItem = false; + $moves = 0; + while ($this->moveToPreviousLine()) { + ++$moves; + // If previous line is a comment, move back again. + if ($this->isCurrentLineComment()) { + continue; + } + $isCollectionItem = $this->isCurrentLineCollectionItem(); + break; + } + + // Move parser back to previous line. + while ($moves > 0) { + $this->moveToNextLine(); + --$moves; + } + + return $isCollectionItem; + } } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index bddf969744eeb..123a2c657066f 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1201,6 +1201,74 @@ public function getInvalidBinaryData() ), ); } + + /** + * @param $lineNumber + * @param $yaml + * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider + */ + public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml) + { + $this->setExpectedException( + '\Symfony\Component\Yaml\Exception\ParseException', + sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber) + ); + + $this->parser->parse($yaml); + } + + public function parserThrowsExceptionWithCorrectLineNumberProvider() + { + return array( + array( + 4, + <<