tsv_css 0.8.0

CSS formatter and parser compatible with Svelte's parseCss — part of tsv
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
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
// Splitting a value run into its operator tokens and operands.
//
// A leaf reaching [`super::parse_single_value`] is one whitespace/comma-delimited run, and
// for most of a stylesheet that run is one token (`red`, `1px`, `var(--a)`). Where it is
// not — `12px/1.5`, `1.5*2.5`, `f(a:1.5)`, `(1.5)(2.5)` — postcss-values-parser, the
// parser behind prettier's CSS printer, reads it as several nodes, and the printer then
// decides each gap on the two nodes' kinds. tsv had no such token, so the run stayed one
// opaque leaf: the number after the operator never normalized, the colon never spaced, and
// the second group never separated.
//
// This module is the tokenizer half of that model. The separator half — which gap is a
// space and which is glue — is `crate::printer::values`'s `ValueOperator` rule, which
// reads the members this produces.
//
// A run that is **one operator token** is a member too, and that is not a special case
// but the same rule read at a whitespace boundary: `1.5 / 2.5`, `1.5 * - 2.5` and
// `--x: a : b` each put the operator in an element of its own, and it reaches the list as
// a `CssValue::Operator` exactly as a glued one does. Only an operand run of one token
// answers "no split".
//
// ⚠️ **The boundary set is not one class of byte.** `*` and `+` end a run wherever they
// occur; `/` and `-` end a **number** (or a closed group / function) and are ordinary
// content inside a word. That asymmetry is postcss's, and it is observable: `1.5/2.5` is
// three nodes and normalizes on both sides, where `a/1.5` is a single word whose `1.5`
// prettier leaves exactly as written.
//
// Where an OPERAND is expected — the run's head, or straight after another operator — the
// question is a third one, and the two signs ask it of **different grammars**
// ([`operator_at_operand_position`]). The `+` asks postcss's: does the next TOKEN come
// back as a word, so that the sign is the word's own ([`starts_word`])? The `-` asks
// css-syntax-3's, of itself: would the `-` and the byte after it start an identifier
// (§4.3.9) or a number (§4.3.10) (the lexer's [`hyphen_starts_own_token`])? The two agree
// on every byte that opens an ident or a number, and part at the bytes postcss's word scan
// runs through but no token production does. That is the RULE; the fifteen bytes it picks
// out of printable ASCII were measured, and the "Hyphen word extent at an operand
// position" entry in `docs/conformance_prettier_css.md` enumerates them.
//
// Three more members are not operators but end every token they touch, each with its
// own extent, each measured rather than reasoned from a class:
//
// - an **`@`-word** (`@a`, and the empty `@`): postcss's `atword` token, whose end set is
//   its own — whitespace, a quote, `,`, `(`, `)`, `{`, `;`, `/` — so `*`, `+`, `-`, `:`,
//   `#`, `.`, a digit and `[` are its CONTENT (`@a*1.50`, `f(@a:1.50)` come back verbatim
//   where `@a/1.50` is an `@`-word, an operator and a number), and a second `@` starts a
//   second one (`@a@b` → `@a @b`);
// - a **brace**, `{` or `}`, its own one-byte token — reachable only in a custom
//   property's value, where css-syntax-3 keeps the original text whatever it holds (a
//   non-custom property refuses a top-level `{}`-block beside any other value; both
//   parsers reject `b: a{1.5}c`). The word between the braces is a member of its own and
//   normalizes (`a{1.50}c(2.50)` → `a{1.5}c(2.5)`, and its own `:` splits there like any
//   other), which is why the brace is not word content the way `[` is (`a[1.50]c` keeps
//   its number: prettier's value *parser* does not descend into a `[…]` block, though its
//   word tokenizer still ends a word at a `*` or `+` inside the brackets and its printer
//   then spaces the pieces — a cataloged divergence,
//   `css/values/operators/bracket_block_operator_prettier_divergence`);
// - a **welded `+`**: postcss's `operator()` returns `this.word()` for a `+` that opens
//   the value or follows an operator when the next token is a word, so the sign is the
//   word's own — `+a(2.50)` is the function `+a` and normalizes to `+a(2.5)`, `1.50 / +a`
//   → `1.5 / +a`. Anywhere else (after a member, at the head of a function's arguments
//   or of a group — the `(` is a node there — and after a comma) the `+` is an operator
//   and prints spaced (`f(+a(2.50))` → `f(+ a(2.5))`). Before a group, a string, an
//   `@`-word or another operator nothing welds (`+(2.5)`, `+'x'`, `+ @a`, `+ +a`), and
//   nothing welds after a `:` either — a `colon` node is no operator for a sign to bind to
//   (`--x: a:+b` → `a: + b`).

use bumpalo::Bump;
use bumpalo::collections::Vec as BumpVec;

use super::scan::{comment_end, is_comment_start, matching_close_paren};
use crate::escapes::escape_len;
use crate::lexer::{hyphen_starts_own_token, string_end};
use crate::number::{number_part_len, starts_number};

/// What the token just emitted was, which is what decides whether a following `/` or `-`
/// opens an operator or is content.
#[derive(Clone, Copy, PartialEq, Eq)]
enum TokenKind {
    /// A `<number>` / `<dimension>` / `<percentage>` — `1.5`, `12px`, `100%`.
    Number,
    /// An ident-ish run — `red`, `a/1.5`, `#FFF-5`. It **absorbs** `/` and `-`.
    Word,
    /// A run that ends the member outright: a function or parenthesized group closed by
    /// its `)`, or a `<unicode-range-token>`. Like `Number` it releases `/` and `-`, and
    /// unlike either what follows it starts a new member whatever that is
    /// (`(1.5)(2.5)`, `f(1.5)g(2.5)`, `(1.5)aa`, `U+26-1.50` → `U+26-1 0.5`).
    Closed,
}

/// What stands to the left of the position being read — the whole of what the operator
/// question turns on, since a byte's reading is its position's.
///
/// [`Preceding::Head`] and [`Preceding::Operator`] share one arm of that question (an
/// operand is what follows either), but they are not one state: at the head a `+` welds
/// only where the caller says so ([`split_value_run`]'s `head_welds`), and after an
/// operator it welds unless that operator was the `:` — which is no operator for a sign
/// to bind to, the same exclusion the element-level rule makes
/// (`super::parser::ValueParser::member_is_operator`). Read as one state the two rules
/// disagreed, and `--x: a:+b` formatted to `a: +b` and then to `a: + b`.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Preceding {
    /// Nothing at all: the run's first byte.
    Head,
    /// An operator member, and whether that operator was the `:`.
    Operator { colon: bool },
    /// An operand token of this kind.
    Token(TokenKind),
}

/// A byte that can open an operator after a [`TokenKind::Number`] or
/// [`TokenKind::Closed`] token — every operator byte there is. The `:` is one only where
/// a `:` can stand as an operator at all; see [`split_value_run`]'s `colon_is_operator`.
const fn releases_operator(b: u8, colon_is_operator: bool) -> bool {
    matches!(b, b'*' | b'+' | b'/' | b'-') || (b == b':' && colon_is_operator)
}

/// A byte that ends a [`TokenKind::Word`]. The strict subset of
/// [`releases_operator`] that is never an ident code point *and* never glues into a
/// postcss word: `/` and `-` are missing on purpose.
const fn ends_word(b: u8, colon_is_operator: bool) -> bool {
    matches!(b, b'*' | b'+') || (b == b':' && colon_is_operator)
}

