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

Breadcrumb

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

function ParserAbstract::parseDocString

Parameters

string|(Expr|InterpolatedStringPart)[] $contents:

array<string, mixed> $attributes:

array<string, mixed> $endTokenAttributes:

File

vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php, line 809

Class

ParserAbstract

Namespace

PhpParser

Code

protected function parseDocString(string $startToken, $contents, string $endToken, array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape) : Expr {
    $kind = strpos($startToken, "'") === false ? String_::KIND_HEREDOC : String_::KIND_NOWDOC;
    $regex = '/\\A[bB]?<<<[ \\t]*[\'"]?([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)[\'"]?(?:\\r\\n|\\n|\\r)\\z/';
    $result = preg_match($regex, $startToken, $matches);
    assert($result === 1);
    $label = $matches[1];
    $result = preg_match('/\\A[ \\t]*/', $endToken, $matches);
    assert($result === 1);
    $indentation = $matches[0];
    $attributes['kind'] = $kind;
    $attributes['docLabel'] = $label;
    $attributes['docIndentation'] = $indentation;
    $indentHasSpaces = false !== strpos($indentation, " ");
    $indentHasTabs = false !== strpos($indentation, "\t");
    if ($indentHasSpaces && $indentHasTabs) {
        $this->emitError(new Error('Invalid indentation - tabs and spaces cannot be mixed', $endTokenAttributes));
        // Proceed processing as if this doc string is not indented
        $indentation = '';
    }
    $indentLen = \strlen($indentation);
    $indentChar = $indentHasSpaces ? " " : "\t";
    if (\is_string($contents)) {
        if ($contents === '') {
            $attributes['rawValue'] = $contents;
            return new String_('', $attributes);
        }
        $contents = $this->stripIndentation($contents, $indentLen, $indentChar, true, true, $attributes);
        $contents = preg_replace('~(\\r\\n|\\n|\\r)\\z~', '', $contents);
        $attributes['rawValue'] = $contents;
        if ($kind === String_::KIND_HEREDOC) {
            $contents = String_::parseEscapeSequences($contents, null, $parseUnicodeEscape);
        }
        return new String_($contents, $attributes);
    }
    else {
        assert(count($contents) > 0);
        if (!$contents[0] instanceof Node\InterpolatedStringPart) {
            // If there is no leading encapsed string part, pretend there is an empty one
            $this->stripIndentation('', $indentLen, $indentChar, true, false, $contents[0]->getAttributes());
        }
        $newContents = [];
        foreach ($contents as $i => $part) {
            if ($part instanceof Node\InterpolatedStringPart) {
                $isLast = $i === \count($contents) - 1;
                $part->value = $this->stripIndentation($part->value, $indentLen, $indentChar, $i === 0, $isLast, $part->getAttributes());
                if ($isLast) {
                    $part->value = preg_replace('~(\\r\\n|\\n|\\r)\\z~', '', $part->value);
                }
                $part->setAttribute('rawValue', $part->value);
                $part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape);
                if ('' === $part->value) {
                    continue;
                }
            }
            $newContents[] = $part;
        }
        return new InterpolatedString($newContents, $attributes);
    }
}
RSS feed
Powered by Drupal