From aad0e2896f512e4a56100d904bc65385ec321aa1 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 23 Jun 2019 14:50:14 +0200 Subject: [PATCH] Remove token registration from TokenEmulator interface --- lib/PhpParser/Lexer/Emulative.php | 11 ++++++----- .../TokenEmulator/CoaleseEqualTokenEmulator.php | 15 +-------------- .../Lexer/TokenEmulator/FnTokenEmulator.php | 15 +-------------- .../TokenEmulator/TokenEmulatorInterface.php | 5 +---- 4 files changed, 9 insertions(+), 37 deletions(-) diff --git a/lib/PhpParser/Lexer/Emulative.php b/lib/PhpParser/Lexer/Emulative.php index 5d10b9a..ac473ad 100644 --- a/lib/PhpParser/Lexer/Emulative.php +++ b/lib/PhpParser/Lexer/Emulative.php @@ -8,12 +8,16 @@ use PhpParser\Lexer; use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator; use PhpParser\Lexer\TokenEmulator\FnTokenEmulator; use PhpParser\Lexer\TokenEmulator\TokenEmulatorInterface; +use PhpParser\Parser\Tokens; class Emulative extends Lexer { const PHP_7_3 = '7.3.0dev'; const PHP_7_4 = '7.4.0dev'; + const T_COALESCE_EQUAL = 1007; + const T_FN = 1008; + const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX' /<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n (?:.*\r?\n)*? @@ -33,14 +37,11 @@ REGEX; { parent::__construct($options); - // prepare token emulators $this->tokenEmulators[] = new FnTokenEmulator(); $this->tokenEmulators[] = new CoaleseEqualTokenEmulator(); - // add emulated tokens here - foreach ($this->tokenEmulators as $emulativeToken) { - $this->tokenMap[$emulativeToken->getTokenId()] = $emulativeToken->getParserTokenId(); - } + $this->tokenMap[self::T_COALESCE_EQUAL] = Tokens::T_COALESCE_EQUAL; + $this->tokenMap[self::T_FN] = Tokens::T_FN; } public function startLexing(string $code, ErrorHandler $errorHandler = null) { diff --git a/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php b/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php index ca6cf13..10f7e1e 100644 --- a/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php +++ b/lib/PhpParser/Lexer/TokenEmulator/CoaleseEqualTokenEmulator.php @@ -3,22 +3,9 @@ namespace PhpParser\Lexer\TokenEmulator; use PhpParser\Lexer\Emulative; -use PhpParser\Parser\Tokens; final class CoaleseEqualTokenEmulator implements TokenEmulatorInterface { - const T_COALESCE_EQUAL = 1007; - - public function getTokenId(): int - { - return self::T_COALESCE_EQUAL; - } - - public function getParserTokenId(): int - { - return Tokens::T_COALESCE_EQUAL; - } - public function isEmulationNeeded(string $code) : bool { // skip version where this is supported @@ -38,7 +25,7 @@ final class CoaleseEqualTokenEmulator implements TokenEmulatorInterface if (isset($tokens[$i + 1])) { if ($tokens[$i][0] === T_COALESCE && $tokens[$i + 1] === '=') { array_splice($tokens, $i, 2, [ - [self::T_COALESCE_EQUAL, '??=', $line] + [Emulative::T_COALESCE_EQUAL, '??=', $line] ]); $c--; continue; diff --git a/lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php b/lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php index 2059693..0939dd4 100644 --- a/lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php +++ b/lib/PhpParser/Lexer/TokenEmulator/FnTokenEmulator.php @@ -3,22 +3,9 @@ namespace PhpParser\Lexer\TokenEmulator; use PhpParser\Lexer\Emulative; -use PhpParser\Parser\Tokens; final class FnTokenEmulator implements TokenEmulatorInterface { - const T_FN = 1008; - - public function getTokenId(): int - { - return self::T_FN; - } - - public function getParserTokenId(): int - { - return Tokens::T_FN; - } - public function isEmulationNeeded(string $code) : bool { // skip version where this is supported @@ -40,7 +27,7 @@ final class FnTokenEmulator implements TokenEmulatorInterface continue; } - $tokens[$i][0] = self::T_FN; + $tokens[$i][0] = Emulative::T_FN; } } diff --git a/lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php b/lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php index 6ef5b9d..e03ccc6 100644 --- a/lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php +++ b/lib/PhpParser/Lexer/TokenEmulator/TokenEmulatorInterface.php @@ -2,12 +2,9 @@ namespace PhpParser\Lexer\TokenEmulator; +/** @internal */ interface TokenEmulatorInterface { - public function getTokenId(): int; - - public function getParserTokenId(): int; - public function isEmulationNeeded(string $code): bool; /**