merged branch oscherler/yaml-comment-colon-fix (PR #8824)

This PR was squashed before being merged into the 2.2 branch (closes #8824).

Discussion
----------

[Yaml] Fix comment containing a colon on a scalar line being parsed as a hash.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Note: I only ran the Yaml Component tests.

This PR fixes a bug in YAML parsing where a comment containing a colon followed by a space, like this:

    foo # warning: this is a scalar with a comment, not a hash

was parsed as a hash, resulting in:

    array( 'foo # warning' => 'this is a scalar with a comment, not a hash' )

instead of:

    'foo'

Commits
-------

4563f1b [Yaml] Fix comment containing a colon on a scalar line being parsed as a hash.
This commit is contained in:
Fabien Potencier 2013-08-22 15:12:42 +02:00
commit 228ecb96d0
2 changed files with 15 additions and 1 deletions

View File

@ -111,7 +111,7 @@ class Parser
$data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
}
}
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && false === strpos($values['key'],' #')) {
if ($context && 'sequence' == $context) {
throw new ParseException('You cannot define a mapping item when in a sequence');
}

View File

@ -49,3 +49,17 @@ yaml: |
foo: bar # a comment
php: |
array('foo' => 'bar')
---
test: Comment containing a colon on a hash line
brief: >
Comment containing a colon on a scalar line
yaml: 'foo # comment: this is also part of the comment'
php: |
'foo'
---
test: 'Hash key containing a #'
brief: >
'Hash key containing a #'
yaml: 'foo#bar: baz'
php: |
array('foo#bar' => 'baz')