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
//! Content styling spec types — shared module per supervisor 2026-05-24
//! disposition (a-modified) REVIVE-WITH-SHARED-MODULE.
//!
//! This module hosts the f-string / builder-API content styling spec types
//! revived from pre-W18.3 (`git show 19de5ef2^:crates/shape-ast/src/
//! interpolation.rs`). Both W18.4 (f-string styling parser/lowering) and
//! W18.5 (`Content.table` / `Content.code` / `Content.kv` builder API)
//! consume these types from this single shared module — no parallel
//! implementations per CLAUDE.md §Parallel-implementation.
//!
//! Module placement rationale: the supervisor's preferred location was
//! `crates/shape-value/src/content_style.rs` (next to `ContentNode`). The
//! dependency graph forbids that placement: `shape-ast` cannot import from
//! `shape-value` (shape-value depends on shape-ast, not the reverse), and
//! the parsers MUST live in shape-ast because the f-string interpolation
//! parser produces typed `InterpolationFormatSpec::ContentStyle(spec)`
//! parts during AST parsing. Placing the types in shape-ast satisfies the
//! "one source of truth" constraint while preserving the workspace
//! dependency invariant. shape-vm's compiler lowering and
//! shape-runtime's W18.5 builders both import from `shape_ast::
//! content_style::*`.
//!
//! The spec types here are SYNTACTIC descriptors — they describe what the
//! user wrote in `f"{x:bold,red}"` or `Content.table(...).border(rounded)`.
//! They are NOT runtime `ContentNode`/`Style`/`Color` types from
//! `shape_value::content`. Conversion happens at the lowering boundary.

use crate::{Result, ShapeError};

// =============================================================================
// SPEC TYPES (revived from 19de5ef2^ — W18.3 deletion source)
// =============================================================================

/// Content-string format specification for rich terminal/HTML output.
///
/// Parsed from `:bold,red` / `:fg(green),bg(blue)` / `:border:rounded` style
/// syntax in f-string interpolations, and constructed by builder methods
/// (`Content.table(...).border(rounded)`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContentFormatSpec {
    pub fg: Option<ColorSpec>,
    pub bg: Option<ColorSpec>,
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub dim: bool,
    pub fixed_precision: Option<u8>,
    pub border: Option<BorderStyleSpec>,
    pub max_rows: Option<usize>,
    pub align: Option<AlignSpec>,
    /// Chart type hint: render the value as a chart instead of text.
    pub chart_type: Option<ChartTypeSpec>,
    /// Column name to use as x-axis data.
    pub x_column: Option<String>,
    /// Column names to use as y-axis series.
    pub y_columns: Vec<String>,
}

impl Default for ContentFormatSpec {
    fn default() -> Self {
        Self {
            fg: None,
            bg: None,
            bold: false,
            italic: false,
            underline: false,
            dim: false,
            fixed_precision: None,
            border: None,
            max_rows: None,
            align: None,
            chart_type: None,
            x_column: None,
            y_columns: vec![],
        }
    }
}

/// Color specification for content strings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColorSpec {
    Named(NamedContentColor),
    Rgb(u8, u8, u8),
}

/// Named colors for content strings.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NamedContentColor {
    Red,
    Green,
    Blue,
    Yellow,
    Magenta,
    Cyan,
    White,
    Default,
}

/// Border style for content-string table rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BorderStyleSpec {
    Rounded,
    Sharp,
    Heavy,
    Double,
    Minimal,
    None,
}

/// Alignment for content-string rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlignSpec {
    Left,
    Center,
    Right,
}

/// Chart type hint for content format spec.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChartTypeSpec {
    Line,
    Bar,
    Scatter,
    Area,
    Histogram,
}

// =============================================================================
// PARSERS (revived from 19de5ef2^)
// =============================================================================

