• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MyIntervals / PHP-CSS-Parser / 16112982700

07 Jul 2025 09:18AM UTC coverage: 57.935%. Remained the same
16112982700

Pull #1307

github

web-flow
Merge 81b4533d5 into 674cfa00c
Pull Request #1307: [DOCS] Temporarily drop some markers from the class diagram

1055 of 1821 relevant lines covered (57.94%)

16.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Parsing/ParserState.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Sabberworm\CSS\Parsing;
6

7
use Sabberworm\CSS\Comment\Comment;
8
use Sabberworm\CSS\Settings;
9

10
/**
11
 * @internal since 8.7.0
12
 */
13
class ParserState
14
{
15
    /**
16
     * @var null
17
     */
18
    public const EOF = null;
19

20
    /**
21
     * @var Settings
22
     */
23
    private $parserSettings;
24

25
    /**
26
     * @var string
27
     */
28
    private $text;
29

30
    /**
31
     * @var array<int, string>
32
     */
33
    private $characters;
34

35
    /**
36
     * @var int<0, max>
37
     */
38
    private $currentPosition = 0;
39

40
    /**
41
     * will only be used if the CSS does not contain an `@charset` declaration
42
     *
43
     * @var string
44
     */
45
    private $charset;
46

47
    /**
48
     * @var int<1, max> $lineNumber
49
     */
50
    private $lineNumber;
51

52
    /**
53
     * @param string $text the complete CSS as text (i.e., usually the contents of a CSS file)
54
     * @param int<1, max> $lineNumber
55
     */
56
    public function __construct(string $text, Settings $parserSettings, int $lineNumber = 1)
×
57
    {
58
        $this->parserSettings = $parserSettings;
×
59
        $this->text = $text;
×
60
        $this->lineNumber = $lineNumber;
×
61
        $this->setCharset($this->parserSettings->getDefaultCharset());
×
62
    }
×
63

64
    /**
65
     * Sets the charset to be used if the CSS does not contain an `@charset` declaration.
66
     *
67
     * @throws SourceException if the charset is UTF-8 and the content has invalid byte sequences
68
     */
69
    public function setCharset(string $charset): void
×
70
    {
71
        $this->charset = $charset;
×
72
        $this->characters = $this->strsplit($this->text);
×
73
    }
×
74

75
    /**
76
     * @return int<1, max>
77
     */
78
    public function currentLine(): int
×
79
    {
80
        return $this->lineNumber;
×
81
    }
82

83
    /**
84
     * @return int<0, max>
85
     */
86
    public function currentColumn(): int
×
87
    {
88
        return $this->currentPosition;
×
89
    }
90

91
    public function getSettings(): Settings
×
92
    {
93
        return $this->parserSettings;
×
94
    }
95

96
    public function anchor(): Anchor
×
97
    {
98
        return new Anchor($this->currentPosition, $this);
×
99
    }
100

101
    /**
102
     * @param int<0, max> $position
103
     */
104
    public function setPosition(int $position): void
×
105
    {
106
        $this->currentPosition = $position;
×
107
    }
×
108

109
    /**
110
     * @return non-empty-string
111
     *
112
     * @throws UnexpectedTokenException
113
     */
114
    public function parseIdentifier(bool $ignoreCase = true): string
×
115
    {
116
        if ($this->isEnd()) {
×
117
            throw new UnexpectedEOFException('', '', 'identifier', $this->lineNumber);
×
118
        }
119
        $result = $this->parseCharacter(true);
×
120
        if ($result === null) {
×
121
            throw new UnexpectedTokenException('', $this->peek(5), 'identifier', $this->lineNumber);
×
122
        }
123
        $character = null;
×
124
        while (!$this->isEnd() && ($character = $this->parseCharacter(true)) !== null) {
×
125
            if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character)) {
×
126
                $result .= $character;
×
127
            } else {
128
                $result .= '\\' . $character;
×
129
            }
130
        }
131
        if ($ignoreCase) {
×
132
            $result = $this->strtolower($result);
×
133
        }
134

135
        return $result;
×
136
    }
137

138
    /**
139
     * @throws UnexpectedEOFException
140
     * @throws UnexpectedTokenException
141
     */
142
    public function parseCharacter(bool $isForIdentifier): ?string
