6
6
use Sabberworm \CSS \Parsing \Anchor ;
7
7
use Sabberworm \CSS \Settings ;
8
8
9
- class ParserState
9
+ use Psr \Log \LoggerAwareInterface ;
10
+ use Psr \Log \LoggerAwareTrait ;
11
+ use Psr \Log \NullLogger ;
12
+
13
+ class ParserState implements LoggerAwareInterface
10
14
{
15
+ use LoggerAwareTrait;
16
+
11
17
/**
12
18
* @var null
13
19
*
@@ -58,6 +64,8 @@ class ParserState
58
64
*/
59
65
public function __construct ($ sText , Settings $ oParserSettings , $ iLineNo = 1 )
60
66
{
67
+ $ this ->logger = new NullLogger ();
68
+
61
69
$ this ->oParserSettings = $ oParserSettings ;
62
70
$ this ->sText = $ sText ;
63
71
$ this ->iCurrentPosition = 0 ;
@@ -141,10 +149,12 @@ public function setPosition($iPosition)
141
149
public function parseIdentifier ($ bIgnoreCase = true )
142
150
{
143
151
if ($ this ->isEnd ()) {
152
+ $ this ->logger ->error ('Unexpected end of file while parsing identifier at line {line} ' , ['line ' => $ this ->iLineNo ]);
144
153
throw new UnexpectedEOFException ('' , '' , 'identifier ' , $ this ->iLineNo );
145
154
}
146
155
$ sResult = $ this ->parseCharacter (true );
147
156
if ($ sResult === null ) {
157
+ $ this ->logger ->error ('Unexpected token while parsing identifier at line {line} ' , ['line ' => $ this ->iLineNo ]);
148
158
throw new UnexpectedTokenException ($ sResult , $ this ->peek (5 ), 'identifier ' , $ this ->iLineNo );
149
159
}
150
160
$ sCharacter = null ;
@@ -291,13 +301,15 @@ public function consume($mValue = 1): string
291
301
$ iLineCount = substr_count ($ mValue , "\n" );
292
302
$ iLength = $ this ->strlen ($ mValue );
293
303
if (!$ this ->streql ($ this ->substr ($ this ->iCurrentPosition , $ iLength ), $ mValue )) {
304
+ $ this ->logger ->error ('Unexpected token "{token}" at line {line} ' , ['token ' => $ mValue , 'line ' => $ this ->iLineNo ]);
294
305
throw new UnexpectedTokenException ($ mValue , $ this ->peek (max ($ iLength , 5 )), $ this ->iLineNo );
295
306
}
296
307
$ this ->iLineNo += $ iLineCount ;
297
308
$ this ->iCurrentPosition += $ this ->strlen ($ mValue );
298
309
return $ mValue ;
299
310
} else {
300
311
if ($ this ->iCurrentPosition + $ mValue > $ this ->iLength ) {
312
+ $ this ->logger ->error ('Unexpected end of file while consuming {count} chars at line {line} ' , ['count ' => $ mValue , 'line ' => $ this ->iLineNo ]);
301
313
throw new UnexpectedEOFException ($ mValue , $ this ->peek (5 ), 'count ' , $ this ->iLineNo );
302
314
}
303
315
$ sResult = $ this ->substr ($ this ->iCurrentPosition , $ mValue );
@@ -324,6 +336,14 @@ public function consumeExpression($mExpression, $iMaxLength = null)
324
336
if (preg_match ($ mExpression , $ sInput , $ aMatches , PREG_OFFSET_CAPTURE ) === 1 ) {
325
337
return $ this ->consume ($ aMatches [0 ][0 ]);
326
338
}
339
+ $ this ->logger ->error (
340
+ 'Unexpected expression "{token}" instead of {expression} at line {line} ' ,
341
+ [
342
+ 'token ' => $ this ->peek (5 ),
343
+ 'expression ' => $ mExpression ,
344
+ 'line ' => $ this ->iLineNo ,
345
+ ]
346
+ );
327
347
throw new UnexpectedTokenException ($ mExpression , $ this ->peek (5 ), 'expression ' , $ this ->iLineNo );
328
348
}
329
349
@@ -397,6 +417,10 @@ public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, a
397
417
}
398
418
399
419
$ this ->iCurrentPosition = $ start ;
420
+ $ this ->logger ->error (
421
+ 'Unexpected end of file while searching for one of "{end}" at line {line} ' ,
422
+ ['end ' => implode ('"," ' , $ aEnd ), 'line ' => $ this ->iLineNo ]
423
+ );
400
424
throw new UnexpectedEOFException (
401
425
'One of (" ' . implode ('"," ' , $ aEnd ) . '") ' ,
402
426
$ this ->peek (5 ),
0 commit comments