22
33namespace GraphQL \Parser ;
44
5+ use GraphQL \Errors \GraphQLError ;
56use GraphQL \Errors \UnexpectedTokenError ;
67
78/**
@@ -13,9 +14,17 @@ class Tokenizer
1314 private $ string = "" ;
1415 private $ cursor = 0 ;
1516
17+ private $ location = [
18+ "line " => 1 ,
19+ "column " => 1
20+ ];
1621 private $ locationHistory = [];
1722
1823 private $ spec = [
24+ // --------------------------------------------
25+ // New-Line(s):
26+ ['/^\n+/ ' , "NL " ],
27+
1928 // --------------------------------------------
2029 // Whitespace(s):
2130 ['/^\s+/ ' , null ],
@@ -120,8 +129,16 @@ public function getNextToken(): ?array
120129 continue ;
121130 }
122131
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
124140 if ($ tokenType === null ) {
141+ $ this ->location ["column " ] += strlen ($ tokenValue );
125142 return $ this ->getNextToken ();
126143 }
127144
@@ -165,6 +182,7 @@ public function match(string $regexp, string $string)
165182 }
166183
167184 $ this ->cursor += strlen ($ matches [0 ]); // add matched length to cursor
185+
168186 return $ matches [0 ];
169187 }
170188
@@ -175,20 +193,7 @@ public function match(string $regexp, string $string)
175193 */
176194 public function getLocation (): array
177195 {
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 ;
192197 }
193198
194199 /**
@@ -199,7 +204,8 @@ public function getLocation(): array
199204 public function getLastLocation ()
200205 {
201206 $ historyLength = count ($ this ->locationHistory );
202- if ($ historyLength <= 1 ) return $ this ->getLocation ();
207+ if ($ historyLength <= 1 )
208+ return $ this ->getLocation ();
203209 return $ this ->locationHistory [$ historyLength - 1 ];
204210 }
205211
0 commit comments