parsec-html-css 0.1.0

CSS Syntax Level 3 for Parsec: tokenizer, parser, An+B and serialization, tested against css-parsing-tests.
Documentation
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway

//! The parser, CSS Syntax §5, with the entry points of the current
//! editor's draft.
//!
//! Each algorithm reads component values, not tokens: from the tokenizer,
//! which builds a block or function whole as soon as it starts, or from a
//! block already parsed. The spec allows either, and it means a rule's
//! `{}` block can be kept as it is and its contents parsed later, by
//! [`SimpleBlock::contents`], with the same result as parsing them at once.
//!
//! Nothing here recurses on the input's nesting, so `((((…` a million deep
//! parses, serializes and drops on any stack.

use std::borrow::Cow;

use crate::tokenizer::{Span, Token, Tokenizer};

/// Which bracket a [`SimpleBlock`] was written with.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Bracket {
    /// `( )`
    Paren,
    /// `[ ]`
    Square,
    /// `{ }`
    Curly,
}

impl Bracket {
    pub fn open(self) -> char {
        match self {
            Bracket::Paren => '(',
            Bracket::Square => '[',
            Bracket::Curly => '{',
        }
    }

    pub fn close(self) -> char {
        match self {
            Bracket::Paren => ')',
            Bracket::Square => ']',
            Bracket::Curly => '}',
        }
    }
}

/// A token, a function or a block: what every value, prelude and block in
/// CSS is a list of.
#[derive(Clone, Debug, PartialEq)]
pub enum ComponentValue<'a> {
    /// Any token but the ones that open a function or a block. `)`, `]` and
    /// `}` with nothing to close are kept too, as the spec keeps them.
    Token(Token<'a>, Span),
    Function(Function<'a>),
    Block(SimpleBlock<'a>),
}

impl<'a> ComponentValue<'a> {
    pub fn span(&self) -> Span {
        match self {
            ComponentValue::Token(_, span) => *span,
            ComponentValue::Function(f) => f.span,
            ComponentValue::Block(b) => b.span,
        }
    }

    pub fn token(&self) -> Option<&Token<'a>> {
        match self {
            ComponentValue::Token(t, _) => Some(t),
            _ => None,
        }
    }

    pub fn is_whitespace(&self) -> bool {
        matches!(self, ComponentValue::Token(Token::Whitespace, _))
    }

    /// Whether this is `token`, which is one without a value, or a delim.
    fn is(&self, token: &Token<'_>) -> bool {
        match (self, token) {
            (ComponentValue::Token(Token::Delim(a), _), Token::Delim(b)) => a == b,
            (ComponentValue::Token(t, _), _) => {
                std::mem::discriminant(t) == std::mem::discriminant(token)
            }
            _ => false,
        }
    }

    fn is_curly(&self) -> bool {
        matches!(self, ComponentValue::Block(b) if b.bracket == Bracket::Curly)
    }
}

/// `name( … )`.
#[derive(Clone, Debug, PartialEq)]
pub struct Function<'a> {
    pub name: Cow<'a, str>,
    pub arguments: Vec<ComponentValue<'a>>,
    /// From the name to the `)`, or to the end of the input if it's missing.
    pub span: Span,
}

/// `( … )`, `[ … ]` or `{ … }`.
#[derive(Clone, Debug, PartialEq)]
pub struct SimpleBlock<'a> {
    pub bracket: Bracket,
    pub contents: Vec<ComponentValue<'a>>,
    pub span: Span,
}

/// Dropping a child at a time from a list, rather than letting each list
/// drop its own children, keeps a deep tree off the stack.
fn drop_flat(children: &mut Vec<ComponentValue<'_>>) {
    if children
        .iter()
        .all(|c| matches!(c, ComponentValue::Token(..)))
    {
        return;
    }
    let mut stack = std::mem::take(children);
    while let Some(child) = stack.pop() {
        match child {
            ComponentValue::Function(mut f) => stack.append(&mut f.arguments),
            ComponentValue::Block(mut b) => stack.append(&mut b.contents),
            ComponentValue::Token(..) => {}
        }
    }
}

impl Drop for Function<'_> {
    fn drop(&mut self) {
        drop_flat(&mut self.arguments);
    }
}

impl Drop for SimpleBlock<'_> {
    fn drop(&mut self) {
        drop_flat(&mut self.contents);
    }
}

