| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace PhpParser; |
| 4 |
|
| 5 |
if (!\function_exists('PhpParser\defineCompatibilityTokens')) { |
| 6 |
function defineCompatibilityTokens(): void { |
| 7 |
$compatTokens = [ |
| 8 |
// PHP 8.0 |
| 9 |
'T_NAME_QUALIFIED', |
| 10 |
'T_NAME_FULLY_QUALIFIED', |
| 11 |
'T_NAME_RELATIVE', |
| 12 |
'T_MATCH', |
| 13 |
'T_NULLSAFE_OBJECT_OPERATOR', |
| 14 |
'T_ATTRIBUTE', |
| 15 |
// PHP 8.1 |
| 16 |
'T_ENUM', |
| 17 |
'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG', |
| 18 |
'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG', |
| 19 |
'T_READONLY', |
| 20 |
// PHP 8.4 |
| 21 |
'T_PROPERTY_C', |
| 22 |
'T_PUBLIC_SET', |
| 23 |
'T_PROTECTED_SET', |
| 24 |
'T_PRIVATE_SET', |
| 25 |
// PHP 8.5 |
| 26 |
'T_PIPE', |
| 27 |
'T_VOID_CAST', |
| 28 |
]; |
| 29 |
|
| 30 |
// PHP-Parser might be used together with another library that also emulates some or all |
| 31 |
// of these tokens. Perform a sanity-check that all already defined tokens have been |
| 32 |
// assigned a unique ID. |
| 33 |
$usedTokenIds = []; |
| 34 |
foreach ($compatTokens as $token) { |
| 35 |
if (\defined($token)) { |
| 36 |
$tokenId = \constant($token); |
| 37 |
if (!\is_int($tokenId)) { |
| 38 |
throw new \Error(sprintf( |
| 39 |
'Token %s has ID of type %s, should be int. ' . |
| 40 |
'You may be using a library with broken token emulation', |
| 41 |
$token, \gettype($tokenId) |
| 42 |
)); |
| 43 |
} |
| 44 |
$clashingToken = $usedTokenIds[$tokenId] ?? null; |
| 45 |
if ($clashingToken !== null) { |
| 46 |
throw new \Error(sprintf( |
| 47 |
'Token %s has same ID as token %s, ' . |
| 48 |
'you may be using a library with broken token emulation', |
| 49 |
$token, $clashingToken |
| 50 |
)); |
| 51 |
} |
| 52 |
$usedTokenIds[$tokenId] = $token; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Now define any tokens that have not yet been emulated. Try to assign IDs from -1 |
| 57 |
// downwards, but skip any IDs that may already be in use. |
| 58 |
$newTokenId = -1; |
| 59 |
foreach ($compatTokens as $token) { |
| 60 |
if (!\defined($token)) { |
| 61 |
while (isset($usedTokenIds[$newTokenId])) { |
| 62 |
$newTokenId--; |
| 63 |
} |
| 64 |
\define($token, $newTokenId); |
| 65 |
$newTokenId--; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
defineCompatibilityTokens(); |
| 71 |
} |