bug #36683 [Yaml] fix parse error when unindented collections contain a comment (wdiesveld)

This PR was squashed before being merged into the 3.4 branch.

Discussion
----------

[Yaml] fix parse error when unindented collections contain a comment

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36558
| License       | MIT

### Problem
The method `Parser::getNextEmbedBlock` did not determine the yaml-block correctly when there was a comment before the first unindented collection item. This was caused by the fact that the check for unindented collection items was done for the _first line of the block only_. So in case this first line is a comment, this check will result in _false_, while in fact the parser is in an unindented collection.

### Solution
In the solution I implemented the parser will check for comment lines as well. As long as the loop encounters a comment line, it will check (in the next iteration) whether the line is an unindented collection item. So this check will be done until all comments before the first uncommented item are parsed.

Commits
-------

58bb2c52ac [Yaml] fix parse error when unindented collections contain a comment
This commit is contained in:
Fabien Potencier 2020-05-04 14:50:49 +02:00
commit bb77914a26
2 changed files with 20 additions and 0 deletions

View File

@ -619,8 +619,14 @@ class Parser
}
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
$isItComment = $this->isCurrentLineComment();
while ($this->moveToNextLine()) {
if ($isItComment && !$isItUnindentedCollection) {
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
$isItComment = $this->isCurrentLineComment();
}
$indent = $this->getCurrentLineIndentation();
if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {

View File

@ -74,3 +74,17 @@ yaml: |
'foo #': baz
php: |
['foo #' => 'baz']
---
test: Comment before first item in unindented collection
brief: >
Comment directly before unindented collection is allowed
yaml: |
collection1:
# comment
- a
- b
collection2:
- a
- b
php: |
['collection1' => ['a', 'b'], 'collection2' => ['a', 'b']]