merman-core 0.8.0-alpha.3

Mermaid parser + semantic model (headless; parity-focused).
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
use crate::diagrams::scan::starts_with_case_insensitive;
use crate::{
    EditorExpectedSyntax, EditorExpectedSyntaxKind, EditorSemanticFacts, EditorSemanticKind,
    EditorSemanticSymbol, Error, ParseMetadata, Result, SourceSpan,
};
use serde_json::{Value, json};
use std::collections::HashSet;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
pub struct PieDiagramRenderModel {
    #[serde(rename = "showData")]
    pub show_data: bool,
    pub title: Option<String>,
    #[serde(rename = "accTitle")]
    pub acc_title: Option<String>,
    #[serde(rename = "accDescr")]
    pub acc_descr: Option<String>,
    pub sections: Vec<PieRenderSection>,
}

impl PieDiagramRenderModel {
    pub(crate) fn sanitize_common_db_fields(&mut self, config: &crate::MermaidConfig) {
        crate::common_db::sanitize_optional_title(&mut self.title, config);
        crate::common_db::sanitize_optional_acc_title(&mut self.acc_title, config);
        crate::common_db::sanitize_optional_acc_descr(&mut self.acc_descr, config);
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PieRenderSection {
    pub label: String,
    pub value: f64,
}

enum PieParseOutput {
    Empty,
    ExpectedPie,
    Model(PieDiagramRenderModel),
}

pub fn parse_pie(code: &str, meta: &ParseMetadata) -> Result<Value> {
    match parse_pie_model(code, meta)? {
        PieParseOutput::Empty => Ok(json!({})),
        PieParseOutput::ExpectedPie => Ok(json!({ "error": "expected pie" })),
        PieParseOutput::Model(model) => Ok(json!({
            "type": meta.diagram_type,
            "showData": model.show_data,
            "title": model.title,
            "accTitle": model.acc_title,
            "accDescr": model.acc_descr,
            "sections": model.sections,
        })),
    }
}

pub fn parse_pie_model_for_render(
    code: &str,
    meta: &ParseMetadata,
) -> Result<PieDiagramRenderModel> {
    match parse_pie_model(code, meta)? {
        PieParseOutput::Empty => Ok(PieDiagramRenderModel::default()),
        PieParseOutput::ExpectedPie => Err(Error::diagram_parse_fallback(
            meta.diagram_type.clone(),
            "expected pie".to_string(),
        )),
        PieParseOutput::Model(model) => Ok(model),
    }
}

pub fn parse_pie_editor_facts(code: &str, _meta: &ParseMetadata) -> EditorSemanticFacts {
    let mut facts = EditorSemanticFacts::new();
    let mut raw_lines = code.split_inclusive('\n').peekable();
    let mut offset = 0usize;
    let mut header_seen = false;

    while let Some(segment) = raw_lines.next() {
        let line_start = offset;
        offset += segment.len();
        let line = segment.strip_suffix('\n').unwrap_or(segment);
        let trimmed = strip_inline_comment(line).trim();
        if trimmed.is_empty() {
            continue;
        }

        if !header_seen {
            if starts_with_case_insensitive(trimmed, "pie") {
                header_seen = true;
                if let Some(rest) = trimmed.strip_prefix("pie") {
                    let rest = rest.trim_start();
                    if !rest.is_empty() {
                        facts.push_directive_prefix("showData");
                        if rest.starts_with("showData") {
                            facts.push_expected_syntax(EditorExpectedSyntax::new(
                                EditorExpectedSyntaxKind::Payload,
                                SourceSpan::new(
                                    line_start + line.find("showData").unwrap_or(0),
                                    line_start
                                        + line.find("showData").unwrap_or(0)
                                        + "showData".len(),
                                ),
                            ));
                        }
                    }
                }
                continue;
            }
            continue;
        }

        if let Some(value) = parse_title_statement_spanned(line, line_start) {
            facts.push_directive_prefix("title");
            push_pie_payload_fact(
                &mut facts,
                value.text,
                value.start,
                "pie title",
                EditorSemanticKind::String,
            );
            continue;
        }
        if let Some(value) = parse_key_value_spanned(line, line_start, "accTitle") {
            facts.push_directive_prefix("accTitle");
            push_pie_payload_fact(
                &mut facts,
                value.text,
                value.start,
                "pie accessibility title",
                EditorSemanticKind::String,
            );
            continue;
        }
        if let Some(value) = parse_acc_descr_inline_spanned(line, line_start) {
            facts.push_directive_prefix("accDescr");
            push_pie_payload_fact(
                &mut facts,
                value.text,
                value.start,
                "pie accessibility description",
                EditorSemanticKind::String,
            );
            continue;
        }
        if let Some(value) = parse_acc_descr_block_spanned(&mut raw_lines, line, line_start) {
            facts.push_directive_prefix("accDescr");
            push_pie_payload_fact(
                &mut facts,
                value.text,
                value.start,
                "pie accessibility description",
                EditorSemanticKind::String,
            );
            continue;
        }

        if let Some((label, value_span)) = parse_section_spanned(line, line_start) {
            facts.push_symbol(EditorSemanticSymbol::outline(
                label.text.to_string(),
                Some("pie section".to_string()),
                EditorSemanticKind::String,
                SourceSpan::new(line_start, line_start + line.len()),
                SourceSpan::new(label.start, label.end),
            ));
            facts.push_expected_syntax(EditorExpectedSyntax::new(
                EditorExpectedSyntaxKind::Payload,
                SourceSpan::new(value_span.start, value_span.end),
            ));
            continue;
        }
    }

    facts
}

fn parse_title_statement_spanned<'a>(line: &'a str, line_start: usize) -> Option<SpannedText<'a>> {
    let t = strip_inline_comment(line).trim_start();
    if !t.starts_with("title") {
        return None;
    }
    let rest = t.strip_prefix("title")?;
    let ws = rest.chars().next()?;
    if !ws.is_whitespace() {
        return None;
    }
    let value = rest.trim_start();
    if value.is_empty() {
        return None;
    }
    let value_rel = line.find(value)?;
    Some(SpannedText {
        text: value,
        start: line_start + value_rel,
        end: line_start + value_rel + value.len(),
    })
}