/// A byte that is an operator at a run's START, or straight after another operator.
///
/// `-` is missing on purpose: there it can be the head of the ident or the number after
/// it (`-webkit-box`, `-1.5`), so whether it is an operator turns on what comes *after*
/// it, which this one-byte test cannot see. [`operator_at_operand_position`] answers the
/// `-` with [`hyphen_starts_own_token`]. `+` is in, since it can open neither an ident nor
/// a number of its own (a signed number is claimed by `number_part_len` ahead of this
/// test) — only WELD onto a word, which the same caller asks with [`starts_word`].
const fn opens_run(b: u8, colon_is_operator: bool) -> bool {
    matches!(b, b'*' | b'/' | b'+') || (b == b':' && colon_is_operator)
}

/// A byte that ends **every** token kind without being an operator: the `@` that opens an
/// `@`-word and the two braces, each a member of its own (see the module doc).
const fn ends_every_token(b: u8) -> bool {
    matches!(b, b'@' | b'{' | b'}')
}

/// A byte that ends an `@`-word — postcss's `atEnd` set (tokenizer `case 64`), which
/// unlike the word's end set DOES end at `/` and does NOT end at `*`, `+`, `-`, `:`, `[`
/// or `]`. Whitespace and `;` cannot occur in a run; a second `@` ends it because
/// `splitWord` splits a word at every `@`. `\` is in postcss's set but is stepped whole
/// here as an escape (lossless; prettier's own output there rides its unbalanced-value
/// fallback, so no cell pins it).
const fn ends_atword(b: u8) -> bool {
    matches!(
        b,
        b'{' | b'}' | b'(' | b')' | b'\'' | b'"' | b',' | b'/' | b'@'
    )
}

/// Does the byte after a `+` open a WORD? postcss's condition is "the next token is a
/// `word`": not a group, a string, an `@`-word, a brace, a comma, a colon, another
/// operator, or the run's end. Everything else — a letter, `#`, `.`, `\`, `[`, `!`, a
/// digit the number production did not claim — starts a word, and the `+` WELDS onto it
/// (postcss's `operator()` returning `this.word()`).
///
/// This is the `+`'s question alone. The `-`'s is [`hyphen_starts_own_token`], which asks
/// css-syntax-3 of the `-` itself rather than postcss of the token after it: read this way
/// a `-` before a `[…]` block or a `#` would be the word's first byte, and the same three
/// bytes then had two readings in one document (see [`operator_at_operand_position`]'s
/// ⚠️).
///
/// A `-` is the one byte the answer cannot be read off alone, which is why the byte
/// **after** it is a parameter: a single `-` opens an ident or signs a number and the `+`
/// stays an operator (`+-a` → `+ -a`, `+-1.5` → `+ -1.5`), but a **doubled** one opens the
/// custom-property-shaped word ([`is_content_pair`]'s first pair), and that word the `+`
/// welds onto exactly as it welds onto a letter — `+--a`, `+--`, `+----a` and `+-- @a` are
/// prettier's own output at every position a word welds at (`1.5 * +--a`, `+ +--a`) and at
/// none of the positions none does (`1.5 + --a`, `f(+ --a)`, `(+ --a)`).
const fn starts_word(b: Option<u8>, after: Option<u8>) -> bool {
    match b {
        None => false,
        Some(b'-') => matches!(after, Some(b'-')),
        Some(b) => !matches!(
            b,
            b'(' | b')' | b'\'' | b'"' | b'@' | b'{' | b'}' | b',' | b':' | b'*' | b'/' | b'+'
        ),
    }
}

/// A byte that could possibly begin a member boundary: any operator byte, plus the `(`
/// that opens a group (a group boundary splits with no operator at all) and the bytes
/// that end every token.
///
/// [`split_value_run`]'s refusal pass reads exactly this, so it must be a **superset** of
/// the three position-keyed tests above — a byte one of them releases but this one does
/// not would make the refusal skip a run that really splits. The assertion below is what
/// holds the four together rather than a comment claiming they agree.
const fn may_open_boundary(b: u8) -> bool {
    releases_operator(b, true) || b == b'(' || ends_every_token(b)
}

const _: () = {
    let mut b = 0u8;
    while b < 128 {
        assert!(
            !(releases_operator(b, true)
                || ends_word(b, true)
                || opens_run(b, true)
                || ends_every_token(b))
                || may_open_boundary(b),
            "a boundary byte the refusal pass cannot see"
        );
        b += 1;
    }
};

/// Is the byte at `i` the first of a **doubled** operator, which postcss reads as content
/// rather than as two operators?
///
/// Two pairs, each observable in what prettier leaves alone:
///
/// - `--` opens a custom-property-shaped word, never a subtraction — `1.5--2.5` is one
///   token and neither number normalizes;
/// - `//` takes the whole run with it — `1.50//2.50`, `aa//1.50` and `///1.50` all come
///   back exactly as authored, where a single `/` splits (`/1.50` → `/1.5`). Separate the
///   pair by a space and both sides normalize again (`1.50/ /2.50` → `1.5/ /2.5`), so it
///   is the adjacency that does it.
fn is_content_pair(bytes: &[u8], i: usize) -> bool {
    matches!(bytes[i], b'-' | b'/') && bytes.get(i + 1) == Some(&bytes[i])
}

/// The length of the `<unicode-range-token>` at `i`, or 0 when none starts there.
///
/// css-syntax-3 §"Check if three code points would start a unicode-range": `U`/`u`, then
/// `+`, then a hex digit or `?`. The `+` inside it is part of the token, so the run must
/// be taken whole here — read as an operator it would space the range apart
/// (`U+26` → `U + 26`).
fn unicode_range_len(bytes: &[u8], i: usize) -> usize {
    let is_range_digit = |b: u8| b.is_ascii_hexdigit() || b == b'?';
    if !bytes[i].eq_ignore_ascii_case(&b'u')
        || bytes.get(i + 1) != Some(&b'+')
        || !bytes.get(i + 2).copied().is_some_and(is_range_digit)
    {
        return 0;
    }
    let mut end = i + 2;
    while bytes.get(end).copied().is_some_and(is_range_digit) {
        end += 1;
    }
    // The range's second half (`U+0025-00FF`), whose `-` is the token's, not an operator.
    if bytes.get(end) == Some(&b'-')
        && bytes
            .get(end + 1)
            .copied()
            .is_some_and(|b| b.is_ascii_hexdigit())
    {
        end += 1;
        while bytes
            .get(end)
            .copied()
            .is_some_and(|b| b.is_ascii_hexdigit())
        {
            end += 1;
        }
    }
    end - i
}

/// One member of a split run: its half-open byte range, and whether it is an operator.
///
/// The flag is the splitter's own verdict, carried rather than re-derived. Read back off
/// the bytes instead, an **operand** that merely opens on an operator character (the `-a`
/// in `1.5*-a`, where the `-` opens the ident rather than a subtraction) would be minted
/// as an operator and take a glue rule that is not its own.
///
/// It is also what a **one-token** run is split on: the same verdict says whether that
/// run is the whitespace-delimited operator element (`1.5 / 2.5`) or an ordinary leaf,
/// so neither the printer's `ValueOperator` nor the weld test
/// (`super::parser::ValueParser::member_is_operator`) has to spell the bytes again.
pub(crate) struct ValueToken {
    pub(crate) start: usize,
    pub(crate) end: usize,
    pub(crate) is_operator: bool,
}

