|
| 1 | +using DiagramForge.Abstractions; |
| 2 | +using DiagramForge.Models; |
| 3 | + |
| 4 | +namespace DiagramForge.Parsers.Mermaid; |
| 5 | + |
| 6 | +internal sealed class MermaidStateParser : IMermaidDiagramParser |
| 7 | +{ |
| 8 | + // Synthetic IDs for the [*] terminal markers. |
| 9 | + internal const string StartNodeId = "__start__"; |
| 10 | + internal const string EndNodeId = "__end__"; |
| 11 | + |
| 12 | + private static readonly string[] EdgeOperators = ["--->", "-->>", "-.->", "==>", "===", "-->", "-.-", "---"]; |
| 13 | + |
| 14 | + public bool CanParse(MermaidDiagramKind kind) => kind == MermaidDiagramKind.StateDiagram; |
| 15 | + |
| 16 | + public Diagram Parse(MermaidDocument document) |
| 17 | + { |
| 18 | + var builder = new DiagramSemanticModelBuilder() |
| 19 | + .WithSourceSyntax("mermaid") |
| 20 | + .WithDiagramType("statediagram"); |
| 21 | + |
| 22 | + builder.WithLayoutHints(new LayoutHints { Direction = LayoutDirection.TopToBottom }); |
| 23 | + |
| 24 | + var nodesSeen = new Dictionary<string, Node>(StringComparer.Ordinal); |
| 25 | + |
| 26 | + Node GetOrCreateNode(string id, string label) |
| 27 | + { |
| 28 | + if (!nodesSeen.TryGetValue(id, out var node)) |
| 29 | + { |
| 30 | + var shape = (id == StartNodeId || id == EndNodeId) ? Shape.Circle : Shape.Rectangle; |
| 31 | + node = new Node(id, label) { Shape = shape }; |
| 32 | + nodesSeen[id] = node; |
| 33 | + builder.AddNode(node); |
| 34 | + } |
| 35 | + |
| 36 | + return node; |
| 37 | + } |
| 38 | + |
| 39 | + for (int i = 1; i < document.Lines.Length; i++) |
| 40 | + { |
| 41 | + var line = document.Lines[i]; |
| 42 | + ParseLine(line, builder, GetOrCreateNode); |
| 43 | + } |
| 44 | + |
| 45 | + return builder.Build(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void ParseLine( |
| 49 | + string line, |
| 50 | + IDiagramSemanticModelBuilder builder, |
| 51 | + Func<string, string, Node> getOrCreate) |
| 52 | + { |
| 53 | + var (matchedOp, opIndex) = FindEdgeOperator(line); |
| 54 | + |
| 55 | + if (matchedOp is not null) |
| 56 | + { |
| 57 | + var left = line[..opIndex].Trim(); |
| 58 | + var right = line[(opIndex + matchedOp.Length)..].Trim(); |
| 59 | + |
| 60 | + // State diagrams use `: label` suffix on the right side for transition labels. |
| 61 | + string? edgeLabel = null; |
| 62 | + int colonIdx = right.IndexOf(" : ", StringComparison.Ordinal); |
| 63 | + if (colonIdx >= 0) |
| 64 | + { |
| 65 | + edgeLabel = right[(colonIdx + 3)..].Trim(); |
| 66 | + right = right[..colonIdx].Trim(); |
| 67 | + } |
| 68 | + |
| 69 | + var srcId = ResolveTerminalId(left, isSource: true); |
| 70 | + var tgtId = ResolveTerminalId(right, isSource: false); |
| 71 | + |
| 72 | + var srcLabel = srcId == StartNodeId ? "[*]" : srcId; |
| 73 | + var tgtLabel = tgtId == EndNodeId ? "[*]" : tgtId; |
| 74 | + |
| 75 | + getOrCreate(srcId, srcLabel); |
| 76 | + getOrCreate(tgtId, tgtLabel); |
| 77 | + |
| 78 | + var edge = new Edge(srcId, tgtId); |
| 79 | + if (edgeLabel is not null) |
| 80 | + edge.Label = new Label(edgeLabel); |
| 81 | + |
| 82 | + edge.LineStyle = matchedOp.Contains('=') ? EdgeLineStyle.Thick |
| 83 | + : matchedOp.Contains('.') ? EdgeLineStyle.Dotted |
| 84 | + : EdgeLineStyle.Solid; |
| 85 | + edge.ArrowHead = matchedOp.Contains('>') ? ArrowHeadStyle.Arrow : ArrowHeadStyle.None; |
| 86 | + |
| 87 | + builder.AddEdge(edge); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // State definition: `id` or `id : Label` |
| 92 | + int defColon = line.IndexOf(" : ", StringComparison.Ordinal); |
| 93 | + if (defColon >= 0) |
| 94 | + { |
| 95 | + var id = line[..defColon].Trim(); |
| 96 | + var label = line[(defColon + 3)..].Trim(); |
| 97 | + if (!string.IsNullOrEmpty(id)) |
| 98 | + getOrCreate(id, label); |
| 99 | + } |
| 100 | + else if (!string.IsNullOrWhiteSpace(line)) |
| 101 | + { |
| 102 | + var id = line.Trim(); |
| 103 | + getOrCreate(id, id); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Finds the earliest (and longest, on a tie) edge operator in <paramref name="line"/>. |
| 109 | + /// </summary> |
| 110 | + private static (string? op, int index) FindEdgeOperator(string line) |
| 111 | + { |
| 112 | + string? matchedOp = null; |
| 113 | + int opIndex = -1; |
| 114 | + |
| 115 | + foreach (var op in EdgeOperators) |
| 116 | + { |
| 117 | + int idx = line.IndexOf(op, StringComparison.Ordinal); |
| 118 | + if (idx >= 0 && (opIndex < 0 || idx < opIndex || (idx == opIndex && op.Length > matchedOp!.Length))) |
| 119 | + { |
| 120 | + opIndex = idx; |
| 121 | + matchedOp = op; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return (matchedOp, opIndex); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Maps the literal <c>[*]</c> token to its synthetic node ID based on whether |
| 130 | + /// it appears as a transition source (<c>__start__</c>) or target (<c>__end__</c>). |
| 131 | + /// </summary> |
| 132 | + private static string ResolveTerminalId(string token, bool isSource) => |
| 133 | + token == "[*]" |
| 134 | + ? (isSource ? StartNodeId : EndNodeId) |
| 135 | + : token; |
| 136 | +} |
0 commit comments