fn parse_key_value_spanned<'a>(
    line: &'a str,
    line_start: usize,
    key: &str,
) -> Option<SpannedText<'a>> {
    let t = strip_inline_comment(line).trim_start();
    if !t.starts_with(key) {
        return None;
    }
    let rest = t.strip_prefix(key)?.trim_start();
    let rest = rest.strip_prefix(':')?;
    let value = rest.trim();
    if value.is_empty() {
        return None;
    }
    let value_rel = line.find(value)?;
    Some(SpannedText {
        text: value,
        start: line_start + value_rel,
        end: line_start + value_rel + value.len(),
    })
}

fn parse_acc_descr_inline_spanned<'a>(line: &'a str, line_start: usize) -> Option<SpannedText<'a>> {
    let t = strip_inline_comment(line).trim_start();
    if !t.starts_with("accDescr") {
        return None;
    }
    let rest = t.strip_prefix("accDescr")?.trim_start();
    let rest = rest.strip_prefix(':')?;
    let value = rest.trim();
    if value.is_empty() {
        return None;
    }
    let value_rel = line.find(value)?;
    Some(SpannedText {
        text: value,
        start: line_start + value_rel,
        end: line_start + value_rel + value.len(),
    })
}

fn parse_acc_descr_block_spanned<'a>(
    lines: &mut std::iter::Peekable<std::str::SplitInclusive<'a, char>>,
    first_line: &'a str,
    line_start: usize,
) -> Option<SpannedText<'a>> {
    let t = strip_inline_comment(first_line).trim_start();
    if !t.starts_with("accDescr") {
        return None;
    }
    let rest = t.strip_prefix("accDescr")?.trim_start();
    let rest = rest.strip_prefix('{')?;
    if let Some(end) = rest.find('}') {
        let value = rest[..end].trim();
        let value_rel = first_line.find(value)?;
        return Some(SpannedText {
            text: value,
            start: line_start + value_rel,
            end: line_start + value_rel + value.len(),
        });
    }
    let value = rest.trim();
    if value.is_empty() {
        return None;
    }
    let value_rel = first_line.find(value)?;
    let _ = lines.peek();
    Some(SpannedText {
        text: value,
        start: line_start + value_rel,
        end: line_start + value_rel + value.len(),
    })
}

