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
use crate::{
    EditorExpectedSyntax, EditorExpectedSyntaxKind, EditorSemanticFacts, EditorSemanticKind,
    EditorSemanticSymbol, Error, ParseMetadata, Result, SourceSpan,
    editor::{format_lalrpop_parse_error, lalrpop_parse_diagnostic, lalrpop_recovery_span},
};
use serde_json::Value;

use super::SequenceDiagramRenderModel;
use super::Tok;
use super::db::{SequenceDb, fast_parse_sequence_signals_only_db};
use super::lexer::Lexer;
use super::sequence_grammar;

pub fn parse_sequence(code: &str, meta: &ParseMetadata) -> Result<Value> {
    let db = parse_sequence_db(code, meta)?;
    Ok(db.into_model(meta))
}

pub fn parse_sequence_model_for_render(
    code: &str,
    meta: &ParseMetadata,
) -> Result<SequenceDiagramRenderModel> {
    let db = parse_sequence_db(code, meta)?;
    Ok(db.into_render_model())
}

pub fn parse_sequence_editor_facts(code: &str, _meta: &ParseMetadata) -> EditorSemanticFacts {
    let parse_result = sequence_grammar::ActionsParser::new().parse(Lexer::new(code));
    let mut facts = collect_sequence_editor_facts_from_tokens(code);
    if let Err(error) = parse_result {
        let span = lalrpop_recovery_span(&error, code.len());
        facts.mark_recovered_from_parse_error(
            format!(
                "sequence parser recovered after parse error: {}",
                format_lalrpop_parse_error(&error)
            ),
            Some(span),
        );
    }

    facts
}

fn parse_sequence_db(code: &str, meta: &ParseMetadata) -> Result<SequenceDb> {
    let wrap_enabled = meta
        .effective_config
        .as_value()
        .get("wrap")
        .and_then(|v| v.as_bool())
        .or_else(|| {
            meta.effective_config
                .as_value()
                .get("sequence")
                .and_then(|v| v.get("wrap"))
                .and_then(|v| v.as_bool())
        });

    if let Some(db) = fast_parse_sequence_signals_only_db(code, wrap_enabled) {
        return Ok(db);
    }

    let actions = sequence_grammar::ActionsParser::new()
        .parse(Lexer::new(code))
        .map_err(|e| {
            Error::diagram_parse_diagnostic(
                meta.diagram_type.clone(),
                lalrpop_parse_diagnostic(&e, code.len()),
            )
        })?;

    let mut db = SequenceDb::new(wrap_enabled);
    for a in actions {
        db.apply(a)
            .map_err(|e| Error::diagram_parse_fallback(meta.diagram_type.clone(), e))?;
    }

    Ok(db)
}

fn collect_sequence_editor_facts_from_tokens(code: &str) -> EditorSemanticFacts {
    let mut facts = EditorSemanticFacts::new();
    let mut collector = SequenceEditorFactCollector::default();

    let mut lexer = Lexer::new(code);
    let mut last_position = lexer.position();
    while let Some(result) = lexer.next() {
        let current_position = lexer.position();
        match result {
            Ok((start, token, end)) => collector.accept(token, start, end, code, &mut facts),
            Err(_) => {
                facts.mark_recovered();
                if current_position == last_position {
                    break;
                }
            }
        }
        last_position = current_position;
    }

    facts
}

#[derive(Debug, Default)]
struct SequenceEditorFactCollector {
    expected_actor: Option<ExpectedSequenceActor>,
    expected_text: Option<ExpectedSequenceText>,
    pending_message_source: Option<PendingSequenceActor>,
    after_box_keyword: bool,
}

#[derive(Debug)]
struct PendingSequenceActor {
    name: String,
    span: SourceSpan,
}

#[derive(Debug, Clone, Copy)]
enum ExpectedSequenceActor {
    Participant,
    Actor,
    MessageTarget,
    NoteActor,
    InteractionTarget,
}

#[derive(Debug, Clone, Copy)]
enum ExpectedSequenceText {
    Message,
    Note,
    Interaction,
}

