From 88d210f9a6078c20e8be9e8d6a62ff70b901010e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Par=C3=A1da=20J=C3=B3zsef?= Date: Tue, 9 Feb 2016 00:53:32 +0100 Subject: [PATCH 1/7] [Yaml] Fix wrong line number when comments are inserted in the middle of a block. --- .../Component/Yaml/Tests/ParserTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index bddf969744eeb..d5c76b98ec81c 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1201,6 +1201,46 @@ 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, + << Date: Thu, 11 Feb 2016 01:48:23 +0100 Subject: [PATCH 2/7] [Yaml] Fix wrong line number when comments are inserted in the middle of a block. --- src/Symfony/Component/Yaml/Parser.php | 3 ++- src/Symfony/Component/Yaml/Tests/ParserTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index c45ba46b874fb..890456c9bea80 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -28,6 +28,7 @@ class Parser private $currentLineNb = -1; private $currentLine = ''; private $refs = array(); + private $skippedCommentLines = 0; /** * Constructor. @@ -154,7 +155,7 @@ public function parse($value, $flags = 0) try { $key = Inline::parseScalar($values['key']); } catch (ParseException $e) { - $e->setParsedLine($this->getRealCurrentLineNb() + 1); + $e->setParsedLine($this->getRealCurrentLineNb() + $this->skippedCommentLines + 1); $e->setSnippet($this->currentLine); throw $e; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index d5c76b98ec81c..93ca1c8301733 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1237,6 +1237,19 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider() # bar # bar bar: "123", +YAML + ), + array( + 8, + << Date: Sat, 13 Feb 2016 00:58:57 +0100 Subject: [PATCH 3/7] [Yaml] Fix wrong line number when comments are inserted in the middle of a block. --- src/Symfony/Component/Yaml/Parser.php | 51 ++++++++++++++++++- .../Component/Yaml/Tests/ParserTest.php | 15 ++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 890456c9bea80..4a33d355c7b14 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -28,7 +28,6 @@ class Parser private $currentLineNb = -1; private $currentLine = ''; private $refs = array(); - private $skippedCommentLines = 0; /** * Constructor. @@ -155,7 +154,7 @@ public function parse($value, $flags = 0) try { $key = Inline::parseScalar($values['key']); } catch (ParseException $e) { - $e->setParsedLine($this->getRealCurrentLineNb() + $this->skippedCommentLines + 1); + $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; @@ -485,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; } /** @@ -805,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 Returns true if the current line is a collection item line, false otherwise. + */ + private function isCurrentLineCollectionItem() + { + $ltrimmedLine = ltrim($this->currentLine, ' '); + + return '' !== $ltrimmedLine && $ltrimmedLine[0] === '-'; + } + + /** + * Tests whether or not the current comment line is in a collection. + * + * @return bool + */ + private function isPreviousNonCommentLineIsCollectionItem() + { + $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 93ca1c8301733..123a2c657066f 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -1250,6 +1250,21 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider() - # bar bar: "123", +YAML + ), + array( + 10, + << Date: Sat, 13 Feb 2016 01:08:30 +0100 Subject: [PATCH 4/7] [Yaml] Fix wrong line number when comments are inserted in the middle of a block. --- src/Symfony/Component/Yaml/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 4a33d355c7b14..de80eec887871 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->isPreviousNonCommentLineIsCollectionItem()) { continue; } From 343ee3eb3120be9c7306d3d3c73bf409c0da16b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Par=C3=A1da=20J=C3=B3zsef?= Date: Sat, 13 Feb 2016 01:29:53 +0100 Subject: [PATCH 5/7] [Yaml] Fix wrong line number when comments are inserted in the middle of a block. --- src/Symfony/Component/Yaml/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index de80eec887871..f709fc8f7e024 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -822,7 +822,7 @@ private function isCurrentLineCollectionItem() { $ltrimmedLine = ltrim($this->currentLine, ' '); - return '' !== $ltrimmedLine && $ltrimmedLine[0] === '-'; + return '' !== $ltrimmedLine && '-' === $ltrimmedLine[0]; } /** From bf4501a535237dceeafa4b8042fbfdd6f61b237a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Par=C3=A1da=20J=C3=B3zsef?= Date: Sat, 30 Apr 2016 08:16:51 +0200 Subject: [PATCH 6/7] [Yaml] Fix comment and change method name --- src/Symfony/Component/Yaml/Parser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index f709fc8f7e024..43a8386c1a1b4 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() && false === $this->isPreviousNonCommentLineIsCollectionItem()) { + if (empty($blockScalarIndentations) && $this->isCurrentLineComment() && false === $this->checkIfPreviousNonCommentLineIsCollectionItem()) { continue; } @@ -826,11 +826,11 @@ private function isCurrentLineCollectionItem() } /** - * Tests whether or not the current comment line is in a collection. + * Tests whether the current comment line is in a collection. * * @return bool */ - private function isPreviousNonCommentLineIsCollectionItem() + private function checkIfPreviousNonCommentLineIsCollectionItem() { $isCollectionItem = false; $moves = 0; From c8b34a76c2d43b33b938b1edb2e4c10cda03cb5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Par=C3=A1da=20J=C3=B3zsef?= Date: Wed, 11 May 2016 08:41:34 +0200 Subject: [PATCH 7/7] Coding standard + unnecessary comment fixes. --- src/Symfony/Component/Yaml/Parser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 43a8386c1a1b4..3a006e4736f02 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -816,7 +816,7 @@ private function isBlockScalarHeader() /** * Returns true if the current line is a collection item. * - * @return bool Returns true if the current line is a collection item line, false otherwise. + * @return bool */ private function isCurrentLineCollectionItem() { @@ -834,7 +834,7 @@ private function checkIfPreviousNonCommentLineIsCollectionItem() { $isCollectionItem = false; $moves = 0; - while($this->moveToPreviousLine()) { + while ($this->moveToPreviousLine()) { ++$moves; // If previous line is a comment, move back again. if ($this->isCurrentLineComment()) { @@ -845,7 +845,7 @@ private function checkIfPreviousNonCommentLineIsCollectionItem() } // Move parser back to previous line. - while($moves > 0) { + while ($moves > 0) { $this->moveToNextLine(); --$moves; }