updated-packages
This commit is contained in:
454
vendor/symfony/yaml/Parser.php
vendored
454
vendor/symfony/yaml/Parser.php
vendored
@@ -23,18 +23,20 @@ use Symfony\Component\Yaml\Tag\TaggedValue;
|
||||
*/
|
||||
class Parser
|
||||
{
|
||||
const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
|
||||
const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
|
||||
public const TAG_PATTERN = '(?P<tag>![\w!.\/:-]+)';
|
||||
public const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
|
||||
public const REFERENCE_PATTERN = '#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u';
|
||||
|
||||
private $filename;
|
||||
private $offset = 0;
|
||||
private $totalNumberOfLines;
|
||||
private $lines = array();
|
||||
private $lines = [];
|
||||
private $currentLineNb = -1;
|
||||
private $currentLine = '';
|
||||
private $refs = array();
|
||||
private $skippedLineNumbers = array();
|
||||
private $locallySkippedLineNumbers = array();
|
||||
private $refs = [];
|
||||
private $skippedLineNumbers = [];
|
||||
private $locallySkippedLineNumbers = [];
|
||||
private $refsBeingParsed = [];
|
||||
|
||||
/**
|
||||
* Parses a YAML file into a PHP value.
|
||||
@@ -81,12 +83,11 @@ class Parser
|
||||
throw new ParseException('The YAML value does not appear to be valid UTF-8.', -1, null, $this->filename);
|
||||
}
|
||||
|
||||
$this->refs = array();
|
||||
$this->refs = [];
|
||||
|
||||
$mbEncoding = null;
|
||||
$data = null;
|
||||
|
||||
if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
|
||||
if (2 /* MB_OVERLOAD_STRING */ & (int) \ini_get('mbstring.func_overload')) {
|
||||
$mbEncoding = mb_internal_encoding();
|
||||
mb_internal_encoding('UTF-8');
|
||||
}
|
||||
@@ -97,33 +98,26 @@ class Parser
|
||||
if (null !== $mbEncoding) {
|
||||
mb_internal_encoding($mbEncoding);
|
||||
}
|
||||
$this->lines = array();
|
||||
$this->refsBeingParsed = [];
|
||||
$this->offset = 0;
|
||||
$this->lines = [];
|
||||
$this->currentLine = '';
|
||||
$this->refs = array();
|
||||
$this->skippedLineNumbers = array();
|
||||
$this->locallySkippedLineNumbers = array();
|
||||
$this->refs = [];
|
||||
$this->skippedLineNumbers = [];
|
||||
$this->locallySkippedLineNumbers = [];
|
||||
$this->totalNumberOfLines = null;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastLineNumberBeforeDeprecation(): int
|
||||
{
|
||||
return $this->getRealCurrentLineNb();
|
||||
}
|
||||
|
||||
private function doParse(string $value, int $flags)
|
||||
{
|
||||
$this->currentLineNb = -1;
|
||||
$this->currentLine = '';
|
||||
$value = $this->cleanup($value);
|
||||
$this->lines = explode("\n", $value);
|
||||
$this->locallySkippedLineNumbers = array();
|
||||
$this->locallySkippedLineNumbers = [];
|
||||
|
||||
if (null === $this->totalNumberOfLines) {
|
||||
$this->totalNumberOfLines = \count($this->lines);
|
||||
@@ -133,7 +127,7 @@ class Parser
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$data = [];
|
||||
$context = null;
|
||||
$allowOverwrite = false;
|
||||
|
||||
@@ -163,12 +157,13 @@ class Parser
|
||||
$isRef = $mergeNode = false;
|
||||
if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
|
||||
if ($context && 'mapping' == $context) {
|
||||
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
throw new ParseException('You cannot define a sequence item when in a mapping.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
$context = 'sequence';
|
||||
|
||||
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
|
||||
if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
|
||||
$isRef = $matches['ref'];
|
||||
$this->refsBeingParsed[] = $isRef;
|
||||
$values['value'] = $matches['value'];
|
||||
}
|
||||
|
||||
@@ -177,7 +172,16 @@ class Parser
|
||||
}
|
||||
|
||||
// array
|
||||
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
|
||||
if (isset($values['value']) && 0 === strpos(ltrim($values['value'], ' '), '-')) {
|
||||
// Inline first child
|
||||
$currentLineNumber = $this->getRealCurrentLineNb();
|
||||
|
||||
$sequenceIndentation = \strlen($values['leadspaces']) + 1;
|
||||
$sequenceYaml = substr($this->currentLine, $sequenceIndentation);
|
||||
$sequenceYaml .= "\n".$this->getNextEmbedBlock($sequenceIndentation, true);
|
||||
|
||||
$data[] = $this->parseBlock($currentLineNumber, rtrim($sequenceYaml), $flags);
|
||||
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
|
||||
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
|
||||
} elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
|
||||
$data[] = new TaggedValue(
|
||||
@@ -185,8 +189,12 @@ class Parser
|
||||
$this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags)
|
||||
);
|
||||
} else {
|
||||
if (isset($values['leadspaces'])
|
||||
&& self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
|
||||
if (
|
||||
isset($values['leadspaces'])
|
||||
&& (
|
||||
'!' === $values['value'][0]
|
||||
|| self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
|
||||
)
|
||||
) {
|
||||
// this is a compact notation element, add to next block and parse
|
||||
$block = $values['value'];
|
||||
@@ -201,13 +209,14 @@ class Parser
|
||||
}
|
||||
if ($isRef) {
|
||||
$this->refs[$isRef] = end($data);
|
||||
array_pop($this->refsBeingParsed);
|
||||
}
|
||||
} elseif (
|
||||
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
|
||||
&& (false === strpos($values['key'], ' #') || \in_array($values['key'][0], array('"', "'")))
|
||||
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
|
||||
&& (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
|
||||
) {
|
||||
if ($context && 'sequence' == $context) {
|
||||
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine, $this->filename);
|
||||
throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
$context = 'mapping';
|
||||
|
||||
@@ -221,7 +230,7 @@ class Parser
|
||||
}
|
||||
|
||||
if (!\is_string($key) && !\is_int($key)) {
|
||||
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);
|
||||
throw new ParseException((is_numeric($key) ? 'Numeric' : 'Non-string').' keys are not supported. Quote your evaluable mapping keys instead.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
|
||||
}
|
||||
|
||||
// Convert float keys to strings, to avoid being converted to integers by PHP
|
||||
@@ -234,7 +243,11 @@ class Parser
|
||||
$allowOverwrite = true;
|
||||
if (isset($values['value'][0]) && '*' === $values['value'][0]) {
|
||||
$refName = substr(rtrim($values['value']), 1);
|
||||
if (!array_key_exists($refName, $this->refs)) {
|
||||
if (!\array_key_exists($refName, $this->refs)) {
|
||||
if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) {
|
||||
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);
|
||||
}
|
||||
|
||||
throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
|
||||
@@ -286,8 +299,9 @@ class Parser
|
||||
$data += $parsed; // array union
|
||||
}
|
||||
}
|
||||
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match('#^&(?P<ref>[^ ]++) *+(?P<value>.*)#u', $values['value'], $matches)) {
|
||||
} elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) {
|
||||
$isRef = $matches['ref'];
|
||||
$this->refsBeingParsed[] = $isRef;
|
||||
$values['value'] = $matches['value'];
|
||||
}
|
||||
|
||||
@@ -345,6 +359,62 @@ class Parser
|
||||
}
|
||||
if ($isRef) {
|
||||
$this->refs[$isRef] = $data[$key];
|
||||
array_pop($this->refsBeingParsed);
|
||||
}
|
||||
} elseif ('"' === $this->currentLine[0] || "'" === $this->currentLine[0]) {
|
||||
if (null !== $context) {
|
||||
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
|
||||
try {
|
||||
return Inline::parse($this->lexInlineQuotedString(), $flags, $this->refs);
|
||||
} catch (ParseException $e) {
|
||||
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
|
||||
$e->setSnippet($this->currentLine);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
} elseif ('{' === $this->currentLine[0]) {
|
||||
if (null !== $context) {
|
||||
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
|
||||
try {
|
||||
$parsedMapping = Inline::parse($this->lexInlineMapping(), $flags, $this->refs);
|
||||
|
||||
while ($this->moveToNextLine()) {
|
||||
if (!$this->isCurrentLineEmpty()) {
|
||||
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
}
|
||||
|
||||
return $parsedMapping;
|
||||
} catch (ParseException $e) {
|
||||
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
|
||||
$e->setSnippet($this->currentLine);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
} elseif ('[' === $this->currentLine[0]) {
|
||||
if (null !== $context) {
|
||||
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
|
||||
try {
|
||||
$parsedSequence = Inline::parse($this->lexInlineSequence(), $flags, $this->refs);
|
||||
|
||||
while ($this->moveToNextLine()) {
|
||||
if (!$this->isCurrentLineEmpty()) {
|
||||
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
}
|
||||
|
||||
return $parsedSequence;
|
||||
} catch (ParseException $e) {
|
||||
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
|
||||
$e->setSnippet($this->currentLine);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
} else {
|
||||
// multiple documents are not supported
|
||||
@@ -377,10 +447,18 @@ class Parser
|
||||
$value = '';
|
||||
|
||||
foreach ($this->lines as $line) {
|
||||
if ('' !== ltrim($line) && '#' === ltrim($line)[0]) {
|
||||
continue;
|
||||
}
|
||||
// If the indentation is not consistent at offset 0, it is to be considered as a ParseError
|
||||
if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) {
|
||||
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
|
||||
if (false !== strpos($line, ': ')) {
|
||||
@trigger_error('Support for mapping keys in multi-line blocks is deprecated since Symfony 4.3 and will throw a ParseException in 5.0.', \E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
if ('' === trim($line)) {
|
||||
$value .= "\n";
|
||||
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
|
||||
@@ -450,6 +528,7 @@ class Parser
|
||||
$parser->totalNumberOfLines = $this->totalNumberOfLines;
|
||||
$parser->skippedLineNumbers = $skippedLineNumbers;
|
||||
$parser->refs = &$this->refs;
|
||||
$parser->refsBeingParsed = $this->refsBeingParsed;
|
||||
|
||||
return $parser->doParse($yaml, $flags);
|
||||
}
|
||||
@@ -496,12 +575,12 @@ class Parser
|
||||
*
|
||||
* @throws ParseException When indentation problem are detected
|
||||
*/
|
||||
private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): ?string
|
||||
private function getNextEmbedBlock(int $indentation = null, bool $inSequence = false): string
|
||||
{
|
||||
$oldLineIndentation = $this->getCurrentLineIndentation();
|
||||
|
||||
if (!$this->moveToNextLine()) {
|
||||
return null;
|
||||
return '';
|
||||
}
|
||||
|
||||
if (null === $indentation) {
|
||||
@@ -536,15 +615,16 @@ class Parser
|
||||
$newIndent = $indentation;
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$data = [];
|
||||
|
||||
if ($this->getCurrentLineIndentation() >= $newIndent) {
|
||||
$data[] = substr($this->currentLine, $newIndent);
|
||||
$data[] = substr($this->currentLine, $newIndent ?? 0);
|
||||
} elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) {
|
||||
$data[] = $this->currentLine;
|
||||
} else {
|
||||
$this->moveToPreviousLine();
|
||||
|
||||
return null;
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
|
||||
@@ -552,12 +632,18 @@ class Parser
|
||||
// and therefore no nested list or mapping
|
||||
$this->moveToPreviousLine();
|
||||
|
||||
return null;
|
||||
return '';
|
||||
}
|
||||
|
||||
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
|
||||
$isItComment = $this->isCurrentLineComment();
|
||||
|
||||
while ($this->moveToNextLine()) {
|
||||
if ($isItComment && !$isItUnindentedCollection) {
|
||||
$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
|
||||
$isItComment = $this->isCurrentLineComment();
|
||||
}
|
||||
|
||||
$indent = $this->getCurrentLineIndentation();
|
||||
|
||||
if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
|
||||
@@ -586,10 +672,13 @@ class Parser
|
||||
return implode("\n", $data);
|
||||
}
|
||||
|
||||
private function hasMoreLines(): bool
|
||||
{
|
||||
return (\count($this->lines) - 1) > $this->currentLineNb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the parser to the next line.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function moveToNextLine(): bool
|
||||
{
|
||||
@@ -604,8 +693,6 @@ class Parser
|
||||
|
||||
/**
|
||||
* Moves the parser to the previous line.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function moveToPreviousLine(): bool
|
||||
{
|
||||
@@ -638,17 +725,21 @@ class Parser
|
||||
$value = substr($value, 1);
|
||||
}
|
||||
|
||||
if (!array_key_exists($value, $this->refs)) {
|
||||
if (!\array_key_exists($value, $this->refs)) {
|
||||
if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) {
|
||||
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);
|
||||
}
|
||||
|
||||
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename);
|
||||
}
|
||||
|
||||
return $this->refs[$value];
|
||||
}
|
||||
|
||||
if (\in_array($value[0], array('!', '|', '>'), true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
|
||||
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
|
||||
if (\in_array($value[0], ['!', '|', '>'], true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
|
||||
$modifiers = $matches['modifiers'] ?? '';
|
||||
|
||||
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
|
||||
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), abs((int) $modifiers));
|
||||
|
||||
if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
|
||||
if ('!!binary' === $matches['tag']) {
|
||||
@@ -662,53 +753,64 @@ class Parser
|
||||
}
|
||||
|
||||
try {
|
||||
$quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;
|
||||
if ('' !== $value && '{' === $value[0]) {
|
||||
$cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
|
||||
|
||||
// do not take following lines into account when the current line is a quoted single line value
|
||||
if (null !== $quotation && self::preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
|
||||
return Inline::parse($value, $flags, $this->refs);
|
||||
return Inline::parse($this->lexInlineMapping($cursor), $flags, $this->refs);
|
||||
} elseif ('' !== $value && '[' === $value[0]) {
|
||||
$cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
|
||||
|
||||
return Inline::parse($this->lexInlineSequence($cursor), $flags, $this->refs);
|
||||
}
|
||||
|
||||
$lines = array();
|
||||
switch ($value[0] ?? '') {
|
||||
case '"':
|
||||
case "'":
|
||||
$cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
|
||||
$parsedValue = Inline::parse($this->lexInlineQuotedString($cursor), $flags, $this->refs);
|
||||
|
||||
while ($this->moveToNextLine()) {
|
||||
// unquoted strings end before the first unindented line
|
||||
if (null === $quotation && 0 === $this->getCurrentLineIndentation()) {
|
||||
$this->moveToPreviousLine();
|
||||
if (isset($this->currentLine[$cursor]) && preg_replace('/\s*(#.*)?$/A', '', substr($this->currentLine, $cursor))) {
|
||||
throw new ParseException(sprintf('Unexpected characters near "%s".', substr($this->currentLine, $cursor)));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
return $parsedValue;
|
||||
default:
|
||||
$lines = [];
|
||||
|
||||
$lines[] = trim($this->currentLine);
|
||||
while ($this->moveToNextLine()) {
|
||||
// unquoted strings end before the first unindented line
|
||||
if (0 === $this->getCurrentLineIndentation()) {
|
||||
$this->moveToPreviousLine();
|
||||
|
||||
// quoted string values end with a line that is terminated with the quotation character
|
||||
if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$lines[] = trim($this->currentLine);
|
||||
}
|
||||
|
||||
for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
|
||||
if ('' === $lines[$i]) {
|
||||
$value .= "\n";
|
||||
$previousLineBlank = true;
|
||||
} elseif ($previousLineBlank) {
|
||||
$value .= $lines[$i];
|
||||
$previousLineBlank = false;
|
||||
} else {
|
||||
$value .= ' '.$lines[$i];
|
||||
$previousLineBlank = false;
|
||||
}
|
||||
}
|
||||
|
||||
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
|
||||
|
||||
$parsedValue = Inline::parse($value, $flags, $this->refs);
|
||||
|
||||
if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
|
||||
throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename);
|
||||
}
|
||||
|
||||
return $parsedValue;
|
||||
}
|
||||
|
||||
for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
|
||||
if ('' === $lines[$i]) {
|
||||
$value .= "\n";
|
||||
$previousLineBlank = true;
|
||||
} elseif ($previousLineBlank) {
|
||||
$value .= $lines[$i];
|
||||
$previousLineBlank = false;
|
||||
} else {
|
||||
$value .= ' '.$lines[$i];
|
||||
$previousLineBlank = false;
|
||||
}
|
||||
}
|
||||
|
||||
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
|
||||
|
||||
$parsedValue = Inline::parse($value, $flags, $this->refs);
|
||||
|
||||
if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
|
||||
throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename);
|
||||
}
|
||||
|
||||
return $parsedValue;
|
||||
} catch (ParseException $e) {
|
||||
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
|
||||
$e->setSnippet($this->currentLine);
|
||||
@@ -723,8 +825,6 @@ class Parser
|
||||
* @param string $style The style indicator that was used to begin this block scalar (| or >)
|
||||
* @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
|
||||
* @param int $indentation The indentation indicator that was used to begin this block scalar
|
||||
*
|
||||
* @return string The text value
|
||||
*/
|
||||
private function parseBlockScalar(string $style, string $chomping = '', int $indentation = 0): string
|
||||
{
|
||||
@@ -734,7 +834,7 @@ class Parser
|
||||
}
|
||||
|
||||
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
||||
$blockLines = array();
|
||||
$blockLines = [];
|
||||
|
||||
// leading blank lines are consumed before determining indentation
|
||||
while ($notEOF && $isCurrentLineBlank) {
|
||||
@@ -888,7 +988,7 @@ class Parser
|
||||
*/
|
||||
private function isCurrentLineComment(): bool
|
||||
{
|
||||
//checking explicitly the first char of the trim is faster than loops or strpos
|
||||
// checking explicitly the first char of the trim is faster than loops or strpos
|
||||
$ltrimmedLine = ltrim($this->currentLine, ' ');
|
||||
|
||||
return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0];
|
||||
@@ -908,7 +1008,7 @@ class Parser
|
||||
*/
|
||||
private function cleanup(string $value): string
|
||||
{
|
||||
$value = str_replace(array("\r\n", "\r"), "\n", $value);
|
||||
$value = str_replace(["\r\n", "\r"], "\n", $value);
|
||||
|
||||
// strip YAML header
|
||||
$count = 0;
|
||||
@@ -995,19 +1095,19 @@ class Parser
|
||||
{
|
||||
if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
|
||||
switch (preg_last_error()) {
|
||||
case PREG_INTERNAL_ERROR:
|
||||
case \PREG_INTERNAL_ERROR:
|
||||
$error = 'Internal PCRE error.';
|
||||
break;
|
||||
case PREG_BACKTRACK_LIMIT_ERROR:
|
||||
case \PREG_BACKTRACK_LIMIT_ERROR:
|
||||
$error = 'pcre.backtrack_limit reached.';
|
||||
break;
|
||||
case PREG_RECURSION_LIMIT_ERROR:
|
||||
case \PREG_RECURSION_LIMIT_ERROR:
|
||||
$error = 'pcre.recursion_limit reached.';
|
||||
break;
|
||||
case PREG_BAD_UTF8_ERROR:
|
||||
case \PREG_BAD_UTF8_ERROR:
|
||||
$error = 'Malformed UTF-8 data.';
|
||||
break;
|
||||
case PREG_BAD_UTF8_OFFSET_ERROR:
|
||||
case \PREG_BAD_UTF8_OFFSET_ERROR:
|
||||
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
|
||||
break;
|
||||
default:
|
||||
@@ -1058,4 +1158,160 @@ class Parser
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private function lexInlineQuotedString(int &$cursor = 0): string
|
||||
{
|
||||
$quotation = $this->currentLine[$cursor];
|
||||
$value = $quotation;
|
||||
++$cursor;
|
||||
|
||||
$previousLineWasNewline = true;
|
||||
$previousLineWasTerminatedWithBackslash = false;
|
||||
$lineNumber = 0;
|
||||
|
||||
do {
|
||||
if (++$lineNumber > 1) {
|
||||
$cursor += strspn($this->currentLine, ' ', $cursor);
|
||||
}
|
||||
|
||||
if ($this->isCurrentLineBlank()) {
|
||||
$value .= "\n";
|
||||
} elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) {
|
||||
$value .= ' ';
|
||||
}
|
||||
|
||||
for (; \strlen($this->currentLine) > $cursor; ++$cursor) {
|
||||
switch ($this->currentLine[$cursor]) {
|
||||
case '\\':
|
||||
if ("'" === $quotation) {
|
||||
$value .= '\\';
|
||||
} elseif (isset($this->currentLine[++$cursor])) {
|
||||
$value .= '\\'.$this->currentLine[$cursor];
|
||||
}
|
||||
|
||||
break;
|
||||
case $quotation:
|
||||
++$cursor;
|
||||
|
||||
if ("'" === $quotation && isset($this->currentLine[$cursor]) && "'" === $this->currentLine[$cursor]) {
|
||||
$value .= "''";
|
||||
break;
|
||||
}
|
||||
|
||||
return $value.$quotation;
|
||||
default:
|
||||
$value .= $this->currentLine[$cursor];
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->isCurrentLineBlank()) {
|
||||
$previousLineWasNewline = true;
|
||||
$previousLineWasTerminatedWithBackslash = false;
|
||||
} elseif ('\\' === $this->currentLine[-1]) {
|
||||
$previousLineWasNewline = false;
|
||||
$previousLineWasTerminatedWithBackslash = true;
|
||||
} else {
|
||||
$previousLineWasNewline = false;
|
||||
$previousLineWasTerminatedWithBackslash = false;
|
||||
}
|
||||
|
||||
if ($this->hasMoreLines()) {
|
||||
$cursor = 0;
|
||||
}
|
||||
} while ($this->moveToNextLine());
|
||||
|
||||
throw new ParseException('Malformed inline YAML string.');
|
||||
}
|
||||
|
||||
private function lexUnquotedString(int &$cursor): string
|
||||
{
|
||||
$offset = $cursor;
|
||||
$cursor += strcspn($this->currentLine, '[]{},: ', $cursor);
|
||||
|
||||
if ($cursor === $offset) {
|
||||
throw new ParseException('Malformed unquoted YAML string.');
|
||||
}
|
||||
|
||||
return substr($this->currentLine, $offset, $cursor - $offset);
|
||||
}
|
||||
|
||||
private function lexInlineMapping(int &$cursor = 0): string
|
||||
{
|
||||
return $this->lexInlineStructure($cursor, '}');
|
||||
}
|
||||
|
||||
private function lexInlineSequence(int &$cursor = 0): string
|
||||
{
|
||||
return $this->lexInlineStructure($cursor, ']');
|
||||
}
|
||||
|
||||
private function lexInlineStructure(int &$cursor, string $closingTag): string
|
||||
{
|
||||
$value = $this->currentLine[$cursor];
|
||||
++$cursor;
|
||||
|
||||
do {
|
||||
$this->consumeWhitespaces($cursor);
|
||||
|
||||
while (isset($this->currentLine[$cursor])) {
|
||||
switch ($this->currentLine[$cursor]) {
|
||||
case '"':
|
||||
case "'":
|
||||
$value .= $this->lexInlineQuotedString($cursor);
|
||||
break;
|
||||
case ':':
|
||||
case ',':
|
||||
$value .= $this->currentLine[$cursor];
|
||||
++$cursor;
|
||||
break;
|
||||
case '{':
|
||||
$value .= $this->lexInlineMapping($cursor);
|
||||
break;
|
||||
case '[':
|
||||
$value .= $this->lexInlineSequence($cursor);
|
||||
break;
|
||||
case $closingTag:
|
||||
$value .= $this->currentLine[$cursor];
|
||||
++$cursor;
|
||||
|
||||
return $value;
|
||||
case '#':
|
||||
break 2;
|
||||
default:
|
||||
$value .= $this->lexUnquotedString($cursor);
|
||||
}
|
||||
|
||||
if ($this->consumeWhitespaces($cursor)) {
|
||||
$value .= ' ';
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->hasMoreLines()) {
|
||||
$cursor = 0;
|
||||
}
|
||||
} while ($this->moveToNextLine());
|
||||
|
||||
throw new ParseException('Malformed inline YAML string.');
|
||||
}
|
||||
|
||||
private function consumeWhitespaces(int &$cursor): bool
|
||||
{
|
||||
$whitespacesConsumed = 0;
|
||||
|
||||
do {
|
||||
$whitespaceOnlyTokenLength = strspn($this->currentLine, ' ', $cursor);
|
||||
$whitespacesConsumed += $whitespaceOnlyTokenLength;
|
||||
$cursor += $whitespaceOnlyTokenLength;
|
||||
|
||||
if (isset($this->currentLine[$cursor])) {
|
||||
return 0 < $whitespacesConsumed;
|
||||
}
|
||||
|
||||
if ($this->hasMoreLines()) {
|
||||
$cursor = 0;
|
||||
}
|
||||
} while ($this->moveToNextLine());
|
||||
|
||||
return 0 < $whitespacesConsumed;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user