chordsketch-chordpro 0.5.0

ChordPro parser, AST, and transforms
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
//! Structured tokeniser for `{start_of_grid}` body lines.
//!
//! Sister-site to `tokenizeGridLine` + `renderGridLine` in
//! `packages/react/src/chordpro-jsx.tsx`. The Rust side
//! exposes the same token shapes so the three Rust renderers
//! (`chordsketch-render-text`, `chordsketch-render-html`,
//! `chordsketch-render-pdf`) and the React JSX walker emit
//! sister-site-identical markup for the same grid input.
//!
//! Grid AST content remains verbatim text in
//! `LyricsSegment::text`; this module is consumed by
//! renderers at output time to extract semantic tokens.
//! Keeping the structured pass out of the AST avoids
//! breaking existing consumers that walk plain lyrics
//! lines.
//!
//! Per the ChordPro spec
//! (<https://www.chordpro.org/chordpro/directives-env_grid/>):
//!
//! - `shape="L+MxB+R"` — left margin cells + measures ×
//!   beats + right margin cells. Default `1+4x4+1`.
//! - `|` / `||` / `|.` / `|:` / `:|` / `:|:` / `|N` —
//!   barlines and volta endings.
//! - `%` / `%%` — repeat last / last two measures.
//! - `.` — empty beat cell (continuation).
//! - `n` — no-chord beat.
//! - `~` inside a cell — separates multiple chords sharing
//!   one beat slot (`C~G` = both C and G in this cell).
//! - `s` immediately after the opening barline — marks the
//!   row as a strum-pattern row. Subsequent cells are
//!   parsed as strum tokens (`up`/`dn`/`u+`/`d+`/`ua`/`da`).
//!
//! Dialect tokens widely used in jazz / pop chord-chart
//! conventions are passed through verbatim (no spec
//! definition, but tolerated):
//!
//! - Row labels (`A`, `Coda`) at the start of the row,
//!   before the first barline.
//! - Trailing comments (`repeat 4 times`) after the last
//!   barline.
//! - Tilde-prefixed strum tokens (`~dn`, `dn~up`, `~ux`).

/// Parsed `shape="L+MxB+R"` attribute on `{start_of_grid}`.
///
/// Renderers can use these dimensions to lay out cell
/// columns consistently (margin-left cells reserved for a
/// label column, body cells split into measures × beats,
/// margin-right cells reserved for a trailing-comment
/// column).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GridShape {
    pub margin_left: u8,
    pub measures: u8,
    pub beats: u8,
    pub margin_right: u8,
}

impl Default for GridShape {
    /// Spec default when `{start_of_grid}` carries no
    /// `shape="..."` attribute: `1+4x4+1`.
    fn default() -> Self {
        Self {
            margin_left: 1,
            measures: 4,
            beats: 4,
            margin_right: 1,
        }
    }
}

impl GridShape {
    /// Parse a `shape="..."` attribute string.
    ///
    /// Three spec-defined forms are accepted:
    /// - `L+MxB+R` — full form (margin-left + measures × beats
    ///   + margin-right).
    /// - `MxB` — body only; margins default to 0.
    /// - `N` — bare cell count; treated as a single measure of
    ///   N beats with no margins.
    ///
    /// The `shape` attribute name and the `x` / `X` / `*`
    /// separator between measures and beats are all matched
    /// case-insensitively. Numeric components must be valid
    /// `u8` (0..=255). Falls back to [`GridShape::default`]
    /// (the spec default `1+4x4+1`) on parse failure.
    #[must_use]
    pub fn parse(raw: &str) -> Self {
        let inner = extract_shape_value(raw).unwrap_or(raw.trim());
        parse_shape_body(inner).unwrap_or_default()
    }
}

/// Extract a `label="..."` attribute from a grid directive's
/// inline value. Returns the unquoted label string if found,
/// otherwise `None`. Used by all renderers to surface the
/// spec-defined `{start_of_grid label="Intro"}` form as a
/// human-readable section heading without leaking the
/// attribute syntax into the rendered output.
#[must_use]
pub fn extract_grid_label(raw: &str) -> Option<String> {
    let start = find_substr_ci(raw, "label")?;
    let after = raw[start + 5..].trim_start();
    let after = after.strip_prefix('=')?.trim_start();
    if let Some(rest) = after.strip_prefix('"') {
        let end = rest.find('"')?;
        return Some(rest[..end].to_string());
    }
    // Bare-value form `label=Intro` — terminator is whitespace.
    let end = after.find(char::is_whitespace).unwrap_or(after.len());
    if end == 0 {
        return None;
    }
    Some(after[..end].to_string())
}

