Permalink
Browse files

[Yaml] fixed parsing when a mapping is mixed within a sequence and vi…

…ce-versa (closes #4634)
  • Loading branch information...
1 parent 8272d98 commit fa301d6bd6d2538b48fbf668879d6e052ed9caad @fabpot fabpot committed Jul 1, 2012
Showing with 36 additions and 0 deletions.
  1. +11 −0 Parser.php
  2. +25 −0 Tests/ParserTest.php
View
@@ -60,6 +60,7 @@ public function parse($value)
}
$data = array();
+ $context = null;
while ($this->moveToNextLine()) {
if ($this->isCurrentLineEmpty()) {
continue;
@@ -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'];
@@ -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) {
View
@@ -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

0 comments on commit fa301d6

Please sign in to comment.