From bc9bbd8436be8d4cb893b2571e934436fa498285 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 24 Mar 2010 15:02:49 +0100 Subject: [PATCH] [Yaml] added support for compact inline notation with immediate sub mapping --- src/Symfony/Components/Yaml/Parser.php | 25 +-- .../Symfony/Components/Yaml/index.yml | 1 + .../Symfony/Components/Yaml/sfCompact.yml | 159 ++++++++++++++++++ 3 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 tests/fixtures/Symfony/Components/Yaml/sfCompact.yml diff --git a/src/Symfony/Components/Yaml/Parser.php b/src/Symfony/Components/Yaml/Parser.php index 3873c6b4d396..330f9a1ef61e 100644 --- a/src/Symfony/Components/Yaml/Parser.php +++ b/src/Symfony/Components/Yaml/Parser.php @@ -83,11 +83,7 @@ public function parse($value) } else { - if (preg_match('/^([^ ]+)\: +({.*?)$/', $values['value'], $matches)) - { - $data[] = array($matches[1] => Inline::load($matches[2])); - } - elseif (isset($values['leadspaces']) + if (isset($values['leadspaces']) && ' ' == $values['leadspaces'] && preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{].*?) *\:(\s+(?P.+?))?\s*$#', $values['value'], $matches)) { @@ -99,7 +95,7 @@ public function parse($value) $block = $values['value']; if (!$this->isNextLineIndented()) { - $block .= "\n".$this->getNextEmbedBlock(); + $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + 2); } $data[] = $parser->parse($block); @@ -283,17 +279,26 @@ protected function getCurrentLineIndentation() /** * Returns the next embed block of YAML. * + * @param integer $indentation The indent level at which the block is to be read, or null for default + * * @return string A YAML string */ - protected function getNextEmbedBlock() + protected function getNextEmbedBlock($indentation = null) { $this->moveToNextLine(); - $newIndent = $this->getCurrentLineIndentation(); + if (null === $indentation) + { + $newIndent = $this->getCurrentLineIndentation(); - if (!$this->isCurrentLineEmpty() && 0 == $newIndent) + if (!$this->isCurrentLineEmpty() && 0 == $newIndent) + { + throw new ParserException(sprintf('Indentation problem at line %d (%s)', $this->getRealCurrentLineNb() + 1, $this->currentLine)); + } + } + else { - throw new ParserException(sprintf('Indentation problem at line %d (%s)', $this->getRealCurrentLineNb() + 1, $this->currentLine)); + $newIndent = $indentation; } $data = array(substr($this->currentLine, $newIndent)); diff --git a/tests/fixtures/Symfony/Components/Yaml/index.yml b/tests/fixtures/Symfony/Components/Yaml/index.yml index e9735d6f4c22..dea165fe1c7d 100644 --- a/tests/fixtures/Symfony/Components/Yaml/index.yml +++ b/tests/fixtures/Symfony/Components/Yaml/index.yml @@ -1,4 +1,5 @@ - sfComments +- sfCompact - sfTests - sfObjects - sfMergeKey diff --git a/tests/fixtures/Symfony/Components/Yaml/sfCompact.yml b/tests/fixtures/Symfony/Components/Yaml/sfCompact.yml new file mode 100644 index 000000000000..c49f31aaf3aa --- /dev/null +++ b/tests/fixtures/Symfony/Components/Yaml/sfCompact.yml @@ -0,0 +1,159 @@ +--- %YAML:1.0 +test: Compact notation +brief: | + Compact notation for sets of mappings with single element +yaml: | + --- + # products purchased + - item : Super Hoop + - item : Basketball + quantity: 1 + - item: + name: Big Shoes + nick: Biggies + quantity: 1 +php: | + array ( + array ( + 'item' => 'Super Hoop', + ), + array ( + 'item' => 'Basketball', + 'quantity' => 1, + ), + array ( + 'item' => array( + 'name' => 'Big Shoes', + 'nick' => 'Biggies' + ), + 'quantity' => 1 + ) + ) +--- +test: Compact notation combined with inline notation +brief: | + Combinations of compact and inline notation are allowed +yaml: | + --- + items: + - { item: Super Hoop, quantity: 1 } + - [ Basketball, Big Shoes ] +php: | + array ( + 'items' => array ( + array ( + 'item' => 'Super Hoop', + 'quantity' => 1, + ), + array ( + 'Basketball', + 'Big Shoes' + ) + ) + ) +--- %YAML:1.0 +test: Compact notation +brief: | + Compact notation for sets of mappings with single element +yaml: | + --- + # products purchased + - item : Super Hoop + - item : Basketball + quantity: 1 + - item: + name: Big Shoes + nick: Biggies + quantity: 1 +php: | + array ( + array ( + 'item' => 'Super Hoop', + ), + array ( + 'item' => 'Basketball', + 'quantity' => 1, + ), + array ( + 'item' => array( + 'name' => 'Big Shoes', + 'nick' => 'Biggies' + ), + 'quantity' => 1 + ) + ) +--- +test: Compact notation combined with inline notation +brief: | + Combinations of compact and inline notation are allowed +yaml: | + --- + items: + - { item: Super Hoop, quantity: 1 } + - [ Basketball, Big Shoes ] +php: | + array ( + 'items' => array ( + array ( + 'item' => 'Super Hoop', + 'quantity' => 1, + ), + array ( + 'Basketball', + 'Big Shoes' + ) + ) + ) +--- %YAML:1.0 +test: Compact notation +brief: | + Compact notation for sets of mappings with single element +yaml: | + --- + # products purchased + - item : Super Hoop + - item : Basketball + quantity: 1 + - item: + name: Big Shoes + nick: Biggies + quantity: 1 +php: | + array ( + array ( + 'item' => 'Super Hoop', + ), + array ( + 'item' => 'Basketball', + 'quantity' => 1, + ), + array ( + 'item' => array( + 'name' => 'Big Shoes', + 'nick' => 'Biggies' + ), + 'quantity' => 1 + ) + ) +--- +test: Compact notation combined with inline notation +brief: | + Combinations of compact and inline notation are allowed +yaml: | + --- + items: + - { item: Super Hoop, quantity: 1 } + - [ Basketball, Big Shoes ] +php: | + array ( + 'items' => array ( + array ( + 'item' => 'Super Hoop', + 'quantity' => 1, + ), + array ( + 'Basketball', + 'Big Shoes' + ) + ) + )