2
2
3
3
namespace GraphQL \Parser ;
4
4
5
+ use GraphQL \Errors \GraphQLError ;
5
6
use GraphQL \Errors \UnexpectedTokenError ;
6
7
7
8
/**
@@ -13,9 +14,17 @@ class Tokenizer
13
14
private $ string = "" ;
14
15
private $ cursor = 0 ;
15
16
17
+ private $ location = [
18
+ "line " => 1 ,
19
+ "column " => 1
20
+ ];
16
21
private $ locationHistory = [];
17
22
18
23
private $ spec = [
24
+ // --------------------------------------------
25
+ // New-Line(s):
26
+ ['/^\n+/ ' , "NL " ],
27
+
19
28
// --------------------------------------------
20
29
// Whitespace(s):
21
30
['/^\s+/ ' , null ],
@@ -120,8 +129,16 @@ public function getNextToken(): ?array
120
129
continue ;
121
130
}
122
131
123
- // this token can be skipped, e.g. whitespace
132
+ // this token can be skipped (newline)
133
+ if ($ tokenType === "NL " ) {
134
+ $ this ->location ["line " ] += strlen ($ tokenValue );
135
+ $ this ->location ["column " ] = 1 ;
136
+ return $ this ->getNextToken ();
137
+ }
138
+
139
+ // this token can be skipped, e.g. whitespace or comment
124
140
if ($ tokenType === null ) {
141
+ $ this ->location ["column " ] += strlen ($ tokenValue );
125
142
return $ this ->getNextToken ();
126
143
}
127
144
@@ -165,6 +182,7 @@ public function match(string $regexp, string $string)
165
182
}
166
183
167
184
$ this ->cursor += strlen ($ matches [0 ]); // add matched length to cursor
185
+
168
186
return $ matches [0 ];
169
187
}
170
188
@@ -175,20 +193,7 @@ public function match(string $regexp, string $string)
175
193
*/
176
194
public function getLocation (): array
177
195
{
178
- $ location = [
179
- "line " => 1 ,
180
- "column " => 1 ,
181
- ];
182
- for ($ i = 0 ; $ i < $ this ->cursor ; $ i ++) {
183
- if ($ this ->string [$ i ] === "\n" ) {
184
- $ location ["line " ]++;
185
- $ location ["column " ] = 1 ;
186
- } else {
187
- $ location ["column " ]++;
188
- }
189
- }
190
-
191
- return $ location ;
196
+ return $ this ->location ;
192
197
}
193
198
194
199
/**
@@ -199,7 +204,8 @@ public function getLocation(): array
199
204
public function getLastLocation ()
200
205
{
201
206
$ historyLength = count ($ this ->locationHistory );
202
- if ($ historyLength <= 1 ) return $ this ->getLocation ();
207
+ if ($ historyLength <= 1 )
208
+ return $ this ->getLocation ();
203
209
return $ this ->locationHistory [$ historyLength - 1 ];
204
210
}
205
211
0 commit comments