@md2nativedocx/core 0.4.0 → 0.6.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/dist/diagrams/plantuml/activity-parser.d.ts +36 -0
- package/dist/diagrams/plantuml/activity-parser.js +699 -0
- package/dist/diagrams/plantuml/activity-parser.js.map +1 -0
- package/dist/diagrams/plantuml/state-parser.d.ts +23 -0
- package/dist/diagrams/plantuml/state-parser.js +256 -0
- package/dist/diagrams/plantuml/state-parser.js.map +1 -0
- package/dist/diagrams/state-diagram/translator.js +64 -6
- package/dist/diagrams/state-diagram/translator.js.map +1 -1
- package/dist/diagrams/state-diagram/types.d.ts +3 -0
- package/dist/layout/layout.js +7 -0
- package/dist/layout/layout.js.map +1 -1
- package/dist/parser/diagram-type.d.ts +4 -0
- package/dist/parser/diagram-type.js +36 -7
- package/dist/parser/diagram-type.js.map +1 -1
- package/dist/render-diagram-odf.js +5 -3
- package/dist/render-diagram-odf.js.map +1 -1
- package/dist/render-diagram.js +19 -5
- package/dist/render-diagram.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parser for PlantUML activity diagrams (the current syntax: `start`, `:action;`, `if`/`while`/`repeat`/
|
|
3
|
+
* `fork`...), into the flowchart AST (`../../types.ts`), so layout, drawing and SmartArt (a straight sequence
|
|
4
|
+
* of actions becomes a SmartArt process) are the flowchart's own. Line-oriented and forgiving like the other
|
|
5
|
+
* parsers: an unrecognized line is a warning, never a throw.
|
|
6
|
+
*
|
|
7
|
+
* The control structures are turned into a graph as they are read. The parser keeps the "open ends" of the
|
|
8
|
+
* flow (the nodes the next element connects from, each with an optional edge label):
|
|
9
|
+
* - `:text;` (multi-line until `;`, or an SDL ending `|` `<` `>` `/` `]` `}`) is a node; `start` and `stop`
|
|
10
|
+
* are small black discs, `end` a crossed circle; `kill` / `detach` end the flow; `(A)` is a connector circle;
|
|
11
|
+
* - `if (c) then (yes)` / `elseif` / `else` / `endif`, and `switch` / `case` / `endswitch`, make a diamond
|
|
12
|
+
* whose branches all continue to whatever follows; `is (x)` / `equals (x)` and `(no) elseif` labels kept;
|
|
13
|
+
* - `while (c) is (x)` ... `endwhile (y)` and `repeat` ... `repeat while (c) is (x) not (y)` loop back to the
|
|
14
|
+
* diamond or the repeat start (`backward:` adds an action on the way back); `break` leaves the loop;
|
|
15
|
+
* - `fork` / `fork again` / `end fork` (and `split`, `end merge`) run their branches side by side (the
|
|
16
|
+
* synchronisation bars are not drawn);
|
|
17
|
+
* - `-> label;` labels the next arrow, `-[hidden]->` makes it invisible; `label` / `goto` jump;
|
|
18
|
+
* - `partition` / `group` / `package` / `rectangle` / `card` `X { ... }` and swimlanes `|Lane|` become
|
|
19
|
+
* subgraphs.
|
|
20
|
+
* Ignored with a warning: notes, `title` / `skinparam` / `<style>` / `!pragma`..., colours; `!include` is
|
|
21
|
+
* never followed (it would read a file or a URL). Creole `**bold**` and `//italic//` are kept, other markup
|
|
22
|
+
* is reduced to its text; `[[link label]]` keeps only the label (no link is ever emitted).
|
|
23
|
+
* Capped (hostile input): 400 nodes, 800 edges, 30 nested blocks, 60 lines per action.
|
|
24
|
+
*/
|
|
25
|
+
const MAX_NODES = 400;
|
|
26
|
+
const MAX_EDGES = 800;
|
|
27
|
+
const MAX_DEPTH = 30;
|
|
28
|
+
const MAX_ACTION_LINES = 60;
|
|
29
|
+
const MAX_LINE = 2000;
|
|
30
|
+
/** SDL / UML stereotypes and endings of an action, to a flowchart shape. */
|
|
31
|
+
const STEREOTYPE_SHAPES = new Map([
|
|
32
|
+
['input', 'parallelogram'],
|
|
33
|
+
['output', 'parallelogramAlt'],
|
|
34
|
+
['procedure', 'subroutine'],
|
|
35
|
+
['load', 'trapezoid'],
|
|
36
|
+
['save', 'trapezoidAlt'],
|
|
37
|
+
['continuous', 'stadium'],
|
|
38
|
+
['task', 'rect'],
|
|
39
|
+
['object', 'rect'],
|
|
40
|
+
['objectsignal', 'asymmetric'],
|
|
41
|
+
['acceptevent', 'asymmetric'],
|
|
42
|
+
['sendsignal', 'asymmetric'],
|
|
43
|
+
['trigger', 'rect'],
|
|
44
|
+
['timeevent', 'hourglass'],
|
|
45
|
+
]);
|
|
46
|
+
const ENDING_SHAPES = new Map([
|
|
47
|
+
['|', 'subroutine'],
|
|
48
|
+
['<', 'asymmetric'],
|
|
49
|
+
['>', 'asymmetric'],
|
|
50
|
+
['/', 'parallelogram'],
|
|
51
|
+
[']', 'rect'],
|
|
52
|
+
['}', 'rect'],
|
|
53
|
+
]);
|
|
54
|
+
/**
|
|
55
|
+
* Whether `line` closes an action: `;`, or an SDL ending `|` `<` `>` `/` `]` `}` that is not doubled (`//italic//`,
|
|
56
|
+
* `>>`, `]]` are markup, not endings), optionally followed by a `<<stereotype>>`.
|
|
57
|
+
*/
|
|
58
|
+
function endsAction(line) {
|
|
59
|
+
const body = line.replace(/\s*<<[^>]*>>\s*$/, '').trimEnd();
|
|
60
|
+
const last = body.charAt(body.length - 1);
|
|
61
|
+
if (last === ';')
|
|
62
|
+
return true;
|
|
63
|
+
if (!'|<>/]}'.includes(last) || last === '')
|
|
64
|
+
return false;
|
|
65
|
+
if (body.charAt(body.length - 2) === last)
|
|
66
|
+
return false;
|
|
67
|
+
// A `>`, `]` or `}` that closes a `<tag>`, `[...]` or `{...}` opened on the same line is markup too.
|
|
68
|
+
const opener = { '>': '<', ']': '[', '}': '{' }[last];
|
|
69
|
+
return opener === undefined || !body.slice(0, -1).includes(opener);
|
|
70
|
+
}
|
|
71
|
+
/** Creole text to label runs: lines, `**bold**`, `//italic//`; other markup reduced to its text. */
|
|
72
|
+
export function creoleRuns(raw) {
|
|
73
|
+
const lines = raw
|
|
74
|
+
.replace(/\\n/g, '\n')
|
|
75
|
+
.replace(/<br\s*\/?>/gi, '\n')
|
|
76
|
+
.split('\n')
|
|
77
|
+
.map((l) => l
|
|
78
|
+
.replace(/\\t/g, ' ')
|
|
79
|
+
.replace(/\[\[([^\]]*)\]\]/g, (_, inner) => (inner.includes(' ') ? inner.slice(inner.indexOf(' ') + 1) : inner)) // [[url label]] -> label; never a link
|
|
80
|
+
.replace(/<img:[^>]*>/gi, '') // an image reference (a file or a URL) is never fetched or kept
|
|
81
|
+
.replace(/<&[\w-]+>/g, '')
|
|
82
|
+
.replace(/<U\+([0-9A-F]{4,6})>/gi, (_, hex) => String.fromCodePoint(Math.min(parseInt(hex, 16), 0x10ffff)))
|
|
83
|
+
.replace(/<\/?(?:b|i|u|s|color|size|font|back|strike|w|plain)\b[^>]*>/gi, '')
|
|
84
|
+
.replace(/""|__|~~|\^\^/g, '')
|
|
85
|
+
.replace(/--([^-\s][^-]*?)--/g, '$1') // --stricken--
|
|
86
|
+
.trim())
|
|
87
|
+
.filter((l, i, all) => l !== '' || (i > 0 && i < all.length - 1));
|
|
88
|
+
const runs = [];
|
|
89
|
+
lines.forEach((line, i) => {
|
|
90
|
+
if (i > 0)
|
|
91
|
+
runs.push({ break: true });
|
|
92
|
+
// Separator lines in a note-like action (`----`, `====`) carry no text.
|
|
93
|
+
if (/^(?:-{3,}|={3,}|\.{3,})$/.test(line))
|
|
94
|
+
return;
|
|
95
|
+
for (const part of line.split(/(\*\*[^*]+\*\*|\/\/[^/]+\/\/)/)) {
|
|
96
|
+
if (part === '')
|
|
97
|
+
continue;
|
|
98
|
+
const run = part.startsWith('**') && part.endsWith('**') && part.length > 4
|
|
99
|
+
? { text: part.slice(2, -2), bold: true }
|
|
100
|
+
: part.startsWith('//') && part.endsWith('//') && part.length > 4
|
|
101
|
+
? { text: part.slice(2, -2), italic: true }
|
|
102
|
+
: { text: part };
|
|
103
|
+
runs.push(run);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const text = runs.map((r) => ('break' in r ? ' ' : r.text)).join('').replace(/\s+/g, ' ').trim();
|
|
107
|
+
return { text, runs };
|
|
108
|
+
}
|
|
109
|
+
/** Parse PlantUML activity-diagram text into the flowchart AST. Never throws on malformed input. */
|
|
110
|
+
export function parsePlantUmlActivity(source) {
|
|
111
|
+
const warnings = [];
|
|
112
|
+
const warned = new Set();
|
|
113
|
+
const warnOnce = (key, message) => {
|
|
114
|
+
if (warned.has(key))
|
|
115
|
+
return;
|
|
116
|
+
warned.add(key);
|
|
117
|
+
warnings.push(message);
|
|
118
|
+
};
|
|
119
|
+
const nodes = [];
|
|
120
|
+
const edges = [];
|
|
121
|
+
const subgraphs = [];
|
|
122
|
+
let counter = 0;
|
|
123
|
+
/** The subgraph new nodes go into: the innermost open group, else the current swimlane. */
|
|
124
|
+
const groupStack = [];
|
|
125
|
+
let lane;
|
|
126
|
+
const lanes = new Map();
|
|
127
|
+
const subgraphById = new Map();
|
|
128
|
+
const newSubgraph = (title, parent) => {
|
|
129
|
+
const id = `__sg_${subgraphs.length}`;
|
|
130
|
+
const sg = { id, title, nodeIds: [], subgraphIds: [] };
|
|
131
|
+
subgraphs.push(sg);
|
|
132
|
+
subgraphById.set(id, sg);
|
|
133
|
+
if (parent)
|
|
134
|
+
subgraphById.get(parent)?.subgraphIds.push(id);
|
|
135
|
+
return id;
|
|
136
|
+
};
|
|
137
|
+
const container = () => groupStack[groupStack.length - 1] ?? lane;
|
|
138
|
+
const addNode = (label, shape, runs) => {
|
|
139
|
+
if (nodes.length >= MAX_NODES) {
|
|
140
|
+
warnOnce('nodes', `Activity diagram limited to ${MAX_NODES} elements; the rest were ignored.`);
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const id = `__a${counter++}`;
|
|
144
|
+
nodes.push({ id, label, labelRuns: runs ?? (label === '' ? [] : [{ text: label }]), shape });
|
|
145
|
+
const sg = container();
|
|
146
|
+
if (sg)
|
|
147
|
+
subgraphById.get(sg)?.nodeIds.push(id);
|
|
148
|
+
return id;
|
|
149
|
+
};
|
|
150
|
+
const addEdge = (from, to, label, type = 'arrow') => {
|
|
151
|
+
if (edges.length >= MAX_EDGES)
|
|
152
|
+
return warnOnce('edges', `Activity diagram limited to ${MAX_EDGES} arrows; the rest were ignored.`);
|
|
153
|
+
const clean = label ? creoleRuns(label) : undefined;
|
|
154
|
+
edges.push(clean && clean.text !== ''
|
|
155
|
+
? { from, to, type, label: clean.text, labelRuns: clean.runs }
|
|
156
|
+
: { from, to, type, label: null, labelRuns: null });
|
|
157
|
+
};
|
|
158
|
+
let tails = [];
|
|
159
|
+
/** Label and style for the next arrow out of the current tails (`-> label;`, `-[hidden]->`). */
|
|
160
|
+
let nextLabel;
|
|
161
|
+
let nextHidden = false;
|
|
162
|
+
const blocks = [];
|
|
163
|
+
const labels = new Map();
|
|
164
|
+
let pendingLabels = [];
|
|
165
|
+
const gotos = [];
|
|
166
|
+
/** Connect the open ends to a new node, which becomes the only open end. */
|
|
167
|
+
const connect = (id) => {
|
|
168
|
+
if (id === undefined)
|
|
169
|
+
return;
|
|
170
|
+
for (const t of tails)
|
|
171
|
+
addEdge(t.id, id, nextLabel ?? t.label, nextHidden ? 'invisible' : 'arrow');
|
|
172
|
+
tails = [{ id }];
|
|
173
|
+
nextLabel = undefined;
|
|
174
|
+
nextHidden = false;
|
|
175
|
+
for (const name of pendingLabels)
|
|
176
|
+
labels.set(name, id);
|
|
177
|
+
pendingLabels = [];
|
|
178
|
+
// The first node of a repeat body is where `repeat while` loops back to.
|
|
179
|
+
for (let i = blocks.length - 1; i >= 0; i--) {
|
|
180
|
+
const b = blocks[i];
|
|
181
|
+
if (b?.kind === 'repeat' && b.start === undefined)
|
|
182
|
+
b.start = id;
|
|
183
|
+
else
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const step = (label, shape, runs) => connect(addNode(label, shape, runs));
|
|
188
|
+
/** A start or stop point: a small black disc. */
|
|
189
|
+
const marker = () => {
|
|
190
|
+
const id = addNode('', 'ellipse');
|
|
191
|
+
const node = nodes.find((n) => n.id === id);
|
|
192
|
+
if (node) {
|
|
193
|
+
node.fill = '000000';
|
|
194
|
+
node.stroke = '000000';
|
|
195
|
+
}
|
|
196
|
+
connect(id);
|
|
197
|
+
};
|
|
198
|
+
/** Tails carry their own label (a branch's `then (yes)`); `nextLabel` overrides it for the next arrow. */
|
|
199
|
+
const labelled = (list, label) => list.map((t) => (label ? { id: t.id, label } : t));
|
|
200
|
+
const open = (block) => {
|
|
201
|
+
if (blocks.length >= MAX_DEPTH) {
|
|
202
|
+
warnOnce('depth', `Activity blocks nested deeper than ${MAX_DEPTH} levels were ignored.`);
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
blocks.push(block);
|
|
206
|
+
return true;
|
|
207
|
+
};
|
|
208
|
+
const top = (kind) => {
|
|
209
|
+
for (let i = blocks.length - 1; i >= 0; i--)
|
|
210
|
+
if (blocks[i]?.kind === kind)
|
|
211
|
+
return blocks[i];
|
|
212
|
+
return undefined;
|
|
213
|
+
};
|
|
214
|
+
const closeTo = (kind) => {
|
|
215
|
+
const b = blocks[blocks.length - 1];
|
|
216
|
+
if (b?.kind === kind)
|
|
217
|
+
return blocks.pop();
|
|
218
|
+
warnings.push(`\`end\` of a ${kind} without a matching opening ignored.`);
|
|
219
|
+
return undefined;
|
|
220
|
+
};
|
|
221
|
+
const diamond = (text) => {
|
|
222
|
+
const { text: plain, runs } = creoleRuns(text);
|
|
223
|
+
const id = addNode(plain, 'diamond', runs);
|
|
224
|
+
connect(id);
|
|
225
|
+
return id;
|
|
226
|
+
};
|
|
227
|
+
/** An action being read over several lines, up to its closing `;` (or SDL ending). */
|
|
228
|
+
let action;
|
|
229
|
+
/** A multi-line arrow label (`-> text` ... `;`). */
|
|
230
|
+
let arrowText;
|
|
231
|
+
let multi;
|
|
232
|
+
let inBlockComment = false;
|
|
233
|
+
const finishAction = (body) => {
|
|
234
|
+
let text = body;
|
|
235
|
+
let shape = 'roundRect';
|
|
236
|
+
const stereo = /<<\s*([#\w]+)\s*>>\s*$/.exec(text);
|
|
237
|
+
if (stereo) {
|
|
238
|
+
const word = stereo[1].toLowerCase();
|
|
239
|
+
if (!word.startsWith('#')) {
|
|
240
|
+
const mapped = STEREOTYPE_SHAPES.get(word);
|
|
241
|
+
if (mapped)
|
|
242
|
+
shape = mapped;
|
|
243
|
+
else
|
|
244
|
+
warnOnce('stereo', `Action stereotypes like \`<<${stereo[1]}>>\` are drawn as plain actions.`);
|
|
245
|
+
}
|
|
246
|
+
text = text.slice(0, stereo.index).trim();
|
|
247
|
+
}
|
|
248
|
+
const ending = text.charAt(text.length - 1);
|
|
249
|
+
if (ending === ';') {
|
|
250
|
+
text = text.slice(0, -1);
|
|
251
|
+
}
|
|
252
|
+
else if (ENDING_SHAPES.has(ending) && endsAction(text)) {
|
|
253
|
+
// An SDL ending picks the shape, unless a stereotype already did.
|
|
254
|
+
if (shape === 'roundRect')
|
|
255
|
+
shape = ENDING_SHAPES.get(ending);
|
|
256
|
+
text = text.slice(0, -1);
|
|
257
|
+
}
|
|
258
|
+
const { text: plain, runs } = creoleRuns(text);
|
|
259
|
+
step(plain, shape, runs);
|
|
260
|
+
};
|
|
261
|
+
for (const rawLine of source.split(/\r?\n/)) {
|
|
262
|
+
const line = rawLine.trim().slice(0, MAX_LINE);
|
|
263
|
+
if (inBlockComment) {
|
|
264
|
+
if (line.includes("'/"))
|
|
265
|
+
inBlockComment = false;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (multi) {
|
|
269
|
+
if (multi.test(line))
|
|
270
|
+
multi = undefined;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (action) {
|
|
274
|
+
// Inside a multi-line action: the text runs to a line ending with `;` (or an SDL ending + stereotype).
|
|
275
|
+
if (action.lines.length < MAX_ACTION_LINES)
|
|
276
|
+
action.lines.push(rawLine.replace(/^\s+/, ''));
|
|
277
|
+
if (endsAction(line)) {
|
|
278
|
+
finishAction(action.lines.join('\n').trim());
|
|
279
|
+
action = undefined;
|
|
280
|
+
}
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (arrowText) {
|
|
284
|
+
arrowText.push(line);
|
|
285
|
+
if (line.endsWith(';')) {
|
|
286
|
+
nextLabel = arrowText.join('\n').replace(/;$/, '');
|
|
287
|
+
arrowText = undefined;
|
|
288
|
+
}
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (line.length === 0 || line.startsWith("'"))
|
|
292
|
+
continue;
|
|
293
|
+
if (line.startsWith("/'")) {
|
|
294
|
+
if (!line.includes("'/", 2))
|
|
295
|
+
inBlockComment = true;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
let m;
|
|
299
|
+
const ctl = controlTokens(line);
|
|
300
|
+
const words = ctl?.filter((t) => 'w' in t).map((t) => t.w).join(' ') ?? '';
|
|
301
|
+
if (/^@(start|end)uml\b/i.test(line)) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
else if (actionBody(line) !== undefined) {
|
|
305
|
+
// An action, possibly coloured (`#pink:text;`), on one line or several; `#blue:(B)` is a connector.
|
|
306
|
+
if (line.startsWith('#'))
|
|
307
|
+
warnOnce('color', 'Colours on actions and swimlanes are ignored.');
|
|
308
|
+
const body = actionBody(line);
|
|
309
|
+
if (/^\([^()]*\)$/.test(body.trim()))
|
|
310
|
+
step(body.trim().slice(1, -1), 'ellipse');
|
|
311
|
+
else if (endsAction(body))
|
|
312
|
+
finishAction(body.trim());
|
|
313
|
+
else
|
|
314
|
+
action = { lines: [body] };
|
|
315
|
+
}
|
|
316
|
+
else if (arrowOf(line) !== undefined) {
|
|
317
|
+
// An arrow: its label (possibly multi-line, up to `;`) goes on the next connection.
|
|
318
|
+
const { style, text } = arrowOf(line);
|
|
319
|
+
if (/hidden/i.test(style))
|
|
320
|
+
nextHidden = true;
|
|
321
|
+
else if (style !== '')
|
|
322
|
+
warnOnce('arrowstyle', 'Arrow colours and styles (`-[#red,dashed]->`) are ignored.');
|
|
323
|
+
if (text === '' || text === ';')
|
|
324
|
+
continue;
|
|
325
|
+
if (text.endsWith(';'))
|
|
326
|
+
nextLabel = text.slice(0, -1);
|
|
327
|
+
else
|
|
328
|
+
arrowText = [text];
|
|
329
|
+
}
|
|
330
|
+
else if (/^start$/i.test(line)) {
|
|
331
|
+
// UML: a filled dot to start, the same dot ringed to stop. Drawn as filled circles (no ringed preset).
|
|
332
|
+
marker();
|
|
333
|
+
}
|
|
334
|
+
else if (/^stop$/i.test(line)) {
|
|
335
|
+
marker();
|
|
336
|
+
tails = [];
|
|
337
|
+
}
|
|
338
|
+
else if (/^end$/i.test(line)) {
|
|
339
|
+
step('', 'crossedCircle');
|
|
340
|
+
tails = [];
|
|
341
|
+
}
|
|
342
|
+
else if (/^(?:kill|detach)$/i.test(line)) {
|
|
343
|
+
tails = [];
|
|
344
|
+
}
|
|
345
|
+
else if (/^\([^()]*\)$/.test(line)) {
|
|
346
|
+
// A connector: a small circle; the flow goes on from it.
|
|
347
|
+
step(line.slice(1, -1), 'ellipse');
|
|
348
|
+
}
|
|
349
|
+
else if (ctl && /^(?:elseif|else if)$/.test(words.replace(/ then$/, '')) && parenCount(ctl) >= 1 && words.endsWith('then')) {
|
|
350
|
+
const b = top('if');
|
|
351
|
+
if (!b || b.kind !== 'if') {
|
|
352
|
+
warnings.push(`\`elseif\` outside an \`if\` ignored: ${line}`);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
b.ends.push(...tails);
|
|
356
|
+
// `(no) elseif (cond) then (yes)`: the leading label is the previous diamond's "no" side.
|
|
357
|
+
const lead = 'p' in ctl[0] ? ctl[0].p : undefined;
|
|
358
|
+
const parens = ctl.filter((t) => 'p' in t).map((t) => t.p);
|
|
359
|
+
const cond = lead !== undefined ? parens[1] : parens[0];
|
|
360
|
+
const yes = lead !== undefined ? parens[2] : parens[1];
|
|
361
|
+
if (cond === undefined)
|
|
362
|
+
continue;
|
|
363
|
+
tails = b.pending.map((t) => ({ id: t.id, ...(lead ? { label: lead } : {}) }));
|
|
364
|
+
const d = diamond(cond);
|
|
365
|
+
if (!d)
|
|
366
|
+
continue;
|
|
367
|
+
b.diamond = d;
|
|
368
|
+
tails = [{ id: d, ...(yes ? { label: yes } : {}) }];
|
|
369
|
+
b.pending = [{ id: d }];
|
|
370
|
+
}
|
|
371
|
+
else if (ctl && /^if( is| equals)? then$/.test(words) && 'w' in ctl[0]) {
|
|
372
|
+
const parens = ctl.filter((t) => 'p' in t).map((t) => t.p);
|
|
373
|
+
const d = diamond(parens[0] ?? '');
|
|
374
|
+
if (!d)
|
|
375
|
+
continue;
|
|
376
|
+
// `then (yes)` labels the "yes" side; without it, `is (x)` / `equals (x)` does.
|
|
377
|
+
const yes = words === 'if then' ? parens[1] : (parens[2] ?? parens[1]);
|
|
378
|
+
if (!open({ kind: 'if', diamond: d, pending: [{ id: d }], ends: [], hasElse: false }))
|
|
379
|
+
continue;
|
|
380
|
+
tails = [{ id: d, ...(yes ? { label: yes } : {}) }];
|
|
381
|
+
}
|
|
382
|
+
else if (ctl && words === 'else' && 'w' in ctl[0]) {
|
|
383
|
+
const b = top('if');
|
|
384
|
+
if (!b || b.kind !== 'if') {
|
|
385
|
+
warnings.push(`\`else\` outside an \`if\` ignored: ${line}`);
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
b.ends.push(...tails);
|
|
389
|
+
b.hasElse = true;
|
|
390
|
+
tails = labelled(b.pending, ctl.find((t) => 'p' in t)?.p);
|
|
391
|
+
}
|
|
392
|
+
else if (/^end\s?if$/i.test(line)) {
|
|
393
|
+
const b = closeTo('if');
|
|
394
|
+
if (!b || b.kind !== 'if')
|
|
395
|
+
continue;
|
|
396
|
+
tails = [...b.ends, ...tails, ...(b.hasElse ? [] : b.pending)];
|
|
397
|
+
}
|
|
398
|
+
else if (ctl && (words === 'switch' || words === 'case') && parenCount(ctl) === 1) {
|
|
399
|
+
const text = ctl.find((t) => 'p' in t).p;
|
|
400
|
+
if (words === 'switch') {
|
|
401
|
+
const d = diamond(text);
|
|
402
|
+
if (!d)
|
|
403
|
+
continue;
|
|
404
|
+
if (!open({ kind: 'switch', diamond: d, ends: [], started: false }))
|
|
405
|
+
continue;
|
|
406
|
+
tails = [];
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
const b = top('switch');
|
|
410
|
+
if (!b || b.kind !== 'switch') {
|
|
411
|
+
warnings.push(`\`case\` outside a \`switch\` ignored: ${line}`);
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
b.ends.push(...tails);
|
|
415
|
+
tails = [{ id: b.diamond, label: text.trim() }];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
else if (/^end\s?switch$/i.test(line)) {
|
|
419
|
+
const b = closeTo('switch');
|
|
420
|
+
if (!b || b.kind !== 'switch')
|
|
421
|
+
continue;
|
|
422
|
+
tails = [...b.ends, ...tails];
|
|
423
|
+
}
|
|
424
|
+
else if (ctl && /^while( is)?$/.test(words)) {
|
|
425
|
+
const parens = ctl.filter((t) => 'p' in t).map((t) => t.p);
|
|
426
|
+
const d = diamond(parens[0] ?? '');
|
|
427
|
+
if (!d)
|
|
428
|
+
continue;
|
|
429
|
+
if (!open({ kind: 'while', diamond: d, breaks: [] }))
|
|
430
|
+
continue;
|
|
431
|
+
tails = [{ id: d, ...(parens[1] ? { label: parens[1] } : {}) }];
|
|
432
|
+
}
|
|
433
|
+
else if (ctl && (words === 'endwhile' || words === 'end while')) {
|
|
434
|
+
const b = closeTo('while');
|
|
435
|
+
if (!b || b.kind !== 'while')
|
|
436
|
+
continue;
|
|
437
|
+
// Back to the condition, through the `backward` action if there is one.
|
|
438
|
+
let back;
|
|
439
|
+
if (b.backward !== undefined) {
|
|
440
|
+
const { text, runs } = creoleRuns(b.backward);
|
|
441
|
+
back = addNode(text, 'roundRect', runs);
|
|
442
|
+
}
|
|
443
|
+
for (const t of tails)
|
|
444
|
+
addEdge(t.id, back ?? b.diamond, nextLabel ?? t.label);
|
|
445
|
+
if (back)
|
|
446
|
+
addEdge(back, b.diamond);
|
|
447
|
+
nextLabel = undefined;
|
|
448
|
+
const no = ctl.find((t) => 'p' in t)?.p;
|
|
449
|
+
tails = [{ id: b.diamond, ...(no ? { label: no } : {}) }, ...b.breaks];
|
|
450
|
+
}
|
|
451
|
+
else if (ctl && /^(?:repeat while|repeatwhile)( is)?( not)?$/.test(words)) {
|
|
452
|
+
const b = closeTo('repeat');
|
|
453
|
+
if (!b || b.kind !== 'repeat')
|
|
454
|
+
continue;
|
|
455
|
+
const parens = ctl.filter((t) => 'p' in t).map((t) => t.p);
|
|
456
|
+
const d = diamond(parens[0] ?? '');
|
|
457
|
+
if (!d)
|
|
458
|
+
continue;
|
|
459
|
+
// `is (x)` labels the way back, `not (y)` the way out; with only `not`, the second paren is the way out.
|
|
460
|
+
const isLabel = / is/.test(words) ? parens[1] : undefined;
|
|
461
|
+
const notLabel = / not/.test(words) ? parens[/ is/.test(words) ? 2 : 1] : undefined;
|
|
462
|
+
// Back to the first element of the body, through the `backward` action if there is one.
|
|
463
|
+
let from = d;
|
|
464
|
+
let label = isLabel;
|
|
465
|
+
if (b.backward !== undefined) {
|
|
466
|
+
const { text, runs } = creoleRuns(b.backward);
|
|
467
|
+
const back = addNode(text, 'roundRect', runs);
|
|
468
|
+
if (back) {
|
|
469
|
+
addEdge(d, back, label);
|
|
470
|
+
from = back;
|
|
471
|
+
label = undefined;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
if (b.start)
|
|
475
|
+
addEdge(from, b.start, label);
|
|
476
|
+
tails = [{ id: d, ...(notLabel ? { label: notLabel } : {}) }, ...b.breaks];
|
|
477
|
+
}
|
|
478
|
+
else if (/^repeat\b/i.test(line)) {
|
|
479
|
+
if (!open({ kind: 'repeat', startTails: tails, breaks: [] }))
|
|
480
|
+
continue;
|
|
481
|
+
const rest = line.slice('repeat'.length).trim();
|
|
482
|
+
if (rest.startsWith(':')) {
|
|
483
|
+
const { text, runs } = creoleRuns(rest.slice(1).replace(/;$/, ''));
|
|
484
|
+
step(text, 'roundRect', runs);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
else if (/^backward\s*:/i.test(line)) {
|
|
488
|
+
const loop = [...blocks].reverse().find((b) => b.kind === 'repeat' || b.kind === 'while');
|
|
489
|
+
const text = line.slice(line.indexOf(':') + 1).replace(/;$/, '');
|
|
490
|
+
if (loop && (loop.kind === 'repeat' || loop.kind === 'while'))
|
|
491
|
+
loop.backward = text;
|
|
492
|
+
else
|
|
493
|
+
warnings.push(`\`backward\` outside a loop ignored: ${line}`);
|
|
494
|
+
}
|
|
495
|
+
else if (/^break$/i.test(line)) {
|
|
496
|
+
const loop = [...blocks].reverse().find((b) => b.kind === 'repeat' || b.kind === 'while');
|
|
497
|
+
if (loop && (loop.kind === 'repeat' || loop.kind === 'while'))
|
|
498
|
+
loop.breaks.push(...tails);
|
|
499
|
+
else
|
|
500
|
+
warnings.push('`break` outside a loop ignored.');
|
|
501
|
+
tails = [];
|
|
502
|
+
}
|
|
503
|
+
else if (/^(?:fork|split)$/i.test(line)) {
|
|
504
|
+
warnOnce('fork', 'Fork and split bars are not drawn; their branches are side by side.');
|
|
505
|
+
if (!open({ kind: 'fork', origin: tails, ends: [] }))
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
else if (/^(?:fork|split)\s+again$/i.test(line)) {
|
|
509
|
+
const b = top('fork');
|
|
510
|
+
if (!b || b.kind !== 'fork') {
|
|
511
|
+
warnings.push(`\`${line}\` outside a fork ignored.`);
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
b.ends.push(...tails);
|
|
515
|
+
tails = b.origin;
|
|
516
|
+
}
|
|
517
|
+
else if (/^end\s?(?:fork|split|merge)\b/i.test(line)) {
|
|
518
|
+
const b = closeTo('fork');
|
|
519
|
+
if (!b || b.kind !== 'fork')
|
|
520
|
+
continue;
|
|
521
|
+
tails = [...b.ends, ...tails];
|
|
522
|
+
}
|
|
523
|
+
else if ((m = line.match(/^label\s+(\S+)$/i))) {
|
|
524
|
+
pendingLabels.push(m[1]);
|
|
525
|
+
}
|
|
526
|
+
else if ((m = line.match(/^goto\s+(\S+)$/i))) {
|
|
527
|
+
gotos.push({ from: tails, target: m[1] });
|
|
528
|
+
tails = [];
|
|
529
|
+
}
|
|
530
|
+
else if (line.startsWith('|') && swimlaneOf(line) !== undefined) {
|
|
531
|
+
// A swimlane: `|Lane|`, `|#color|Lane|`, `|id| Title`.
|
|
532
|
+
const { name, title } = swimlaneOf(line);
|
|
533
|
+
if (!lanes.has(name))
|
|
534
|
+
lanes.set(name, newSubgraph(creoleRuns(title).text));
|
|
535
|
+
lane = lanes.get(name);
|
|
536
|
+
}
|
|
537
|
+
else if ((m = line.match(/^(partition|group|package|rectangle|card)\s/i)) && line.endsWith('{')) {
|
|
538
|
+
const raw = line.slice(m[1].length, -1).trim().replace(/^#\S+\s+/, '');
|
|
539
|
+
const title = creoleRuns(unquote(raw)).text;
|
|
540
|
+
if (open({ kind: 'group', subgraph: newSubgraph(title, container()) }))
|
|
541
|
+
groupStack.push(subgraphs[subgraphs.length - 1]?.id);
|
|
542
|
+
}
|
|
543
|
+
else if (line === '}') {
|
|
544
|
+
const b = blocks[blocks.length - 1];
|
|
545
|
+
if (b?.kind === 'group') {
|
|
546
|
+
blocks.pop();
|
|
547
|
+
groupStack.pop();
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
warnings.push('`}` without an open group ignored.');
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
else if (/^note\b/i.test(line) || /^floating\s+note\b/i.test(line)) {
|
|
554
|
+
warnOnce('note', 'Notes are not drawn in activity diagrams.');
|
|
555
|
+
if (!/(?<!:):(?!:)/.test(line))
|
|
556
|
+
multi = /^end\s*note$/i;
|
|
557
|
+
}
|
|
558
|
+
else if (/^!include/i.test(line)) {
|
|
559
|
+
warnOnce('include', '`!include` is not followed (it would read a file or a URL); its content is missing.');
|
|
560
|
+
}
|
|
561
|
+
else if (/^!/.test(line)) {
|
|
562
|
+
warnOnce('preprocessor', 'PlantUML preprocessor lines (`!pragma`, `!theme`, variables...) are ignored.');
|
|
563
|
+
}
|
|
564
|
+
else if (/^skinparam\b/i.test(line)) {
|
|
565
|
+
warnOnce('skinparam', '`skinparam` styling is ignored; the diagram uses the default look.');
|
|
566
|
+
if (line.endsWith('{'))
|
|
567
|
+
multi = /^\}$/;
|
|
568
|
+
}
|
|
569
|
+
else if (/^<style>/i.test(line)) {
|
|
570
|
+
warnOnce('style', '`<style>` blocks are ignored; the diagram uses the default look.');
|
|
571
|
+
if (!/<\/style>\s*$/i.test(line))
|
|
572
|
+
multi = /^<\/style>$/i;
|
|
573
|
+
}
|
|
574
|
+
else if (/^(?:title|caption|header|footer|legend|scale|hide|show|left to right direction|top to bottom direction|link|mainframe)\b/i.test(line)) {
|
|
575
|
+
warnOnce('display', 'Display commands (`title`, `header`, `scale`, ...) are ignored.');
|
|
576
|
+
if (/^legend\b/i.test(line))
|
|
577
|
+
multi = /^end\s*legend$/i;
|
|
578
|
+
}
|
|
579
|
+
else if ((m = line.match(/^-(?![->\s])(.+)$/))) {
|
|
580
|
+
// `-text` (no arrowhead): a label for the next arrow.
|
|
581
|
+
nextLabel = m[1].replace(/<<[^>]*>>\s*$/, '').replace(/;$/, '');
|
|
582
|
+
}
|
|
583
|
+
else if ((m = line.match(/^(?:[-*]+)\s+(.+)$/))) {
|
|
584
|
+
// List-style actions (`- Action`, `* Action`, `** Sub-action`).
|
|
585
|
+
const { text, runs } = creoleRuns(m[1]);
|
|
586
|
+
step(text, 'roundRect', runs);
|
|
587
|
+
}
|
|
588
|
+
else {
|
|
589
|
+
warnings.push(`Unsupported line ignored: ${line}`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (action)
|
|
593
|
+
warnings.push('An action was never closed with `;`; its text was dropped.');
|
|
594
|
+
if (multi)
|
|
595
|
+
warnings.push('A multi-line block (`note`, `legend`, `<style>`) was never closed; the rest was ignored.');
|
|
596
|
+
if (blocks.some((b) => b.kind !== 'group'))
|
|
597
|
+
warnings.push('One or more blocks (`if`, `while`, `repeat`, `fork`...) were never closed.');
|
|
598
|
+
for (const g of gotos) {
|
|
599
|
+
const target = labels.get(g.target);
|
|
600
|
+
if (target === undefined) {
|
|
601
|
+
warnings.push(`\`goto ${g.target}\` has no matching \`label\`; ignored.`);
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
for (const t of g.from)
|
|
605
|
+
addEdge(t.id, target, t.label);
|
|
606
|
+
}
|
|
607
|
+
// Empty subgraphs (a swimlane or a partition with nothing in it) are dropped.
|
|
608
|
+
const used = subgraphs.filter((sg) => sg.nodeIds.length > 0 || sg.subgraphIds.length > 0);
|
|
609
|
+
const usedIds = new Set(used.map((sg) => sg.id));
|
|
610
|
+
for (const sg of used)
|
|
611
|
+
sg.subgraphIds = sg.subgraphIds.filter((id) => usedIds.has(id));
|
|
612
|
+
return { ast: { direction: 'TD', nodes, edges, subgraphs: used }, warnings };
|
|
613
|
+
}
|
|
614
|
+
function controlTokens(line) {
|
|
615
|
+
const out = [];
|
|
616
|
+
let i = 0;
|
|
617
|
+
while (i < line.length) {
|
|
618
|
+
const c = line.charAt(i);
|
|
619
|
+
if (c === ' ' || c === '\t') {
|
|
620
|
+
i++;
|
|
621
|
+
}
|
|
622
|
+
else if (c === '(') {
|
|
623
|
+
let depth = 1;
|
|
624
|
+
let j = i + 1;
|
|
625
|
+
for (; j < line.length && depth > 0; j++) {
|
|
626
|
+
if (line.charAt(j) === '(')
|
|
627
|
+
depth++;
|
|
628
|
+
else if (line.charAt(j) === ')')
|
|
629
|
+
depth--;
|
|
630
|
+
}
|
|
631
|
+
if (depth !== 0)
|
|
632
|
+
return undefined;
|
|
633
|
+
out.push({ p: line.slice(i + 1, j - 1).trim() });
|
|
634
|
+
i = j;
|
|
635
|
+
}
|
|
636
|
+
else if (/[A-Za-z]/.test(c)) {
|
|
637
|
+
let j = i;
|
|
638
|
+
while (j < line.length && /[A-Za-z]/.test(line.charAt(j)))
|
|
639
|
+
j++;
|
|
640
|
+
out.push({ w: line.slice(i, j).toLowerCase() });
|
|
641
|
+
i = j;
|
|
642
|
+
}
|
|
643
|
+
else if (c === ';' && i === line.length - 1) {
|
|
644
|
+
i++;
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
return undefined;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return out;
|
|
651
|
+
}
|
|
652
|
+
const parenCount = (toks) => toks.filter((t) => 'p' in t).length;
|
|
653
|
+
/** The text of an action line (`:text...`, `#color:text...`), or undefined when `line` is not one. */
|
|
654
|
+
function actionBody(line) {
|
|
655
|
+
if (line.startsWith(':'))
|
|
656
|
+
return line.slice(1);
|
|
657
|
+
if (!line.startsWith('#'))
|
|
658
|
+
return undefined;
|
|
659
|
+
const colon = line.indexOf(':');
|
|
660
|
+
if (colon < 2 || /\s/.test(line.slice(0, colon)))
|
|
661
|
+
return undefined;
|
|
662
|
+
return line.slice(colon + 1);
|
|
663
|
+
}
|
|
664
|
+
/** An arrow line `-> text`, `-[#red,dashed]-> text`, `--> text`: its style and text, or undefined. */
|
|
665
|
+
function arrowOf(line) {
|
|
666
|
+
let i = 0;
|
|
667
|
+
while (line.charAt(i) === '-')
|
|
668
|
+
i++;
|
|
669
|
+
if (i === 0)
|
|
670
|
+
return undefined;
|
|
671
|
+
let style = '';
|
|
672
|
+
if (line.charAt(i) === '[') {
|
|
673
|
+
const close = line.indexOf(']', i);
|
|
674
|
+
if (close === -1)
|
|
675
|
+
return undefined;
|
|
676
|
+
style = line.slice(i, close + 1);
|
|
677
|
+
i = close + 1;
|
|
678
|
+
}
|
|
679
|
+
while (line.charAt(i) === '-')
|
|
680
|
+
i++;
|
|
681
|
+
if (line.charAt(i) !== '>')
|
|
682
|
+
return undefined;
|
|
683
|
+
return { style, text: line.slice(i + 1).trim() };
|
|
684
|
+
}
|
|
685
|
+
/** A swimlane line `|Lane|`, `|#color|Lane|`, `|id| Title`: its name and title, or undefined. */
|
|
686
|
+
function swimlaneOf(line) {
|
|
687
|
+
const parts = line.split('|');
|
|
688
|
+
// parts[0] is empty (the line starts with `|`); a colour part comes first when present.
|
|
689
|
+
let k = 1;
|
|
690
|
+
if ((parts[k] ?? '').startsWith('#') && parts.length > 3)
|
|
691
|
+
k++;
|
|
692
|
+
const name = (parts[k] ?? '').trim();
|
|
693
|
+
if (name === '' || parts.length < k + 2)
|
|
694
|
+
return undefined;
|
|
695
|
+
const title = parts.slice(k + 1).join('|').trim() || name;
|
|
696
|
+
return { name, title };
|
|
697
|
+
}
|
|
698
|
+
const unquote = (s) => s.trim().replace(/^"(.*)"$/, '$1');
|
|
699
|
+
//# sourceMappingURL=activity-parser.js.map
|