/// Parse a content-string format spec like `"fg(red), bold, fixed(2)"`.
///
/// Used by both the f-string interpolation parser (when a `:spec` part is
/// recognised as content-styling syntax) and any builder-API path that
/// accepts a stringly-typed style descriptor.
pub fn parse_content_format_spec(raw_spec: &str) -> Result<ContentFormatSpec> {
    let mut spec = ContentFormatSpec::default();
    let trimmed = raw_spec.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;
        }

        // Boolean flags (no parens)
        match entry {
            "bold" => {
                spec.bold = true;
                continue;
            }
            "italic" => {
                spec.italic = true;
                continue;
            }
            "underline" => {
                spec.underline = true;
                continue;
            }
            "dim" => {
                spec.dim = true;
                continue;
            }
            _ => {}
        }

        // Named-color shorthand: `red` / `green` / ... at top level → fg(named)
        if let Ok(color) = parse_color_spec(entry) {
            spec.fg = Some(color);
            continue;
        }

        // Call-like specs: fg(...), bg(...), fixed(...), border(...), max_rows(...), align(...)
        if let Some(idx) = entry.find('(') {
            if !entry.ends_with(')') {
                return Err(ShapeError::RuntimeError {
                    message: format!("Unclosed parenthesis in content format spec '{}'", entry),
                    location: None,
                });
            }
            let key = entry[..idx].trim();
            let inner = entry[idx + 1..entry.len() - 1].trim();
            match key {
                "fg" => {
                    spec.fg = Some(parse_color_spec(inner)?);
                }
                "bg" => {
                    spec.bg = Some(parse_color_spec(inner)?);
                }
                "fixed" => {
                    spec.fixed_precision = Some(parse_u8_value(inner, "fixed precision")?);
                }
                "border" => {
                    spec.border = Some(parse_border_style_spec(inner)?);
                }
                "max_rows" => {
                    spec.max_rows = Some(parse_usize_value(inner, "max_rows")?);
                }
                "align" => {
                    spec.align = Some(parse_align_spec(inner)?);
                }
                "chart" => {
                    spec.chart_type = Some(parse_chart_type_spec(inner)?);
                }
                "x" => {
                    spec.x_column = Some(inner.to_string());
                }
                "y" => {
                    // y(col) or y(col1, col2, ...)
                    spec.y_columns = inner
                        .split(',')
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty())
                        .collect();
                }
                other => {
                    return Err(ShapeError::RuntimeError {
                        message: format!(
                            "Unknown content format key '{}'. Supported: fg, bg, bold, italic, underline, dim, fixed, border, max_rows, align, chart, x, y.",
                            other
                        ),
                        location: None,
                    });
                }
            }
            continue;
        }

        return Err(ShapeError::RuntimeError {
            message: format!(
                "Unknown content format entry '{}'. Expected a flag (bold, italic, ...) or key(value).",
                entry
            ),
            location: None,
        });
    }

    Ok(spec)
}

pub fn parse_color_spec(s: &str) -> Result<ColorSpec> {
    let s = s.trim();
    // Try RGB: rgb(r, g, b)
    if s.starts_with("rgb(") && s.ends_with(')') {
        let inner = &s[4..s.len() - 1];
        let parts: Vec<&str> = inner.split(',').map(|p| p.trim()).collect();
        if parts.len() != 3 {
            return Err(ShapeError::RuntimeError {
                message: format!("rgb() expects 3 values, got {}", parts.len()),
                location: None,
            });
        }
        let r = parse_u8_value(parts[0], "red")?;
        let g = parse_u8_value(parts[1], "green")?;
        let b = parse_u8_value(parts[2], "blue")?;
        return Ok(ColorSpec::Rgb(r, g, b));
    }
    // Named color
    match s {
        "red" => Ok(ColorSpec::Named(NamedContentColor::Red)),
        "green" => Ok(ColorSpec::Named(NamedContentColor::Green)),
        "blue" => Ok(ColorSpec::Named(NamedContentColor::Blue)),
        "yellow" => Ok(ColorSpec::Named(NamedContentColor::Yellow)),
        "magenta" => Ok(ColorSpec::Named(NamedContentColor::Magenta)),
        "cyan" => Ok(ColorSpec::Named(NamedContentColor::Cyan)),
        "white" => Ok(ColorSpec::Named(NamedContentColor::White)),
        "default" => Ok(ColorSpec::Named(NamedContentColor::Default)),
        _ => Err(ShapeError::RuntimeError {
            message: format!(
                "Unknown color '{}'. Expected: red, green, blue, yellow, magenta, cyan, white, default, or rgb(r,g,b).",
                s
            ),
            location: None,
        }),
    }
}