fn parse_section_spanned<'a>(
    line: &'a str,
    line_start: usize,
) -> Option<(SpannedText<'a>, SpannedText<'a>)> {
    let t = strip_inline_comment(line).trim_start();
    let (label, rest) = parse_quoted_string(t)?;
    let rest = rest.trim_start();
    let rest = rest.strip_prefix(':')?.trim_start();

    let mut num = String::new();
    for c in rest.chars() {
        if c.is_ascii_digit() || c == '-' || c == '.' {
            num.push(c);
        } else {
            break;
        }
    }
    if num.is_empty() {
        return None;
    }
    let label_rel = line.find(&label)?;
    let value_rel = line.find(&num)?;
    Some((
        SpannedText {
            text: &line[label_rel..label_rel + label.len()],
            start: line_start + label_rel,
            end: line_start + label_rel + label.len(),
        },
        SpannedText {
            text: &line[value_rel..value_rel + num.len()],
            start: line_start + value_rel,
            end: line_start + value_rel + num.len(),
        },
    ))
}

fn push_pie_payload_fact(
    facts: &mut EditorSemanticFacts,
    text: &str,
    start: usize,
    detail: &'static str,
    kind: EditorSemanticKind,
) {
    let end = start + text.len();
    facts.push_expected_syntax(EditorExpectedSyntax::new(
        EditorExpectedSyntaxKind::Payload,
        SourceSpan::new(start, end),
    ));
    facts.push_symbol(EditorSemanticSymbol::payload(
        text.to_string(),
        Some(detail.to_string()),
        kind,
        SourceSpan::new(start, end),
        SourceSpan::new(start, end),
    ));
}

#[derive(Debug, Clone, Copy)]
struct SpannedText<'a> {
    text: &'a str,
    start: usize,
    end: usize,
}