impl SequenceEditorFactCollector {
    fn accept(
        &mut self,
        token: Tok,
        start: usize,
        end: usize,
        code: &str,
        facts: &mut EditorSemanticFacts,
    ) {
        match token {
            Tok::SequenceDiagram | Tok::Newline => self.reset_line_state(),
            Tok::Participant => self.expect_actor(ExpectedSequenceActor::Participant),
            Tok::ActorKw => self.expect_actor(ExpectedSequenceActor::Actor),
            Tok::Create | Tok::Destroy => self.expect_actor(ExpectedSequenceActor::Participant),
            Tok::Activate | Tok::Deactivate => {
                self.expect_actor(ExpectedSequenceActor::InteractionTarget)
            }
            Tok::Links => {
                facts.push_directive_prefix("links");
                self.expect_actor(ExpectedSequenceActor::InteractionTarget);
            }
            Tok::Link => {
                facts.push_directive_prefix("link");
                self.expect_actor(ExpectedSequenceActor::InteractionTarget);
            }
            Tok::Properties => {
                facts.push_directive_prefix("properties");
                self.expect_actor(ExpectedSequenceActor::InteractionTarget);
            }
            Tok::Details => {
                facts.push_directive_prefix("details");
                self.expect_actor(ExpectedSequenceActor::InteractionTarget);
            }
            Tok::Note => {
                self.pending_message_source = None;
            }
            Tok::LeftOf | Tok::RightOf | Tok::Over | Tok::Comma => {
                self.expect_actor(ExpectedSequenceActor::NoteActor);
            }
            Tok::Box => {
                self.after_box_keyword = true;
            }
            Tok::SignalType(_) => {
                self.push_pending_message_source(facts);
                self.expect_actor(ExpectedSequenceActor::MessageTarget);
            }
            Tok::Actor(name) => {
                if let Some(expected) = self.expected_actor {
                    self.push_actor(name, expected, start, end, facts);
                } else {
                    self.pending_message_source = Some(PendingSequenceActor {
                        name,
                        span: SourceSpan::new(start, end),
                    });
                }
            }
            Tok::RestOfLine(text) => {
                if self.after_box_keyword {
                    push_sequence_box_symbol(text, start, end, code, facts);
                    self.after_box_keyword = false;
                }
            }
            Tok::Text(text) => {
                if let Some(expected) = self.expected_text.take() {
                    push_sequence_text_payload(text, expected, start, end, code, facts);
                }
            }
            Tok::Title(text) | Tok::CompatTitle(text) => {
                facts.push_directive_prefix("title");
                push_sequence_named_payload(text, "sequence title", start, end, code, facts);
            }
            Tok::AccTitle(text) => {
                facts.push_directive_prefix("accTitle");
                push_sequence_named_payload(
                    text,
                    "sequence accessibility title",
                    start,
                    end,
                    code,
                    facts,
                );
            }
            Tok::AccDescr(text) | Tok::AccDescrMultiline(text) => {
                facts.push_directive_prefix("accDescr");
                push_sequence_named_payload(
                    text,
                    "sequence accessibility description",
                    start,
                    end,
                    code,
                    facts,
                );
            }
            Tok::End
            | Tok::Loop
            | Tok::Rect
            | Tok::Opt
            | Tok::Alt
            | Tok::Else
            | Tok::Par
            | Tok::ParOver
            | Tok::And
            | Tok::Critical
            | Tok::Option
            | Tok::Break
            | Tok::As
            | Tok::Autonumber
            | Tok::Off
            | Tok::Plus
            | Tok::Minus
            | Tok::Central
            | Tok::Num(_)
            | Tok::Config(_) => {}
        }
    }

    fn reset_line_state(&mut self) {
        self.expected_actor = None;
        self.expected_text = None;
        self.pending_message_source = None;
        self.after_box_keyword = false;
    }

    fn expect_actor(&mut self, expected: ExpectedSequenceActor) {
        self.expected_actor = Some(expected);
    }

    fn push_actor(
        &mut self,
        name: String,
        expected: ExpectedSequenceActor,
        start: usize,
        end: usize,
        facts: &mut EditorSemanticFacts,
    ) {
        let kind = match expected {
            ExpectedSequenceActor::Actor => EditorSemanticKind::Variable,
            ExpectedSequenceActor::Participant
            | ExpectedSequenceActor::MessageTarget
            | ExpectedSequenceActor::NoteActor
            | ExpectedSequenceActor::InteractionTarget => EditorSemanticKind::Event,
        };
        let detail = match expected {
            ExpectedSequenceActor::Actor => "sequence actor",
            ExpectedSequenceActor::Participant => "sequence participant",
            ExpectedSequenceActor::MessageTarget => "sequence participant reference",
            ExpectedSequenceActor::NoteActor => "sequence note participant",
            ExpectedSequenceActor::InteractionTarget => "sequence participant reference",
        };
        let span = SourceSpan::new(start, end);
        facts.push_symbol(EditorSemanticSymbol::new(
            name.clone(),
            Some(detail.to_string()),
            kind,
            span,
            span,
        ));
        self.expected_text = match expected {
            ExpectedSequenceActor::MessageTarget => Some(ExpectedSequenceText::Message),
            ExpectedSequenceActor::NoteActor => Some(ExpectedSequenceText::Note),
            ExpectedSequenceActor::InteractionTarget => Some(ExpectedSequenceText::Interaction),
            ExpectedSequenceActor::Participant | ExpectedSequenceActor::Actor => None,
        };
        self.expected_actor = None;
    }