pub fn parse_border_style_spec(s: &str) -> Result<BorderStyleSpec> {
    match s.trim() {
        "rounded" => Ok(BorderStyleSpec::Rounded),
        "sharp" => Ok(BorderStyleSpec::Sharp),
        "heavy" => Ok(BorderStyleSpec::Heavy),
        "double" => Ok(BorderStyleSpec::Double),
        "minimal" => Ok(BorderStyleSpec::Minimal),
        "none" => Ok(BorderStyleSpec::None),
        _ => Err(ShapeError::RuntimeError {
            message: format!(
                "Unknown border style '{}'. Expected: rounded, sharp, heavy, double, minimal, none.",
                s
            ),
            location: None,
        }),
    }
}

pub fn parse_align_spec(s: &str) -> Result<AlignSpec> {
    match s.trim() {
        "left" => Ok(AlignSpec::Left),
        "center" => Ok(AlignSpec::Center),
        "right" => Ok(AlignSpec::Right),
        _ => Err(ShapeError::RuntimeError {
            message: format!(
                "Unknown align value '{}'. Expected: left, center, right.",
                s
            ),
            location: None,
        }),
    }
}

pub fn parse_chart_type_spec(s: &str) -> Result<ChartTypeSpec> {
    match s.trim().to_lowercase().as_str() {
        "line" => Ok(ChartTypeSpec::Line),
        "bar" => Ok(ChartTypeSpec::Bar),
        "scatter" => Ok(ChartTypeSpec::Scatter),
        "area" => Ok(ChartTypeSpec::Area),
        "histogram" => Ok(ChartTypeSpec::Histogram),
        _ => Err(ShapeError::RuntimeError {
            message: format!(
                "Unknown chart type '{}'. Expected: line, bar, scatter, area, histogram.",
                s
            ),
            location: None,
        }),
    }
}

// =============================================================================
// SHARED PARSE HELPERS (used by parse_content_format_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 content 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,
        })
}