fn parse_pie_model(code: &str, meta: &ParseMetadata) -> Result<PieParseOutput> {
    let mut raw_lines = code.lines();

    let mut header: Option<String> = None;
    for line in &mut raw_lines {
        let t = strip_inline_comment(line).trim();
        if !t.is_empty() {
            header = Some(t.to_string());
            break;
        }
    }

    let Some(header) = header else {
        return Ok(PieParseOutput::Empty);
    };

    let mut it0 = header.split_whitespace();
    let Some(first) = it0.next() else {
        return Ok(PieParseOutput::Empty);
    };
    if first != "pie" {
        return Ok(PieParseOutput::ExpectedPie);
    }

    let mut show_data = false;
    let mut title: Option<String> = None;
    let mut acc_title: Option<String> = None;
    let mut acc_descr: Option<String> = None;
    let mut unsupported: Option<String> = None;

    fn token_boundary_ok(s: &str, token_len: usize) -> bool {
        let Some(rest) = s.get(token_len..) else {
            return true;
        };
        match rest.chars().next() {
            None => true,
            Some(c) => c.is_whitespace(),
        }
    }

    let header_after = header
        .trim_start_matches(|c: char| c.is_whitespace())
        .strip_prefix("pie")
        .unwrap_or("");
    let mut rest = header_after.trim_start();
    while !rest.is_empty() {
        if rest.starts_with("showData") && token_boundary_ok(rest, "showData".len()) {
            show_data = true;
            rest = rest["showData".len()..].trim_start();
            continue;
        }
        if rest.starts_with("title") && token_boundary_ok(rest, "title".len()) {
            let after = rest["title".len()..].trim_start();
            title = Some(after.to_string());
            rest = "";
            continue;
        }
        if rest.starts_with("accTitle")
            && let Some(v) = parse_key_value(rest, "accTitle")
        {
            acc_title = Some(v);
            rest = "";
            continue;
        }
        if rest.starts_with("accDescr") {
            if let Some(v) = parse_acc_descr_inline(rest) {
                acc_descr = Some(v);
                rest = "";
                continue;
            }
            if starts_acc_descr_block(rest) {
                let mut parts: Vec<String> = Vec::new();
                for next_line in raw_lines.by_ref() {
                    let s = strip_inline_comment(next_line);
                    if s.contains('}') {
                        let before = s.split('}').next().unwrap_or("").trim();
                        if !before.is_empty() {
                            parts.push(before.to_string());
                        }
                        break;
                    }
                    let trimmed = s.trim();
                    if trimmed.is_empty() {
                        continue;
                    }
                    parts.push(trimmed.to_string());
                }
                acc_descr = Some(parts.join("\n"));
                rest = "";
                continue;
            }
        }
        unsupported = Some(rest.split_whitespace().next().unwrap_or(rest).to_string());
        break;
    }

    if let Some(tok) = unsupported {
        return Err(Error::diagram_parse_fallback(
            meta.diagram_type.clone(),
            format!("unexpected pie header token: {tok}"),
        ));
    }

    let mut sections: Vec<PieRenderSection> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();

    let mut lines = raw_lines.peekable();
    while let Some(line) = lines.next() {
        let t = strip_inline_comment(line).trim();
        if t.is_empty() {
            continue;
        }

        if let Some(v) = parse_title_statement(t) {
            title = Some(v);
            continue;
        }

        if let Some(v) = parse_key_value(t, "accTitle") {
            acc_title = Some(v);
            continue;
        }

        if let Some(v) = parse_acc_descr_inline(t) {
            acc_descr = Some(v);
            continue;
        }

        if starts_acc_descr_block(t) {
            let mut parts: Vec<String> = Vec::new();
            for next_line in lines.by_ref() {
                let s = strip_inline_comment(next_line);
                if s.contains('}') {
                    let before = s.split('}').next().unwrap_or("").trim();
                    if !before.is_empty() {
                        parts.push(before.to_string());
                    }
                    break;
                }
                let trimmed = s.trim();
                if trimmed.is_empty() {
                    continue;
                }
                parts.push(trimmed.to_string());
            }
            acc_descr = Some(parts.join("\n"));
            continue;
        }

        if let Some((label, value)) = parse_section(t) {
            if value < 0.0 {
                return Err(Error::diagram_parse_fallback(
                    meta.diagram_type.clone(),
                    format!(
                        "\"{label}\" has invalid value: {value}. Negative values are not allowed in pie charts. All slice values must be >= 0."
                    ),
                ));
            }
            if seen.insert(label.clone()) {
                sections.push(PieRenderSection { label, value });
            }
            continue;
        }

        return Err(Error::diagram_parse_fallback(
            meta.diagram_type.clone(),
            format!("unexpected pie statement: {t}"),
        ));
    }

    Ok(PieParseOutput::Model(PieDiagramRenderModel {
        show_data,
        title,
        acc_title,
        acc_descr,
        sections,
    }))
}

fn strip_inline_comment(line: &str) -> &str {
    match line.find("%%") {
        Some(idx) => &line[..idx],
        None => line,
    }
}

fn parse_title_statement(line: &str) -> Option<String> {
    let t = line.trim_start();
    if !t.starts_with("title") {
        return None;
    }
    let rest = t.strip_prefix("title")?;
    match rest.chars().next() {
        None => Some(String::new()),
        Some(c) if c.is_whitespace() => Some(rest.trim_start().to_string()),
        _ => None,
    }
}

