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
use core::fmt;
use crate::KindSet;
/// Kind represents the token "Type", categorised mostly by the token types within the CSS Syntax spec.
///
/// Importantly, `Kind` is represented as `u8` and must only use the 5 low bits, because the upper 3 bits get used to
/// house details about each kind, that a token would be interested in learning about.
///
/// Maintaining parity with the spec makes it easier to reason about logic around the parser, despite it being possible to
/// group a bunch of these tokens into a single "delimiter" token. These Delim kinds, however, set the upper bit which
/// means they cannot be inserted directly into a token. Instead a token.
#[repr(u8)]
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Kind {
// Trivias (mask as 0b0_00XX)
/// Represents the [<eof-token>][1] defined in CSS. While CSS stipulates that this token is never produced by a
/// tokenizer, this [Lexer][crate::Lexer] _will_ produce [<eof-token>s][1] if the underlying source has been
/// fully consumed.
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-eof-token
Eof = 0b0000,
/// Represents the [<whitespace-token>][1] defined in CSS.
///
/// ```md
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ───────╭──┤│
/// ├─ "\t" ──────┤
/// ╰─ <newline> ─╯
///
/// <whitespace-token>
/// │├─╭─ <whitespace> ─╮─┤│
/// ╰────────────────╯
/// ```
///
/// While CSS stipulates that this token represents collapsed whitespace, it is possible for [Lexer][crate::Lexer]
/// to produce multiple consecutive [Kind::Whitespace] tokens if the
/// [Feature::SeparateWhitespace][crate::Feature::SeparateWhitespace] runtime feature is enabled. In this case,
/// `<whitespace-token>` becomes:
///
/// ```md
/// <whitespace-token>
/// │├──╮─╭─ " " ───────╮─╭──┤│
/// │ ╰─────────────╯ │
/// ├─╭─ "\t" ──────╮─┤
/// │ ╰─────────────╯ │
/// ╰─╭─ <newline> ─╮─╯
/// ╰─────────────╯
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#whitespace-token-diagram
#[default]
Whitespace = 0b0001,
/// Represents the [<comment>][1] defined in CSS. While CSS stipulates comment tokens are not produced during
/// tokenization, they are for this [Lexer][crate::Lexer] as they're needed in order to preserve them.
///
/// ```md
/// <comment>
/// ╭──────────────────────────────────────────╮
/// │├─ "/*" ─╯-╭─ (anything but "*" followed by "/") ─╮─╰─ "*/" ─┤│
/// ╰──────────────────────────────────────╯
/// ```
///
/// It is possible for [Lexer][crate::Lexer] to produce [Kind::Whitespace] tokens that begin `//` if the
/// [Feature::SingleLineComments][crate::Feature::SingleLineComments] runtime feature is enabled. In this mode,
/// `<comment>` becomes:
///
/// ```md
/// <comment>
/// ╭──────────────────────────────────────────╮
/// │├──╮─ "/*" ─╯-╭─ (anything but "*" followed by "/") ─╮─╰─ "*/" ─╭─┤│
/// │ ╰──────────────────────────────────────╯ │
/// │ ╭───────────────────────────╮ │
/// ╰─ "//" ───────╯-╭─ (anything but "\n") ─╮─╰─ "\n" ──────────╯
/// ╰───────────────────────╯
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#comment-diagram
Comment = 0b0010,
/// Represents both the [<cdc-token>][1] and [<cdo-token>][2]s defined in CSS. While CSS separates these tokens,
/// they're only useful representations at the top-level stylesheet, anywhere else they represent a parse error, and
/// it's a little pointless to define two tokens types for what amounts to a parse error.
///
/// ```md
/// <cdo-token>
/// │├─ "<!--" ─┤│
///
/// <cdc-token>
/// │├─ "-->" ─┤│
///
/// <cdc-or-cdo-token> (Not part of the CSS specification)
/// │├──╮─ <cdo-token> ─╭──┤│
/// ╰─ <crc-token> ─╯
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#CDC-token-diagram
/// [2]: https://drafts.csswg.org/css-syntax/#CDO-token-diagram
CdcOrCdo = 0b0011,
// Numerics (mask as 0b0_010X)
/// Represents the [<number-token>][1].
///
/// ```md
///
/// <number-token>
/// ╭─ "+" ─╮
/// │├─├───────┤───╭─ [digit] ─╮─ "." ─╭─ [digit] ─╮──╭───╮──────────────────────────────────╭──┤│
/// ╰─ "-" ─╯ │ ╰───────────╯ ╰───────────╯ │ │ ╭─ "+" ─╮ │
/// ├───────── ╭─ [digit] ─╮─────────────┤ ├─ "e" ─╭─├───────┤──╭─ [digit] ─╮─╯
/// │ ╰───────────╯ │ ╰─ "E" ─╯ ╰─ "-" ─╯ ╰───────────╯
/// ╰──── "." ─╭─ [digit] ─╮─────────────╯
/// ╰───────────╯
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#number-token-diagram
Number = 0b0100,
/// Represents the [<dimension-token>][1].
///
/// Here we deviate from the spec slightly, which has both [<dimension-token>][1] and [<percentage-token>][2].
/// `<percentage-token>` represents a dimension with a `%` symbol, but having this as a separate token results in more
/// work in the parser for little gain in the Lexer. So instead this lexer does not have a `<percentage-token>` and
/// instead folds the grammar for it inside of `<dimension-token>`.
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ───────╭──┤│
/// ├─ "\t" ──────┤
/// ╰─ <newline> ─╯
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <ident-token>
/// ╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
/// │├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
/// ╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
/// ╰───────────────────────────────────────╯
///
/// <number-token>
/// ╭─ "+" ─╮
/// │├─├───────┤─╮─╭─ [digit] ─╮─ "." ─╭─ [digit] ─╮──╭───╮──────────────────────────────────╭──┤│
/// ╰─ "-" ─╯ │ ╰───────────╯ ╰───────────╯ │ │ ╭─ "+" ─╮ │
/// ├───────── ╭─ [digit] ─╮─────────────┤ ├─ "e" ─╭─├───────┤──╭─ [digit] ─╮─╯
/// │ ╰───────────╯ │ ╰─ "E" ─╯ ╰─ "-" ─╯ ╰───────────╯
/// ╰──── "." ─╭─ [digit] ─╮─────────────╯
/// ╰───────────╯
///
/// <dimension-token>
/// │├─ <number-token> ─ <ident-token> ─┤│
///
/// ```
///
/// ```md
///
/// <dimension-token> // Refined for this lexer, not true to the standard.
/// │├─ <number-token> ─╮─ <ident-token> ─╭──┤│
/// ╰────── "%" ──────╯
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#dimension-token-diagram
/// [2]: https://drafts.csswg.org/css-syntax/#percentage-token-diagram
Dimension = 0b0101,
// Errors (mask as 0b1_XXXX)
/// Represents the [<bad-string-token>][1]. This token is a failure to fully lex the [<string-token>][2].
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-bad-string-token
/// [2]: https://drafts.csswg.org/css-syntax/#typedef-string-token
BadString = 0b1_1100,
/// Represents the [<bad-url-token>][1]. This token is a failure to fully lex the [<url-token>][2].
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-bad-url-token
/// [2]: https://drafts.csswg.org/css-syntax/#typedef-url-token
BadUrl = 0b1_1101,
/// These kind are non-standard Bad kinds and never emitted by the Lexer, but can be used by Parsers to denote a
/// token that are either:
/// - a Token that was unexpected in this position.
/// - a Token that was inserted to recover the parser to a known state.
BadWhitespace = 0b1_0001,
BadComment = 0b1_0010,
BadCdcOrCdo = 0b1_0011,
BadNumber = 0b1_0100,
BadDimension = 0b1_0101,
BadIdent = 0b1_1000,
BadFunction = 0b1_1001,
BadAtKeyword = 0b1_1010,
BadHash = 0b1_1011,
BadDelim = 0b1_1111,
// Variable length Ident-like Tokens (mask: 0b0_1XXX)
/// Represents the [<ident-token>][1].
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ─────╭──┤│
/// ├─ "\t" ────┤
/// ╰─ newline ─╯
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <ident-token>
/// ╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
/// │├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
/// ╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
/// ╰───────────────────────────────────────╯
///
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#ident-token-diagram
Ident = 0b1000,
/// Represents the [<function-token>][1].
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ───────╭──┤│
/// ├─ "\t" ──────┤
/// ╰─ <newline> ─╯
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <ident-token>
/// ╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
/// │├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
/// ╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
/// ╰───────────────────────────────────────╯
///
/// <function-token>
/// │├─ <ident-token> ─ "(" ─┤│
///
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#function-token-diagram
Function = 0b1001,
/// Represents the [<at-keyword-token>][1].
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ───────╭──┤│
/// ├─ "\t" ──────┤
/// ╰─ <newline> ─╯
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <ident-token>
/// ╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
/// │├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
/// ╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
/// ╰───────────────────────────────────────╯
///
/// <at-keyword-token>
/// │├─ "@" ─ <ident-token> ─┤│
///
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#hash-token-diagram
AtKeyword = 0b1010,
/// Represents the [<hash-token>][1].
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ───────╭──┤│
/// ├─ "\t" ──────┤
/// ╰─ <newline> ─╯
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <hash-token>
/// │├─ "#" ──╭─╮─ [a-z, A-Z, 0-9, "_", "-", non-ASCII] ─╭─╮─┤│
/// │ ╰─────────────── <escape> ───────────────╯ │
/// ╰────────────────────────────────────────────╯
///
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#at-keyword-token-diagram
Hash = 0b1011,
/// Represents the [<string-token>][1].
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <string-token>
/// ╭───────────────────────────────────╮
/// │├─╮─ """ ─╯─╭─╮─ [not """, "\", newline] ─╭─╮─╰── """ ─╭─┤│
/// │ │ ├──────── <escape> ─────────┤ │ │
/// │ │ ╰───── "\" ─ <newline> ─────╯ │ │
/// │ ╰───────────────────────────────╯ │
/// │ ╭───────────────────────────────────╮ │
/// ╰─ "'" ─╯─╭─╮─ [not """, "\", newline] ─╭─╮─╰── "'" ─╯
/// │ ├──────── <escape> ─────────┤ │
/// │ ╰───── "\" ─ <newline> ─────╯ │
/// ╰───────────────────────────────╯
///
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#string-token-diagram
String = 0b1100,
/// Represents the [<url-token>][1].
///
/// ```md
///
/// <newline>
/// │├──╮─ "\n" ───╭──┤│
/// ├─ "\r\n" ─┤
/// ├─ "\r" ───┤
/// ╰─ "\f" ───╯
///
/// <whitespace>
/// │├──╮─ " " ───────╭──┤│
/// ├─ "\t" ──────┤
/// ╰─ <newline> ─╯
///
/// <whitespace-token>
/// │├─╭─ <whitespace> ─╮─┤│
/// ╰────────────────╯
///
/// <ws*>
/// ╭──────────────────────────╮
/// │├─╯─╭─ <whitespace-token> ─╮─╰─┤│
/// ╰──────────────────────╯
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
///
/// <escape>
/// │├─ "\" ─╮───── [not <newline> or <hexdigit>] ───╭─┤│
/// ╰─╭── <hexdigit> ─╮──╮────────────────╭─╯
/// ╰─ (1-6 times) ─╯ ╰─ <whitespace> ─╯
///
/// <ident-token>
/// ╭───────────────── "--" ─────────────────────╮ ╭───────────────────────────────────────────╮
/// │├─╯─╮───────╭─╮─ [a-z, A-Z, "_", non-ASCII] ─╭─╰──╯─╭─╮─ [a-z, A-Z, 0-9, "_", non-ASCII] ─╭─╮─╰──┤│
/// ╰─ "-" ─╯ ╰──────── <escape> ────────────╯ │ ╰──────────── <escape> ─────────────╯ │
/// ╰───────────────────────────────────────╯
///
/// <url-token>
/// ╭───────────────────────────────────────────────────────────────────╮
/// │├─ <ident-token "url"> ─ "(" ─ <ws*> ─╯─╭─╮─ [not """ "'" "(" ")" "\" <whitespace> or non-printable] ─╭─╮─╰─ <ws*> ─ ")" ─┤│
/// │ ╰──────────────────────── <escape> ─────────────────────────╯ │
/// ╰───────────────────────────────────────────────────────────────╯
///
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#url-token-diagram
Url = 0b1101,
/// Represents the [<unicode-range-token>][1]. This token is only produced when the
/// [Feature::UnicodeRange][crate::Feature::UnicodeRange] feature is enabled.
///
/// ```md
///
/// <hexdigit>
/// │├─ [ 0-9, A-F, a-f ] ─┤│
///
/// <unicode-range-token>
/// │├─╮─ 'U' ─╭─ '+' ─╭──────────────────╭── <hexdigit> ─╮──────────────────╭─┤│
/// ╰─ 'u' ─╯ │ ╰─ (1-6 times) ─╯ │
/// │ ╭───────────────────╮ │
/// ├─╯─╭── <hexdigit> ─╮─╰─╭───────────── ? ───────────╮─┤
/// │ ╰─ (1-5 times) ─╯ ╰─ (1 to (6 digits) times) ─╯ │
/// │ │
/// ╰────╭── <hexdigit> ─╮── '-' ──╭── <hexdigit> ─╮──────╯
/// ╰─ (1-5 times) ─╯ ╰─ (1-5 times) ─╯
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#unicode-range-token-diagram
UnicodeRange = 0b1110,
/// Represents the [<delim-token>][1]. The `<delim-token>` has a value composed of a single code point.
///
/// ```md
/// <delim-token>
/// │├─ [codepoint] ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-delim-token
Delim = 0b1111,
// Single character Tokens (mask 0b11_XXXX)
/// Represents the [<colon-token>][1].
///
/// ```md
/// <colon-token>
/// │├─ ":" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-colon-token
Colon = 0b10_0001,
/// Represents the [<semicolon-token>][1].
///
/// ```md
/// <semicolon-token>
/// │├─ ";" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-semicolon-token
Semicolon = 0b10_0010,
/// Represents the [<comma-token>][1].
///
/// ```md
/// <comma-token>
/// │├─ "," ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-comma-token
Comma = 0b10_0011,
/// Represents the [<\[-token>][1].
///
/// ```md
/// <[-token>
/// │├─ "[" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-open-square
LeftSquare = 0b10_0100,
/// Represents the [<\]-token>][1].
///
/// ```md
/// <]-token>
/// │├─ "]" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-close-square
RightSquare = 0b10_0101,
/// Represents the [<(-token>][1].
///
/// ```md
/// <(-token>
/// │├─ "(" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-open-paren
LeftParen = 0b10_0110,
/// Represents the [<)-token>][1].
///
/// ```md
/// <)-token>
/// │├─ ")" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-close-paren
RightParen = 0b10_0111,
/// Represents the [<{-token>][1].
///
/// ```md
/// <{-token>
/// │├─ "{" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-open-curly
LeftCurly = 0b10_1000,
/// Represents the [<}-token>][1].
///
/// ```md
/// <}-token>
/// │├─ "}" ─┤│
/// ```
///
/// [1]: https://drafts.csswg.org/css-syntax/#typedef-close-curly
RightCurly = 0b10_1001,
/// These kind are non-standard Bad kinds and never emitted by the Lexer, but can be used by Parsers to denote a
/// token that are either:
/// - a Token that was unexpected in this position.
/// - a Token that was inserted to recover the parser to a known state.
BadColon = 0b11_0001,
BadSemicolon = 0b11_0010,
BadComma = 0b11_0011,
BadLeftSquare = 0b11_0100,
BadRightSquare = 0b11_0101,
BadLeftParen = 0b11_0110,
BadRightParen = 0b11_0111,
BadLeftCurly = 0b11_1000,
BadRightCurly = 0b11_1001,
}
impl Kind {
pub(crate) const fn from_bits(bits: u8) -> Self {
match bits {
0b0001 => Self::Whitespace,
0b0010 => Self::Comment,
0b0011 => Self::CdcOrCdo,
0b0100 => Self::Number,
0b0101 => Self::Dimension,
// 0b0110 => Reserved
// 0b0111 => Reserved
0b1000 => Self::Ident,
0b1001 => Self::Function,
0b1010 => Self::AtKeyword,
0b1011 => Self::Hash,
0b1100 => Self::String,
0b1101 => Self::Url,
0b1110 => Self::UnicodeRange,
0b1111 => Self::Delim,
// Error tokens are represented in 5 bits.
0b1_0001 => Self::BadWhitespace,
0b1_0010 => Self::BadComment,
0b1_0011 => Self::BadCdcOrCdo,
0b1_0100 => Self::BadNumber,
0b1_0101 => Self::BadDimension,
// 0b1_0110 => Self::Reserved,
// 0b1_0111 => Self::Reserved,
0b1_1000 => Self::BadIdent,
0b1_1001 => Self::BadFunction,
0b1_1010 => Self::BadAtKeyword,
0b1_1011 => Self::BadHash,
0b1_1100 => Self::BadString,
0b1_1101 => Self::BadUrl,
0b1_1110 => Self::UnicodeRange,
0b1_1111 => Self::BadDelim,
// Single character delimiters are represented in 6 bits
0b10_0001 => Self::Colon,
0b10_0010 => Self::Semicolon,
0b10_0011 => Self::Comma,
0b10_0100 => Self::LeftSquare,
0b10_0101 => Self::RightSquare,
0b10_0110 => Self::LeftParen,
0b10_0111 => Self::RightParen,
0b10_1000 => Self::LeftCurly,
0b10_1001 => Self::RightCurly,
0b11_0001 => Self::BadColon,
0b11_0010 => Self::BadSemicolon,
0b11_0011 => Self::BadComma,
0b11_0100 => Self::BadLeftSquare,
0b11_0101 => Self::BadRightSquare,
0b11_0110 => Self::BadLeftParen,
0b11_0111 => Self::BadRightParen,
0b11_1000 => Self::BadLeftCurly,
0b11_1001 => Self::BadRightCurly,
_ => Self::Eof,
}
}
#[doc(hidden)]
pub const fn as_str(&self) -> &'static str {
match *self {
Kind::Eof => "Eof",
Kind::Whitespace => "Whitespace",
Kind::Comment => "Comment",
Kind::CdcOrCdo => "CdcOrCdo",
Kind::Number => "Number",
Kind::Dimension => "Dimension",
Kind::Ident => "Ident",
Kind::Function => "Function",
Kind::AtKeyword => "AtKeyword",
Kind::Hash => "Hash",
Kind::String => "String",
Kind::Url => "Url",
Kind::UnicodeRange => "UnicodeRange",
Kind::Delim => "Delim",
Kind::BadWhitespace => "BadWhitespace",
Kind::BadComment => "BadComment",
Kind::BadCdcOrCdo => "BadCdcOrCdo",
Kind::BadNumber => "BadNumber",
Kind::BadDimension => "BadDimension",
Kind::BadIdent => "BadIdent",
Kind::BadFunction => "BadFunction",
Kind::BadAtKeyword => "BadAtKeyword",
Kind::BadHash => "BadHash",
Kind::BadString => "BadString",
Kind::BadUrl => "BadUrl",
Kind::BadDelim => "BadDelim",
Kind::Colon => "Colon",
Kind::Semicolon => "Semicolon",
Kind::Comma => "Comma",
Kind::LeftSquare => "LeftSquare",
Kind::RightSquare => "RightSquare",
Kind::LeftParen => "LeftParen",
Kind::RightParen => "RightParen",
Kind::LeftCurly => "LeftCurly",
Kind::RightCurly => "RightCurly",
Kind::BadColon => "BadColon",
Kind::BadSemicolon => "BadSemicolon",
Kind::BadComma => "BadComma",
Kind::BadLeftSquare => "BadLeftSquare",
Kind::BadRightSquare => "BadRightSquare",
Kind::BadLeftParen => "BadLeftParen",
Kind::BadRightParen => "BadRightParen",
Kind::BadLeftCurly => "BadLeftCurly",
Kind::BadRightCurly => "BadRightCurly",
}
}
pub const fn is_bad(&self) -> bool {
(*self as u8) & 0b11_0000 == 0b01_0000
}
}
impl fmt::Debug for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Kind::{}", self.as_str())
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Kind::{}", self.as_str())
}
}
impl PartialEq<KindSet> for Kind {
fn eq(&self, other: &KindSet) -> bool {
other.contains_bits(*self as u8)
}
}
#[test]
fn test_from_bits() {
assert_eq!(Kind::from_bits(Kind::Eof as u8), Kind::Eof);
assert_eq!(Kind::from_bits(Kind::Whitespace as u8), Kind::Whitespace);
assert_eq!(Kind::from_bits(Kind::Comment as u8), Kind::Comment);
assert_eq!(Kind::from_bits(Kind::CdcOrCdo as u8), Kind::CdcOrCdo);
assert_eq!(Kind::from_bits(Kind::Number as u8), Kind::Number);
assert_eq!(Kind::from_bits(Kind::Dimension as u8), Kind::Dimension);
assert_eq!(Kind::from_bits(Kind::Ident as u8), Kind::Ident);
assert_eq!(Kind::from_bits(Kind::Function as u8), Kind::Function);
assert_eq!(Kind::from_bits(Kind::AtKeyword as u8), Kind::AtKeyword);
assert_eq!(Kind::from_bits(Kind::Hash as u8), Kind::Hash);
assert_eq!(Kind::from_bits(Kind::String as u8), Kind::String);
assert_eq!(Kind::from_bits(Kind::Url as u8), Kind::Url);
assert_eq!(Kind::from_bits(Kind::UnicodeRange as u8), Kind::UnicodeRange);
assert_eq!(Kind::from_bits(Kind::Delim as u8), Kind::Delim);
assert_eq!(Kind::from_bits(Kind::BadWhitespace as u8), Kind::BadWhitespace);
assert_eq!(Kind::from_bits(Kind::BadComment as u8), Kind::BadComment);
assert_eq!(Kind::from_bits(Kind::BadCdcOrCdo as u8), Kind::BadCdcOrCdo);
assert_eq!(Kind::from_bits(Kind::BadNumber as u8), Kind::BadNumber);
assert_eq!(Kind::from_bits(Kind::BadDimension as u8), Kind::BadDimension);
assert_eq!(Kind::from_bits(Kind::BadIdent as u8), Kind::BadIdent);
assert_eq!(Kind::from_bits(Kind::BadFunction as u8), Kind::BadFunction);
assert_eq!(Kind::from_bits(Kind::BadAtKeyword as u8), Kind::BadAtKeyword);
assert_eq!(Kind::from_bits(Kind::BadHash as u8), Kind::BadHash);
assert_eq!(Kind::from_bits(Kind::BadString as u8), Kind::BadString);
assert_eq!(Kind::from_bits(Kind::BadUrl as u8), Kind::BadUrl);
assert_eq!(Kind::from_bits(Kind::BadDelim as u8), Kind::BadDelim);
assert_eq!(Kind::from_bits(Kind::Colon as u8), Kind::Colon);
assert_eq!(Kind::from_bits(Kind::Semicolon as u8), Kind::Semicolon);
assert_eq!(Kind::from_bits(Kind::Comma as u8), Kind::Comma);
assert_eq!(Kind::from_bits(Kind::LeftSquare as u8), Kind::LeftSquare);
assert_eq!(Kind::from_bits(Kind::RightSquare as u8), Kind::RightSquare);
assert_eq!(Kind::from_bits(Kind::LeftParen as u8), Kind::LeftParen);
assert_eq!(Kind::from_bits(Kind::RightParen as u8), Kind::RightParen);
assert_eq!(Kind::from_bits(Kind::LeftCurly as u8), Kind::LeftCurly);
assert_eq!(Kind::from_bits(Kind::RightCurly as u8), Kind::RightCurly);
assert_eq!(Kind::from_bits(Kind::BadColon as u8), Kind::BadColon);
assert_eq!(Kind::from_bits(Kind::BadSemicolon as u8), Kind::BadSemicolon);
assert_eq!(Kind::from_bits(Kind::BadComma as u8), Kind::BadComma);
assert_eq!(Kind::from_bits(Kind::BadLeftSquare as u8), Kind::BadLeftSquare);
assert_eq!(Kind::from_bits(Kind::BadRightSquare as u8), Kind::BadRightSquare);
assert_eq!(Kind::from_bits(Kind::BadLeftParen as u8), Kind::BadLeftParen);
assert_eq!(Kind::from_bits(Kind::BadRightParen as u8), Kind::BadRightParen);
assert_eq!(Kind::from_bits(Kind::BadLeftCurly as u8), Kind::BadLeftCurly);
assert_eq!(Kind::from_bits(Kind::BadRightCurly as u8), Kind::BadRightCurly);
}
#[test]
fn size_test() {
assert_eq!(::std::mem::size_of::<Kind>(), 1);
}