-
Notifications
You must be signed in to change notification settings - Fork 75
/
Parser.php
958 lines (778 loc) · 25.6 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
<?php
/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/
namespace ScssPhp\ScssPhp\Parser;
use ScssPhp\ScssPhp\Exception\SassFormatException;
use ScssPhp\ScssPhp\Logger\AdaptingLogger;
use ScssPhp\ScssPhp\Logger\LocationAwareLoggerInterface;
use ScssPhp\ScssPhp\Logger\LoggerInterface;
use ScssPhp\ScssPhp\Logger\QuietLogger;
use ScssPhp\ScssPhp\SourceSpan\FileSpan;
use ScssPhp\ScssPhp\Util;
use ScssPhp\ScssPhp\Util\Character;
use ScssPhp\ScssPhp\Util\ParserUtil;
/**
* @internal
*/
class Parser
{
/**
* @var StringScanner
* @readonly
*/
protected $scanner;
/**
* @var LocationAwareLoggerInterface
* @readonly
*/
protected $logger;
/**
* @var string|null
* @readonly
*/
protected $sourceUrl;
/**
* Parses $text as a CSS identifier and returns the result.
*
* @throws SassFormatException if parsing fails.
*/
public static function parseIdentifier(string $text, ?LoggerInterface $logger = null): string
{
return (new Parser($text, $logger))->doParseIdentifier();
}
/**
* Returns whether $text is a valid CSS identifier.
*/
public static function isIdentifier(string $text, ?LoggerInterface $logger = null): bool
{
try {
self::parseIdentifier($text, $logger);
return true;
} catch (SassFormatException $e) {
return false;
}
}
public function __construct(string $contents, ?LoggerInterface $logger = null, ?string $sourceUrl = null)
{
$this->scanner = new StringScanner($contents);
$this->logger = AdaptingLogger::adaptLogger($logger ?? new QuietLogger());
$this->sourceUrl = $sourceUrl;
}
/**
* @throws SassFormatException
*/
private function doParseIdentifier(): string
{
try {
$result = $this->identifier();
$this->scanner->expectDone();
return $result;
} catch (FormatException $e) {
throw $this->wrapException($e);
}
}
/**
* Consumes whitespace, including any comments.
*/
protected function whitespace(): void
{
do {
$this->whitespaceWithoutComments();
} while ($this->scanComment());
}
/**
* Consumes whitespace, but not comments.
*/
protected function whitespaceWithoutComments(): void
{
while (!$this->scanner->isDone() && Character::isWhitespace($this->scanner->peekChar())) {
$this->scanner->readChar();
}
}
/**
* Consumes spaces and tabs.
*/
protected function spaces(): void
{
while (!$this->scanner->isDone() && Character::isSpaceOrTab($this->scanner->peekChar())) {
$this->scanner->readChar();
}
}
/**
* Consumes and ignores a comment if possible.
*
* Returns whether the comment was consumed.
*/
protected function scanComment(): bool
{
if ($this->scanner->peekChar() !== '/') {
return false;
}
$next = $this->scanner->peekChar(1);
if ($next === '/') {
$this->silentComment();
return true;
}
if ($next === '*') {
$this->loudComment();
return true;
}
return false;
}
/**
* Like {@see whitespace}, but throws an error if no whitespace is consumed.
*/
protected function expectWhitespace(): void
{
if ($this->scanner->isDone() || !(Character::isWhitespace($this->scanner->peekChar()) || $this->scanComment())) {
$this->scanner->error('Expected whitespace.');
}
$this->whitespace();
}
/**
* Consumes and ignores a silent (Sass-style) comment.
*/
protected function silentComment(): void
{
$this->scanner->expect('//');
while (!$this->scanner->isDone() && !Character::isNewline($this->scanner->peekChar())) {
$this->scanner->readChar();
}
}
/**
* Consumes and ignores a loud (CSS-style) comment.
*/
protected function loudComment(): void
{
$this->scanner->expect('/*');
while (true) {
$next = $this->scanner->readChar();
if ($next !== '*') {
continue;
}
do {
$next = $this->scanner->readChar();
} while ($next === '*');
if ($next === '/') {
break;
}
}
}
/**
* Consumes a plain CSS identifier.
*
* If $normalize is `true`, this converts underscores into hyphens.
*
* If $unit is `true`, this doesn't parse a `-` followed by a digit. This
* ensures that `1px-2px` parses as subtraction rather than the unit
* `px-2px`.
*/
protected function identifier(bool $normalize = false, bool $unit = false): string
{
$text = '';
if ($this->scanner->scanChar('-')) {
$text .= '-';
if ($this->scanner->scanChar('-')) {
$text .= '-';
$text .= $this->consumeIdentifierBody($normalize, $unit);
return $text;
}
}
$first = $this->scanner->peekChar();
if ($first === null) {
$this->scanner->error('Expected identifier.');
}
if ($normalize && $first === '_') {
$this->scanner->readChar();
$text .= '-';
} elseif (Character::isNameStart($first)) {
$text .= $this->scanner->readUtf8Char();
} elseif ($first === '\\') {
$text .= $this->escape(true);
} else {
$this->scanner->error('Expected identifier.');
}
$text .= $this->consumeIdentifierBody($normalize, $unit);
return $text;
}
/**
* Consumes a chunk of a plain CSS identifier after the name start.
*/
public function identifierBody(): string
{
$text = $this->consumeIdentifierBody();
if ($text === '') {
$this->scanner->error('Expected identifier body.');
}
return $text;
}
private function consumeIdentifierBody(bool $normalize = false, bool $unit = false): string
{
$text = '';
while (true) {
$next = $this->scanner->peekChar();
if ($next === null) {
break;
}
if ($unit && $next === '-') {
$second = $this->scanner->peekChar(1);
if ($second !== null && ($second === '.' || Character::isDigit($second))) {
break;
}
$text .= $this->scanner->readChar();
} elseif ($normalize && $next === '_') {
$this->scanner->readChar();
$text .= '-';
} elseif (Character::isName($next)) {
$text .= $this->scanner->readUtf8Char();
} elseif ($next === '\\') {
$text .= $this->escape();
} else {
break;
}
}
return $text;
}
/**
* Consumes a plain CSS string.
*
* This returns the parsed contents of the string—that is, it doesn't include
* quotes and its escapes are resolved.
*/
protected function string(): string
{
$quote = $this->scanner->readChar();
if ($quote !== '"' && $quote !== "'") {
$this->scanner->error('Expected string.');
}
$buffer = '';
while (true) {
$next = $this->scanner->peekChar();
if ($next === $quote) {
$this->scanner->readChar();
break;
}
if ($next === null || Character::isNewline($next)) {
$this->scanner->error("Expected $quote.");
}
if ($next === '\\') {
$second = $this->scanner->peekChar(1);
if ($second !== null && Character::isNewline($second)) {
$this->scanner->readChar();
$this->scanner->readChar();
} else {
$buffer .= $this->escapeCharacter();
}
} else {
$buffer .= $this->scanner->readUtf8Char();
}
}
return $buffer;
}
/**
* Consumes and returns a natural number (that is, a non-negative integer) as a double.
*
* Doesn't support scientific notation.
*/
protected function naturalNumber(): float
{
$first = $this->scanner->readChar();
if (!Character::isDigit($first)) {
$this->scanner->error('Expected digit.', $this->scanner->getPosition() - 1);
}
$number = (float) intval($first);
while (Character::isDigit($this->scanner->peekChar())) {
$number *= 10;
$number += intval($this->scanner->readChar());
}
return $number;
}
/**
* Consumes tokens until it reaches a top-level `";"`, `")"`, `"]"`,
* or `"}"` and returns their contents as a string.
*
* If $allowEmpty is `false` (the default), this requires at least one token.
*/
protected function declarationValue(bool $allowEmpty = false): string
{
$buffer = '';
$brackets = [];
$wroteNewline = false;
while (true) {
$next = $this->scanner->peekChar();
if ($next === null) {
break;
}
switch ($next) {
case '\\':
$buffer .= $this->escape(true);
$wroteNewline = false;
break;
case '"':
case "'":
$buffer .= $this->rawText([$this, 'string']);
$wroteNewline = false;
break;
case '/':
if ($this->scanner->peekChar(1) === '*') {
$buffer .= $this->rawText([$this, 'loudComment']);
} else {
$buffer .= $this->scanner->readChar();
}
$wroteNewline = false;
break;
case ' ':
case "\t":
$second = $this->scanner->peekChar(1);
if ($wroteNewline || $second === null || !Character::isWhitespace($second)) {
$buffer .= ' ';
}
$this->scanner->readChar();
break;
case "\n":
case "\r":
case "\f":
$prev = $this->scanner->peekChar(-1);
if ($prev === null || !Character::isNewline($prev)) {
$buffer .= "\n";
}
$this->scanner->readChar();
$wroteNewline = true;
break;
case '(':
case '{':
case '[':
$buffer .= $next;
$brackets[] = Character::opposite($this->scanner->readChar());
$wroteNewline = false;
break;
case ')':
case '}':
case ']':
if (empty($brackets)) {
break 2;
}
$buffer .= $next;
$this->scanner->expectChar(array_pop($brackets));
$wroteNewline = false;
break;
case ';':
if (empty($brackets)) {
break 2;
}
$buffer .= $this->scanner->readChar();
break;
case 'u':
case 'U':
$url = $this->tryUrl();
if ($url !== null) {
$buffer .= $url;
} else {
$buffer .= $this->scanner->readChar();
}
$wroteNewline = false;
break;
default:
if ($this->lookingAtIdentifier()) {
$buffer .= $this->identifier();
} else {
$buffer .= $this->scanner->readUtf8Char();
}
$wroteNewline = false;
break;
}
}
if (!empty($brackets)) {
$this->scanner->expectChar(array_pop($brackets));
}
if (!$allowEmpty && $buffer === '') {
$this->scanner->error('Expected token.');
}
return $buffer;
}
/**
* Consumes a `url()` token if possible, and returns `null` otherwise.
*/
protected function tryUrl(): ?string
{
$start = $this->scanner->getPosition();
if (!$this->scanIdentifier('url')) {
return null;
}
if (!$this->scanner->scanChar('(')) {
$this->scanner->setPosition($start);
return null;
}
$this->whitespace();
$buffer = 'url(';
while (true) {
$next = $this->scanner->peekChar();
if ($next === null) {
break;
}
$nextCharCode = \ord($next);
if ($next === '\\') {
$buffer .= $this->escape();
} elseif ($next === '%' || $next === '&' || $next === '#' || ($nextCharCode >= \ord('*') && $nextCharCode <= \ord('~')) || $nextCharCode >= 0x80) {
$buffer .= $this->scanner->readUtf8Char();
} elseif (Character::isWhitespace($next)) {
$this->whitespace();
if ($this->scanner->peekChar() !== ')') {
break;
}
} elseif ($next === ')') {
$buffer .= $this->scanner->readChar();
return $buffer;
} else {
break;
}
}
$this->scanner->setPosition($start);
return null;
}
/**
* Consumes a Sass variable name, and returns its name without the dollar sign.
*/
protected function variableName(): string
{
$this->scanner->expectChar('$');
return $this->identifier(true);
}
/**
* Consumes an escape sequence and returns the text that defines it.
*
* If $identifierStart is true, this normalizes the escape sequence as
* though it were at the beginning of an identifier.
*/
protected function escape(bool $identifierStart = false): string
{
$start = $this->scanner->getPosition();
$this->scanner->expectChar('\\');
$first = $this->scanner->peekChar();
if ($first === null) {
$this->scanner->error('Expected escape sequence.');
}
if (Character::isNewline($first)) {
$this->scanner->error('Expected escape sequence.');
}
if (Character::isHex($first)) {
$value = 0;
for ($i = 0; $i < 6; $i++) {
$next = $this->scanner->peekChar();
if ($next === null || !Character::isHex($next)) {
break;
}
$value *= 16;
$value += hexdec($this->scanner->readChar());
assert(\is_int($value));
}
$this->scanCharIf([Character::class, 'isWhitespace']);
$valueText = Util::mbChr($value);
} else {
$valueText = $this->scanner->readUtf8Char();
$value = Util::mbOrd($valueText);
}
if ($identifierStart ? Character::isNameStart($valueText) : Character::isName($valueText)) {
if ($value > 0x10ffff) {
$this->scanner->error('Invalid Unicode code point.', $start);
}
return $valueText;
}
if ($value < 0x1f || $valueText === "\x7f" || ($identifierStart && Character::isDigit($valueText))) {
return '\\' . bin2hex($valueText) . ' ';
}
return '\\' . $valueText;
}
/**
* Consumes an escape sequence and returns the character it represents.
*/
protected function escapeCharacter(): string
{
return ParserUtil::consumeEscapedCharacter($this->scanner);
}
/**
* @param callable(string): bool $condition
*
* @phpstan-impure
*/
protected function scanCharIf(callable $condition): bool
{
$next = $this->scanner->peekChar();
if ($next === null || !$condition($next)) {
return false;
}
$this->scanner->readChar();
return true;
}
/**
* Consumes the next character or escape sequence if it matches $character.
*
* Matching will be case-insensitive unless $caseSensitive is true.
* When matching case-insensitively, $character must be passed in lowercase.
*
* This only supports ASCII identifier characters.
*/
protected function scanIdentChar(string $character, bool $caseSensitive = false): bool
{
$matches = function (string $actual) use ($character, $caseSensitive): bool {
if ($caseSensitive) {
return $actual === $character;
}
return \strtolower($actual) === $character;
};
$next = $this->scanner->peekChar();
if ($next !== null && $matches($next)) {
$this->scanner->readChar();
return true;
}
if ($next === '\\') {
$start = $this->scanner->getPosition();
if ($matches($this->escapeCharacter())) {
return true;
}
$this->scanner->setPosition($start);
}
return false;
}
/**
* Consumes the next character or escape sequence and asserts it matches $char.
*
* Matching will be case-insensitive unless $caseSensitive is true.
* When matching case-insensitively, $char must be passed in lowercase.
*
* This only supports ASCII identifier characters.
*/
protected function expectIdentChar(string $char, bool $caseSensitive = false): void
{
if ($this->scanIdentChar($char, $caseSensitive)) {
return;
}
$this->scanner->error("Expected \"$char\"");
}
/**
* Returns whether the scanner is immediately before a number.
*
* This follows [the CSS algorithm][].
*
* [the CSS algorithm]: https://drafts.csswg.org/css-syntax-3/#starts-with-a-number
*/
protected function lookingAtNumber(): bool
{
$first = $this->scanner->peekChar();
if ($first === null) {
return false;
}
if (Character::isDigit($first)) {
return true;
}
if ($first === '.') {
$second = $this->scanner->peekChar(1);
return $second !== null && Character::isDigit($second);
}
if ($first === '+' || $first === '-') {
$second = $this->scanner->peekChar(1);
if ($second === null) {
return false;
}
if (Character::isDigit($second)) {
return true;
}
if ($second !== '.') {
return false;
}
$third = $this->scanner->peekChar(2);
return $third !== null && Character::isDigit($third);
}
return false;
}
/**
* Returns whether the scanner is immediately before a plain CSS identifier.
*
* If $forward is passed, this looks that many characters forward instead.
*
* This is based on [the CSS algorithm][], but it assumes all backslashes
* start escapes.
*
* [the CSS algorithm]: https://drafts.csswg.org/css-syntax-3/#would-start-an-identifier
*/
protected function lookingAtIdentifier(int $forward = 0): bool
{
$first = $this->scanner->peekChar($forward);
if ($first === null) {
return false;
}
if ($first === '\\' || Character::isNameStart($first)) {
return true;
}
if ($first !== '-') {
return false;
}
$second = $this->scanner->peekChar($forward + 1);
if ($second === null) {
return false;
}
return $second === '\\' || $second === '-' || Character::isNameStart($second);
}
/**
* Returns whether the scanner is immediately before a sequence of characters
* that could be part of a plain CSS identifier body.
*/
protected function lookingAtIdentifierBody(): bool
{
$next = $this->scanner->peekChar();
return $next !== null && ($next === '\\' || Character::isName($next));
}
/**
* Consumes an identifier if its name exactly matches $text.
*
* When matching case-insensitively, $text must be passed in lowercase.
*
* This only supports ASCII identifiers.
*/
protected function scanIdentifier(string $text, bool $caseSensitive = false): bool
{
if (!$this->lookingAtIdentifier()) {
return false;
}
$start = $this->scanner->getPosition();
if ($this->consumeIdentifier($text, $caseSensitive) && !$this->lookingAtIdentifierBody()) {
return true;
}
$this->scanner->setPosition($start);
return false;
}
/**
* Returns whether an identifier whose name exactly matches $text is at the
* current scanner position.
*
* This doesn't move the scan pointer forward
*/
protected function matchesIdentifier(string $text, bool $caseSensitive = false): bool
{
if (!$this->lookingAtIdentifier()) {
return false;
}
$start = $this->scanner->getPosition();
$result = $this->consumeIdentifier($text, $caseSensitive) && !$this->lookingAtIdentifierBody();
$this->scanner->setPosition($start);
return $result;
}
/**
* Consumes $text as an identifer, but doesn't verify whether there's
* additional identifier text afterwards.
*
* Returns `true` if the full $text is consumed and `false` otherwise, but
* doesn't reset the scan pointer.
*/
private function consumeIdentifier(string $text, bool $caseSensitive): bool
{
for ($i = 0; $i < \strlen($text); $i++) {
if (!$this->scanIdentChar($text[$i], $caseSensitive)) {
return false;
}
}
return true;
}
/**
* Consumes an identifier asserts that its name exactly matches $text.
*
* When matching case-insensitively, $text must be passed in lowercase.
*
* This only supports ASCII identifiers.
*/
protected function expectIdentifier(string $text, ?string $name = null, bool $caseSensitive = false): void
{
$name = $name ?? "\"$text\"";
$start = $this->scanner->getPosition();
for ($i = 0; $i < \strlen($text); $i++) {
if ($this->scanIdentChar($text[$i], $caseSensitive)) {
continue;
}
$this->scanner->error("Expected $name.", $start);
}
if (!$this->lookingAtIdentifierBody()) {
return;
}
$this->scanner->error("Expected $name.", $start);
}
/**
* Runs $consumer and returns the source text that it consumes.
*
* @param callable(): void $consumer
*/
protected function rawText(callable $consumer): string
{
$start = $this->scanner->getPosition();
$consumer();
return $this->scanner->substring($start);
}
/**
* Prints a warning to standard error, associated with $span.
*/
protected function warn(string $message, FileSpan $span): void
{
$this->logger->warn($message, false, $span);
}
/**
* Throws an error associated with $position.
*
* @throws FormatException
*
* @return never-returns
*/
protected function error(string $message, FileSpan $span, ?\Throwable $previous = null): void
{
throw new FormatException($message, $span, $previous);
}
protected function wrapException(FormatException $error): SassFormatException
{
$span = $error->getSpan();
if ($span->getLength() === 0 && 0 === stripos($error->getMessage(), 'expected')) {
$startPosition = $this->firstNewlineBefore($span->getStart()->getOffset());
if ($startPosition !== $span->getStart()->getOffset()) {
$span = $span->getFile()->span($startPosition, $startPosition);
}
}
return new SassFormatException($error->getMessage(), $span, $error);
}
/**
* If [position] is separated from the previous non-whitespace character in
* `$scanner->getString()` by one or more newlines, returns the offset of the last
* separating newline.
*
* Otherwise returns $position.
*
* This helps avoid missing token errors pointing at the next closing bracket
* rather than the line where the problem actually occurred.
*
* @param int $position
*
* @return int
*/
private function firstNewlineBefore(int $position): int
{
$index = $position - 1;
$lastNewline = null;
$string = $this->scanner->getString();
while ($index >= 0) {
$char = $string[$index];
if (!Character::isWhitespace($char)) {
return $lastNewline ?? $position;
}
if (Character::isNewline($char)) {
$lastNewline = $index;
}
$index--;
}
// If the document *only* contains whitespace before $position, always
// return $position.
return $position;
}
}