impl<'a> SimpleBlock<'a> {
    /// Parses a `{}` block's contents as declarations and rules, the way
    /// style rules and nested at-rules hold them. `src` is the text the
    /// block was parsed from.
    pub fn contents(mut self, src: &'a str) -> BlockContents<'a> {
        let values = std::mem::take(&mut self.contents);
        let mut p = Parser::new(src, values.into_iter());
        let items = p.block_contents();
        BlockContents {
            items,
            errors: p.errors,
        }
    }
}

/// `@name prelude;` or `@name prelude { block }`.
///
/// `B` is what the `{}` block is kept as: a [`SimpleBlock`], unparsed,
/// from [`parse_stylesheet`] and the other entry points, or a [`Block`],
/// its contents parsed, from [`parse_stylesheet_deep`].
#[derive(Clone, Debug, PartialEq)]
pub struct AtRule<'a, B = SimpleBlock<'a>> {
    pub name: Cow<'a, str>,
    pub prelude: Vec<ComponentValue<'a>>,
    /// The `{}` block. Unparsed, what it holds depends on the rule, so
    /// [`SimpleBlock::contents`] parses it when the rule calls for that.
    pub block: Option<B>,
    pub span: Span,
}

/// `prelude { block }`: a style rule when the prelude is a selector. `B`
/// is as for [`AtRule`].
#[derive(Clone, Debug, PartialEq)]
pub struct QualifiedRule<'a, B = SimpleBlock<'a>> {
    pub prelude: Vec<ComponentValue<'a>>,
    /// The `{}` block. Unparsed, [`SimpleBlock::contents`] gives its
    /// declarations and nested rules.
    pub block: B,
    pub span: Span,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Rule<'a, B = SimpleBlock<'a>> {
    At(AtRule<'a, B>),
    Qualified(QualifiedRule<'a, B>),
}

impl<B> Rule<'_, B> {
    pub fn span(&self) -> Span {
        match self {
            Rule::At(r) => r.span,
            Rule::Qualified(r) => r.span,
        }
    }
}

/// `name: value !important`.
#[derive(Clone, Debug, PartialEq)]
pub struct Declaration<'a> {
    pub name: Cow<'a, str>,
    /// Without the whitespace at either end or the `!important`.
    pub value: Vec<ComponentValue<'a>>,
    pub important: bool,
    /// From the name to the end of the value or the `important`, not
    /// including the `;`.
    pub span: Span,
}

impl Declaration<'_> {
    /// The value as it was written, which a custom property keeps:
    /// comments and all, from `src`, the text it was parsed from.
    pub fn original_text<'s>(&self, src: &'s str) -> &'s str {
        match (self.value.first(), self.value.last()) {
            (Some(first), Some(last)) => &src[first.span().start..last.span().end],
            _ => "",
        }
    }
}

/// One thing in a block: style rules and the nesting-era at-rules mix
/// declarations and rules, in order.
#[derive(Clone, Debug, PartialEq)]
pub enum Item<'a, B = SimpleBlock<'a>> {
    Declaration(Declaration<'a>),
    Rule(Rule<'a, B>),
}

/// A rule's `{}` block with its contents parsed, as declarations and
/// rules, and its rules' blocks too: what [`parse_stylesheet_deep`] makes.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Block<'a> {
    pub items: Vec<Item<'a, Block<'a>>>,
    /// From the `{` to the `}`, or to the end of the input if it's missing.
    pub span: Span,
}

impl Drop for Block<'_> {
    /// Takes the blocks inside out a level at a time, as [`drop_flat`]
    /// does for component values, so deep nesting stays off the stack.
    fn drop(&mut self) {
        if self.items.iter().all(|i| matches!(i, Item::Declaration(_))) {
            return;
        }
        let mut stack = std::mem::take(&mut self.items);
        while let Some(item) = stack.pop() {
            match item {
                Item::Rule(Rule::Qualified(mut r)) => stack.append(&mut r.block.items),
                Item::Rule(Rule::At(AtRule {
                    block: Some(mut b), ..
                })) => stack.append(&mut b.items),
                _ => {}
            }
        }
    }
}

/// What a parse found wrong, and where.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ParseError {
    pub kind: ErrorKind,
    pub span: Span,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind {
    /// Nothing but whitespace and comments where one thing was wanted.
    Empty,
    /// More after the one thing that was wanted.
    ExtraInput,
    /// Something that isn't a declaration where one was wanted, dropped.
    InvalidDeclaration,
    /// A rule that didn't parse (a prelude with no block, say), dropped.
    InvalidRule,
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let what = match self.kind {
            ErrorKind::Empty => "nothing to parse",
            ErrorKind::ExtraInput => "unexpected input after the end",
            ErrorKind::InvalidDeclaration => "invalid declaration",
            ErrorKind::InvalidRule => "invalid rule",
        };
        write!(f, "{what} at bytes {}..{}", self.span.start, self.span.end)
    }
}

impl std::error::Error for ParseError {}

/// A stylesheet's rules, and what was dropped while parsing it.
#[derive(Clone, Debug, PartialEq)]
pub struct Stylesheet<'a, B = SimpleBlock<'a>> {
    pub rules: Vec<Rule<'a, B>>,
    pub errors: Vec<ParseError>,
}

impl<B> Default for Stylesheet<'_, B> {
    fn default() -> Self {
        Stylesheet {
            rules: Vec::new(),
            errors: Vec::new(),
        }
    }
}

/// A block's declarations and rules, and what was dropped while parsing it.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct BlockContents<'a> {
    pub items: Vec<Item<'a>>,
    pub errors: Vec<ParseError>,
}

/// Component values straight from the tokenizer. A function or block is
/// built whole, with a stack of open ones rather than recursion. The
/// values of every open one share one buffer, so each list is allocated
/// once, at its final size, when it closes.
pub(crate) struct Values<'a> {
    pub(crate) tokens: Tokenizer<'a>,
    open: Vec<Open<'a>>,
    buffer: Vec<ComponentValue<'a>>,
}

impl<'a> Values<'a> {
    pub(crate) fn new(tokens: Tokenizer<'a>) -> Values<'a> {
        Values {
            tokens,
            open: Vec::new(),
            buffer: Vec::new(),
        }
    }
}

struct Open<'a> {
    /// `None` for a function.
    bracket: Option<Bracket>,
    name: Cow<'a, str>,
    start: usize,
    /// Where its values start in the buffer.
    at: usize,
}

/// Moves `buf[at..]` into a list of its own size, by copying the bytes,
/// and leaves `buf` its room for next time.
pub(crate) fn take_tail<T>(buf: &mut Vec<T>, at: usize) -> Vec<T> {
    if at > 0 {
        return buf.split_off(at);
    }
    // `split_off(0)` would hand over the whole buffer, room and all.
    let mut tail = Vec::with_capacity(buf.len());
    tail.append(buf);
    tail
}

/// What a token opens, if anything.
#[inline]
fn opens<'a>(token: Token<'a>, start: usize, at: usize) -> Result<Open<'a>, Token<'a>> {
    let (bracket, name) = match token {
        Token::Function(name) => (None, name),
        Token::LeftParen => (Some(Bracket::Paren), Cow::Borrowed("")),
        Token::LeftBracket => (Some(Bracket::Square), Cow::Borrowed("")),
        Token::LeftBrace => (Some(Bracket::Curly), Cow::Borrowed("")),
        other => return Err(other),
    };
    Ok(Open {
        bracket,
        name,
        start,
        at,
    })
}

impl<'a> Open<'a> {
    fn closes(&self, token: &Token<'_>) -> bool {
        matches!(
            (self.bracket, token),
            (None | Some(Bracket::Paren), Token::RightParen)
                | (Some(Bracket::Square), Token::RightBracket)
                | (Some(Bracket::Curly), Token::RightBrace)
        )
    }

    fn finish(self, values: Vec<ComponentValue<'a>>, end: usize) -> ComponentValue<'a> {
        let span = Span::new(self.start, end);
        match self.bracket {
            None => ComponentValue::Function(Function {
                name: self.name,
                arguments: values,
                span,
            }),
            Some(bracket) => ComponentValue::Block(SimpleBlock {
                bracket,
                contents: values,
                span,
            }),
        }
    }
}

impl<'a> Iterator for Values<'a> {
    type Item = ComponentValue<'a>;

    fn next(&mut self) -> Option<ComponentValue<'a>> {
        let (token, span) = self.tokens.next_token()?;
        Some(self.whole(token, span))
    }
}

impl<'a> Values<'a> {
    /// The component value `token` starts: the token, or the whole of the
    /// function or block it opens.
    #[inline]
    pub(crate) fn whole(&mut self, token: Token<'a>, span: Span) -> ComponentValue<'a> {
        match opens(token, span.start, 0) {
            Ok(open) => self.open.push(open),
            Err(token) => return ComponentValue::Token(token, span),
        }
        self.rest_of_open()
    }

    fn rest_of_open(&mut self) -> ComponentValue<'a> {
        loop {
            let Some((token, span)) = self.tokens.next_token() else {
                // Everything still open closes at the end of the input.
                let end = self.tokens.source().len();
                loop {
                    let open = self.open.pop().unwrap();
                    let values = take_tail(&mut self.buffer, open.at);
                    let done = open.finish(values, end);
                    if self.open.is_empty() {
                        return done;
                    }
                    self.buffer.push(done);
                }
            };
            if self.open.last().unwrap().closes(&token) {
                let open = self.open.pop().unwrap();
                let values = take_tail(&mut self.buffer, open.at);
                let done = open.finish(values, span.end);
                if self.open.is_empty() {
                    return done;
                }
                self.buffer.push(done);
                continue;
            }
            if !matches!(
                token,
                Token::Function(_) | Token::LeftParen | Token::LeftBracket | Token::LeftBrace
            ) {
                self.buffer.push(ComponentValue::Token(token, span));
                continue;
            }
            match opens(token, span.start, self.buffer.len()) {
                Ok(open) => self.open.push(open),
                Err(token) => self.buffer.push(ComponentValue::Token(token, span)),
            }
        }
    }
}

/// Where a parser's values come from. A parsed block's own list can be
/// looked along, so a declaration's values move out of it in one go.
trait Source<'a>: Iterator<Item = ComponentValue<'a>> {
    /// What's left, if it can be seen without taking it.
    fn rest(&self) -> Option<&[ComponentValue<'a>]> {
        None
    }
}

impl<'a> Source<'a> for Values<'a> {}

impl<'a> Source<'a> for std::vec::IntoIter<ComponentValue<'a>> {
    fn rest(&self) -> Option<&[ComponentValue<'a>]> {
        Some(self.as_slice())
    }
}

/// How many values come before the next `;` (or, nested, `}`).
fn before_semicolon(values: &[ComponentValue<'_>], nested: bool) -> usize {
    values
        .iter()
        .position(|v| v.is(&Token::Semicolon) || (nested && v.is(&Token::RightBrace)))
        .unwrap_or(values.len())
}

/// A stream of component values with the spec's one item of lookahead,
/// and room to put back what a failed declaration took.
struct Parser<'a, I> {
    src: &'a str,
    input: I,
    back: Vec<ComponentValue<'a>>,
    /// Where a prelude collects, reused from rule to rule.
    scratch: Vec<ComponentValue<'a>>,
    errors: Vec<ParseError>,
}

fn text(src: &str) -> Parser<'_, Values<'_>> {
    Parser::new(src, Values::new(Tokenizer::new(src)))
}

impl<'a, I: Source<'a>> Parser<'a, I> {
    fn new(src: &'a str, input: I) -> Parser<'a, I> {
        Parser {
            src,
            input,
            back: Vec::new(),
            scratch: Vec::new(),
            errors: Vec::new(),
        }
    }

    fn peek(&mut self) -> Option<&ComponentValue<'a>> {
        if self.back.is_empty() {
            if self.input.rest().is_some() {
                return self.input.rest().and_then(<[_]>::first);
            }
            let next = self.input.next()?;
            self.back.push(next);
        }
        self.back.last()
    }

    fn next(&mut self) -> Option<ComponentValue<'a>> {
        self.back.pop().or_else(|| self.input.next())
    }

    fn put_back(&mut self, values: Vec<ComponentValue<'a>>) {
        self.back.extend(values.into_iter().rev());
    }

    fn skip_whitespace(&mut self) {
        while self.peek().is_some_and(ComponentValue::is_whitespace) {
            self.next();
        }
    }

    fn error(&mut self, kind: ErrorKind, start: usize, end: usize) {
        self.errors.push(ParseError {
            kind,
            span: Span::new(start, end.max(start)),
        });
    }

    /// Consume a stylesheet's contents. With `cdo_cdc` false this is the
    /// 2021 draft's "list of rules", where `<!--` and `-->` aren't skipped.
    fn rules(&mut self, cdo_cdc: bool) -> Vec<Rule<'a>> {
        let mut rules = Vec::new();
        while let Some(next) = self.peek() {
            match next {
                ComponentValue::Token(Token::Whitespace, _) => {
                    self.next();
                }
                ComponentValue::Token(Token::Cdo | Token::Cdc, _) if cdo_cdc => {
                    self.next();
                }
                ComponentValue::Token(Token::AtKeyword(_), _) => {
                    let rule = self.at_rule(false);
                    rules.push(Rule::At(rule));
                }
                _ => {
                    if let Some(rule) = self.qualified_rule(false, false) {
                        rules.push(Rule::Qualified(rule));
                    }
                }
            }
        }
        rules
    }

    /// Consume an at-rule; the next value is its at-keyword. Every at-rule
    /// is valid at this level, so one always comes back.
    fn at_rule(&mut self, nested: bool) -> AtRule<'a> {
        let Some(ComponentValue::Token(Token::AtKeyword(name), span)) = self.next() else {
            unreachable!("at_rule is called on an at-keyword");
        };
        let mut rule = AtRule {
            name,
            prelude: Vec::new(),
            block: None,
            span,
        };
        while let Some(next) = self.next() {
            match next {
                ComponentValue::Token(Token::Semicolon, _) => break,
                ComponentValue::Token(Token::RightBrace, _) if nested => {
                    self.back.push(next);
                    break;
                }
                ComponentValue::Block(block) if block.bracket == Bracket::Curly => {
                    rule.span.end = block.span.end;
                    rule.block = Some(block);
                    break;
                }
                other => {
                    rule.span.end = other.span().end;
                    self.scratch.push(other);
                }
            }
        }
        rule.prelude = take_tail(&mut self.scratch, 0);
        rule
    }

    /// Consume a qualified rule. `stop` is the nested form's `;`, which
    /// ends a rule that's really a broken declaration.
    fn qualified_rule(&mut self, stop: bool, nested: bool) -> Option<QualifiedRule<'a>> {
        let end_of_input = self.src.len();
        let start = self.peek().map_or(end_of_input, |v| v.span().start);
        // The prelude collects in `scratch`, to be allocated once at its size.
        self.scratch.clear();
        let mut end = start;
        loop {
            let Some(next) = self.next() else {
                self.scratch.clear();
                self.error(ErrorKind::InvalidRule, start, end);
                return None;
            };
            match next {
                ComponentValue::Token(Token::Semicolon, _) if stop => {
                    self.back.push(next);
                    self.scratch.clear();
                    self.error(ErrorKind::InvalidRule, start, end);
                    return None;
                }
                ComponentValue::Token(Token::RightBrace, _) if nested => {
                    self.back.push(next);
                    self.scratch.clear();
                    self.error(ErrorKind::InvalidRule, start, end);
                    return None;
                }
                ComponentValue::Block(block) if block.bracket == Bracket::Curly => {
                    let end = block.span.end;
                    let prelude = take_tail(&mut self.scratch, 0);
                    if looks_like_custom_property(&prelude) {
                        // `--x:hover {…}` is a custom property gone wrong,
                        // never a rule; nested, the property runs to its `;`.
                        if nested {
                            self.bad_declaration_remnants(true);
                        }
                        self.error(ErrorKind::InvalidRule, start, end);
                        return None;
                    }
                    return Some(QualifiedRule {
                        prelude,
                        block,
                        span: Span::new(start, end),
                    });
                }
                other => {
                    end = other.span().end;
                    self.scratch.push(other);
                }
            }
        }
    }

    fn bad_declaration_remnants(&mut self, nested: bool) {
        while let Some(next) = self.next() {
            match next {
                ComponentValue::Token(Token::Semicolon, _) => return,
                ComponentValue::Token(Token::RightBrace, _) if nested => {
                    self.back.push(next);
                    return;
                }
                _ => {}
            }
        }
    }

    /// Everything up to the next `;` (or the `}` ending a nested block), the
    /// most a declaration can be.
    fn up_to_semicolon(&mut self, nested: bool) -> Vec<ComponentValue<'a>> {
        let mut values = Vec::new();
        while let Some(next) = self.peek() {
            if next.is(&Token::Semicolon) || (nested && next.is(&Token::RightBrace)) {
                break;
            }
            values.push(self.next().unwrap());
        }
        values
    }

    /// Consume a declaration: takes what one could be, and gives it back if
    /// it isn't one, for the caller to try as a rule.
    fn declaration(&mut self, nested: bool) -> Result<Declaration<'a>, Vec<ComponentValue<'a>>> {
        // From a parsed block, look first and move only what's kept.
        if self.back.is_empty()
            && let Some(rest) = self.input.rest()
        {
            let n = before_semicolon(rest, nested);
            if let Some(plan) = plan(&rest[..n]) {
                let name = self.input.next().unwrap();
                // Not `skip(…).take(…)`: an empty take never runs the skip,
                // which left the colon behind to be read as a rule.
                for _ in 1..plan.value_start {
                    self.input.next();
                }
                let value = self
                    .input
                    .by_ref()
                    .take(plan.value_end - plan.value_start)
                    .collect();
                // Past the `!important` and trailing whitespace.
                for _ in plan.value_end.max(plan.value_start)..n {
                    self.input.next();
                }
                return Ok(build(name, value, &plan, self.src));
            }
            return Err(self.input.by_ref().take(n).collect());
        }
        let values = self.up_to_semicolon(nested);
        declaration_from(values, self.src)
    }

    /// Consume a block's contents.
    fn block_contents(&mut self) -> Vec<Item<'a>> {
        let mut items = Vec::new();
        while let Some(next) = self.peek() {
            match next {
                ComponentValue::Token(Token::Whitespace | Token::Semicolon, _) => {
                    self.next();
                }
                // Only parsed text can have a `}` here, a block's own never
                // does: it's dropped, as a stray one is at the top level.
                ComponentValue::Token(Token::RightBrace, span) => {
                    let span = *span;
                    self.next();
                    self.error(ErrorKind::InvalidRule, span.start, span.end);
                }
                ComponentValue::Token(Token::AtKeyword(_), _) => {
                    let rule = self.at_rule(true);
                    items.push(Item::Rule(Rule::At(rule)));
                }
                // Only an ident can start a declaration.
                ComponentValue::Token(Token::Ident(_), _) => match self.declaration(true) {
                    Ok(decl) => items.push(Item::Declaration(decl)),
                    Err(values) => {
                        self.put_back(values);
                        if let Some(rule) = self.qualified_rule(true, true) {
                            items.push(Item::Rule(Rule::Qualified(rule)));
                        }
                    }
                },
                _ => {
                    if let Some(rule) = self.qualified_rule(true, true) {
                        items.push(Item::Rule(Rule::Qualified(rule)));
                    }
                }
            }
        }
        items
    }

    /// Parse a list of declarations, keeping the at-rules the spec has the
    /// caller drop, in case the caller wants them.
    fn declaration_list(&mut self) -> Vec<Item<'a>> {
        let mut items = Vec::new();
        while let Some(next) = self.peek() {
            match next {
                ComponentValue::Token(Token::Whitespace | Token::Semicolon, _) => {
                    self.next();
                }
                ComponentValue::Token(Token::AtKeyword(_), _) => {
                    let rule = self.at_rule(false);
                    items.push(Item::Rule(Rule::At(rule)));
                }
                _ => match self.declaration(false) {
                    Ok(decl) => items.push(Item::Declaration(decl)),
                    Err(values) => {
                        let start = values.first().map_or(0, |v| v.span().start);
                        let end = values.last().map_or(start, |v| v.span().end);
                        self.error(ErrorKind::InvalidDeclaration, start, end);
                    }
                },
            }
        }
        items
    }

    /// Fails with ExtraInput unless only whitespace is left.
    fn end<T>(&mut self, value: T) -> Result<T, ParseError> {
        self.skip_whitespace();
        match self.peek() {
            None => Ok(value),
            Some(v) => {
                let start = v.span().start;
                Err(ParseError {
                    kind: ErrorKind::ExtraInput,
                    span: Span::new(start, self.src.len()),
                })
            }
        }
    }

    fn start(&mut self) -> Result<(), ParseError> {
        self.skip_whitespace();
        match self.peek() {
            Some(_) => Ok(()),
            None => Err(ParseError {
                kind: ErrorKind::Empty,
                span: Span::new(0, self.src.len()),
            }),
        }
    }
}

/// Whether a prelude starts like a custom property: `--name` then `:`.
pub(crate) fn looks_like_custom_property(prelude: &[ComponentValue<'_>]) -> bool {
    let mut it = prelude.iter().filter(|v| !v.is_whitespace());
    matches!(it.next(), Some(ComponentValue::Token(Token::Ident(n), _)) if n.starts_with("--"))
        && it.next().is_some_and(|v| v.is(&Token::Colon))
}

/// Where a declaration's parts are in the values `up_to_semicolon` took:
/// worked out before anything moves, so a failed one costs no moves.
struct Plan {
    value_start: usize,
    value_end: usize,
    important: bool,
    unicode_range: bool,
    span: Span,
}

/// The plan for `values` as a declaration, or `None` if they aren't one.
fn plan(values: &[ComponentValue<'_>]) -> Option<Plan> {
    let Some(ComponentValue::Token(Token::Ident(name), first)) = values.first() else {
        return None;
    };
    let mut colon = 1;
    while values.get(colon).is_some_and(ComponentValue::is_whitespace) {
        colon += 1;
    }
    let colon_end = match values.get(colon) {
        Some(ComponentValue::Token(Token::Colon, span)) => span.end,
        _ => return None,
    };
    let after = colon + 1;
    let v = value_plan(name, &values[after..])?;
    Some(Plan {
        value_start: after + v.start,
        value_end: after + v.end,
        important: v.important,
        unicode_range: name.eq_ignore_ascii_case("unicode-range"),
        span: Span::new(first.start, v.last_end.unwrap_or(colon_end)),
    })
}

/// Where a declaration's value is in what follows its colon.
pub(crate) struct ValuePlan {
    pub(crate) start: usize,
    pub(crate) end: usize,
    pub(crate) important: bool,
    /// The end of the last thing that isn't whitespace, `!important`
    /// included: where the declaration's span ends.
    pub(crate) last_end: Option<usize>,
}

/// The plan for `values`, all that follows a declaration's colon, or
/// `None` if a `{}` block where one isn't allowed makes it no declaration.
pub(crate) fn value_plan(name: &str, values: &[ComponentValue<'_>]) -> Option<ValuePlan> {
    let mut start = 0;
    while values.get(start).is_some_and(ComponentValue::is_whitespace) {
        start += 1;
    }
    // The last two non-whitespace values: `!` and `important`?
    let mut solid = (start..values.len())
        .rev()
        .filter(|&i| !values[i].is_whitespace());
    let last = solid.next();
    let before = solid.next();
    let important = match (before, last) {
        (Some(bang), Some(word))
            if values[bang].is(&Token::Delim('!'))
                && matches!(&values[word], ComponentValue::Token(Token::Ident(w), _)
                    if w.eq_ignore_ascii_case("important")) =>
        {
            Some(bang)
        }
        _ => None,
    };
    let mut end = important.unwrap_or(values.len());
    while end > start && values[end - 1].is_whitespace() {
        end -= 1;
    }
    if !name.starts_with("--") {
        // A `{}` block is allowed only as the whole of a property's value.
        let value = &values[start..end];
        if value.len() > 1 && value.iter().any(ComponentValue::is_curly) {
            return None;
        }
    }
    Some(ValuePlan {
        start,
        end,
        important: important.is_some(),
        last_end: last.map(|i| values[i].span().end),
    })
}

/// Builds the planned declaration from its name and value.
fn build<'a>(
    name: ComponentValue<'a>,
    mut value: Vec<ComponentValue<'a>>,
    plan: &Plan,
    src: &'a str,
) -> Declaration<'a> {
    let ComponentValue::Token(Token::Ident(name), _) = name else {
        unreachable!("a plan starts with an ident")
    };
    if plan.unicode_range && !value.is_empty() {
        // The one place the spec retokenizes: `U+0-7F` is a range here.
        let from = value[0].span().start;
        let to = value[value.len() - 1].span().end;
        value = Values::new(unicode_range_tokens(src, from, to)).collect();
    }
    Declaration {
        name,
        value,
        important: plan.important,
        span: plan.span,
    }
}

/// Builds a declaration from what `up_to_semicolon` took, or hands it back.
fn declaration_from<'a>(
    mut values: Vec<ComponentValue<'a>>,
    src: &'a str,
) -> Result<Declaration<'a>, Vec<ComponentValue<'a>>> {
    let Some(plan) = plan(&values) else {
        return Err(values);
    };
    let blank = ComponentValue::Token(Token::Whitespace, Span::default());
    let name = std::mem::replace(&mut values[0], blank);
    // The value moves down in place: no new list.
    values.truncate(plan.value_end);
    values.drain(..plan.value_start);
    Ok(build(name, values, &plan, src))
}

/// A tokenizer over `src[from..to]` making unicode-range tokens, with
/// spans that still point into `src`.
pub(crate) fn unicode_range_tokens(src: &str, from: usize, to: usize) -> Tokenizer<'_> {
    let mut t = Tokenizer::with_unicode_ranges(&src[..to]);
    t.reset(from);
    t
}

/// Parse a stylesheet: its rules, with `<!--` and `-->` skipped between
/// them as the HTML `<style>` element's history needs.
///
/// ```
/// let sheet = parsec_css::parse_stylesheet("a { color: red } @media print { b {} }");
/// assert_eq!(sheet.rules.len(), 2);
/// assert!(sheet.errors.is_empty());
/// ```
pub fn parse_stylesheet(css: &str) -> Stylesheet<'_> {
    let mut p = text(css);
    let rules = p.rules(true);
    Stylesheet {
        rules,
        errors: p.errors,
    }
}

/// Parse a list of rules as the 2021 Candidate Recommendation did, where
/// `<!--` and `-->` are ordinary tokens. The current draft's "parse a
/// stylesheet's contents" is [`parse_stylesheet`].
pub fn parse_rule_list(css: &str) -> Stylesheet<'_> {
    let mut p = text(css);
    let rules = p.rules(false);
    Stylesheet {
        rules,
        errors: p.errors,
    }
}

/// Parse a block's contents: declarations, at-rules and nested style rules,
/// in order, the way a style rule's `{}` block holds them since CSS
/// Nesting. [`SimpleBlock::contents`] does the same for a parsed block.
///
/// ```
/// use parsec_css::Item;
///
/// let block = parsec_css::parse_block_contents("color: red; &:hover { color: blue }");
/// assert!(matches!(&block.items[0], Item::Declaration(d) if d.name == "color"));
/// assert!(matches!(&block.items[1], Item::Rule(_)));
/// ```
pub fn parse_block_contents(css: &str) -> BlockContents<'_> {
    let mut p = text(css);
    let items = p.block_contents();
    BlockContents {
        items,
        errors: p.errors,
    }
}

/// Parse a list of declarations, as for a `style` attribute. The spec has
/// at-rules here parsed and thrown away; they're kept, as
/// [`Item::Rule`]s, for you to throw away. Nothing else becomes a rule.
pub fn parse_declaration_list(css: &str) -> BlockContents<'_> {
    let mut p = text(css);
    let items = p.declaration_list();
    BlockContents {
        items,
        errors: p.errors,
    }
}

/// Parse a rule: exactly one, with only whitespace around it.
pub fn parse_rule(css: &str) -> Result<Rule<'_>, ParseError> {
    let mut p = text(css);
    p.start()?;
    let rule = if matches!(
        p.peek(),
        Some(ComponentValue::Token(Token::AtKeyword(_), _))
    ) {
        Rule::At(p.at_rule(false))
    } else {
        match p.qualified_rule(false, false) {
            Some(rule) => Rule::Qualified(rule),
            None => return Err(p.errors.pop().unwrap()),
        }
    };
    p.end(rule)
}

/// Parse a declaration, as `@supports (…)` does: exactly one.
pub fn parse_declaration(css: &str) -> Result<Declaration<'_>, ParseError> {
    let mut p = text(css);
    p.start()?;
    match p.declaration(false) {
        Ok(decl) => p.end(decl),
        Err(values) => {
            let start = values.first().map_or(0, |v| v.span().start);
            Err(ParseError {
                kind: ErrorKind::InvalidDeclaration,
                span: Span::new(start, css.len()),
            })
        }
    }
}

/// Parse a component value: exactly one, with only whitespace around it.
pub fn parse_component_value(css: &str) -> Result<ComponentValue<'_>, ParseError> {
    let mut p = text(css);
    p.start()?;
    let value = p.next().unwrap();
    p.end(value)
}

/// Parse a list of component values: the whole input, as a selector or a
/// media query list is read.
pub fn parse_component_values(css: &str) -> Vec<ComponentValue<'_>> {
    Values::new(Tokenizer::new(css)).collect()
}

/// Parse a comma-separated list of component values, split at the commas
/// outside any block or function.
pub fn parse_comma_separated(css: &str) -> Vec<Vec<ComponentValue<'_>>> {
    let mut values = Values::new(Tokenizer::new(css)).peekable();
    let mut groups = Vec::new();
    while values.peek().is_some() {
        let mut group = Vec::new();
        for v in values.by_ref() {
            if v.is(&Token::Comma) {
                break;
            }
            group.push(v);
        }
        groups.push(group);
    }
    groups
}

/// The value of a `unicode-range` descriptor, tokenized with unicode-range
/// tokens: the one place CSS makes them.
pub fn parse_unicode_range_value(css: &str) -> Vec<ComponentValue<'_>> {
    Values::new(Tokenizer::with_unicode_ranges(css)).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deep_nesting_parses_and_drops() {
        let css = "(".repeat(1_000_000);
        let values = parse_component_values(&css);
        assert_eq!(values.len(), 1);
        drop(values);
        let css = format!("a{{b:{}}}", "[".repeat(200_000));
        let sheet = parse_stylesheet(&css);
        let Rule::Qualified(rule) = sheet.rules.into_iter().next().unwrap() else {
            panic!()
        };
        assert_eq!(rule.block.contents(&css).items.len(), 1);
    }

    #[test]
    fn spans_cover_rules_and_declarations() {
        let css = "a { color: red !important; } @x y;";
        let sheet = parse_stylesheet(css);
        assert_eq!(
            sheet.rules[0].span().text(css),
            "a { color: red !important; }"
        );
        assert_eq!(
            sheet.rules[1].span().text(css),
            "@x y;".trim_end_matches(';')
        );
        let Rule::Qualified(rule) = sheet.rules.into_iter().next().unwrap() else {
            panic!()
        };
        let Item::Declaration(d) = &rule.block.contents(css).items[0] else {
            panic!()
        };
        assert_eq!(d.span.text(css), "color: red !important");
        assert_eq!(d.original_text(css), "red");
        assert!(d.important);
    }

    #[test]
    fn block_contents_parsed_later_match_parsing_at_once() {
        let css = "color: red; &:hover { b: c } --x: {a} b; d:e{f}; @media x { g: h }";
        let at_once = parse_block_contents(css);
        let wrapped = format!("{{{css}}}");
        let Ok(ComponentValue::Block(block)) = parse_component_value(&wrapped) else {
            panic!()
        };
        let later = block.contents(&wrapped);
        assert_eq!(at_once.items.len(), later.items.len());
        assert_eq!(at_once.errors.len(), later.errors.len());
    }

    #[test]
    fn unicode_range_descriptor_gets_range_tokens() {
        let d = parse_declaration("unicode-range: U+0-7F, u+4??").unwrap();
        let ranges: Vec<_> = d
            .value
            .iter()
            .filter_map(|v| match v.token() {
                Some(Token::UnicodeRange { start, end }) => Some((*start, *end)),
                _ => None,
            })
            .collect();
        assert_eq!(ranges, [(0, 0x7f), (0x400, 0x4ff)]);
    }
}