Debian Codegraph @

vendor/symfony/yaml/Parser.php

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