2019-06-30 12:13:28 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace PhpParser\Lexer\TokenEmulator;
|
|
|
|
|
|
|
|
use PhpParser\Lexer\Emulative;
|
2019-06-30 13:10:12 +02:00
|
|
|
use PhpParser\Token;
|
2019-06-30 12:13:28 +02:00
|
|
|
|
|
|
|
final class NumericLiteralSeparatorEmulator implements TokenEmulatorInterface
|
|
|
|
{
|
|
|
|
const BIN = '(?:0b[01]+(?:_[01]+)*)';
|
|
|
|
const HEX = '(?:0x[0-9a-f]+(?:_[0-9a-f]+)*)';
|
|
|
|
const DEC = '(?:[0-9]+(?:_[0-9]+)*)';
|
|
|
|
const SIMPLE_FLOAT = '(?:' . self::DEC . '\.' . self::DEC . '?|\.' . self::DEC . ')';
|
|
|
|
const EXP = '(?:e[+-]?' . self::DEC . ')';
|
|
|
|
const FLOAT = '(?:' . self::SIMPLE_FLOAT . self::EXP . '?|' . self::DEC . self::EXP . ')';
|
|
|
|
const NUMBER = '~' . self::FLOAT . '|' . self::BIN . '|' . self::HEX . '|' . self::DEC . '~iA';
|
|
|
|
|
|
|
|
public function isEmulationNeeded(string $code) : bool
|
|
|
|
{
|
|
|
|
// skip version where this is supported
|
|
|
|
if (version_compare(\PHP_VERSION, Emulative::PHP_7_4, '>=')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return preg_match('~[0-9a-f]_[0-9a-f]~i', $code) !== false;
|
|
|
|
}
|
|
|
|
|
2019-06-30 13:10:12 +02:00
|
|
|
/**
|
|
|
|
* @param Token[] $tokens
|
|
|
|
* @return Token[]
|
|
|
|
*/
|
2019-06-30 12:13:28 +02:00
|
|
|
public function emulate(string $code, array $tokens): array
|
|
|
|
{
|
|
|
|
// We need to manually iterate and manage a count because we'll change
|
|
|
|
// the tokens array on the way
|
|
|
|
for ($i = 0, $c = count($tokens); $i < $c; ++$i) {
|
|
|
|
$token = $tokens[$i];
|
2019-06-30 13:10:12 +02:00
|
|
|
$tokenLen = \strlen($token->value);
|
2019-06-30 12:13:28 +02:00
|
|
|
|
2019-06-30 13:10:12 +02:00
|
|
|
if ($token->id !== T_LNUMBER && $token->id !== T_DNUMBER) {
|
2019-06-30 12:13:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-30 13:10:12 +02:00
|
|
|
$res = preg_match(self::NUMBER, $code, $matches, 0, $token->filePos);
|
2019-06-30 12:13:28 +02:00
|
|
|
assert($res, "No number at number token position");
|
|
|
|
|
|
|
|
$match = $matches[0];
|
|
|
|
$matchLen = \strlen($match);
|
|
|
|
if ($matchLen === $tokenLen) {
|
|
|
|
// Original token already holds the full number.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tokenKind = $this->resolveIntegerOrFloatToken($match);
|
2019-06-30 13:10:12 +02:00
|
|
|
$newTokens = [new Token($tokenKind, $match, $token->line, $token->filePos)];
|
2019-06-30 12:13:28 +02:00
|
|
|
|
|
|
|
$numTokens = 1;
|
|
|
|
$len = $tokenLen;
|
|
|
|
while ($matchLen > $len) {
|
|
|
|
$nextToken = $tokens[$i + $numTokens];
|
2019-06-30 13:10:12 +02:00
|
|
|
$nextTokenLen = \strlen($nextToken->value);
|
2019-06-30 12:13:28 +02:00
|
|
|
|
|
|
|
$numTokens++;
|
|
|
|
if ($matchLen < $len + $nextTokenLen) {
|
|
|
|
// Split trailing characters into a partial token.
|
2019-06-30 13:10:12 +02:00
|
|
|
$partialText = substr($nextToken->value, $matchLen - $len);
|
|
|
|
$newTokens[] = new Token($nextToken->id, $partialText, $nextToken->line, $nextToken->filePos);
|
2019-06-30 12:13:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$len += $nextTokenLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_splice($tokens, $i, $numTokens, $newTokens);
|
|
|
|
$c -= $numTokens - \count($newTokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveIntegerOrFloatToken(string $str): int
|
|
|
|
{
|
|
|
|
$str = str_replace('_', '', $str);
|
|
|
|
|
|
|
|
if (stripos($str, '0b') === 0) {
|
|
|
|
$num = bindec($str);
|
|
|
|
} elseif (stripos($str, '0x') === 0) {
|
|
|
|
$num = hexdec($str);
|
|
|
|
} elseif (stripos($str, '0') === 0 && ctype_digit($str)) {
|
|
|
|
$num = octdec($str);
|
|
|
|
} else {
|
|
|
|
$num = +$str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_float($num) ? T_DNUMBER : T_LNUMBER;
|
|
|
|
}
|
|
|
|
}
|