@@ -269,34 +269,6 @@ protected function pushToken($type, $value = null)
269269 $ this ->tokens [] = new Token ($ type , $ this ->line , $ tokenPositionInLine , $ this ->filename , $ value );
270270 }
271271
272- /**
273- * @param int $endType
274- * @param string $endRegex
275- *
276- * @throws Exception
277- */
278- protected function lex ($ endType , $ endRegex )
279- {
280- preg_match ($ endRegex , $ this ->code , $ match , PREG_OFFSET_CAPTURE , $ this ->cursor );
281-
282- if (!empty ($ this ->brackets ) || !isset ($ match [0 ])) {
283- $ this ->lexExpression ();
284- } elseif ($ match [0 ][1 ] === $ this ->cursor ) {
285- $ this ->pushToken ($ endType , $ match [0 ][0 ]);
286- $ this ->moveCursor ($ match [0 ][0 ]);
287- $ this ->moveCurrentPosition ();
288- $ this ->popState ();
289- } elseif ($ this ->getState () === self ::STATE_COMMENT ) {
290- // Parse as text until the end position.
291- $ this ->lexData ($ match [0 ][1 ]);
292- } else {
293- // Should not happen
294- while ($ this ->cursor < $ match [0 ][1 ]) {
295- $ this ->lexExpression ();
296- }
297- }
298- }
299-
300272 /**
301273 * @throws Exception
302274 */
@@ -357,23 +329,56 @@ protected function lexExpression()
357329 */
358330 protected function lexBlock ()
359331 {
360- $ this ->lex (Token::BLOCK_END_TYPE , $ this ->regexes ['lex_block ' ]);
332+ $ endRegex = $ this ->regexes ['lex_block ' ];
333+ preg_match ($ endRegex , $ this ->code , $ match , PREG_OFFSET_CAPTURE , $ this ->cursor );
334+
335+ if (!empty ($ this ->brackets ) || !isset ($ match [0 ])) {
336+ $ this ->lexExpression ();
337+ } else {
338+ $ this ->pushToken (Token::BLOCK_END_TYPE , $ match [0 ][0 ]);
339+ $ this ->moveCursor ($ match [0 ][0 ]);
340+ $ this ->moveCurrentPosition ();
341+ $ this ->popState ();
342+ }
361343 }
362344
363345 /**
364346 * @throws Exception
365347 */
366348 protected function lexVariable ()
367349 {
368- $ this ->lex (Token::VAR_END_TYPE , $ this ->regexes ['lex_variable ' ]);
350+ $ endRegex = $ this ->regexes ['lex_variable ' ];
351+ preg_match ($ endRegex , $ this ->code , $ match , PREG_OFFSET_CAPTURE , $ this ->cursor );
352+
353+ if (!empty ($ this ->brackets ) || !isset ($ match [0 ])) {
354+ $ this ->lexExpression ();
355+ } else {
356+ $ this ->pushToken (Token::VAR_END_TYPE , $ match [0 ][0 ]);
357+ $ this ->moveCursor ($ match [0 ][0 ]);
358+ $ this ->moveCurrentPosition ();
359+ $ this ->popState ();
360+ }
369361 }
370362
371363 /**
372364 * @throws Exception
373365 */
374366 protected function lexComment ()
375367 {
376- $ this ->lex (Token::COMMENT_END_TYPE , $ this ->regexes ['lex_comment ' ]);
368+ $ endRegex = $ this ->regexes ['lex_comment ' ];
369+ preg_match ($ endRegex , $ this ->code , $ match , PREG_OFFSET_CAPTURE , $ this ->cursor );
370+
371+ if (!isset ($ match [0 ])) {
372+ throw new Exception ('Unclosed comment ' );
373+ } elseif ($ match [0 ][1 ] === $ this ->cursor ) {
374+ $ this ->pushToken (Token::COMMENT_END_TYPE , $ match [0 ][0 ]);
375+ $ this ->moveCursor ($ match [0 ][0 ]);
376+ $ this ->moveCurrentPosition ();
377+ $ this ->popState ();
378+ } else {
379+ // Parse as text until the end position.
380+ $ this ->lexData ($ match [0 ][1 ]);
381+ }
377382 }
378383
379384 /**
0 commit comments