[Yaml] fixed parsing when a mapping is mixed within a sequence and vice-versa (closes #4634)

This commit is contained in:
Fabien Potencier 2012-07-01 11:19:53 +02:00
parent 2e356c1ab9
commit a1b73887f7
2 changed files with 36 additions and 0 deletions

View File

@ -60,6 +60,7 @@ class Parser
}
$data = array();
$context = null;
while ($this->moveToNextLine()) {
if ($this->isCurrentLineEmpty()) {
continue;
@ -72,6 +73,11 @@ class Parser
$isRef = $isInPlace = $isProcessed = false;
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
if ($context && 'mapping' == $context) {
throw new ParseException('You cannot define a sequence item when in a mapping');
}
$context = 'sequence';
if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
$isRef = $matches['ref'];
$values['value'] = $matches['value'];
@ -104,6 +110,11 @@ class Parser
}
}
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
if ($context && 'sequence' == $context) {
throw new ParseException('You cannot define a mapping item when in a sequence');
}
$context = 'mapping';
try {
$key = Inline::parseScalar($values['key']);
} catch (ParseException $e) {

View File

@ -160,6 +160,31 @@ EOF;
$this->parser->parse($yaml);
}
/**
* @expectedException Symfony\Component\Yaml\Exception\ParseException
*/
public function testSequenceInAMapping()
{
Yaml::parse(<<<EOF
yaml:
hash: me
- array stuff
EOF
);
}
/**
* @expectedException Symfony\Component\Yaml\Exception\ParseException
*/
public function testMappingInASequence()
{
Yaml::parse(<<<EOF
yaml:
- array stuff
hash: me
EOF
);
}
}
class B