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 ;
@@ -137,10 +145,12 @@ public function setPosition($iPosition): void
137
145
public function parseIdentifier ($ bIgnoreCase = true )
138
146
{
139
147
if ($ this ->isEnd ()) {
148
+ $ this ->logger ->error ('Unexpected end of file while parsing identifier at line {line} ' , ['line ' => $ this ->iLineNo ]);
140
149
throw new UnexpectedEOFException ('' , '' , 'identifier ' , $ this ->iLineNo );
141
150
}
142
151
$ sResult = $ this ->parseCharacter (true );
143
152
if ($ sResult === null ) {
153
+ $ this ->logger ->error ('Unexpected token while parsing identifier at line {line} ' , ['line ' => $ this ->iLineNo ]);
144
154
throw new UnexpectedTokenException ($ sResult , $ this ->peek (5 ), 'identifier ' , $ this ->iLineNo );
145
155
}
146
156
$ sCharacter = null ;
@@ -287,13 +297,15 @@ public function consume($mValue = 1): string
287
297
$ iLineCount = substr_count ($ mValue , "\n" );
288
298
$ iLength = $ this ->strlen ($ mValue );
289
299
if (!$ this ->streql ($ this ->substr ($ this ->iCurrentPosition , $ iLength ), $ mValue )) {
300
+ $ this ->logger ->error ('Unexpected token "{token}" at line {line} ' , ['token ' => $ mValue , 'line ' => $ this ->iLineNo ]);
290
301
throw new UnexpectedTokenException ($ mValue , $ this ->peek (max ($ iLength , 5 )), $ this ->iLineNo );
291
302
}
292
303
$ this ->iLineNo += $ iLineCount ;
293
304
$ this ->iCurrentPosition += $ this ->strlen ($ mValue );
294
305
return $ mValue ;
295
306
} else {
296
307
if ($ this ->iCurrentPosition + $ mValue > $ this ->iLength ) {
308
+ $ this ->logger ->error ('Unexpected end of file while consuming {count} chars at line {line} ' , ['count ' => $ mValue , 'line ' => $ this ->iLineNo ]);
297
309
throw new UnexpectedEOFException ($ mValue , $ this ->peek (5 ), 'count ' , $ this ->iLineNo );
298
310
}
299
311
$ sResult = $ this ->substr ($ this ->iCurrentPosition , $ mValue );
@@ -318,6 +330,14 @@ public function consumeExpression($mExpression, $iMaxLength = null): string
318
330
if (preg_match ($ mExpression , $ sInput , $ aMatches , PREG_OFFSET_CAPTURE ) === 1 ) {
319
331
return $ this ->consume ($ aMatches [0 ][0 ]);
320
332
}
333
+ $ this ->logger ->error (
334
+ 'Unexpected expression "{token}" instead of {expression} at line {line} ' ,
335
+ [
336
+ 'token ' => $ this ->peek (5 ),
337
+ 'expression ' => $ mExpression ,
338
+ 'line ' => $ this ->iLineNo ,
339
+ ]
340
+ );
321
341
throw new UnexpectedTokenException ($ mExpression , $ this ->peek (5 ), 'expression ' , $ this ->iLineNo );
322
342
}
323
343
@@ -391,6 +411,10 @@ public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, a
391
411
}
392
412
393
413
$ this ->iCurrentPosition = $ start ;
414
+ $ this ->logger ->error (
415
+ 'Unexpected end of file while searching for one of "{end}" at line {line} ' ,
416
+ ['end ' => implode ('"," ' , $ aEnd ), 'line ' => $ this ->iLineNo ]
417
+ );
394
418
throw new UnexpectedEOFException (
395
419
'One of (" ' . implode ('"," ' , $ aEnd ) . '") ' ,
396
420
$ this ->peek (5 ),
0 commit comments