| 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 |
* @access private |
| 12 |
*/ |
| 13 |
class WP_Parser_Token { |
| 14 |
/** |
| 15 |
* Token ID represented as an integer constant. |
| 16 |
* |
| 17 |
* @var int $id |
| 18 |
*/ |
| 19 |
public $id; |
| 20 |
|
| 21 |
/** |
| 22 |
* Byte offset in the input where the token begins. |
| 23 |
* |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
public $start; |
| 27 |
|
| 28 |
/** |
| 29 |
* Byte length of the token in the input. |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
public $length; |
| 34 |
|
| 35 |
/** |
| 36 |
* Input bytes from which the token was parsed. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $input; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
* @param int $id Token type. |
| 46 |
* @param int $start Byte offset in the input where the token begins. |
| 47 |
* @param int $length Byte length of the token in the input. |
| 48 |
* @param string $input Input bytes from which the token was parsed. |
| 49 |
*/ |
| 50 |
public function __construct( |
| 51 |
int $id, |
| 52 |
int $start, |
| 53 |
int $length, |
| 54 |
string $input |
| 55 |
) { |
| 56 |
$this->id = $id; |
| 57 |
$this->start = $start; |
| 58 |
$this->length = $length; |
| 59 |
$this->input = $input; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the raw bytes of the token from the input. |
| 64 |
* |
| 65 |
* @return string The token bytes. |
| 66 |
*/ |
| 67 |
public function get_bytes(): string { |
| 68 |
return substr( $this->input, $this->start, $this->length ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get the real unquoted value of the token. |
| 73 |
* |
| 74 |
* @return string The token value. |
| 75 |
*/ |
| 76 |
public function get_value(): string { |
| 77 |
return $this->get_bytes(); |
| 78 |
} |
| 79 |
} |
| 80 |
|