From a80a61b5c2c2039ec469811b55b11d9ddf38f39d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 23 Feb 2010 12:07:10 +0100 Subject: [PATCH] [Yaml] added support for compact notation (patch from redotheoffice - closes #8082) --- src/Symfony/Components/Yaml/Parser.php | 21 +++++++++++++++++-- .../Yaml/YtsSpecificationExamples.yml | 18 ++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Components/Yaml/Parser.php b/src/Symfony/Components/Yaml/Parser.php index 824a651f55..d6cbaf172d 100644 --- a/src/Symfony/Components/Yaml/Parser.php +++ b/src/Symfony/Components/Yaml/Parser.php @@ -65,7 +65,7 @@ class Parser } $isRef = $isInPlace = $isProcessed = false; - if (preg_match('#^\-(\s+(?P.+?))?\s*$#', $this->currentLine, $values)) + if (preg_match('#^\-((?P\s+)(?P.+?))?\s*$#', $this->currentLine, $values)) { if (isset($values['value']) && preg_match('#^&(?P[^ ]+) *(?P.*)#', $values['value'], $matches)) { @@ -87,13 +87,30 @@ class Parser { $data[] = array($matches[1] => Inline::load($matches[2])); } + elseif (isset($values['leadspaces']) + && ' ' == $values['leadspaces'] + && preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"].*?) *\:(\s+(?P.+?))?\s*$#', $values['value'], $matches)) + { + // this is a compact notation element, add to next block and parse + $c = $this->getRealCurrentLineNb(); + $parser = new Parser($c); + $parser->refs =& $this->refs; + + $block = $values['value']; + if (!$this->isNextLineIndented()) + { + $block .= "\n".$this->getNextEmbedBlock(); + } + + $data[] = $parser->parse($block); + } else { $data[] = $this->parseValue($values['value']); } } } - else if (preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ ].*?) *\:(\s+(?P.+?))?\s*$#', $this->currentLine, $values)) + else if (preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"].*?) *\:(\s+(?P.+?))?\s*$#', $this->currentLine, $values)) { $key = Inline::parseScalar($values['key']); diff --git a/tests/fixtures/Symfony/Components/Yaml/YtsSpecificationExamples.yml b/tests/fixtures/Symfony/Components/Yaml/YtsSpecificationExamples.yml index c975fe5892..a1d300f00e 100644 --- a/tests/fixtures/Symfony/Components/Yaml/YtsSpecificationExamples.yml +++ b/tests/fixtures/Symfony/Components/Yaml/YtsSpecificationExamples.yml @@ -149,7 +149,7 @@ yaml: | hr: 63, avg: 0.288 } -ruby: | +php: | array( 'Mark McGwire' => array( 'hr' => 65, 'avg' => 0.278 ), @@ -286,7 +286,6 @@ syck: | --- test: Sequence key shortcut -todo: true spec: 2.12 yaml: | --- @@ -297,6 +296,21 @@ yaml: | quantity: 4 - item : Big Shoes quantity: 1 +php: | + array ( + array ( + 'item' => 'Super Hoop', + 'quantity' => 1, + ), + array ( + 'item' => 'Basketball', + 'quantity' => 4, + ), + array ( + 'item' => 'Big Shoes', + 'quantity' => 1, + ) + ) perl: | [ { item => 'Super Hoop', quantity => 1 },