|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\GraphQLError; |
|
6
|
|
|
use Digia\GraphQL\Error\SyntaxError; |
|
7
|
|
|
use Digia\GraphQL\Language\Reader\ReaderInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Lexer implements LexerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var Source|null |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $source; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $options = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array|ReaderInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $readers; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The previously focused non-ignored token. |
|
29
|
|
|
* |
|
30
|
|
|
* @var Token |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $lastToken; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The currently focused non-ignored token. |
|
36
|
|
|
* |
|
37
|
|
|
* @var Token |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $token; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The (1-indexed) line containing the current token. |
|
43
|
|
|
* |
|
44
|
|
|
* @var int |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $line; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The character offset at which the current line begins. |
|
50
|
|
|
* |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $lineStart; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Lexer constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param ReaderInterface[] $readers |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(array $readers) |
|
61
|
|
|
{ |
|
62
|
|
|
$startOfFileToken = new Token(TokenKindEnum::SOF); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($readers as $reader) { |
|
65
|
|
|
$reader->setLexer($this); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->readers = $readers; |
|
69
|
|
|
$this->lastToken = $startOfFileToken; |
|
70
|
|
|
$this->token = $startOfFileToken; |
|
71
|
|
|
$this->line = 1; |
|
72
|
|
|
$this->lineStart = 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function advance(): Token |
|
79
|
|
|
{ |
|
80
|
|
|
$this->lastToken = $this->token; |
|
81
|
|
|
|
|
82
|
|
|
return $this->token = $this->lookahead(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function lookahead(): Token |
|
89
|
|
|
{ |
|
90
|
|
|
$token = $this->token; |
|
91
|
|
|
|
|
92
|
|
|
if (TokenKindEnum::EOF !== $token->getKind()) { |
|
93
|
|
|
do { |
|
94
|
|
|
$next = $this->readToken($token); |
|
95
|
|
|
$token->setNext($next); |
|
96
|
|
|
$token = $next; |
|
97
|
|
|
} while (TokenKindEnum::COMMENT === $token->getKind()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $token; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @inheritdoc |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getBody(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->getSource()->getBody(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @inheritdoc |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getTokenKind(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->token->getKind(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @inheritdoc |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getTokenValue(): ?string |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->token->getValue(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getToken(): Token |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->token; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @inheritdoc |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getSource(): Source |
|
139
|
|
|
{ |
|
140
|
|
|
if ($this->source instanceof Source) { |
|
141
|
|
|
return $this->source; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
throw new \Exception('No source has been set.'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @inheritdoc |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getLastToken(): Token |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->lastToken; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param Source $source |
|
157
|
|
|
* @return Lexer |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setSource(Source $source) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->source = $source; |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param array $options |
|
167
|
|
|
* @return |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setOptions(array $options) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->options = $options; |
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param int $code |
|
177
|
|
|
* @param int $pos |
|
178
|
|
|
* @param int $line |
|
179
|
|
|
* @param int $col |
|
180
|
|
|
* @param Token $prev |
|
181
|
|
|
* @return Token |
|
182
|
|
|
* @throws SyntaxError |
|
183
|
|
|
*/ |
|
184
|
|
|
public function read(int $code, int $pos, int $line, int $col, Token $prev): Token |
|
185
|
|
|
{ |
|
186
|
|
|
if (($reader = $this->getReader($code, $pos)) !== null) { |
|
187
|
|
|
return $reader->read($code, $pos, $line, $col, $prev); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
throw new SyntaxError($this->unexpectedCharacterMessage($code)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param Token $prev |
|
195
|
|
|
* @return Token |
|
196
|
|
|
* @throws GraphQLError |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function readToken(Token $prev): Token |
|
199
|
|
|
{ |
|
200
|
|
|
$body = $this->source->getBody(); |
|
|
|
|
|
|
201
|
|
|
$bodyLength = mb_strlen($body); |
|
202
|
|
|
|
|
203
|
|
|
$pos = $this->positionAfterWhitespace($body, $prev->getEnd()); |
|
204
|
|
|
$line = $this->line; |
|
205
|
|
|
$col = 1 + $pos - $this->lineStart; |
|
206
|
|
|
|
|
207
|
|
|
if ($pos >= $bodyLength) { |
|
208
|
|
|
return new Token(TokenKindEnum::EOF, $bodyLength, $bodyLength, $line, $col, $prev); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$code = charCodeAt($body, $pos); |
|
212
|
|
|
|
|
213
|
|
|
if (isSourceCharacter($code)) { |
|
214
|
|
|
throw new SyntaxError(sprintf('Cannot contain the invalid character %s', printCharCode($code))); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $this->read($code, $pos, $line, $col, $prev); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param int $code |
|
222
|
|
|
* @return string |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function unexpectedCharacterMessage(int $code): string |
|
225
|
|
|
{ |
|
226
|
|
|
if ($code === 39) { |
|
227
|
|
|
// ' |
|
228
|
|
|
return 'Unexpected single quote character (\'), did you mean to use a double quote (")?'; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return sprintf('Cannot parse the unexpected character %s', printCharCode($code)); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param int $code |
|
236
|
|
|
* @param int $pos |
|
237
|
|
|
* @return ReaderInterface|null |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function getReader(int $code, int $pos): ?ReaderInterface |
|
240
|
|
|
{ |
|
241
|
|
|
foreach ($this->readers as $reader) { |
|
242
|
|
|
if ($reader instanceof ReaderInterface && $reader->supportsReader($code, $pos)) { |
|
243
|
|
|
return $reader; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return null; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param string $body |
|
252
|
|
|
* @param int $startPosition |
|
253
|
|
|
* @return int |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function positionAfterWhitespace(string $body, int $startPosition): int |
|
256
|
|
|
{ |
|
257
|
|
|
$bodyLength = mb_strlen($body); |
|
258
|
|
|
$pos = $startPosition; |
|
259
|
|
|
|
|
260
|
|
|
while ($pos < $bodyLength) { |
|
261
|
|
|
$code = charCodeAt($body, $pos); |
|
262
|
|
|
|
|
263
|
|
|
if ($code === 9 || $code === 32 || $code === 44 || $code === 0xfeff) { |
|
264
|
|
|
// tab | space | comma | BOM |
|
265
|
|
|
++$pos; |
|
266
|
|
|
} elseif ($code === 10) { |
|
267
|
|
|
// new line |
|
268
|
|
|
++$pos; |
|
269
|
|
|
$this->advanceLine($pos); |
|
270
|
|
|
} elseif ($code === 13) { |
|
271
|
|
|
// carriage return |
|
272
|
|
|
if (charCodeAt($body, $pos + 1) === 10) { |
|
273
|
|
|
$pos += 2; |
|
274
|
|
|
} else { |
|
275
|
|
|
++$pos; |
|
276
|
|
|
} |
|
277
|
|
|
$this->advanceLine($pos); |
|
278
|
|
|
} else { |
|
279
|
|
|
break; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $pos; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param int $pos |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function advanceLine(int $pos) |
|
290
|
|
|
{ |
|
291
|
|
|
++$this->line; |
|
292
|
|
|
$this->lineStart = $pos; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.