shape-ast 0.3.2

AST types and Pest grammar for the Shape programming language
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
//! Shared formatted-string interpolation parsing.
//!
//! This module is intentionally syntax-only. It extracts literal and
//! expression segments from `f"..."` strings so all consumers (compiler,
//! type checker, LSP) can run their normal expression pipelines on the
//! extracted `{...}` expressions.

use crate::ast::InterpolationMode;
use crate::content_style::{ContentFormatSpec, parse_content_format_spec};
use crate::{Result, ShapeError};

/// Horizontal alignment for formatted output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatAlignment {
    Left,
    Center,
    Right,
}

impl FormatAlignment {
    fn parse(s: &str) -> Option<Self> {
        match s {
            "left" => Some(Self::Left),
            "center" => Some(Self::Center),
            "right" => Some(Self::Right),
            _ => None,
        }
    }
}

/// Color hint for formatted output.
///
/// Renderers may map these hints to ANSI, HTML, or plain output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatColor {
    Default,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
}

impl FormatColor {
    fn parse(s: &str) -> Option<Self> {
        match s {
            "default" => Some(Self::Default),
            "red" => Some(Self::Red),
            "green" => Some(Self::Green),
            "yellow" => Some(Self::Yellow),
            "blue" => Some(Self::Blue),
            "magenta" => Some(Self::Magenta),
            "cyan" => Some(Self::Cyan),
            "white" => Some(Self::White),
            _ => None,
        }
    }
}

/// Typed table rendering configuration for interpolation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableFormatSpec {
    pub max_rows: Option<usize>,
    pub align: Option<FormatAlignment>,
    pub precision: Option<u8>,
    pub color: Option<FormatColor>,
    pub border: bool,
}

impl Default for TableFormatSpec {
    fn default() -> Self {
        Self {
            max_rows: None,
            align: None,
            precision: None,
            color: None,
            border: true,
        }
    }
}

/// Typed format specification for interpolation expressions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InterpolationFormatSpec {
    /// Fixed-point numeric precision (`fixed(2)`).
    Fixed { precision: u8 },
    /// Tabular formatting for `DataTable`-like values (`table(...)`).
    Table(TableFormatSpec),
    /// Content-styling specification (R8 W4 W18.4 — supervisor 2026-05-24
    /// D1 + (a-modified) REVIVE-WITH-SHARED-MODULE). Parsed from f-string
    /// styling syntax like `{x:bold,red}` / `{x:fg(blue),italic}`.
    /// The presence of this variant in any interpolation part flips the
    /// f-string's return type from `string` to `content` per D1's
    /// syntax-determined rule.
    ContentStyle(ContentFormatSpec),
}

/// A parsed segment of an interpolated string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InterpolationPart {
    /// Literal text.
    Literal(String),
    /// Expression segment with optional format specifier.
    Expression {
        /// Raw Shape expression between `{` and `}`.
        expr: String,
        /// Optional typed format spec after top-level `:`.
        format_spec: Option<InterpolationFormatSpec>,
    },
}

/// Parse a formatted string payload into interpolation parts.
pub fn parse_interpolation(s: &str) -> Result<Vec<InterpolationPart>> {
    parse_interpolation_with_mode(s, InterpolationMode::Braces)
}

