[Yaml] look for colon in parsed inline string

Looking for a colon in an unquoted mapping value can lead to falsely
reported parse errors (e.g. when a comment after the mapping value
contains a colon).
This commit is contained in:
Christian Flothmann 2015-11-29 19:05:35 +01:00
parent 6ff5015d8f
commit 21270585a6
2 changed files with 20 additions and 8 deletions

View File

@ -473,15 +473,17 @@ class Parser
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
}
if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($value, ': ')) {
@trigger_error(sprintf('Using a colon in an unquoted mapping value in line %d is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
// to be thrown in 3.0
// throw new ParseException('A colon cannot be used in an unquoted mapping value.');
}
try {
return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
$parsedValue = Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
@trigger_error(sprintf('Using a colon in an unquoted mapping value in line %d is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
// to be thrown in 3.0
// throw new ParseException('A colon cannot be used in an unquoted mapping value.');
}
return $parsedValue;
} catch (ParseException $e) {
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
$e->setSnippet($this->currentLine);

View File

@ -808,6 +808,16 @@ EOF;
restore_error_handler();
}
public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
{
$yaml = <<<EOT
foo:
bar: foobar # Note: a comment after a colon
EOT;
$this->assertSame(array('foo' => array('bar' => 'foobar')), $this->parser->parse($yaml));
}
}
class B