@@ -269,34 +269,6 @@ protected function pushToken($type, $value = null)
269
269
$ this ->tokens [] = new Token ($ type , $ this ->line , $ tokenPositionInLine , $ this ->filename , $ value );
270
270
}
271
271
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
-
300
272
/**
301
273
* @throws Exception
302
274
*/
@@ -357,23 +329,56 @@ protected function lexExpression()
357
329
*/
358
330
protected function lexBlock ()
359
331
{
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
+ }
361
343
}
362
344
363
345
/**
364
346
* @throws Exception
365
347
*/
366
348
protected function lexVariable ()
367
349
{
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
+ }
369
361
}
370
362
371
363
/**
372
364
* @throws Exception
373
365
*/
374
366
protected function lexComment ()
375
367
{
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
+ }
377
382
}
378
383
379
384
/**
0 commit comments