/// The members `run` splits into, or `None` when it is a single **operand** token —
/// the overwhelmingly common case, which allocates nothing and leaves the caller's
/// existing leaf classification untouched. A run that is a single token which IS an
/// operator still splits, into that one member: a whitespace-delimited `/`, `:` or `-`
/// element (`1.5 / 2.5`, `--x: a : b`) is an operator node to postcss like any other, and
/// the separator rule reads its kind off the node rather than off its bytes.
///
/// `colon_is_operator` says whether a `:` can stand as an operator here: inside a
/// function's argument list or a parenthesized group, and at the top level of a **custom
/// property's** value, whose text css-syntax-3 admits whatever it holds. On a plain
/// property a top-level `:` is not a value token at all (prettier's parser rejects
/// `a { b: c:d }` outright), so there is no oracle for splitting one and the run is left
/// as the author wrote it.
///
/// `head_welds` says whether a `+` opening the run welds onto the word after it (the
/// module doc's third member): true for the first run of a declaration's whole value and
/// for a run straight after an operator member, false at the head of a group's interior
/// and after a comma. A `+` after another operator *inside* the run welds regardless.
///
/// Escapes, strings, comments and parenthesized interiors are **opaque**: a `\/` never
/// splits (which is what keeps `inset: a\+b` on its cataloged divergence), a `/*` opens a
/// comment rather than a division, and an operator inside a function's arguments belongs
/// to that function's own run.
pub(crate) fn split_value_run<'arena>(
    run: &str,
    colon_is_operator: bool,
    head_welds: bool,
    arena: &'arena Bump,
) -> Option<BumpVec<'arena, ValueToken>> {
    let bytes = run.as_bytes();
    // A run holding no byte that could open a boundary is the common case, and one pass
    // that can only answer "no" is cheaper than the tokenizer's state machine. A run that
    // opens on a word asks a narrower set (see [`opens_on_word`]), which is what keeps a
    // hyphenated ident (`sans-serif`, `border-box`) out of the tokenizer.
    let may_split = match bytes {
        _ if opens_on_word(bytes) => any_byte_has(bytes, ENDS_WORD_RUN),
        // A `-` before a digit signs the number it opens (§4.3.10), so the run's first token
        // is that number and its boundaries are the ordinary set, read past the sign.
        [b'-', digit, rest @ ..] if digit.is_ascii_digit() => any_byte_has(rest, MAY_OPEN_BOUNDARY),
        _ => any_byte_has(bytes, MAY_OPEN_BOUNDARY),
    };
    if !may_split {
        debug_assert!(
            tokenize_run(run, colon_is_operator, head_welds, arena).is_none(),
            "the refusal pass declined a run that splits: {run:?}"
        );
        return None;
    }
    tokenize_run(run, colon_is_operator, head_welds, arena)
}

/// Does `bytes` open on a [`TokenKind::Word`] — an ASCII letter, `_` or `#`, a `-` before
/// a letter, or the doubled `-` of a custom-property name? At the run's head none of them
/// is an operator (a `--` is content, [`is_content_pair`]) and none starts a number or a
/// unicode range without a `+` after it, so the first token is a word, and a word ends
/// only at a byte [`ends_word_run`] names. A run holding none of those is that one word,
/// whatever `/` and `-` it carries.
fn opens_on_word(bytes: &[u8]) -> bool {
    match bytes {
        [b, ..] if b.is_ascii_alphabetic() || matches!(b, b'_' | b'#') => true,
        [b'-', b'-', ..] => true,
        [b'-', b, ..] => b.is_ascii_alphabetic(),
        _ => false,
    }
}

/// Every byte at which [`scan_token_body`] can end a [`TokenKind::Word`], the `:` read as
/// an operator: [`ends_word`], [`ends_every_token`], and the `(` that closes it into a
/// group (after which `/` and `-` release). The opaque constructs it steps over (escapes,
/// strings, comments, `[…]` blocks) only hide bytes from it, never add one.
const fn ends_word_run(b: u8) -> bool {
    ends_word(b, true) || b == b'(' || ends_every_token(b)
}

/// [`may_open_boundary`] and [`ends_word_run`] as one table, one bit each.
const BOUNDARY_BITS: [u8; 256] = {
    let mut table = [0u8; 256];
    let mut b = 0usize;
    while b < 256 {
        table[b] = (may_open_boundary(b as u8) as u8) * MAY_OPEN_BOUNDARY
            + (ends_word_run(b as u8) as u8) * ENDS_WORD_RUN;
        b += 1;
    }
    table
};
const MAY_OPEN_BOUNDARY: u8 = 1;
const ENDS_WORD_RUN: u8 = 2;

/// Does any byte of `bytes` carry `bit` in [`BOUNDARY_BITS`]? An OR folded over the run
/// eight bytes at a time, asked once per chunk, with the last few bytes folded on their
/// own. A value run is a handful of bytes, too short for a per-byte exit's branch or a
/// word loop's per-call splats to pay for themselves — but a fixed chunk of eight is a
/// loop the compiler unrolls into eight table loads with no counter between them, and
/// the one test behind it lets a run that does split leave at its first chunk.
#[inline]
fn any_byte_has(bytes: &[u8], bit: u8) -> bool {
    let fold = |chunk: &[u8]| {
        chunk
            .iter()
            .fold(0u8, |seen, &b| seen | BOUNDARY_BITS[b as usize])
    };
    let mut chunks = bytes.chunks_exact(8);
    for chunk in &mut chunks {
        if fold(chunk) & bit != 0 {
            return true;
        }
    }
    fold(chunks.remainder()) & bit != 0
}

/// The tokenizer behind [`split_value_run`], without its refusal pass.
fn tokenize_run<'arena>(
    run: &str,
    colon_is_operator: bool,
    head_welds: bool,
    arena: &'arena Bump,
) -> Option<BumpVec<'arena, ValueToken>> {
    let len = run.len();
    if len == 0 {
        return None;
    }
    let mut i = 0usize;
    // What stands to the left of `i` (see [`Preceding`]).
    let mut previous = Preceding::Head;

    // The first member alone settles the common case — a run that is one operand token —
    // before any `Vec` is minted. A one-token run splits only when that token is an
    // operator (the whitespace-delimited element above). The verdict is the splitter's own
    // — read back off the bytes, the `-a` in `1.5*-a` would be minted as an operator it is
    // not (see [`ValueToken::is_operator`]).
    let first = next_member(run, &mut i, &mut previous, colon_is_operator, head_welds);
    if i == len && !first.is_operator {
        return None;
    }
    // In the document's arena rather than the heap: the members are read once, straight
    // into the `List` the caller builds there, and a heap `Vec` paid a free for each.
    let mut tokens = BumpVec::with_capacity_in(4, arena);
    tokens.push(first);
    while i < len {
        tokens.push(next_member(
            run,
            &mut i,
            &mut previous,
            colon_is_operator,
            head_welds,
        ));
    }
    Some(tokens)
}

