Skip to content

Commit 28ee818

Browse files
committed
drop comments while lexing unquoted strings
1 parent bf598c9 commit 28ee818

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

Parser.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,18 @@ private function lexInlineQuotedString(int &$cursor = 0): string
11581158
private function lexUnquotedString(int &$cursor): string
11591159
{
11601160
$offset = $cursor;
1161-
$cursor += strcspn($this->currentLine, '[]{},:', $cursor);
1161+
1162+
while ($cursor < strlen($this->currentLine)) {
1163+
if (in_array($this->currentLine[$cursor], ['[', ']', '{', '}', ',', ':'], true)) {
1164+
break;
1165+
}
1166+
1167+
if (\in_array($this->currentLine[$cursor], [' ', "\t"], true) && '#' === ($this->currentLine[$cursor + 1] ?? '')) {
1168+
break;
1169+
}
1170+
1171+
++$cursor;
1172+
}
11621173

11631174
if ($cursor === $offset) {
11641175
throw new ParseException('Malformed unquoted YAML string.');
@@ -1235,7 +1246,7 @@ private function consumeWhitespaces(int &$cursor): bool
12351246
$whitespacesConsumed = 0;
12361247

12371248
do {
1238-
$whitespaceOnlyTokenLength = strspn($this->currentLine, ' ', $cursor);
1249+
$whitespaceOnlyTokenLength = strspn($this->currentLine, " \t", $cursor);
12391250
$whitespacesConsumed += $whitespaceOnlyTokenLength;
12401251
$cursor += $whitespaceOnlyTokenLength;
12411252

Tests/ParserTest.php

+63
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,69 @@ public function testParseMultiLineUnquotedString()
17511751
$this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
17521752
}
17531753

1754+
/**
1755+
* @dataProvider unquotedStringWithTrailingComment
1756+
*/
1757+
public function testParseMultiLineUnquotedStringWithTrailingComment(string $yaml, array $expected)
1758+
{
1759+
$this->assertSame($expected, $this->parser->parse($yaml));
1760+
}
1761+
1762+
public function unquotedStringWithTrailingComment()
1763+
{
1764+
return [
1765+
'comment after comma' => [
1766+
<<<'YAML'
1767+
{
1768+
foo: 3, # comment
1769+
bar: 3
1770+
}
1771+
YAML,
1772+
['foo' => 3, 'bar' => 3],
1773+
],
1774+
'comment after space' => [
1775+
<<<'YAML'
1776+
{
1777+
foo: 3 # comment
1778+
}
1779+
YAML,
1780+
['foo' => 3],
1781+
],
1782+
'comment after space, but missing space after #' => [
1783+
<<<'YAML'
1784+
{
1785+
foo: 3 #comment
1786+
}
1787+
YAML,
1788+
['foo' => 3],
1789+
],
1790+
'comment after tab' => [
1791+
<<<YAML
1792+
{
1793+
foo: 3\t# comment
1794+
}
1795+
YAML,
1796+
['foo' => 3],
1797+
],
1798+
'comment after tab, but missing space after #' => [
1799+
<<<YAML
1800+
{
1801+
foo: 3\t#comment
1802+
}
1803+
YAML,
1804+
['foo' => 3],
1805+
],
1806+
'# in mapping value' => [
1807+
<<<'YAML'
1808+
{
1809+
foo: example.com/#about
1810+
}
1811+
YAML,
1812+
['foo' => 'example.com/#about'],
1813+
],
1814+
];
1815+
}
1816+
17541817
/**
17551818
* @dataProvider escapedQuotationCharactersInQuotedStrings
17561819
*/

0 commit comments

Comments
 (0)