rfc:token_as_object
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:token_as_object [2020/02/15 15:46] – nikic | rfc:token_as_object [2020/03/02 17:56] – nikic | ||
---|---|---|---|
Line 32: | Line 32: | ||
| | ||
final public function __construct(int $id, string $text, int $line = -1, int $pos = -1); | final 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 73: | Line 87: | ||
To guarantee a well-defined construction behavior, the '' | To guarantee a well-defined construction behavior, the '' | ||
- | |||
- | ===== Open Questions ===== | ||
==== 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-> | + | '' |
- | | + | |
- | } | + | 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; | ||
} | } | ||
</ | </ | ||
+ | |||
+ | These kinds of search/ | ||
+ | |||
+ | <PHP> | ||
+ | public function isIgnorable(): | ||
+ | return $this-> | ||
+ | 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 ===== |
rfc/token_as_object.txt · Last modified: 2020/11/12 13:33 by nikic