@grethel-labs/excaliplant 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +302 -0
- package/index.mjs +89 -0
- package/package.json +53 -0
- package/src/layout/elk_layout.mjs +517 -0
- package/src/layout/sequence_layout.mjs +90 -0
- package/src/layout/sizing.mjs +160 -0
- package/src/model/diagram.mjs +207 -0
- package/src/parser/component_context.mjs +129 -0
- package/src/parser/engine.mjs +87 -0
- package/src/parser/plantuml.mjs +126 -0
- package/src/parser/plugins/component/connections.mjs +26 -0
- package/src/parser/plugins/component/containers.mjs +36 -0
- package/src/parser/plugins/component/notes.mjs +65 -0
- package/src/parser/plugins/component/preamble.mjs +25 -0
- package/src/parser/plugins/component/shapes.mjs +111 -0
- package/src/parser/plugins/sequence/messages.mjs +37 -0
- package/src/parser/plugins/sequence/notes.mjs +84 -0
- package/src/parser/plugins/sequence/participants.mjs +18 -0
- package/src/parser/plugins/sequence/preamble.mjs +11 -0
- package/src/parser/sequence_context.mjs +39 -0
- package/src/parser/utils.mjs +150 -0
- package/src/render/excalidraw.mjs +736 -0
- package/src/render/sequence_render.mjs +201 -0
- package/src/render/svg.mjs +167 -0
- package/src/style/colors.mjs +75 -0
- package/src/style/text.mjs +42 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// PlantUML → Diagram model.
|
|
2
|
+
//
|
|
3
|
+
// This file is intentionally tiny: it just dispatches between component
|
|
4
|
+
// and sequence diagrams, assembles the plugin pipeline, and drives the
|
|
5
|
+
// engine. All real parsing logic lives in `plugins/`.
|
|
6
|
+
//
|
|
7
|
+
// ## Extending the parser
|
|
8
|
+
//
|
|
9
|
+
// 1. Drop a new plugin file in `plugins/component/` or
|
|
10
|
+
// `plugins/sequence/`. A plugin is `{ name, tryLine?, tryStart? }`
|
|
11
|
+
// — see `engine.mjs` for the contract.
|
|
12
|
+
// 2. Register it here by adding it to the corresponding default array.
|
|
13
|
+
// 3. Or pass it at call time via
|
|
14
|
+
// `parsePlantUml(text, { plugins: { component: [...], sequence: [...] } })`.
|
|
15
|
+
//
|
|
16
|
+
// The model classes (`Box`, `Plane`, `Connection`, `Participant`,
|
|
17
|
+
// `Message`, …) are unchanged — plugins manipulate them through the
|
|
18
|
+
// context helpers in `component_context.mjs` / `sequence_context.mjs`.
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @diagram plugins
|
|
22
|
+
*
|
|
23
|
+
* Each parser plugin is a tiny self-contained file that handles ONE
|
|
24
|
+
* PlantUML construct. The engine offers each input line to plugins
|
|
25
|
+
* in registration order; the first plugin that returns `true` wins.
|
|
26
|
+
*
|
|
27
|
+
* To add support for a new PlantUML keyword, drop a new file in
|
|
28
|
+
* `src/parser/plugins/` and append it to the default array in
|
|
29
|
+
* [`plantuml.mjs`](./src/parser/plantuml.mjs). No engine change required.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { runEngine } from "./engine.mjs";
|
|
33
|
+
import { explodeBraces, stripComment } from "./utils.mjs";
|
|
34
|
+
import { createComponentContext } from "./component_context.mjs";
|
|
35
|
+
import { createSequenceContext } from "./sequence_context.mjs";
|
|
36
|
+
|
|
37
|
+
// Component plugin registry.
|
|
38
|
+
import { titlePlugin as componentTitlePlugin, closeBracePlugin } from "./plugins/component/preamble.mjs";
|
|
39
|
+
import { containerPlugin } from "./plugins/component/containers.mjs";
|
|
40
|
+
import {
|
|
41
|
+
bracketBoxPlugin, usecaseParensPlugin, shapeKeywordPlugin,
|
|
42
|
+
} from "./plugins/component/shapes.mjs";
|
|
43
|
+
import { connectionPlugin } from "./plugins/component/connections.mjs";
|
|
44
|
+
import {
|
|
45
|
+
noteOfPlugin, noteFreePlugin, noteBlockPlugin, _resetNoteCounter,
|
|
46
|
+
} from "./plugins/component/notes.mjs";
|
|
47
|
+
|
|
48
|
+
// Sequence plugin registry.
|
|
49
|
+
import { titlePlugin as sequenceTitlePlugin } from "./plugins/sequence/preamble.mjs";
|
|
50
|
+
import { participantPlugin } from "./plugins/sequence/participants.mjs";
|
|
51
|
+
import { messagePlugin } from "./plugins/sequence/messages.mjs";
|
|
52
|
+
import {
|
|
53
|
+
noteSidePlugin, noteOverPlugin,
|
|
54
|
+
noteSideBlockPlugin, noteOverBlockPlugin,
|
|
55
|
+
} from "./plugins/sequence/notes.mjs";
|
|
56
|
+
|
|
57
|
+
export const DEFAULT_COMPONENT_PLUGINS = [
|
|
58
|
+
componentTitlePlugin,
|
|
59
|
+
closeBracePlugin,
|
|
60
|
+
// Block plugins (note, class) come BEFORE container so a `note … of X`
|
|
61
|
+
// line isn't mis-parsed by some other rule, and `class X { … }` isn't
|
|
62
|
+
// captured by the bare-container regex.
|
|
63
|
+
noteBlockPlugin,
|
|
64
|
+
noteOfPlugin,
|
|
65
|
+
noteFreePlugin,
|
|
66
|
+
shapeKeywordPlugin, // owns the class-block start
|
|
67
|
+
containerPlugin,
|
|
68
|
+
bracketBoxPlugin,
|
|
69
|
+
usecaseParensPlugin,
|
|
70
|
+
connectionPlugin, // last: the regex is greedy
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
export const DEFAULT_SEQUENCE_PLUGINS = [
|
|
74
|
+
sequenceTitlePlugin,
|
|
75
|
+
participantPlugin,
|
|
76
|
+
noteSideBlockPlugin,
|
|
77
|
+
noteOverBlockPlugin,
|
|
78
|
+
noteSidePlugin,
|
|
79
|
+
noteOverPlugin,
|
|
80
|
+
messagePlugin,
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Parse a PlantUML source string into the input-agnostic diagram model.
|
|
85
|
+
* The parser auto-detects sequence vs. component syntax via
|
|
86
|
+
* {@link looksLikeSequence}.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} text PlantUML source.
|
|
89
|
+
* @param {object} [opts]
|
|
90
|
+
* @param {{component?: any[], sequence?: any[]}} [opts.plugins]
|
|
91
|
+
* Override the default plugin lists. Useful for adding support for
|
|
92
|
+
* unsupported PlantUML constructs without forking the library.
|
|
93
|
+
* @returns {import("../model/diagram.mjs").Diagram
|
|
94
|
+
* | import("../model/diagram.mjs").SequenceDiagram}
|
|
95
|
+
*/
|
|
96
|
+
export function parsePlantUml(text, opts = {}) {
|
|
97
|
+
const componentPlugins = opts.plugins?.component ?? DEFAULT_COMPONENT_PLUGINS;
|
|
98
|
+
const sequencePlugins = opts.plugins?.sequence ?? DEFAULT_SEQUENCE_PLUGINS;
|
|
99
|
+
|
|
100
|
+
if (looksLikeSequence(text)) {
|
|
101
|
+
return runEngine({
|
|
102
|
+
lines: text.split(/\r?\n/),
|
|
103
|
+
plugins: sequencePlugins,
|
|
104
|
+
ctx: createSequenceContext(),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_resetNoteCounter();
|
|
109
|
+
return runEngine({
|
|
110
|
+
lines: explodeBraces(text.split(/\r?\n/)),
|
|
111
|
+
plugins: componentPlugins,
|
|
112
|
+
ctx: createComponentContext(),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Sequence-only keywords trigger the sequence pipeline. `actor` and
|
|
117
|
+
// bare `A --> B` arrows alone are not enough — component diagrams use
|
|
118
|
+
// them too.
|
|
119
|
+
function looksLikeSequence(text) {
|
|
120
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
121
|
+
const line = stripComment(raw).trim();
|
|
122
|
+
if (!line) continue;
|
|
123
|
+
if (/^(participant|boundary|control|collections|queue)\b/.test(line)) return true;
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Generic component-style connection: `A op B [: label]`.
|
|
2
|
+
//
|
|
3
|
+
// All arrow flavours (plain, dashed, inheritance, composition, …) are
|
|
4
|
+
// decoded by `classifyArrow()`; to support a new arrow operator just
|
|
5
|
+
// extend that classifier.
|
|
6
|
+
|
|
7
|
+
import { classifyArrow, unescapeLabel } from "../../utils.mjs";
|
|
8
|
+
|
|
9
|
+
const CONNECTION_LINE = /^(\S+)\s+([-.*o<|>]+(?:up|down|left|right|UP|DOWN|LEFT|RIGHT)?[-.*o<|>]*)\s+(\S+)(?:\s*:\s*(.+))?$/;
|
|
10
|
+
|
|
11
|
+
export const connectionPlugin = {
|
|
12
|
+
name: "component.connection",
|
|
13
|
+
tryLine(line, ctx) {
|
|
14
|
+
const m = line.match(CONNECTION_LINE);
|
|
15
|
+
if (!m) return false;
|
|
16
|
+
const [, fromId, op, toId, label] = m;
|
|
17
|
+
const arrow = classifyArrow(op);
|
|
18
|
+
if (!arrow) return false;
|
|
19
|
+
ctx.queueConnection({
|
|
20
|
+
fromId, toId,
|
|
21
|
+
label: unescapeLabel(label?.trim() || ""),
|
|
22
|
+
...arrow,
|
|
23
|
+
});
|
|
24
|
+
return true;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Container keywords: package | frame | folder | node | rectangle, plus
|
|
2
|
+
// the bare `together { … }`. Recognised only when the line opens a brace.
|
|
3
|
+
|
|
4
|
+
import { slug } from "../../utils.mjs";
|
|
5
|
+
|
|
6
|
+
const CONTAINER_KEYWORDS = ["package", "frame", "folder", "node", "rectangle"];
|
|
7
|
+
const CONTAINER_OPEN_QUOTED = new RegExp(
|
|
8
|
+
`^(${CONTAINER_KEYWORDS.join("|")})\\s+"([^"]+)"(?:\\s+as\\s+(\\S+))?\\s*\\{`);
|
|
9
|
+
const CONTAINER_OPEN_BARE = new RegExp(
|
|
10
|
+
`^(${CONTAINER_KEYWORDS.join("|")})\\s+(\\S+)\\s*\\{`);
|
|
11
|
+
const TOGETHER_OPEN = /^together\s*\{/;
|
|
12
|
+
|
|
13
|
+
export const containerPlugin = {
|
|
14
|
+
name: "component.container",
|
|
15
|
+
tryLine(line, ctx) {
|
|
16
|
+
if (TOGETHER_OPEN.test(line)) {
|
|
17
|
+
ctx.openContainer({
|
|
18
|
+
id: `together_${ctx.diagram.planes.length}_${ctx.boxes.size}`,
|
|
19
|
+
title: "",
|
|
20
|
+
kind: "together",
|
|
21
|
+
});
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
let m = line.match(CONTAINER_OPEN_QUOTED);
|
|
25
|
+
if (m) {
|
|
26
|
+
ctx.openContainer({ id: m[3] || slug(m[2]), title: m[2], kind: m[1] });
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
m = line.match(CONTAINER_OPEN_BARE);
|
|
30
|
+
if (m) {
|
|
31
|
+
ctx.openContainer({ id: m[2], title: m[2], kind: m[1] });
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// PlantUML notes for component-style diagrams.
|
|
2
|
+
//
|
|
3
|
+
// note left|right|top|bottom of <id> : text (single line)
|
|
4
|
+
// note left|right|top|bottom of <id> (multi-line, ended by `end note`)
|
|
5
|
+
// note "text" as Nx (free standing)
|
|
6
|
+
|
|
7
|
+
import { unescapeLabel } from "../../utils.mjs";
|
|
8
|
+
|
|
9
|
+
const NOTE_OF = /^note\s+(left|right|top|bottom)\s+of\s+(\S+)\s*:\s*(.+)$/;
|
|
10
|
+
const NOTE_FREE = /^note\s+"([^"]+)"\s+as\s+(\S+)$/;
|
|
11
|
+
const NOTE_BLOCK_OPEN = /^note\s+(left|right|top|bottom)\s+of\s+(\S+)\s*$/;
|
|
12
|
+
|
|
13
|
+
let _counter = 0;
|
|
14
|
+
const nextId = () => `note_${_counter++}`;
|
|
15
|
+
export function _resetNoteCounter() { _counter = 0; }
|
|
16
|
+
|
|
17
|
+
export const noteOfPlugin = {
|
|
18
|
+
name: "component.noteOf",
|
|
19
|
+
tryLine(line, ctx) {
|
|
20
|
+
const m = line.match(NOTE_OF);
|
|
21
|
+
if (!m) return false;
|
|
22
|
+
ctx.queueNote({
|
|
23
|
+
id: nextId(),
|
|
24
|
+
side: m[1],
|
|
25
|
+
targetId: m[2],
|
|
26
|
+
text: unescapeLabel(m[3].trim()),
|
|
27
|
+
});
|
|
28
|
+
return true;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const noteFreePlugin = {
|
|
33
|
+
name: "component.noteFree",
|
|
34
|
+
tryLine(line, ctx) {
|
|
35
|
+
const m = line.match(NOTE_FREE);
|
|
36
|
+
if (!m) return false;
|
|
37
|
+
ctx.addBox({
|
|
38
|
+
id: m[2], title: unescapeLabel(m[1]), description: "",
|
|
39
|
+
shape: "note", stereotype: "",
|
|
40
|
+
});
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const noteBlockPlugin = {
|
|
46
|
+
name: "component.noteBlock",
|
|
47
|
+
tryStart(line) {
|
|
48
|
+
const m = line.match(NOTE_BLOCK_OPEN);
|
|
49
|
+
if (!m) return null;
|
|
50
|
+
const side = m[1];
|
|
51
|
+
const targetId = m[2];
|
|
52
|
+
const lines = [];
|
|
53
|
+
return {
|
|
54
|
+
onLine(l) { lines.push(l); },
|
|
55
|
+
tryEnd(l, ctx) {
|
|
56
|
+
if (!/^end\s*note$/i.test(l)) return false;
|
|
57
|
+
ctx.queueNote({
|
|
58
|
+
id: nextId(), side, targetId,
|
|
59
|
+
text: lines.join("\\n"),
|
|
60
|
+
});
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Title + closing brace.
|
|
2
|
+
//
|
|
3
|
+
// Each plugin file deliberately handles ONE PlantUML concept. To support
|
|
4
|
+
// a new construct, drop a new file in this folder and register it.
|
|
5
|
+
|
|
6
|
+
import { TITLE_LINE, stripQuotes } from "../../utils.mjs";
|
|
7
|
+
|
|
8
|
+
export const titlePlugin = {
|
|
9
|
+
name: "component.title",
|
|
10
|
+
tryLine(line, ctx) {
|
|
11
|
+
const m = line.match(TITLE_LINE);
|
|
12
|
+
if (!m) return false;
|
|
13
|
+
ctx.setTitle(stripQuotes(m[1].trim()));
|
|
14
|
+
return true;
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const closeBracePlugin = {
|
|
19
|
+
name: "component.close",
|
|
20
|
+
tryLine(line, ctx) {
|
|
21
|
+
if (line !== "}") return false;
|
|
22
|
+
ctx.closeContainer();
|
|
23
|
+
return true;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Box / shape declarations: bracket shorthand, usecase parens, and the
|
|
2
|
+
// keyword-prefixed forms (component / actor / database / cloud / interface
|
|
3
|
+
// / entity / class / rectangle / boundary / control / node-without-brace
|
|
4
|
+
// / usecase).
|
|
5
|
+
//
|
|
6
|
+
// `class X { … }` is a special case: the brace introduces a member list
|
|
7
|
+
// rather than a nested container, so this plugin uses the engine's block
|
|
8
|
+
// mode to consume the body.
|
|
9
|
+
|
|
10
|
+
import { STEREOTYPE, slug, unescapeLabel, normaliseShape } from "../../utils.mjs";
|
|
11
|
+
|
|
12
|
+
const BOX_BRACKET = /^\[([^\]]+)\](?:\s*<<\s*[^>]+\s*>>)?(?:\s+as\s+(\S+))?(?:\s*:\s*(.+))?$/;
|
|
13
|
+
const USECASE_PARENS = /^\(([^)]+)\)(?:\s+as\s+(\S+))?(?:\s*:\s*(.+))?$/;
|
|
14
|
+
|
|
15
|
+
const SHAPE_KEYWORDS = ["component", "actor", "usecase", "database", "node",
|
|
16
|
+
"cloud", "interface", "entity", "class", "rectangle", "boundary", "control"];
|
|
17
|
+
const SHAPE_LINE = new RegExp(
|
|
18
|
+
`^(${SHAPE_KEYWORDS.join("|")})\\s+(?:"([^"]+)"|(\\S+))` +
|
|
19
|
+
`(?:\\s*<<\\s*[^>]+\\s*>>)?` +
|
|
20
|
+
`(?:\\s+as\\s+(\\S+))?` +
|
|
21
|
+
`(?:\\s*:\\s*(.+))?` +
|
|
22
|
+
`(\\s*\\{)?$`);
|
|
23
|
+
|
|
24
|
+
export const bracketBoxPlugin = {
|
|
25
|
+
name: "component.bracketBox",
|
|
26
|
+
tryLine(line, ctx) {
|
|
27
|
+
const m = line.match(BOX_BRACKET);
|
|
28
|
+
if (!m) return false;
|
|
29
|
+
const [, label, alias, description] = m;
|
|
30
|
+
const stereo = (line.match(STEREOTYPE) || [])[1] || "";
|
|
31
|
+
ctx.addBox({
|
|
32
|
+
id: alias || slug(label),
|
|
33
|
+
title: unescapeLabel(label),
|
|
34
|
+
description: unescapeLabel(description?.trim() || ""),
|
|
35
|
+
shape: "rectangle",
|
|
36
|
+
stereotype: stereo,
|
|
37
|
+
});
|
|
38
|
+
return true;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const usecaseParensPlugin = {
|
|
43
|
+
name: "component.usecaseParens",
|
|
44
|
+
tryLine(line, ctx) {
|
|
45
|
+
const m = line.match(USECASE_PARENS);
|
|
46
|
+
if (!m) return false;
|
|
47
|
+
const [, label, alias, description] = m;
|
|
48
|
+
ctx.addBox({
|
|
49
|
+
id: alias || slug(label),
|
|
50
|
+
title: unescapeLabel(label),
|
|
51
|
+
description: unescapeLabel(description?.trim() || ""),
|
|
52
|
+
shape: "usecase",
|
|
53
|
+
});
|
|
54
|
+
return true;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const shapeKeywordPlugin = {
|
|
59
|
+
name: "component.shapeKeyword",
|
|
60
|
+
tryStart(line, ctx) {
|
|
61
|
+
const m = line.match(SHAPE_LINE);
|
|
62
|
+
if (!m) return null;
|
|
63
|
+
const [, shapeKw, qTitle, bareId, alias, description, opensBrace] = m;
|
|
64
|
+
// `class X { … }` → enter member-collection block.
|
|
65
|
+
if (opensBrace && shapeKw === "class") {
|
|
66
|
+
const stereo = (line.match(STEREOTYPE) || [])[1] || "";
|
|
67
|
+
const id = alias || bareId || slug(qTitle);
|
|
68
|
+
const box = ctx.addBox({
|
|
69
|
+
id,
|
|
70
|
+
title: unescapeLabel(qTitle || bareId || alias),
|
|
71
|
+
description: unescapeLabel(description?.trim() || ""),
|
|
72
|
+
shape: "class",
|
|
73
|
+
stereotype: stereo,
|
|
74
|
+
members: [],
|
|
75
|
+
});
|
|
76
|
+
const collected = [];
|
|
77
|
+
return {
|
|
78
|
+
onLine(memberLine) { collected.push(memberLine); },
|
|
79
|
+
tryEnd(memberLine) {
|
|
80
|
+
if (memberLine !== "}") return false;
|
|
81
|
+
box.members = collected;
|
|
82
|
+
return true;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
},
|
|
88
|
+
tryLine(line, ctx) {
|
|
89
|
+
const m = line.match(SHAPE_LINE);
|
|
90
|
+
if (!m) return false;
|
|
91
|
+
const [, shapeKw, qTitle, bareId, alias, description, opensBrace] = m;
|
|
92
|
+
// Non-class brace forms (e.g. `node X { … }`) become containers.
|
|
93
|
+
if (opensBrace) {
|
|
94
|
+
ctx.openContainer({
|
|
95
|
+
id: alias || bareId || slug(qTitle),
|
|
96
|
+
title: qTitle || bareId || alias,
|
|
97
|
+
kind: shapeKw,
|
|
98
|
+
});
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
const stereo = (line.match(STEREOTYPE) || [])[1] || "";
|
|
102
|
+
ctx.addBox({
|
|
103
|
+
id: alias || bareId || slug(qTitle),
|
|
104
|
+
title: unescapeLabel(qTitle || bareId || alias),
|
|
105
|
+
description: unescapeLabel(description?.trim() || ""),
|
|
106
|
+
shape: normaliseShape(shapeKw),
|
|
107
|
+
stereotype: stereo,
|
|
108
|
+
});
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Sequence-diagram messages.
|
|
2
|
+
//
|
|
3
|
+
// Operator forms supported:
|
|
4
|
+
// -> sync
|
|
5
|
+
// --> reply (dashed)
|
|
6
|
+
// ->> async open arrowhead
|
|
7
|
+
// <- reverse
|
|
8
|
+
// <--> bidirectional
|
|
9
|
+
|
|
10
|
+
import { unescapeLabel } from "../../utils.mjs";
|
|
11
|
+
|
|
12
|
+
const SEQ_MESSAGE = /^(\S+)\s+((?:<-+>?|-+>>?|<<?-+|<-+|--)+)\s+(\S+)(?:\s*:\s*(.*))?$/;
|
|
13
|
+
|
|
14
|
+
export const messagePlugin = {
|
|
15
|
+
name: "sequence.message",
|
|
16
|
+
tryLine(line, ctx) {
|
|
17
|
+
const m = line.match(SEQ_MESSAGE);
|
|
18
|
+
if (!m) return false;
|
|
19
|
+
const [, fromId, op, toId, label] = m;
|
|
20
|
+
const from = ctx.ensureParticipant(fromId);
|
|
21
|
+
const to = ctx.ensureParticipant(toId);
|
|
22
|
+
const dashed = op.includes("--");
|
|
23
|
+
const reversed = op.startsWith("<") && !op.endsWith(">");
|
|
24
|
+
const bidir = /<.*>/.test(op);
|
|
25
|
+
const asyncOpen = op.includes(">>") || op.includes("<<");
|
|
26
|
+
const [src, dst] = reversed ? [to, from] : [from, to];
|
|
27
|
+
ctx.addMessage({
|
|
28
|
+
from: src, to: dst,
|
|
29
|
+
label: unescapeLabel(label?.trim() || ""),
|
|
30
|
+
dashed,
|
|
31
|
+
kind: src === dst ? "self" : (dashed ? "reply" : (asyncOpen ? "async" : "sync")),
|
|
32
|
+
startArrowhead: bidir ? (asyncOpen ? "arrow" : "triangle") : null,
|
|
33
|
+
endArrowhead: asyncOpen ? "arrow" : "triangle",
|
|
34
|
+
});
|
|
35
|
+
return true;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Sequence-diagram notes.
|
|
2
|
+
//
|
|
3
|
+
// note left|right [of <id>] : text
|
|
4
|
+
// note over A[, B] : text
|
|
5
|
+
// plus the multi-line variants ended by `end note`.
|
|
6
|
+
|
|
7
|
+
import { unescapeLabel } from "../../utils.mjs";
|
|
8
|
+
|
|
9
|
+
const SEQ_NOTE_SIDE = /^note\s+(left|right)(?:\s+of\s+(\S+))?\s*:\s*(.+)$/;
|
|
10
|
+
const SEQ_NOTE_OVER = /^note\s+over\s+(\S+)(?:\s*,\s*(\S+))?\s*:\s*(.+)$/;
|
|
11
|
+
const SEQ_NOTE_BLOCK_OPEN_SIDE = /^note\s+(left|right)(?:\s+of\s+(\S+))?\s*$/;
|
|
12
|
+
const SEQ_NOTE_BLOCK_OPEN_OVER = /^note\s+over\s+(\S+)(?:\s*,\s*(\S+))?\s*$/;
|
|
13
|
+
|
|
14
|
+
const lastParticipant = (ctx) => ctx.diagram.participants[ctx.diagram.participants.length - 1];
|
|
15
|
+
|
|
16
|
+
export const noteSidePlugin = {
|
|
17
|
+
name: "sequence.noteSide",
|
|
18
|
+
tryLine(line, ctx) {
|
|
19
|
+
const m = line.match(SEQ_NOTE_SIDE);
|
|
20
|
+
if (!m) return false;
|
|
21
|
+
const [, side, targetId, text] = m;
|
|
22
|
+
const target = targetId ? ctx.ensureParticipant(targetId) : lastParticipant(ctx);
|
|
23
|
+
if (!target) return true;
|
|
24
|
+
ctx.addNote({ text: unescapeLabel(text), side, target });
|
|
25
|
+
return true;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const noteOverPlugin = {
|
|
30
|
+
name: "sequence.noteOver",
|
|
31
|
+
tryLine(line, ctx) {
|
|
32
|
+
const m = line.match(SEQ_NOTE_OVER);
|
|
33
|
+
if (!m) return false;
|
|
34
|
+
const [, aId, bId, text] = m;
|
|
35
|
+
ctx.addNote({
|
|
36
|
+
text: unescapeLabel(text),
|
|
37
|
+
side: "over",
|
|
38
|
+
target: ctx.ensureParticipant(aId),
|
|
39
|
+
target2: bId ? ctx.ensureParticipant(bId) : null,
|
|
40
|
+
});
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const noteSideBlockPlugin = {
|
|
46
|
+
name: "sequence.noteSideBlock",
|
|
47
|
+
tryStart(line, ctx) {
|
|
48
|
+
const m = line.match(SEQ_NOTE_BLOCK_OPEN_SIDE);
|
|
49
|
+
if (!m) return null;
|
|
50
|
+
const side = m[1];
|
|
51
|
+
const targetId = m[2];
|
|
52
|
+
const target = targetId ? ctx.ensureParticipant(targetId) : lastParticipant(ctx);
|
|
53
|
+
const lines = [];
|
|
54
|
+
return {
|
|
55
|
+
onLine(l) { lines.push(l); },
|
|
56
|
+
tryEnd(l, ctx2) {
|
|
57
|
+
if (!/^end\s*note$/i.test(l)) return false;
|
|
58
|
+
if (target) {
|
|
59
|
+
ctx2.addNote({ text: lines.join("\n"), side, target, target2: null });
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const noteOverBlockPlugin = {
|
|
68
|
+
name: "sequence.noteOverBlock",
|
|
69
|
+
tryStart(line, ctx) {
|
|
70
|
+
const m = line.match(SEQ_NOTE_BLOCK_OPEN_OVER);
|
|
71
|
+
if (!m) return null;
|
|
72
|
+
const a = ctx.ensureParticipant(m[1]);
|
|
73
|
+
const b = m[2] ? ctx.ensureParticipant(m[2]) : null;
|
|
74
|
+
const lines = [];
|
|
75
|
+
return {
|
|
76
|
+
onLine(l) { lines.push(l); },
|
|
77
|
+
tryEnd(l, ctx2) {
|
|
78
|
+
if (!/^end\s*note$/i.test(l)) return false;
|
|
79
|
+
ctx2.addNote({ text: lines.join("\n"), side: "over", target: a, target2: b });
|
|
80
|
+
return true;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Sequence-diagram participant declarations.
|
|
2
|
+
|
|
3
|
+
import { slug, unescapeLabel } from "../../utils.mjs";
|
|
4
|
+
|
|
5
|
+
const SEQ_PARTICIPANT = /^(participant|actor|boundary|control|collections|queue|database|entity)\s+(?:"([^"]+)"|(\S+))(?:\s+as\s+(\S+))?(?:\s*<<\s*([^>]+?)\s*>>)?$/;
|
|
6
|
+
|
|
7
|
+
export const participantPlugin = {
|
|
8
|
+
name: "sequence.participant",
|
|
9
|
+
tryLine(line, ctx) {
|
|
10
|
+
const m = line.match(SEQ_PARTICIPANT);
|
|
11
|
+
if (!m) return false;
|
|
12
|
+
const [, kw, qTitle, bareId, alias, stereo] = m;
|
|
13
|
+
const id = alias || bareId || slug(qTitle);
|
|
14
|
+
const title = unescapeLabel(qTitle || bareId || id);
|
|
15
|
+
ctx.declareParticipant({ id, title, shape: kw, stereotype: stereo || "" });
|
|
16
|
+
return true;
|
|
17
|
+
},
|
|
18
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TITLE_LINE, stripQuotes } from "../../utils.mjs";
|
|
2
|
+
|
|
3
|
+
export const titlePlugin = {
|
|
4
|
+
name: "sequence.title",
|
|
5
|
+
tryLine(line, ctx) {
|
|
6
|
+
const m = line.match(TITLE_LINE);
|
|
7
|
+
if (!m) return false;
|
|
8
|
+
ctx.setTitle(stripQuotes(m[1].trim()));
|
|
9
|
+
return true;
|
|
10
|
+
},
|
|
11
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Mutable parsing context for sequence diagrams.
|
|
2
|
+
|
|
3
|
+
import { Participant, Message, SequenceDiagram, SequenceNote } from "../model/diagram.mjs";
|
|
4
|
+
|
|
5
|
+
export function createSequenceContext() {
|
|
6
|
+
const diagram = new SequenceDiagram();
|
|
7
|
+
let messageCounter = 0;
|
|
8
|
+
let noteCounter = 0;
|
|
9
|
+
|
|
10
|
+
const ctx = {
|
|
11
|
+
get result() { return diagram; },
|
|
12
|
+
diagram,
|
|
13
|
+
|
|
14
|
+
setTitle(t) { diagram.title = t; },
|
|
15
|
+
|
|
16
|
+
ensureParticipant(id) {
|
|
17
|
+
let p = diagram.participantById(id);
|
|
18
|
+
if (!p) {
|
|
19
|
+
p = new Participant({ id, title: id });
|
|
20
|
+
diagram.addParticipant(p);
|
|
21
|
+
}
|
|
22
|
+
return p;
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
declareParticipant({ id, title, shape = "participant", stereotype = "" }) {
|
|
26
|
+
if (diagram.participantById(id)) return diagram.participantById(id);
|
|
27
|
+
const p = new Participant({ id, title, shape, stereotype });
|
|
28
|
+
diagram.addParticipant(p);
|
|
29
|
+
return p;
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
nextMessageId() { return `seq_${messageCounter++}`; },
|
|
33
|
+
nextNoteId() { return `seqnote_${noteCounter++}`; },
|
|
34
|
+
|
|
35
|
+
addMessage(spec) { diagram.addMessage(new Message({ id: ctx.nextMessageId(), ...spec })); },
|
|
36
|
+
addNote(spec) { diagram.addNote(new SequenceNote({ id: ctx.nextNoteId(), ...spec })); },
|
|
37
|
+
};
|
|
38
|
+
return ctx;
|
|
39
|
+
}
|