Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Parsing/ParserState.php

raw
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 use function Safe\iconv;
11 use function Safe\preg_match;
12 use function Safe\preg_split;
13
14 /**
15 * @internal since 8.7.0
16 */
17 class ParserState
18 {
19 public const EOF = null;
20
21 /**
22 * @var Settings
23 */
24 private $parserSettings;
25
26 /**
27 * @var string
28 */
29 private $text;
30
31 /**
32 * @var array<int, string>
33 */
34 private $characters;
35
36 /**
37 * @var int<0, max>
38 */
39 private $currentPosition = 0;
40
41 /**
42 * will only be used if the CSS does not contain an `@charset` declaration
43 *
44 * @var string
45 */
46 private $charset;
47
48 /**
49 * @var int<1, max> $lineNumber
50 */
51 private $lineNumber;
52
53 /**
54 * @param string $text the complete CSS as text (i.e., usually the contents of a CSS file)
55 * @param int<1, max> $lineNumber
56 */
57 public function __construct(string $text, Settings $parserSettings, int $lineNumber = 1)
58 {
59 $this->parserSettings = $parserSettings;
60 $this->text = $text;
61 $this->lineNumber = $lineNumber;
62 $this->setCharset($this->parserSettings->getDefaultCharset());
63 }
64
65 /**
66 * Sets the charset to be used if the CSS does not contain an `@charset` declaration.
67 */
68 public function setCharset(string $charset): void
69 {
70 $this->charset = $charset;
71 $this->characters = $this->strsplit($this->text);
72 }
73
74 /**
75 * @return int<1, max>
76 */
77 public function currentLine(): int
78 {
79 return $this->lineNumber;
80 }
81
82 /**
83 * @return int<0, max>
84 */
85 public function currentColumn(): int
86 {
87 return $this->currentPosition;
88 }
89
90 public function getSettings(): Settings
91 {
92 return $this->parserSettings;
93 }
94
95 public function anchor(): Anchor
96 {
97 return new Anchor($this->currentPosition, $this);
98 }
99
100 /**
101 * @param int<0, max> $position
102 */
103 public function setPosition(int $position): void
104 {
105 $this->currentPosition = $position;
106 }
107
108 /**
109 * @return non-empty-string
110 *
111 * @throws UnexpectedTokenException
112 */
113 public function parseIdentifier(bool $ignoreCase = true): string
114 {
115 if ($this->isEnd()) {
116 throw new UnexpectedEOFException('', '', 'identifier', $this->lineNumber);
117 }
118 $result = $this->parseCharacter(true);
119 if ($result === null) {
120 throw new UnexpectedTokenException('', $this->peek(5), 'identifier', $this->lineNumber);
121 }
122 $character = null;
123 while (!$this->isEnd() && ($character = $this->parseCharacter(true)) !== null) {
124 if (preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character) !== 0) {
125 $result .= $character;
126 } else {
127 $result .= '\\' . $character;
128 }
129 }
130 if ($ignoreCase) {
131 $result = $this->strtolower($result);
132 }
133
134 return $result;
135 }
136
137 /**
138 * @throws UnexpectedEOFException
139 * @throws UnexpectedTokenException
140 */
141 public function parseCharacter(bool $isForIdentifier): ?string
142 {
143 if ($this->peek() === '\\') {
144 $this->consume('\\');
145 if ($this->comes('\\n') || $this->comes('\\r')) {
146 return '';
147 }
148 if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
149 return $this->consume(1);
150 }
151 $hexCodePoint = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
152 if ($this->strlen($hexCodePoint) < 6) {
153 // Consume whitespace after incomplete unicode escape
154 if (preg_match('/\\s/isSu', $this->peek()) !== 0) {
155 if ($this->comes('\\r\\n')) {
156 $this->consume(2);
157 } else {
158 $this->consume(1);
159 }
160 }
161 }
162 $codePoint = \intval($hexCodePoint, 16);
163 $utf32EncodedCharacter = '';
164 for ($i = 0; $i < 4; ++$i) {
165 $utf32EncodedCharacter .= \chr($codePoint & 0xff);
166 $codePoint = $codePoint >> 8;
167 }
168 return iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
169 }
170 if ($isForIdentifier) {
171 $peek = \ord($this->peek());
172 // Ranges: a-z A-Z 0-9 - _
173 if (
174 ($peek >= 97 && $peek <= 122)
175 || ($peek >= 65 && $peek <= 90)
176 || ($peek >= 48 && $peek <= 57)
177 || ($peek === 45)
178 || ($peek === 95)
179 || ($peek > 0xa1)
180 ) {
181 return $this->consume(1);
182 }
183 } else {
184 return $this->consume(1);
185 }
186
187 return null;
188 }
189
190 /**
191 * Consumes whitespace and/or comments until the next non-whitespace character that isn't a slash opening a comment.
192 *
193 * @param list<Comment> $comments Any comments consumed will be appended to this array.
194 *
195 * @return string the whitespace consumed, without the comments
196 *
197 * @throws UnexpectedEOFException
198 * @throws UnexpectedTokenException
199 *
200 * @phpstan-impure
201 * This method may change the state of the object by advancing the internal position;
202 * it does not simply 'get' a value.
203 */
204 public function consumeWhiteSpace(array &$comments = []): string
205 {
206 $consumed = '';
207 do {
208 while (preg_match('/\\s/isSu', $this->peek()) === 1) {
209 $consumed .= $this->consume(1);
210 }
211 if ($this->parserSettings->usesLenientParsing()) {
212 try {
213 $comment = $this->consumeComment();
214 } catch (UnexpectedEOFException $e) {
215 $this->currentPosition = \count($this->characters);
216 break;
217 }
218 } else {
219 $comment = $this->consumeComment();
220 }
221 if ($comment instanceof Comment) {
222 $comments[] = $comment;
223 }
224 } while ($comment instanceof Comment);
225
226 return $consumed;
227 }
228
229 /**
230 * @param non-empty-string $string
231 */
232 public function comes(string $string, bool $caseInsensitive = false): bool
233 {
234 $peek = $this->peek(\strlen($string));
235
236 return ($peek !== '') && $this->streql($peek, $string, $caseInsensitive);
237 }
238
239 /**
240 * @param int<1, max> $length
241 * @param int<0, max> $offset
242 */
243 public function peek(int $length = 1, int $offset = 0): string
244 {
245 $offset += $this->currentPosition;
246 if ($offset >= \count($this->characters)) {
247 return '';
248 }
249
250 return $this->substr($offset, $length);
251 }
252
253 /**
254 * @param string|int<1, max> $value
255 *
256 * @throws UnexpectedEOFException
257 * @throws UnexpectedTokenException
258 */
259 public function consume($value = 1): string
260 {
261 if (\is_string($value)) {
262 $numberOfLines = \substr_count($value, "\n");
263 $length = $this->strlen($value);
264 if (!$this->streql($this->substr($this->currentPosition, $length), $value)) {
265 throw new UnexpectedTokenException(
266 $value,
267 $this->peek(\max($length, 5)),
268 'literal',
269 $this->lineNumber
270 );
271 }
272
273 $this->lineNumber += $numberOfLines;
274 $this->currentPosition += $this->strlen($value);
275 $result = $value;
276 } else {
277 if ($this->currentPosition + $value > \count($this->characters)) {
278 throw new UnexpectedEOFException((string) $value, $this->peek(5), 'count', $this->lineNumber);
279 }
280
281 $result = $this->substr($this->currentPosition, $value);
282 $numberOfLines = \substr_count($result, "\n");
283 $this->lineNumber += $numberOfLines;
284 $this->currentPosition += $value;
285 }
286
287 return $result;
288 }
289
290 /**
291 * If the possibly-expected next content is next, consume it.
292 *
293 * @param non-empty-string $nextContent
294 *
295 * @return bool whether the possibly-expected content was found and consumed
296 */
297 public function consumeIfComes(string $nextContent): bool
298 {
299 $length = $this->strlen($nextContent);
300 if (!$this->streql($this->substr($this->currentPosition, $length), $nextContent)) {
301 return false;
302 }
303
304 $numberOfLines = \substr_count($nextContent, "\n");
305 $this->lineNumber += $numberOfLines;
306 $this->currentPosition += $this->strlen($nextContent);
307
308 return true;
309 }
310
311 /**
312 * @param string $expression
313 * @param int<1, max>|null $maximumLength
314 *
315 * @throws UnexpectedEOFException
316 * @throws UnexpectedTokenException
317 */
318 public function consumeExpression(string $expression, ?int $maximumLength = null): string
319 {
320 $matches = null;
321 $input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
322 if (preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
323 throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
324 }
325
326 return $this->consume($matches[0][0]);
327 }
328
329 /**
330 * @return Comment|false
331 */
332 public function consumeComment()
333 {
334 $lineNumber = $this->lineNumber;
335 $comment = null;
336
337 if ($this->comes('/*')) {
338 $this->consume(1);
339 $comment = '';
340 while (($char = $this->consume(1)) !== '') {
341 $comment .= $char;
342 if ($this->comes('*/')) {
343 $this->consume(2);
344 break;
345 }
346 }
347 }
348
349 // We skip the * which was included in the comment.
350 return \is_string($comment) ? new Comment(\substr($comment, 1), $lineNumber) : false;
351 }
352
353 public function isEnd(): bool
354 {
355 return $this->currentPosition >= \count($this->characters);
356 }
357
358 /**
359 * @param list<string|self::EOF>|string|self::EOF $stopCharacters
360 * @param list<Comment> $comments
361 *
362 * @throws UnexpectedEOFException
363 * @throws UnexpectedTokenException
364 */
365 public function consumeUntil(
366 $stopCharacters,
367 bool $includeEnd = false,
368 bool $consumeEnd = false,
369 array &$comments = []
370 ): string {
371 $stopCharacters = \is_array($stopCharacters) ? $stopCharacters : [$stopCharacters];
372 $consumedCharacters = '';
373 $start = $this->currentPosition;
374
375 $comments = \array_merge($comments, $this->consumeComments());
376 while (!$this->isEnd()) {
377 $character = $this->consume(1);
378 if (\in_array($character, $stopCharacters, true)) {
379 if ($includeEnd) {
380 $consumedCharacters .= $character;
381 } elseif (!$consumeEnd) {
382 $this->currentPosition -= $this->strlen($character);
383 }
384 return $consumedCharacters;
385 }
386 $consumedCharacters .= $character;
387 $comments = \array_merge($comments, $this->consumeComments());
388 }
389
390 if (\in_array(self::EOF, $stopCharacters, true)) {
391 return $consumedCharacters;
392 }
393
394 $this->currentPosition = $start;
395 throw new UnexpectedEOFException(
396 'One of ("' . \implode('","', $stopCharacters) . '")',
397 $this->peek(5),
398 'search',
399 $this->lineNumber
400 );
401 }
402
403 private function inputLeft(): string
404 {
405 return $this->substr($this->currentPosition, -1);
406 }
407
408 public function streql(string $string1, string $string2, bool $caseInsensitive = true): bool
409 {
410 return $caseInsensitive
411 ? ($this->strtolower($string1) === $this->strtolower($string2))
412 : ($string1 === $string2);
413 }
414
415 /**
416 * @param int<1, max> $numberOfCharacters
417 */
418 public function backtrack(int $numberOfCharacters): void
419 {
420 $this->currentPosition -= $numberOfCharacters;
421 }
422
423 /**
424 * @return int<0, max>
425 */
426 public function strlen(string $string): int
427 {
428 return $this->parserSettings->hasMultibyteSupport()
429 ? \mb_strlen($string, $this->charset)
430 : \strlen($string);
431 }
432
433 /**
434 * @param int<0, max> $offset
435 */
436 private function substr(int $offset, int $length): string
437 {
438 if ($length < 0) {
439 $length = \count($this->characters) - $offset + $length;
440 }
441 if ($offset + $length > \count($this->characters)) {
442 $length = \count($this->characters) - $offset;
443 }
444 $result = '';
445 while ($length > 0) {
446 $result .= $this->characters[$offset];
447 $offset++;
448 $length--;
449 }
450
451 return $result;
452 }
453
454 /**
455 * @return ($string is non-empty-string ? non-empty-string : string)
456 */
457 private function strtolower(string $string): string
458 {
459 return $this->parserSettings->hasMultibyteSupport()
460 ? \mb_strtolower($string, $this->charset)
461 : \strtolower($string);
462 }
463
464 /**
465 * @return list<string>
466 */
467 private function strsplit(string $string): array
468 {
469 if ($this->parserSettings->hasMultibyteSupport()) {
470 if ($this->streql($this->charset, 'utf-8')) {
471 $result = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
472 } else {
473 $length = \mb_strlen($string, $this->charset);
474 $result = [];
475 for ($i = 0; $i < $length; ++$i) {
476 $result[] = \mb_substr($string, $i, 1, $this->charset);
477 }
478 }
479 } else {
480 $result = ($string !== '') ? \str_split($string) : [];
481 }
482
483 return $result;
484 }
485
486 /**
487 * @return list<Comment>
488 */
489 private function consumeComments(): array
490 {
491 $comments = [];
492
493 while (true) {
494 $comment = $this->consumeComment();
495 if ($comment instanceof Comment) {
496 $comments[] = $comment;
497 } else {
498 return $comments;
499 }
500 }
501 }
502 }