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

Breadcrumb

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

function Emulative::fixupTokens

Parameters

list<Token> $tokens:

Return value

list<Token>

1 call to Emulative::fixupTokens()
Emulative::tokenize in vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php
Tokenize the provided source code.

File

vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php, line 125

Class

Emulative

Namespace

PhpParser\Lexer

Code

private function fixupTokens(array $tokens) : array {
    if (\count($this->patches) === 0) {
        return $tokens;
    }
    // Load first patch
    $patchIdx = 0;
    list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
    // We use a manual loop over the tokens, because we modify the array on the fly
    $posDelta = 0;
    $lineDelta = 0;
    for ($i = 0, $c = \count($tokens); $i < $c; $i++) {
        $token = $tokens[$i];
        $pos = $token->pos;
        $token->pos += $posDelta;
        $token->line += $lineDelta;
        $localPosDelta = 0;
        $len = \strlen($token->text);
        while ($patchPos >= $pos && $patchPos < $pos + $len) {
            $patchTextLen = \strlen($patchText);
            if ($patchType === 'remove') {
                if ($patchPos === $pos && $patchTextLen === $len) {
                    // Remove token entirely
                    array_splice($tokens, $i, 1, []);
                    $i--;
                    $c--;
                }
                else {
                    // Remove from token string
                    $token->text = substr_replace($token->text, '', $patchPos - $pos + $localPosDelta, $patchTextLen);
                    $localPosDelta -= $patchTextLen;
                }
                $lineDelta -= \substr_count($patchText, "\n");
            }
            elseif ($patchType === 'add') {
                // Insert into the token string
                $token->text = substr_replace($token->text, $patchText, $patchPos - $pos + $localPosDelta, 0);
                $localPosDelta += $patchTextLen;
                $lineDelta += \substr_count($patchText, "\n");
            }
            elseif ($patchType === 'replace') {
                // Replace inside the token string
                $token->text = substr_replace($token->text, $patchText, $patchPos - $pos + $localPosDelta, $patchTextLen);
            }
            else {
                assert(false);
            }
            // Fetch the next patch
            $patchIdx++;
            if ($patchIdx >= \count($this->patches)) {
                // No more patches. However, we still need to adjust position.
                $patchPos = \PHP_INT_MAX;
                break;
            }
            list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
        }
        $posDelta += $localPosDelta;
    }
    return $tokens;
}

API Navigation

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