@standarx/graph 0.0.0-stage → 1.0.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.
Files changed (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.fr.md +436 -0
  3. package/README.md +435 -2
  4. package/THIRD_PARTY_NOTICES.md +22 -0
  5. package/assets/fonts/Inter-Regular.ttf +0 -0
  6. package/assets/fonts/Inter-SemiBold.ttf +0 -0
  7. package/assets/fonts/OFL.txt +92 -0
  8. package/dist/adapter-0bggwh8x.js +393 -0
  9. package/dist/cli-4m00785g.js +20 -0
  10. package/dist/cli-5v9761s9.js +330 -0
  11. package/dist/cli-9f1pdw5q.js +462 -0
  12. package/dist/cli-a4ax1vkc.js +172 -0
  13. package/dist/cli-b4n941t8.js +1439 -0
  14. package/dist/cli-eqq7a3wz.js +1144 -0
  15. package/dist/cli-gh5166k6.js +1565 -0
  16. package/dist/cli-kzp16pt5.js +207 -0
  17. package/dist/cli-mzmjkk5v.js +11 -0
  18. package/dist/cli-njgyf690.js +1060 -0
  19. package/dist/cli-nkm2j980.js +141 -0
  20. package/dist/cli-nyckgf1t.js +4 -0
  21. package/dist/cli-sfdqkbd1.js +433 -0
  22. package/dist/cli-txrzzr2q.js +0 -0
  23. package/dist/cli-vecew2na.js +709 -0
  24. package/dist/cli-wt9dmbhm.js +560 -0
  25. package/dist/cli-xhdxp3cj.js +387 -0
  26. package/dist/cli-ywgtv1cg.js +39 -0
  27. package/dist/cli.d.ts +2 -0
  28. package/dist/cli.js +415 -0
  29. package/dist/core/index.d.ts +6 -0
  30. package/dist/core/index.js +93 -0
  31. package/dist/core/spatial.d.ts +16 -0
  32. package/dist/core/threshold.d.ts +1 -0
  33. package/dist/core/view.d.ts +80 -0
  34. package/dist/export/index.d.ts +18 -0
  35. package/dist/export/index.js +172 -0
  36. package/dist/export/pdf.d.ts +5 -0
  37. package/dist/export/png.d.ts +4 -0
  38. package/dist/format/index.d.ts +4 -0
  39. package/dist/format/index.js +71 -0
  40. package/dist/format/json-schema.d.ts +2 -0
  41. package/dist/format/schema.d.ts +787 -0
  42. package/dist/format/semantic/er.d.ts +3 -0
  43. package/dist/format/semantic/graph.d.ts +3 -0
  44. package/dist/format/semantic/sequence.d.ts +3 -0
  45. package/dist/format/semantic/shared.d.ts +14 -0
  46. package/dist/format/semantic/state.d.ts +3 -0
  47. package/dist/format/semantic/tree.d.ts +3 -0
  48. package/dist/format/transition-label.d.ts +2 -0
  49. package/dist/format/validate.d.ts +11 -0
  50. package/dist/html/index.d.ts +3 -0
  51. package/dist/html/index.js +127 -0
  52. package/dist/html/layout-worker.d.ts +1 -0
  53. package/dist/html/layout-worker.generated.d.ts +1 -0
  54. package/dist/html/mount.d.ts +12 -0
  55. package/dist/html/page.d.ts +14 -0
  56. package/dist/html/render.d.ts +7 -0
  57. package/dist/html/runtime.d.ts +1 -0
  58. package/dist/html/runtime.generated.d.ts +1 -0
  59. package/dist/html/shell.d.ts +10 -0
  60. package/dist/html/svg-adapter.d.ts +8 -0
  61. package/dist/html/ui.d.ts +34 -0
  62. package/dist/language/analysis.d.ts +38 -0
  63. package/dist/language/complete.d.ts +3 -0
  64. package/dist/language/docs.d.ts +11 -0
  65. package/dist/language/format.d.ts +17 -0
  66. package/dist/language/hover.d.ts +3 -0
  67. package/dist/language/index.d.ts +6 -0
  68. package/dist/language/index.js +1385 -0
  69. package/dist/language/navigation.d.ts +9 -0
  70. package/dist/language/positions.d.ts +7 -0
  71. package/dist/language/symbols.d.ts +3 -0
  72. package/dist/language/types.d.ts +34 -0
  73. package/dist/layout/elk.d.ts +35 -0
  74. package/dist/layout/er.d.ts +3 -0
  75. package/dist/layout/font-metrics.d.ts +9 -0
  76. package/dist/layout/graph.d.ts +3 -0
  77. package/dist/layout/index.d.ts +5 -0
  78. package/dist/layout/index.js +17 -0
  79. package/dist/layout/measure.d.ts +10 -0
  80. package/dist/layout/sequence.d.ts +3 -0
  81. package/dist/layout/shapes.d.ts +16 -0
  82. package/dist/layout/shared.d.ts +31 -0
  83. package/dist/layout/state.d.ts +3 -0
  84. package/dist/layout/tree.d.ts +3 -0
  85. package/dist/layout/types.d.ts +198 -0
  86. package/dist/layout-worker.generated-emmvgfb1.js +34 -0
  87. package/dist/mermaid/common.d.ts +36 -0
  88. package/dist/mermaid/er.d.ts +3 -0
  89. package/dist/mermaid/graph.d.ts +4 -0
  90. package/dist/mermaid/index.d.ts +6 -0
  91. package/dist/mermaid/index.js +6 -0
  92. package/dist/mermaid/mindmap.d.ts +3 -0
  93. package/dist/mermaid/sequence.d.ts +3 -0
  94. package/dist/mermaid/state.d.ts +3 -0
  95. package/dist/remark/index.d.ts +9 -0
  96. package/dist/remark/index.js +104 -0
  97. package/dist/shared/errors.d.ts +6 -0
  98. package/dist/shared/package-root.d.ts +5 -0
  99. package/dist/svg/escape.d.ts +1 -0
  100. package/dist/svg/index.d.ts +3 -0
  101. package/dist/svg/index.js +25 -0
  102. package/dist/svg/render.d.ts +9 -0
  103. package/dist/svg/theme.d.ts +62 -0
  104. package/dist/svg-adapter-gq5vmp6q.js +227 -0
  105. package/dist/text/build.d.ts +10 -0
  106. package/dist/text/canonical.d.ts +10 -0
  107. package/dist/text/errors.d.ts +33 -0
  108. package/dist/text/highlight.d.ts +13 -0
  109. package/dist/text/index.d.ts +33 -0
  110. package/dist/text/index.js +27 -0
  111. package/dist/text/lexer.d.ts +29 -0
  112. package/dist/text/parser.d.ts +160 -0
  113. package/dist/text/print.d.ts +6 -0
  114. package/dist/text/slug.d.ts +1 -0
  115. package/dist/text/syntax.d.ts +8 -0
  116. package/dist/version.d.ts +1 -0
  117. package/dist/webgl/adapter.d.ts +4 -0
  118. package/dist/webgl/index.d.ts +7 -0
  119. package/dist/webgl/index.js +15 -0
  120. package/dist/webgl/runtime.d.ts +1 -0
  121. package/dist/webgl/runtime.generated.d.ts +1 -0
  122. package/grammars/standargraph.tmLanguage.json +599 -0
  123. package/language-configuration.json +35 -0
  124. package/licenses/EPL-2.0.txt +277 -0
  125. package/licenses/MPL-2.0.txt +373 -0
  126. package/package.json +133 -4
  127. package/schema/graph-document.v1.json +1737 -0
@@ -0,0 +1,1060 @@
1
+ // src/text/errors.ts
2
+ var STOP = Symbol("too many errors");
3
+ var MAX_ERRORS = 50;
4
+
5
+ class LineMap {
6
+ starts = [0];
7
+ constructor(source) {
8
+ for (let index = 0;index < source.length; index++) {
9
+ if (source.charCodeAt(index) === 10)
10
+ this.starts.push(index + 1);
11
+ }
12
+ }
13
+ line(offset) {
14
+ let low = 0;
15
+ let high = this.starts.length - 1;
16
+ while (low < high) {
17
+ const middle = low + high + 1 >> 1;
18
+ if (this.starts[middle] <= offset)
19
+ low = middle;
20
+ else
21
+ high = middle - 1;
22
+ }
23
+ return low + 1;
24
+ }
25
+ position(offset) {
26
+ const line = this.line(offset);
27
+ return { line, column: offset - this.starts[line - 1] + 1 };
28
+ }
29
+ error(code, range, message) {
30
+ const start = this.position(range.start);
31
+ const end = this.position(range.end);
32
+ return {
33
+ line: start.line,
34
+ column: start.column,
35
+ endLine: end.line,
36
+ endColumn: end.column,
37
+ code,
38
+ message
39
+ };
40
+ }
41
+ }
42
+
43
+ class Sink {
44
+ lines;
45
+ errors = [];
46
+ muted = 0;
47
+ constructor(lines) {
48
+ this.lines = lines;
49
+ }
50
+ lineOf(offset) {
51
+ return this.lines.line(offset);
52
+ }
53
+ report(code, range, message) {
54
+ if (this.muted > 0)
55
+ return;
56
+ if (this.errors.length >= MAX_ERRORS) {
57
+ this.errors.push(this.lines.error("E_TOKEN", range, "Too many errors"));
58
+ throw STOP;
59
+ }
60
+ this.errors.push(this.lines.error(code, range, message));
61
+ }
62
+ sorted() {
63
+ return this.errors.map((error, order) => ({ error, order })).sort((a, b) => a.error.line - b.error.line || a.error.column - b.error.column || a.order - b.order).map(({ error }) => error);
64
+ }
65
+ }
66
+
67
+ // src/text/lexer.ts
68
+ var OPERATORS = [
69
+ ["-->", "REPLY"],
70
+ ["->>", "ASYNC"],
71
+ ["->", "ARROW"],
72
+ ["--", "LINE"]
73
+ ];
74
+ var CARD = /0\.\.1|1\.\.\*|0\.\.\*/y;
75
+ var NUMBER = /-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?/y;
76
+ var IDENT = /[\p{L}_][\p{L}\p{N}_]*(?:[./-][\p{L}\p{N}_]+)*/uy;
77
+ var UNICODE_ESCAPE = /^\{([0-9a-fA-F]{1,6})\}/;
78
+ var SIMPLE_ESCAPES = { n: `
79
+ `, t: "\t" };
80
+ var PUNCTUATION = "{}[]()=,*";
81
+ function normalize(source) {
82
+ return source.replace(/^/, "").replace(/\r\n/g, `
83
+ `);
84
+ }
85
+
86
+ class Lexer {
87
+ source;
88
+ sink;
89
+ comments;
90
+ pos = 0;
91
+ depth = 0;
92
+ openAt = 0;
93
+ constructor(source, sink, comments) {
94
+ this.source = source;
95
+ this.sink = sink;
96
+ this.comments = comments;
97
+ }
98
+ seek(pos) {
99
+ this.pos = pos;
100
+ this.depth = 0;
101
+ }
102
+ next() {
103
+ const source = this.source;
104
+ let char;
105
+ for (;; ) {
106
+ char = source[this.pos];
107
+ while (char === " " || char === "\t")
108
+ char = source[++this.pos];
109
+ if (char === "#") {
110
+ const commentStart = this.pos;
111
+ while (char !== undefined && char !== `
112
+ `)
113
+ char = source[++this.pos];
114
+ this.comments?.push({ start: commentStart, end: this.pos });
115
+ }
116
+ if (char === `
117
+ `) {
118
+ const start = this.pos++;
119
+ if (this.depth > 0)
120
+ continue;
121
+ return this.token("NEWLINE", start, this.pos);
122
+ }
123
+ break;
124
+ }
125
+ if (char === undefined) {
126
+ if (this.depth === 0)
127
+ return this.token("EOF", this.pos, this.pos);
128
+ const at = this.openAt;
129
+ this.depth = 0;
130
+ this.sink.report("E_EXPECTED", { start: at, end: at + 1 }, `Expected "]" before the end of the file: the "[" opened at line ${this.sink.lineOf(at)} is never closed`);
131
+ return this.token("ERROR", at, at + 1);
132
+ }
133
+ const start = this.pos;
134
+ if (char === '"')
135
+ return this.quoted(start, '"', "STRING");
136
+ if (char === "`")
137
+ return this.quoted(start, "`", "QUOTED_ID");
138
+ for (const [text, kind] of OPERATORS) {
139
+ if (source.startsWith(text, start))
140
+ return this.advance(kind, start, text.length);
141
+ }
142
+ CARD.lastIndex = start;
143
+ const card = CARD.exec(source);
144
+ if (card)
145
+ return this.advance("CARD", start, card[0].length);
146
+ if (source.startsWith("..", start))
147
+ return this.advance("DOTS", start, 2);
148
+ if (PUNCTUATION.includes(char)) {
149
+ if (char === "[") {
150
+ if (this.depth === 0)
151
+ this.openAt = start;
152
+ this.depth++;
153
+ } else if (char === "]" && this.depth > 0) {
154
+ this.depth--;
155
+ }
156
+ return this.advance("PUNCT", start, 1);
157
+ }
158
+ NUMBER.lastIndex = start;
159
+ const number = NUMBER.exec(source);
160
+ if (number)
161
+ return this.advance("NUMBER", start, number[0].length);
162
+ IDENT.lastIndex = start;
163
+ const ident = IDENT.exec(source);
164
+ if (ident)
165
+ return this.advance("IDENT", start, ident[0].length);
166
+ const unexpected = String.fromCodePoint(source.codePointAt(start));
167
+ const end = start + unexpected.length;
168
+ this.sink.report("E_TOKEN", { start, end }, `Unexpected character "${unexpected}"`);
169
+ this.pos = end;
170
+ return this.token("ERROR", start, end);
171
+ }
172
+ token(kind, start, end, value) {
173
+ const text = this.source.slice(start, end);
174
+ return { kind, text, value: value ?? text, start, end };
175
+ }
176
+ advance(kind, start, length) {
177
+ this.pos = start + length;
178
+ return this.token(kind, start, this.pos);
179
+ }
180
+ quoted(start, quote, kind) {
181
+ const source = this.source;
182
+ const what = kind === "STRING" ? "string" : "quoted id";
183
+ let index = start + 1;
184
+ let value = "";
185
+ let bad = false;
186
+ for (;; ) {
187
+ const char = source[index];
188
+ if (char === undefined || char === `
189
+ `) {
190
+ this.sink.report("E_STRING", { start, end: index }, `Unterminated ${what}`);
191
+ this.pos = index;
192
+ return this.token("ERROR", start, index);
193
+ }
194
+ if (char === quote) {
195
+ index++;
196
+ break;
197
+ }
198
+ if (char !== "\\") {
199
+ value += char;
200
+ index++;
201
+ continue;
202
+ }
203
+ const escape = source[index + 1];
204
+ if (escape === undefined || escape === `
205
+ `) {
206
+ index++;
207
+ continue;
208
+ }
209
+ if (escape === quote || escape === "\\") {
210
+ value += escape;
211
+ index += 2;
212
+ continue;
213
+ }
214
+ if (kind === "STRING" && Object.hasOwn(SIMPLE_ESCAPES, escape)) {
215
+ value += SIMPLE_ESCAPES[escape];
216
+ index += 2;
217
+ continue;
218
+ }
219
+ if (kind === "STRING" && escape === "u") {
220
+ const match = UNICODE_ESCAPE.exec(source.slice(index + 2, index + 10));
221
+ const code = match ? Number.parseInt(match[1], 16) : -1;
222
+ if (match && code <= 1114111 && (code < 55296 || code > 57343)) {
223
+ value += String.fromCodePoint(code);
224
+ index += 2 + match[0].length;
225
+ continue;
226
+ }
227
+ const length = match ? 2 + match[0].length : 2;
228
+ this.sink.report("E_ESCAPE", { start: index, end: index + length }, `Invalid unicode escape in string: expected \\u{ followed by 1 to 6 hexadecimal digits of a code point, then }`);
229
+ bad = true;
230
+ index += length;
231
+ continue;
232
+ }
233
+ const unknown = String.fromCodePoint(source.codePointAt(index + 1));
234
+ this.sink.report("E_ESCAPE", { start: index, end: index + 1 + unknown.length }, `Unknown escape "\\${unknown}" in ${what}`);
235
+ bad = true;
236
+ index += 1 + unknown.length;
237
+ }
238
+ this.pos = index;
239
+ return bad ? this.token("ERROR", start, index) : this.token(kind, start, index, value);
240
+ }
241
+ }
242
+
243
+ // src/text/parser.ts
244
+ var KINDS = ["graph", "sequence", "state", "er", "tree"];
245
+ var FLAGS = new Set(["pk", "fk", "uk", "null"]);
246
+ var CARDINALITIES2 = new Map([
247
+ ["1", "one"],
248
+ ["0..1", "zero-or-one"],
249
+ ["1..*", "one-or-more"],
250
+ ["0..*", "zero-or-more"]
251
+ ]);
252
+ var OPERATOR_TOKENS = new Map([
253
+ ["ARROW", "->"],
254
+ ["LINE", "--"],
255
+ ["ASYNC", "->>"],
256
+ ["REPLY", "-->"],
257
+ ["DOTS", ".."]
258
+ ]);
259
+ var OPERATOR_WORDS = new Map([
260
+ ["to", "->"],
261
+ ["with", "--"],
262
+ ["async", "->>"],
263
+ ["reply", "-->"],
264
+ ["via", ".."]
265
+ ]);
266
+ var WORD_OF = {
267
+ "->": "to",
268
+ "--": "with",
269
+ "->>": "async",
270
+ "-->": "reply",
271
+ "..": "via"
272
+ };
273
+ var OPERATOR_RULES = {
274
+ graph: { noun: "a graph", allowed: ["->", "--"] },
275
+ sequence: { noun: "a sequence diagram", allowed: ["->", "->>", "-->"] },
276
+ state: { noun: "a state machine", allowed: ["->"] },
277
+ er: { noun: "an entity-relation diagram", allowed: ["--", ".."] },
278
+ tree: { noun: "a tree", allowed: [] }
279
+ };
280
+ var ABORT = Symbol("abort statement");
281
+ function operatorOf(token) {
282
+ return token.kind === "IDENT" ? OPERATOR_WORDS.get(token.text) : OPERATOR_TOKENS.get(token.kind);
283
+ }
284
+ function isPunct(token, text) {
285
+ return token.kind === "PUNCT" && token.text === text;
286
+ }
287
+ function isWord(token, word) {
288
+ return token.kind === "IDENT" && token.text === word;
289
+ }
290
+ function isLineEnd(token) {
291
+ return token.kind === "NEWLINE" || token.kind === "EOF";
292
+ }
293
+ function joinChoices(items) {
294
+ return items.length < 2 ? items.join("") : `${items.slice(0, -1).join(", ")} or ${items[items.length - 1]}`;
295
+ }
296
+
297
+ class Parser {
298
+ source;
299
+ lexer;
300
+ sink;
301
+ lenient;
302
+ buffer = [];
303
+ lastEnd = 0;
304
+ kind = "graph";
305
+ draft;
306
+ partial;
307
+ constructor(source, lexer, sink, lenient = false) {
308
+ this.source = source;
309
+ this.lexer = lexer;
310
+ this.sink = sink;
311
+ this.lenient = lenient;
312
+ }
313
+ keep(error, node, start) {
314
+ if (error !== ABORT && error !== STOP)
315
+ return;
316
+ node.range = this.from(start);
317
+ node.incomplete = true;
318
+ this.draft = node;
319
+ }
320
+ peek(offset = 0) {
321
+ while (this.buffer.length <= offset)
322
+ this.buffer.push(this.lexer.next());
323
+ return this.buffer[offset];
324
+ }
325
+ next() {
326
+ const token = this.peek();
327
+ this.buffer.shift();
328
+ if (token.kind !== "NEWLINE" && token.kind !== "EOF")
329
+ this.lastEnd = token.end;
330
+ return token;
331
+ }
332
+ seek(pos) {
333
+ this.lexer.seek(pos);
334
+ this.buffer.length = 0;
335
+ }
336
+ from(start) {
337
+ return { start, end: Math.max(start, this.lastEnd) };
338
+ }
339
+ describe(token) {
340
+ if (token.kind === "NEWLINE")
341
+ return "end of line";
342
+ if (token.kind === "EOF")
343
+ return "end of file";
344
+ return `"${token.text}"`;
345
+ }
346
+ unexpected(token, expected) {
347
+ if (token.kind !== "ERROR") {
348
+ this.sink.report("E_EXPECTED", token, `${expected}, found ${this.describe(token)}`);
349
+ }
350
+ throw ABORT;
351
+ }
352
+ fail(code, range, message) {
353
+ this.sink.report(code, range, message);
354
+ throw ABORT;
355
+ }
356
+ skipNewlines() {
357
+ while (this.peek().kind === "NEWLINE")
358
+ this.next();
359
+ }
360
+ endLine() {
361
+ const token = this.peek();
362
+ if (!isLineEnd(token))
363
+ this.unexpected(token, "Expected end of line");
364
+ this.next();
365
+ }
366
+ recover() {
367
+ let last;
368
+ let count = 0;
369
+ for (;; ) {
370
+ const token = this.peek();
371
+ if (token.kind === "EOF")
372
+ break;
373
+ this.next();
374
+ if (token.kind === "NEWLINE")
375
+ break;
376
+ last = token;
377
+ count++;
378
+ }
379
+ if (last && (isPunct(last, "{") || isWord(last, "do") && count > 1)) {
380
+ return { text: last.text, range: last };
381
+ }
382
+ return;
383
+ }
384
+ id() {
385
+ const token = this.peek();
386
+ if (token.kind === "IDENT" || token.kind === "QUOTED_ID") {
387
+ this.next();
388
+ return { value: token.value, range: token };
389
+ }
390
+ return this.unexpected(token, "Expected an id");
391
+ }
392
+ label() {
393
+ const token = this.peek();
394
+ if (token.kind !== "STRING")
395
+ return;
396
+ this.next();
397
+ return { value: token.value, range: token };
398
+ }
399
+ value() {
400
+ const token = this.peek();
401
+ if (token.kind === "STRING") {
402
+ this.next();
403
+ return { type: "string", value: token.value, range: token };
404
+ }
405
+ if (token.kind === "NUMBER") {
406
+ this.next();
407
+ return { type: "number", value: Number(token.text), range: token };
408
+ }
409
+ if (token.kind === "IDENT") {
410
+ this.next();
411
+ return { type: "ident", value: token.text, range: token };
412
+ }
413
+ if (isPunct(token, "[")) {
414
+ this.next();
415
+ const items = [];
416
+ for (;; ) {
417
+ if (isPunct(this.peek(), "]"))
418
+ break;
419
+ items.push(this.value());
420
+ const separator = this.peek();
421
+ if (isPunct(separator, ","))
422
+ this.next();
423
+ else if (!isPunct(separator, "]"))
424
+ this.unexpected(separator, 'Expected "," or "]"');
425
+ }
426
+ const close = this.next();
427
+ return { type: "list", items, range: { start: token.start, end: close.end } };
428
+ }
429
+ return this.unexpected(token, "Expected a value");
430
+ }
431
+ attrs() {
432
+ if (!isPunct(this.peek(), "["))
433
+ return [];
434
+ this.next();
435
+ const attrs = [];
436
+ for (;; ) {
437
+ const key = this.peek();
438
+ if (isPunct(key, "]")) {
439
+ this.next();
440
+ return attrs;
441
+ }
442
+ if (key.kind !== "IDENT" && key.kind !== "QUOTED_ID") {
443
+ this.unexpected(key, "Expected an attribute name");
444
+ }
445
+ this.next();
446
+ const equals = this.peek();
447
+ if (!isPunct(equals, "="))
448
+ this.unexpected(equals, `Expected "=" after attribute "${key.value}"`);
449
+ this.next();
450
+ const value = this.value();
451
+ attrs.push({
452
+ key: key.value,
453
+ keyRange: key,
454
+ value,
455
+ range: { start: key.start, end: value.range.end }
456
+ });
457
+ const separator = this.peek();
458
+ if (isPunct(separator, ","))
459
+ this.next();
460
+ else if (!isPunct(separator, "]"))
461
+ this.unexpected(separator, 'Expected "," or "]"');
462
+ }
463
+ }
464
+ operator() {
465
+ const token = this.peek();
466
+ const symbol = operatorOf(token);
467
+ if (symbol === undefined)
468
+ return;
469
+ const rules = OPERATOR_RULES[this.kind];
470
+ if (!rules.allowed.includes(symbol)) {
471
+ const words = token.kind === "IDENT";
472
+ const allowed = rules.allowed.map((item) => words ? WORD_OF[item] : item);
473
+ this.fail("E_OPERATOR", token, `Operator "${token.text}" does not exist in ${rules.noun} (use ${joinChoices(allowed)})`);
474
+ }
475
+ this.next();
476
+ return { symbol, range: token };
477
+ }
478
+ opener(required) {
479
+ const token = this.peek();
480
+ const brace = isPunct(token, "{");
481
+ if (brace || isWord(token, "do") && isLineEnd(this.peek(1))) {
482
+ this.next();
483
+ this.endLine();
484
+ return { text: brace ? "{" : "do", range: token };
485
+ }
486
+ if (required)
487
+ this.unexpected(token, 'Expected "{" or "do" to open a block');
488
+ return;
489
+ }
490
+ body(opener, statement, out = []) {
491
+ for (;; ) {
492
+ this.skipNewlines();
493
+ const token = this.peek();
494
+ if (token.kind === "EOF") {
495
+ if (opener) {
496
+ this.sink.report("E_BLOCK", opener.range, `Block opened at line ${this.sink.lineOf(opener.range.start)} is never closed`);
497
+ }
498
+ return out;
499
+ }
500
+ const brace = isPunct(token, "}");
501
+ const end = isWord(token, "end") && isLineEnd(this.peek(1)) && opener !== undefined;
502
+ if (brace || end) {
503
+ const expected = opener?.text === "do" ? "end" : "}";
504
+ if (opener === undefined) {
505
+ this.sink.report("E_BLOCK", token, 'Unexpected "}"');
506
+ } else if (token.text !== expected) {
507
+ this.sink.report("E_BLOCK", token, `block opened with "${opener.text}" must close with "${expected}"`);
508
+ }
509
+ this.next();
510
+ try {
511
+ this.endLine();
512
+ } catch (error) {
513
+ if (error !== ABORT)
514
+ throw error;
515
+ this.recover();
516
+ }
517
+ if (opener)
518
+ return out;
519
+ continue;
520
+ }
521
+ try {
522
+ out.push(statement());
523
+ } catch (error) {
524
+ const draft = this.draft;
525
+ this.draft = undefined;
526
+ if (error === STOP) {
527
+ if (draft)
528
+ out.push(draft);
529
+ throw error;
530
+ }
531
+ if (error !== ABORT)
532
+ throw error;
533
+ const nested = this.recover();
534
+ const inner = [];
535
+ if (draft) {
536
+ if (draft.t === "group" && Array.isArray(draft.body))
537
+ draft.body = inner;
538
+ out.push(draft);
539
+ }
540
+ if (nested)
541
+ this.body(nested, statement, inner);
542
+ }
543
+ }
544
+ }
545
+ header() {
546
+ this.skipNewlines();
547
+ const token = this.peek();
548
+ const absent = () => this.lenient ? {
549
+ kindRange: { start: token.start, end: token.start },
550
+ attrs: [],
551
+ range: { start: token.start, end: token.start },
552
+ missing: true
553
+ } : undefined;
554
+ if (token.kind === "ERROR")
555
+ return absent();
556
+ if (!(token.kind === "IDENT" && KINDS.includes(token.text))) {
557
+ this.sink.report("E_HEADER", token, `Expected a header such as graph "Title" (kinds: ${KINDS.join(", ")})`);
558
+ return absent();
559
+ }
560
+ this.next();
561
+ this.kind = token.text;
562
+ const header = { kindRange: token, attrs: [], range: token };
563
+ try {
564
+ header.title = this.label();
565
+ header.attrs = this.attrs();
566
+ header.range = this.from(token.start);
567
+ this.endLine();
568
+ } catch (error) {
569
+ if (error !== ABORT)
570
+ throw error;
571
+ header.range = this.from(token.start);
572
+ this.recover();
573
+ }
574
+ return header;
575
+ }
576
+ graphStatement() {
577
+ const first = this.peek();
578
+ const start = first.start;
579
+ if (isWord(first, "group")) {
580
+ this.next();
581
+ const group = {
582
+ t: "group",
583
+ id: this.id(),
584
+ label: undefined,
585
+ attrs: [],
586
+ range: this.from(start),
587
+ body: []
588
+ };
589
+ try {
590
+ group.label = this.label();
591
+ group.attrs = this.attrs();
592
+ group.range = this.from(start);
593
+ const opener = this.opener(true);
594
+ this.body(opener, () => this.graphStatement(), group.body);
595
+ return group;
596
+ } catch (error) {
597
+ this.keep(error, group, start);
598
+ throw error;
599
+ }
600
+ }
601
+ const ids = [this.id()];
602
+ const ops = [];
603
+ let label;
604
+ let attrs = [];
605
+ try {
606
+ for (;; ) {
607
+ const op = this.operator();
608
+ if (!op)
609
+ break;
610
+ ops.push(op);
611
+ ids.push(this.id());
612
+ }
613
+ label = this.label();
614
+ attrs = this.attrs();
615
+ const range = this.from(start);
616
+ this.endLine();
617
+ if (ops.length === 0)
618
+ return { t: "node", id: ids[0], label, attrs, range };
619
+ return { t: "chain", ids, ops, label, attrs, range };
620
+ } catch (error) {
621
+ const range = { start, end: start };
622
+ this.keep(error, ops.length === 0 ? { t: "node", id: ids[0], label, attrs, range } : { t: "chain", ids, ops, label, attrs, range }, start);
623
+ throw error;
624
+ }
625
+ }
626
+ sequenceStatement() {
627
+ const start = this.peek().start;
628
+ const id = this.id();
629
+ const participant = {
630
+ t: "participant",
631
+ id,
632
+ label: undefined,
633
+ attrs: [],
634
+ range: this.from(start)
635
+ };
636
+ let node = participant;
637
+ try {
638
+ const op = this.operator();
639
+ if (!op) {
640
+ participant.label = this.label();
641
+ participant.attrs = this.attrs();
642
+ participant.range = this.from(start);
643
+ this.endLine();
644
+ return participant;
645
+ }
646
+ const message = {
647
+ t: "message",
648
+ from: id,
649
+ to: this.id(),
650
+ op,
651
+ label: undefined,
652
+ attrs: [],
653
+ range: this.from(start)
654
+ };
655
+ node = message;
656
+ message.label = this.label();
657
+ message.attrs = this.attrs();
658
+ message.range = this.from(start);
659
+ this.endLine();
660
+ return message;
661
+ } catch (error) {
662
+ this.keep(error, node, start);
663
+ throw error;
664
+ }
665
+ }
666
+ stateStatement() {
667
+ const first = this.peek();
668
+ const start = first.start;
669
+ let from;
670
+ let fallback;
671
+ if (isPunct(first, "*")) {
672
+ this.next();
673
+ from = { star: true, value: "*", range: first };
674
+ } else if (isWord(first, "initial") && operatorOf(this.peek(1)) !== undefined) {
675
+ this.next();
676
+ from = { star: true, value: "*", range: first };
677
+ } else {
678
+ const id = this.id();
679
+ const state = {
680
+ t: "state",
681
+ id,
682
+ label: undefined,
683
+ attrs: [],
684
+ range: this.from(start)
685
+ };
686
+ if (operatorOf(this.peek()) === undefined) {
687
+ try {
688
+ state.label = this.label();
689
+ state.attrs = this.attrs();
690
+ state.range = this.from(start);
691
+ const opener = this.opener(false);
692
+ if (!opener) {
693
+ this.endLine();
694
+ return state;
695
+ }
696
+ state.body = [];
697
+ this.body(opener, () => this.stateStatement(), state.body);
698
+ return state;
699
+ } catch (error) {
700
+ this.keep(error, state, start);
701
+ throw error;
702
+ }
703
+ }
704
+ from = { star: false, ...id };
705
+ fallback = state;
706
+ }
707
+ let node = fallback;
708
+ try {
709
+ const op = this.operator();
710
+ if (!op)
711
+ this.unexpected(this.peek(), 'Expected "->"');
712
+ const target = this.peek();
713
+ let to;
714
+ if (isPunct(target, "*") || isWord(target, "final")) {
715
+ this.next();
716
+ to = { star: true, value: "*", range: target };
717
+ } else {
718
+ to = { star: false, ...this.id() };
719
+ }
720
+ const transition = {
721
+ t: "transition",
722
+ from,
723
+ to,
724
+ op,
725
+ label: undefined,
726
+ attrs: [],
727
+ range: this.from(start)
728
+ };
729
+ node = transition;
730
+ transition.label = this.label();
731
+ transition.attrs = this.attrs();
732
+ transition.range = this.from(start);
733
+ this.endLine();
734
+ return transition;
735
+ } catch (error) {
736
+ if (node)
737
+ this.keep(error, node, start);
738
+ throw error;
739
+ }
740
+ }
741
+ card() {
742
+ const token = this.peek();
743
+ if (token.kind !== "NUMBER" && token.kind !== "CARD")
744
+ return;
745
+ const value = CARDINALITIES2.get(token.text);
746
+ if (value === undefined) {
747
+ this.fail("E_VALUE", token, `Cardinality must be 1, 0..1, 1..* or 0..*; found "${token.text}"`);
748
+ }
749
+ this.next();
750
+ return { value, range: token };
751
+ }
752
+ typeExpression() {
753
+ const first = this.next();
754
+ let text = first.text;
755
+ if (isPunct(this.peek(), "(")) {
756
+ this.next();
757
+ text += "(";
758
+ for (;; ) {
759
+ const arg = this.peek();
760
+ if (arg.kind !== "NUMBER" && arg.kind !== "IDENT")
761
+ this.unexpected(arg, "Expected a type argument");
762
+ this.next();
763
+ text += arg.text;
764
+ const separator = this.peek();
765
+ if (isPunct(separator, ",")) {
766
+ this.next();
767
+ text += ",";
768
+ } else if (isPunct(separator, ")")) {
769
+ this.next();
770
+ text += ")";
771
+ break;
772
+ } else {
773
+ this.unexpected(separator, 'Expected "," or ")"');
774
+ }
775
+ }
776
+ }
777
+ if (isPunct(this.peek(), "[") && isPunct(this.peek(1), "]")) {
778
+ this.next();
779
+ this.next();
780
+ text += "[]";
781
+ }
782
+ return { value: text, range: { start: first.start, end: this.lastEnd } };
783
+ }
784
+ attributeLine() {
785
+ const start = this.peek().start;
786
+ const line = {
787
+ name: this.id(),
788
+ type: undefined,
789
+ flags: [],
790
+ attrs: [],
791
+ range: this.from(start)
792
+ };
793
+ try {
794
+ const token = this.peek();
795
+ if (token.kind === "STRING") {
796
+ this.next();
797
+ line.type = { value: token.value, range: token };
798
+ } else if (token.kind === "IDENT" && !FLAGS.has(token.text)) {
799
+ line.type = this.typeExpression();
800
+ }
801
+ for (;; ) {
802
+ const flag = this.peek();
803
+ if (flag.kind !== "IDENT" || !FLAGS.has(flag.text))
804
+ break;
805
+ this.next();
806
+ line.flags.push({ value: flag.text, range: flag });
807
+ }
808
+ line.attrs = this.attrs();
809
+ line.range = this.from(start);
810
+ this.endLine();
811
+ return line;
812
+ } catch (error) {
813
+ this.keep(error, line, start);
814
+ throw error;
815
+ }
816
+ }
817
+ erStatement() {
818
+ const start = this.peek().start;
819
+ const id = this.id();
820
+ const entity = {
821
+ t: "entity",
822
+ id,
823
+ label: undefined,
824
+ attrs: [],
825
+ range: this.from(start)
826
+ };
827
+ let node = entity;
828
+ try {
829
+ const next = this.peek();
830
+ if (next.kind === "NUMBER" || next.kind === "CARD" || operatorOf(next) !== undefined) {
831
+ const fromCard = this.card();
832
+ const op = this.operator();
833
+ if (!op)
834
+ this.unexpected(this.peek(), 'Expected "--" or ".."');
835
+ const toCard = this.card();
836
+ const relation = {
837
+ t: "relation",
838
+ from: id,
839
+ fromCard,
840
+ op,
841
+ toCard,
842
+ to: this.id(),
843
+ label: undefined,
844
+ attrs: [],
845
+ range: this.from(start)
846
+ };
847
+ node = relation;
848
+ relation.label = this.label();
849
+ relation.attrs = this.attrs();
850
+ relation.range = this.from(start);
851
+ this.endLine();
852
+ return relation;
853
+ }
854
+ entity.label = this.label();
855
+ entity.attrs = this.attrs();
856
+ entity.range = this.from(start);
857
+ const opener = this.opener(false);
858
+ if (!opener) {
859
+ this.endLine();
860
+ return entity;
861
+ }
862
+ entity.attributes = [];
863
+ this.body(opener, () => this.attributeLine(), entity.attributes);
864
+ return entity;
865
+ } catch (error) {
866
+ this.keep(error, node, start);
867
+ throw error;
868
+ }
869
+ }
870
+ treeBody(lines) {
871
+ const source = this.source;
872
+ const levels = [];
873
+ let indentChar;
874
+ let firstRoot = 0;
875
+ let pos = this.lexer.pos;
876
+ while (pos < source.length) {
877
+ let end = source.indexOf(`
878
+ `, pos);
879
+ if (end < 0)
880
+ end = source.length;
881
+ const nextLine = end + 1;
882
+ let text = pos;
883
+ while (text < end && (source[text] === " " || source[text] === "\t"))
884
+ text++;
885
+ if (text >= end || source[text] === "#") {
886
+ pos = nextLine;
887
+ continue;
888
+ }
889
+ const indent = { start: pos, end: text };
890
+ const width = text - pos;
891
+ const indentation = source.slice(pos, text);
892
+ if (indentation.includes(" ") && indentation.includes("\t") || width > 0 && indentChar !== undefined && indentation[0] !== indentChar) {
893
+ this.sink.report("E_INDENT", indent, "Tabs and spaces are mixed in indentation");
894
+ pos = nextLine;
895
+ continue;
896
+ }
897
+ if (width > 0)
898
+ indentChar ??= indentation[0];
899
+ let parent = -1;
900
+ let keep = levels.length;
901
+ if (levels.length === 0) {
902
+ if (width !== 0) {
903
+ this.sink.report("E_INDENT", indent, "The first line of a tree must not be indented");
904
+ pos = nextLine;
905
+ continue;
906
+ }
907
+ firstRoot = text;
908
+ } else if (width === 0) {
909
+ this.sink.report("E_ROOT", { start: text, end }, `A tree has one root; this line starts a second one (first root at line ${this.sink.lineOf(firstRoot)})`);
910
+ pos = nextLine;
911
+ continue;
912
+ } else if (width > levels[levels.length - 1].width) {
913
+ parent = levels[levels.length - 1].index;
914
+ } else {
915
+ const level = levels.findIndex((item) => item.width === width);
916
+ if (level < 0) {
917
+ this.sink.report("E_INDENT", indent, `Indentation of ${width} does not match an open level (${levels.map((item) => item.width).join(", ")})`);
918
+ pos = nextLine;
919
+ continue;
920
+ }
921
+ parent = levels[level].parent;
922
+ keep = level;
923
+ }
924
+ let label;
925
+ let attrs = [];
926
+ let incomplete = false;
927
+ if (source[text] === '"') {
928
+ this.seek(text);
929
+ try {
930
+ const token = this.next();
931
+ if (token.kind !== "STRING")
932
+ throw ABORT;
933
+ label = { value: token.value, range: token };
934
+ attrs = this.attrs();
935
+ this.endLine();
936
+ } catch (error) {
937
+ if (error !== ABORT)
938
+ throw error;
939
+ incomplete = true;
940
+ this.recover();
941
+ }
942
+ } else {
943
+ let rest = text;
944
+ while (rest < end && source[rest] !== "[" && source[rest] !== "#")
945
+ rest++;
946
+ const bare = source.slice(text, rest).trimEnd();
947
+ if (bare === "") {
948
+ this.sink.report("E_EXPECTED", { start: rest, end: rest + 1 }, 'Expected a label, found "["');
949
+ pos = nextLine;
950
+ continue;
951
+ }
952
+ const quote = bare.indexOf('"');
953
+ if (quote >= 0) {
954
+ this.sink.report("E_TOKEN", { start: text + quote, end: text + quote + 1 }, 'Bare text cannot contain """; write the whole label as a string');
955
+ pos = nextLine;
956
+ continue;
957
+ }
958
+ label = { value: bare, range: { start: text, end: text + bare.length } };
959
+ this.seek(rest);
960
+ try {
961
+ attrs = this.attrs();
962
+ this.endLine();
963
+ } catch (error) {
964
+ if (error !== ABORT)
965
+ throw error;
966
+ incomplete = true;
967
+ this.recover();
968
+ }
969
+ }
970
+ pos = this.lexer.pos;
971
+ if (!label)
972
+ continue;
973
+ levels.length = keep;
974
+ levels.push({ width, parent, index: lines.length });
975
+ const line = {
976
+ label,
977
+ attrs,
978
+ parent,
979
+ range: { start: text, end: Math.max(label.range.end, this.lastEnd) }
980
+ };
981
+ if (incomplete)
982
+ line.incomplete = true;
983
+ lines.push(line);
984
+ }
985
+ return lines;
986
+ }
987
+ document() {
988
+ const header = this.header();
989
+ if (!header)
990
+ return;
991
+ const kind = this.kind;
992
+ switch (kind) {
993
+ case "graph": {
994
+ const ast = { kind, header, body: [] };
995
+ this.partial = ast;
996
+ this.body(undefined, () => this.graphStatement(), ast.body);
997
+ return ast;
998
+ }
999
+ case "sequence": {
1000
+ const ast = { kind, header, body: [] };
1001
+ this.partial = ast;
1002
+ this.body(undefined, () => this.sequenceStatement(), ast.body);
1003
+ return ast;
1004
+ }
1005
+ case "state": {
1006
+ const ast = { kind, header, body: [] };
1007
+ this.partial = ast;
1008
+ this.body(undefined, () => this.stateStatement(), ast.body);
1009
+ return ast;
1010
+ }
1011
+ case "er": {
1012
+ const ast = { kind, header, body: [] };
1013
+ this.partial = ast;
1014
+ this.body(undefined, () => this.erStatement(), ast.body);
1015
+ return ast;
1016
+ }
1017
+ case "tree": {
1018
+ const ast = { kind, header, body: [] };
1019
+ this.partial = ast;
1020
+ this.treeBody(ast.body);
1021
+ return ast;
1022
+ }
1023
+ }
1024
+ }
1025
+ }
1026
+ function parseSyntax(source, sink) {
1027
+ const lexer = new Lexer(source, sink);
1028
+ const parser = new Parser(source, lexer, sink);
1029
+ try {
1030
+ return parser.document();
1031
+ } catch (error) {
1032
+ if (error === STOP)
1033
+ return;
1034
+ throw error;
1035
+ }
1036
+ }
1037
+ function parseSyntaxPartial(source, sink) {
1038
+ const lexer = new Lexer(source, sink);
1039
+ const parser = new Parser(source, lexer, sink, true);
1040
+ try {
1041
+ const ast = parser.document();
1042
+ if (ast)
1043
+ return ast;
1044
+ } catch (error) {
1045
+ if (error !== STOP)
1046
+ throw error;
1047
+ }
1048
+ return parser.partial ?? {
1049
+ kind: "graph",
1050
+ header: {
1051
+ kindRange: { start: 0, end: 0 },
1052
+ attrs: [],
1053
+ range: { start: 0, end: 0 },
1054
+ missing: true
1055
+ },
1056
+ body: []
1057
+ };
1058
+ }
1059
+
1060
+ export { STOP, LineMap, Sink, IDENT, normalize, Lexer, KINDS, FLAGS, CARDINALITIES2, OPERATOR_WORDS, OPERATOR_RULES, operatorOf, parseSyntax, parseSyntaxPartial };