1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Yaml;
13
14use Symfony\Component\Yaml\Exception\ParseException;
15use Symfony\Component\Yaml\Tag\TaggedValue;
16
17/**
18 * Parser parses YAML strings to convert them to PHP arrays.
19 *
20 * @author Fabien Potencier <fabien@symfony.com>
21 *
22 * @final
23 */
24class Parser
25{
26 const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
27 const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
28
29 private $filename;
30 private $offset = 0;
31 private $totalNumberOfLines;
32 private $lines = [];
33 private $currentLineNb = -1;
34 private $currentLine = '';
35 private $refs = [];
36 private $skippedLineNumbers = [];
37 private $locallySkippedLineNumbers = [];
38 private $refsBeingParsed = [];
39
40 /**
41 * Parses a YAML file into a PHP value.
42 *
43 * @param string $filename The path to the YAML file to be parsed
44 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
45 *
46 * @return mixed The YAML converted to a PHP value
47 *
48 * @throws ParseException If the file could not be read or the YAML is not valid
49 */
50 public function parseFile(string $filename, int $flags = 0)
51 {
52 if (!is_file($filename)) {
53 throw new ParseException(sprintf('File "%s" does not exist.', $filename));
54 }
55
56 if (!is_readable($filename)) {
57 throw new ParseException(sprintf('File "%s" cannot be read.', $filename));
58 }
59
60 $this->filename = $filename;
61
62 try {
63 return $this->parse(file_get_contents($filename), $flags);
64 } finally {
65 $this->filename = null;
66 }
67 }
68
69 /**
70 * Parses a YAML string to a PHP value.
71 *
72 * @param string $value A YAML string
73 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
74 *
75 * @return mixed A PHP value
76 *
77 * @throws ParseException If the YAML is not valid
78 */
79 public function parse(string $value, int $flags = 0)
80 {
81 if (false === preg_match('//u', $value)) {
82 throw new ParseException('The YAML value does not appear to be valid UTF-8.', -1, null, $this->filename);
83 }
84
85 $this->refs = [];
86
87 $mbEncoding = null;
88
89 if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
90 $mbEncoding = mb_internal_encoding();
91 mb_internal_encoding('UTF-8');
92 }
93
94 try {
95 $data = $this->doParse($value, $flags);
96 } finally {
97 if (null !== $mbEncoding) {
98 mb_internal_encoding($mbEncoding);
99 }
100 $this->lines = [];
101 $this->currentLine = '';
102 $this->refs = [];
103 $this->skippedLineNumbers = [];
104 $this->locallySkippedLineNumbers = [];
105 }
106
107 return $data;
108 }
109
110 private function doParse(string $value, int $flags)
111 {
112 $this->currentLineNb = -1;
113 $this->currentLine = '';
114 $value = $this->cleanup($value);
115 $this->lines = explode("\n", $value);
116 $this->locallySkippedLineNumbers = [];
117
118 if (null === $this->totalNumberOfLines) {
119 $this->totalNumberOfLines = \count($this->lines);
120 }
121
122 if (!$this->moveToNextLine()) {
123 return null;
124 }
125
126 $data = [];
127 $context = null;
128 $allowOverwrite = false;
129
130 while ($this->isCurrentLineEmpty()) {
131 if (!$this->moveToNextLine()) {
132 return null;
133 }
134 }
135
136 // Resolves the tag and returns if end of the document
137 if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) {
138 return new TaggedValue($tag, '');
139 }
140
141 do {
142 if ($this->isCurrentLineEmpty()) {
143 continue;
144 }
145
146 // tab?
147 if ("\t" === $this->currentLine[0]) {
148 throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
149 }
150
151 Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename);
152
153 $isRef = $mergeNode = false;
154 if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
155 if ($context && 'mapping' == $context) {
156 throw new ParseException('You cannot define a sequence item when in a mapping.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
157 }
158 $context = 'sequence';
159
160 if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
161 $isRef = $matches['ref'];
162 $this->refsBeingParsed[] = $isRef;
163 $values['value'] = $matches['value'];
164 }
165
166 if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) {
167 throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
168 }
169
170 // array
171 if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
172 $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
173 } elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
174 $data[] = new TaggedValue(
175 $subTag,
176 $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
177 );
178 } else {
179 if (isset($values['leadspaces'])
180 && self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
181 ) {
182 // this is a compact notation element, add to next block and parse
183 $block = $values['value'];
184 if ($this->isNextLineIndented()) {
185 $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1);
186 }
187
188 $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags);
189 } else {
190 $data[] = $this->parseValue($values['value'], $flags, $context);
191 }
192 }
193 if ($isRef) {
194 $this->refs[$isRef] = end($data);
195 array_pop($this->refsBeingParsed);
196 }
197 } elseif (
198 self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
199 && (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
200 ) {
201 if ($context && 'sequence' == $context) {
202 throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
203 }
204 $context = 'mapping';
205
206 try {
207 $key = Inline::parseScalar($values['key']);
208 } catch (ParseException $e) {
209 $e->setParsedLine($this->getRealCurrentLineNb() + 1);
210 $e->setSnippet($this->currentLine);
211
212 throw $e;
213 }
214
215 if (!\is_string($key) && !\is_int($key)) {
216 throw new ParseException(sprintf('%s keys are not supported. Quote your evaluable mapping keys instead.', is_numeric($key) ? 'Numeric' : 'Non-string'), $this->getRealCurrentLineNb() + 1, $this->currentLine);
217 }
218
219 // Convert float keys to strings, to avoid being converted to integers by PHP
220 if (\is_float($key)) {
221 $key = (string) $key;
222 }
223
224 if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
225 $mergeNode = true;
226 $allowOverwrite = true;
227 if (isset($values['value'][0]) && '*' === $values['value'][0]) {
228 $refName = substr(rtrim($values['value']), 1);
229 if (!\array_key_exists($refName, $this->refs)) {
230 if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) {
231 throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $refName, $refName), $this->currentLineNb + 1, $this->currentLine, $this->filename);
232 }
233
234 throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
235 }
236
237 $refValue = $this->refs[$refName];
238
239 if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $refValue instanceof \stdClass) {
240 $refValue = (array) $refValue;
241 }
242
243 if (!\is_array($refValue)) {
244 throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
245 }
246
247 $data += $refValue; // array union
248 } else {
249 if (isset($values['value']) && '' !== $values['value']) {
250 $value = $values['value'];
251 } else {
252 $value = $this->getNextEmbedBlock();
253 }
254 $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags);
255
256 if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsed instanceof \stdClass) {
257 $parsed = (array) $parsed;
258 }
259
260 if (!\is_array($parsed)) {
261 throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
262 }
263
264 if (isset($parsed[0])) {
265 // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
266 // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
267 // in the sequence override keys specified in later mapping nodes.
268 foreach ($parsed as $parsedItem) {
269 if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsedItem instanceof \stdClass) {
270 $parsedItem = (array) $parsedItem;
271 }
272
273 if (!\is_array($parsedItem)) {
274 throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem, $this->filename);
275 }
276
277 $data += $parsedItem; // array union
278 }
279 } else {
280 // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
281 // current mapping, unless the key already exists in it.
282 $data += $parsed; // array union
283 }
284 }
285 } elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
286 $isRef = $matches['ref'];
287 $this->refsBeingParsed[] = $isRef;
288 $values['value'] = $matches['value'];
289 }
290
291 $subTag = null;
292 if ($mergeNode) {
293 // Merge keys
294 } elseif (!isset($values['value']) || '' === $values['value'] || 0 === strpos($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
295 // hash
296 // if next line is less indented or equal, then it means that the current value is null
297 if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
298 // Spec: Keys MUST be unique; first one wins.
299 // But overwriting is allowed when a merge node is used in current block.
300 if ($allowOverwrite || !isset($data[$key])) {
301 if (null !== $subTag) {
302 $data[$key] = new TaggedValue($subTag, '');
303 } else {
304 $data[$key] = null;
305 }
306 } else {
307 throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
308 }
309 } else {
310 // remember the parsed line number here in case we need it to provide some contexts in error messages below
311 $realCurrentLineNbKey = $this->getRealCurrentLineNb();
312 $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
313 if ('<<' === $key) {
314 $this->refs[$refMatches['ref']] = $value;
315
316 if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) {
317 $value = (array) $value;
318 }
319
320 $data += $value;
321 } elseif ($allowOverwrite || !isset($data[$key])) {
322 // Spec: Keys MUST be unique; first one wins.
323 // But overwriting is allowed when a merge node is used in current block.
324 if (null !== $subTag) {
325 $data[$key] = new TaggedValue($subTag, $value);
326 } else {
327 $data[$key] = $value;
328 }
329 } else {
330 throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $realCurrentLineNbKey + 1, $this->currentLine);
331 }
332 }
333 } else {
334 $value = $this->parseValue(rtrim($values['value']), $flags, $context);
335 // Spec: Keys MUST be unique; first one wins.
336 // But overwriting is allowed when a merge node is used in current block.
337 if ($allowOverwrite || !isset($data[$key])) {
338 $data[$key] = $value;
339 } else {
340 throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine);
341 }
342 }
343 if ($isRef) {
344 $this->refs[$isRef] = $data[$key];
345 array_pop($this->refsBeingParsed);
346 }
347 } elseif ('"' === $this->currentLine[0] || "'" === $this->currentLine[0]) {
348 if (null !== $context) {
349 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
350 }
351
352 try {
353 return Inline::parse($this->parseQuotedString($this->currentLine), $flags, $this->refs);
354 } catch (ParseException $e) {
355 $e->setParsedLine($this->getRealCurrentLineNb() + 1);
356 $e->setSnippet($this->currentLine);
357
358 throw $e;
359 }
360 } elseif ('{' === $this->currentLine[0]) {
361 if (null !== $context) {
362 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
363 }
364
365 try {
366 $parsedMapping = Inline::parse($this->lexInlineMapping($this->currentLine), $flags, $this->refs);
367
368 while ($this->moveToNextLine()) {
369 if (!$this->isCurrentLineEmpty()) {
370 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
371 }
372 }
373
374 return $parsedMapping;
375 } catch (ParseException $e) {
376 $e->setParsedLine($this->getRealCurrentLineNb() + 1);
377 $e->setSnippet($this->currentLine);
378
379 throw $e;
380 }
381 } elseif ('[' === $this->currentLine[0]) {
382 if (null !== $context) {
383 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
384 }
385
386 try {
387 $parsedSequence = Inline::parse($this->lexInlineSequence($this->currentLine), $flags, $this->refs);
388
389 while ($this->moveToNextLine()) {
390 if (!$this->isCurrentLineEmpty()) {
391 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
392 }
393 }
394
395 return $parsedSequence;
396 } catch (ParseException $e) {
397 $e->setParsedLine($this->getRealCurrentLineNb() + 1);
398 $e->setSnippet($this->currentLine);
399
400 throw $e;
401 }
402 } else {
403 // multiple documents are not supported
404 if ('---' === $this->currentLine) {
405 throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
406 }
407
408 if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) {
409 throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
410 }
411
412 // 1-liner optionally followed by newline(s)
413 if (\is_string($value) && $this->lines[0] === trim($value)) {
414 try {
415 $value = Inline::parse($this->lines[0], $flags, $this->refs);
416 } catch (ParseException $e) {
417 $e->setParsedLine($this->getRealCurrentLineNb() + 1);
418 $e->setSnippet($this->currentLine);
419
420 throw $e;
421 }
422
423 return $value;
424 }
425
426 // try to parse the value as a multi-line string as a last resort
427 if (0 === $this->currentLineNb) {
428 $previousLineWasNewline = false;
429 $previousLineWasTerminatedWithBackslash = false;
430 $value = '';
431
432 foreach ($this->lines as $line) {
433 if ('' !== ltrim($line) && '#' === ltrim($line)[0]) {
434 continue;
435 }
436 // If the indentation is not consistent at offset 0, it is to be considered as a ParseError
437 if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) {
438 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
439 }
440
441 if (false !== strpos($line, ': ')) {
442 throw new ParseException('Mapping values are not allowed in multi-line blocks.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
443 }
444
445 if ('' === trim($line)) {
446 $value .= "\n";
447 } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
448 $value .= ' ';
449 }
450
451 if ('' !== trim($line) && '\\' === substr($line, -1)) {
452 $value .= ltrim(substr($line, 0, -1));
453 } elseif ('' !== trim($line)) {
454 $value .= trim($line);
455 }
456
457 if ('' === trim($line)) {
458 $previousLineWasNewline = true;
459 $previousLineWasTerminatedWithBackslash = false;
460 } elseif ('\\' === substr($line, -1)) {
461 $previousLineWasNewline = false;
462 $previousLineWasTerminatedWithBackslash = true;
463 } else {
464 $previousLineWasNewline = false;
465 $previousLineWasTerminatedWithBackslash = false;
466 }
467 }
468
469 try {
470 return Inline::parse(trim($value));
471 } catch (ParseException $e) {
472 // fall-through to the ParseException thrown below
473 }
474 }
475
476 throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
477 }
478 } while ($this->moveToNextLine());
479
480 if (null !== $tag) {
481 $data = new TaggedValue($tag, $data);
482 }
483
484 if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !\is_object($data) && 'mapping' === $context) {
485 $object = new \stdClass();
486
487 foreach ($data as $key => $value) {
488 $object->$key = $value;
489 }
490
491 $data = $object;
492 }
493
494 return empty($data) ? null : $data;
495 }
496
497 private function parseBlock(int $offset, string $yaml, int $flags)
498 {
499 $skippedLineNumbers = $this->skippedLineNumbers;
500
501 foreach ($this->locallySkippedLineNumbers as $lineNumber) {
502 if ($lineNumber < $offset) {
503 continue;
504 }
505
506 $skippedLineNumbers[] = $lineNumber;
507 }
508
509 $parser = new self();
510 $parser->offset = $offset;
511 $parser->totalNumberOfLines = $this->totalNumberOfLines;
512 $parser->skippedLineNumbers = $skippedLineNumbers;
513 $parser->refs = &$this->refs;
514 $parser->refsBeingParsed = $this->refsBeingParsed;
515
516 return $parser->doParse($yaml, $flags);
517 }
518
519 /**
520 * Returns the current line number (takes the offset into account).
521 *
522 * @internal
523 *
524 * @return int The current line number
525 */
526 public function getRealCurrentLineNb(): int
527 {
528 $realCurrentLineNumber = $this->currentLineNb + $this->offset;
529
530 foreach ($this->skippedLineNumbers as $skippedLineNumber) {
531 if ($skippedLineNumber > $realCurrentLineNumber) {
532 break;
533 }
534
535 ++$realCurrentLineNumber;
536 }
537
538 return $realCurrentLineNumber;
539 }
540
541 /**
542 * Returns the current line indentation.
543 *
544 * @return int The current line indentation
545 */
546 private function getCurrentLineIndentation(): int
547 {
548 return \strlen($this->currentLine) - \strlen(ltrim($this->currentLine, ' '));
549 }
550
551 /**
552 * Returns the next embed block of YAML.
553 *
554 * @param int|null $indentation The indent level at which the block is to be read, or null for default
555 * @param bool $inSequence True if the enclosing data structure is a sequence
556 *
557 * @return string A YAML string
558 *
559 * @throws ParseException When indentation problem are detected
560 */
561 private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string
562 {
563 $oldLineIndentation = $this->getCurrentLineIndentation();
564
565 if (!$this->moveToNextLine()) {
566 return '';
567 }
568
569 if (null === $indentation) {
570 $newIndent = null;
571 $movements = 0;
572
573 do {
574 $EOF = false;
575
576 // empty and comment-like lines do not influence the indentation depth
577 if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
578 $EOF = !$this->moveToNextLine();
579
580 if (!$EOF) {
581 ++$movements;
582 }
583 } else {
584 $newIndent = $this->getCurrentLineIndentation();
585 }
586 } while (!$EOF && null === $newIndent);
587
588 for ($i = 0; $i < $movements; ++$i) {
589 $this->moveToPreviousLine();
590 }
591
592 $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
593
594 if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
595 throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
596 }
597 } else {
598 $newIndent = $indentation;
599 }
600
601 $data = [];
602 if ($this->getCurrentLineIndentation() >= $newIndent) {
603 $data[] = substr($this->currentLine, $newIndent);
604 } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
605 $data[] = $this->currentLine;
606 } else {
607 $this->moveToPreviousLine();
608
609 return '';
610 }
611
612 if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
613 // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
614 // and therefore no nested list or mapping
615 $this->moveToPreviousLine();
616
617 return '';
618 }
619
620 $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
621
622 while ($this->moveToNextLine()) {
623 $indent = $this->getCurrentLineIndentation();
624
625 if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
626 $this->moveToPreviousLine();
627 break;
628 }
629
630 if ($this->isCurrentLineBlank()) {
631 $data[] = substr($this->currentLine, $newIndent);
632 continue;
633 }
634
635 if ($indent >= $newIndent) {
636 $data[] = substr($this->currentLine, $newIndent);
637 } elseif ($this->isCurrentLineComment()) {
638 $data[] = $this->currentLine;
639 } elseif (0 == $indent) {
640 $this->moveToPreviousLine();
641
642 break;
643 } else {
644 throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
645 }
646 }
647
648 return implode("\n", $data);
649 }
650
651 /**
652 * Moves the parser to the next line.
653 */
654 private function moveToNextLine(): bool
655 {
656 if ($this->currentLineNb >= \count($this->lines) - 1) {
657 return false;
658 }
659
660 $this->currentLine = $this->lines[++$this->currentLineNb];
661
662 return true;
663 }
664
665 /**
666 * Moves the parser to the previous line.
667 */
668 private function moveToPreviousLine(): bool
669 {
670 if ($this->currentLineNb < 1) {
671 return false;
672 }
673
674 $this->currentLine = $this->lines[--$this->currentLineNb];
675
676 return true;
677 }
678
679 /**
680 * Parses a YAML value.
681 *
682 * @param string $value A YAML value
683 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
684 * @param string $context The parser context (either sequence or mapping)
685 *
686 * @return mixed A PHP value
687 *
688 * @throws ParseException When reference does not exist
689 */
690 private function parseValue(string $value, int $flags, string $context)
691 {
692 if (0 === strpos($value, '*')) {
693 if (false !== $pos = strpos($value, '#')) {
694 $value = substr($value, 1, $pos - 2);
695 } else {
696 $value = substr($value, 1);
697 }
698
699 if (!\array_key_exists($value, $this->refs)) {
700 if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) {
701 throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $value, $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
702 }
703
704 throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
705 }
706
707 return $this->refs[$value];
708 }
709
710 if (\in_array($value[0], ['!', '|', '>'], true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
711 $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
712
713 $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs((int) $modifiers));
714
715 if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
716 if ('!!binary' === $matches['tag']) {
717 return Inline::evaluateBinaryScalar($data);
718 }
719
720 return new TaggedValue(substr($matches['tag'], 1), $data);
721 }
722
723 return $data;
724 }
725
726 try {
727 if ('' !== $value && '{' === $value[0]) {
728 return Inline::parse($this->lexInlineMapping($value), $flags, $this->refs);
729 } elseif ('' !== $value && '[' === $value[0]) {
730 return Inline::parse($this->lexInlineSequence($value), $flags, $this->refs);
731 }
732
733 $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;
734
735 // do not take following lines into account when the current line is a quoted single line value
736 if (null !== $quotation && self::preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
737 return Inline::parse($value, $flags, $this->refs);
738 }
739
740 $lines = [];
741
742 while ($this->moveToNextLine()) {
743 // unquoted strings end before the first unindented line
744 if (null === $quotation && 0 === $this->getCurrentLineIndentation()) {
745 $this->moveToPreviousLine();
746
747 break;
748 }
749
750 $lines[] = trim($this->currentLine);
751
752 // quoted string values end with a line that is terminated with the quotation character
753 if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
754 break;
755 }
756 }
757
758 for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
759 if ('' === $lines[$i]) {
760 $value .= "\n";
761 $previousLineBlank = true;
762 } elseif ($previousLineBlank) {
763 $value .= $lines[$i];
764 $previousLineBlank = false;
765 } else {
766 $value .= ' '.$lines[$i];
767 $previousLineBlank = false;
768 }
769 }
770
771 Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
772
773 $parsedValue = Inline::parse($value, $flags, $this->refs);
774
775 if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
776 throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename);
777 }
778
779 return $parsedValue;
780 } catch (ParseException $e) {
781 $e->setParsedLine($this->getRealCurrentLineNb() + 1);
782 $e->setSnippet($this->currentLine);
783
784 throw $e;
785 }
786 }
787
788 /**
789 * Parses a block scalar.
790 *
791 * @param string $style The style indicator that was used to begin this block scalar (| or >)
792 * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
793 * @param int $indentation The indentation indicator that was used to begin this block scalar
794 */
795 private function parseBlockScalar(string $style, string $chomping = '', int $indentation = 0): string
796 {
797 $notEOF = $this->moveToNextLine();
798 if (!$notEOF) {
799 return '';
800 }
801
802 $isCurrentLineBlank = $this->isCurrentLineBlank();
803 $blockLines = [];
804
805 // leading blank lines are consumed before determining indentation
806 while ($notEOF && $isCurrentLineBlank) {
807 // newline only if not EOF
808 if ($notEOF = $this->moveToNextLine()) {
809 $blockLines[] = '';
810 $isCurrentLineBlank = $this->isCurrentLineBlank();
811 }
812 }
813
814 // determine indentation if not specified
815 if (0 === $indentation) {
816 $currentLineLength = \strlen($this->currentLine);
817
818 for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) {
819 ++$indentation;
820 }
821 }
822
823 if ($indentation > 0) {
824 $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
825
826 while (
827 $notEOF && (
828 $isCurrentLineBlank ||
829 self::preg_match($pattern, $this->currentLine, $matches)
830 )
831 ) {
832 if ($isCurrentLineBlank && \strlen($this->currentLine) > $indentation) {
833 $blockLines[] = substr($this->currentLine, $indentation);
834 } elseif ($isCurrentLineBlank) {
835 $blockLines[] = '';
836 } else {
837 $blockLines[] = $matches[1];
838 }
839
840 // newline only if not EOF
841 if ($notEOF = $this->moveToNextLine()) {
842 $isCurrentLineBlank = $this->isCurrentLineBlank();
843 }
844 }
845 } elseif ($notEOF) {
846 $blockLines[] = '';
847 }
848
849 if ($notEOF) {
850 $blockLines[] = '';
851 $this->moveToPreviousLine();
852 } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
853 $blockLines[] = '';
854 }
855
856 // folded style
857 if ('>' === $style) {
858 $text = '';
859 $previousLineIndented = false;
860 $previousLineBlank = false;
861
862 for ($i = 0, $blockLinesCount = \count($blockLines); $i < $blockLinesCount; ++$i) {
863 if ('' === $blockLines[$i]) {
864 $text .= "\n";
865 $previousLineIndented = false;
866 $previousLineBlank = true;
867 } elseif (' ' === $blockLines[$i][0]) {
868 $text .= "\n".$blockLines[$i];
869 $previousLineIndented = true;
870 $previousLineBlank = false;
871 } elseif ($previousLineIndented) {
872 $text .= "\n".$blockLines[$i];
873 $previousLineIndented = false;
874 $previousLineBlank = false;
875 } elseif ($previousLineBlank || 0 === $i) {
876 $text .= $blockLines[$i];
877 $previousLineIndented = false;
878 $previousLineBlank = false;
879 } else {
880 $text .= ' '.$blockLines[$i];
881 $previousLineIndented = false;
882 $previousLineBlank = false;
883 }
884 }
885 } else {
886 $text = implode("\n", $blockLines);
887 }
888
889 // deal with trailing newlines
890 if ('' === $chomping) {
891 $text = preg_replace('/\n+$/', "\n", $text);
892 } elseif ('-' === $chomping) {
893 $text = preg_replace('/\n+$/', '', $text);
894 }
895
896 return $text;
897 }
898
899 /**
900 * Returns true if the next line is indented.
901 *
902 * @return bool Returns true if the next line is indented, false otherwise
903 */
904 private function isNextLineIndented(): bool
905 {
906 $currentIndentation = $this->getCurrentLineIndentation();
907 $movements = 0;
908
909 do {
910 $EOF = !$this->moveToNextLine();
911
912 if (!$EOF) {
913 ++$movements;
914 }
915 } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()));
916
917 if ($EOF) {
918 return false;
919 }
920
921 $ret = $this->getCurrentLineIndentation() > $currentIndentation;
922
923 for ($i = 0; $i < $movements; ++$i) {
924 $this->moveToPreviousLine();
925 }
926
927 return $ret;
928 }
929
930 /**
931 * Returns true if the current line is blank or if it is a comment line.
932 *
933 * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise
934 */
935 private function isCurrentLineEmpty(): bool
936 {
937 return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
938 }
939
940 /**
941 * Returns true if the current line is blank.
942 *
943 * @return bool Returns true if the current line is blank, false otherwise
944 */
945 private function isCurrentLineBlank(): bool
946 {
947 return '' == trim($this->currentLine, ' ');
948 }
949
950 /**
951 * Returns true if the current line is a comment line.
952 *
953 * @return bool Returns true if the current line is a comment line, false otherwise
954 */
955 private function isCurrentLineComment(): bool
956 {
957 //checking explicitly the first char of the trim is faster than loops or strpos
958 $ltrimmedLine = ltrim($this->currentLine, ' ');
959
960 return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0];
961 }
962
963 private function isCurrentLineLastLineInDocument(): bool
964 {
965 return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
966 }
967
968 /**
969 * Cleanups a YAML string to be parsed.
970 *
971 * @param string $value The input YAML string
972 *
973 * @return string A cleaned up YAML string
974 */
975 private function cleanup(string $value): string
976 {
977 $value = str_replace(["\r\n", "\r"], "\n", $value);
978
979 // strip YAML header
980 $count = 0;
981 $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
982 $this->offset += $count;
983
984 // remove leading comments
985 $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
986 if (1 === $count) {
987 // items have been removed, update the offset
988 $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
989 $value = $trimmedValue;
990 }
991
992 // remove start of the document marker (---)
993 $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
994 if (1 === $count) {
995 // items have been removed, update the offset
996 $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
997 $value = $trimmedValue;
998
999 // remove end of the document marker (...)
1000 $value = preg_replace('#\.\.\.\s*$#', '', $value);
1001 }
1002
1003 return $value;
1004 }
1005
1006 /**
1007 * Returns true if the next line starts unindented collection.
1008 *
1009 * @return bool Returns true if the next line starts unindented collection, false otherwise
1010 */
1011 private function isNextLineUnIndentedCollection(): bool
1012 {
1013 $currentIndentation = $this->getCurrentLineIndentation();
1014 $movements = 0;
1015
1016 do {
1017 $EOF = !$this->moveToNextLine();
1018
1019 if (!$EOF) {
1020 ++$movements;
1021 }
1022 } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()));
1023
1024 if ($EOF) {
1025 return false;
1026 }
1027
1028 $ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem();
1029
1030 for ($i = 0; $i < $movements; ++$i) {
1031 $this->moveToPreviousLine();
1032 }
1033
1034 return $ret;
1035 }
1036
1037 /**
1038 * Returns true if the string is un-indented collection item.
1039 *
1040 * @return bool Returns true if the string is un-indented collection item, false otherwise
1041 */
1042 private function isStringUnIndentedCollectionItem(): bool
1043 {
1044 return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
1045 }
1046
1047 /**
1048 * A local wrapper for "preg_match" which will throw a ParseException if there
1049 * is an internal error in the PCRE engine.
1050 *
1051 * This avoids us needing to check for "false" every time PCRE is used
1052 * in the YAML engine
1053 *
1054 * @throws ParseException on a PCRE internal error
1055 *
1056 * @see preg_last_error()
1057 *
1058 * @internal
1059 */
1060 public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int
1061 {
1062 if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
1063 switch (preg_last_error()) {
1064 case PREG_INTERNAL_ERROR:
1065 $error = 'Internal PCRE error.';
1066 break;
1067 case PREG_BACKTRACK_LIMIT_ERROR:
1068 $error = 'pcre.backtrack_limit reached.';
1069 break;
1070 case PREG_RECURSION_LIMIT_ERROR:
1071 $error = 'pcre.recursion_limit reached.';
1072 break;
1073 case PREG_BAD_UTF8_ERROR:
1074 $error = 'Malformed UTF-8 data.';
1075 break;
1076 case PREG_BAD_UTF8_OFFSET_ERROR:
1077 $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
1078 break;
1079 default:
1080 $error = 'Error.';
1081 }
1082
1083 throw new ParseException($error);
1084 }
1085
1086 return $ret;
1087 }
1088
1089 /**
1090 * Trim the tag on top of the value.
1091 *
1092 * Prevent values such as "!foo {quz: bar}" to be considered as
1093 * a mapping block.
1094 */
1095 private function trimTag(string $value): string
1096 {
1097 if ('!' === $value[0]) {
1098 return ltrim(substr($value, 1, strcspn($value, " \r\n", 1)), ' ');
1099 }
1100
1101 return $value;
1102 }
1103
1104 private function getLineTag(string $value, int $flags, bool $nextLineCheck = true): ?string
1105 {
1106 if ('' === $value || '!' !== $value[0] || 1 !== self::preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) {
1107 return null;
1108 }
1109
1110 if ($nextLineCheck && !$this->isNextLineIndented()) {
1111 return null;
1112 }
1113
1114 $tag = substr($matches['tag'], 1);
1115
1116 // Built-in tags
1117 if ($tag && '!' === $tag[0]) {
1118 throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), $this->getRealCurrentLineNb() + 1, $value, $this->filename);
1119 }
1120
1121 if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
1122 return $tag;
1123 }
1124
1125 throw new ParseException(sprintf('Tags support is not enabled. You must use the flag "Yaml::PARSE_CUSTOM_TAGS" to use "%s".', $matches['tag']), $this->getRealCurrentLineNb() + 1, $value, $this->filename);
1126 }
1127
1128 private function parseQuotedString(string $yaml): ?string
1129 {
1130 if ('' === $yaml || ('"' !== $yaml[0] && "'" !== $yaml[0])) {
1131 throw new \InvalidArgumentException(sprintf('"%s" is not a quoted string.', $yaml));
1132 }
1133
1134 $lines = [$yaml];
1135
1136 while ($this->moveToNextLine()) {
1137 $lines[] = $this->currentLine;
1138
1139 if (!$this->isCurrentLineEmpty() && $yaml[0] === $this->currentLine[-1]) {
1140 break;
1141 }
1142 }
1143
1144 $value = '';
1145
1146 for ($i = 0, $linesCount = \count($lines), $previousLineWasNewline = false, $previousLineWasTerminatedWithBackslash = false; $i < $linesCount; ++$i) {
1147 if ('' === trim($lines[$i])) {
1148 $value .= "\n";
1149 } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
1150 $value .= ' ';
1151 }
1152
1153 if ('' !== trim($lines[$i]) && '\\' === substr($lines[$i], -1)) {
1154 $value .= ltrim(substr($lines[$i], 0, -1));
1155 } elseif ('' !== trim($lines[$i])) {
1156 $value .= trim($lines[$i]);
1157 }
1158
1159 if ('' === trim($lines[$i])) {
1160 $previousLineWasNewline = true;
1161 $previousLineWasTerminatedWithBackslash = false;
1162 } elseif ('\\' === substr($lines[$i], -1)) {
1163 $previousLineWasNewline = false;
1164 $previousLineWasTerminatedWithBackslash = true;
1165 } else {
1166 $previousLineWasNewline = false;
1167 $previousLineWasTerminatedWithBackslash = false;
1168 }
1169 }
1170
1171 return $value;
1172
1173 for ($i = 1; isset($yaml[$i]) && $quotation !== $yaml[$i]; ++$i) {
1174 }
1175
1176 // quoted single line string
1177 if (isset($yaml[$i]) && $quotation === $yaml[$i]) {
1178 return $yaml;
1179 }
1180
1181 $lines = [$yaml];
1182
1183 while ($this->moveToNextLine()) {
1184 for ($i = 1; isset($this->currentLine[$i]) && $quotation !== $this->currentLine[$i]; ++$i) {
1185 }
1186
1187 $lines[] = trim($this->currentLine);
1188
1189 if (isset($this->currentLine[$i]) && $quotation === $this->currentLine[$i]) {
1190 break;
1191 }
1192 }
1193 }
1194
1195 private function lexInlineMapping(string $yaml): string
1196 {
1197 if ('' === $yaml || '{' !== $yaml[0]) {
1198 throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml));
1199 }
1200
1201 for ($i = 1; isset($yaml[$i]) && '}' !== $yaml[$i]; ++$i) {
1202 }
1203
1204 if (isset($yaml[$i]) && '}' === $yaml[$i]) {
1205 return $yaml;
1206 }
1207
1208 $lines = [$yaml];
1209
1210 while ($this->moveToNextLine()) {
1211 $lines[] = $this->currentLine;
1212 }
1213
1214 return implode("\n", $lines);
1215 }
1216
1217 private function lexInlineSequence(string $yaml): string
1218 {
1219 if ('' === $yaml || '[' !== $yaml[0]) {
1220 throw new \InvalidArgumentException(sprintf('"%s" is not a sequence.', $yaml));
1221 }
1222
1223 for ($i = 1; isset($yaml[$i]) && ']' !== $yaml[$i]; ++$i) {
1224 }
1225
1226 if (isset($yaml[$i]) && ']' === $yaml[$i]) {
1227 return $yaml;
1228 }
1229
1230 $value = $yaml;
1231
1232 while ($this->moveToNextLine()) {
1233 for ($i = 1; isset($this->currentLine[$i]) && ']' !== $this->currentLine[$i]; ++$i) {
1234 }
1235
1236 $value .= trim($this->currentLine);
1237
1238 if (isset($this->currentLine[$i]) && ']' === $this->currentLine[$i]) {
1239 break;
1240 }
1241 }
1242
1243 return $value;
1244 }
1245}
1246