note 126646 added to class.phptoken
| From: | info at ensostudio dot ru | Date: | Tue, 30 Nov 2021 05:35:59 +0000 |
| Subject: | note 126646 added to class.phptoken | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217308@lists.php.net to get a copy of this message | ||
<?php
/**
* PhpToken polyfill.
*/
class PhpToken
{
/**
* @var int Identifier
*/
public $id;
/**
* @var string The textual content of the token
*/
public $text;
/**
* @var int The starting line number (1-based) of the token
*/
public $line;
/**
* @var int The starting position (0-based) in the tokenized string
*/
public $pos;
/**
* Creates a new instance.
*
* @param int|string $id
T_* constant or an ASCII codepoint representing a
single-char token
* @param string $text The textual content of the token
* @param int $line The starting line number (1-based) of the token
* @param int $position The starting position (0-based) in the tokenized string
* @return void
*/
public function __construct($id, $text, $line = -1, $position = -1)
{
$this->id = is_int($id) ? $id : ord($id);
$this->text = $text;
$this->line = $line;
$this->pos = $position;
}
/**
* Returns the name of the token.
*
* @return string|null
*/
public function getTokenName()
{
$name = token_name($this->id);
return $name === 'UNKNOWN' ? null : $name;
}
/**
* Tells whether the token is of given kind.
*
* @param int|string|array $kind Either a single value to match the token's id or textual
content, or an array
* thereof
* @return bool
*/
public function is($kind)
{
$token = (is_int($kind) || (is_array($kind) && is_int(current($kind))))
? $this->id
: $this->getTokenName();
return in_array($token, (array) $kind, true);
}
/**
* Tells whether the token would be ignored by the PHP parser.
*
* @return bool
*/
public function isIgnorable()
{
return $this->is([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_INLINE_HTML]);
}
/**
* Returns the textual content of the token.
*
* @return string
*/
public function __toString()
{
return (string) $this->text;
}
/**
* Splits given source into PHP tokens, represented by self objects.
*
* @param string $code The PHP source to parse
* @param int $flags TOKEN_PARSE - recognises the ability to usereserved words in
specific contexts
* @return static[]
*/
public static function tokenize($code, $flags = 0): array
{
$tokens = [];
$line = 1;
$position = 0;
foreach (token_get_all($code, $flags) as $token) {
if (is_string($token)) {
$id = $token;
$text = $token;
} else {
if ($token[2] !== $line) {
$position = 0;
}
list($id, $text, $line) = $token;
}
$tokens[] = new static($id, $text, $line, $position);
$position += strlen($text);
}
return $tokens;
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 31.129.215.166)
----
Manual Page -- https://php.net/manual/en/class.phptoken.php
Edit -- https://main.php.net/note/edit/126646
Del: integrated -- https://main.php.net/note/delete/126646/integrated
Del: useless -- https://main.php.net/note/delete/126646/useless
Del: bad code -- https://main.php.net/note/delete/126646/bad+code
Del: spam -- https://main.php.net/note/delete/126646/spam
Del: non-english -- https://main.php.net/note/delete/126646/non-english
Del: in docs -- https://main.php.net/note/delete/126646/in+docs
Del: other reasons-- https://main.php.net/note/delete/126646
Reject -- https://main.php.net/note/reject/126646
Search -- https://main.php.net/manage/user-notes.php