fn parse_key_value(line: &str, key: &str) -> Option<String> {
    let t = line.trim_start();
    if !t.starts_with(key) {
        return None;
    }
    let rest = t.strip_prefix(key)?.trim_start();
    let rest = rest.strip_prefix(':')?.trim_start();
    Some(rest.to_string())
}

fn parse_acc_descr_inline(line: &str) -> Option<String> {
    let t = line.trim_start();
    if !t.starts_with("accDescr") {
        return None;
    }
    let rest = t.strip_prefix("accDescr")?.trim_start();
    if let Some(rest) = rest.strip_prefix(':') {
        return Some(rest.trim_start().to_string());
    }
    None
}

fn starts_acc_descr_block(line: &str) -> bool {
    let t = line.trim_start();
    if !t.starts_with("accDescr") {
        return false;
    }
    let rest = t.trim_start_matches("accDescr").trim_start();
    rest.starts_with('{')
}

fn parse_section(line: &str) -> Option<(String, f64)> {
    let t = line.trim_start();
    let (label, rest) = parse_quoted_string(t)?;
    let rest = rest.trim_start();
    let rest = rest.strip_prefix(':')?.trim_start();

    let mut num = String::new();
    for c in rest.chars() {
        if c.is_ascii_digit() || c == '-' || c == '.' {
            num.push(c);
        } else {
            break;
        }
    }
    if num.is_empty() {
        return None;
    }
    let value: f64 = num.parse().ok()?;
    Some((label, value))
}

fn parse_quoted_string(input: &str) -> Option<(String, &str)> {
    let mut chars = input.chars();
    let quote = chars.next()?;
    if quote != '"' && quote != '\'' {
        return None;
    }
    let mut out = String::new();
    let mut escaped = false;
    let mut idx = 1;
    for c in chars {
        idx += c.len_utf8();
        if escaped {
            out.push(c);
            escaped = false;
            continue;
        }
        if c == '\\' {
            escaped = true;
            continue;
        }
        if c == quote {
            return Some((out, &input[idx..]));
        }
        out.push(c);
    }
    None
}

#[cfg(test)]
mod tests {
    use crate::{Engine, ParseOptions};

    #[test]
    fn pie_supports_title_statement_after_header() {
        let engine = Engine::new();
        let input = r#"
pie showData
  title Market Share
  "A" : 1
  "B" : 2
"#;

        let parsed = engine
            .parse_diagram_sync(input, ParseOptions::strict())
            .unwrap()
            .expect("diagram detected");

        assert_eq!(parsed.meta.diagram_type, "pie");
        assert_eq!(
            parsed.model.get("title").and_then(|v| v.as_str()),
            Some("Market Share")
        );
        assert_eq!(
            parsed.model.get("showData").and_then(|v| v.as_bool()),
            Some(true)
        );
    }

    #[test]
    fn pie_supports_header_acc_title_inline() {
        let engine = Engine::new();
        let input = r#"
pie accTitle: sample wow
  "A" : 1
"#;

        let parsed = engine
            .parse_diagram_sync(input, ParseOptions::strict())
            .unwrap()
            .expect("diagram detected");

        assert_eq!(parsed.meta.diagram_type, "pie");
        assert_eq!(
            parsed.model.get("accTitle").and_then(|v| v.as_str()),
            Some("sample wow")
        );
    }

    #[test]
    fn pie_supports_header_acc_descr_block() {
        let engine = Engine::new();
        let input = r#"
pie accDescr {
  first line
  second line
}
  "A" : 1
"#;

        let parsed = engine
            .parse_diagram_sync(input, ParseOptions::strict())
            .unwrap()
            .expect("diagram detected");

        assert_eq!(parsed.meta.diagram_type, "pie");
        assert_eq!(
            parsed.model.get("accDescr").and_then(|v| v.as_str()),
            Some("first line\nsecond line")
        );
    }
}