mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 23:28:15 +01:00
8ad4129442
Closes #905
226 lines
8.3 KiB
PHP
226 lines
8.3 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace PhpParser\Lexer;
|
|
|
|
use PhpParser\Error;
|
|
use PhpParser\ErrorHandler;
|
|
use PhpParser\Lexer;
|
|
use PhpParser\Lexer\TokenEmulator\AttributeEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\EnumTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\ExplicitOctalEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\FlexibleDocStringEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\MatchTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\NullsafeTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\NumericLiteralSeparatorEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\ReadonlyFunctionTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\ReadonlyTokenEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\ReverseEmulator;
|
|
use PhpParser\Lexer\TokenEmulator\TokenEmulator;
|
|
use PhpParser\PhpVersion;
|
|
|
|
class Emulative extends Lexer {
|
|
/** @var array{int, string, string}[] Patches used to reverse changes introduced in the code */
|
|
private $patches = [];
|
|
|
|
/** @var list<TokenEmulator> */
|
|
private $emulators = [];
|
|
|
|
/** @var PhpVersion */
|
|
private $targetPhpVersion;
|
|
/** @var PhpVersion */
|
|
private $hostPhpVersion;
|
|
|
|
/**
|
|
* @param array{usedAttributes?: string[], phpVersion?: PhpVersion|string} $options Lexer options.
|
|
* In addition to the usual options, accepts a 'phpVersion' (PhpVersion object or string)
|
|
* that specifies the version to emulate. Defaults to newest supported.
|
|
*/
|
|
public function __construct(array $options = []) {
|
|
$version = $options['phpVersion'] ?? PhpVersion::getNewestSupported();
|
|
if (!$version instanceof PhpVersion) {
|
|
$version = PhpVersion::fromString($version);
|
|
}
|
|
$this->targetPhpVersion = $version;
|
|
$this->hostPhpVersion = PhpVersion::getHostVersion();
|
|
unset($options['phpVersion']);
|
|
|
|
parent::__construct($options);
|
|
|
|
$emulators = [
|
|
new FlexibleDocStringEmulator(),
|
|
new FnTokenEmulator(),
|
|
new MatchTokenEmulator(),
|
|
new CoaleseEqualTokenEmulator(),
|
|
new NumericLiteralSeparatorEmulator(),
|
|
new NullsafeTokenEmulator(),
|
|
new AttributeEmulator(),
|
|
new EnumTokenEmulator(),
|
|
new ReadonlyTokenEmulator(),
|
|
new ExplicitOctalEmulator(),
|
|
new ReadonlyFunctionTokenEmulator(),
|
|
];
|
|
|
|
// Collect emulators that are relevant for the PHP version we're running
|
|
// and the PHP version we're targeting for emulation.
|
|
foreach ($emulators as $emulator) {
|
|
$emulatorPhpVersion = $emulator->getPhpVersion();
|
|
if ($this->isForwardEmulationNeeded($emulatorPhpVersion)) {
|
|
$this->emulators[] = $emulator;
|
|
} elseif ($this->isReverseEmulationNeeded($emulatorPhpVersion)) {
|
|
$this->emulators[] = new ReverseEmulator($emulator);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function startLexing(string $code, ?ErrorHandler $errorHandler = null): void {
|
|
$emulators = array_filter($this->emulators, function ($emulator) use ($code) {
|
|
return $emulator->isEmulationNeeded($code);
|
|
});
|
|
|
|
if (empty($emulators)) {
|
|
// Nothing to emulate, yay
|
|
parent::startLexing($code, $errorHandler);
|
|
return;
|
|
}
|
|
|
|
$this->patches = [];
|
|
foreach ($emulators as $emulator) {
|
|
$code = $emulator->preprocessCode($code, $this->patches);
|
|
}
|
|
|
|
$collector = new ErrorHandler\Collecting();
|
|
parent::startLexing($code, $collector);
|
|
$this->sortPatches();
|
|
$this->fixupTokens();
|
|
|
|
$errors = $collector->getErrors();
|
|
if (!empty($errors)) {
|
|
$this->fixupErrors($errors);
|
|
foreach ($errors as $error) {
|
|
$errorHandler->handleError($error);
|
|
}
|
|
}
|
|
|
|
foreach ($emulators as $emulator) {
|
|
$this->tokens = $emulator->emulate($code, $this->tokens);
|
|
}
|
|
}
|
|
|
|
private function isForwardEmulationNeeded(PhpVersion $emulatorPhpVersion): bool {
|
|
return $this->hostPhpVersion->older($emulatorPhpVersion)
|
|
&& $this->targetPhpVersion->newerOrEqual($emulatorPhpVersion);
|
|
}
|
|
|
|
private function isReverseEmulationNeeded(PhpVersion $emulatorPhpVersion): bool {
|
|
return $this->hostPhpVersion->newerOrEqual($emulatorPhpVersion)
|
|
&& $this->targetPhpVersion->older($emulatorPhpVersion);
|
|
}
|
|
|
|
private function sortPatches(): void {
|
|
// Patches may be contributed by different emulators.
|
|
// Make sure they are sorted by increasing patch position.
|
|
usort($this->patches, function ($p1, $p2) {
|
|
return $p1[0] <=> $p2[0];
|
|
});
|
|
}
|
|
|
|
private function fixupTokens(): void {
|
|
if (\count($this->patches) === 0) {
|
|
return;
|
|
}
|
|
|
|
// 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;
|
|
for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
|
|
$token = $this->tokens[$i];
|
|
$pos = $token->pos;
|
|
$token->pos += $posDelta;
|
|
$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($this->tokens, $i, 1, []);
|
|
$i--;
|
|
$c--;
|
|
} else {
|
|
// Remove from token string
|
|
$token->text = substr_replace(
|
|
$token->text, '', $patchPos - $pos + $localPosDelta, $patchTextLen
|
|
);
|
|
$localPosDelta -= $patchTextLen;
|
|
}
|
|
} elseif ($patchType === 'add') {
|
|
// Insert into the token string
|
|
$token->text = substr_replace(
|
|
$token->text, $patchText, $patchPos - $pos + $localPosDelta, 0
|
|
);
|
|
$localPosDelta += $patchTextLen;
|
|
} 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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fixup line and position information in errors.
|
|
*
|
|
* @param Error[] $errors
|
|
*/
|
|
private function fixupErrors(array $errors): void {
|
|
foreach ($errors as $error) {
|
|
$attrs = $error->getAttributes();
|
|
|
|
$posDelta = 0;
|
|
$lineDelta = 0;
|
|
foreach ($this->patches as $patch) {
|
|
list($patchPos, $patchType, $patchText) = $patch;
|
|
if ($patchPos >= $attrs['startFilePos']) {
|
|
// No longer relevant
|
|
break;
|
|
}
|
|
|
|
if ($patchType === 'add') {
|
|
$posDelta += strlen($patchText);
|
|
$lineDelta += substr_count($patchText, "\n");
|
|
} elseif ($patchType === 'remove') {
|
|
$posDelta -= strlen($patchText);
|
|
$lineDelta -= substr_count($patchText, "\n");
|
|
}
|
|
}
|
|
|
|
$attrs['startFilePos'] += $posDelta;
|
|
$attrs['endFilePos'] += $posDelta;
|
|
$attrs['startLine'] += $lineDelta;
|
|
$attrs['endLine'] += $lineDelta;
|
|
$error->setAttributes($attrs);
|
|
}
|
|
}
|
|
}
|