@edc4it/reveal.js-railroad
Version:
[](#)
1,018 lines • 54.5 kB
JavaScript
var Z = Object.defineProperty;
var J = (c, t, e) => t in c ? Z(c, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : c[t] = e;
var N = (c, t, e) => (J(c, typeof t != "symbol" ? t + "" : t, e), e);
const k = {}, s = {
DEBUG: !1,
// if true, writes some debug information into attributes
VS: 8,
// minimum vertical separation between things. For a 3px stroke, must be at least 4
AR: 10,
// radius of arcs
DIAGRAM_CLASS: "railroad-diagram",
// class to put on the root <svg>
STROKE_ODD_PIXEL_LENGTH: !0,
// is the stroke width an odd (1px, 3px, etc) pixel length?
INTERNAL_ALIGNMENT: "center",
// how to align items when they have extra space. left/right/center
CHAR_WIDTH: 8.5,
// width of each monospace character. play until you find the right value for your font
COMMENT_CHAR_WIDTH: 7,
// comments are in smaller text by default
ESCAPE_HTML: !0
// Should Diagram.toText() produce HTML-escaped text, or raw?
}, Q = `
svg {
background-color: hsl(30,20%,95%);
}
path {
stroke-width: 3;
stroke: black;
fill: rgba(0,0,0,0);
}
text {
font: bold 14px monospace;
text-anchor: middle;
white-space: pre;
}
text.diagram-text {
font-size: 12px;
}
text.diagram-arrow {
font-size: 16px;
}
text.label {
text-anchor: start;
}
text.comment {
font: italic 12px monospace;
}
g.non-terminal text {
/*font-style: italic;*/
}
rect {
stroke-width: 3;
stroke: black;
fill: hsl(120,100%,90%);
}
rect.group-box {
stroke: gray;
stroke-dasharray: 10 5;
fill: none;
}
path.diagram-text {
stroke-width: 3;
stroke: black;
fill: white;
cursor: help;
}
g.diagram-text:hover path.diagram-text {
fill: #eee;
}`;
class A {
constructor(t, e, r) {
r ? this.children = r : this.children = [], this.tagName = t, this.attrs = D(e, {});
}
format(t, e, r) {
}
addTo(t) {
if (t instanceof A)
return t.children.push(this), this;
var e = this.toSVG();
return t.appendChild(e), e;
}
toSVG() {
var t = dt(this.tagName, this.attrs);
return typeof this.children == "string" ? t.textContent = this.children : this.children.forEach(function(e) {
t.appendChild(e.toSVG());
}), t;
}
toString() {
var t = "<" + this.tagName, e = this.tagName == "g" || this.tagName == "svg";
for (var r in this.attrs)
t += " " + r + '="' + (this.attrs[r] + "").replace(/&/g, "&").replace(/"/g, """) + '"';
return t += ">", e && (t += `
`), typeof this.children == "string" ? t += lt(this.children) : this.children.forEach(function(h) {
t += h;
}), t += "</" + this.tagName + `>
`, t;
}
toTextDiagram() {
return new _(0, 0, []);
}
toText() {
var t = this.toTextDiagram(), e = t.lines.join(`
`) + `
`;
return s.ESCAPE_HTML && (e = e.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)), e;
}
walk(t) {
t(this);
}
}
class f extends A {
constructor(t, e) {
super("path"), this.attrs.d = "M" + t + " " + e;
}
m(t, e) {
return this.attrs.d += "m" + t + " " + e, this;
}
h(t) {
return this.attrs.d += "h" + t, this;
}
right(t) {
return this.h(Math.max(0, t));
}
left(t) {
return this.h(-Math.max(0, t));
}
v(t) {
return this.attrs.d += "v" + t, this;
}
down(t) {
return this.v(Math.max(0, t));
}
up(t) {
return this.v(-Math.max(0, t));
}
arc(t) {
var e = s.AR, r = s.AR;
(t[0] == "e" || t[1] == "w") && (e *= -1), (t[0] == "s" || t[1] == "n") && (r *= -1);
var h;
return t == "ne" || t == "es" || t == "sw" || t == "wn" ? h = 1 : h = 0, this.attrs.d += "a" + s.AR + " " + s.AR + " 0 0 " + h + " " + e + " " + r, this;
}
arc_8(t, e) {
const r = s.AR, h = 1 / Math.sqrt(2) * r, i = r - h;
let n = "a " + r + " " + r + " 0 0 " + (e == "cw" ? "1" : "0") + " ";
const o = t + e, w = o == "ncw" ? [h, i] : o == "necw" ? [i, h] : o == "ecw" ? [-i, h] : o == "secw" ? [-h, i] : o == "scw" ? [-h, -i] : o == "swcw" ? [-i, -h] : o == "wcw" ? [i, -h] : o == "nwcw" ? [h, -i] : o == "nccw" ? [-h, i] : o == "nwccw" ? [-i, h] : o == "wccw" ? [i, h] : o == "swccw" ? [h, i] : o == "sccw" ? [h, -i] : o == "seccw" ? [i, -h] : o == "eccw" ? [-i, -h] : o == "neccw" ? [-h, -i] : null;
return n += w.join(" "), this.attrs.d += n, this;
}
l(t, e) {
return this.attrs.d += "l" + t + " " + e, this;
}
format() {
return this.attrs.d += "h.5", this;
}
toTextDiagram() {
return new _(0, 0, []);
}
}
class G extends A {
constructor(t, e, r, h) {
super(t, r, h), this.items = e.map(P);
}
walk(t) {
t(this), this.items.forEach((e) => e.walk(t));
}
}
class Y extends G {
constructor(...t) {
super("svg", t, { class: s.DIAGRAM_CLASS }), this.items[0] instanceof U || this.items.unshift(new U()), this.items[this.items.length - 1] instanceof H || this.items.push(new H()), this.up = this.down = this.height = this.width = 0;
for (const e of this.items)
this.width += e.width + (e.needsSpace ? 20 : 0), this.up = Math.max(this.up, e.up - this.height), this.height += e.height, this.down = Math.max(this.down - e.height, e.down);
this.formatted = !1;
}
format(t, e, r, h) {
t = D(t, 20), e = D(e, t, 20), r = D(r, t, 20), h = D(h, e, 20);
var i = h, n = t;
n += this.up;
for (var o = new A("g", s.STROKE_ODD_PIXEL_LENGTH ? { transform: "translate(.5 .5)" } : {}), w = 0; w < this.items.length; w++) {
var d = this.items[w];
d.needsSpace && (new f(i, n).h(10).addTo(o), i += 10), d.format(i, n, d.width).addTo(o), i += d.width, n += d.height, d.needsSpace && (new f(i, n).h(10).addTo(o), i += 10);
}
return this.attrs.width = this.width + h + e, this.attrs.height = this.up + this.height + this.down + t + r, this.attrs.viewBox = "0 0 " + this.attrs.width + " " + this.attrs.height, o.addTo(this), this.formatted = !0, this;
}
addTo(t) {
if (!t) {
var e = document.getElementsByTagName("script");
e = e[e.length - 1], t = e.parentNode;
}
return super.addTo.call(this, t);
}
toSVG() {
return this.formatted || this.format(), super.toSVG.call(this);
}
toString() {
return this.formatted || this.format(), super.toString.call(this);
}
toStandalone(t) {
this.formatted || this.format();
const e = new A("style", {}, t || Q);
this.children.push(e), this.attrs.xmlns = "http://www.w3.org/2000/svg", this.attrs["xmlns:xlink"] = "http://www.w3.org/1999/xlink";
const r = super.toString.call(this);
return this.children.pop(), delete this.attrs.xmlns, r;
}
toTextDiagram() {
var [t] = _._getParts(["separator"]), e = this.items[0].toTextDiagram();
for (const h of this.items.slice(1)) {
var r = h.toTextDiagram();
h.needsSpace && (r = r.expand(1, 1, 0, 0)), e = e.appendRight(r, t);
}
return e;
}
}
k.Diagram = (...c) => new Y(...c);
class $ extends A {
constructor(...t) {
var e = new Y(...t);
return e.items[0] = new U({ type: "complex" }), e.items[e.items.length - 1] = new H({ type: "complex" }), e;
}
}
k.ComplexDiagram = (...c) => new $(...c);
class z extends G {
constructor(...t) {
super("g", t), this.items.length, this.needsSpace = !0, this.up = this.down = this.height = this.width = 0;
for (var e = 0; e < this.items.length; e++) {
var r = this.items[e];
this.width += r.width + (r.needsSpace ? 20 : 0), this.up = Math.max(this.up, r.up - this.height), this.height += r.height, this.down = Math.max(this.down - r.height, r.down);
}
this.items[0].needsSpace && (this.width -= 10), this.items[this.items.length - 1].needsSpace && (this.width -= 10), s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "sequence");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).h(h[1]).addTo(this), t += h[0];
for (var i = 0; i < this.items.length; i++) {
var n = this.items[i];
n.needsSpace && i > 0 && (new f(t, e).h(10).addTo(this), t += 10), n.format(t, e, n.width).addTo(this), t += n.width, e += n.height, n.needsSpace && i < this.items.length - 1 && (new f(t, e).h(10).addTo(this), t += 10);
}
return this;
}
toTextDiagram() {
var [t] = _._getParts(["separator"]), e = new _(0, 0, [""]);
for (const h of this.items) {
var r = h.toTextDiagram();
h.needsSpace && (r = r.expand(1, 1, 0, 0)), e = e.appendRight(r, t);
}
return e;
}
}
k.Sequence = (...c) => new z(...c);
class tt extends G {
constructor(...t) {
if (super("g", t), t.length === 0)
throw new RangeError("Stack() must have at least one child.");
this.width = Math.max.apply(null, this.items.map(function(i) {
return i.width + (i.needsSpace ? 20 : 0);
})), this.items.length > 1 && (this.width += s.AR * 2), this.needsSpace = !0, this.up = this.items[0].up, this.down = this.items[this.items.length - 1].down, this.height = 0;
for (var e = this.items.length - 1, r = 0; r < this.items.length; r++) {
var h = this.items[r];
this.height += h.height, r > 0 && (this.height += Math.max(s.AR * 2, h.up + s.VS)), r < e && (this.height += Math.max(s.AR * 2, h.down + s.VS));
}
s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "stack");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), t += h[0];
var i = t;
this.items.length > 1 && (new f(t, e).h(s.AR).addTo(this), t += s.AR);
for (var n = 0; n < this.items.length; n++) {
var o = this.items[n], w = this.width - (this.items.length > 1 ? s.AR * 2 : 0);
o.format(t, e, w).addTo(this), t += w, e += o.height, n !== this.items.length - 1 && (new f(t, e).arc("ne").down(Math.max(0, o.down + s.VS - s.AR * 2)).arc("es").left(w).arc("nw").down(Math.max(0, this.items[n + 1].up + s.VS - s.AR * 2)).arc("ws").addTo(this), e += Math.max(o.down + s.VS, s.AR * 2) + Math.max(this.items[n + 1].up + s.VS, s.AR * 2), t = i + s.AR);
}
return this.items.length > 1 && (new f(t, e).h(s.AR).addTo(this), t += s.AR), new f(t, e).h(h[1]).addTo(this), this;
}
toTextDiagram() {
var [t, e, r, h, i, n] = _._getParts(["corner_bot_left", "corner_bot_right", "corner_top_left", "corner_top_right", "line", "line_vertical"]), o = [];
for (const S of this.items)
o.push(S.toTextDiagram());
var w = Math.max(...o.map(function(S) {
return S.width;
})), d = [], p = [], u = new _(0, 0, [i.repeat(w)]), a = null;
for (var [l, g] of O(o)) {
if (l == 0) {
d.push(i + i);
for (var v = 0; v < g.height - g.entry - 1; v++)
d.push(" ");
} else {
for (a = a.appendBelow(u, []), d.push(r + i), v = 0; v < g.entry; v++)
d.push(n + " ");
for (d.push(t + i), v = 0; v < g.height - g.entry - 1; v++)
d.push(" ");
for (v = 0; v < g.exit; v++)
p.push(" ");
}
if (l < o.length - 1) {
for (p.push(i + h), v = 0; v < g.height - g.exit - 1; v++)
p.push(" " + n);
p.push(i + e);
} else
p.push(i + i);
var [T, m] = _._gaps(w, g.width);
g = g.expand(T, m, 0, 0), l == 0 ? a = g : a = a.appendBelow(g, []);
}
var x = new _(0, 0, d);
a = x.appendRight(a, "");
var I = new _(0, p.length - 1, p);
return a = a.appendRight(I, ""), a;
}
}
k.Stack = (...c) => new tt(...c);
class et extends G {
constructor(...t) {
if (super("g", t), t.length === 0)
throw new RangeError("OptionalSequence() must have at least one child.");
if (t.length === 1)
return new z(t);
var e = s.AR;
this.needsSpace = !1, this.width = 0, this.up = 0, this.height = y(this.items, function(o) {
return o.height;
}), this.down = this.items[0].down;
for (var r = 0, h = 0; h < this.items.length; h++) {
var i = this.items[h];
this.up = Math.max(this.up, Math.max(e * 2, i.up + s.VS) - r), r += i.height, h > 0 && (this.down = Math.max(this.height + this.down, r + Math.max(e * 2, i.down + s.VS)) - this.height);
var n = (i.needsSpace ? 10 : 0) + i.width;
h === 0 ? this.width += e + Math.max(n, e) : this.width += e * 2 + Math.max(n, e) + e;
}
s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "optseq");
}
format(t, e, r) {
var h = s.AR, i = B(r, this.width);
new f(t, e).right(i[0]).addTo(this), new f(t + i[0] + this.width, e + this.height).right(i[1]).addTo(this), t += i[0];
for (var n = e - this.up, o = this.items.length - 1, w = 0; w < this.items.length; w++) {
var d = this.items[w], p = d.needsSpace ? 10 : 0, u = d.width + p;
w === 0 ? (new f(t, e).arc("se").up(e - n - h * 2).arc("wn").right(u - h).arc("ne").down(e + d.height - n - h * 2).arc("ws").addTo(this), new f(t, e).right(p + h).addTo(this), d.format(t + p + h, e, d.width).addTo(this), t += u + h, e += d.height) : w < o ? (new f(t, n).right(h * 2 + Math.max(u, h) + h).arc("ne").down(e - n + d.height - h * 2).arc("ws").addTo(this), new f(t, e).right(h * 2).addTo(this), d.format(t + h * 2, e, d.width).addTo(this), new f(t + d.width + h * 2, e + d.height).right(p + h).addTo(this), new f(t, e).arc("ne").down(d.height + Math.max(d.down + s.VS, h * 2) - h * 2).arc("ws").right(u - h).arc("se").up(d.down + s.VS - h * 2).arc("wn").addTo(this), t += h * 2 + Math.max(u, h) + h, e += d.height) : (new f(t, e).right(h * 2).addTo(this), d.format(t + h * 2, e, d.width).addTo(this), new f(t + h * 2 + d.width, e + d.height).right(p + h).addTo(this), new f(t, e).arc("ne").down(d.height + Math.max(d.down + s.VS, h * 2) - h * 2).arc("ws").right(u - h).arc("se").up(d.down + s.VS - h * 2).arc("wn").addTo(this));
}
return this;
}
toTextDiagram() {
var [t, e, r, h, i, n] = _._getParts(["line", "line_vertical", "roundcorner_bot_left", "roundcorner_bot_right", "roundcorner_top_left", "roundcorner_top_right"]), o = [];
for (const M of this.items)
o.push(M.toTextDiagram());
for (var w = Math.max(...o.map(function(M) {
return M.entry;
})), d = Math.max(...o.slice(0, -1).map(function(M) {
return M.entry;
})), p = w - d, u = [], a = 0; a < p; a++)
u.push(" ");
for (u.push(i + t), a = 0; a < d; a++)
u.push(e + " ");
u.push(h + t);
var l = new _(u.length - 1, u.length - 1, u);
for (const [M, V] of O(o)) {
if (M > 0) {
for (u = [], a = 0; a < p; a++)
u.push(" ");
for (u.push(t + t), a = 0; a < l.exit - p - 1; a++)
u.push(" ");
for (u.push(t + n), a = 0; a < V.height - V.entry - 1; a++)
u.push(" " + e);
u.push(" " + r);
var g = new _(l.exit, l.exit, u);
for (l = l.appendRight(g, ""), u = [], a = 0; a < p; a++)
u.push(" ");
var v = M < o.length - 1 ? t : " ";
for (u.push(t + n + v), a = 0; a < l.exit - p - 1; a++)
u.push(" " + e + " ");
for (u.push(t + r + t), a = 0; a < V.height - V.entry - 1; a++)
u.push(" ");
u.push(t + t + t);
var T = new _(l.exit, l.exit, u);
l = l.appendRight(T, "");
}
var m = new _(0, 0, []);
if (M < o.length - 1) {
for (u = [], u.push(t.repeat(V.width)), a = 0; a < d - V.entry; a++)
u.push(" ".repeat(V.width));
var x = new _(0, 0, u);
m = m.appendBelow(x, []);
}
if (m = m.appendBelow(V, [], !0, !0), M > 0) {
var I = new _(0, 0, [t.repeat(V.width)]);
m = m.appendBelow(I, []);
}
if (l = l.appendRight(m, ""), 0 < M) {
for (u = [], a = 0; a < p; a++)
u.push(" ");
var S = M < o.length - 1 ? t : " ";
for (u.push(S.repeat(2)), a = 0; a < l.exit - p - 1; a++)
u.push(" ");
for (u.push(t + i), a = 0; a < m.height - m.exit - 2; a++)
u.push(" " + e);
u.push(t + h);
var E = new _(l.exit, l.exit, u);
l = l.appendRight(E, "");
}
}
return l;
}
}
k.OptionalSequence = (...c) => new et(...c);
class ht extends G {
constructor(...t) {
if (super("g", t), t.length === 1)
return new z(t);
if (t.length !== 2)
throw new RangeError("AlternatingSequence() must have one or two children.");
this.needsSpace = !1;
const e = s.AR, r = s.VS, h = Math.max, i = this.items[0], n = this.items[1], o = 1 / Math.sqrt(2) * e * 2, w = (1 - 1 / Math.sqrt(2)) * e * 2, d = Math.max(e, s.VS), p = d - w + o, u = h(e + e, d / 2 + e + e, d / 2 + r + i.down);
this.up = u + i.height + i.up;
const a = h(e + e, d / 2 + e + e, d / 2 + r + n.up);
this.down = a + n.height + n.down, this.height = 0;
const l = 2 * (i.needsSpace ? 10 : 0) + i.width, g = 2 * (n.needsSpace ? 10 : 0) + n.width;
this.width = 2 * e + h(l, p, g) + 2 * e, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "altseq");
}
format(t, e, r) {
const h = s.AR, i = B(r, this.width);
new f(t, e).right(i[0]).addTo(this), t += i[0], new f(t + this.width, e).right(i[1]).addTo(this);
const n = this.items[0], o = this.items[1], w = this.up - n.up, d = this.up - n.up - n.height;
new f(t, e).arc("se").up(w - 2 * h).arc("wn").addTo(this), n.format(t + 2 * h, e - w, this.width - 4 * h).addTo(this), new f(t + this.width - 2 * h, e - d).arc("ne").down(d - 2 * h).arc("ws").addTo(this);
const p = this.down - o.down - o.height, u = this.down - o.down;
new f(t, e).arc("ne").down(p - 2 * h).arc("ws").addTo(this), o.format(t + 2 * h, e + p, this.width - 4 * h).addTo(this), new f(t + this.width - 2 * h, e + u).arc("se").up(u - 2 * h).arc("wn").addTo(this);
const a = 1 / Math.sqrt(2) * h * 2, l = (1 - 1 / Math.sqrt(2)) * h * 2, g = Math.max(h, s.VS), v = g - l + a, T = (this.width - 4 * h - v) / 2;
return new f(t + h, e - g / 2 - h).arc("ws").right(T).arc_8("n", "cw").l(v - a, g - l).arc_8("sw", "ccw").right(T).arc("ne").addTo(this), new f(t + h, e + g / 2 + h).arc("wn").right(T).arc_8("s", "ccw").l(v - a, -(g - l)).arc_8("nw", "cw").right(T).arc("se").addTo(this), this;
}
toTextDiagram() {
for (var [t, e, r, h, i, n, o, w, d] = _._getParts(["cross_diag", "roundcorner_bot_left", "roundcorner_bot_right", "roundcorner_top_left", "roundcorner_top_right", "line", "line_vertical", "tee_left", "tee_right"]), p = this.items[0].toTextDiagram(), u = this.items[1].toTextDiagram(), a = _._maxWidth(p, u), [l, g] = _._gaps(a, 0), v = [], T = [], m = [], [x, I] = _._gaps(p.width, 0), S = p.expand(l - x, g - I, 0, 0), E = 0; E < S.entry; E++)
v.push(" ");
for (v.push(h + n), E = 0; E < S.height - S.entry - 1; E++)
v.push(o + " ");
for (v.push(e + n), E = 0; E < S.exit; E++)
T.push(" ");
for (T.push(n + i), E = 0; E < S.height - S.exit - 1; E++)
T.push(" " + o);
for (T.push(n + r), m.push(n.repeat(l - 1) + i + " " + h + n.repeat(g - 2)), m.push(" ".repeat(l - 1) + " " + t + " " + " ".repeat(g - 2)), m.push(n.repeat(l - 1) + r + " " + e + n.repeat(g - 2)), v.push(" "), T.push(" "), [x, I] = _._gaps(u.width, 0), u = u.expand(l - x, g - I, 0, 0), S = S.appendBelow(u, m, !0, !0), v.push(h + n), E = 0; E < u.entry; E++)
v.push(o + " ");
for (v.push(e + n), T.push(n + i), E = 0; E < u.exit; E++)
T.push(" " + o);
T.push(n + r), S = S.alter(p.height + Math.trunc(m.length / 2), p.height + Math.trunc(m.length / 2));
var M = new _(p.height + Math.trunc(m.length / 2), p.height + Math.trunc(m.length / 2), v), V = new _(p.height + Math.trunc(m.length / 2), p.height + Math.trunc(m.length / 2), T);
return S = M.appendRight(S, "").appendRight(V, ""), S = new _(1, 1, [h, w, e]).appendRight(S, "").appendRight(new _(1, 1, [i, d, r]), ""), S;
}
}
k.AlternatingSequence = (...c) => new ht(...c);
class L extends G {
constructor(t, ...e) {
if (super("g", e), typeof t != "number" || t !== Math.floor(t))
throw new TypeError("The first argument of Choice() must be an integer.");
if (t < 0 || t >= e.length)
throw new RangeError("The first argument of Choice() must be an index for one of the items.");
this.normal = t, this.width = q(this.items, (w) => w.width) + s.AR * 4;
var r = this.items[0], h = this.items[e.length - 1], i = this.items[t];
this.separators = Array.from({ length: e.length - 1 }, (w) => 0), this.up = 0;
for (var n, o = t - 1; o >= 0; o--) {
o == t - 1 ? n = s.AR * 2 : n = s.AR;
let w = this.items[o], d = this.items[o + 1], p = d.up + s.VS + w.down + w.height, u = d.height + d.up + s.VS + w.down, a = s.VS;
(u < n || p < n) && (a += Math.max(n - p, n - u)), this.separators[o] = a, this.up += d.up + a + w.down + w.height;
}
this.up += r.up, this.height = i.height, this.down = 0;
for (var o = t + 1; o < this.items.length; o++) {
o == t + 1 ? n = s.AR * 2 : n = s.AR;
let d = this.items[o], p = this.items[o - 1], u = p.height + p.down + s.VS + d.up, a = p.down + s.VS + d.up + d.height, l = s.VS;
(u < n || a < n) && (l += Math.max(n - u, n - a)), this.separators[o - 1] = l, this.down += p.down + l + d.up + d.height;
}
this.down += h.down, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "choice");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).h(h[1]).addTo(this), t += h[0];
for (var i = this.items.length - 1, n = this.width - s.AR * 4, w = 0, o = this.normal - 1; o >= 0; o--) {
let d = this.items[o], p = this.items[o + 1];
w += p.up + this.separators[o] + d.down + d.height, new f(t, e).arc("se").up(w - s.AR * 2).arc("wn").addTo(this), d.format(t + s.AR * 2, e - w, n).addTo(this), new f(t + s.AR * 2 + n, e - w + d.height).arc("ne").down(w - d.height + this.height - s.AR * 2).arc("ws").addTo(this);
}
new f(t, e).right(s.AR * 2).addTo(this), this.items[this.normal].format(t + s.AR * 2, e, n).addTo(this), new f(t + s.AR * 2 + n, e + this.height).right(s.AR * 2).addTo(this);
for (var w = 0, o = this.normal + 1; o <= i; o++) {
let p = this.items[o], u = this.items[o - 1];
w += u.height + u.down + this.separators[o - 1] + p.up, new f(t, e).arc("ne").down(w - s.AR * 2).arc("ws").addTo(this), p.format || console.log(p), p.format(t + s.AR * 2, e + w, n).addTo(this), new f(t + s.AR * 2 + n, e + w + p.height).arc("se").up(w - s.AR * 2 + p.height - this.height).arc("wn").addTo(this);
}
return this;
}
toTextDiagram() {
var [t, e, r, h, i, n, o] = _._getParts(["cross", "line", "line_vertical", "roundcorner_bot_left", "roundcorner_bot_right", "roundcorner_top_left", "roundcorner_top_right"]), w = [];
for (const b of this.items)
w.push(b.toTextDiagram().expand(1, 1, 0, 0));
var d = Math.max(...w.map(function(b) {
return b.width;
})), p = new _(0, 0, []);
for (var [u, a] of O(w)) {
var [l, g] = _._gaps(d, a.width);
a = a.expand(l, g, 0, 0);
for (var v = !0, T = [], m = [], x = 0; x < a.height; x++)
T.push(r), m.push(r);
var I = !1, S = !1;
if (u <= this.normal && (T[a.entry] = n, m[a.exit] = o, u == 0)) {
for (v = !1, x = 0; x < a.entry; x++)
T[x] = " ";
for (x = 0; x < a.exit; x++)
m[x] = " ";
}
if (u >= this.normal && (T[a.entry] = h, m[a.exit] = i, u == 0 && (v = !1), u == this.items.length - 1)) {
for (x = a.entry + 1; x < a.height; x++)
T[x] = " ";
for (x = a.exit + 1; x < a.height; x++)
m[x] = " ";
}
u == this.normal && (T[a.entry] = t, m[a.exit] = t, I = !0, S = !0, u == 0 && u == this.items.length - 1 ? (T[a.entry] = e, m[a.exit] = e) : u == 0 ? (T[a.entry] = o, m[a.exit] = n) : u == this.items.length - 1 && (T[a.entry] = i, m[a.exit] = h));
var E = new _(a.entry, a.entry, T), M = new _(a.exit, a.exit, m);
a = E.appendRight(a, "").appendRight(M, "");
var V = v ? [r + " ".repeat(_._maxWidth(p, a) - 2) + r] : [];
p = p.appendBelow(a, V, I, S);
}
return p;
}
}
k.Choice = (...c) => new L(...c);
class rt extends G {
constructor(...t) {
if (super("g", t), t.length === 0)
throw new RangeError("HorizontalChoice() must have at least one child.");
if (t.length === 1)
return new z(t);
const e = this.items.slice(0, -1), r = this.items.slice(1, -1), h = this.items[0], i = this.items[this.items.length - 1];
this.needsSpace = !1, this.width = s.AR, this.width += s.AR * 2 * (this.items.length - 1), this.width += y(this.items, (n) => n.width + (n.needsSpace ? 20 : 0)), this.width += i.height > 0 ? s.AR : 0, this.width += s.AR, this.height = 0, this._upperTrack = Math.max(
s.AR * 2,
s.VS,
q(e, (n) => n.up) + s.VS
), this.up = Math.max(this._upperTrack, i.up), this._lowerTrack = Math.max(
s.VS,
q(r, (n) => n.height + Math.max(n.down + s.VS, s.AR * 2)),
i.height + i.down + s.VS
), h.height < this._lowerTrack && (this._lowerTrack = Math.max(this._lowerTrack, h.height + s.AR * 2)), this.down = Math.max(this._lowerTrack, h.height + h.down), s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "horizontalchoice");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).h(h[1]).addTo(this), t += h[0];
const i = this.items[0], n = this.items[this.items.length - 1], o = this.items.slice(1), w = this.items.slice(0, -1);
var d = y(w, (l) => l.width + (l.needsSpace ? 20 : 0)) + (this.items.length - 2) * s.AR * 2 - s.AR;
new f(t, e).arc("se").v(-(this._upperTrack - s.AR * 2)).arc("wn").h(d).addTo(this);
var p = y(o, (l) => l.width + (l.needsSpace ? 20 : 0)) + (this.items.length - 2) * s.AR * 2 + (n.height > 0 ? s.AR : 0) - s.AR, u = t + s.AR + i.width + (i.needsSpace ? 20 : 0) + s.AR * 2;
new f(u, e + this._lowerTrack).h(p).arc("se").v(-(this._lowerTrack - s.AR * 2)).arc("wn").addTo(this);
for (const [l, g] of O(this.items)) {
l === 0 ? (new f(t, e).h(s.AR).addTo(this), t += s.AR) : (new f(t, e - this._upperTrack).arc("ne").v(this._upperTrack - s.AR * 2).arc("ws").addTo(this), t += s.AR * 2);
var a = g.width + (g.needsSpace ? 20 : 0);
g.format(t, e, a).addTo(this), t += a, l === this.items.length - 1 ? g.height === 0 ? new f(t, e).h(s.AR).addTo(this) : new f(t, e + g.height).arc("se").addTo(this) : l === 0 && g.height > this._lowerTrack ? g.height - this._lowerTrack >= s.AR * 2 ? new f(t, e + g.height).arc("se").v(this._lowerTrack - g.height + s.AR * 2).arc("wn").addTo(this) : new f(t, e + g.height).l(s.AR * 2, this._lowerTrack - g.height).addTo(this) : new f(t, e + g.height).arc("ne").v(this._lowerTrack - g.height - s.AR * 2).arc("ws").addTo(this);
}
return this;
}
toTextDiagram() {
var [t, e, r, h, i, n] = _._getParts(["line", "line_vertical", "roundcorner_bot_left", "roundcorner_bot_right", "roundcorner_top_left", "roundcorner_top_right"]), o = [];
for (const b of this.items)
o.push(b.toTextDiagram());
for (var w = Math.max(...o.map(function(b) {
return b.entry;
})), d = Math.max(...o.slice(0, -1).map(function(b) {
return b.entry;
})), p = w - d, u = Math.max(...o.slice(1).map(function(b) {
return b.height - Math.min(b.entry, b.exit) - 1;
})), a = [], l = 0; l < p; l++)
a.push(" ");
for (a.push(i + t), l = 0; l < d; l++)
a.push(e + " ");
a.push(h + t);
var g = new _(a.length - 1, a.length - 1, a);
for (const [b, C] of O(o)) {
if (b > 0) {
for (a = [], l = 0; l < p; l++)
a.push(" ");
var v = b == o.length - 1 ? " " : t;
for (a.push(n + v), l = 0; l < d; l++)
a.push(e + " ");
for (a.push(r + t), l = 0; l < u; l++)
a.push(e + " ");
a.push(t + t);
var T = new _(g.exit, g.exit, a);
g = g.appendRight(T, "");
}
var m = new _(0, 0, []);
if (b < o.length - 1) {
for (a = [], a.push(t.repeat(C.width)), l = 0; l < d - C.entry; l++)
a.push(" ".repeat(C.width));
var x = new _(0, 0, a);
m = m.appendBelow(x, []);
}
if (m = m.appendBelow(C, [], !0, !0), b > 0) {
for (a = [], l = 0; l < u - (C.height - C.entry) + 1; l++)
a.push(" ".repeat(C.width));
a.push(t.repeat(C.width));
var I = new _(0, 0, a);
m = m.appendBelow(I, []);
}
if (g = g.appendRight(m, ""), b < o.length - 1) {
for (a = [], l = 0; l < p; l++)
a.push(" ");
for (a.push(t + t), l = 0; l < g.exit - p - 1; l++)
a.push(" ");
for (a.push(t + n), l = 0; l < u - (g.exit - g.entry); l++)
a.push(" " + e);
var S = b > 0 ? t : " ";
a.push(S + r);
var E = w + 1 + (g.exit - g.entry), M = new _(E, w + 1, a);
g = g.appendRight(M, "");
} else {
a = [];
var V = g.exit != g.entry ? " " : t;
for (a.push(V + i), l = 0; l < g.exit - g.entry - 1; l++)
a.push(" " + e);
for (g.exit != g.entry && a.push(t + h), l = 0; l < u - (g.exit - g.entry); l++)
a.push(" " + e);
a.push(t + h), M = new _(g.exit - g.entry, 0, a), g = g.appendRight(M, "");
}
}
return g;
}
}
k.HorizontalChoice = (...c) => new rt(...c);
class st extends G {
constructor(t, e, ...r) {
if (super("g", r), typeof t != "number" || t !== Math.floor(t))
throw new TypeError("The first argument of MultipleChoice() must be an integer.");
if (t < 0 || t >= r.length)
throw new RangeError("The first argument of MultipleChoice() must be an index for one of the items.");
if (this.normal = t, e != "any" && e != "all")
throw new SyntaxError("The second argument of MultipleChoice must be 'any' or 'all'.");
this.type = e, this.needsSpace = !0, this.innerWidth = q(this.items, function(i) {
return i.width;
}), this.width = 30 + s.AR + this.innerWidth + s.AR + 20, this.up = this.items[0].up, this.down = this.items[this.items.length - 1].down, this.height = this.items[t].height;
for (var h = 0; h < this.items.length; h++) {
let i = this.items[h], n;
h == t - 1 || h == t + 1 ? n = 10 + s.AR : n = s.AR, h < t ? this.up += Math.max(n, i.height + i.down + s.VS + this.items[h + 1].up) : h > t && (this.down += Math.max(n, i.up + s.VS + this.items[h - 1].down + this.items[h - 1].height));
}
this.down -= this.items[t].height, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "multiplechoice");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).right(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).right(h[1]).addTo(this), t += h[0];
for (var i = this.items[this.normal], n, o = this.normal - 1; o >= 0; o--) {
var w = this.items[o];
o == this.normal - 1 && (n = Math.max(10 + s.AR, i.up + s.VS + w.down + w.height)), new f(t + 30, e).up(n - s.AR).arc("wn").addTo(this), w.format(t + 30 + s.AR, e - n, this.innerWidth).addTo(this), new f(t + 30 + s.AR + this.innerWidth, e - n + w.height).arc("ne").down(n - w.height + this.height - s.AR - 10).addTo(this), o !== 0 && (n += Math.max(s.AR, w.up + s.VS + this.items[o - 1].down + this.items[o - 1].height));
}
for (new f(t + 30, e).right(s.AR).addTo(this), i.format(t + 30 + s.AR, e, this.innerWidth).addTo(this), new f(t + 30 + s.AR + this.innerWidth, e + this.height).right(s.AR).addTo(this), o = this.normal + 1; o < this.items.length; o++) {
let p = this.items[o];
o == this.normal + 1 && (n = Math.max(10 + s.AR, i.height + i.down + s.VS + p.up)), new f(t + 30, e).down(n - s.AR).arc("ws").addTo(this), p.format(t + 30 + s.AR, e + n, this.innerWidth).addTo(this), new f(t + 30 + s.AR + this.innerWidth, e + n + p.height).arc("se").up(n - s.AR + p.height - i.height).addTo(this), o != this.items.length - 1 && (n += Math.max(s.AR, p.height + p.down + s.VS + this.items[o + 1].up));
}
var d = new A("g", { class: "diagram-text" }).addTo(this);
return new A("title", {}, this.type == "any" ? "take one or more branches, once each, in any order" : "take all branches, once each, in any order").addTo(d), new A("path", {
d: "M " + (t + 30) + " " + (e - 10) + " h -26 a 4 4 0 0 0 -4 4 v 12 a 4 4 0 0 0 4 4 h 26 z",
class: "diagram-text"
}).addTo(d), new A("text", {
x: t + 15,
y: e + 4,
class: "diagram-text"
}, this.type == "any" ? "1+" : "all").addTo(d), new A("path", {
d: "M " + (t + this.width - 20) + " " + (e - 10) + " h 16 a 4 4 0 0 1 4 4 v 12 a 4 4 0 0 1 -4 4 h -16 z",
class: "diagram-text"
}).addTo(d), new A("path", {
d: "M " + (t + this.width - 13) + " " + (e - 2) + " a 4 4 0 1 0 6 -1 m 2.75 -1 h -4 v 4 m 0 -3 h 2",
style: "stroke-width: 1.75"
}).addTo(d), this;
}
toTextDiagram() {
var [t] = _._getParts(["multi_repeat"]);
if (this.type == "any")
var e = _.rect("1+");
else
e = _.rect("all");
var r = L.prototype.toTextDiagram.call(this), h = _.rect(t);
return r = e.appendRight(r, ""), r = r.appendRight(h, ""), r;
}
}
k.MultipleChoice = (...c) => new st(...c);
class F extends A {
constructor(t, e) {
if (e === void 0)
return new L(1, new W(), t);
if (e === "skip")
return new L(0, new W(), t);
throw "Unknown value for Optional()'s 'skip' argument.";
}
}
k.Optional = (...c) => new F(...c);
class X extends A {
constructor(t, e) {
super("g"), e = e || new W(), this.item = P(t), this.rep = P(e), this.width = Math.max(this.item.width, this.rep.width) + s.AR * 2, this.height = this.item.height, this.up = this.item.up, this.down = Math.max(s.AR * 2, this.item.down + s.VS + this.rep.up + this.rep.height + this.rep.down), this.needsSpace = !0, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "oneormore");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).h(h[1]).addTo(this), t += h[0], new f(t, e).right(s.AR).addTo(this), this.item.format(t + s.AR, e, this.width - s.AR * 2).addTo(this), new f(t + this.width - s.AR, e + this.height).right(s.AR).addTo(this);
var i = Math.max(s.AR * 2, this.item.height + this.item.down + s.VS + this.rep.up);
return new f(t + s.AR, e).arc("nw").down(i - s.AR * 2).arc("ws").addTo(this), this.rep.format(t + s.AR, e + i, this.width - s.AR * 2).addTo(this), new f(t + this.width - s.AR, e + i + this.rep.height).arc("se").up(i - s.AR * 2 + this.rep.height - this.item.height).arc("en").addTo(this), this;
}
toTextDiagram() {
var [t, e, r, h, i, n, o] = _._getParts(["line", "repeat_top_left", "repeat_left", "repeat_bot_left", "repeat_top_right", "repeat_right", "repeat_bot_right"]), w = this.item.toTextDiagram(), d = this.rep.toTextDiagram(), p = _._maxWidth(w, d);
d = d.expand(0, p - d.width, 0, 0), w = w.expand(0, p - w.width, 0, 0);
var u = w.appendBelow(d, []), a = [];
a.push(e + t);
for (var l = 0; l < w.height - w.entry + d.entry - 1; l++)
a.push(r + " ");
a.push(h + t);
var g = new _(0, 0, a);
g = g.appendRight(u, "");
var v = [];
for (v.push(t + i), l = 0; l < w.height - w.exit + d.exit - 1; l++)
v.push(" " + n);
v.push(t + o);
var T = new _(0, 0, v), m = g.appendRight(T, "");
return m;
}
walk(t) {
t(this), this.item.walk(t), this.rep.walk(t);
}
}
k.OneOrMore = (...c) => new X(...c);
class it extends A {
constructor(t, e, r) {
return new F(new X(t, e), r);
}
}
k.ZeroOrMore = (...c) => new it(...c);
class at extends A {
constructor(t, e) {
super("g"), this.item = P(t), this.label = e instanceof A ? e : e ? new K(e) : void 0, this.width = Math.max(
this.item.width + (this.item.needsSpace ? 20 : 0),
this.label ? this.label.width : 0,
s.AR * 2
), this.height = this.item.height, this.boxUp = this.up = Math.max(this.item.up + s.VS, s.AR), this.label && (this.up += this.label.up + this.label.height + this.label.down), this.down = Math.max(this.item.down + s.VS, s.AR), this.needsSpace = !0, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "group");
}
format(t, e, r) {
var h = B(r, this.width);
return new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).h(h[1]).addTo(this), t += h[0], new A("rect", {
x: t,
y: e - this.boxUp,
width: this.width,
height: this.boxUp + this.height + this.down,
rx: s.AR,
ry: s.AR,
class: "group-box"
}).addTo(this), this.item.format(t, e, this.width).addTo(this), this.label && this.label.format(
t,
e - (this.boxUp + this.label.down + this.label.height),
this.label.width
).addTo(this), this;
}
toTextDiagram() {
var t = _.roundrect(this.item.toTextDiagram(), !0);
if (this.label != null) {
var e = this.label.toTextDiagram();
t = e.appendBelow(t, [], !0, !0).expand(0, 0, 1, 0);
}
return t;
}
walk(t) {
t(this), this.item.walk(t), this.label.walk(t);
}
}
k.Group = (...c) => new at(...c);
class U extends A {
constructor({ type: t = "simple", label: e } = {}) {
super("g"), this.width = 20, this.height = 0, this.up = 10, this.down = 10, this.type = t, e && (this.label = "" + e, this.width = Math.max(20, this.label.length * s.CHAR_WIDTH + 10)), s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "start");
}
format(t, e) {
let r = new f(t, e - 10);
return this.type === "complex" ? r.down(20).m(0, -10).right(this.width).addTo(this) : r.down(20).m(10, -20).down(20).m(-10, -10).right(this.width).addTo(this), this.label && new A("text", { x: t, y: e - 15, style: "text-anchor:start" }, this.label).addTo(this), this;
}
toTextDiagram() {
var [t, e, r] = _._getParts(["cross", "line", "tee_right"]);
if (this.type === "simple")
var h = r + t + e;
else
h = r + e;
var i = new _(0, 0, []);
this.label != null && (i = new _(0, 0, [this.label]), h = _._padR(h, i.width, e));
var n = new _(0, 0, [h]);
return i.appendBelow(n, [], !0, !0);
}
}
k.Start = (...c) => new U(...c);
class H extends A {
constructor({ type: t = "simple" } = {}) {
super("path"), this.width = 20, this.height = 0, this.up = 10, this.down = 10, this.type = t, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "end");
}
format(t, e) {
return this.type === "complex" ? this.attrs.d = "M " + t + " " + e + " h 20 m 0 -10 v 20" : this.attrs.d = "M " + t + " " + e + " h 20 m -10 -10 v 20 m 10 -20 v 20", this;
}
toTextDiagram() {
var [t, e, r] = _._getParts(["cross", "line", "tee_left"]);
if (this.type === "simple")
var h = e + t + r;
else
h = e + r;
return new _(0, 0, [h]);
}
}
k.End = (...c) => new H(...c);
class j extends A {
constructor(t, { href: e, title: r, cls: h } = {}) {
super("g", { class: ["terminal", h].join(" ") }), this.text = "" + t, this.href = e, this.title = r, this.cls = h, this.width = this.text.length * s.CHAR_WIDTH + 20, this.height = 0, this.up = 11, this.down = 11, this.needsSpace = !0, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "terminal");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e).h(h[1]).addTo(this), t += h[0], new A("rect", { x: t, y: e - 11, width: this.width, height: this.up + this.down, rx: 10, ry: 10 }).addTo(this);
var i = new A("text", { x: t + this.width / 2, y: e + 4 }, this.text);
return this.href ? new A("a", { "xlink:href": this.href }, [i]).addTo(this) : i.addTo(this), this.title && new A("title", {}, [this.title]).addTo(this), this;
}
toTextDiagram() {
return _.roundrect(this.text);
}
}
k.Terminal = (...c) => new j(...c);
class nt extends A {
constructor(t, { href: e, title: r, cls: h = "" } = {}) {
super("g", { class: ["non-terminal", h].join(" ") }), this.text = "" + t, this.href = e, this.title = r, this.cls = h, this.width = this.text.length * s.CHAR_WIDTH + 20, this.height = 0, this.up = 11, this.down = 11, this.needsSpace = !0, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "nonterminal");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e).h(h[1]).addTo(this), t += h[0], new A("rect", { x: t, y: e - 11, width: this.width, height: this.up + this.down }).addTo(this);
var i = new A("text", { x: t + this.width / 2, y: e + 4 }, this.text);
return this.href ? new A("a", { "xlink:href": this.href }, [i]).addTo(this) : i.addTo(this), this.title && new A("title", {}, [this.title]).addTo(this), this;
}
toTextDiagram() {
return _.rect(this.text);
}
}
k.NonTerminal = (...c) => new nt(...c);
class K extends A {
constructor(t, { href: e, title: r, cls: h = "" } = {}) {
super("g", { class: ["comment", h].join(" ") }), this.text = "" + t, this.href = e, this.title = r, this.cls = h, this.width = this.text.length * s.COMMENT_CHAR_WIDTH + 10, this.height = 0, this.up = 8, this.down = 8, this.needsSpace = !0, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "comment");
}
format(t, e, r) {
var h = B(r, this.width);
new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e + this.height).h(h[1]).addTo(this), t += h[0];
var i = new A("text", { x: t + this.width / 2, y: e + 5, class: "comment" }, this.text);
return this.href ? new A("a", { "xlink:href": this.href }, [i]).addTo(this) : i.addTo(this), this.title && new A("title", {}, this.title).addTo(this), this;
}
toTextDiagram() {
return new _(0, 0, [this.text]);
}
}
k.Comment = (...c) => new K(...c);
class W extends A {
constructor() {
super("g"), this.width = 0, this.height = 0, this.up = 0, this.down = 0, this.needsSpace = !1, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "skip");
}
format(t, e, r) {
return new f(t, e).right(r).addTo(this), this;
}
toTextDiagram() {
var [t] = _._getParts(["line"]);
return new _(0, 0, [t]);
}
}
k.Skip = (...c) => new W(...c);
class ot extends A {
constructor({ width: t = 50, up: e = 15, height: r = 25, down: h = 15, needsSpace: i = !0 } = {}) {
super("g"), this.width = t, this.height = r, this.up = e, this.down = h, this.needsSpace = !0, s.DEBUG && (this.attrs["data-updown"] = this.up + " " + this.height + " " + this.down, this.attrs["data-type"] = "block");
}
format(t, e, r) {
var h = B(r, this.width);
return new f(t, e).h(h[0]).addTo(this), new f(t + h[0] + this.width, e).h(h[1]).addTo(this), t += h[0], new A("rect", { x: t, y: e - this.up, width: this.width, height: this.up + this.height + this.down }).addTo(this), this;
}
toTextDiagram() {
return _.rect("");
}
}
k.Block = (...c) => new ot(...c);
const R = class R {
constructor(t, e, r) {
if (this.entry = t, this.exit = e, this.height = r.length, this.lines = Array.from(r), this.width = r.length > 0 ? r[0].length : 0, t > r.length)
throw new Error(`Entry is not within diagram vertically:
` + this._dump(!1));
if (e > r.length)
throw new Error(`Exit is not within diagram vertically:
` + this._dump(!1));
for (var h = 0; h < r.length; h++)
if (r[0].length != r[h].length)
throw new Error(`Diagram data is not rectangular:
` + this._dump(!1));
}
alter(t = null, e = null, r = null) {
var h = t || this.entry, i = e || this.exit, n = r || this.lines;
return new R(h, i, Array.from(n));
}
appendBelow(t, e, r = !1, h = !1) {
var i = Math.max(this.width, t.width), n = [], o = this.center(i, " ").lines;
for (const p of o)
n.push(p);
for (const p of e)
n.push(R._padR(p, i, " "));
o = t.center(i, " ").lines;
for (const p of o)
n.push(p);
var w = r ? this.height + e.length + t.entry : this.entry, d = h ? this.height + e.length + t.exit : this.exit;
return new R(w, d, n);
}
appendRight(t, e) {
for (var r = Math.max(this.exit, t.entry), h = Math.max(this.height - this.exit, t.height - t.entry) + r, i = r - this.exit, n = h - this.height - i, o = r - t.entry, w = h - t.height - o, d = this.expand(0, 0, i, n), p = t.expand(0, 0, o, w), u = [], a = 0; a < h; a++) {
var l = a != r ? " ".repeat(e.length) : e;
u.push(d.lines[a] + l + p.lines[a]);
}
var g = this.entry + i, v = t.exit + o;
return new R(g, v, u);
}
center(t, e) {
if (t < this.width)
throw new Error("Cannot center into smaller width");
if (t === this.width)
return this.copy();
for (var r = t - this.width, h = Math.trunc(r / 2), i = [], n = 0; n < this.height; n++)
i.push(e.repeat(h));
var o = [];
for (n = 0; n < this.height; n++)
o.push(e.repeat(r - h));
return new R(this.entry, this.exit, R._encloseLines(this.lines, i, o));
}
copy() {
return new R(this.entry, this.exit, Array.from(this.lines));
}
expand(t, e, r, h) {
if (t < 0 || e < 0 || r < 0 || h < 0)
throw new Error("Expansion values cannot be negative");
if (t + e + r + h === 0)
return this.copy();
for (var i = R.parts.line, n = [], o = 0; o < r; o++)
n.push(" ".repeat(this.width + t + e));
for (o = 0; o < this.height; o++) {
var w = o === this.entry ? i : " ", d = o === this.exit ? i : " ";
n.push(w.repeat(t) + this.lines[o] + d.repeat(e));
}
for (o = 0; o < h; o++)
n.push(" ".repeat(this.width + t + e));
return new R(this.entry + r, this.exit + r, n);
}
static rect(t, e = !1) {
return R._rectish("rect", t, e);
}
static roundrect(t, e = !1) {
return R._rectish("roundrect", t, e);
}
static setFormatting(t = null, e = null) {
t !== null && (R.parts = {}, e !== null && (R.parts = { ...R.parts, ...e }), R.parts = { ...R.parts, ...t });
for (const [r, h] of R.parts)
h.length != 1;
}
_dump(t = !0) {
var e = `
`, r = "height=" + this.height + " lines.length=" + this.lines.length;
this.entry > this.lines.length && (r += "; entry outside diagram: entry=" + this.entry), this.exit > this.lines.length && (r += "; exit outside diagram: exit=" + this.exit);
for (var h = 0; h < Math.max(this.lines.length, this.entry + 1, this.exit + 1); h++)
r = r + e + "[" + ("00" + h).slice(-3) + "]", h < this.lines.length && (r += " '" + this.lines[h] + "' len=" + this.lines[h].length), h === this.entry && h === this.exit ? r += " <- entry, exit" : h === this.entry ? r += " <- entry" : h === this.exit && (r += " <- exit");
return t && console.log(r), r;
}
static _encloseLines(t, e, r) {
if (t.length != e.length)
throw new Error("All arguments must be the same length");
if (t.length != r.length)
throw new Error("All arguments must be the same length");
for (var h = [], i = 0; i < t.length; i++)
h.push(e[i] + t[i] + r[i]);
return h;
}
static _gaps(t, e) {
var r = t - e;
if (s.INTERNAL_ALIGNMENT === "left")
return [0, r];
if (s.INTERNAL_ALIGNMENT === "right")
return [r, 0];
var h = Math.trunc(r / 2), i = r - h;
return [h, i];
}
static _getParts(t) {
var e = [];
for (const r of t) {
if (R.parts[r] == null)
throw new Error("Text diagram part " + r + "not found.");
e.push(R.parts[r]);
}
return e;
}
static _maxWidth(...t) {
var e = 0;
for (const h of t) {
if (h instanceof R)
var r = h.width;
else
h instanceof Array ? r = Math.max(h.map(function(i) {
return i.length;
})) : Number.isInteger(h) ? r = Number.toString(h).length : r = h.length;
e = r > e ? r : e;
}
return e;
}
static _padL(t, e, r) {
if ((e - t.length) % r.length != 0)
throw new Error("Gap " + (e - t.length) + " must be a multiple of pad string '" + r + "'");
return r.repeat(Math.trunc(e - t.length / r.length)) + t;
}
static _padR(t, e, r) {
if ((e - t.length) % r.length != 0)
throw new Error("Gap " + (e - t.length) + " must be a multiple of pad string '" + r + "'");
return t + r.repeat(Math.trunc(e - t.length / r.length));
}
static _rectish(t, e, r = !1) {
var h = r ? "_dashed" : "", [i, n, o, w, d, p, u, a, l, g] = R._getParts([t + "_top_left", t + "_left" + h, t + "_bot_left", t + "_top_right", t + "_right" + h, t + "_bot_right", t + "_top" + h, t + "_bot" + h, "line", "cross"]), v = e instanceof R;
if (v)
var T = e;
else
T = new R(0, 0, [e]);
var m = [];
if (m.push(u.repeat(T.width + 2)), v)
m += T.expand(1, 1, 0, 0).lines;
else
for (var x = 0; x < T.lines.length; x++)
m.push(" " + T.lines[x] + " ");
m.push(a.repeat(T.width + 2));
var I = T.entry + 1, S = T.exit + 1, E = R._maxWidth(i, n, o), M = [];
for (M.push(R._padR(i, E, u)), x = 1; x < m.length - 1; x++)
M.push(R._padR(n, E, " "));
M.push(R._padR(o, E, a)), v && (M[I] = g);
var V = R._maxWidth(w, d, p), b = [];
for (b.push(R._padL(w, V, u)), x = 1; x < m.length - 1; x++)
b.push(R._padL(d, V, " "));
fo