/// Parse a formatted string payload into interpolation parts using the given mode.
pub fn parse_interpolation_with_mode(
    s: &str,
    mode: InterpolationMode,
) -> Result<Vec<InterpolationPart>> {
    let mut parts = Vec::new();
    let mut current_text = String::new();
    let mut chars = s.chars().peekable();

    while let Some(ch) = chars.next() {
        // Backslash-escaped delimiters: `\{` → `{`, `\}` → `}`, `\$` → `$`, `\#` → `#`
        if ch == '\\'
            && matches!(
                chars.peek(),
                Some(&'{') | Some(&'}') | Some(&'$') | Some(&'#')
            )
        {
            current_text.push(chars.next().unwrap());
            continue;
        }

        match mode {
            InterpolationMode::Braces => match ch {
                '{' => {
                    if chars.peek() == Some(&'{') {
                        chars.next();
                        current_text.push('{');
                        continue;
                    }

                    if !current_text.is_empty() {
                        parts.push(InterpolationPart::Literal(current_text.clone()));
                        current_text.clear();
                    }

                    let raw_expr = parse_expression_content(&mut chars)?;
                    let (expr, format_spec) = split_expression_and_format_spec(&raw_expr)?;
                    parts.push(InterpolationPart::Expression { expr, format_spec });
                }
                '}' => {
                    if chars.peek() == Some(&'}') {
                        chars.next();
                        current_text.push('}');
                    } else {
                        return Err(ShapeError::RuntimeError {
                            message:
                                "Unmatched '}' in interpolation string. Use '}}' for a literal '}'"
                                    .to_string(),
                            location: None,
                        });
                    }
                }
                _ => current_text.push(ch),
            },
            InterpolationMode::Dollar | InterpolationMode::Hash => {
                let sigil = mode.sigil().expect("sigil mode must provide sigil");
                if ch == sigil {
                    if chars.peek() == Some(&sigil) {
                        chars.next();
                        if chars.peek() == Some(&'{') {
                            chars.next();
                            current_text.push(sigil);
                            current_text.push('{');
                        } else {
                            current_text.push(sigil);
                        }
                        continue;
                    }

                    if chars.peek() == Some(&'{') {
                        chars.next();
                        if !current_text.is_empty() {
                            parts.push(InterpolationPart::Literal(current_text.clone()));
                            current_text.clear();
                        }
                        let raw_expr = parse_expression_content(&mut chars)?;
                        let (expr, format_spec) = split_expression_and_format_spec(&raw_expr)?;
                        parts.push(InterpolationPart::Expression { expr, format_spec });
                        continue;
                    }
                }

                current_text.push(ch);
            }
        }
    }

    if !current_text.is_empty() {
        parts.push(InterpolationPart::Literal(current_text));
    }

    Ok(parts)
}

/// Check whether a string contains at least one interpolation segment.
pub fn has_interpolation(s: &str) -> bool {
    has_interpolation_with_mode(s, InterpolationMode::Braces)
}

/// Check whether a string contains at least one interpolation segment for the mode.
pub fn has_interpolation_with_mode(s: &str, mode: InterpolationMode) -> bool {
    let mut chars = s.chars().peekable();
    while let Some(ch) = chars.next() {
        // Skip backslash-escaped braces
        if ch == '\\' && matches!(chars.peek(), Some(&'{') | Some(&'}')) {
            chars.next();
            continue;
        }
        match mode {
            InterpolationMode::Braces => {
                if ch == '{' {
                    if chars.peek() != Some(&'{') {
                        return true;
                    }
                    chars.next();
                }
            }
            InterpolationMode::Dollar | InterpolationMode::Hash => {
                let sigil = mode.sigil().expect("sigil mode must provide sigil");
                if ch == sigil && chars.peek() == Some(&'{') {
                    return true;
                }
            }
        }
    }
    false
}

/// Split interpolation content `expr[:spec]` at the top-level format separator.
///
/// This preserves `::` (enum/type separators) and ignores separators inside
/// nested delimiters/strings.
pub fn split_expression_and_format_spec(
    raw: &str,
) -> Result<(String, Option<InterpolationFormatSpec>)> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return Err(ShapeError::RuntimeError {
            message: "Empty expression in interpolation".to_string(),
            location: None,
        });
    }

    let split_at = find_top_level_format_colon(trimmed);

    if let Some(idx) = split_at {
        let expr = trimmed[..idx].trim();
        let spec = trimmed[idx + 1..].trim();
        if expr.is_empty() {
            return Err(ShapeError::RuntimeError {
                message: "Missing expression before format spec in interpolation".to_string(),
                location: None,
            });
        }
        if spec.is_empty() {
            return Err(ShapeError::RuntimeError {
                message: "Missing format spec after ':' in interpolation".to_string(),
                location: None,
            });
        }
        Ok((expr.to_string(), Some(parse_format_spec(spec)?)))
    } else {
        Ok((trimmed.to_string(), None))
    }
}