fn extract_shape_value(raw: &str) -> Option<&str> {
    // `shape="..."` — quoted form
    if let Some(start) = find_substr_ci(raw, "shape") {
        let after = raw[start + 5..].trim_start();
        let after = after.strip_prefix('=')?.trim_start();
        if let Some(rest) = after.strip_prefix('"') {
            let end = rest.find('"')?;
            return Some(&rest[..end]);
        }
        // `shape=foo` — bare-value form, terminator is whitespace.
        let end = after.find(char::is_whitespace).unwrap_or(after.len());
        return Some(&after[..end]);
    }
    None
}

/// Find an ASCII case-insensitive substring in a (possibly
/// non-ASCII) string, returning the BYTE offset of the match.
///
/// Walks char boundaries via `char_indices` instead of raw byte
/// offsets so multibyte UTF-8 sequences preceding the needle
/// don't trigger a "byte index N is not a char boundary" panic
/// when we slice for the comparison. The needle is restricted
/// to ASCII (the grid module only searches for the literal
/// `"shape"`), so byte-length comparison is safe.
fn find_substr_ci(hay: &str, needle: &str) -> Option<usize> {
    let n = needle.len();
    if n == 0 {
        return Some(0);
    }
    for (idx, _) in hay.char_indices() {
        let end = idx.checked_add(n)?;
        if end > hay.len() {
            return None;
        }
        // Only attempt the slice when `end` lands on a char
        // boundary; otherwise this position is the middle of a
        // multibyte sequence and the substring obviously can't
        // match an ASCII needle.
        if !hay.is_char_boundary(end) {
            continue;
        }
        if hay[idx..end].eq_ignore_ascii_case(needle) {
            return Some(idx);
        }
    }
    None
}

fn parse_shape_body(s: &str) -> Option<GridShape> {
    // The spec defines three forms (see
    // https://www.chordpro.org/chordpro/directives-env_grid/):
    //   `L+MxB+R`  — full form with explicit margin cells
    //   `MxB`      — body only; margins fall back to 0
    //   `N`        — bare cell count; treated as `0+NxN+0`
    //                where the body is a single measure of N
    //                beats (the spec lets the second factor
    //                default to the missing one)
    let parts: Vec<&str> = s.split('+').map(str::trim).collect();
    match parts.as_slice() {
        // Full form: L+MxB+R
        [left, body, right] => {
            let left = left.parse::<u8>().ok()?;
            let right = right.parse::<u8>().ok()?;
            let (measures, beats) = split_body_measures_beats(body)?;
            Some(GridShape {
                margin_left: left,
                measures,
                beats,
                margin_right: right,
            })
        }
        // Body-only form: MxB (margins default to 0)
        [body] => {
            let (measures, beats) = split_body_measures_beats(body)?;
            Some(GridShape {
                margin_left: 0,
                measures,
                beats,
                margin_right: 0,
            })
        }
        _ => None,
    }
}

/// Split a body specifier into `(measures, beats)`.
///
/// `4x4` → `(4, 4)`. `16` (bare cell count) → `(1, 16)` — the
/// spec treats a single integer as a flat 1-measure × N-beats
/// strip, which is what `shape="16"` renders to in the
/// reference implementation.
fn split_body_measures_beats(body: &str) -> Option<(u8, u8)> {
    let parts: Vec<&str> = body.split(['x', 'X', '*']).map(str::trim).collect();
    match parts.as_slice() {
        [measures, beats] => Some((measures.parse().ok()?, beats.parse().ok()?)),
        [single] => Some((1, single.parse().ok()?)),
        _ => None,
    }
}

