/
parser.dart
717 lines (630 loc) · 20.8 KB
/
parser.dart
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
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:charcode/charcode.dart';
import 'package:meta/meta.dart';
import 'package:source_span/source_span.dart';
import 'package:string_scanner/string_scanner.dart';
import '../exception.dart';
import '../logger.dart';
import '../util/character.dart';
import '../utils.dart';
/// The abstract base class for all parsers.
///
/// This provides utility methods and common token parsing. Unless specified
/// otherwise, a parse method throws a [SassFormatException] if it fails to
/// parse.
class Parser {
/// The scanner that scans through the text being parsed.
final SpanScanner scanner;
/// The logger to use when emitting warnings.
@protected
final Logger logger;
/// Parses [text] as a CSS identifier and returns the result.
///
/// Throws a [SassFormatException] if parsing fails.
static String parseIdentifier(String text, {Logger logger}) =>
Parser(text, logger: logger)._parseIdentifier();
/// Returns whether [text] is a valid CSS identifier.
static bool isIdentifier(String text, {Logger logger}) {
try {
parseIdentifier(text, logger: logger);
return true;
} on SassFormatException {
return false;
}
}
/// Returns whether [text] starts like a variable declaration.
///
/// Ignores everything after the `:`.
static bool isVariableDeclarationLike(String text, {Logger logger}) =>
Parser(text, logger: logger)._isVariableDeclarationLike();
@protected
Parser(String contents, {url, Logger logger})
: scanner = SpanScanner(contents, sourceUrl: url),
logger = logger ?? const Logger.stderr();
String _parseIdentifier() {
return wrapSpanFormatException(() {
var result = identifier();
scanner.expectDone();
return result;
});
}
bool _isVariableDeclarationLike() {
if (!scanner.scanChar($dollar)) return false;
if (!lookingAtIdentifier()) return false;
identifier();
whitespace();
return scanner.scanChar($colon);
}
// ## Tokens
/// Consumes whitespace, including any comments.
@protected
void whitespace() {
do {
whitespaceWithoutComments();
} while (scanComment());
}
/// Consumes whitespace, but not comments.
@protected
void whitespaceWithoutComments() {
while (!scanner.isDone && isWhitespace(scanner.peekChar())) {
scanner.readChar();
}
}
/// Consumes spaces and tabs.
@protected
void spaces() {
while (!scanner.isDone && isSpaceOrTab(scanner.peekChar())) {
scanner.readChar();
}
}
/// Consumes and ignores a comment if possible.
///
/// Returns whether the comment was consumed.
@protected
bool scanComment() {
if (scanner.peekChar() != $slash) return false;
var next = scanner.peekChar(1);
if (next == $slash) {
silentComment();
return true;
} else if (next == $asterisk) {
loudComment();
return true;
} else {
return false;
}
}
/// Consumes and ignores a silent (Sass-style) comment.
@protected
void silentComment() {
scanner.expect("//");
while (!scanner.isDone && !isNewline(scanner.peekChar())) {
scanner.readChar();
}
}
/// Consumes and ignores a loud (CSS-style) comment.
@protected
void loudComment() {
scanner.expect("/*");
while (true) {
var next = scanner.readChar();
if (next != $asterisk) continue;
do {
next = scanner.readChar();
} while (next == $asterisk);
if (next == $slash) 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
String identifier({bool normalize = false, bool unit = false}) {
// NOTE: this logic is largely duplicated in
// StylesheetParser.interpolatedIdentifier. Most changes here should be
// mirrored there.
var text = StringBuffer();
if (scanner.scanChar($dash)) {
text.writeCharCode($dash);
if (scanner.scanChar($dash)) {
text.writeCharCode($dash);
_identifierBody(text, normalize: normalize, unit: unit);
return text.toString();
}
}
var first = scanner.peekChar();
if (first == null) {
scanner.error("Expected identifier.");
} else if (normalize && first == $underscore) {
scanner.readChar();
text.writeCharCode($dash);
} else if (isNameStart(first)) {
text.writeCharCode(scanner.readChar());
} else if (first == $backslash) {
text.write(escape(identifierStart: true));
} else {
scanner.error("Expected identifier.");
}
_identifierBody(text, normalize: normalize, unit: unit);
return text.toString();
}
/// Consumes a chunk of a plain CSS identifier after the name start.
@protected
String identifierBody() {
var text = StringBuffer();
_identifierBody(text);
if (text.isEmpty) scanner.error("Expected identifier body.");
return text.toString();
}
/// Like [_identifierBody], but parses the body into the [text] buffer.
void _identifierBody(StringBuffer text,
{bool normalize = false, bool unit = false}) {
while (true) {
var next = scanner.peekChar();
if (next == null) {
break;
} else if (unit && next == $dash) {
// Disallow `-` followed by a dot or a digit digit in units.
var second = scanner.peekChar(1);
if (second != null && (second == $dot || isDigit(second))) break;
text.writeCharCode(scanner.readChar());
} else if (normalize && next == $underscore) {
scanner.readChar();
text.writeCharCode($dash);
} else if (isName(next)) {
text.writeCharCode(scanner.readChar());
} else if (next == $backslash) {
text.write(escape());
} else {
break;
}
}
}
/// 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
String string() {
// NOTE: this logic is largely duplicated in ScssParser._interpolatedString.
// Most changes here should be mirrored there.
var quote = scanner.readChar();
if (quote != $single_quote && quote != $double_quote) {
scanner.error("Expected string.",
position: quote == null ? scanner.position : scanner.position - 1);
}
var buffer = StringBuffer();
while (true) {
var next = scanner.peekChar();
if (next == quote) {
scanner.readChar();
break;
} else if (next == null || isNewline(next)) {
scanner.error("Expected ${String.fromCharCode(quote)}.");
} else if (next == $backslash) {
if (isNewline(scanner.peekChar(1))) {
scanner.readChar();
scanner.readChar();
} else {
buffer.writeCharCode(escapeCharacter());
}
} else {
buffer.writeCharCode(scanner.readChar());
}
}
return buffer.toString();
}
/// Consumes and returns a natural number (that is, a non-negative integer).
///
/// Doesn't support scientific notation.
@protected
int naturalNumber() {
var first = scanner.readChar();
if (!isDigit(first)) {
scanner.error("Expected digit.", position: scanner.position - 1);
}
var number = asDecimal(first);
while (isDigit(scanner.peekChar())) {
number *= 10;
number += asDecimal(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
String declarationValue({bool allowEmpty = false}) {
// NOTE: this logic is largely duplicated in
// StylesheetParser._interpolatedDeclarationValue. Most changes here should
// be mirrored there.
var buffer = StringBuffer();
var brackets = <int>[];
var wroteNewline = false;
loop:
while (true) {
var next = scanner.peekChar();
switch (next) {
case $backslash:
buffer.write(escape(identifierStart: true));
wroteNewline = false;
break;
case $double_quote:
case $single_quote:
buffer.write(rawText(string));
wroteNewline = false;
break;
case $slash:
if (scanner.peekChar(1) == $asterisk) {
buffer.write(rawText(loudComment));
} else {
buffer.writeCharCode(scanner.readChar());
}
wroteNewline = false;
break;
case $space:
case $tab:
if (wroteNewline || !isWhitespace(scanner.peekChar(1))) {
buffer.writeCharCode($space);
}
scanner.readChar();
break;
case $lf:
case $cr:
case $ff:
if (!isNewline(scanner.peekChar(-1))) buffer.writeln();
scanner.readChar();
wroteNewline = true;
break;
case $lparen:
case $lbrace:
case $lbracket:
buffer.writeCharCode(next);
brackets.add(opposite(scanner.readChar()));
wroteNewline = false;
break;
case $rparen:
case $rbrace:
case $rbracket:
if (brackets.isEmpty) break loop;
buffer.writeCharCode(next);
scanner.expectChar(brackets.removeLast());
wroteNewline = false;
break;
case $semicolon:
if (brackets.isEmpty) break loop;
buffer.writeCharCode(scanner.readChar());
break;
case $u:
case $U:
var url = tryUrl();
if (url != null) {
buffer.write(url);
} else {
buffer.writeCharCode(scanner.readChar());
}
wroteNewline = false;
break;
default:
if (next == null) break loop;
if (lookingAtIdentifier()) {
buffer.write(identifier());
} else {
buffer.writeCharCode(scanner.readChar());
}
wroteNewline = false;
break;
}
}
if (brackets.isNotEmpty) scanner.expectChar(brackets.last);
if (!allowEmpty && buffer.isEmpty) scanner.error("Expected token.");
return buffer.toString();
}
/// Consumes a `url()` token if possible, and returns `null` otherwise.
@protected
String tryUrl() {
// NOTE: this logic is largely duplicated in ScssParser._tryUrlContents.
// Most changes here should be mirrored there.
var start = scanner.state;
if (!scanIdentifier("url")) return null;
if (!scanner.scanChar($lparen)) {
scanner.state = start;
return null;
}
whitespace();
// Match Ruby Sass's behavior: parse a raw URL() if possible, and if not
// backtrack and re-parse as a function expression.
var buffer = StringBuffer()..write("url(");
while (true) {
var next = scanner.peekChar();
if (next == null) {
break;
} else if (next == $percent ||
next == $ampersand ||
next == $hash ||
(next >= $asterisk && next <= $tilde) ||
next >= 0x0080) {
buffer.writeCharCode(scanner.readChar());
} else if (next == $backslash) {
buffer.write(escape());
} else if (isWhitespace(next)) {
whitespace();
if (scanner.peekChar() != $rparen) break;
} else if (next == $rparen) {
buffer.writeCharCode(scanner.readChar());
return buffer.toString();
} else {
break;
}
}
scanner.state = start;
return null;
}
/// Consumes a Sass variable name, and returns its name without the dollar
/// sign.
@protected
String variableName() {
scanner.expectChar($dollar);
return identifier(normalize: true);
}
// ## Characters
/// 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
String escape({bool identifierStart = false}) {
// See https://drafts.csswg.org/css-syntax-3/#consume-escaped-code-point.
scanner.expectChar($backslash);
var value = 0;
var first = scanner.peekChar();
if (first == null) {
return "";
} else if (isNewline(first)) {
scanner.error("Expected escape sequence.");
return null;
} else if (isHex(first)) {
for (var i = 0; i < 6; i++) {
var next = scanner.peekChar();
if (next == null || !isHex(next)) break;
value *= 16;
value += asHex(scanner.readChar());
}
scanCharIf(isWhitespace);
} else {
value = scanner.readChar();
}
if (identifierStart ? isNameStart(value) : isName(value)) {
return String.fromCharCode(value);
} else if (value <= 0x1F ||
value == 0x7F ||
(identifierStart && isDigit(value))) {
var buffer = StringBuffer()..writeCharCode($backslash);
if (value > 0xF) buffer.writeCharCode(hexCharFor(value >> 4));
buffer.writeCharCode(hexCharFor(value & 0xF));
buffer.writeCharCode($space);
return buffer.toString();
} else {
return String.fromCharCodes([$backslash, value]);
}
}
/// Consumes an escape sequence and returns the character it represents.
@protected
int escapeCharacter() {
// See https://drafts.csswg.org/css-syntax-3/#consume-escaped-code-point.
scanner.expectChar($backslash);
var first = scanner.peekChar();
if (first == null) {
return 0xFFFD;
} else if (isNewline(first)) {
scanner.error("Expected escape sequence.");
return 0;
} else if (isHex(first)) {
var value = 0;
for (var i = 0; i < 6; i++) {
var next = scanner.peekChar();
if (next == null || !isHex(next)) break;
value = (value << 4) + asHex(scanner.readChar());
}
if (isWhitespace(scanner.peekChar())) scanner.readChar();
if (value == 0 ||
(value >= 0xD800 && value <= 0xDFFF) ||
value >= 0x10FFFF) {
return 0xFFFD;
} else {
return value;
}
} else {
return scanner.readChar();
}
}
// Consumes the next character if it matches [condition].
//
// Returns whether or not the character was consumed.
@protected
bool scanCharIf(bool condition(int character)) {
var next = scanner.peekChar();
if (!condition(next)) return false;
scanner.readChar();
return true;
}
/// Consumes the next character if it's equal to [letter], ignoring ASCII
/// case.
@protected
bool scanCharIgnoreCase(int letter) {
if (!equalsLetterIgnoreCase(letter, scanner.peekChar())) return false;
scanner.readChar();
return true;
}
/// Consumes the next character and asserts that it's equal to [letter],
/// ignoring ASCII case.
@protected
void expectCharIgnoreCase(int letter) {
var actual = scanner.readChar();
if (equalsLetterIgnoreCase(letter, actual)) return;
scanner.error('Expected "${String.fromCharCode(letter)}".',
position: actual == null ? scanner.position : scanner.position - 1);
}
// ## Utilities
/// 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
bool lookingAtNumber() {
var first = scanner.peekChar();
if (first == null) return false;
if (isDigit(first)) return true;
if (first == $dot) {
var second = scanner.peekChar(1);
return second != null && isDigit(second);
} else if (first == $plus || first == $minus) {
var second = scanner.peekChar(1);
if (second == null) return false;
if (isDigit(second)) return true;
if (second != $dot) return false;
var third = scanner.peekChar(2);
return third != null && isDigit(third);
} else {
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
bool lookingAtIdentifier([int forward]) {
// See also [ScssParser._lookingAtInterpolatedIdentifier].
forward ??= 0;
var first = scanner.peekChar(forward);
if (first == null) return false;
if (isNameStart(first) || first == $backslash) return true;
if (first != $dash) return false;
var second = scanner.peekChar(forward + 1);
if (second == null) return false;
return isNameStart(second) || second == $backslash || second == $dash;
}
/// Returns whether the scanner is immediately before a sequence of characters
/// that could be part of a plain CSS identifier body.
@protected
bool lookingAtIdentifierBody() {
var next = scanner.peekChar();
return next != null && (isName(next) || next == $backslash);
}
/// Consumes an identifier if its name exactly matches [text].
@protected
bool scanIdentifier(String text) {
if (!lookingAtIdentifier()) return false;
var start = scanner.state;
for (var i = 0; i < text.length; i++) {
var next = text.codeUnitAt(i);
if (scanCharIgnoreCase(next)) continue;
scanner.state = start;
return false;
}
if (!lookingAtIdentifierBody()) return true;
scanner.state = start;
return false;
}
/// Consumes an identifier and asserts that its name exactly matches [text].
@protected
void expectIdentifier(String text, {String name}) {
name ??= '"$text"';
var start = scanner.position;
for (var i = 0; i < text.length; i++) {
var next = text.codeUnitAt(i);
if (scanCharIgnoreCase(next)) continue;
scanner.error("Expected $name.", position: start);
}
if (!lookingAtIdentifierBody()) return;
scanner.error("Expected $name", position: start);
}
/// Runs [consumer] and returns the source text that it consumes.
@protected
String rawText(void consumer()) {
var start = scanner.position;
consumer();
return scanner.substring(start);
}
/// Prints a warning to standard error, associated with [span].
@protected
void warn(String message, FileSpan span) => logger.warn(message, span: span);
/// Throws an error associated with [span].
@protected
@alwaysThrows
void error(String message, FileSpan span) =>
throw StringScannerException(message, span, scanner.string);
/// Runs callback and, if it throws a [SourceSpanFormatException], rethrows it
/// with [message] as its message.
@protected
T withErrorMessage<T>(String message, T callback()) {
try {
return callback();
} on SourceSpanFormatException catch (error) {
throw SourceSpanFormatException(message, error.span, error.source);
}
}
/// Prints a source span highlight of the current location being scanned.
///
/// If [message] is passed, prints that as well. This is intended for use when
/// debugging parser failures.
@protected
void debug([message]) {
if (message == null) {
print(scanner.emptySpan.highlight(color: true));
} else {
print(scanner.emptySpan.message(message.toString(), color: true));
}
}
/// Runs [callback] and wraps any [SourceSpanFormatException] it throws in a
/// [SassFormatException].
@protected
T wrapSpanFormatException<T>(T callback()) {
try {
return callback();
} on SourceSpanFormatException catch (error) {
var span = error.span as FileSpan;
if (startsWithIgnoreCase(error.message, "expected") && span.length == 0) {
var startPosition = _firstNewlineBefore(span.start.offset);
if (startPosition != span.start.offset) {
span = span.file.span(startPosition, startPosition);
}
}
throw SassFormatException(error.message, span);
}
}
/// If [position] is separated from the previous non-whitespace character in
/// `scanner.string` 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.
int _firstNewlineBefore(int position) {
var index = position - 1;
int lastNewline;
while (index >= 0) {
var codeUnit = scanner.string.codeUnitAt(index);
if (!isWhitespace(codeUnit)) return lastNewline ?? position;
if (isNewline(codeUnit)) lastNewline = index;
index--;
}
// If the document *only* contains whitespace before [position], always
// return [position].
return position;
}
}