Skip to content

Commit 76047b6

Browse files
bug #62409 [Yaml] Align unquoted multiline scalar parsing with spec for comments (yoeunes)
This PR was merged into the 6.4 branch. Discussion ---------- [Yaml] Align unquoted multiline scalar parsing with spec for comments | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT This PR is a follow-up to [#62359](#62359 (comment)) based on feedback from `@xabbuh` regarding spec compliance. My previous PR fixed a `ParseException` on blank lines, but it handled comments incorrectly (it **ignored** them inside the scalar). This PR aligns the parser with the YAML spec ([3.2.3.3](https://yaml.org/spec/1.2.2/#3233-comments)): 1. It **keeps the fix** for blank lines (they are correctly preserved). 2. It **changes comment handling**: instead of ignoring an indented comment, the parser now **terminates** the multiline scalar when it encounters one. Commits ------- 7911644 [Yaml] Align unquoted multiline scalar parsing with spec for comments
2 parents 924c675 + 7911644 commit 76047b6

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ private function parseValue(string $value, int $flags, string $context): mixed
775775
}
776776

777777
if ($this->isCurrentLineComment()) {
778-
continue;
778+
break;
779779
}
780780

781781
$lines[] = trim($this->currentLine);

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,26 +1752,23 @@ public function testParseMultiLineUnquotedString()
17521752
}
17531753

17541754
/**
1755-
* @dataProvider getUnquotedMultilineScalarIgnoresCommentsData
1755+
* @dataProvider getUnquotedMultilineScalarHandlesCommentsAndBlanksData
17561756
*/
1757-
public function testUnquotedMultilineScalarIgnoresComments(string $yaml, array $expected)
1757+
public function testUnquotedMultilineScalarHandlesCommentsAndBlanks(string $yaml, array $expected)
17581758
{
17591759
$this->assertSame($expected, $this->parser->parse($yaml));
17601760
}
17611761

1762-
public static function getUnquotedMultilineScalarIgnoresCommentsData()
1762+
public static function getUnquotedMultilineScalarHandlesCommentsAndBlanksData()
17631763
{
1764-
yield 'comments interspersed' => [
1764+
yield 'comments interspersed stops scalar' => [
17651765
<<<YAML
17661766
key: unquoted
1767-
# this comment should be ignored
1768-
next line
1769-
# another comment
1770-
final line
1767+
# this comment terminates
17711768
another_key: works
17721769
YAML,
17731770
[
1774-
'key' => 'unquoted next line final line',
1771+
'key' => 'unquoted',
17751772
'another_key' => 'works',
17761773
],
17771774
];
@@ -1789,17 +1786,16 @@ public static function getUnquotedMultilineScalarIgnoresCommentsData()
17891786
],
17901787
];
17911788

1792-
yield 'blank lines and comments' => [
1789+
yield 'blank lines are preserved and comment stops scalar' => [
17931790
<<<YAML
17941791
key: unquoted
17951792
next line
17961793
1797-
# this comment should be ignored
1798-
final line
1794+
# this comment terminates the scalar
17991795
another_key: works
18001796
YAML,
18011797
[
1802-
'key' => "unquoted next line\nfinal line",
1798+
'key' => 'unquoted next line',
18031799
'another_key' => 'works',
18041800
],
18051801
];
@@ -1818,6 +1814,21 @@ public static function getUnquotedMultilineScalarIgnoresCommentsData()
18181814
];
18191815
}
18201816

1817+
public function testUnquotedMultilineScalarThrowsOnOrphanedLineAfterComment()
1818+
{
1819+
$this->expectException(ParseException::class);
1820+
$this->expectExceptionMessage('Unable to parse at line 3 (near " next line")');
1821+
1822+
$yaml = <<<YAML
1823+
key: unquoted
1824+
# this comment terminates
1825+
next line
1826+
another_key: works
1827+
YAML;
1828+
1829+
$this->parser->parse($yaml);
1830+
}
1831+
18211832
/**
18221833
* @dataProvider unquotedStringWithTrailingComment
18231834
*/

0 commit comments

Comments
 (0)