/// Single barline variant emitted by the tokeniser.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GridBarline {
    /// `|` — bare single barline.
    Single,
    /// `||` — double barline.
    Double,
    /// `|.` — final barline (end of piece / section).
    Final,
    /// `|:` — start of repeated section.
    RepeatStart,
    /// `:|` — end of repeated section.
    RepeatEnd,
    /// `:|:` — combined end + start (renders as a single
    /// visual glyph in standard notation).
    RepeatBoth,
}

/// One token in a tokenised grid row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GridToken {
    Space,
    Barline(GridBarline),
    /// `|N` volta ending marker (N ∈ 1..=9).
    Volta(u8),
    /// Chord cell (1 or more chord names). Single-element
    /// vec for a plain cell like `C`; multi-element for
    /// `~`-separated cells like `C~G`. An empty string in
    /// the vec preserves a leading-tilde anticipation
    /// marker (`~A` → `["", "A"]`).
    Cell(Vec<String>),
    /// `%` — repeat previous measure.
    Percent1,
    /// `%%` — repeat previous two measures.
    Percent2,
    /// `.` — beat continuation / empty cell.
    Continuation,
    /// `n` — no-chord marker (rare; iReal Pro convention).
    NoChord,
}

/// Per-row classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GridRowKind {
    /// Standard chord row.
    Chord,
    /// Strum-pattern row — cells encode `up`/`dn`/`u+`/`d+`/
    /// etc. instead of chord names. Marked by a leading
    /// `s`/`S` cell immediately after the opening barline.
    Strum,
}

/// Structured representation of a single grid body line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GridRow {
    /// Optional row label (`A`, `Coda`, etc.) at the start
    /// of the line, before the first barline. Not formally
    /// in the ChordPro spec but a widely used dialect
    /// convention.
    pub label: Option<String>,
    pub kind: GridRowKind,
    /// Body tokens between the first and last barline,
    /// inclusive of the barlines themselves.
    pub body: Vec<GridToken>,
    /// Optional trailing comment (`repeat 4 times`, etc.)
    /// after the last barline. Dialect convention.
    pub trailing_comment: Option<String>,
}

/// Tokenise a grid row into `[GridToken]`.
///
/// Sister-site to `tokenizeGridLine` in
/// `packages/react/src/chordpro-jsx.tsx`. Both functions
/// MUST produce token streams with one-to-one variant
/// correspondence so the four rendering surfaces (3 Rust +
/// the React JSX walker) emit equivalent markup.
#[must_use]
pub fn tokenize_grid_line(input: &str) -> Vec<GridToken> {
    let bytes = input.as_bytes();
    let mut out: Vec<GridToken> = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i];
        if c == b' ' || c == b'\t' {
            // Coalesce whitespace runs into one Space token.
            while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
                i += 1;
            }
            out.push(GridToken::Space);
            continue;
        }
        if c == b'|' {
            let next = bytes.get(i + 1).copied();
            match next {
                Some(b':') => {
                    out.push(GridToken::Barline(GridBarline::RepeatStart));
                    i += 2;
                    continue;
                }
                Some(b'.') => {
                    out.push(GridToken::Barline(GridBarline::Final));
                    i += 2;
                    continue;
                }
                Some(b'|') => {
                    out.push(GridToken::Barline(GridBarline::Double));
                    i += 2;
                    continue;
                }
                Some(d @ b'1'..=b'9') => {
                    out.push(GridToken::Volta(d - b'0'));
                    i += 2;
                    continue;
                }
                _ => {
                    out.push(GridToken::Barline(GridBarline::Single));
                    i += 1;
                    continue;
                }
            }
        }
        if c == b':' && bytes.get(i + 1) == Some(&b'|') {
            if bytes.get(i + 2) == Some(&b':') {
                out.push(GridToken::Barline(GridBarline::RepeatBoth));
                i += 3;
                continue;
            }
            out.push(GridToken::Barline(GridBarline::RepeatEnd));
            i += 2;
            continue;
        }
        if c == b'%' {
            if bytes.get(i + 1) == Some(&b'%') {
                out.push(GridToken::Percent2);
                i += 2;
                continue;
            }
            out.push(GridToken::Percent1);
            i += 1;
            continue;
        }
        if c == b'.' {
            out.push(GridToken::Continuation);
            i += 1;
            continue;
        }
        if c == b'n' && (i + 1 >= bytes.len() || matches!(bytes[i + 1], b' ' | b'\t' | b'|')) {
            out.push(GridToken::NoChord);
            i += 1;
            continue;
        }
        // Read a cell token — any contiguous run of
        // non-whitespace / non-bar / non-colon characters.
        // Chord brackets `[X]` are unwrapped.
        let start = i;
        while i < bytes.len() && !matches!(bytes[i], b' ' | b'\t' | b'|' | b':') {
            i += 1;
        }
        let mut raw = &input[start..i];
        if raw.starts_with('[') && raw.ends_with(']') && raw.len() >= 2 {
            raw = &raw[1..raw.len() - 1];
        }
        if !raw.is_empty() {
            let names: Vec<String> = raw.split('~').map(|s| s.to_string()).collect();
            out.push(GridToken::Cell(names));
        }
    }
    out
}