    fn push_pending_message_source(&mut self, facts: &mut EditorSemanticFacts) {
        if let Some(actor) = self.pending_message_source.take() {
            facts.push_symbol(EditorSemanticSymbol::new(
                actor.name,
                Some("sequence participant reference".to_string()),
                EditorSemanticKind::Event,
                actor.span,
                actor.span,
            ));
        }
    }
}

fn push_sequence_text_payload(
    text: String,
    expected: ExpectedSequenceText,
    start: usize,
    end: usize,
    code: &str,
    facts: &mut EditorSemanticFacts,
) {
    let detail = match expected {
        ExpectedSequenceText::Message => "sequence message",
        ExpectedSequenceText::Note => "sequence note",
        ExpectedSequenceText::Interaction => "sequence interaction payload",
    };
    facts.push_expected_syntax(EditorExpectedSyntax::new(
        EditorExpectedSyntaxKind::Payload,
        SourceSpan::new(start, end),
    ));
    push_sequence_named_payload(text, detail, start, end, code, facts);
}

fn push_sequence_named_payload(
    text: String,
    detail: &str,
    start: usize,
    end: usize,
    code: &str,
    facts: &mut EditorSemanticFacts,
) {
    let Some(selection) = sequence_payload_selection(&text, start, end, code) else {
        return;
    };
    facts.push_symbol(EditorSemanticSymbol::payload(
        text,
        Some(detail.to_string()),
        EditorSemanticKind::String,
        SourceSpan::new(start, end),
        selection,
    ));
}

fn sequence_payload_selection(
    text: &str,
    start: usize,
    end: usize,
    code: &str,
) -> Option<SourceSpan> {
    if text.is_empty() {
        return None;
    }
    let raw = code.get(start..end)?;
    let local_start = raw.rfind(text)?;
    Some(SourceSpan::new(
        start + local_start,
        start + local_start + text.len(),
    ))
}

fn push_sequence_box_symbol(
    text: String,
    start: usize,
    end: usize,
    code: &str,
    facts: &mut EditorSemanticFacts,
) {
    let Some((name, selection)) = sequence_box_name_and_selection(&text, start, end, code) else {
        return;
    };
    facts.push_symbol(EditorSemanticSymbol::new(
        name,
        Some("sequence box".to_string()),
        EditorSemanticKind::Package,
        SourceSpan::new(start, end),
        selection,
    ));
}

fn sequence_box_name_and_selection(
    text: &str,
    start: usize,
    end: usize,
    code: &str,
) -> Option<(String, SourceSpan)> {
    let raw = code.get(start..end).unwrap_or(text);
    let leading = raw.len().saturating_sub(raw.trim_start().len());
    let trailing = raw.trim_end().len();
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return None;
    }

    let (color, title_candidate) = split_sequence_box_color_and_title(trimmed);
    let title_start = if looks_like_css_color(color) {
        trimmed.len().saturating_sub(title_candidate.len())
    } else {
        0
    };
    let title = trimmed[title_start..].trim();
    if title.is_empty() {
        return None;
    }

    let local_start = leading + title_start + trimmed[title_start..].len()
        - trimmed[title_start..].trim_start().len();
    let local_end = start + trailing;
    Some((
        title.to_string(),
        SourceSpan::new(start + local_start, local_end),
    ))
}

fn looks_like_css_color(value: &str) -> bool {
    let value = value.trim();
    value.starts_with('#')
        || value.starts_with("rgb(")
        || value.starts_with("rgba(")
        || value.starts_with("hsl(")
        || value.starts_with("hsla(")
        || matches!(
            value,
            "transparent" | "red" | "green" | "blue" | "white" | "black" | "grey" | "gray"
        )
}

fn split_sequence_box_color_and_title(input: &str) -> (&str, &str) {
    let lower = input.to_ascii_lowercase();
    for prefix in ["rgba", "rgb", "hsla", "hsl"] {
        if lower.starts_with(prefix)
            && let Some(end) = input.find(')')
        {
            return (input[..=end].trim(), &input[end + 1..]);
        }
    }

    let mut end = 0usize;
    for (idx, ch) in input.char_indices() {
        if ch.is_ascii_alphanumeric() || ch == '_' {
            end = idx + ch.len_utf8();
            continue;
        }
        break;
    }
    (&input[..end], &input[end..])
}