8000 Add spaces support in mapping keys + implicit values support · symfony/symfony@51b87f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 51b87f7

Browse files
committed
Add spaces support in mapping keys + implicit values support
1 parent 87f0361 commit 51b87f7

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ public static function parseScalar(StringReader $reader, &$flags = 0, &$delimite
415415
if (1 === strspn($scalar, '%', 0, 1)) {
416416
@trigger_error(sprintf('Not quoting the scalar "%s" starting with the "%s" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', $scalar, $scalar[0]), E_USER_DEPRECATED);
417417
}
418+
$scalar = trim($scalar, "\t ");
418419

419420
if ($evaluate) {
420421
return self::evaluateScalar($scalar, $flags);
@@ -472,21 +473,22 @@ private static function parseMapping(StringReader $reader, &$flags, &$references
472473
{
473474
$mapping = array();
474475
self::parseStructure($reader, '}', function () use ($reader, &$mapping, $flags, $references) {
475-
$key = self::parseValue($reader, $flags, '},: ', false, $references);
476+
$key = self::parseValue($reader, $flags, '},:', false, $references);
476477
// @todo deprecate using special values without
477478
// non-specific tag (false, true, null, ...)
478479
if (!is_string($key) && !is_int($key)) {
479480
throw new ParseException(sprintf('Mapping keys must be strings or integers, type "%s" provided.', gettype($key)));
480481
}
481-
$reader->readSpan(' ');
482-
$reader->expectChar(':');
482+
if ($reader->readChar(':')) {
483+
if (!$reader->readChar(' ')) {
484+
@trigger_error('Omitting the space after the colon that follows a mapping key definition is deprecated since version 3.2 and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
485+
}
483486

484-
if (!$reader->readChar(' ')) {
485-
@trigger_error('Omitting the space after the colon that follows a mapping key definition is deprecated since version 3.2 and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
487+
$value = self::parseValue($reader, $flags, '},', true, $references);
488+
} else {
489+
$value = null;
486490
}
487491

488-
$value = self::parseValue($reader, $flags, '},', true, $references);
489-
490492
if (array_key_exists($key, $mapping)) {
491493
@trigger_error(sprintf('Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
492494
} else {

src/Symfony/Component/Yaml/Parser.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ public function parse($value, $flags = 0)
116116
}
117117

118118
$isRef = $mergeNode = false;
119-
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
119+
if (preg_match('#\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#Au', $this->currentLine, $values)) {
120120
if ($context && 'mapping' == $context) {
121121
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
122122
}
123123
$context = 'sequence';
124124

125-
if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
125+
if (isset($values['value']) && preg_match('#&(?P<ref>[^ ]+) *(?P<value>.*)#Au', $values['value'], $matches)) {
126126
$isRef = $matches['ref'];
127127
$values['value'] = $matches['value'];
128128
}
@@ -132,7 +132,7 @@ public function parse($value, $flags = 0)
132132
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags);
133133
} else {
134134
if (isset($values['leadspaces'])
135-
&& preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
135+
&& preg_match('#(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#Au', $values['value'], $matches)
136136
) {
137137
// this is a compact notation element, add to next block and parse
138138
$block = $values['value'];
@@ -148,7 +148,7 @@ public function parse($value, $flags = 0)
148148
if ($isRef) {
149149
$this->refs[$isRef] = end($data);
150150
}
151-
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
151+
} elseif (preg_match('#(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#Au', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
152152
if ($context && 'sequence' == $context) {
153153
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
154154
}
@@ -228,7 +228,7 @@ public function parse($value, $flags = 0)
228228
}
229229
}
230230
}
231-
} elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
231+
} elseif (isset($values['value']) && preg_match('#&(?P<ref>[^ ]+) *(?P<value>.*)#Au', $values['value'], $matches)) {
232232
$isRef = $matches['ref'];
233233
$values['value'] = $matches['value'];
234234
}
@@ -571,7 +571,7 @@ private function parseValue($value, $flags, $context)
571571
return $this->refs[$value];
572572
}
573573

574-
if (preg_match('/^'.self::TAG_PATTERN.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
574+
if (preg_match('/'.self::TAG_PATTERN.self::BLOCK_SCALAR_HEADER_PATTERN.'$/Au', $value, $matches)) {
575575
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
576576

577577
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
@@ -630,7 +630,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
630630

631631
// determine indentation if not specified
632632
if (0 === $indentation) {
633-
if (preg_match('/^ +/', $this->currentLine, $matches)) {
633+
if (preg_match('/ +/A', $this->currentLine, $matches)) {
634634
$indentation = strlen($matches[0]);
635635
}
636636
}
@@ -851,7 +851,7 @@ private function isNextLineUnIndentedCollection()
851851
*/
852852
private function isStringUnIndentedCollectionItem()
853853
{
854-
return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
854+
return $this->currentLine && '-' === $this->currentLine[0];
855855
}
856856

857857
/**

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ public function getTestsForParseWithMapObjects()
490490
array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
491491
array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
492492

493+
// mapping with implicit values
494+
array('{fo o, bar, q uz : bar}', (object) array('fo o' => null, 'bar' => null, 'q uz' => 'bar')),
495+
493496
// nested sequences and mappings
494497
array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
495498
array('[foo, {bar: foo}]', array('foo', (object) array('bar' => 'foo'))),

0 commit comments

Comments
 (0)
0