/// Walk a tokenised grid row to extract its label, strum
/// classification, body, and trailing comment.
///
/// Sister-site to the bucket-split logic in
/// `renderGridLine` (React JSX walker). The two implementations
/// MUST agree on every classification rule.
#[must_use]
pub fn classify_grid_row(input: &str) -> GridRow {
    let tokens = tokenize_grid_line(input);
    // Empty input early-out — avoids two layers of
    // `saturating_sub(1)` guarding against an empty token
    // stream later in the function.
    if tokens.is_empty() {
        return GridRow {
            label: None,
            kind: GridRowKind::Chord,
            body: Vec::new(),
            trailing_comment: None,
        };
    }
    let first_bar = tokens.iter().position(is_barline_like);
    let last_bar = tokens.iter().rposition(is_barline_like);

    let label = match first_bar {
        Some(idx) if idx > 0 => {
            let text = render_label_text(&tokens[..idx]);
            if text.is_empty() { None } else { Some(text) }
        }
        _ => None,
    };
    let trailing_comment = match last_bar {
        Some(idx) if idx + 1 < tokens.len() => {
            let text = render_label_text(&tokens[idx + 1..]);
            if text.is_empty() { None } else { Some(text) }
        }
        _ => None,
    };
    let body_slice: &[GridToken] = match (first_bar, last_bar) {
        (Some(f), Some(l)) if l >= f => &tokens[f..=l],
        // No barline anywhere — every token is body content.
        _ => &tokens[..],
    };

    // Strum row detection: the first cell after the opening
    // barline is `s` / `S` (case-insensitive).
    let mut kind = GridRowKind::Chord;
    let mut strum_marker_idx: Option<usize> = None;
    for (i, t) in body_slice.iter().enumerate() {
        if is_barline_like(t) {
            continue;
        }
        if matches!(t, GridToken::Space) {
            continue;
        }
        if let GridToken::Cell(names) = t {
            if names.len() == 1 && (names[0] == "s" || names[0] == "S") {
                kind = GridRowKind::Strum;
                strum_marker_idx = Some(i);
            }
        }
        break;
    }

    // Drop the strum-row marker cell from the body — it's a
    // row-type signal, not a musical cell.
    let body: Vec<GridToken> = body_slice
        .iter()
        .enumerate()
        .filter(|(i, _)| strum_marker_idx != Some(*i))
        .map(|(_, t)| t.clone())
        .collect();

    GridRow {
        label,
        kind,
        body,
        trailing_comment,
    }
}

fn is_barline_like(t: &GridToken) -> bool {
    matches!(t, GridToken::Barline(_) | GridToken::Volta(_))
}