/// Find the top-level format-separator `:` in an interpolation expression.
///
/// Returns the byte index of the separator if present.
pub fn find_top_level_format_colon(raw: &str) -> Option<usize> {
    let bytes = raw.as_bytes();
    let mut paren_depth = 0usize;
    let mut brace_depth = 0usize;
    let mut bracket_depth = 0usize;
    let mut in_string: Option<char> = None;
    let mut escaped = false;

    for (idx, ch) in raw.char_indices() {
        if let Some(quote) = in_string {
            if escaped {
                escaped = false;
                continue;
            }
            if ch == '\\' {
                escaped = true;
                continue;
            }
            if ch == quote {
                in_string = None;
            }
            continue;
        }

        match ch {
            '"' | '\'' => in_string = Some(ch),
            '(' => paren_depth += 1,
            ')' => paren_depth = paren_depth.saturating_sub(1),
            '{' => brace_depth += 1,
            '}' => brace_depth = brace_depth.saturating_sub(1),
            '[' => bracket_depth += 1,
            ']' => bracket_depth = bracket_depth.saturating_sub(1),
            ':' if paren_depth == 0 && brace_depth == 0 && bracket_depth == 0 => {
                let prev_is_colon = idx > 0 && bytes[idx - 1] == b':';
                let next_is_colon = idx + 1 < bytes.len() && bytes[idx + 1] == b':';
                if !prev_is_colon && !next_is_colon {
                    return Some(idx);
                }
            }
            _ => {}
        }
    }

    None
}

fn parse_format_spec(raw_spec: &str) -> Result<InterpolationFormatSpec> {
    let spec = raw_spec.trim();

    // Legacy shorthand kept as a parser alias, but normalized into typed format.
    if let Some(precision) = parse_legacy_fixed_precision(spec)? {
        return Ok(InterpolationFormatSpec::Fixed { precision });
    }

    if let Some(inner) = parse_call_like_spec(spec, "fixed")? {
        let precision = parse_u8_value(inner.trim(), "fixed precision")?;
        return Ok(InterpolationFormatSpec::Fixed { precision });
    }

    if let Some(inner) = parse_call_like_spec(spec, "table")? {
        return Ok(InterpolationFormatSpec::Table(parse_table_format_spec(
            inner,
        )?));
    }

    // R8 W4 W18.4 (supervisor 2026-05-24 D1): try content-styling syntax
    // (`bold` / `red` / `fg(blue)` / `bold,red` / etc.). The content-style
    // parser is permissive about composition (multiple comma-separated
    // entries) and surfaces a descriptive error if no entry matches. Any
    // successful parse here triggers the syntax-determined `string` →
    // `content` flip at the f-string return-type inference site.
    let content_spec = parse_content_format_spec(spec).map_err(|err| ShapeError::RuntimeError {
        message: format!(
            "Unsupported interpolation format spec '{}'. Supported: fixed(N), table(...), content-styling (bold, italic, underline, dim, fg(color), bg(color), border(style), align(side), chart(type)). Inner error: {}",
            spec, err
        ),
        location: None,
    })?;
    Ok(InterpolationFormatSpec::ContentStyle(content_spec))
}

fn parse_legacy_fixed_precision(spec: &str) -> Result<Option<u8>> {
    if let Some(rest) = spec.strip_prefix('.') {
        let digits = rest.strip_suffix('f').unwrap_or(rest);
        if digits.is_empty() {
            return Err(ShapeError::RuntimeError {
                message: "Legacy fixed format requires digits after '.'".to_string(),
                location: None,
            });
        }
        if digits.chars().all(|c| c.is_ascii_digit()) {
            return Ok(Some(parse_u8_value(digits, "fixed precision")?));
        }
    }
    Ok(None)
}

fn parse_call_like_spec<'a>(spec: &'a str, name: &str) -> Result<Option<&'a str>> {
    if !spec.starts_with(name) {
        return Ok(None);
    }

    let rest = &spec[name.len()..];
    if !rest.starts_with('(') || !rest.ends_with(')') {
        return Err(ShapeError::RuntimeError {
            message: format!("Format spec '{}' must use call syntax: {}(...)", spec, name),
            location: None,
        });
    }

    Ok(Some(&rest[1..rest.len() - 1]))
}

