| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast\Syntax; |
| 11 |
|
| 12 |
/** |
| 13 |
* A token emitted by the tokenizer. |
| 14 |
* |
| 15 |
* @author Marco Marchiò <[email protected]> |
| 16 |
*/ |
| 17 |
class Token implements \JSONSerializable |
| 18 |
{ |
| 19 |
//Type constants |
| 20 |
/** |
| 21 |
* Boolean literal |
| 22 |
*/ |
| 23 |
const TYPE_BOOLEAN_LITERAL = "Boolean"; |
| 24 |
|
| 25 |
/** |
| 26 |
* Identifier |
| 27 |
*/ |
| 28 |
const TYPE_IDENTIFIER = "Identifier"; |
| 29 |
|
| 30 |
/** |
| 31 |
* Private identifier |
| 32 |
*/ |
| 33 |
const TYPE_PRIVATE_IDENTIFIER = "PrivateIdentifier"; |
| 34 |
|
| 35 |
/** |
| 36 |
* Keyword |
| 37 |
*/ |
| 38 |
const TYPE_KEYWORD = "Keyword"; |
| 39 |
|
| 40 |
/** |
| 41 |
* Null literal |
| 42 |
*/ |
| 43 |
const TYPE_NULL_LITERAL = "Null"; |
| 44 |
|
| 45 |
/** |
| 46 |
* Numeric literal |
| 47 |
*/ |
| 48 |
const TYPE_NUMERIC_LITERAL = "Numeric"; |
| 49 |
|
| 50 |
/** |
| 51 |
* BigInt literal |
| 52 |
*/ |
| 53 |
const TYPE_BIGINT_LITERAL = "BigInt"; |
| 54 |
|
| 55 |
/** |
| 56 |
* Punctuator |
| 57 |
*/ |
| 58 |
const TYPE_PUNCTUATOR = "Punctuator"; |
| 59 |
|
| 60 |
//This constant is kept only for backward compatibility since it was |
| 61 |
//first written with a typo |
| 62 |
const TYPE_PUNCTUTATOR = "Punctuator"; |
| 63 |
|
| 64 |
/** |
| 65 |
* String literal |
| 66 |
*/ |
| 67 |
const TYPE_STRING_LITERAL = "String"; |
| 68 |
|
| 69 |
/** |
| 70 |
* Regular expression |
| 71 |
*/ |
| 72 |
const TYPE_REGULAR_EXPRESSION = "RegularExpression"; |
| 73 |
|
| 74 |
/** |
| 75 |
* Template |
| 76 |
*/ |
| 77 |
const TYPE_TEMPLATE = "Template"; |
| 78 |
|
| 79 |
/** |
| 80 |
* Comment |
| 81 |
*/ |
| 82 |
const TYPE_COMMENT = "Comment"; |
| 83 |
|
| 84 |
/** |
| 85 |
* JSX text |
| 86 |
*/ |
| 87 |
const TYPE_JSX_TEXT = "JSXText"; |
| 88 |
|
| 89 |
/** |
| 90 |
* JSX identifier |
| 91 |
*/ |
| 92 |
const TYPE_JSX_IDENTIFIER = "JSXIdentifier"; |
| 93 |
|
| 94 |
/** |
| 95 |
* Tokens' type that is one of the type constants |
| 96 |
* |
| 97 |
* @var string |
| 98 |
*/ |
| 99 |
public $type; |
| 100 |
|
| 101 |
/** |
| 102 |
* Token's value |
| 103 |
* |
| 104 |
* @var string |
| 105 |
*/ |
| 106 |
public $value; |
| 107 |
|
| 108 |
/** |
| 109 |
* Token's location in the source code |
| 110 |
* |
| 111 |
* @var SourceLocation |
| 112 |
*/ |
| 113 |
public $location; |
| 114 |
|
| 115 |
/** |
| 116 |
* Class constructor |
| 117 |
* |
| 118 |
* @param string $type Token's type |
| 119 |
* @param string $value Token's value |
| 120 |
*/ |
| 121 |
public function __construct($type, $value) |
| 122 |
{ |
| 123 |
$this->type = $type; |
| 124 |
$this->value = $value; |
| 125 |
$this->location = new SourceLocation(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the token's type |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function getType() |
| 134 |
{ |
| 135 |
return $this->type; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Returns the token's value |
| 140 |
* |
| 141 |
* @return string |
| 142 |
*/ |
| 143 |
public function getValue() |
| 144 |
{ |
| 145 |
return $this->value; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the token's location in the source code |
| 150 |
* |
| 151 |
* @return SourceLocation |
| 152 |
*/ |
| 153 |
public function getLocation() |
| 154 |
{ |
| 155 |
return $this->location; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Sets the start position of the token in the source code |
| 160 |
* |
| 161 |
* @param Position $position Start position |
| 162 |
* |
| 163 |
* @return $this |
| 164 |
*/ |
| 165 |
public function setStartPosition(Position $position) |
| 166 |
{ |
| 167 |
$this->location->start = $position; |
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Sets the end position of the token in the source code |
| 173 |
* |
| 174 |
* @param Position $position End position |
| 175 |
* |
| 176 |
* @return $this |
| 177 |
*/ |
| 178 |
public function setEndPosition(Position $position) |
| 179 |
{ |
| 180 |
$this->location->end = $position; |
| 181 |
return $this; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns a serializable version of the node |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
#[\ReturnTypeWillChange] |
| 190 |
public function jsonSerialize() |
| 191 |
{ |
| 192 |
return array( |
| 193 |
"type" => $this->type, |
| 194 |
"value" => $this->value, |
| 195 |
"location" => $this->location |
| 196 |
); |
| 197 |
} |
| 198 |
} |