rfc:token_as_object
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:token_as_object [2020/02/14 16:22] – nikic | rfc:token_as_object [2020/11/12 13:33] (current) – nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2020-02-13 | * Date: 2020-02-13 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
* Implementation: | * Implementation: | ||
Line 9: | Line 9: | ||
The '' | The '' | ||
+ | |||
+ | > **Note:** PhpToken:: | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 27: | Line 29: | ||
/** | /** | ||
* Same as token_get_all(), | * Same as token_get_all(), | ||
- | * @return | + | * @return |
*/ | */ | ||
public static function getAll(string $code, int $flags = 0): array; | public static function getAll(string $code, int $flags = 0): array; | ||
| | ||
- | public function __construct(int $id, string $text, int $line = -1, int $pos = -1); | + | |
+ | |||
+ | /** Get the name of the token. */ | ||
+ | public function getTokenName(): | ||
+ | |||
+ | /** | ||
+ | * Whether the token has the given ID, the given text, | ||
+ | * or has an ID/text part of the given array. | ||
+ | * | ||
+ | * @param int|string|array $kind | ||
+ | */ | ||
+ | public function is($kind): bool; | ||
+ | |||
+ | /** Whether this token would be ignored by the PHP parser. */ | ||
+ | public function isIgnorable(): | ||
} | } | ||
</ | </ | ||
Line 45: | Line 61: | ||
* The token stores the position in the file, so that consumers don't have to compute and store it separately. | * The token stores the position in the file, so that consumers don't have to compute and store it separately. | ||
- | Finally | + | Finally, the tokens take up significantly less memory, and are faster to construct as well. On a large file: |
< | < | ||
Line 56: | Line 72: | ||
</ | </ | ||
- | ===== Open Questions | + | ==== Extensibility |
+ | |||
+ | The '' | ||
+ | |||
+ | < | ||
+ | class MyPhpToken extends PhpToken { | ||
+ | public function getLowerText() { | ||
+ | return strtolower($this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $tokens | ||
+ | var_dump($tokens[0] instanceof MyPhpToken); | ||
+ | $tokens[0]-> | ||
+ | </ | ||
+ | |||
+ | To guarantee a well-defined construction behavior, the '' | ||
==== Additional methods ==== | ==== Additional methods ==== | ||
- | There are a few useful helper methods that could be added to the '' | + | The '' |
<PHP> | <PHP> | ||
- | class PhpToken | + | public function getTokenName(): |
- | /** Whether | + | |
- | * or has an ID/text part of the given array. */ | + | return chr($this-> |
- | public function is($kind): bool { | + | } elseif (' |
- | if (is_array($kind)) { | + | return $name; |
- | foreach ($kind as $singleKind) { | + | } else { |
- | if (is_string($singleKind)) { | + | return null; |
- | if ($this-> | + | } |
- | return true; | + | } |
- | } | + | </PHP> |
- | } else if (is_int($singleKind)) { | + | |
- | if ($this-> | + | '' |
- | return true; | + | |
- | } | + | It should be noted that tokens that are not known to PHP are commonly used, for example when emulating lexer behavior from future PHP versions. In this case custom token IDs are used, so they should be handled gracefully. |
- | } else { | + | |
- | throw new TypeError(" | + | < |
+ | public function is($kind): bool { | ||
+ | if (is_array($kind)) { | ||
+ | foreach ($kind as $singleKind) { | ||
+ | if (is_string($singleKind)) { | ||
+ | if ($this-> | ||
+ | return true; | ||
+ | } | ||
+ | } else if (is_int($singleKind)) { | ||
+ | if ($this-> | ||
+ | return true; | ||
} | } | ||
+ | } else { | ||
+ | throw new TypeError(" | ||
} | } | ||
- | return false; | ||
- | } else if (is_string($kind)) { | ||
- | return $this-> | ||
- | } else if (is_int($kind)) { | ||
- | return $this-> | ||
- | } else { | ||
- | throw new TypeError(" | ||
} | } | ||
+ | return false; | ||
+ | } else if (is_string($kind)) { | ||
+ | return $this-> | ||
+ | } else if (is_int($kind)) { | ||
+ | return $this-> | ||
+ | } else { | ||
+ | throw new TypeError(" | ||
} | } | ||
+ | } | ||
+ | </ | ||
- | /** Whether this token would be ignored by the PHP parser. */ | + | The '' |
- | public function isIgnorable(): bool { | + | |
- | return | + | |
- | T_WHITESPACE, | + | |
- | T_COMMENT, | + | |
- | T_DOC_COMMENT, | + | |
- | T_OPEN_TAG, | + | |
- | | + | |
- | } | + | |
- | /** Get the name of the token. | + | While non-generic code can easily check the appropriate property, such as '' |
- | public function | + | |
- | if ($this->id < 256) { | + | < |
- | | + | // An example, NOT part of the PhpToken interface. |
- | } else { | + | public function |
- | return | + | |
+ | for ($count = \count($tokens); | ||
+ | | ||
+ | return $pos; | ||
} | } | ||
} | } | ||
+ | return -1; | ||
} | } | ||
</ | </ | ||
- | ==== Allowing extension | + | These kinds of search/ |
- | If the class is extended, should | + | < |
+ | public function isIgnorable(): | ||
+ | return $this->is([ | ||
+ | T_WHITESPACE, | ||
+ | T_COMMENT, | ||
+ | T_DOC_COMMENT, | ||
+ | T_OPEN_TAG, | ||
+ | ]); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | As a special case, it is very common that whitespace and comments need to be skipped during token processing. The '' | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Line 130: | Line 183: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Yes / No. | + | Voting opened 2020-03-06 and closes 2020-03-20. |
+ | |||
+ | <doodle title=" | ||
+ | | ||
+ | | ||
+ | </ | ||
rfc/token_as_object.txt · Last modified: 2020/11/12 13:33 by nikic