1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fubhy\GraphQL\Language; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A representation of a lexed Token. |
7
|
|
|
*/ |
8
|
|
|
class Token |
9
|
|
|
{ |
10
|
|
|
const EOF_TYPE = 1; |
11
|
|
|
const BANG_TYPE = 2; |
12
|
|
|
const DOLLAR_TYPE = 3; |
13
|
|
|
const PAREN_L_TYPE = 4; |
14
|
|
|
const PAREN_R_TYPE = 5; |
15
|
|
|
const SPREAD_TYPE = 6; |
16
|
|
|
const COLON_TYPE = 7; |
17
|
|
|
const EQUALS_TYPE = 8; |
18
|
|
|
const AT_TYPE = 9; |
19
|
|
|
const BRACKET_L_TYPE = 10; |
20
|
|
|
const BRACKET_R_TYPE = 11; |
21
|
|
|
const BRACE_L_TYPE = 12; |
22
|
|
|
const PIPE_TYPE = 13; |
23
|
|
|
const BRACE_R_TYPE = 14; |
24
|
|
|
const NAME_TYPE = 15; |
25
|
|
|
const VARIABLE_TYPE = 16; |
26
|
|
|
const INT_TYPE = 17; |
27
|
|
|
const FLOAT_TYPE = 18; |
28
|
|
|
const STRING_TYPE = 19; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The token type. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $type; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The start position. |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $start; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The end position. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $end; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The token value. |
53
|
|
|
* |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
protected $value; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param int $type |
62
|
|
|
* The token type. |
63
|
|
|
* @param int $start |
64
|
|
|
* The start position. |
65
|
|
|
* @param int $end |
66
|
|
|
* The end position. |
67
|
|
|
* @param null|string $value |
68
|
|
|
* The token value. |
69
|
|
|
*/ |
70
|
426 |
|
public function __construct($type, $start, $end, $value = NULL) |
71
|
|
|
{ |
72
|
426 |
|
$this->type = $type; |
73
|
426 |
|
$this->start = $start; |
74
|
426 |
|
$this->end = $end; |
75
|
426 |
|
$this->value = $value; |
76
|
426 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets the token type. |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
* The token type |
83
|
|
|
*/ |
84
|
309 |
|
public function getType() |
85
|
|
|
{ |
86
|
309 |
|
return $this->type; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets the start position. |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
* The start position. |
94
|
|
|
*/ |
95
|
309 |
|
public function getStart() |
96
|
|
|
{ |
97
|
309 |
|
return $this->start; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets the end position. |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
* The end position. |
105
|
|
|
*/ |
106
|
309 |
|
public function getEnd() |
107
|
|
|
{ |
108
|
309 |
|
return $this->end; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets the token value. |
113
|
|
|
* |
114
|
|
|
* @return string|null |
115
|
|
|
* The token value |
116
|
|
|
*/ |
117
|
309 |
|
public function getValue() |
118
|
|
|
{ |
119
|
309 |
|
return $this->value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Gets the token value. |
124
|
|
|
* |
125
|
|
|
* @return string|null |
126
|
|
|
* The token value |
127
|
|
|
*/ |
128
|
|
|
public function getDescription() |
129
|
|
|
{ |
130
|
|
|
return self::typeToString($this->type) . ($this->value ? ' "' . $this->value . '"' : ''); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns a string representation of the token. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
* A string representation of the token |
138
|
|
|
*/ |
139
|
|
|
public function __toString() |
140
|
|
|
{ |
141
|
|
|
$description = self::typeToString($this->type); |
142
|
|
|
return $this->value ? sprintf('%s "%s"', $description, $this->value) : $description; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the constant representation (internal) of a given type. |
147
|
|
|
* |
148
|
|
|
* @param int $type |
149
|
|
|
* The type as an integer |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
* The string representation |
153
|
|
|
*/ |
154
|
|
|
public static function typeToString($type) |
155
|
|
|
{ |
156
|
|
|
switch ($type) { |
157
|
|
|
case self::EOF_TYPE: |
158
|
|
|
return 'EOF'; |
159
|
|
|
case self::BANG_TYPE: |
160
|
|
|
return '!'; |
161
|
|
|
case self::DOLLAR_TYPE: |
162
|
|
|
return '$'; |
163
|
|
|
case self::PAREN_L_TYPE: |
164
|
|
|
return '('; |
165
|
|
|
case self::PAREN_R_TYPE: |
166
|
|
|
return ')'; |
167
|
|
|
case self::SPREAD_TYPE: |
168
|
|
|
return '...'; |
169
|
|
|
case self::COLON_TYPE: |
170
|
|
|
return ':'; |
171
|
|
|
case self::EQUALS_TYPE: |
172
|
|
|
return '='; |
173
|
|
|
case self::AT_TYPE: |
174
|
|
|
return '@'; |
175
|
|
|
case self::BRACKET_L_TYPE: |
176
|
|
|
return '['; |
177
|
|
|
case self::BRACKET_R_TYPE: |
178
|
|
|
return ']'; |
179
|
|
|
case self::BRACE_L_TYPE: |
180
|
|
|
return '{'; |
181
|
|
|
case self::PIPE_TYPE: |
182
|
|
|
return '|'; |
183
|
|
|
case self::BRACE_R_TYPE: |
184
|
|
|
return '}'; |
185
|
|
|
case self::NAME_TYPE: |
186
|
|
|
return 'Name'; |
187
|
|
|
case self::VARIABLE_TYPE: |
188
|
|
|
return 'Variable'; |
189
|
|
|
case self::INT_TYPE: |
190
|
|
|
return 'Int'; |
191
|
|
|
case self::FLOAT_TYPE: |
192
|
|
|
return 'Float'; |
193
|
|
|
case self::STRING_TYPE: |
194
|
|
|
return 'String'; |
195
|
|
|
default: |
196
|
|
|
throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|