fn parse_table_format_spec(inner: &str) -> Result<TableFormatSpec> {
    let mut spec = TableFormatSpec::default();
    let trimmed = inner.trim();

    if trimmed.is_empty() {
        return Ok(spec);
    }

    for entry in split_top_level_commas(trimmed)? {
        let entry = entry.trim();
        if entry.is_empty() {
            continue;
        }

        let (key, value) = entry
            .split_once('=')
            .ok_or_else(|| ShapeError::RuntimeError {
                message: format!(
                    "Invalid table format argument '{}'. Expected key=value pairs.",
                    entry
                ),
                location: None,
            })?;
        let key = key.trim();
        let value = value.trim();

        match key {
            "max_rows" => {
                spec.max_rows = Some(parse_usize_value(value, "max_rows")?);
            }
            "align" => {
                spec.align = Some(FormatAlignment::parse(value).ok_or_else(|| {
                    ShapeError::RuntimeError {
                        message: format!(
                            "Invalid align value '{}'. Expected: left, center, right.",
                            value
                        ),
                        location: None,
                    }
                })?);
            }
            "precision" => {
                spec.precision = Some(parse_u8_value(value, "precision")?);
            }
            "color" => {
                spec.color = Some(FormatColor::parse(value).ok_or_else(|| {
                    ShapeError::RuntimeError {
                        message: format!(
                            "Invalid color value '{}'. Expected: default, red, green, yellow, blue, magenta, cyan, white.",
                            value
                        ),
                        location: None,
                    }
                })?);
            }
            "border" => {
                spec.border = parse_on_off(value)?;
            }
            other => {
                return Err(ShapeError::RuntimeError {
                    message: format!(
                        "Unknown table format key '{}'. Supported: max_rows, align, precision, color, border.",
                        other
                    ),
                    location: None,
                });
            }
        }
    }

    Ok(spec)
}

fn split_top_level_commas(s: &str) -> Result<Vec<&str>> {
    let mut parts = Vec::new();
    let mut start = 0usize;
    let mut paren_depth = 0usize;
    let mut brace_depth = 0usize;
    let mut bracket_depth = 0usize;
    let mut in_string: Option<char> = None;
    let mut escaped = false;

    for (idx, ch) in s.char_indices() {
        if let Some(quote) = in_string {
            if escaped {
                escaped = false;
                continue;
            }
            if ch == '\\' {
                escaped = true;
                continue;
            }
            if ch == quote {
                in_string = None;
            }
            continue;
        }

        match ch {
            '"' | '\'' => in_string = Some(ch),
            '(' => paren_depth += 1,
            ')' => paren_depth = paren_depth.saturating_sub(1),
            '{' => brace_depth += 1,
            '}' => brace_depth = brace_depth.saturating_sub(1),
            '[' => bracket_depth += 1,
            ']' => bracket_depth = bracket_depth.saturating_sub(1),
            ',' if paren_depth == 0 && brace_depth == 0 && bracket_depth == 0 => {
                parts.push(&s[start..idx]);
                start = idx + 1;
            }
            _ => {}
        }
    }

    if in_string.is_some() || paren_depth != 0 || brace_depth != 0 || bracket_depth != 0 {
        return Err(ShapeError::RuntimeError {
            message: "Unclosed delimiter in table format spec".to_string(),
            location: None,
        });
    }

    parts.push(&s[start..]);
    Ok(parts)
}

fn parse_u8_value(value: &str, label: &str) -> Result<u8> {
    value.parse::<u8>().map_err(|_| ShapeError::RuntimeError {
        message: format!(
            "Invalid {} '{}'. Expected an integer in range 0..=255.",
            label, value
        ),
        location: None,
    })
}

fn parse_usize_value(value: &str, label: &str) -> Result<usize> {
    value
        .parse::<usize>()
        .map_err(|_| ShapeError::RuntimeError {
            message: format!(
                "Invalid {} '{}'. Expected a non-negative integer.",
                label, value
            ),
            location: None,
        })
}

fn parse_on_off(value: &str) -> Result<bool> {
    match value {
        "on" => Ok(true),
        "off" => Ok(false),
        _ => Err(ShapeError::RuntimeError {
            message: format!("Invalid border value '{}'. Expected on or off.", value),
            location: None,
        }),
    }
}