/// Read the member at `*i`, leaving `*i` one past it and `previous` naming it.
///
/// ⚠️ `inline(always)`, not `inline`, and the difference is measured: under a plain
/// `#[inline]` it outlines with `scan_token` folded into it, and the tokenizer's two call
/// sites each pay the call — about +2.8% cycles on the CSS parse-and-wire path with
/// instructions flat.
#[expect(clippy::inline_always)]
#[inline(always)]
fn next_member(
    run: &str,
    i: &mut usize,
    previous: &mut Preceding,
    colon_is_operator: bool,
    head_welds: bool,
) -> ValueToken {
    let start = *i;
    if operator_stands_here(run, start, *previous, colon_is_operator, head_welds) {
        *previous = Preceding::Operator {
            colon: run.as_bytes()[start] == b':',
        };
        *i += 1;
        return ValueToken {
            start,
            end: start + 1,
            is_operator: true,
        };
    }
    *previous = Preceding::Token(scan_token(run, i, colon_is_operator));
    debug_assert!(*i > start, "a token consumed nothing: {run:?} at {start}");
    ValueToken {
        start,
        end: *i,
        is_operator: false,
    }
}

/// Is the byte at `i` an operator in its own right, given what came before it?
///
/// Three readings of one question, because what releases an operator is what precedes it:
/// a number or a closed run releases every operator byte, a word releases only the two
/// that are never ident content, and at a run's start (or straight after another
/// operator) only a byte that cannot *begin* an operand can be one — and there a `+`
/// that can weld (`head_welds` at the run's start, after any operator but the `:`) is the
/// head of the word that follows it, not an operator.
fn operator_stands_here(
    run: &str,
    i: usize,
    previous: Preceding,
    colon_is_operator: bool,
    head_welds: bool,
) -> bool {
    let bytes = run.as_bytes();
    let b = bytes[i];
    // A `/*` opens a comment, never a division (css-syntax-3 §4.3.2).
    if is_comment_start(bytes, i) {
        return false;
    }
    if is_content_pair(bytes, i) {
        return false;
    }
    match previous {
        Preceding::Token(TokenKind::Number | TokenKind::Closed) => {
            releases_operator(b, colon_is_operator)
        }
        Preceding::Token(TokenKind::Word) => ends_word(b, colon_is_operator),
        // The two positions an OPERAND is expected at differ in exactly one input — whether
        // a `+` here welds onto the word after it — so they are one reading with that input
        // named: at the run's head the caller says, and after an operator every operator but
        // the `:` welds.
        Preceding::Head => operator_at_operand_position(run, i, colon_is_operator, head_welds),
        Preceding::Operator { colon } => {
            operator_at_operand_position(run, i, colon_is_operator, !colon)
        }
    }
}

/// [`operator_stands_here`] where an OPERAND is expected — at the run's head, or straight
/// after another operator. Only a byte that cannot *begin* an operand is an operator there.
///
/// `plus_welds` says whether a `+` here is the head of the word after it rather than an
/// operator (postcss's `operator()` returning `this.word()`): a `colon` node is no operator
/// for a sign to bind to, which is the element-level rule's exclusion too
/// (`super::parser::ValueParser::member_is_operator`), so `--x: a:+b` and `f(a:+b)` space the
/// `+` on both sides in one pass where `--x: a: +1.5` keeps the number's own sign.
///
/// A `-` here is the operator wherever it opens neither an IDENT (css-syntax-3 §4.3.9)
/// nor a NUMBER (§4.3.10) of its own, which is the lexer's [`hyphen_starts_own_token`] —
/// the one reading the printer's glue refusal takes too, so the two cannot part. A
/// doubled `-` never reaches it ([`operator_stands_here`] answers `--` as content ahead of
/// this), but §4.3.9 names it and the reading admits it, so they agree by construction.
///
/// ⚠️ **Not [`starts_word`], which is the `+`'s question.** postcss's word runs on to the
/// next byte in a *printer's* end set, so `#`, `.`, `!`, `%`, `[`, `]` and the rest of the
/// fifteen bytes this module's header names are all word content to it, and a `-` in
/// front of any of them would be the word's first byte. Read that way the same `-` had
/// two readings in one document: run-final it is an operator on every reading (`+-` then
/// ` [a]`), the head rule glued it onto the block, and the glued text `-[a]` then read
/// back as one word — two passes, two forms. Asking the byte-level question also keeps it
/// off the token this module would scan, which is a third answer again: a quote and a `[`
/// are both word CONTENT to [`scan_token_body`], so `-'x'` and `-[a]` each scan whole. See
/// `tests/fixtures/css/values/operators/hyphen_word_extent_prettier_divergence`.
fn operator_at_operand_position(
    run: &str,
    i: usize,
    colon_is_operator: bool,
    plus_welds: bool,
) -> bool {
    let bytes = run.as_bytes();
    let b = bytes[i];
    let next = bytes.get(i + 1).copied();
    let after = bytes.get(i + 2).copied();
    // A `-` here is the operator wherever it does not open an IDENT or a NUMBER of its
    // own — a quote, a `(`, a `,` / `:` / `{` / `}` / `@` / `*` / `/` / `+`, a `[…]`
    // block, a `#`, a `!`, and the run's end (`+-'x'`, `+-(2.5)`, `+-/2.5`, `+-*a`,
    // `--x: +-: a`, `1.5 /-`, `+-[a]`, `+-#a`). A letter, a digit, `_`, `é`, an escape
    // and a second `-` are the bound, where the `-` is that token's own head and
    // `-webkit-box`, `-1.5`, `-\61`, `--a` stay whole.
    if b == b'-' {
        return !hyphen_starts_own_token(&run[i + 1..]);
    }
    opens_run(b, colon_is_operator)
        && !starts_number(&run[i..])
        && !(b == b'+' && plus_welds && starts_word(next, after))
}

/// Consume one token starting at `*i`, returning what it was.
fn scan_token(run: &str, i: &mut usize, colon_is_operator: bool) -> TokenKind {
    let bytes = run.as_bytes();

    if bytes[*i] == b'(' {
        return close_group(run, i);
    }
    // A welded `+` (the only way a `+` reaches here — an operator is claimed by
    // `operator_stands_here` first): the sign is the head of the token after it, which
    // `starts_word` guaranteed is there and is not a boundary byte.
    if bytes[*i] == b'+' {
        *i += 1;
        return scan_token(run, i, colon_is_operator);
    }
    if bytes[*i] == b'@' {
        return scan_atword(run, i);
    }
    // A brace is a one-byte member. It releases every operator like a closed group
    // (`a{1.5}/2.5` is a `}`, an operator and a number to postcss) and what follows it
    // starts a new member.
    if matches!(bytes[*i], b'{' | b'}') {
        *i += 1;
        return TokenKind::Closed;
    }

    let range_len = unicode_range_len(bytes, *i);
    if range_len > 0 {
        *i += range_len;
        return TokenKind::Closed;
    }

    let number_len = number_part_len(&run[*i..]);
    if number_len > 0 {
        *i += number_len;
        // The unit. It ends at every operator byte, `/` and `-` included — which is what
        // makes `12px/1.5` a dimension and an operator rather than a dimension whose unit
        // is `px/1.5`.
        return scan_token_body(run, i, colon_is_operator, TokenKind::Number);
    }

    scan_token_body(run, i, colon_is_operator, TokenKind::Word)
}