×
143
    {
144
        if ($this->peek() === '\\') {
×
145
            $this->consume('\\');
×
146
            if ($this->comes('\\n') || $this->comes('\\r')) {
×
147
                return '';
×
148
            }
149
            if (\preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
×
150
                return $this->consume(1);
×
151
            }
152
            $hexCodePoint = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
×
153
            if ($this->strlen($hexCodePoint) < 6) {
×
154
                // Consume whitespace after incomplete unicode escape
155
                if (\preg_match('/\\s/isSu', $this->peek())) {
×
156
                    if ($this->comes('\\r\\n')) {
×
157
                        $this->consume(2);
×
158
                    } else {
159
                        $this->consume(1);
×
160
                    }
161
                }
162
            }
163
            $codePoint = \intval($hexCodePoint, 16);
×
164
            $utf32EncodedCharacter = '';
×
165
            for ($i = 0; $i < 4; ++$i) {
×
166
                $utf32EncodedCharacter .= \chr($codePoint & 0xff);
×
167
                $codePoint = $codePoint >> 8;
×
168
            }
169
            return \iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
×
170
        }
171
        if ($isForIdentifier) {
×
172
            $peek = \ord($this->peek());
×
173
            // Ranges: a-z A-Z 0-9 - _
174
            if (
175
                ($peek >= 97 && $peek <= 122)
×
176
                || ($peek >= 65 && $peek <= 90)
×
177
                || ($peek >= 48 && $peek <= 57)
×
178
                || ($peek === 45)
×
179
                || ($peek === 95)
×
180
                || ($peek > 0xa1)
×
181
            ) {
182
                return $this->consume(1);
×
183
            }
184
        } else {
185
            return $this->consume(1);
×
186
        }
187

188
        return null;
×
189
    }
190

191
    /**
192
     * @return list<Comment>
193
     *
194
     * @throws UnexpectedEOFException
195
     * @throws UnexpectedTokenException
196
     */
197
    public function consumeWhiteSpace(): array
×
198
    {
199
        $comments = [];
×
200
        do {
201
            while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
×
202
                $this->consume(1);
×
203
            }
204
            if ($this->parserSettings->usesLenientParsing()) {
×
205
                try {
206
                    $comment = $this->consumeComment();
×
207
                } catch (UnexpectedEOFException $e) {
×
208
                    $this->currentPosition = \count($this->characters);
×
209
                    break;
×
210
                }
211
            } else {
212
                $comment = $this->consumeComment();
×
213
            }
214
            if ($comment instanceof Comment) {
×
215
                $comments[] = $comment;
×
216
            }
217
        } while ($comment instanceof Comment);
×
218

219
        return $comments;
×
220
    }
221

222
    /**
223
     * @param non-empty-string $string
224
     */
225
    public function comes(string $string, bool $caseInsensitive = false): bool
×
226
    {
227
        $peek = $this->peek(\strlen($string));
×
228

229
        return ($peek !== '') && $this->streql($peek, $string, $caseInsensitive);
×
230
    }
231

232
    /**
233
     * @param int<1, max> $length
234
     * @param int<0, max> $offset
235
     */
236
    public function peek(int $length = 1, int $offset = 0): string
×
237
    {
238
        $offset += $this->currentPosition;
×
239
        if ($offset >= \count($this->characters)) {
×
240
            return '';
×
241
        }
242

243
        return $this->substr($offset, $length);
×
244
    }
245

246
    /**
247
     * @param string|int<1, max> $value
248
     *
249
     * @throws UnexpectedEOFException
250
     * @throws UnexpectedTokenException
251
     */
252
    public function consume($value = 1): string
×
253
    {
254
        if (\is_string($value)) {
×
255
            $numberOfLines = \substr_count($value, "\n");
×
256
            $length = $this->strlen($value);
×
257
            if (!$this->streql($this->substr($this->currentPosition, $length), $value)) {
×
258
                throw new UnexpectedTokenException(
×
259
                    $value,
×
260
                    $this->peek(\max($length, 5)),
×
261
                    'literal',
×
262
                    $this->lineNumber
×
263
                );
264
            }
265

266
            $this->lineNumber += $numberOfLines;
×
267
            $this->currentPosition += $this->strlen($value);
×
268
            $result = $value;
×
269
        } else {
270
            if ($this->currentPosition + $value > \count($this->characters)) {
×
271
                throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
×
272
            }
273

274
            $result = $this->substr($this->currentPosition, $value);
×
275
            $numberOfLines = \substr_count($result, "\n");
×
276
            $this->lineNumber += $numberOfLines;
×
277
            $this->currentPosition += $value;
×
278
        }
279

280
        return $result;
×
281
    }
282

283
    /**
284
     * @param string $expression
285
     * @param int<1, max>|null $maximumLength
286
     *
287
     * @throws UnexpectedEOFException
288
     * @throws UnexpectedTokenException
289
     */
290
    public function consumeExpression(string $expression, ?int $maximumLength = null): string
×
291
    {
292
        $matches = null;
×
293
        $input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
×
294
        if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
×
295
            throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
×
296
        }
297

298
        return $this->consume($matches[0][0]);
×
299
    }
300

301
    /**
302
     * @return Comment|false
303
     */
304
    public function consumeComment()
