Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Parser.php

function Parser::parseValue

Parses a YAML value.

Parameters

string $value A YAML value:

int $flags A bit field of Yaml::PARSE_* constants to customize the YAML parser behavior:

string $context The parser context (either sequence or mapping):

Throws

ParseException When reference does not exist

1 call to Parser::parseValue()
Parser::doParse in vendor/symfony/yaml/Parser.php

File

vendor/symfony/yaml/Parser.php, line 710

Class

Parser
Parser parses YAML strings to convert them to PHP arrays.

Namespace

Symfony\Component\Yaml

Code

private function parseValue(string $value, int $flags, string $context) : mixed {
    if (str_starts_with($value, '*')) {
        if (false !== ($pos = strpos($value, '#'))) {
            $value = substr($value, 1, $pos - 2);
        }
        else {
            $value = substr($value, 1);
        }
        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], [
        '!',
        '|',
        '>',
    ], 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), abs((int) $modifiers));
        if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
            if ('!!binary' === $matches['tag']) {
                return Inline::evaluateBinaryScalar($data);
            }
            return new TaggedValue(substr($matches['tag'], 1), $data);
        }
        return $data;
    }
    try {
        if ('' !== $value && '{' === $value[0]) {
            $cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
            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);
        }
        switch ($value[0] ?? '') {
            case '"':
            case "'":
                $cursor = \strlen(rtrim($this->currentLine)) - \strlen(rtrim($value));
                $parsedValue = Inline::parse($this->lexInlineQuotedString($cursor), $flags, $this->refs);
                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)));
                }
                return $parsedValue;
            default:
                $lines = [];
                while ($this->moveToNextLine()) {
                    // unquoted strings end before the first unindented line
                    if (0 === $this->getCurrentLineIndentation()) {
                        $this->moveToPreviousLine();
                        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] && str_contains($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);
        throw $e;
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal