@@ -146,8 +146,8 @@ public function __construct(Lexer $lexer, WhitespaceControl $whitespaceControl)
146146 */
147147 public function parse (string $ code ): Program
148148 {
149- $ tokens = $ this ->lexer ->tokenize ($ code );
150- $ this ->tokens = $ this -> postprocessTokens ( $ tokens ) ;
149+ $ this ->lexer ->initialize ($ code );
150+ $ this ->tokens = [] ;
151151 $ result = $ this ->doParse ();
152152 $ result = $ this ->whitespaceControl ->accept ($ result );
153153
@@ -161,33 +161,26 @@ public function parse(string $code): Program
161161 return $ result ;
162162 }
163163
164- /**
165- * @param list<Token> $tokens
166- * @return list<Token>
167- */
168- protected function postprocessTokens (array $ tokens ): array
164+ private function readNextToken (): Token
169165 {
170- $ numTokens = count ( $ tokens );
166+ $ token = $ this -> lexer -> getNextToken ( );
171167
172- if ($ numTokens === 0 ) {
173- // empty input - just add sentinel token
174- return [new Token (Lexer::T_EOF , "\0" , 0 , 0 )];
175- }
168+ if ($ token === null ) {
169+ if ($ this ->tokens ) {
170+ $ lastToken = end ($ this ->tokens );
171+ $ line = $ lastToken ->line ;
172+ $ column = $ lastToken ->column + strlen ($ lastToken ->text );
173+ } else {
174+ $ line = 0 ;
175+ $ column = 0 ;
176+ }
176177
177- $ lastToken = $ tokens [$ numTokens - 1 ];
178+ $ token = new Token (Lexer::T_EOF , "\0" , $ line , $ column );
179+ }
178180
179- // Add sentinel token
180- $ column = $ lastToken ->column + strlen ($ lastToken ->text );
181- $ tokens [] = new Token (Lexer::T_EOF , "\0" , $ lastToken ->line , $ column );
182- return $ tokens ;
183- }
181+ $ this ->tokens [] = $ token ;
184182
185- /**
186- * @return Token[]
187- */
188- public function getTokens (): array
189- {
190- return $ this ->tokens ;
183+ return $ token ;
191184 }
192185
193186 protected function doParse (): Program
@@ -218,7 +211,7 @@ protected function doParse(): Program
218211 $ rule = $ this ->actionDefault [$ state ];
219212 } else {
220213 if ($ symbol === self ::SYMBOL_NONE ) {
221- $ token = $ this ->tokens [++$ this ->tokenPos ];
214+ $ token = $ this ->tokens [++$ this ->tokenPos ] ?? $ this -> readNextToken () ;
222215 $ tokenName = $ token ->name ;
223216
224217 // Map the lexer token id to the internally used symbols.
0 commit comments