/// The shared tail of both token shapes: walk content until a boundary this `kind`
/// releases, stepping every opaque construct whole. A `(` turns the token into a
/// [`TokenKind::Closed`] one (a function name meeting its argument list, or a bare group).
fn scan_token_body(
    run: &str,
    i: &mut usize,
    colon_is_operator: bool,
    kind: TokenKind,
) -> TokenKind {
    let bytes = run.as_bytes();
    let len = bytes.len();

    while *i < len {
        let b = bytes[*i];

        if b == b'\\'
            && let Some(step) = escape_len(run, *i)
        {
            *i += step;
            continue;
        }
        if b == b'"' || b == b'\'' {
            *i = string_end(bytes, *i).unwrap_or(len);
            continue;
        }
        if is_comment_start(bytes, *i) {
            *i = comment_end(bytes, *i);
            continue;
        }
        if b == b'(' {
            return close_group(run, i);
        }
        // A `[…]` simple block is **content**, where a `(…)` one is a value group and a
        // `{…}` one is two brace members around a run of its own: css-syntax-3 gives all
        // three a payload, but prettier's value parser descends only into the paren
        // block, leaves the bracket block as word text (`[1.50]` keeps its number on
        // both sides) and tokenizes each brace on its own. So the run continues through
        // a bracket block, operators and all — Tailwind's `--modifier(…, [*])` is one
        // token, not a bracket around a multiplication.
        if b == b'[' {
            *i = simple_block_end(run, *i);
            continue;
        }
        if ends_every_token(b) {
            return kind;
        }
        // A doubled operator is content (see `is_content_pair`), and **both** of its
        // bytes are: stepping only the first would leave the second standing as an
        // operator against the token this one just ended.
        if is_content_pair(bytes, *i) {
            *i += 2;
            continue;
        }
        let boundary = match kind {
            TokenKind::Number | TokenKind::Closed => releases_operator(b, colon_is_operator),
            TokenKind::Word => ends_word(b, colon_is_operator),
        };
        if boundary {
            return kind;
        }
        *i += 1;
    }
    kind
}

/// Consume the `@`-word at `*i` (its `@` included), returning what it is to the
/// separator rule: a closed member, since what follows it — an operator, a group, a
/// string, another `@` — always starts a member of its own.
fn scan_atword(run: &str, i: &mut usize) -> TokenKind {
    let bytes = run.as_bytes();
    debug_assert_eq!(bytes[*i], b'@', "scan_atword must be entered on a `@`");
    *i += 1;
    while *i < bytes.len() {
        let b = bytes[*i];
        if b == b'\\'
            && let Some(step) = escape_len(run, *i)
        {
            *i += step;
            continue;
        }
        if is_comment_start(bytes, *i) {
            *i = comment_end(bytes, *i);
            continue;
        }
        if ends_atword(b) {
            break;
        }
        *i += 1;
    }
    TokenKind::Closed
}

/// Is `text` exactly one `{…}` simple block — the **whole-value** block css-syntax-3
/// singles out ("a top-level {}-block is only allowed as the entire value of a non-custom
/// property")? The printer keeps that block's own braces spaced as authored where a brace
/// *inside* a value is glued to its interior.
pub(crate) fn is_whole_value_block(text: &str) -> bool {
    text.starts_with('{') && simple_block_end(text, 0) == text.len()
}

/// One past the `]` / `}` closing the simple block at `open`, or the run's end when it
/// never closes.
///
/// Nesting and the opaque constructs are stepped exactly as [`scan_token_body`] steps
/// them, so a bracket inside a string or an escape closes nothing.
fn simple_block_end(run: &str, open: usize) -> usize {
    let bytes = run.as_bytes();
    debug_assert!(
        matches!(bytes.get(open), Some(b'[' | b'{')),
        "simple_block_end must be entered on a `[` or a `{{`"
    );
    let close = if bytes[open] == b'[' { b']' } else { b'}' };
    let mut depth = 0u32;
    let mut i = open;
    while i < bytes.len() {
        let b = bytes[i];
        if b == b'\\'
            && let Some(step) = escape_len(run, i)
        {
            i += step;
            continue;
        }
        if b == b'"' || b == b'\'' {
            i = string_end(bytes, i).unwrap_or(bytes.len());
            continue;
        }
        if is_comment_start(bytes, i) {
            i = comment_end(bytes, i);
            continue;
        }
        if b == bytes[open] {
            depth += 1;
        } else if b == close {
            depth -= 1;
            if depth == 0 {
                return i + 1;
            }
        }
        i += 1;
    }
    bytes.len()
}

/// Step over the balanced `(`…`)` at `*i`, leaving `*i` one past its close. An
/// unbalanced `(` swallows the rest of the run, which keeps the leaf opaque exactly as it
/// is today.
fn close_group(run: &str, i: &mut usize) -> TokenKind {
    match matching_close_paren(run, *i) {
        Some(close) => *i = close + 1,
        None => *i = run.len(),
    }
    TokenKind::Closed
}

#[cfg(test)]
mod tests {
    use super::{BOUNDARY_BITS, ENDS_WORD_RUN, MAY_OPEN_BOUNDARY, any_byte_has, split_value_run};

    /// The chunked fold against the byte loop it is: for both bits, a carrier of each
    /// bit alone, of both and of neither at every position of every length that crosses
    /// zero to five chunk boundaries, plus two carriers at every pair of positions.
    #[test]
    fn any_byte_has_matches_the_byte_loop() {
        let model =
            |bytes: &[u8], bit: u8| bytes.iter().any(|&b| BOUNDARY_BITS[b as usize] & bit != 0);
        let carrier = |bits: u8| {
            (0..=255u8)
                .find(|&b| BOUNDARY_BITS[b as usize] == bits)
                .unwrap_or_else(|| panic!("no byte carries exactly {bits:#b}"))
        };
        let neither = carrier(0);
        // `ENDS_WORD_RUN` alone may have no byte (every word-ending byte may also open a
        // boundary); the pairs below then cover it through the bytes that carry both.
        let specials: Vec<u8> = (0..=255u8)
            .filter(|&b| BOUNDARY_BITS[b as usize] != 0)
            .fold(Vec::new(), |mut seen: Vec<u8>, b| {
                if !seen
                    .iter()
                    .any(|&s| BOUNDARY_BITS[s as usize] == BOUNDARY_BITS[b as usize])
                {
                    seen.push(b);
                }
                seen
            });
        assert!(!specials.is_empty());
        for len in 0..=41usize {
            let plain = vec![neither; len];
            for bit in [MAY_OPEN_BOUNDARY, ENDS_WORD_RUN] {
                assert_eq!(any_byte_has(&plain, bit), model(&plain, bit), "plain {len}");
                for &special in &specials {
                    for at in 0..len {
                        let mut bytes = plain.clone();
                        bytes[at] = special;
                        assert_eq!(
                            any_byte_has(&bytes, bit),
                            model(&bytes, bit),
                            "{special:#x} at {at} of {len}, bit {bit}"
                        );
                        for &other in &specials {
                            for second in at + 1..len {
                                bytes[second] = other;
                                assert_eq!(
                                    any_byte_has(&bytes, bit),
                                    model(&bytes, bit),
                                    "{special:#x} at {at}, {other:#x} at {second} of {len}"
                                );
                                bytes[second] = neither;
                            }
                        }
                    }
                }
            }
        }
    }