fn parse_expression_content(chars: &mut std::iter::Peekable<std::str::Chars>) -> Result<String> {
    let mut expr = String::new();
    let mut brace_depth = 1usize;

    while let Some(ch) = chars.next() {
        match ch {
            '{' => {
                brace_depth += 1;
                expr.push(ch);
            }
            '}' => {
                brace_depth = brace_depth.saturating_sub(1);
                if brace_depth == 0 {
                    return if expr.trim().is_empty() {
                        Err(ShapeError::RuntimeError {
                            message: "Empty expression in interpolation".to_string(),
                            location: None,
                        })
                    } else {
                        Ok(expr)
                    };
                }
                expr.push(ch);
            }
            '"' => {
                expr.push(ch);
                while let Some(c) = chars.next() {
                    expr.push(c);
                    if c == '"' {
                        break;
                    }
                    if c == '\\' {
                        if let Some(escaped) = chars.next() {
                            expr.push(escaped);
                        }
                    }
                }
            }
            '\'' => {
                expr.push(ch);
                while let Some(c) = chars.next() {
                    expr.push(c);
                    if c == '\'' {
                        break;
                    }
                    if c == '\\' {
                        if let Some(escaped) = chars.next() {
                            expr.push(escaped);
                        }
                    }
                }
            }
            _ => expr.push(ch),
        }
    }

    Err(ShapeError::RuntimeError {
        message: "Unclosed interpolation (missing })".to_string(),
        location: None,
    })
}

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

    #[test]
    fn parse_basic_interpolation() {
        let parts = parse_interpolation("value: {x}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "value: "));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "x"
        ));
    }

    #[test]
    fn parse_format_spec() {
        let parts = parse_interpolation("px={price:fixed(2)}").unwrap();
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: Some(spec)
            } if expr == "price" && *spec == InterpolationFormatSpec::Fixed { precision: 2 }
        ));
    }

    #[test]
    fn parse_legacy_fixed_precision_alias() {
        let parts = parse_interpolation("px={price:.2f}").unwrap();
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: Some(spec)
            } if expr == "price" && *spec == InterpolationFormatSpec::Fixed { precision: 2 }
        ));
    }

    #[test]
    fn parse_table_format_spec() {
        let parts = parse_interpolation(
            "rows={dt:table(max_rows=5, align=right, precision=2, color=green, border=off)}",
        )
        .unwrap();

        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: Some(InterpolationFormatSpec::Table(TableFormatSpec {
                    max_rows: Some(5),
                    align: Some(FormatAlignment::Right),
                    precision: Some(2),
                    color: Some(FormatColor::Green),
                    border: false
                }))
            } if expr == "dt"
        ));
    }

    #[test]
    fn parse_table_format_unknown_key_errors() {
        let err = parse_interpolation("rows={dt:table(foo=1)}").unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("Unknown table format key"),
            "unexpected error: {}",
            msg
        );
    }

    #[test]
    fn parse_double_colon_is_not_format_spec() {
        let parts = parse_interpolation("{Type::Variant}").unwrap();
        assert!(matches!(
            &parts[0],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "Type::Variant"
        ));
    }

    #[test]
    fn escaped_braces_do_not_count_as_interpolation() {
        assert!(!has_interpolation("Use {{x}} for literal"));
        assert!(has_interpolation("Use {x} for value"));
    }

    #[test]
    fn parse_dollar_interpolation() {
        let parts = parse_interpolation_with_mode(
            "json={\"name\": ${user.name}}",
            InterpolationMode::Dollar,
        )
        .unwrap();
        assert_eq!(parts.len(), 3);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "json={\"name\": "));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "user.name"
        ));
        assert!(matches!(&parts[2], InterpolationPart::Literal(s) if s == "}"));
    }

    #[test]
    fn parse_hash_interpolation() {
        let parts = parse_interpolation_with_mode("echo #{cmd}", InterpolationMode::Hash).unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "echo "));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "cmd"
        ));
    }

    #[test]
    fn escaped_sigil_opener_is_literal_in_sigil_modes() {
        let parts =
            parse_interpolation_with_mode("literal $${x}", InterpolationMode::Dollar).unwrap();
        assert_eq!(parts.len(), 1);
        assert!(matches!(
            &parts[0],
            InterpolationPart::Literal(s) if s == "literal ${x}"
        ));
    }

    #[test]
    fn braces_are_plain_text_in_sigil_mode() {
        assert!(!has_interpolation_with_mode(
            "{\"a\": 1}",
            InterpolationMode::Dollar
        ));
        assert!(has_interpolation_with_mode(
            "${x}",
            InterpolationMode::Dollar
        ));
    }

    // --- LOW-2: backslash-escaped braces in interpolation ---

    #[test]
    fn backslash_escaped_braces_produce_literal_text() {
        // `\{` and `\}` should produce literal `{` and `}`, not interpolation.
        let parts = parse_interpolation("hello \\{world\\}").unwrap();
        assert_eq!(parts.len(), 1);
        assert!(matches!(
            &parts[0],
            InterpolationPart::Literal(s) if s == "hello {world}"
        ));
    }

    #[test]
    fn backslash_escaped_braces_not_counted_as_interpolation() {
        assert!(!has_interpolation("hello \\{world\\}"));
        assert!(has_interpolation("hello {world}"));
    }

    #[test]
    fn backslash_escaped_braces_mixed_with_real_interpolation() {
        // `\{literal\} and {expr}` → Literal("{literal} and "), Expression("expr")
        let parts = parse_interpolation("\\{literal\\} and {expr}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(
            &parts[0],
            InterpolationPart::Literal(s) if s == "{literal} and "
        ));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression { expr, .. } if expr == "expr"
        ));
    }

    // --- R8 W4 W18.4: content-styling f-string syntax ---

    #[test]
    fn parse_content_style_bold() {
        use crate::content_style::ContentFormatSpec;
        let parts = parse_interpolation("{x:bold}").unwrap();
        assert_eq!(parts.len(), 1);
        match &parts[0] {
            InterpolationPart::Expression {
                expr,
                format_spec: Some(InterpolationFormatSpec::ContentStyle(spec)),
            } => {
                assert_eq!(expr, "x");
                let expected = ContentFormatSpec {
                    bold: true,
                    ..Default::default()
                };
                assert_eq!(spec, &expected);
            }
            other => panic!("expected ContentStyle bold, got {:?}", other),
        }
    }

    #[test]
    fn parse_content_style_bold_red_canonical() {
        use crate::content_style::{ColorSpec, NamedContentColor};
        // Canonical W18.4 example from supervisor: `{x:bold,red}` parses to
        // ContentStyle with bold=true and fg=Named(Red).
        let parts = parse_interpolation("hello {name:bold,red}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "hello "));
        match &parts[1] {
            InterpolationPart::Expression {
                expr,
                format_spec: Some(InterpolationFormatSpec::ContentStyle(spec)),
            } => {
                assert_eq!(expr, "name");
                assert!(spec.bold);
                assert_eq!(spec.fg, Some(ColorSpec::Named(NamedContentColor::Red)));
            }
            other => panic!("expected ContentStyle, got {:?}", other),
        }
    }

    #[test]
    fn parse_content_style_fg_call() {
        use crate::content_style::{ColorSpec, NamedContentColor};
        let parts = parse_interpolation("{x:fg(green)}").unwrap();
        match &parts[0] {
            InterpolationPart::Expression {
                format_spec: Some(InterpolationFormatSpec::ContentStyle(spec)),
                ..
            } => {
                assert_eq!(spec.fg, Some(ColorSpec::Named(NamedContentColor::Green)));
            }
            other => panic!("expected ContentStyle, got {:?}", other),
        }
    }

    #[test]
    fn fixed_and_table_still_parse_separately_from_content_style() {
        // Regression: fixed(N) must not be reinterpreted as content-style.
        let parts = parse_interpolation("{x:fixed(2)}").unwrap();
        match &parts[0] {
            InterpolationPart::Expression {
                format_spec: Some(InterpolationFormatSpec::Fixed { precision }),
                ..
            } => {
                assert_eq!(*precision, 2);
            }
            other => panic!("expected Fixed, got {:?}", other),
        }

        let parts = parse_interpolation("{x:table()}").unwrap();
        assert!(matches!(
            &parts[0],
            InterpolationPart::Expression {
                format_spec: Some(InterpolationFormatSpec::Table(_)),
                ..
            }
        ));
    }
}