@standarx/graph 1.0.0 → 1.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/README.fr.md +4 -2
- package/README.md +4 -2
- package/dist/{cli-eqq7a3wz.js → cli-abzdy8pn.js} +1 -1
- package/dist/{cli-xhdxp3cj.js → cli-m29a3wgx.js} +201 -9
- package/dist/{cli-b4n941t8.js → cli-nhmh6yxk.js} +1082 -36
- package/dist/{cli-nkm2j980.js → cli-z3ccx2y4.js} +1 -1
- package/dist/cli.js +3 -5
- package/dist/core/index.js +4 -6
- package/dist/export/index.js +4 -6
- package/dist/format/index.js +4 -6
- package/dist/html/index.js +5 -7
- package/dist/html/layout-worker.generated.d.ts +1 -1
- package/dist/language/actions.d.ts +3 -0
- package/dist/language/diagnostics.d.ts +14 -0
- package/dist/language/index.d.ts +3 -1
- package/dist/language/index.js +180 -5
- package/dist/language/types.d.ts +12 -0
- package/dist/layout/index.js +3 -4
- package/dist/{layout-worker.generated-emmvgfb1.js → layout-worker.generated-xsmtzrfj.js} +1 -1
- package/dist/remark/index.js +4 -6
- package/dist/svg/index.js +1 -1
- package/dist/text/index.js +3 -5
- package/dist/webgl/index.js +1 -1
- package/package.json +1 -1
- package/dist/cli-kzp16pt5.js +0 -207
- package/dist/cli-njgyf690.js +0 -1060
|
@@ -2,22 +2,9 @@ import {
|
|
|
2
2
|
SHAPES2,
|
|
3
3
|
DirectionSchema2,
|
|
4
4
|
PSEUDO2,
|
|
5
|
-
TREE_LAYOUTS2
|
|
6
|
-
} from "./cli-kzp16pt5.js";
|
|
7
|
-
import {
|
|
5
|
+
TREE_LAYOUTS2,
|
|
8
6
|
validate2
|
|
9
|
-
} from "./cli-
|
|
10
|
-
import {
|
|
11
|
-
STOP,
|
|
12
|
-
LineMap,
|
|
13
|
-
Sink,
|
|
14
|
-
normalize,
|
|
15
|
-
Lexer,
|
|
16
|
-
KINDS,
|
|
17
|
-
FLAGS,
|
|
18
|
-
OPERATOR_WORDS,
|
|
19
|
-
parseSyntax
|
|
20
|
-
} from "./cli-njgyf690.js";
|
|
7
|
+
} from "./cli-m29a3wgx.js";
|
|
21
8
|
|
|
22
9
|
// src/text/slug.ts
|
|
23
10
|
function slug(label) {
|
|
@@ -546,6 +533,1065 @@ function build(ast, ctx) {
|
|
|
546
533
|
return Object.assign(result, buildTree(ast, ctx));
|
|
547
534
|
}
|
|
548
535
|
}
|
|
536
|
+
|
|
537
|
+
// src/text/errors.ts
|
|
538
|
+
var STOP = Symbol("too many errors");
|
|
539
|
+
var MAX_ERRORS = 50;
|
|
540
|
+
|
|
541
|
+
class LineMap {
|
|
542
|
+
starts = [0];
|
|
543
|
+
constructor(source) {
|
|
544
|
+
for (let index = 0;index < source.length; index++) {
|
|
545
|
+
if (source.charCodeAt(index) === 10)
|
|
546
|
+
this.starts.push(index + 1);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
line(offset) {
|
|
550
|
+
let low = 0;
|
|
551
|
+
let high = this.starts.length - 1;
|
|
552
|
+
while (low < high) {
|
|
553
|
+
const middle = low + high + 1 >> 1;
|
|
554
|
+
if (this.starts[middle] <= offset)
|
|
555
|
+
low = middle;
|
|
556
|
+
else
|
|
557
|
+
high = middle - 1;
|
|
558
|
+
}
|
|
559
|
+
return low + 1;
|
|
560
|
+
}
|
|
561
|
+
position(offset) {
|
|
562
|
+
const line = this.line(offset);
|
|
563
|
+
return { line, column: offset - this.starts[line - 1] + 1 };
|
|
564
|
+
}
|
|
565
|
+
error(code, range, message) {
|
|
566
|
+
const start = this.position(range.start);
|
|
567
|
+
const end = this.position(range.end);
|
|
568
|
+
return {
|
|
569
|
+
line: start.line,
|
|
570
|
+
column: start.column,
|
|
571
|
+
endLine: end.line,
|
|
572
|
+
endColumn: end.column,
|
|
573
|
+
code,
|
|
574
|
+
message
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
class Sink {
|
|
580
|
+
lines;
|
|
581
|
+
errors = [];
|
|
582
|
+
muted = 0;
|
|
583
|
+
constructor(lines) {
|
|
584
|
+
this.lines = lines;
|
|
585
|
+
}
|
|
586
|
+
lineOf(offset) {
|
|
587
|
+
return this.lines.line(offset);
|
|
588
|
+
}
|
|
589
|
+
report(code, range, message) {
|
|
590
|
+
if (this.muted > 0)
|
|
591
|
+
return;
|
|
592
|
+
if (this.errors.length >= MAX_ERRORS) {
|
|
593
|
+
this.errors.push(this.lines.error("E_TOKEN", range, "Too many errors"));
|
|
594
|
+
throw STOP;
|
|
595
|
+
}
|
|
596
|
+
this.errors.push(this.lines.error(code, range, message));
|
|
597
|
+
}
|
|
598
|
+
sorted() {
|
|
599
|
+
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);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// src/text/lexer.ts
|
|
604
|
+
var OPERATORS = [
|
|
605
|
+
["-->", "REPLY"],
|
|
606
|
+
["->>", "ASYNC"],
|
|
607
|
+
["->", "ARROW"],
|
|
608
|
+
["--", "LINE"]
|
|
609
|
+
];
|
|
610
|
+
var CARD = /0\.\.1|1\.\.\*|0\.\.\*/y;
|
|
611
|
+
var NUMBER = /-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?/y;
|
|
612
|
+
var IDENT = /[\p{L}_][\p{L}\p{N}_]*(?:[./-][\p{L}\p{N}_]+)*/uy;
|
|
613
|
+
var UNICODE_ESCAPE = /^\{([0-9a-fA-F]{1,6})\}/;
|
|
614
|
+
var SIMPLE_ESCAPES = { n: `
|
|
615
|
+
`, t: "\t" };
|
|
616
|
+
var PUNCTUATION = "{}[]()=,*";
|
|
617
|
+
function normalize(source) {
|
|
618
|
+
return source.replace(/^/, "").replace(/\r\n/g, `
|
|
619
|
+
`);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
class Lexer {
|
|
623
|
+
source;
|
|
624
|
+
sink;
|
|
625
|
+
comments;
|
|
626
|
+
pos = 0;
|
|
627
|
+
depth = 0;
|
|
628
|
+
openAt = 0;
|
|
629
|
+
constructor(source, sink, comments) {
|
|
630
|
+
this.source = source;
|
|
631
|
+
this.sink = sink;
|
|
632
|
+
this.comments = comments;
|
|
633
|
+
}
|
|
634
|
+
seek(pos) {
|
|
635
|
+
this.pos = pos;
|
|
636
|
+
this.depth = 0;
|
|
637
|
+
}
|
|
638
|
+
next() {
|
|
639
|
+
const source = this.source;
|
|
640
|
+
let char;
|
|
641
|
+
for (;; ) {
|
|
642
|
+
char = source[this.pos];
|
|
643
|
+
while (char === " " || char === "\t")
|
|
644
|
+
char = source[++this.pos];
|
|
645
|
+
if (char === "#") {
|
|
646
|
+
const commentStart = this.pos;
|
|
647
|
+
while (char !== undefined && char !== `
|
|
648
|
+
`)
|
|
649
|
+
char = source[++this.pos];
|
|
650
|
+
this.comments?.push({ start: commentStart, end: this.pos });
|
|
651
|
+
}
|
|
652
|
+
if (char === `
|
|
653
|
+
`) {
|
|
654
|
+
const start = this.pos++;
|
|
655
|
+
if (this.depth > 0)
|
|
656
|
+
continue;
|
|
657
|
+
return this.token("NEWLINE", start, this.pos);
|
|
658
|
+
}
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
if (char === undefined) {
|
|
662
|
+
if (this.depth === 0)
|
|
663
|
+
return this.token("EOF", this.pos, this.pos);
|
|
664
|
+
const at = this.openAt;
|
|
665
|
+
this.depth = 0;
|
|
666
|
+
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`);
|
|
667
|
+
return this.token("ERROR", at, at + 1);
|
|
668
|
+
}
|
|
669
|
+
const start = this.pos;
|
|
670
|
+
if (char === '"')
|
|
671
|
+
return this.quoted(start, '"', "STRING");
|
|
672
|
+
if (char === "`")
|
|
673
|
+
return this.quoted(start, "`", "QUOTED_ID");
|
|
674
|
+
for (const [text, kind] of OPERATORS) {
|
|
675
|
+
if (source.startsWith(text, start))
|
|
676
|
+
return this.advance(kind, start, text.length);
|
|
677
|
+
}
|
|
678
|
+
CARD.lastIndex = start;
|
|
679
|
+
const card = CARD.exec(source);
|
|
680
|
+
if (card)
|
|
681
|
+
return this.advance("CARD", start, card[0].length);
|
|
682
|
+
if (source.startsWith("..", start))
|
|
683
|
+
return this.advance("DOTS", start, 2);
|
|
684
|
+
if (PUNCTUATION.includes(char)) {
|
|
685
|
+
if (char === "[") {
|
|
686
|
+
if (this.depth === 0)
|
|
687
|
+
this.openAt = start;
|
|
688
|
+
this.depth++;
|
|
689
|
+
} else if (char === "]" && this.depth > 0) {
|
|
690
|
+
this.depth--;
|
|
691
|
+
}
|
|
692
|
+
return this.advance("PUNCT", start, 1);
|
|
693
|
+
}
|
|
694
|
+
NUMBER.lastIndex = start;
|
|
695
|
+
const number = NUMBER.exec(source);
|
|
696
|
+
if (number)
|
|
697
|
+
return this.advance("NUMBER", start, number[0].length);
|
|
698
|
+
IDENT.lastIndex = start;
|
|
699
|
+
const ident = IDENT.exec(source);
|
|
700
|
+
if (ident)
|
|
701
|
+
return this.advance("IDENT", start, ident[0].length);
|
|
702
|
+
const unexpected = String.fromCodePoint(source.codePointAt(start));
|
|
703
|
+
const end = start + unexpected.length;
|
|
704
|
+
this.sink.report("E_TOKEN", { start, end }, `Unexpected character "${unexpected}"`);
|
|
705
|
+
this.pos = end;
|
|
706
|
+
return this.token("ERROR", start, end);
|
|
707
|
+
}
|
|
708
|
+
token(kind, start, end, value) {
|
|
709
|
+
const text = this.source.slice(start, end);
|
|
710
|
+
return { kind, text, value: value ?? text, start, end };
|
|
711
|
+
}
|
|
712
|
+
advance(kind, start, length) {
|
|
713
|
+
this.pos = start + length;
|
|
714
|
+
return this.token(kind, start, this.pos);
|
|
715
|
+
}
|
|
716
|
+
quoted(start, quote, kind) {
|
|
717
|
+
const source = this.source;
|
|
718
|
+
const what = kind === "STRING" ? "string" : "quoted id";
|
|
719
|
+
let index = start + 1;
|
|
720
|
+
let value = "";
|
|
721
|
+
let bad = false;
|
|
722
|
+
for (;; ) {
|
|
723
|
+
const char = source[index];
|
|
724
|
+
if (char === undefined || char === `
|
|
725
|
+
`) {
|
|
726
|
+
this.sink.report("E_STRING", { start, end: index }, `Unterminated ${what}`);
|
|
727
|
+
this.pos = index;
|
|
728
|
+
return this.token("ERROR", start, index);
|
|
729
|
+
}
|
|
730
|
+
if (char === quote) {
|
|
731
|
+
index++;
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
if (char !== "\\") {
|
|
735
|
+
value += char;
|
|
736
|
+
index++;
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
const escape = source[index + 1];
|
|
740
|
+
if (escape === undefined || escape === `
|
|
741
|
+
`) {
|
|
742
|
+
index++;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
if (escape === quote || escape === "\\") {
|
|
746
|
+
value += escape;
|
|
747
|
+
index += 2;
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (kind === "STRING" && Object.hasOwn(SIMPLE_ESCAPES, escape)) {
|
|
751
|
+
value += SIMPLE_ESCAPES[escape];
|
|
752
|
+
index += 2;
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
if (kind === "STRING" && escape === "u") {
|
|
756
|
+
const match = UNICODE_ESCAPE.exec(source.slice(index + 2, index + 10));
|
|
757
|
+
const code = match ? Number.parseInt(match[1], 16) : -1;
|
|
758
|
+
if (match && code <= 1114111 && (code < 55296 || code > 57343)) {
|
|
759
|
+
value += String.fromCodePoint(code);
|
|
760
|
+
index += 2 + match[0].length;
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
const length = match ? 2 + match[0].length : 2;
|
|
764
|
+
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 }`);
|
|
765
|
+
bad = true;
|
|
766
|
+
index += length;
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
const unknown = String.fromCodePoint(source.codePointAt(index + 1));
|
|
770
|
+
this.sink.report("E_ESCAPE", { start: index, end: index + 1 + unknown.length }, `Unknown escape "\\${unknown}" in ${what}`);
|
|
771
|
+
bad = true;
|
|
772
|
+
index += 1 + unknown.length;
|
|
773
|
+
}
|
|
774
|
+
this.pos = index;
|
|
775
|
+
return bad ? this.token("ERROR", start, index) : this.token(kind, start, index, value);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// src/text/parser.ts
|
|
780
|
+
var KINDS = ["graph", "sequence", "state", "er", "tree"];
|
|
781
|
+
var FLAGS = new Set(["pk", "fk", "uk", "null"]);
|
|
782
|
+
var CARDINALITIES2 = new Map([
|
|
783
|
+
["1", "one"],
|
|
784
|
+
["0..1", "zero-or-one"],
|
|
785
|
+
["1..*", "one-or-more"],
|
|
786
|
+
["0..*", "zero-or-more"]
|
|
787
|
+
]);
|
|
788
|
+
var OPERATOR_TOKENS = new Map([
|
|
789
|
+
["ARROW", "->"],
|
|
790
|
+
["LINE", "--"],
|
|
791
|
+
["ASYNC", "->>"],
|
|
792
|
+
["REPLY", "-->"],
|
|
793
|
+
["DOTS", ".."]
|
|
794
|
+
]);
|
|
795
|
+
var OPERATOR_WORDS = new Map([
|
|
796
|
+
["to", "->"],
|
|
797
|
+
["with", "--"],
|
|
798
|
+
["async", "->>"],
|
|
799
|
+
["reply", "-->"],
|
|
800
|
+
["via", ".."]
|
|
801
|
+
]);
|
|
802
|
+
var WORD_OF = {
|
|
803
|
+
"->": "to",
|
|
804
|
+
"--": "with",
|
|
805
|
+
"->>": "async",
|
|
806
|
+
"-->": "reply",
|
|
807
|
+
"..": "via"
|
|
808
|
+
};
|
|
809
|
+
var OPERATOR_RULES = {
|
|
810
|
+
graph: { noun: "a graph", allowed: ["->", "--"] },
|
|
811
|
+
sequence: { noun: "a sequence diagram", allowed: ["->", "->>", "-->"] },
|
|
812
|
+
state: { noun: "a state machine", allowed: ["->"] },
|
|
813
|
+
er: { noun: "an entity-relation diagram", allowed: ["--", ".."] },
|
|
814
|
+
tree: { noun: "a tree", allowed: [] }
|
|
815
|
+
};
|
|
816
|
+
var ABORT = Symbol("abort statement");
|
|
817
|
+
function operatorOf(token) {
|
|
818
|
+
return token.kind === "IDENT" ? OPERATOR_WORDS.get(token.text) : OPERATOR_TOKENS.get(token.kind);
|
|
819
|
+
}
|
|
820
|
+
function isPunct(token, text) {
|
|
821
|
+
return token.kind === "PUNCT" && token.text === text;
|
|
822
|
+
}
|
|
823
|
+
function isWord(token, word) {
|
|
824
|
+
return token.kind === "IDENT" && token.text === word;
|
|
825
|
+
}
|
|
826
|
+
function isLineEnd(token) {
|
|
827
|
+
return token.kind === "NEWLINE" || token.kind === "EOF";
|
|
828
|
+
}
|
|
829
|
+
function joinChoices(items) {
|
|
830
|
+
return items.length < 2 ? items.join("") : `${items.slice(0, -1).join(", ")} or ${items[items.length - 1]}`;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
class Parser {
|
|
834
|
+
source;
|
|
835
|
+
lexer;
|
|
836
|
+
sink;
|
|
837
|
+
lenient;
|
|
838
|
+
buffer = [];
|
|
839
|
+
lastEnd = 0;
|
|
840
|
+
kind = "graph";
|
|
841
|
+
draft;
|
|
842
|
+
partial;
|
|
843
|
+
constructor(source, lexer, sink, lenient = false) {
|
|
844
|
+
this.source = source;
|
|
845
|
+
this.lexer = lexer;
|
|
846
|
+
this.sink = sink;
|
|
847
|
+
this.lenient = lenient;
|
|
848
|
+
}
|
|
849
|
+
keep(error, node, start) {
|
|
850
|
+
if (error !== ABORT && error !== STOP)
|
|
851
|
+
return;
|
|
852
|
+
node.range = this.from(start);
|
|
853
|
+
node.incomplete = true;
|
|
854
|
+
this.draft = node;
|
|
855
|
+
}
|
|
856
|
+
peek(offset = 0) {
|
|
857
|
+
while (this.buffer.length <= offset)
|
|
858
|
+
this.buffer.push(this.lexer.next());
|
|
859
|
+
return this.buffer[offset];
|
|
860
|
+
}
|
|
861
|
+
next() {
|
|
862
|
+
const token = this.peek();
|
|
863
|
+
this.buffer.shift();
|
|
864
|
+
if (token.kind !== "NEWLINE" && token.kind !== "EOF")
|
|
865
|
+
this.lastEnd = token.end;
|
|
866
|
+
return token;
|
|
867
|
+
}
|
|
868
|
+
seek(pos) {
|
|
869
|
+
this.lexer.seek(pos);
|
|
870
|
+
this.buffer.length = 0;
|
|
871
|
+
}
|
|
872
|
+
from(start) {
|
|
873
|
+
return { start, end: Math.max(start, this.lastEnd) };
|
|
874
|
+
}
|
|
875
|
+
describe(token) {
|
|
876
|
+
if (token.kind === "NEWLINE")
|
|
877
|
+
return "end of line";
|
|
878
|
+
if (token.kind === "EOF")
|
|
879
|
+
return "end of file";
|
|
880
|
+
return `"${token.text}"`;
|
|
881
|
+
}
|
|
882
|
+
unexpected(token, expected) {
|
|
883
|
+
if (token.kind !== "ERROR") {
|
|
884
|
+
this.sink.report("E_EXPECTED", token, `${expected}, found ${this.describe(token)}`);
|
|
885
|
+
}
|
|
886
|
+
throw ABORT;
|
|
887
|
+
}
|
|
888
|
+
fail(code, range, message) {
|
|
889
|
+
this.sink.report(code, range, message);
|
|
890
|
+
throw ABORT;
|
|
891
|
+
}
|
|
892
|
+
skipNewlines() {
|
|
893
|
+
while (this.peek().kind === "NEWLINE")
|
|
894
|
+
this.next();
|
|
895
|
+
}
|
|
896
|
+
endLine() {
|
|
897
|
+
const token = this.peek();
|
|
898
|
+
if (!isLineEnd(token))
|
|
899
|
+
this.unexpected(token, "Expected end of line");
|
|
900
|
+
this.next();
|
|
901
|
+
}
|
|
902
|
+
recover() {
|
|
903
|
+
let last;
|
|
904
|
+
let count = 0;
|
|
905
|
+
for (;; ) {
|
|
906
|
+
const token = this.peek();
|
|
907
|
+
if (token.kind === "EOF")
|
|
908
|
+
break;
|
|
909
|
+
this.next();
|
|
910
|
+
if (token.kind === "NEWLINE")
|
|
911
|
+
break;
|
|
912
|
+
last = token;
|
|
913
|
+
count++;
|
|
914
|
+
}
|
|
915
|
+
if (last && (isPunct(last, "{") || isWord(last, "do") && count > 1)) {
|
|
916
|
+
return { text: last.text, range: last };
|
|
917
|
+
}
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
id() {
|
|
921
|
+
const token = this.peek();
|
|
922
|
+
if (token.kind === "IDENT" || token.kind === "QUOTED_ID") {
|
|
923
|
+
this.next();
|
|
924
|
+
return { value: token.value, range: token };
|
|
925
|
+
}
|
|
926
|
+
return this.unexpected(token, "Expected an id");
|
|
927
|
+
}
|
|
928
|
+
label() {
|
|
929
|
+
const token = this.peek();
|
|
930
|
+
if (token.kind !== "STRING")
|
|
931
|
+
return;
|
|
932
|
+
this.next();
|
|
933
|
+
return { value: token.value, range: token };
|
|
934
|
+
}
|
|
935
|
+
value() {
|
|
936
|
+
const token = this.peek();
|
|
937
|
+
if (token.kind === "STRING") {
|
|
938
|
+
this.next();
|
|
939
|
+
return { type: "string", value: token.value, range: token };
|
|
940
|
+
}
|
|
941
|
+
if (token.kind === "NUMBER") {
|
|
942
|
+
this.next();
|
|
943
|
+
return { type: "number", value: Number(token.text), range: token };
|
|
944
|
+
}
|
|
945
|
+
if (token.kind === "IDENT") {
|
|
946
|
+
this.next();
|
|
947
|
+
return { type: "ident", value: token.text, range: token };
|
|
948
|
+
}
|
|
949
|
+
if (isPunct(token, "[")) {
|
|
950
|
+
this.next();
|
|
951
|
+
const items = [];
|
|
952
|
+
for (;; ) {
|
|
953
|
+
if (isPunct(this.peek(), "]"))
|
|
954
|
+
break;
|
|
955
|
+
items.push(this.value());
|
|
956
|
+
const separator = this.peek();
|
|
957
|
+
if (isPunct(separator, ","))
|
|
958
|
+
this.next();
|
|
959
|
+
else if (!isPunct(separator, "]"))
|
|
960
|
+
this.unexpected(separator, 'Expected "," or "]"');
|
|
961
|
+
}
|
|
962
|
+
const close = this.next();
|
|
963
|
+
return { type: "list", items, range: { start: token.start, end: close.end } };
|
|
964
|
+
}
|
|
965
|
+
return this.unexpected(token, "Expected a value");
|
|
966
|
+
}
|
|
967
|
+
attrs() {
|
|
968
|
+
if (!isPunct(this.peek(), "["))
|
|
969
|
+
return [];
|
|
970
|
+
this.next();
|
|
971
|
+
const attrs = [];
|
|
972
|
+
for (;; ) {
|
|
973
|
+
const key = this.peek();
|
|
974
|
+
if (isPunct(key, "]")) {
|
|
975
|
+
this.next();
|
|
976
|
+
return attrs;
|
|
977
|
+
}
|
|
978
|
+
if (key.kind !== "IDENT" && key.kind !== "QUOTED_ID") {
|
|
979
|
+
this.unexpected(key, "Expected an attribute name");
|
|
980
|
+
}
|
|
981
|
+
this.next();
|
|
982
|
+
const equals = this.peek();
|
|
983
|
+
if (!isPunct(equals, "="))
|
|
984
|
+
this.unexpected(equals, `Expected "=" after attribute "${key.value}"`);
|
|
985
|
+
this.next();
|
|
986
|
+
const value = this.value();
|
|
987
|
+
attrs.push({
|
|
988
|
+
key: key.value,
|
|
989
|
+
keyRange: key,
|
|
990
|
+
value,
|
|
991
|
+
range: { start: key.start, end: value.range.end }
|
|
992
|
+
});
|
|
993
|
+
const separator = this.peek();
|
|
994
|
+
if (isPunct(separator, ","))
|
|
995
|
+
this.next();
|
|
996
|
+
else if (!isPunct(separator, "]"))
|
|
997
|
+
this.unexpected(separator, 'Expected "," or "]"');
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
operator() {
|
|
1001
|
+
const token = this.peek();
|
|
1002
|
+
const symbol = operatorOf(token);
|
|
1003
|
+
if (symbol === undefined)
|
|
1004
|
+
return;
|
|
1005
|
+
const rules = OPERATOR_RULES[this.kind];
|
|
1006
|
+
if (!rules.allowed.includes(symbol)) {
|
|
1007
|
+
const words = token.kind === "IDENT";
|
|
1008
|
+
const allowed = rules.allowed.map((item) => words ? WORD_OF[item] : item);
|
|
1009
|
+
this.fail("E_OPERATOR", token, `Operator "${token.text}" does not exist in ${rules.noun} (use ${joinChoices(allowed)})`);
|
|
1010
|
+
}
|
|
1011
|
+
this.next();
|
|
1012
|
+
return { symbol, range: token };
|
|
1013
|
+
}
|
|
1014
|
+
opener(required) {
|
|
1015
|
+
const token = this.peek();
|
|
1016
|
+
const brace = isPunct(token, "{");
|
|
1017
|
+
if (brace || isWord(token, "do") && isLineEnd(this.peek(1))) {
|
|
1018
|
+
this.next();
|
|
1019
|
+
this.endLine();
|
|
1020
|
+
return { text: brace ? "{" : "do", range: token };
|
|
1021
|
+
}
|
|
1022
|
+
if (required)
|
|
1023
|
+
this.unexpected(token, 'Expected "{" or "do" to open a block');
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
body(opener, statement, out = []) {
|
|
1027
|
+
for (;; ) {
|
|
1028
|
+
this.skipNewlines();
|
|
1029
|
+
const token = this.peek();
|
|
1030
|
+
if (token.kind === "EOF") {
|
|
1031
|
+
if (opener) {
|
|
1032
|
+
this.sink.report("E_BLOCK", opener.range, `Block opened at line ${this.sink.lineOf(opener.range.start)} is never closed`);
|
|
1033
|
+
}
|
|
1034
|
+
return out;
|
|
1035
|
+
}
|
|
1036
|
+
const brace = isPunct(token, "}");
|
|
1037
|
+
const end = isWord(token, "end") && isLineEnd(this.peek(1)) && opener !== undefined;
|
|
1038
|
+
if (brace || end) {
|
|
1039
|
+
const expected = opener?.text === "do" ? "end" : "}";
|
|
1040
|
+
if (opener === undefined) {
|
|
1041
|
+
this.sink.report("E_BLOCK", token, 'Unexpected "}"');
|
|
1042
|
+
} else if (token.text !== expected) {
|
|
1043
|
+
this.sink.report("E_BLOCK", token, `block opened with "${opener.text}" must close with "${expected}"`);
|
|
1044
|
+
}
|
|
1045
|
+
this.next();
|
|
1046
|
+
try {
|
|
1047
|
+
this.endLine();
|
|
1048
|
+
} catch (error) {
|
|
1049
|
+
if (error !== ABORT)
|
|
1050
|
+
throw error;
|
|
1051
|
+
this.recover();
|
|
1052
|
+
}
|
|
1053
|
+
if (opener)
|
|
1054
|
+
return out;
|
|
1055
|
+
continue;
|
|
1056
|
+
}
|
|
1057
|
+
try {
|
|
1058
|
+
out.push(statement());
|
|
1059
|
+
} catch (error) {
|
|
1060
|
+
const draft = this.draft;
|
|
1061
|
+
this.draft = undefined;
|
|
1062
|
+
if (error === STOP) {
|
|
1063
|
+
if (draft)
|
|
1064
|
+
out.push(draft);
|
|
1065
|
+
throw error;
|
|
1066
|
+
}
|
|
1067
|
+
if (error !== ABORT)
|
|
1068
|
+
throw error;
|
|
1069
|
+
const nested = this.recover();
|
|
1070
|
+
const inner = [];
|
|
1071
|
+
if (draft) {
|
|
1072
|
+
if (draft.t === "group" && Array.isArray(draft.body))
|
|
1073
|
+
draft.body = inner;
|
|
1074
|
+
out.push(draft);
|
|
1075
|
+
}
|
|
1076
|
+
if (nested)
|
|
1077
|
+
this.body(nested, statement, inner);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
header() {
|
|
1082
|
+
this.skipNewlines();
|
|
1083
|
+
const token = this.peek();
|
|
1084
|
+
const absent = () => this.lenient ? {
|
|
1085
|
+
kindRange: { start: token.start, end: token.start },
|
|
1086
|
+
attrs: [],
|
|
1087
|
+
range: { start: token.start, end: token.start },
|
|
1088
|
+
missing: true
|
|
1089
|
+
} : undefined;
|
|
1090
|
+
if (token.kind === "ERROR")
|
|
1091
|
+
return absent();
|
|
1092
|
+
if (!(token.kind === "IDENT" && KINDS.includes(token.text))) {
|
|
1093
|
+
this.sink.report("E_HEADER", token, `Expected a header such as graph "Title" (kinds: ${KINDS.join(", ")})`);
|
|
1094
|
+
return absent();
|
|
1095
|
+
}
|
|
1096
|
+
this.next();
|
|
1097
|
+
this.kind = token.text;
|
|
1098
|
+
const header = { kindRange: token, attrs: [], range: token };
|
|
1099
|
+
try {
|
|
1100
|
+
header.title = this.label();
|
|
1101
|
+
header.attrs = this.attrs();
|
|
1102
|
+
header.range = this.from(token.start);
|
|
1103
|
+
this.endLine();
|
|
1104
|
+
} catch (error) {
|
|
1105
|
+
if (error !== ABORT)
|
|
1106
|
+
throw error;
|
|
1107
|
+
header.range = this.from(token.start);
|
|
1108
|
+
this.recover();
|
|
1109
|
+
}
|
|
1110
|
+
return header;
|
|
1111
|
+
}
|
|
1112
|
+
graphStatement() {
|
|
1113
|
+
const first = this.peek();
|
|
1114
|
+
const start = first.start;
|
|
1115
|
+
if (isWord(first, "group")) {
|
|
1116
|
+
this.next();
|
|
1117
|
+
const group = {
|
|
1118
|
+
t: "group",
|
|
1119
|
+
id: this.id(),
|
|
1120
|
+
label: undefined,
|
|
1121
|
+
attrs: [],
|
|
1122
|
+
range: this.from(start),
|
|
1123
|
+
body: []
|
|
1124
|
+
};
|
|
1125
|
+
try {
|
|
1126
|
+
group.label = this.label();
|
|
1127
|
+
group.attrs = this.attrs();
|
|
1128
|
+
group.range = this.from(start);
|
|
1129
|
+
const opener = this.opener(true);
|
|
1130
|
+
this.body(opener, () => this.graphStatement(), group.body);
|
|
1131
|
+
return group;
|
|
1132
|
+
} catch (error) {
|
|
1133
|
+
this.keep(error, group, start);
|
|
1134
|
+
throw error;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
const ids = [this.id()];
|
|
1138
|
+
const ops = [];
|
|
1139
|
+
let label;
|
|
1140
|
+
let attrs = [];
|
|
1141
|
+
try {
|
|
1142
|
+
for (;; ) {
|
|
1143
|
+
const op = this.operator();
|
|
1144
|
+
if (!op)
|
|
1145
|
+
break;
|
|
1146
|
+
ops.push(op);
|
|
1147
|
+
ids.push(this.id());
|
|
1148
|
+
}
|
|
1149
|
+
label = this.label();
|
|
1150
|
+
attrs = this.attrs();
|
|
1151
|
+
const range = this.from(start);
|
|
1152
|
+
this.endLine();
|
|
1153
|
+
if (ops.length === 0)
|
|
1154
|
+
return { t: "node", id: ids[0], label, attrs, range };
|
|
1155
|
+
return { t: "chain", ids, ops, label, attrs, range };
|
|
1156
|
+
} catch (error) {
|
|
1157
|
+
const range = { start, end: start };
|
|
1158
|
+
this.keep(error, ops.length === 0 ? { t: "node", id: ids[0], label, attrs, range } : { t: "chain", ids, ops, label, attrs, range }, start);
|
|
1159
|
+
throw error;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
sequenceStatement() {
|
|
1163
|
+
const start = this.peek().start;
|
|
1164
|
+
const id = this.id();
|
|
1165
|
+
const participant = {
|
|
1166
|
+
t: "participant",
|
|
1167
|
+
id,
|
|
1168
|
+
label: undefined,
|
|
1169
|
+
attrs: [],
|
|
1170
|
+
range: this.from(start)
|
|
1171
|
+
};
|
|
1172
|
+
let node = participant;
|
|
1173
|
+
try {
|
|
1174
|
+
const op = this.operator();
|
|
1175
|
+
if (!op) {
|
|
1176
|
+
participant.label = this.label();
|
|
1177
|
+
participant.attrs = this.attrs();
|
|
1178
|
+
participant.range = this.from(start);
|
|
1179
|
+
this.endLine();
|
|
1180
|
+
return participant;
|
|
1181
|
+
}
|
|
1182
|
+
const message = {
|
|
1183
|
+
t: "message",
|
|
1184
|
+
from: id,
|
|
1185
|
+
to: this.id(),
|
|
1186
|
+
op,
|
|
1187
|
+
label: undefined,
|
|
1188
|
+
attrs: [],
|
|
1189
|
+
range: this.from(start)
|
|
1190
|
+
};
|
|
1191
|
+
node = message;
|
|
1192
|
+
message.label = this.label();
|
|
1193
|
+
message.attrs = this.attrs();
|
|
1194
|
+
message.range = this.from(start);
|
|
1195
|
+
this.endLine();
|
|
1196
|
+
return message;
|
|
1197
|
+
} catch (error) {
|
|
1198
|
+
this.keep(error, node, start);
|
|
1199
|
+
throw error;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
stateStatement() {
|
|
1203
|
+
const first = this.peek();
|
|
1204
|
+
const start = first.start;
|
|
1205
|
+
let from;
|
|
1206
|
+
let fallback;
|
|
1207
|
+
if (isPunct(first, "*")) {
|
|
1208
|
+
this.next();
|
|
1209
|
+
from = { star: true, value: "*", range: first };
|
|
1210
|
+
} else if (isWord(first, "initial") && operatorOf(this.peek(1)) !== undefined) {
|
|
1211
|
+
this.next();
|
|
1212
|
+
from = { star: true, value: "*", range: first };
|
|
1213
|
+
} else {
|
|
1214
|
+
const id = this.id();
|
|
1215
|
+
const state = {
|
|
1216
|
+
t: "state",
|
|
1217
|
+
id,
|
|
1218
|
+
label: undefined,
|
|
1219
|
+
attrs: [],
|
|
1220
|
+
range: this.from(start)
|
|
1221
|
+
};
|
|
1222
|
+
if (operatorOf(this.peek()) === undefined) {
|
|
1223
|
+
try {
|
|
1224
|
+
state.label = this.label();
|
|
1225
|
+
state.attrs = this.attrs();
|
|
1226
|
+
state.range = this.from(start);
|
|
1227
|
+
const opener = this.opener(false);
|
|
1228
|
+
if (!opener) {
|
|
1229
|
+
this.endLine();
|
|
1230
|
+
return state;
|
|
1231
|
+
}
|
|
1232
|
+
state.body = [];
|
|
1233
|
+
this.body(opener, () => this.stateStatement(), state.body);
|
|
1234
|
+
return state;
|
|
1235
|
+
} catch (error) {
|
|
1236
|
+
this.keep(error, state, start);
|
|
1237
|
+
throw error;
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
from = { star: false, ...id };
|
|
1241
|
+
fallback = state;
|
|
1242
|
+
}
|
|
1243
|
+
let node = fallback;
|
|
1244
|
+
try {
|
|
1245
|
+
const op = this.operator();
|
|
1246
|
+
if (!op)
|
|
1247
|
+
this.unexpected(this.peek(), 'Expected "->"');
|
|
1248
|
+
const target = this.peek();
|
|
1249
|
+
let to;
|
|
1250
|
+
if (isPunct(target, "*") || isWord(target, "final")) {
|
|
1251
|
+
this.next();
|
|
1252
|
+
to = { star: true, value: "*", range: target };
|
|
1253
|
+
} else {
|
|
1254
|
+
to = { star: false, ...this.id() };
|
|
1255
|
+
}
|
|
1256
|
+
const transition = {
|
|
1257
|
+
t: "transition",
|
|
1258
|
+
from,
|
|
1259
|
+
to,
|
|
1260
|
+
op,
|
|
1261
|
+
label: undefined,
|
|
1262
|
+
attrs: [],
|
|
1263
|
+
range: this.from(start)
|
|
1264
|
+
};
|
|
1265
|
+
node = transition;
|
|
1266
|
+
transition.label = this.label();
|
|
1267
|
+
transition.attrs = this.attrs();
|
|
1268
|
+
transition.range = this.from(start);
|
|
1269
|
+
this.endLine();
|
|
1270
|
+
return transition;
|
|
1271
|
+
} catch (error) {
|
|
1272
|
+
if (node)
|
|
1273
|
+
this.keep(error, node, start);
|
|
1274
|
+
throw error;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
card() {
|
|
1278
|
+
const token = this.peek();
|
|
1279
|
+
if (token.kind !== "NUMBER" && token.kind !== "CARD")
|
|
1280
|
+
return;
|
|
1281
|
+
const value = CARDINALITIES2.get(token.text);
|
|
1282
|
+
if (value === undefined) {
|
|
1283
|
+
this.fail("E_VALUE", token, `Cardinality must be 1, 0..1, 1..* or 0..*; found "${token.text}"`);
|
|
1284
|
+
}
|
|
1285
|
+
this.next();
|
|
1286
|
+
return { value, range: token };
|
|
1287
|
+
}
|
|
1288
|
+
typeExpression() {
|
|
1289
|
+
const first = this.next();
|
|
1290
|
+
let text = first.text;
|
|
1291
|
+
if (isPunct(this.peek(), "(")) {
|
|
1292
|
+
this.next();
|
|
1293
|
+
text += "(";
|
|
1294
|
+
for (;; ) {
|
|
1295
|
+
const arg = this.peek();
|
|
1296
|
+
if (arg.kind !== "NUMBER" && arg.kind !== "IDENT")
|
|
1297
|
+
this.unexpected(arg, "Expected a type argument");
|
|
1298
|
+
this.next();
|
|
1299
|
+
text += arg.text;
|
|
1300
|
+
const separator = this.peek();
|
|
1301
|
+
if (isPunct(separator, ",")) {
|
|
1302
|
+
this.next();
|
|
1303
|
+
text += ",";
|
|
1304
|
+
} else if (isPunct(separator, ")")) {
|
|
1305
|
+
this.next();
|
|
1306
|
+
text += ")";
|
|
1307
|
+
break;
|
|
1308
|
+
} else {
|
|
1309
|
+
this.unexpected(separator, 'Expected "," or ")"');
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
if (isPunct(this.peek(), "[") && isPunct(this.peek(1), "]")) {
|
|
1314
|
+
this.next();
|
|
1315
|
+
this.next();
|
|
1316
|
+
text += "[]";
|
|
1317
|
+
}
|
|
1318
|
+
return { value: text, range: { start: first.start, end: this.lastEnd } };
|
|
1319
|
+
}
|
|
1320
|
+
attributeLine() {
|
|
1321
|
+
const start = this.peek().start;
|
|
1322
|
+
const line = {
|
|
1323
|
+
name: this.id(),
|
|
1324
|
+
type: undefined,
|
|
1325
|
+
flags: [],
|
|
1326
|
+
attrs: [],
|
|
1327
|
+
range: this.from(start)
|
|
1328
|
+
};
|
|
1329
|
+
try {
|
|
1330
|
+
const token = this.peek();
|
|
1331
|
+
if (token.kind === "STRING") {
|
|
1332
|
+
this.next();
|
|
1333
|
+
line.type = { value: token.value, range: token };
|
|
1334
|
+
} else if (token.kind === "IDENT" && !FLAGS.has(token.text)) {
|
|
1335
|
+
line.type = this.typeExpression();
|
|
1336
|
+
}
|
|
1337
|
+
for (;; ) {
|
|
1338
|
+
const flag = this.peek();
|
|
1339
|
+
if (flag.kind !== "IDENT" || !FLAGS.has(flag.text))
|
|
1340
|
+
break;
|
|
1341
|
+
this.next();
|
|
1342
|
+
line.flags.push({ value: flag.text, range: flag });
|
|
1343
|
+
}
|
|
1344
|
+
line.attrs = this.attrs();
|
|
1345
|
+
line.range = this.from(start);
|
|
1346
|
+
this.endLine();
|
|
1347
|
+
return line;
|
|
1348
|
+
} catch (error) {
|
|
1349
|
+
this.keep(error, line, start);
|
|
1350
|
+
throw error;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
erStatement() {
|
|
1354
|
+
const start = this.peek().start;
|
|
1355
|
+
const id = this.id();
|
|
1356
|
+
const entity = {
|
|
1357
|
+
t: "entity",
|
|
1358
|
+
id,
|
|
1359
|
+
label: undefined,
|
|
1360
|
+
attrs: [],
|
|
1361
|
+
range: this.from(start)
|
|
1362
|
+
};
|
|
1363
|
+
let node = entity;
|
|
1364
|
+
try {
|
|
1365
|
+
const next = this.peek();
|
|
1366
|
+
if (next.kind === "NUMBER" || next.kind === "CARD" || operatorOf(next) !== undefined) {
|
|
1367
|
+
const fromCard = this.card();
|
|
1368
|
+
const op = this.operator();
|
|
1369
|
+
if (!op)
|
|
1370
|
+
this.unexpected(this.peek(), 'Expected "--" or ".."');
|
|
1371
|
+
const toCard = this.card();
|
|
1372
|
+
const relation = {
|
|
1373
|
+
t: "relation",
|
|
1374
|
+
from: id,
|
|
1375
|
+
fromCard,
|
|
1376
|
+
op,
|
|
1377
|
+
toCard,
|
|
1378
|
+
to: this.id(),
|
|
1379
|
+
label: undefined,
|
|
1380
|
+
attrs: [],
|
|
1381
|
+
range: this.from(start)
|
|
1382
|
+
};
|
|
1383
|
+
node = relation;
|
|
1384
|
+
relation.label = this.label();
|
|
1385
|
+
relation.attrs = this.attrs();
|
|
1386
|
+
relation.range = this.from(start);
|
|
1387
|
+
this.endLine();
|
|
1388
|
+
return relation;
|
|
1389
|
+
}
|
|
1390
|
+
entity.label = this.label();
|
|
1391
|
+
entity.attrs = this.attrs();
|
|
1392
|
+
entity.range = this.from(start);
|
|
1393
|
+
const opener = this.opener(false);
|
|
1394
|
+
if (!opener) {
|
|
1395
|
+
this.endLine();
|
|
1396
|
+
return entity;
|
|
1397
|
+
}
|
|
1398
|
+
entity.attributes = [];
|
|
1399
|
+
this.body(opener, () => this.attributeLine(), entity.attributes);
|
|
1400
|
+
return entity;
|
|
1401
|
+
} catch (error) {
|
|
1402
|
+
this.keep(error, node, start);
|
|
1403
|
+
throw error;
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
treeBody(lines) {
|
|
1407
|
+
const source = this.source;
|
|
1408
|
+
const levels = [];
|
|
1409
|
+
let indentChar;
|
|
1410
|
+
let firstRoot = 0;
|
|
1411
|
+
let pos = this.lexer.pos;
|
|
1412
|
+
while (pos < source.length) {
|
|
1413
|
+
let end = source.indexOf(`
|
|
1414
|
+
`, pos);
|
|
1415
|
+
if (end < 0)
|
|
1416
|
+
end = source.length;
|
|
1417
|
+
const nextLine = end + 1;
|
|
1418
|
+
let text = pos;
|
|
1419
|
+
while (text < end && (source[text] === " " || source[text] === "\t"))
|
|
1420
|
+
text++;
|
|
1421
|
+
if (text >= end || source[text] === "#") {
|
|
1422
|
+
pos = nextLine;
|
|
1423
|
+
continue;
|
|
1424
|
+
}
|
|
1425
|
+
const indent = { start: pos, end: text };
|
|
1426
|
+
const width = text - pos;
|
|
1427
|
+
const indentation = source.slice(pos, text);
|
|
1428
|
+
if (indentation.includes(" ") && indentation.includes("\t") || width > 0 && indentChar !== undefined && indentation[0] !== indentChar) {
|
|
1429
|
+
this.sink.report("E_INDENT", indent, "Tabs and spaces are mixed in indentation");
|
|
1430
|
+
pos = nextLine;
|
|
1431
|
+
continue;
|
|
1432
|
+
}
|
|
1433
|
+
if (width > 0)
|
|
1434
|
+
indentChar ??= indentation[0];
|
|
1435
|
+
let parent = -1;
|
|
1436
|
+
let keep = levels.length;
|
|
1437
|
+
if (levels.length === 0) {
|
|
1438
|
+
if (width !== 0) {
|
|
1439
|
+
this.sink.report("E_INDENT", indent, "The first line of a tree must not be indented");
|
|
1440
|
+
pos = nextLine;
|
|
1441
|
+
continue;
|
|
1442
|
+
}
|
|
1443
|
+
firstRoot = text;
|
|
1444
|
+
} else if (width === 0) {
|
|
1445
|
+
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)})`);
|
|
1446
|
+
pos = nextLine;
|
|
1447
|
+
continue;
|
|
1448
|
+
} else if (width > levels[levels.length - 1].width) {
|
|
1449
|
+
parent = levels[levels.length - 1].index;
|
|
1450
|
+
} else {
|
|
1451
|
+
const level = levels.findIndex((item) => item.width === width);
|
|
1452
|
+
if (level < 0) {
|
|
1453
|
+
this.sink.report("E_INDENT", indent, `Indentation of ${width} does not match an open level (${levels.map((item) => item.width).join(", ")})`);
|
|
1454
|
+
pos = nextLine;
|
|
1455
|
+
continue;
|
|
1456
|
+
}
|
|
1457
|
+
parent = levels[level].parent;
|
|
1458
|
+
keep = level;
|
|
1459
|
+
}
|
|
1460
|
+
let label;
|
|
1461
|
+
let attrs = [];
|
|
1462
|
+
let incomplete = false;
|
|
1463
|
+
if (source[text] === '"') {
|
|
1464
|
+
this.seek(text);
|
|
1465
|
+
try {
|
|
1466
|
+
const token = this.next();
|
|
1467
|
+
if (token.kind !== "STRING")
|
|
1468
|
+
throw ABORT;
|
|
1469
|
+
label = { value: token.value, range: token };
|
|
1470
|
+
attrs = this.attrs();
|
|
1471
|
+
this.endLine();
|
|
1472
|
+
} catch (error) {
|
|
1473
|
+
if (error !== ABORT)
|
|
1474
|
+
throw error;
|
|
1475
|
+
incomplete = true;
|
|
1476
|
+
this.recover();
|
|
1477
|
+
}
|
|
1478
|
+
} else {
|
|
1479
|
+
let rest = text;
|
|
1480
|
+
while (rest < end && source[rest] !== "[" && source[rest] !== "#")
|
|
1481
|
+
rest++;
|
|
1482
|
+
const bare = source.slice(text, rest).trimEnd();
|
|
1483
|
+
if (bare === "") {
|
|
1484
|
+
this.sink.report("E_EXPECTED", { start: rest, end: rest + 1 }, 'Expected a label, found "["');
|
|
1485
|
+
pos = nextLine;
|
|
1486
|
+
continue;
|
|
1487
|
+
}
|
|
1488
|
+
const quote = bare.indexOf('"');
|
|
1489
|
+
if (quote >= 0) {
|
|
1490
|
+
this.sink.report("E_TOKEN", { start: text + quote, end: text + quote + 1 }, 'Bare text cannot contain """; write the whole label as a string');
|
|
1491
|
+
pos = nextLine;
|
|
1492
|
+
continue;
|
|
1493
|
+
}
|
|
1494
|
+
label = { value: bare, range: { start: text, end: text + bare.length } };
|
|
1495
|
+
this.seek(rest);
|
|
1496
|
+
try {
|
|
1497
|
+
attrs = this.attrs();
|
|
1498
|
+
this.endLine();
|
|
1499
|
+
} catch (error) {
|
|
1500
|
+
if (error !== ABORT)
|
|
1501
|
+
throw error;
|
|
1502
|
+
incomplete = true;
|
|
1503
|
+
this.recover();
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
pos = this.lexer.pos;
|
|
1507
|
+
if (!label)
|
|
1508
|
+
continue;
|
|
1509
|
+
levels.length = keep;
|
|
1510
|
+
levels.push({ width, parent, index: lines.length });
|
|
1511
|
+
const line = {
|
|
1512
|
+
label,
|
|
1513
|
+
attrs,
|
|
1514
|
+
parent,
|
|
1515
|
+
range: { start: text, end: Math.max(label.range.end, this.lastEnd) }
|
|
1516
|
+
};
|
|
1517
|
+
if (incomplete)
|
|
1518
|
+
line.incomplete = true;
|
|
1519
|
+
lines.push(line);
|
|
1520
|
+
}
|
|
1521
|
+
return lines;
|
|
1522
|
+
}
|
|
1523
|
+
document() {
|
|
1524
|
+
const header = this.header();
|
|
1525
|
+
if (!header)
|
|
1526
|
+
return;
|
|
1527
|
+
const kind = this.kind;
|
|
1528
|
+
switch (kind) {
|
|
1529
|
+
case "graph": {
|
|
1530
|
+
const ast = { kind, header, body: [] };
|
|
1531
|
+
this.partial = ast;
|
|
1532
|
+
this.body(undefined, () => this.graphStatement(), ast.body);
|
|
1533
|
+
return ast;
|
|
1534
|
+
}
|
|
1535
|
+
case "sequence": {
|
|
1536
|
+
const ast = { kind, header, body: [] };
|
|
1537
|
+
this.partial = ast;
|
|
1538
|
+
this.body(undefined, () => this.sequenceStatement(), ast.body);
|
|
1539
|
+
return ast;
|
|
1540
|
+
}
|
|
1541
|
+
case "state": {
|
|
1542
|
+
const ast = { kind, header, body: [] };
|
|
1543
|
+
this.partial = ast;
|
|
1544
|
+
this.body(undefined, () => this.stateStatement(), ast.body);
|
|
1545
|
+
return ast;
|
|
1546
|
+
}
|
|
1547
|
+
case "er": {
|
|
1548
|
+
const ast = { kind, header, body: [] };
|
|
1549
|
+
this.partial = ast;
|
|
1550
|
+
this.body(undefined, () => this.erStatement(), ast.body);
|
|
1551
|
+
return ast;
|
|
1552
|
+
}
|
|
1553
|
+
case "tree": {
|
|
1554
|
+
const ast = { kind, header, body: [] };
|
|
1555
|
+
this.partial = ast;
|
|
1556
|
+
this.treeBody(ast.body);
|
|
1557
|
+
return ast;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
function parseSyntax(source, sink) {
|
|
1563
|
+
const lexer = new Lexer(source, sink);
|
|
1564
|
+
const parser = new Parser(source, lexer, sink);
|
|
1565
|
+
try {
|
|
1566
|
+
return parser.document();
|
|
1567
|
+
} catch (error) {
|
|
1568
|
+
if (error === STOP)
|
|
1569
|
+
return;
|
|
1570
|
+
throw error;
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
function parseSyntaxPartial(source, sink) {
|
|
1574
|
+
const lexer = new Lexer(source, sink);
|
|
1575
|
+
const parser = new Parser(source, lexer, sink, true);
|
|
1576
|
+
try {
|
|
1577
|
+
const ast = parser.document();
|
|
1578
|
+
if (ast)
|
|
1579
|
+
return ast;
|
|
1580
|
+
} catch (error) {
|
|
1581
|
+
if (error !== STOP)
|
|
1582
|
+
throw error;
|
|
1583
|
+
}
|
|
1584
|
+
return parser.partial ?? {
|
|
1585
|
+
kind: "graph",
|
|
1586
|
+
header: {
|
|
1587
|
+
kindRange: { start: 0, end: 0 },
|
|
1588
|
+
attrs: [],
|
|
1589
|
+
range: { start: 0, end: 0 },
|
|
1590
|
+
missing: true
|
|
1591
|
+
},
|
|
1592
|
+
body: []
|
|
1593
|
+
};
|
|
1594
|
+
}
|
|
549
1595
|
// src/text/canonical.ts
|
|
550
1596
|
function childrenOf(items) {
|
|
551
1597
|
const children = new Map;
|
|
@@ -689,10 +1735,10 @@ var SEMANTIC_TOKEN_TYPES2 = [
|
|
|
689
1735
|
var SEMANTIC_TOKEN_MODIFIERS2 = ["declaration", "defaultLibrary"];
|
|
690
1736
|
var ENUM_KEYS = new Set(["shape", "pseudo", "confidence", "direction", "layout"]);
|
|
691
1737
|
var SYMBOL_OPERATORS = new Set(["ARROW", "LINE", "ASYNC", "REPLY", "DOTS"]);
|
|
692
|
-
function
|
|
1738
|
+
function isPunct2(token, text) {
|
|
693
1739
|
return token !== undefined && token.kind === "PUNCT" && token.text === text;
|
|
694
1740
|
}
|
|
695
|
-
function
|
|
1741
|
+
function isWord2(token, word) {
|
|
696
1742
|
return token !== undefined && token.kind === "IDENT" && token.text === word;
|
|
697
1743
|
}
|
|
698
1744
|
function isId(token) {
|
|
@@ -819,7 +1865,7 @@ class Highlighter {
|
|
|
819
1865
|
}
|
|
820
1866
|
}
|
|
821
1867
|
attrs(cursor) {
|
|
822
|
-
if (!
|
|
1868
|
+
if (!isPunct2(cursor.peek(), "["))
|
|
823
1869
|
return;
|
|
824
1870
|
cursor.take();
|
|
825
1871
|
let depth = 1;
|
|
@@ -835,7 +1881,7 @@ class Highlighter {
|
|
|
835
1881
|
this.mark(token, "operator");
|
|
836
1882
|
continue;
|
|
837
1883
|
}
|
|
838
|
-
if ((token.kind === "IDENT" || token.kind === "QUOTED_ID") &&
|
|
1884
|
+
if ((token.kind === "IDENT" || token.kind === "QUOTED_ID") && isPunct2(cursor.peek(), "=")) {
|
|
839
1885
|
key = token.text;
|
|
840
1886
|
this.mark(token, "property");
|
|
841
1887
|
} else if (token.kind === "STRING") {
|
|
@@ -857,7 +1903,7 @@ class Highlighter {
|
|
|
857
1903
|
rest(cursor) {
|
|
858
1904
|
while (!cursor.done) {
|
|
859
1905
|
const token = cursor.peek();
|
|
860
|
-
if (
|
|
1906
|
+
if (isPunct2(token, "[")) {
|
|
861
1907
|
this.attrs(cursor);
|
|
862
1908
|
continue;
|
|
863
1909
|
}
|
|
@@ -866,7 +1912,7 @@ class Highlighter {
|
|
|
866
1912
|
this.mark(token, "string");
|
|
867
1913
|
else if (token.kind === "NUMBER" || token.kind === "CARD")
|
|
868
1914
|
this.mark(token, "number");
|
|
869
|
-
else if (isOperator(token) ||
|
|
1915
|
+
else if (isOperator(token) || isPunct2(token, "*"))
|
|
870
1916
|
this.mark(token, "operator");
|
|
871
1917
|
else if (isId(token))
|
|
872
1918
|
this.mark(token, "variable");
|
|
@@ -876,10 +1922,10 @@ class Highlighter {
|
|
|
876
1922
|
}
|
|
877
1923
|
opener(cursor, block) {
|
|
878
1924
|
const token = cursor.peek();
|
|
879
|
-
if (
|
|
1925
|
+
if (isPunct2(token, "{")) {
|
|
880
1926
|
cursor.take();
|
|
881
1927
|
this.blocks.push(block);
|
|
882
|
-
} else if (
|
|
1928
|
+
} else if (isWord2(token, "do") && cursor.last) {
|
|
883
1929
|
this.mark(cursor.take(), "keyword");
|
|
884
1930
|
this.blocks.push(block);
|
|
885
1931
|
}
|
|
@@ -910,19 +1956,19 @@ class Highlighter {
|
|
|
910
1956
|
} else if (type !== undefined && type.kind === "IDENT" && !FLAGS.has(type.text)) {
|
|
911
1957
|
cursor.take();
|
|
912
1958
|
this.mark(type, "type");
|
|
913
|
-
if (
|
|
1959
|
+
if (isPunct2(cursor.peek(), "(")) {
|
|
914
1960
|
cursor.take();
|
|
915
|
-
while (!cursor.done && !
|
|
1961
|
+
while (!cursor.done && !isPunct2(cursor.peek(), ")") && !isPunct2(cursor.peek(), "[")) {
|
|
916
1962
|
const arg = cursor.take();
|
|
917
1963
|
if (arg.kind === "NUMBER")
|
|
918
1964
|
this.mark(arg, "number");
|
|
919
1965
|
else if (arg.kind === "IDENT")
|
|
920
1966
|
this.mark(arg, "type");
|
|
921
1967
|
}
|
|
922
|
-
if (
|
|
1968
|
+
if (isPunct2(cursor.peek(), ")"))
|
|
923
1969
|
cursor.take();
|
|
924
1970
|
}
|
|
925
|
-
if (
|
|
1971
|
+
if (isPunct2(cursor.peek(), "[") && isPunct2(cursor.peek(1), "]")) {
|
|
926
1972
|
cursor.take();
|
|
927
1973
|
cursor.take();
|
|
928
1974
|
}
|
|
@@ -942,13 +1988,13 @@ class Highlighter {
|
|
|
942
1988
|
const first = cursor.peek();
|
|
943
1989
|
if (first === undefined)
|
|
944
1990
|
return;
|
|
945
|
-
if (this.blocks.length > 0 &&
|
|
1991
|
+
if (this.blocks.length > 0 && isWord2(first, "end") && tokens.length === 1) {
|
|
946
1992
|
cursor.take();
|
|
947
1993
|
this.mark(first, "keyword");
|
|
948
1994
|
this.blocks.pop();
|
|
949
1995
|
return;
|
|
950
1996
|
}
|
|
951
|
-
if (
|
|
1997
|
+
if (isPunct2(first, "}")) {
|
|
952
1998
|
this.blocks.pop();
|
|
953
1999
|
cursor.take();
|
|
954
2000
|
this.rest(cursor);
|
|
@@ -958,7 +2004,7 @@ class Highlighter {
|
|
|
958
2004
|
this.attributeLine(cursor);
|
|
959
2005
|
return;
|
|
960
2006
|
}
|
|
961
|
-
if (kind === "graph" &&
|
|
2007
|
+
if (kind === "graph" && isWord2(first, "group")) {
|
|
962
2008
|
cursor.take();
|
|
963
2009
|
this.mark(first, "keyword");
|
|
964
2010
|
const id = cursor.peek();
|
|
@@ -972,7 +2018,7 @@ class Highlighter {
|
|
|
972
2018
|
this.rest(cursor);
|
|
973
2019
|
return;
|
|
974
2020
|
}
|
|
975
|
-
if (kind === "state" && (
|
|
2021
|
+
if (kind === "state" && (isPunct2(first, "*") || isWord2(first, "initial") && isOperator(cursor.peek(1)))) {
|
|
976
2022
|
cursor.take();
|
|
977
2023
|
this.mark(first, "operator");
|
|
978
2024
|
} else if (isId(first)) {
|
|
@@ -1006,7 +2052,7 @@ class Highlighter {
|
|
|
1006
2052
|
if (kind === "er")
|
|
1007
2053
|
this.card(cursor);
|
|
1008
2054
|
const target = cursor.peek();
|
|
1009
|
-
if (kind === "state" && (
|
|
2055
|
+
if (kind === "state" && (isPunct2(target, "*") || isWord2(target, "final"))) {
|
|
1010
2056
|
cursor.take();
|
|
1011
2057
|
this.mark(target, "operator");
|
|
1012
2058
|
} else if (isId(target)) {
|
|
@@ -1111,7 +2157,7 @@ var STYLES = {
|
|
|
1111
2157
|
}
|
|
1112
2158
|
};
|
|
1113
2159
|
var IDENT_SOURCE = String.raw`[\p{L}_][\p{L}\p{N}_]*(?:[./-][\p{L}\p{N}_]+)*`;
|
|
1114
|
-
var
|
|
2160
|
+
var IDENT2 = new RegExp(`^${IDENT_SOURCE}$`, "u");
|
|
1115
2161
|
var NUMBER_SOURCE = String.raw`-?[0-9]+(?:\.[0-9]+)?`;
|
|
1116
2162
|
var TYPE_ARG = `(?:${NUMBER_SOURCE}|${IDENT_SOURCE})`;
|
|
1117
2163
|
var TYPE = new RegExp(String.raw`^${IDENT_SOURCE}(?:\(${TYPE_ARG}(?:,${TYPE_ARG})*\))?(?:\[\])?$`, "u");
|
|
@@ -1142,7 +2188,7 @@ function backticked(value) {
|
|
|
1142
2188
|
return `\`${value.replace(/[\\`]/g, "\\$&")}\``;
|
|
1143
2189
|
}
|
|
1144
2190
|
function ident(value, reserved) {
|
|
1145
|
-
return
|
|
2191
|
+
return IDENT2.test(value) && !WORDS.has(value) && !reserved?.has(value) ? value : backticked(value);
|
|
1146
2192
|
}
|
|
1147
2193
|
function pad(depth) {
|
|
1148
2194
|
return " ".repeat(depth);
|
|
@@ -1172,7 +2218,7 @@ function metaPairs(meta) {
|
|
|
1172
2218
|
for (const [name, value] of Object.entries(meta.data ?? {})) {
|
|
1173
2219
|
const key = `data.${name}`;
|
|
1174
2220
|
pairs.push([
|
|
1175
|
-
|
|
2221
|
+
IDENT2.test(key) ? key : backticked(key),
|
|
1176
2222
|
typeof value === "string" ? quote(value) : typeof value === "number" ? numberText(value) : String(value)
|
|
1177
2223
|
]);
|
|
1178
2224
|
}
|
|
@@ -1436,4 +2482,4 @@ function loadSource2(source) {
|
|
|
1436
2482
|
};
|
|
1437
2483
|
}
|
|
1438
2484
|
|
|
1439
|
-
export { canonicalize2, SEMANTIC_TOKEN_TYPES2, SEMANTIC_TOKEN_MODIFIERS2, highlight2, print2, parse2, sniff2, loadSource2 };
|
|
2485
|
+
export { STOP, LineMap, Sink, IDENT, normalize, Lexer, KINDS, FLAGS, CARDINALITIES2, OPERATOR_WORDS, OPERATOR_RULES, operatorOf, parseSyntaxPartial, canonicalize2, SEMANTIC_TOKEN_TYPES2, SEMANTIC_TOKEN_MODIFIERS2, highlight2, print2, parse2, sniff2, loadSource2 };
|