    /// The members as text. Every member that is an operator is spelled with its own
    /// byte, so the `is_operator` tag is graded by the same assertion as the split. The
    /// run is read as a group's interior when `in_group_interior` — where a `:` is an
    /// operator and the head does not weld — and as a plain declaration value's first run
    /// otherwise.
    fn split(run: &str, in_group_interior: bool) -> Option<Vec<String>> {
        split_at(run, in_group_interior, !in_group_interior)
    }

    fn split_at(run: &str, colon_is_operator: bool, head_welds: bool) -> Option<Vec<String>> {
        let arena = bumpalo::Bump::new();
        split_value_run(run, colon_is_operator, head_welds, &arena).map(|tokens| {
            tokens
                .into_iter()
                .map(|t| {
                    let text = &run[t.start..t.end];
                    if t.is_operator {
                        format!("op {text}")
                    } else {
                        text.to_owned()
                    }
                })
                .collect()
        })
    }

    /// The single-token runs, which must stay a leaf so every existing classification
    /// (colour, dimension, function, opaque identifier) keeps its path.
    #[test]
    fn a_single_token_run_does_not_split() {
        for run in [
            "red",
            "1px",
            "1.5",
            "-1.5",
            "1.5e-2",
            "#FFF",
            "100%",
            "a/1.5",
            "#FFF/1.5",
            "#FFF-5",
            "aa-bb",
            "a/1.5/2.5",
            "1.5--2.5",
            "var(--a)",
            "f(a, b)",
            "(1.5)",
            "url(a/1.5)",
            r"a\+b",
            r"a\/b",
            "1.5/*c*/",
            "'a/1.5'",
            "f(a:1.5)",
            // a doubled operator is content, and takes the whole run with it
            "1.50//2.50",
            "aa//1.50",
            "///1.50",
            "//x",
            // a `<unicode-range-token>` carries its own `+` and `-`
            "U+26",
            "u+4??",
            "U+0025-00FF",
            // a `[…]` simple block is content, operators and all
            "[*]",
            "[1.50]",
            "[a*b]",
            "[integer]",
            // an `@`-word runs through `*`, `+`, `-`, `:`, `#`, `.`, a digit and `[`
            "@a",
            "@",
            "@a*1.50",
            "@a+1.50",
            "@a-1.50",
            "@a1.50",
            "@a#FFF",
            "@a.50",
            "@a[1.50]c",
            r"@a\62 c",
            // a `+` opening the value welds onto the word after it
            "+a",
            "+a(2.50)",
            "+#FFF",
            "+a-1.50",
            "+a/1.50",
            r"+\61",
            "+u+26",
        ] {
            assert_eq!(split(run, false), None, "{run:?} is one token");
        }
    }

    /// A `/` or `-` ends a number and a closed run; inside a word it is content.
    #[test]
    fn division_and_subtraction_end_only_a_number_or_a_closed_run() {
        assert_eq!(split("1.5/2.5", false).unwrap(), ["1.5", "op /", "2.5"]);
        assert_eq!(split("12px/1.5", false).unwrap(), ["12px", "op /", "1.5"]);
        assert_eq!(split("100%/2", false).unwrap(), ["100%", "op /", "2"]);
        assert_eq!(split("1.5-2.5", false).unwrap(), ["1.5", "op -", "2.5"]);
        assert_eq!(split("f(a)/1.5", false).unwrap(), ["f(a)", "op /", "1.5"]);
        assert_eq!(split("(1.5)/2.5", false).unwrap(), ["(1.5)", "op /", "2.5"]);
        assert_eq!(
            split("1.5/2.5/aa", false).unwrap(),
            ["1.5", "op /", "2.5", "op /", "aa"]
        );
    }

    /// `*` and `+` end every run, a word included.
    #[test]
    fn multiplication_and_addition_end_every_run() {
        assert_eq!(split("1.5*2.5", false).unwrap(), ["1.5", "op *", "2.5"]);
        assert_eq!(split("aa*bb", false).unwrap(), ["aa", "op *", "bb"]);
        assert_eq!(split("aa+bb", false).unwrap(), ["aa", "op +", "bb"]);
        assert_eq!(split("#FFF+5", false).unwrap(), ["#FFF", "op +", "5"]);
        assert_eq!(split("aa*bb/cc", false).unwrap(), ["aa", "op *", "bb/cc"]);
    }

    /// A `:` is an operator only where `colon_is_operator` says it can stand as one —
    /// inside a group, and at the top level of a custom property's value (where the head
    /// still welds, unlike a group's). On a plain property there is no oracle for
    /// splitting one. Inside an `@`-word it is content anywhere.
    #[test]
    fn a_colon_splits_only_where_it_is_an_operator() {
        assert_eq!(split("a:1.5", false), None);
        assert_eq!(split("a:1.5", true).unwrap(), ["a", "op :", "1.5"]);
        assert_eq!(split("1.5:2.5", true).unwrap(), ["1.5", "op :", "2.5"]);
        assert_eq!(split("@a:1.5", true), None);
        // a custom property's value: the colon splits and the head still welds
        assert_eq!(split_at("a:1.5", true, true).unwrap(), ["a", "op :", "1.5"]);
        assert_eq!(split_at("+a(2.5)", true, true), None);
        assert_eq!(split_at("a:1.5", false, true), None);
        // a colon that opens the run, and two in a row, are members of their own
        assert_eq!(split_at(":a", true, true).unwrap(), ["op :", "a"]);
        assert_eq!(
            split_at("a::1.5", true, true).unwrap(),
            ["a", "op :", "op :", "1.5"]
        );
        // the sign the author glued to the digits after a colon is the number's own
        assert_eq!(
            split_at("a:+1.5", true, true).unwrap(),
            ["a", "op :", "+1.5"]
        );
        // a `{…}` block's interior splits at its colon like any other run
        assert_eq!(
            split_at("a{--b:1.5}c", true, true).unwrap(),
            ["a", "{", "--b", "op :", "1.5", "}", "c"]
        );
        // a doubled `/` is content, so a URL-shaped value is a word, a colon and a word
        assert_eq!(
            split_at("https://a.fuz.dev/b", true, true).unwrap(),
            ["https", "op :", "//a.fuz.dev/b"]
        );
    }

    /// A run that is one **operator** token is a member of its own — the
    /// whitespace-delimited element (`1.5 / 2.5`, `--x: a : b`, `1.5 * - 2.5`), which
    /// reaches the list as an operator node rather than as a word spelled with the
    /// operator's byte.
    #[test]
    fn a_one_token_operator_run_is_a_member() {
        assert_eq!(split("/", false).unwrap(), ["op /"]);
        assert_eq!(split("*", false).unwrap(), ["op *"]);
        assert_eq!(split("+", false).unwrap(), ["op +"]);
        assert_eq!(split("-", false).unwrap(), ["op -"]);
        // and the bytes postcss's word scan runs THROUGH but no token production opens on:
        // a `[…]` block, a `#`, a `.` no digit follows, a `!`
        assert_eq!(split("-[a]", false).unwrap(), ["op -", "[a]"]);
        assert_eq!(split("-#a", false).unwrap(), ["op -", "#a"]);
        assert_eq!(split("-.a", false).unwrap(), ["op -", ".a"]);
        assert_eq!(split("-!a", false).unwrap(), ["op -", "!a"]);
        assert_eq!(split(":", true).unwrap(), ["op :"]);
        // …and only where the byte really is one: a plain property's `:` is content
        assert_eq!(split(":", false), None);
        // a one-token OPERAND run still answers `None`, which is nearly every value
        assert_eq!(split("1.5", false), None);
        assert_eq!(split("-a", false), None);
        assert_eq!(split("-1.5", false), None);
    }