// =============================================================================
// TESTS
// =============================================================================

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

    #[test]
    fn parse_content_format_spec_bold() {
        let spec = parse_content_format_spec("bold").unwrap();
        assert!(spec.bold);
        assert!(!spec.italic);
    }

    #[test]
    fn parse_content_format_spec_multiple_flags() {
        let spec = parse_content_format_spec("bold, italic, underline").unwrap();
        assert!(spec.bold);
        assert!(spec.italic);
        assert!(spec.underline);
        assert!(!spec.dim);
    }

    #[test]
    fn parse_content_format_spec_fg_named() {
        let spec = parse_content_format_spec("fg(red)").unwrap();
        assert_eq!(spec.fg, Some(ColorSpec::Named(NamedContentColor::Red)));
    }

    #[test]
    fn parse_content_format_spec_fg_rgb() {
        let spec = parse_content_format_spec("fg(rgb(255, 128, 0))").unwrap();
        assert_eq!(spec.fg, Some(ColorSpec::Rgb(255, 128, 0)));
    }

    #[test]
    fn parse_content_format_spec_full() {
        let spec = parse_content_format_spec(
            "fg(green), bg(blue), bold, fixed(2), border(rounded), align(center)",
        )
        .unwrap();
        assert_eq!(spec.fg, Some(ColorSpec::Named(NamedContentColor::Green)));
        assert_eq!(spec.bg, Some(ColorSpec::Named(NamedContentColor::Blue)));
        assert!(spec.bold);
        assert_eq!(spec.fixed_precision, Some(2));
        assert_eq!(spec.border, Some(BorderStyleSpec::Rounded));
        assert_eq!(spec.align, Some(AlignSpec::Center));
    }

    #[test]
    fn parse_content_format_spec_unknown_key_errors() {
        let err = parse_content_format_spec("foo(bar)").unwrap_err();
        assert!(err.to_string().contains("Unknown content format key"));
    }

    #[test]
    fn parse_content_format_spec_chart_type() {
        let spec = parse_content_format_spec("chart(bar)").unwrap();
        assert_eq!(spec.chart_type, Some(ChartTypeSpec::Bar));
    }

    #[test]
    fn parse_content_format_spec_chart_with_axes() {
        let spec = parse_content_format_spec("chart(line), x(month), y(revenue, profit)").unwrap();
        assert_eq!(spec.chart_type, Some(ChartTypeSpec::Line));
        assert_eq!(spec.x_column, Some("month".to_string()));
        assert_eq!(spec.y_columns, vec!["revenue", "profit"]);
    }

    #[test]
    fn parse_content_format_spec_chart_invalid_type() {
        let err = parse_content_format_spec("chart(pie)").unwrap_err();
        assert!(err.to_string().contains("Unknown chart type"));
    }

    #[test]
    fn parse_color_spec_named() {
        assert_eq!(
            parse_color_spec("red").unwrap(),
            ColorSpec::Named(NamedContentColor::Red)
        );
        assert_eq!(
            parse_color_spec("default").unwrap(),
            ColorSpec::Named(NamedContentColor::Default)
        );
    }

    #[test]
    fn parse_color_spec_rgb() {
        assert_eq!(
            parse_color_spec("rgb(10, 20, 30)").unwrap(),
            ColorSpec::Rgb(10, 20, 30)
        );
    }

    #[test]
    fn parse_color_spec_invalid() {
        assert!(parse_color_spec("octarine").is_err());
        assert!(parse_color_spec("rgb(1, 2)").is_err());
        assert!(parse_color_spec("rgb(300, 0, 0)").is_err());
    }

    #[test]
    fn parse_border_style_all() {
        assert_eq!(parse_border_style_spec("rounded").unwrap(), BorderStyleSpec::Rounded);
        assert_eq!(parse_border_style_spec("sharp").unwrap(), BorderStyleSpec::Sharp);
        assert_eq!(parse_border_style_spec("heavy").unwrap(), BorderStyleSpec::Heavy);
        assert_eq!(parse_border_style_spec("double").unwrap(), BorderStyleSpec::Double);
        assert_eq!(parse_border_style_spec("minimal").unwrap(), BorderStyleSpec::Minimal);
        assert_eq!(parse_border_style_spec("none").unwrap(), BorderStyleSpec::None);
        assert!(parse_border_style_spec("triple").is_err());
    }

    #[test]
    fn parse_align_all() {
        assert_eq!(parse_align_spec("left").unwrap(), AlignSpec::Left);
        assert_eq!(parse_align_spec("center").unwrap(), AlignSpec::Center);
        assert_eq!(parse_align_spec("right").unwrap(), AlignSpec::Right);
        assert!(parse_align_spec("justify").is_err());
    }

    #[test]
    fn parse_chart_type_all() {
        assert_eq!(parse_chart_type_spec("line").unwrap(), ChartTypeSpec::Line);
        assert_eq!(parse_chart_type_spec("bar").unwrap(), ChartTypeSpec::Bar);
        assert_eq!(parse_chart_type_spec("scatter").unwrap(), ChartTypeSpec::Scatter);
        assert_eq!(parse_chart_type_spec("area").unwrap(), ChartTypeSpec::Area);
        assert_eq!(parse_chart_type_spec("histogram").unwrap(), ChartTypeSpec::Histogram);
        assert!(parse_chart_type_spec("pie").is_err());
    }

    #[test]
    fn parse_content_format_spec_top_level_color_shorthand() {
        // `{x:red}` → ColorSpec::Named(Red) as fg
        let spec = parse_content_format_spec("red").unwrap();
        assert_eq!(spec.fg, Some(ColorSpec::Named(NamedContentColor::Red)));
    }

    #[test]
    fn parse_content_format_spec_bold_red_shorthand() {
        // `{x:bold,red}` → bold + fg(red) — the canonical W18.4 example
        let spec = parse_content_format_spec("bold, red").unwrap();
        assert!(spec.bold);
        assert_eq!(spec.fg, Some(ColorSpec::Named(NamedContentColor::Red)));
    }
}