Skip to content

Commit

Permalink
[Yaml] fixed parsing when a mapping is mixed within a sequence and vi…
Browse files Browse the repository at this point in the history
…ce-versa (closes #4634)
  • Loading branch information
fabpot committed Jul 1, 2012
1 parent 8272d98 commit fa301d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Parser.php
Expand Up @@ -60,6 +60,7 @@ public function parse($value)
}

$data = array();
$context = null;
while ($this->moveToNextLine()) {
if ($this->isCurrentLineEmpty()) {
continue;
Expand All @@ -72,6 +73,11 @@ public function parse($value)

$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'];
Expand Down Expand Up @@ -104,6 +110,11 @@ public function parse($value)
}
}
} 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) {
Expand Down
25 changes: 25 additions & 0 deletions Tests/ParserTest.php
Expand Up @@ -160,6 +160,31 @@ public function testUnindentedCollectionException()
$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
Expand Down

0 comments on commit fa301d6

Please sign in to comment.