×
305
    {
306
        $lineNumber = $this->lineNumber;
×
307
        $comment = null;
×
308

309
        if ($this->comes('/*')) {
×
310
            $this->consume(1);
×
311
            $comment = '';
×
312
            while (($char = $this->consume(1)) !== '') {
×
313
                $comment .= $char;
×
314
                if ($this->comes('*/')) {
×
315
                    $this->consume(2);
×
316
                    break;
×
317
                }
318
            }
319
        }
320

321
        // We skip the * which was included in the comment.
322
        return \is_string($comment) ? new Comment(\substr($comment, 1), $lineNumber) : false;
×
323
    }
324

325
    public function isEnd(): bool
×
326
    {
327
        return $this->currentPosition >= \count($this->characters);
×
328
    }
329

330
    /**
331
     * @param list<string>|string $stopCharacters
332
     * @param array<int, Comment> $comments
333
     *
334
     * @throws UnexpectedEOFException
335
     * @throws UnexpectedTokenException
336
     */
337
    public function consumeUntil(
×
338
        $stopCharacters,
339
        bool $includeEnd = false,
340
        bool $consumeEnd = false,
341
        array &$comments = []
342
    ): string {
343
        $stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
×
344
        $consumedCharacters = '';
×
345
        $start = $this->currentPosition;
×
346

347
        while (!$this->isEnd()) {
×
348
            $character = $this->consume(1);
×
349
            if (\in_array($character, $stopCharacters, true)) {
×
350
                if ($includeEnd) {
×
351
                    $consumedCharacters .= $character;
×
352
                } elseif (!$consumeEnd) {
×
353
                    $this->currentPosition -= $this->strlen($character);
×
354
                }
355
                return $consumedCharacters;
×
356
            }
357
            $consumedCharacters .= $character;
×
358
            $comment = $this->consumeComment();
×
359
            if ($comment instanceof Comment) {
×
360
                $comments[] = $comment;
×
361
            }
362
        }
363

364
        if (\in_array(self::EOF, $stopCharacters, true)) {
×
365
            return $consumedCharacters;
×
366
        }
367

368
        $this->currentPosition = $start;
×
369
        throw new UnexpectedEOFException(
×
370
            'One of ("' . \implode('","', $stopCharacters) . '")',
×
371
            $this->peek(5),
×
372
            'search',
×
373
            $this->lineNumber
×
374
        );
375
    }
376

377
    private function inputLeft(): string
×
378
    {
379
        return $this->substr($this->currentPosition, -1);
×
380
    }
381

382
    public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
×
383
    {
384
        return $caseInsensitive
×
385
            ? ($this->strtolower($string1) === $this->strtolower($string2))
×
386
            : ($string1 === $string2);
×
387
    }
388

389
    /**
390
     * @param int<1, max> $numberOfCharacters
391
     */
392
    public function backtrack(int $numberOfCharacters): void
×
393
    {
394
        $this->currentPosition -= $numberOfCharacters;
×
395
    }
×
396

397
    /**
398
     * @return int<0, max>
399
     */
400
    public function strlen(string $string): int
×
401
    {
402
        return $this->parserSettings->hasMultibyteSupport()
×
403
            ? \mb_strlen($string, $this->charset)
×
404
            : \strlen($string);
×
405
    }
406

407
    /**
408
     * @param int<0, max> $offset
409
     */
410
    private function substr(int $offset, int $length): string
×
411
    {
412
        if ($length < 0) {
×
413
            $length = \count($this->characters) - $offset + $length;
×
414
        }
415
        if ($offset + $length > \count($this->characters)) {
×
416
            $length = \count($this->characters) - $offset;
×
417
        }
418
        $result = '';
×
419
        while ($length > 0) {
×
420
            $result .= $this->characters[$offset];
×
421
            $offset++;
×
422
            $length--;
×
423
        }
424

425
        return $result;
×
426
    }
427

428
    /**
429
     * @return ($string is non-empty-string ? non-empty-string : string)
430
     */
431
    private function strtolower(string $string): string
×
432
    {
433
        return $this->parserSettings->hasMultibyteSupport()
×
434
            ? \mb_strtolower($string, $this->charset)
×
435
            : \strtolower($string);
×
436
    }
437

438
    /**
439
     * @return list<string>
440
     *
441
     * @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
442
     */
443
    private function strsplit(string $string): array
×
444
    {
445
        if ($this->parserSettings->hasMultibyteSupport()) {
×
446
            if ($this->streql($this->charset, 'utf-8')) {
×
447
                $result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
×
448
                if (!\is_array($result)) {
×
449
                    throw new SourceException('`preg_split` failed with error ' . \preg_last_error());
×
450
                }
451
            } else {
452
                $length = \mb_strlen($string, $this->charset);
×
453
                $result = [];
×
454
                for ($i = 0; $i < $length; ++$i) {
×
455
                    $result[] = \mb_substr($string, $i, 1, $this->charset);
×
456
                }
457
            }
458
        } else {
459
            $result = ($string !== '') ? \str_split($string) : [];
×
460
        }
461

462
        return $result;
×
463
    }
464
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc