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
use super::component_value_list::{
    IsKnownComponentValueList, IsKnownComponentValueListWithConstEmpty, KnownComponentValueList,
};

/// A list of [`ComponentValue`] with the following constraints:
/// - The list doesn't start or end with whitespace.
/// - In the list, no whitespace values are adjacent.
/// - The list doesn't end with `!` `important`.
pub struct KnownDeclarationValueList<'a, L: IsKnownComponentValueList<'a>> {
    inner: KnownComponentValueList<'a, L>,
}

impl<'a, L: IsKnownComponentValueList<'a>> Copy for KnownDeclarationValueList<'a, L> {}
impl<'a, L: IsKnownComponentValueList<'a>> Clone for KnownDeclarationValueList<'a, L> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'a, L: IsKnownComponentValueList<'a>> std::fmt::Debug for KnownDeclarationValueList<'a, L> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("KnownDeclarationValueList")
            .field(&self.inner)
            .finish()
    }
}

impl<'a, L: IsKnownComponentValueList<'a>> KnownDeclarationValueList<'a, L> {
    pub const fn full(&self) -> crate::token::stream::CopyableTokenStream<'a> {
        self.inner.full()
    }

    pub const fn full_as_str(&self) -> &'a str {
        self.inner.full_as_str()
    }

    pub(crate) const fn as_known_component_value_list(&self) -> &KnownComponentValueList<'a, L> {
        &self.inner
    }
}

impl<'a, L: IsKnownComponentValueListWithConstEmpty<'a>> KnownDeclarationValueList<'a, L> {
    pub(crate) const EMPTY: Self = {
        Self {
            inner: KnownComponentValueList::EMPTY,
        }
    };
}

impl<'a, L: IsKnownComponentValueList<'a>> KnownDeclarationValueList<'a, L> {
    pub(crate) const fn start_builder() -> builder::KnownDeclarationValueListBuilder<'a, L> {
        builder::KnownDeclarationValueListBuilder::new()
    }
}

pub(crate) mod builder {
    use crate::{
        collections::{
            array_vec::ArrayVec,
            component_value_list::{
                builder::KnownComponentValueListBuilder, IsKnownComponentValueList,
                IsKnownComponentValueListWithConstEmpty, KnownComponentValueList,
            },
            known::IsKnownCollection,
            lead_vec::LeadVec,
            HasConstDummyValue,
        },
        parse::{
            component_value::{ComponentValue, TokenAndRemaining},
            declaration::Important,
        },
        token::{
            stream::{CopyableTokenStream, TokenStream},
            tokens::{Token, WhitespaceToken},
        },
    };

    use super::KnownDeclarationValueList;

    /// A copyable version of [`TokenAndRemaining<ComponentValue>`]
    #[derive(Clone, Copy)]
    struct ValueAndRemaining<'a, V = ComponentValue<'a>> {
        cv: V,
        remaining: CopyableTokenStream<'a>,
        // full == cv + remaining
        full: CopyableTokenStream<'a>,
    }

    impl<'a> ValueAndRemaining<'a> {
        const fn try_into_whitespace(self) -> Option<ValueAndRemaining<'a, WhitespaceToken<'a>>> {
            if let Some(whitespace) = self.cv.as_whitespace() {
                Some(ValueAndRemaining {
                    cv: *whitespace,
                    remaining: self.remaining,
                    full: self.full,
                })
            } else {
                None
            }
        }
    }

    impl<'a> ValueAndRemaining<'a, WhitespaceToken<'a>> {
        const fn into_whitespace_and_remaining(self) -> WhitespaceAndRemaining<'a> {
            WhitespaceAndRemaining {
                whitespace: self.cv,
                whitespace_and_remaining: self.full,
            }
        }
    }

    impl<'a> HasConstDummyValue for ValueAndRemaining<'a> {
        const DUMMY_VALUE: Self = ValueAndRemaining {
            cv: ComponentValue::PreservedTokens(Token::DUMMY_VALUE),
            remaining: CopyableTokenStream::EMPTY,
            full: TokenStream::new(" ").to_copyable(),
        };
    }

    #[derive(Clone, Copy)]
    struct WhitespaceAndRemaining<'a> {
        whitespace: WhitespaceToken<'a>,
        whitespace_and_remaining: CopyableTokenStream<'a>,
    }

    #[derive(Clone, Copy)]
    struct WhitespaceAndValueAndRemaining<'a> {
        whitespace: Option<WhitespaceAndRemaining<'a>>,
        value: ValueAndRemaining<'a>,
    }

    impl<'a> WhitespaceAndValueAndRemaining<'a> {
        const fn push_to<
            L: IsKnownComponentValueListWithConstEmpty<'a>,
            const ARRAY_VEC_CAP: usize,
            const LEAD_VEC_CAP: usize,
        >(
            self,
            res: KnownComponentValueListBuilder<'a, L>,
        ) -> KnownComponentValueListBuilder<'a, L>
        where
            L::Collection: IsKnownCollection<
                ComponentValue<'a>,
                ArrayVecType = ArrayVec<ComponentValue<'a>, ARRAY_VEC_CAP>,
                LeadVecType = LeadVec<ComponentValue<'a>, LEAD_VEC_CAP>,
            >,
        {
            let WhitespaceAndValueAndRemaining {
                whitespace,
                value: value_that_is_not_last_2,
            } = self;

            if let Some(w) = whitespace {
                res.with_push(
                    value_of_whitespace(w.whitespace),
                    w.whitespace_and_remaining,
                )
            } else {
                res
            }
            .with_push(value_that_is_not_last_2.cv, value_that_is_not_last_2.full)
        }
    }

    impl<'a> HasConstDummyValue for WhitespaceAndValueAndRemaining<'a> {
        const DUMMY_VALUE: Self = Self {
            whitespace: None,
            value: ValueAndRemaining::DUMMY_VALUE,
        };
    }

    #[allow(clippy::large_enum_variant)]
    enum ValueList<'a> {
        Empty,
        NotEmpty {
            first: ValueAndRemaining<'a>,
            last_3: ArrayVec<WhitespaceAndValueAndRemaining<'a>, 3>,
            last_whitespace: Option<ValueAndRemaining<'a, WhitespaceToken<'a>>>,
            real_len: usize,
        },
    }

    enum ValueListPop2<'a> {
        Empty,
        One(ValueAndRemaining<'a>),
        More {
            // first: ValueAndRemaining<'a>,
            last: ValueAndRemaining<'a>,
            // real_len: usize,
        },
    }

    impl<'a> ValueListPop2<'a> {
        const fn last(&self) -> Option<&ValueAndRemaining<'a>> {
            match self {
                ValueListPop2::Empty => None,
                ValueListPop2::One(var) => Some(var),
                ValueListPop2::More {
                    // first: _,
                    last,
                    // real_len: _,
                } => Some(last),
            }
        }
    }

    const fn some_to_1<T>(v: &Option<T>) -> usize {
        if v.is_some() {
            1
        } else {
            0
        }
    }

    const fn count_some<T>(vs: &[&Option<T>]) -> usize {
        let mut res = 0;
        let mut i = 0;
        while i < vs.len() {
            if vs[i].is_some() {
                res += 1;
            }
            i += 1;
        }

        res
    }

    impl<'a> ValueList<'a> {
        /// ret.0 means the values that are not one of the last two non-whitespace.
        const fn with_push(
            self,
            v: ValueAndRemaining<'a>,
        ) -> (Option<WhitespaceAndValueAndRemaining<'a>>, Self) {
            match self {
                ValueList::Empty => {
                    assert!(
                        !v.cv.is_whitespace(),
                        "first component value in declaration value list can't be whitespace"
                    );
                    (
                        None,
                        Self::NotEmpty {
                            first: v,
                            last_3: ArrayVec::EMPTY,
                            last_whitespace: None,
                            real_len: 1,
                        },
                    )
                }
                ValueList::NotEmpty {
                    first,
                    last_3,
                    last_whitespace,
                    real_len,
                } => {
                    let v = if let Some(whitespace) = last_whitespace {
                        assert!(
                            !v.cv.is_whitespace(),
                            "unexpected adjacent whitespace tokens"
                        );
                        WhitespaceAndValueAndRemaining {
                            whitespace: Some(whitespace.into_whitespace_and_remaining()),
                            value: v,
                        }
                    } else {
                        //
                        if let Some(whitespace) = v.try_into_whitespace() {
                            return (
                                None,
                                Self::NotEmpty {
                                    first,
                                    last_3,
                                    last_whitespace: Some(whitespace),
                                    real_len: real_len + 1,
                                },
                            );
                        } else {
                            WhitespaceAndValueAndRemaining {
                                whitespace: None,
                                value: v,
                            }
                        }
                    };

                    let (popped, last_3) = last_3.with_force_push(v);

                    let returned_val = match last_3.len() {
                        2 => {
                            debug_assert!(popped.is_none());
                            // now we can assure that `first` is not one of the last two.
                            Some(WhitespaceAndValueAndRemaining {
                                whitespace: None,
                                value: first,
                            })
                        }
                        3 => {
                            debug_assert!(popped.is_none());
                            // now we can assure that `last_3[0]` is not one of the last two.
                            Some(match last_3.first() {
                                Some(v) => *v,
                                None => unreachable!(),
                            })
                        }
                        _ => popped,
                    };

                    (
                        returned_val,
                        Self::NotEmpty {
                            first,
                            last_3,
                            // we have processed the case where last_whitespace is_some
                            last_whitespace: None,
                            real_len: real_len + 1,
                        },
                    )
                }
            }
        }

        #[allow(clippy::result_large_err)]
        const fn pop_last_2_important(self) -> Result<(Important<'a>, ValueListPop2<'a>), Self> {
            match self {
                ValueList::Empty => Err(self),
                ValueList::NotEmpty {
                    first,
                    ref last_3,
                    last_whitespace,
                    real_len,
                } => match last_3.as_slice() {
                    [WhitespaceAndValueAndRemaining {
                        whitespace: aw,
                        value: a,
                    }, WhitespaceAndValueAndRemaining {
                        whitespace: bw,
                        value: b,
                    }, WhitespaceAndValueAndRemaining {
                        whitespace: cw,
                        value: c,
                    }] => {
                        debug_assert!(
                            real_len >= 4 + count_some(&[aw, bw, cw]) + some_to_1(&last_whitespace)
                        );
                        if let Some((bang, important)) = Important::is_bang_important(b.cv, c.cv) {
                            Ok((
                                Important {
                                    full: b.full.before(c.remaining),
                                    bang,
                                    important,
                                },
                                ValueListPop2::More {
                                    // first,
                                    last: *a,
                                    // real_len: real_len - 2,
                                },
                            ))
                        } else {
                            Err(self)
                        }
                    }
                    [WhitespaceAndValueAndRemaining {
                        whitespace: bw,
                        value: b,
                    }, WhitespaceAndValueAndRemaining {
                        whitespace: cw,
                        value: c,
                    }] => {
                        debug_assert!(
                            real_len == 3 + count_some(&[bw, cw]) + some_to_1(&last_whitespace)
                        );

                        if let Some((bang, important)) = Important::is_bang_important(b.cv, c.cv) {
                            Ok((
                                Important {
                                    full: b.full.before(c.remaining),
                                    bang,
                                    important,
                                },
                                ValueListPop2::One(first),
                            ))
                        } else {
                            Err(self)
                        }
                    }
                    [WhitespaceAndValueAndRemaining {
                        whitespace: cw,
                        value: c,
                    }] => {
                        debug_assert!(real_len == 2 + some_to_1(cw) + some_to_1(&last_whitespace));
                        let b = first;
                        if let Some((bang, important)) = Important::is_bang_important(b.cv, c.cv) {
                            Ok((
                                Important {
                                    full: b.full.before(c.remaining),
                                    bang,
                                    important,
                                },
                                ValueListPop2::Empty,
                            ))
                        } else {
                            Err(self)
                        }
                    }
                    [] => {
                        debug_assert!(real_len == 1 + some_to_1(&last_whitespace));

                        Err(self)
                    }
                    _ => unreachable!(),
                },
            }
        }

        const fn last_non_whitespace(&self) -> Option<ValueAndRemaining<'a>> {
            match self {
                ValueList::Empty => None,
                ValueList::NotEmpty {
                    first,
                    last_3,
                    last_whitespace: _,
                    real_len: _,
                } => Some(match last_3.as_slice().last() {
                    Some(v) => v.value,
                    None => *first,
                }),
            }
        }

        const fn value_including_important_without_trailing_whitespace(
            &self,
        ) -> Option<CopyableTokenStream<'a>> {
            match self {
                ValueList::Empty => None,
                ValueList::NotEmpty {
                    first,
                    last_3,
                    last_whitespace: _,
                    real_len: _,
                } => Some(match last_3.as_slice().last() {
                    Some(last) => first.full.before(last.value.remaining),
                    None => first.full.before(first.remaining),
                }),
            }
        }

        /// Last 2 non whitespace that haven't been returned.
        const fn last_2_non_whitespace_copied(
            &self,
        ) -> ArrayVec<WhitespaceAndValueAndRemaining<'a>, 2> {
            match self {
                ValueList::Empty => ArrayVec::EMPTY,
                ValueList::NotEmpty {
                    first,
                    last_3,
                    last_whitespace: _,
                    real_len: _,
                } => {
                    match last_3.as_slice() {
                        // the first has been returned by Self::with_push
                        [_, b, c] => ArrayVec::new_filled([*b, *c]),
                        [b, c] => ArrayVec::new_filled([*b, *c]),
                        [c] => ArrayVec::new_filled([
                            WhitespaceAndValueAndRemaining {
                                whitespace: None,
                                value: *first,
                            },
                            *c,
                        ]),
                        [] => ArrayVec::EMPTY.with_push(WhitespaceAndValueAndRemaining {
                            whitespace: None,
                            value: *first,
                        }),
                        _ => unreachable!(),
                    }
                }
            }
        }
    }

    pub(crate) struct KnownDeclarationValueListBuilder<'a, L: IsKnownComponentValueList<'a>> {
        // the full list is list_builder + value_list.last_3
        list_builder: KnownComponentValueListBuilder<'a, L>,
        value_list: ValueList<'a>,
    }

    impl<'a, L: IsKnownComponentValueList<'a>> KnownDeclarationValueListBuilder<'a, L> {
        pub(super) const fn new() -> Self {
            Self {
                list_builder: KnownComponentValueList::start_builder(),
                value_list: ValueList::Empty,
            }
        }
    }

    const fn value_of_whitespace(ws: WhitespaceToken) -> ComponentValue {
        ComponentValue::PreservedTokens(Token::Whitespace(ws))
    }

    impl<
            'a,
            L: IsKnownComponentValueListWithConstEmpty<'a>,
            const ARRAY_VEC_CAP: usize,
            const LEAD_VEC_CAP: usize,
        > KnownDeclarationValueListBuilder<'a, L>
    where
        L::Collection: IsKnownCollection<
            ComponentValue<'a>,
            ArrayVecType = ArrayVec<ComponentValue<'a>, ARRAY_VEC_CAP>,
            LeadVecType = LeadVec<ComponentValue<'a>, LEAD_VEC_CAP>,
        >,
    {
        pub(crate) const fn with_push(
            self,
            tar: TokenAndRemaining<'a, ComponentValue<'a>>,
        ) -> Self {
            let TokenAndRemaining {
                token,
                remaining,
                full,
            } = tar;
            let full = full.to_copyable();

            let (value_that_is_not_last_2, value_list) =
                self.value_list.with_push(ValueAndRemaining {
                    cv: token,
                    remaining: remaining.to_copyable(),
                    full,
                });

            let list_builder = if let Some(wv) = value_that_is_not_last_2 {
                wv.push_to(self.list_builder)
            } else {
                self.list_builder
            };

            Self {
                list_builder,
                value_list,
            }
        }

        pub(crate) const fn build(
            self,
            after_colon: CopyableTokenStream<'a>,
        ) -> BuildOutput<'a, L> {
            // after values and !important
            let after_value_and_important = match self.value_list.last_non_whitespace() {
                Some(v) => v.remaining,
                None => after_colon,
            };

            // after values and !important
            // let after_value_and_important_and_trailing_whitespace =
            //     match self.value_list.last_maybe_whitespace() {
            //         Some(v) => v.remaining,
            //         None => after_colon,
            //     };

            let value_and_important = match self
                .value_list
                .value_including_important_without_trailing_whitespace()
            {
                Some(v) => v,
                None => after_colon.before(after_colon),
            };

            let important;
            let list;
            match self.value_list.pop_last_2_important() {
                Ok((i, values)) => {
                    // values before `!important` has already been pushed into list_builder.
                    important = Some(i);

                    list = {
                        let remaining = match values.last() {
                            Some(last) => last.remaining,
                            None => after_colon,
                        };
                        self.list_builder.build(remaining)
                    };
                }
                Err(values) => {
                    // last 2 values hasn't been pushed into list_builder.
                    important = None;

                    list = {
                        let mut list_builder = self.list_builder;

                        {
                            let last_2 = values.last_2_non_whitespace_copied();
                            let last_2 = last_2.as_slice();

                            let mut i = 0;
                            while i < last_2.len() {
                                list_builder = last_2[i].push_to(list_builder);

                                i += 1;
                            }
                        }

                        list_builder.build(match values.last_non_whitespace() {
                            Some(last) => last.remaining,
                            None => after_colon,
                        })
                    };
                }
            }

            BuildOutput {
                value: KnownDeclarationValueList { inner: list },
                important,
                value_and_important,
                after_value_and_important,
                // after_value_and_important_and_trailing_whitespace,
            }
        }
    }

    pub(crate) struct BuildOutput<'a, L: IsKnownComponentValueListWithConstEmpty<'a>> {
        pub(crate) value: KnownDeclarationValueList<'a, L>,
        pub(crate) important: Option<Important<'a>>,
        pub(crate) value_and_important: CopyableTokenStream<'a>,
        pub(crate) after_value_and_important: CopyableTokenStream<'a>,
        // pub(crate) after_value_and_important_and_trailing_whitespace: CopyableTokenStream<'a>,
    }
}