    /// A `-` where an OPERAND is expected is the operator wherever it opens neither an
    /// IDENT (css-syntax-3 §4.3.9) nor a NUMBER (§4.3.10) of its own — a quote, a `(`, a
    /// `)`, a `,`, a `:`, a brace, an `@`, a `*`, a `/`, a `+`, a `[…]` block, a `#`, a
    /// `!`, or the run's end — and that token's own head wherever it does. The question is
    /// the byte's, not the token this module would scan: `-'x'` and `-[a]` both scan whole
    /// here, and both of them split.
    #[test]
    fn a_hyphen_is_the_operator_wherever_it_opens_no_token_of_its_own() {
        assert_eq!(split("-'x'", false).unwrap(), ["op -", "'x'"]);
        assert_eq!(split(r#"-"x""#, false).unwrap(), ["op -", r#""x""#]);
        assert_eq!(split("-(2.5)", false).unwrap(), ["op -", "(2.5)"]);
        assert_eq!(split("-,a", false).unwrap(), ["op -", ",a"]);
        assert_eq!(split("-)", false).unwrap(), ["op -", ")"]);
        assert_eq!(split_at("-:a", true, true).unwrap(), ["op -", "op :", "a"]);
        assert_eq!(split("-{1.5}", false).unwrap(), ["op -", "{", "1.5", "}"]);
        assert_eq!(split("-@a", false).unwrap(), ["op -", "@a"]);
        assert_eq!(split("-*a", false).unwrap(), ["op -", "op *", "a"]);
        assert_eq!(split("-/2.5", false).unwrap(), ["op -", "op /", "2.5"]);
        assert_eq!(split("-+a", false).unwrap(), ["op -", "+a"]);
        assert_eq!(split("-+1.5", false).unwrap(), ["op -", "+1.5"]);
        assert_eq!(split("-", false).unwrap(), ["op -"]);
        // and the same reading straight after another operator, where the head's `+` is
        // an operator of its own because a `-` opens no word for it to weld onto
        assert_eq!(split("+-'x'", false).unwrap(), ["op +", "op -", "'x'"]);
        assert_eq!(split("+-(2.5)", false).unwrap(), ["op +", "op -", "(2.5)"]);
        assert_eq!(
            split("1.5*-+a", false).unwrap(),
            ["1.5", "op *", "op -", "+a"]
        );
        // the bound: a byte that DOES open an ident or a number keeps the `-` inside it —
        // an ident (`-webkit-box`), a number, a `.`-opened number, an escape, `_`, a
        // non-ASCII ident code point, and the doubled `-` that opens a
        // custom-property-shaped word
        assert_eq!(split("-webkit-box", false), None);
        assert_eq!(split("-1.5", false), None);
        assert_eq!(split("-.5", false), None);
        assert_eq!(split(r"-\61", false), None);
        assert_eq!(split("-_a", false), None);
        assert_eq!(split("-\u{e9}", false), None);
        assert_eq!(split("--a", false), None);
        // the `+` keeps postcss's word for its own weld, so the two signs part exactly at
        // the bytes above: the `+` is an operator before a `-` that opens no word of its
        // own, and the `-` behind it is one before a `[…]` block where it is not before a
        // letter
        assert_eq!(split("+-[a]", false).unwrap(), ["op +", "op -", "[a]"]);
        assert_eq!(split("+-a", false).unwrap(), ["op +", "-a"]);
        assert_eq!(split("+[a]", false), None);
    }

    /// An `@`-word ends every token before it and is ended by its own set: a quote, a
    /// comma, a paren, a brace, a `/` or another `@`.
    #[test]
    fn an_at_word_is_a_member_of_its_own() {
        assert_eq!(split("a@b", false).unwrap(), ["a", "@b"]);
        assert_eq!(split("@a@b", false).unwrap(), ["@a", "@b"]);
        assert_eq!(split("1.5@a", false).unwrap(), ["1.5", "@a"]);
        assert_eq!(split("#FFF@a", false).unwrap(), ["#FFF", "@a"]);
        assert_eq!(split("(1.5)@a", false).unwrap(), ["(1.5)", "@a"]);
        assert_eq!(split("@a(2.5)", false).unwrap(), ["@a", "(2.5)"]);
        assert_eq!(split("a@(2.5)", false).unwrap(), ["a", "@", "(2.5)"]);
        assert_eq!(split("@(2.5)", false).unwrap(), ["@", "(2.5)"]);
        assert_eq!(split("@a/1.5", false).unwrap(), ["@a", "op /", "1.5"]);
        assert_eq!(split("1.5/@a", false).unwrap(), ["1.5", "op /", "@a"]);
        assert_eq!(split("'x'@a", false).unwrap(), ["'x'", "@a"]);
        assert_eq!(split("@a'x'", false).unwrap(), ["@a", "'x'"]);
        assert_eq!(split("@a{1.5}", false).unwrap(), ["@a", "{", "1.5", "}"]);
        // an escaped `@` is ident content, never an `@`-word's start
        assert_eq!(split(r"a\@b", false), None);
        // a `-` straight before an `@`-word is an operator (glued by the sign rule), where
        // inside a word it is content (`a-` then the `@`-word)
        assert_eq!(split("-@a", false).unwrap(), ["op -", "@a"]);
        assert_eq!(
            split("1.5*-@a", false).unwrap(),
            ["1.5", "op *", "op -", "@a"]
        );
        assert_eq!(split("a-@b", false).unwrap(), ["a-", "@b"]);
        assert_eq!(split("-a", false), None);
    }

    /// Each brace is a one-byte member that releases every operator; the word between
    /// two braces is a member of its own.
    #[test]
    fn a_brace_is_a_member_of_its_own() {
        assert_eq!(
            split("a{1.5}c(2.5)", false).unwrap(),
            ["a", "{", "1.5", "}", "c(2.5)"]
        );
        assert_eq!(split("{1.5}", false).unwrap(), ["{", "1.5", "}"]);
        assert_eq!(split("{", false), None);
        assert_eq!(split("}", false), None);
        assert_eq!(
            split("a{{1.5}}c", false).unwrap(),
            ["a", "{", "{", "1.5", "}", "}", "c"]
        );
        assert_eq!(split("a{}c", false).unwrap(), ["a", "{", "}", "c"]);
        assert_eq!(
            split("a{1.5}/2.5", false).unwrap(),
            ["a", "{", "1.5", "}", "op /", "2.5"]
        );
        assert_eq!(
            split("a*{1.5}c", false).unwrap(),
            ["a", "op *", "{", "1.5", "}", "c"]
        );
        assert_eq!(
            split("a{1.5*2.5}c", false).unwrap(),
            ["a", "{", "1.5", "op *", "2.5", "}", "c"]
        );
        // a brace inside a bracket block, a string or an escape is content
        assert_eq!(split("a[{]c", false), None);
        assert_eq!(split("'{'", false), None);
        assert_eq!(split(r"a\{b", false), None);
    }

    /// A `+` welds onto the word after it at the value's head and after an operator;
    /// anywhere else, and before anything but a word, it is an operator.
    #[test]
    fn a_plus_welds_at_the_head_and_after_an_operator() {
        assert_eq!(split("+a*1.5", false).unwrap(), ["+a", "op *", "1.5"]);
        assert_eq!(
            split("1.5/+a(2.5)", false).unwrap(),
            ["1.5", "op /", "+a(2.5)"]
        );
        assert_eq!(split("1.5*+a", false).unwrap(), ["1.5", "op *", "+a"]);
        assert_eq!(
            split("1.5/++a", false).unwrap(),
            ["1.5", "op /", "op +", "+a"]
        );
        assert_eq!(split("++a", false).unwrap(), ["op +", "+a"]);
        assert_eq!(split("+a+b", false).unwrap(), ["+a", "op +", "b"]);
        assert_eq!(split("a+b", false).unwrap(), ["a", "op +", "b"]);
        // before a group, a string, an `@`-word or an operator it stays an operator
        assert_eq!(split("+(2.5)", false).unwrap(), ["op +", "(2.5)"]);
        assert_eq!(split("+'x'", false).unwrap(), ["op +", "'x'"]);
        assert_eq!(split("+@a", false).unwrap(), ["op +", "@a"]);
        assert_eq!(split("+-a", false).unwrap(), ["op +", "-a"]);
        assert_eq!(split("+/1.5", false).unwrap(), ["op +", "op /", "1.5"]);
        assert_eq!(split("1.5*+", false).unwrap(), ["1.5", "op *", "op +"]);
        // at the head of a group's interior, or after a comma, the head does not weld —
        // but a `+` after an operator inside the run still does
        assert_eq!(
            split_at("+a(2.5)", true, false).unwrap(),
            ["op +", "a(2.5)"]
        );
        assert_eq!(split_at("+a", false, false).unwrap(), ["op +", "a"]);
        assert_eq!(
            split_at("1.5/+a", true, false).unwrap(),
            ["1.5", "op /", "+a"]
        );
        // a signed number is a number, never a weld
        assert_eq!(split("+1.5", false), None);
        assert_eq!(split_at("+1.5", true, false), None);
    }

    /// The word a `+` welds onto can be the custom-property-shaped one: a DOUBLED `-`
    /// opens it where a single one opens an ident or signs a number.
    #[test]
    fn a_plus_welds_onto_a_doubled_dash_word() {
        assert_eq!(split("+--a", false), None);
        assert_eq!(split("+--", false), None);
        assert_eq!(split("+----a", false), None);
        assert_eq!(split("+--@a", false).unwrap(), ["+--", "@a"]);
        assert_eq!(split("1.5*+--a", false).unwrap(), ["1.5", "op *", "+--a"]);
        assert_eq!(split("++--a", false).unwrap(), ["op +", "+--a"]);
        // a single `-` after the sign is the ident's or the number's, and the `+` stays
        // an operator
        assert_eq!(split("+-a", false).unwrap(), ["op +", "-a"]);
        assert_eq!(split("+-1.5", false).unwrap(), ["op +", "-1.5"]);
        // and at the positions no word welds at, neither does this one
        assert_eq!(split_at("+--a", true, false).unwrap(), ["op +", "--a"]);
        assert_eq!(split_at("+--a", false, false).unwrap(), ["op +", "--a"]);
        assert_eq!(split("1.5+--a", false).unwrap(), ["1.5", "op +", "--a"]);
    }

    /// A single `/` still splits where the doubled one does not.
    #[test]
    fn a_lone_slash_still_splits() {
        assert_eq!(split("/1.5", false).unwrap(), ["op /", "1.5"]);
        assert_eq!(split("1.5/", false).unwrap(), ["1.5", "op /"]);
    }

    /// A member is not an operator just because it BEGINS with one's byte: at a run's
    /// start — or, as here, straight after another operator — a `-` before an ident code
    /// point opens the ident, so `-a` is an OPERAND however its first byte reads.
    /// Re-deriving the tag from the bytes is what `ValueToken::is_operator` exists to
    /// avoid.
    #[test]
    fn an_operand_opening_on_an_operator_byte_is_not_tagged_as_one() {
        assert_eq!(split("1.5*-a", false).unwrap(), ["1.5", "op *", "-a"]);
        assert_eq!(split("1.5*-1.5", false).unwrap(), ["1.5", "op *", "-1.5"]);
    }

    /// …and a `-` with nothing after it in the run IS the operator, in both of its
    /// positions: the whitespace-delimited element (`-`, the whole run) and a glued run's
    /// tail (`+-`, `1.5/-`, `a:-`). Read the second as a word and the two spellings of one
    /// value print each other forever (`+-` → `+ -` → `+-`).
    #[test]
    fn a_trailing_hyphen_is_the_operator_in_both_positions() {
        assert_eq!(split("-", false).unwrap(), ["op -"]);
        assert_eq!(split("+-", false).unwrap(), ["op +", "op -"]);
        assert_eq!(split("1.5/-", false).unwrap(), ["1.5", "op /", "op -"]);
        assert_eq!(split("1.5*-", false).unwrap(), ["1.5", "op *", "op -"]);
        assert_eq!(split_at("a:-", true, true).unwrap(), ["a", "op :", "op -"]);
    }

    /// A `+` welds onto the word after it wherever the run's head or another operator
    /// stands before it — but never after a `:`, which is no operator for a sign to bind
    /// to. The element-level rule says the same (`ValueParser::member_is_operator`), and
    /// the two must agree or one pass welds and the next splits.
    #[test]
    fn a_plus_does_not_weld_after_a_colon() {
        assert_eq!(
            split_at("a:+b", true, true).unwrap(),
            ["a", "op :", "op +", "b"]
        );
        assert_eq!(split_at(":+a", true, true).unwrap(), ["op :", "op +", "a"]);
        // …while the sign of a NUMBER is the number's own at every position
        assert_eq!(
            split_at("a:+1.5", true, true).unwrap(),
            ["a", "op :", "+1.5"]
        );
        // and after any other operator the weld stands
        assert_eq!(
            split_at("1.5/+b", true, true).unwrap(),
            ["1.5", "op /", "+b"]
        );
    }

    /// A unicode range ends its member, so a number glued past it is its own.
    #[test]
    fn a_unicode_range_ends_the_member() {
        assert_eq!(split("U+26-1.50", false).unwrap(), ["U+26-1", ".50"]);
    }

    /// A closed run ends the member outright, so the next one starts against it.
    #[test]
    fn a_closed_run_ends_the_member() {
        assert_eq!(split("(1.5)(2.5)", false).unwrap(), ["(1.5)", "(2.5)"]);
        assert_eq!(split("f(1.5)g(2.5)", false).unwrap(), ["f(1.5)", "g(2.5)"]);
        assert_eq!(split("(1.5)aa", false).unwrap(), ["(1.5)", "aa"]);
        assert_eq!(split("1.5/(2.5)", false).unwrap(), ["1.5", "op /", "(2.5)"]);
    }

    /// An unbalanced `(` swallows the rest of the run, which is what keeps the leaf
    /// opaque rather than inventing a member boundary inside it.
    #[test]
    fn an_unbalanced_paren_swallows_the_run() {
        assert_eq!(split("1.5/(2.5", false).unwrap(), ["1.5", "op /", "(2.5"]);
        assert_eq!(split("(1.5", false), None);
    }
}