From 6571eb04b3f4f9e98ded588af23e7a2d27fb1728 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Mon, 21 Aug 2023 22:05:22 +0800 Subject: [PATCH] Fix block scalar array parsing --- Parser.php | 7 +++++-- Tests/ParserTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Parser.php b/Parser.php index d8886bb1..e4bacd78 100644 --- a/Parser.php +++ b/Parser.php @@ -199,9 +199,8 @@ private function doParse(string $value, int $flags) || self::preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P.+?))?\s*$#u', $this->trimTag($values['value']), $matches) ) ) { - // this is a compact notation element, add to next block and parse $block = $values['value']; - if ($this->isNextLineIndented()) { + if ($this->isNextLineIndented() || isset($matches['value']) && '>-' === $matches['value']) { $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1); } @@ -949,6 +948,10 @@ private function isNextLineIndented(): bool } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment())); if ($EOF) { + for ($i = 0; $i < $movements; ++$i) { + $this->moveToPreviousLine(); + } + return false; } diff --git a/Tests/ParserTest.php b/Tests/ParserTest.php index 98e5e73e..741a6ad8 100644 --- a/Tests/ParserTest.php +++ b/Tests/ParserTest.php @@ -2690,6 +2690,44 @@ public static function circularReferenceProvider() return $tests; } + public function testBlockScalarArray() + { + $yaml = <<<'YAML' +anyOf: + - $ref: >- + #/string/bar +anyOfMultiline: + - $ref: >- + #/string/bar + second line +nested: + anyOf: + - $ref: >- + #/string/bar +YAML; + $expected = [ + 'anyOf' => [ + 0 => [ + '$ref' => '#/string/bar', + ], + ], + 'anyOfMultiline' => [ + 0 => [ + '$ref' => '#/string/bar second line', + ], + ], + 'nested' => [ + 'anyOf' => [ + 0 => [ + '$ref' => '#/string/bar', + ], + ], + ], + ]; + + $this->assertSame($expected, $this->parser->parse($yaml)); + } + /** * @dataProvider indentedMappingData */