/// Render label / comment tokens as a flat string. Used
/// for the label column (text before the first barline) and
/// the comment column (text after the last barline).
fn render_label_text(tokens: &[GridToken]) -> String {
    let mut out = String::new();
    for t in tokens {
        match t {
            GridToken::Space => out.push(' '),
            GridToken::Cell(names) => out.push_str(&names.join("~")),
            GridToken::Continuation => out.push('.'),
            GridToken::Percent1 => out.push('%'),
            GridToken::Percent2 => out.push_str("%%"),
            GridToken::NoChord => out.push('n'),
            _ => {}
        }
    }
    out.trim().to_string()
}

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

    #[test]
    fn shape_default_when_missing() {
        assert_eq!(GridShape::parse(""), GridShape::default());
        assert_eq!(GridShape::parse("garbage"), GridShape::default());
    }

    #[test]
    fn shape_parses_attribute_form() {
        assert_eq!(
            GridShape::parse(r#"shape="1+4x2+4""#),
            GridShape {
                margin_left: 1,
                measures: 4,
                beats: 2,
                margin_right: 4,
            }
        );
        assert_eq!(
            GridShape::parse(r#"shape="0+2x4+4""#),
            GridShape {
                margin_left: 0,
                measures: 2,
                beats: 4,
                margin_right: 4,
            }
        );
    }

    #[test]
    fn shape_survives_multibyte_preceding_text() {
        // Regression: previous `find_substr_ci` byte-indexed
        // through the input, panicking when a multibyte char
        // preceded "shape". Public API must not panic on any
        // input.
        let shape = GridShape::parse("\u{3042}shape=\"2+8x3+1\"");
        assert_eq!(
            shape,
            GridShape {
                margin_left: 2,
                measures: 8,
                beats: 3,
                margin_right: 1,
            }
        );
        // Multibyte preceding bare-value form should also work.
        assert_eq!(GridShape::parse("\u{30B7}shape=1+4x4+1").measures, 4);
        // Pure non-ASCII input falls back to default (no panic).
        assert_eq!(GridShape::parse("\u{4E2D}\u{6587}"), GridShape::default());
    }

    #[test]
    fn shape_parses_bare_value() {
        assert_eq!(
            GridShape::parse("2+8x3+1"),
            GridShape {
                margin_left: 2,
                measures: 8,
                beats: 3,
                margin_right: 1,
            }
        );
    }

    #[test]
    fn extract_label_quoted_form() {
        assert_eq!(
            extract_grid_label(r#"label="Intro" shape="4x4""#),
            Some("Intro".to_string())
        );
    }

    #[test]
    fn extract_label_bare_form() {
        assert_eq!(extract_grid_label("label=Outro"), Some("Outro".to_string()));
    }

    #[test]
    fn extract_label_missing() {
        assert_eq!(extract_grid_label(r#"shape="1+4x4+1""#), None);
        assert_eq!(extract_grid_label(""), None);
    }

    #[test]
    fn shape_parses_body_only_form() {
        // Spec form `shape="MxB"` — margins default to 0.
        assert_eq!(
            GridShape::parse(r#"shape="4x4""#),
            GridShape {
                margin_left: 0,
                measures: 4,
                beats: 4,
                margin_right: 0,
            }
        );
    }

    #[test]
    fn shape_parses_bare_cell_count() {
        // Spec form `shape="N"` — single-measure strip of N
        // beats, no margins.
        assert_eq!(
            GridShape::parse(r#"shape="16""#),
            GridShape {
                margin_left: 0,
                measures: 1,
                beats: 16,
                margin_right: 0,
            }
        );
    }

    #[test]
    fn tokenize_basic_bar() {
        let toks = tokenize_grid_line("| G  .  .  . |");
        let kinds: Vec<_> = toks
            .iter()
            .filter(|t| !matches!(t, GridToken::Space))
            .collect();
        assert_eq!(kinds.len(), 6);
        assert!(matches!(kinds[0], GridToken::Barline(GridBarline::Single)));
        assert!(matches!(kinds[1], GridToken::Cell(n) if n == &vec!["G".to_string()]));
        assert!(matches!(kinds[2], GridToken::Continuation));
        assert!(matches!(kinds[3], GridToken::Continuation));
        assert!(matches!(kinds[4], GridToken::Continuation));
        assert!(matches!(kinds[5], GridToken::Barline(GridBarline::Single)));
    }

    #[test]
    fn tokenize_all_barline_variants() {
        let toks = tokenize_grid_line("|: G :|: C :| D || E |.");
        let bars: Vec<_> = toks
            .iter()
            .filter_map(|t| match t {
                GridToken::Barline(b) => Some(*b),
                _ => None,
            })
            .collect();
        assert_eq!(
            bars,
            vec![
                GridBarline::RepeatStart,
                GridBarline::RepeatBoth,
                GridBarline::RepeatEnd,
                GridBarline::Double,
                GridBarline::Final,
            ]
        );
    }

    #[test]
    fn tokenize_volta_endings() {
        let toks = tokenize_grid_line("|1 Em |2 Am");
        let voltas: Vec<u8> = toks
            .iter()
            .filter_map(|t| match t {
                GridToken::Volta(n) => Some(*n),
                _ => None,
            })
            .collect();
        assert_eq!(voltas, vec![1, 2]);
    }

    #[test]
    fn tokenize_percent_repeat_markers() {
        let toks = tokenize_grid_line("| % . | %% . |");
        let kinds: Vec<_> = toks
            .iter()
            .filter(|t| !matches!(t, GridToken::Space))
            .map(std::mem::discriminant)
            .collect();
        // bar percent1 cont bar percent2 cont bar
        assert_eq!(kinds.len(), 7);
        assert!(matches!(toks[2], GridToken::Percent1));
        // toks[8] should be Percent2 (depending on spaces); find it
        assert!(toks.iter().any(|t| matches!(t, GridToken::Percent2)));
    }

    #[test]
    fn tokenize_multi_chord_tilde_split() {
        let toks = tokenize_grid_line("| C~G ~A |");
        let cells: Vec<&Vec<String>> = toks
            .iter()
            .filter_map(|t| match t {
                GridToken::Cell(n) => Some(n),
                _ => None,
            })
            .collect();
        assert_eq!(cells[0], &vec!["C".to_string(), "G".to_string()]);
        assert_eq!(cells[1], &vec!["".to_string(), "A".to_string()]);
    }

    #[test]
    fn tokenize_no_chord_marker() {
        let toks = tokenize_grid_line("| n | G |");
        assert!(toks.iter().any(|t| matches!(t, GridToken::NoChord)));
    }

    #[test]
    fn tokenize_empty_bracket_cell_drops_to_no_token() {
        // `[]` unwraps to an empty string, which is then
        // filtered out by the cell-emission step. This is
        // intentional: an explicitly-empty bracket carries no
        // chord information, so the tokeniser treats it the
        // same as whitespace. The renderer therefore lays out
        // the surrounding beats as if `[]` weren't there.
        let toks = tokenize_grid_line("| [] G |");
        // Exactly one cell token surfaces — the `G`.
        let cells: Vec<&Vec<String>> = toks
            .iter()
            .filter_map(|t| match t {
                GridToken::Cell(n) => Some(n),
                _ => None,
            })
            .collect();
        assert_eq!(cells.len(), 1);
        assert_eq!(cells[0], &vec!["G".to_string()]);
    }

    #[test]
    fn tokenize_unwraps_bracketed_chord_names() {
        let toks = tokenize_grid_line("| [Am] [C] |");
        let cells: Vec<&Vec<String>> = toks
            .iter()
            .filter_map(|t| match t {
                GridToken::Cell(n) => Some(n),
                _ => None,
            })
            .collect();
        assert_eq!(cells[0], &vec!["Am".to_string()]);
        assert_eq!(cells[1], &vec!["C".to_string()]);
    }

    #[test]
    fn classify_row_with_label() {
        let row = classify_grid_row("A || G7 . | C . |");
        assert_eq!(row.label.as_deref(), Some("A"));
        assert_eq!(row.kind, GridRowKind::Chord);
        assert!(row.trailing_comment.is_none());
    }

    #[test]
    fn classify_row_with_trailing_comment() {
        let row = classify_grid_row("|: G :| repeat 4 times");
        assert_eq!(row.label, None);
        assert_eq!(row.trailing_comment.as_deref(), Some("repeat 4 times"));
    }

    #[test]
    fn classify_row_strum_detection() {
        let row = classify_grid_row("|s dn up dn up |");
        assert_eq!(row.kind, GridRowKind::Strum);
        // The leading `s` cell should be filtered out.
        let cells: Vec<&Vec<String>> = row
            .body
            .iter()
            .filter_map(|t| match t {
                GridToken::Cell(n) => Some(n),
                _ => None,
            })
            .collect();
        assert_eq!(cells.len(), 4);
        assert_eq!(cells[0], &vec!["dn".to_string()]);
    }

    #[test]
    fn classify_row_chord_when_not_strum() {
        let row = classify_grid_row("| G7 . . . |");
        assert_eq!(row.kind, GridRowKind::Chord);
    }
}