| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A token, representing a leaf in the parse tree. |
| 5 |
* |
| 6 |
* This class represents a token that is consumed and recognized by WP_Parser. |
| 7 |
* In a parse tree, a token represent a leaf, that is, a node without children. |
| 8 |
* It is a simple generic container for a token ID and value, that can be used |
| 9 |
* as a base class and extended for specific use cases. |
| 10 |
*/ |
| 11 |
class WP_Parser_Token { |
| 12 |
/** |
| 13 |
* Token ID represented as an integer constant. |
| 14 |
* |
| 15 |
* @var int $id |
| 16 |
*/ |
| 17 |
public $id; |
| 18 |
|
| 19 |
/** |
| 20 |
* Byte offset in the input where the token begins. |
| 21 |
* |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
public $start; |
| 25 |
|
| 26 |
/** |
| 27 |
* Byte length of the token in the input. |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
public $length; |
| 32 |
|
| 33 |
/** |
| 34 |
* Input bytes from which the token was parsed. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $input; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor. |
| 42 |
* |
| 43 |
* @param int $id Token type. |
| 44 |
* @param int $start Byte offset in the input where the token begins. |
| 45 |
* @param int $length Byte length of the token in the input. |
| 46 |
* @param string $input Input bytes from which the token was parsed. |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
int $id, |
| 50 |
int $start, |
| 51 |
int $length, |
| 52 |
string $input |
| 53 |
) { |
| 54 |
$this->id = $id; |
| 55 |
$this->start = $start; |
| 56 |
$this->length = $length; |
| 57 |
$this->input = $input; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the raw bytes of the token from the input. |
| 62 |
* |
| 63 |
* @return string The token bytes. |
| 64 |
*/ |
| 65 |
public function get_bytes(): string { |
| 66 |
return substr( $this->input, $this->start, $this->length ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the real unquoted value of the token. |
| 71 |
* |
| 72 |
* @return string The token value. |
| 73 |
*/ |
| 74 |
public function get_value(): string { |
| 75 |
return $this->get_bytes(); |
| 76 |
} |
| 77 |
} |
| 78 |
|