@margoengine/fsm 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +267 -3
- package/dist/document.d.ts +78 -0
- package/dist/document.js +149 -0
- package/dist/events.d.ts +41 -0
- package/dist/events.js +50 -0
- package/dist/final.d.ts +39 -0
- package/dist/final.js +116 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +129 -0
- package/dist/inspect.d.ts +43 -0
- package/dist/inspect.js +111 -0
- package/dist/invoke.d.ts +55 -0
- package/dist/invoke.js +79 -0
- package/dist/parallel.d.ts +96 -0
- package/dist/parallel.js +99 -0
- package/dist/scxml.d.ts +22 -0
- package/dist/scxml.js +423 -0
- package/dist/trace.d.ts +92 -0
- package/dist/trace.js +118 -0
- package/package.json +86 -6
package/dist/scxml.js
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { validateMachine as e } from "./document.js";
|
|
2
|
+
//#region ../shared/xml-document.js
|
|
3
|
+
var t = "http://www.w3.org/XML/1998/namespace", n = "http://www.w3.org/2000/xmlns/", r = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD\\u{10000}-\\u{EFFFF}", i = `[${r}][${r}\\-.0-9\\u00B7\\u0300-\\u036F\\u203F\\u2040]*`, a = RegExp(`^${i}(?::${i})?$`, "u"), o = /[^\s/>=]+/y, s = /[ \t\r\n]*/y, c = /[^\t\n\r --�\u{10000}-\u{10FFFF}]/u, l = {
|
|
4
|
+
lt: "<",
|
|
5
|
+
gt: ">",
|
|
6
|
+
amp: "&",
|
|
7
|
+
quot: "\"",
|
|
8
|
+
apos: "'"
|
|
9
|
+
}, u = class {
|
|
10
|
+
constructor(e, t, n, r, i, a, o) {
|
|
11
|
+
this.nodeType = 1, this.tagName = e, this.prefix = t, this.localName = n, this.namespaceURI = r, this.attributes = i, this.parentElement = a, this.childNodes = [], this.children = [], this.start = o, this.contentStart = -1, this.end = -1;
|
|
12
|
+
}
|
|
13
|
+
get textContent() {
|
|
14
|
+
let e = "", t = [this];
|
|
15
|
+
for (; t.length;) {
|
|
16
|
+
let n = t.pop();
|
|
17
|
+
if (n.nodeType === 3 || n.nodeType === 4) e += n.data;
|
|
18
|
+
else if (n.nodeType === 1) for (let e = n.childNodes.length - 1; e >= 0; e--) t.push(n.childNodes[e]);
|
|
19
|
+
}
|
|
20
|
+
return e;
|
|
21
|
+
}
|
|
22
|
+
getAttribute(e) {
|
|
23
|
+
for (let t of this.attributes) if (t.name === e) return t.value;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
getAttributeNS(e, t) {
|
|
27
|
+
let n = e || null;
|
|
28
|
+
for (let e of this.attributes) if (e.namespaceURI === n && e.localName === t) return e.value;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
hasAttribute(e) {
|
|
32
|
+
return this.getAttribute(e) !== null;
|
|
33
|
+
}
|
|
34
|
+
}, d = class {
|
|
35
|
+
constructor(e) {
|
|
36
|
+
this.documentElement = e;
|
|
37
|
+
}
|
|
38
|
+
*#e() {
|
|
39
|
+
let e = [this.documentElement];
|
|
40
|
+
for (; e.length;) {
|
|
41
|
+
let t = e.pop();
|
|
42
|
+
yield t;
|
|
43
|
+
for (let n = t.children.length - 1; n >= 0; n--) e.push(t.children[n]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
getElementById(e) {
|
|
47
|
+
for (let t of this.#e()) if (t.getAttributeNS(null, "id") === e) return t;
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
getElementsByTagNameNS(e, t) {
|
|
51
|
+
let n = e || null;
|
|
52
|
+
return [...this.#e()].filter((r) => (e === "*" || r.namespaceURI === n) && (t === "*" || r.localName === t));
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function f(e, r = {}) {
|
|
56
|
+
let i = r.maxDepth ?? 512, f = r.maxElements ?? Infinity, p = (t, n) => {
|
|
57
|
+
let i = `${t} (line ${e.slice(0, n).split("\n").length})`;
|
|
58
|
+
throw r.fail && r.fail(i), TypeError(`Malformed XML: ${i}; supply a well-formed XML 1.0 document`);
|
|
59
|
+
};
|
|
60
|
+
typeof e != "string" && p("input is not a string", 0);
|
|
61
|
+
let m = c.exec(e);
|
|
62
|
+
m && p(`character U+${m[0].codePointAt(0).toString(16).toUpperCase()} is not allowed in XML`, m.index);
|
|
63
|
+
let h = (e, t) => e.indexOf("&") < 0 ? e : e.replace(/&([^;&]*)(;?)/g, (e, n, r) => {
|
|
64
|
+
if (r || p("unterminated entity or character reference", t), n[0] === "#") {
|
|
65
|
+
let r = /^#x[0-9A-Fa-f]+$/.test(n) ? Number.parseInt(n.slice(2), 16) : /^#[0-9]+$/.test(n) ? Number.parseInt(n.slice(1), 10) : NaN, i = r <= 1114111 ? String.fromCodePoint(r) : "";
|
|
66
|
+
return (!i || c.test(i) || r >= 55296 && r <= 57343) && p(`invalid character reference ${e}`, t), i;
|
|
67
|
+
}
|
|
68
|
+
return Object.hasOwn(l, n) || p(`undefined entity ${e}; only the five XML entities exist`, t), l[n];
|
|
69
|
+
}), g = (t) => (s.lastIndex = t, s.exec(e), s.lastIndex), _ = (t) => {
|
|
70
|
+
o.lastIndex = t;
|
|
71
|
+
let n = o.exec(e)?.[0] ?? "";
|
|
72
|
+
return a.test(n) || p(`invalid element or attribute name "${n.slice(0, 40)}"`, t), n;
|
|
73
|
+
}, v = [Object.assign(Object.create(null), {
|
|
74
|
+
xml: t,
|
|
75
|
+
"": null
|
|
76
|
+
})], y = [], b = +(e.charCodeAt(0) === 65279), x = null, S = !1, C = 0, w = () => y[y.length - 1], T = (e, t, n = 3) => {
|
|
77
|
+
if (!y.length) {
|
|
78
|
+
(n === 4 || /[^ \t\r\n]/.test(e)) && p("text outside the root element", t);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
n === 3 && e.includes("]]>") && p("\"]]>\" in text", t);
|
|
82
|
+
let r = n === 3 ? h(e.replace(/\r\n?/g, "\n"), t) : e.replace(/\r\n?/g, "\n");
|
|
83
|
+
w().childNodes.push({
|
|
84
|
+
nodeType: n,
|
|
85
|
+
data: r,
|
|
86
|
+
textContent: r
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
for (; b < e.length;) {
|
|
90
|
+
let r = e.indexOf("<", b);
|
|
91
|
+
if (r < 0) {
|
|
92
|
+
T(e.slice(b), b);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
if (r > b && T(e.slice(b, r), b), e.startsWith("<!--", r)) {
|
|
96
|
+
let t = e.indexOf("-->", r + 4);
|
|
97
|
+
t < 0 && p("unterminated comment", r);
|
|
98
|
+
let n = e.slice(r + 4, t);
|
|
99
|
+
(n.includes("--") || n.endsWith("-")) && p("\"--\" inside a comment", r), y.length && w().childNodes.push({
|
|
100
|
+
nodeType: 8,
|
|
101
|
+
data: n,
|
|
102
|
+
textContent: n
|
|
103
|
+
}), b = t + 3;
|
|
104
|
+
} else if (e.startsWith("<![CDATA[", r)) {
|
|
105
|
+
let t = e.indexOf("]]>", r + 9);
|
|
106
|
+
t < 0 && p("unterminated CDATA section", r), T(e.slice(r + 9, t), r, 4), b = t + 3;
|
|
107
|
+
} else if (e.startsWith("<!DOCTYPE", r)) {
|
|
108
|
+
(S || x) && p("a document type declaration must come once, before the root element", r), S = !0;
|
|
109
|
+
let t = r + 9, n = "";
|
|
110
|
+
for (; t < e.length; t++) {
|
|
111
|
+
let i = e[t];
|
|
112
|
+
if (n) n = i === n ? "" : n;
|
|
113
|
+
else if (i === "\"" || i === "'") n = i;
|
|
114
|
+
else if (i === "[") p("DTD internal subsets (entity declarations) are not supported", r);
|
|
115
|
+
else if (i === ">") break;
|
|
116
|
+
}
|
|
117
|
+
t >= e.length && p("unterminated document type declaration", r), b = t + 1;
|
|
118
|
+
} else if (e[r + 1] === "?") {
|
|
119
|
+
let t = e.indexOf("?>", r + 2);
|
|
120
|
+
t < 0 && p("unterminated processing instruction", r);
|
|
121
|
+
let n = e.slice(r + 2, t), i = /^[^ \t\r\n]*/.exec(n)[0];
|
|
122
|
+
if ((!a.test(i) || i.includes(":")) && p("invalid processing instruction target", r), i.toLowerCase() === "xml") (i !== "xml" || r !== +(e.charCodeAt(0) === 65279)) && p("the XML declaration must start the document", r);
|
|
123
|
+
else if (y.length) {
|
|
124
|
+
let e = n.slice(i.length).replace(/^[ \t\r\n]+/, "");
|
|
125
|
+
w().childNodes.push({
|
|
126
|
+
nodeType: 7,
|
|
127
|
+
target: i,
|
|
128
|
+
data: e,
|
|
129
|
+
textContent: e
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
b = t + 2;
|
|
133
|
+
} else if (e[r + 1] === "/") {
|
|
134
|
+
let t = _(r + 2), n = g(r + 2 + t.length);
|
|
135
|
+
e[n] !== ">" && p(`malformed end tag </${t}>`, r);
|
|
136
|
+
let i = y.pop();
|
|
137
|
+
(!i || i.tagName !== t) && p(`end tag </${t}> does not match <${i ? i.tagName : "nothing"}>`, r), v.pop(), i.end = n + 1, b = n + 1;
|
|
138
|
+
} else {
|
|
139
|
+
x && !y.length && p("more than one root element", r);
|
|
140
|
+
let a = _(r + 1), o = [], s = r + 1 + a.length, c = !1;
|
|
141
|
+
for (;;) {
|
|
142
|
+
let t = g(s);
|
|
143
|
+
if (e.startsWith("/>", t)) {
|
|
144
|
+
c = !0, s = t + 2;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (e[t] === ">") {
|
|
148
|
+
s = t + 1;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
t >= e.length && p(`unterminated start tag <${a}>`, r), t === s && p(`attributes of <${a}> need white space between them`, t);
|
|
152
|
+
let n = _(t), i = g(t + n.length);
|
|
153
|
+
e[i] !== "=" && p(`attribute ${n} has no value`, t), i = g(i + 1);
|
|
154
|
+
let l = e[i];
|
|
155
|
+
l !== "\"" && l !== "'" && p(`attribute ${n} value is not quoted`, i);
|
|
156
|
+
let u = e.indexOf(l, i + 1);
|
|
157
|
+
u < 0 && p(`unterminated value of attribute ${n}`, i);
|
|
158
|
+
let d = e.slice(i + 1, u);
|
|
159
|
+
d.includes("<") && p(`"<" in the value of attribute ${n}`, i), o.some((e) => e.name === n) && p(`duplicate attribute ${n}`, t), o.push({
|
|
160
|
+
name: n,
|
|
161
|
+
value: h(d.replace(/\r\n|[\r\n\t]/g, " "), i)
|
|
162
|
+
}), s = u + 1;
|
|
163
|
+
}
|
|
164
|
+
let l = Object.create(v[v.length - 1]);
|
|
165
|
+
for (let { name: e, value: i } of o) if (e === "xmlns") (i === t || i === n) && p("the reserved XML namespaces cannot be the default", r), l[""] = i || null;
|
|
166
|
+
else if (e.startsWith("xmlns:")) {
|
|
167
|
+
let a = e.slice(6);
|
|
168
|
+
(a === "xmlns" || a === "xml" != (i === t) || i === n || !i) && p(`invalid namespace declaration ${e}`, r), l[a] = i;
|
|
169
|
+
}
|
|
170
|
+
let d = (e, t) => {
|
|
171
|
+
let n = e.indexOf(":");
|
|
172
|
+
if (n < 0) return [
|
|
173
|
+
null,
|
|
174
|
+
e,
|
|
175
|
+
t ? l[""] : null
|
|
176
|
+
];
|
|
177
|
+
let i = e.slice(0, n), a = l[i];
|
|
178
|
+
return a || p(`namespace prefix ${i} is not declared`, r), [
|
|
179
|
+
i,
|
|
180
|
+
e.slice(n + 1),
|
|
181
|
+
a
|
|
182
|
+
];
|
|
183
|
+
}, m = o.map(({ name: e, value: t }) => {
|
|
184
|
+
let [r, i, a] = e === "xmlns" ? [
|
|
185
|
+
null,
|
|
186
|
+
"xmlns",
|
|
187
|
+
n
|
|
188
|
+
] : e.startsWith("xmlns:") ? [
|
|
189
|
+
"xmlns",
|
|
190
|
+
e.slice(6),
|
|
191
|
+
n
|
|
192
|
+
] : d(e, !1);
|
|
193
|
+
return {
|
|
194
|
+
name: e,
|
|
195
|
+
prefix: r,
|
|
196
|
+
localName: i,
|
|
197
|
+
namespaceURI: a,
|
|
198
|
+
value: t
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
for (let e = 0; e < m.length; e++) for (let t = 0; t < e; t++) m[e].namespaceURI === m[t].namespaceURI && m[e].localName === m[t].localName && p(`attributes ${m[t].name} and ${m[e].name} are the same name`, r);
|
|
202
|
+
let [S, T, E] = d(a, !0);
|
|
203
|
+
++C > f && p(`more than ${f} elements`, r), y.length >= i && p(`elements nest deeper than ${i}`, r);
|
|
204
|
+
let D = new u(a, S, T, E, m, w() ?? null, r);
|
|
205
|
+
D.contentStart = s, y.length ? (w().childNodes.push(D), w().children.push(D)) : x = D, c ? D.end = s : (y.push(D), v.push(l)), b = s;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return y.length && p(`document ends inside <${w().tagName}>`, e.length), x || p("no root element", e.length), new d(x);
|
|
209
|
+
}
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/scxml.ts
|
|
212
|
+
var p = "http://www.w3.org/2005/07/scxml", m = "urn:margoengine:fsm:1", h = "http://www.w3.org/2000/xmlns/", g = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/, _ = /^[A-Za-z_][\w.-]*$/, v = 1048576;
|
|
213
|
+
function y(e) {
|
|
214
|
+
throw TypeError(`Unsupported SCXML: ${e}; use the documented document subset or native machine JSON`);
|
|
215
|
+
}
|
|
216
|
+
var b = (e, t) => e.getAttributeNS(m, t), x = (e) => (b(e, "name") ?? e.getAttribute("id")) || y("state has no id/name"), S = (e) => Array.from(e.children), C = (e, t) => e.namespaceURI === p && e.localName === t, w = (e) => C(e, "state") || C(e, "parallel");
|
|
217
|
+
function T(e, t, n = []) {
|
|
218
|
+
for (let r of Array.from(e.attributes)) r.namespaceURI !== h && (r.namespaceURI === m && n.includes(r.localName) ? ([
|
|
219
|
+
"wrapper",
|
|
220
|
+
"regionHistory",
|
|
221
|
+
"stay"
|
|
222
|
+
].includes(r.localName) && !["true", "false"].includes(r.value) && y("invalid native boolean"), r.localName === "transitions" && r.value !== "empty" && y("invalid native transitions marker")) : (r.namespaceURI || !t.includes(r.name)) && y(`attribute ${r.name} on ${e.localName}`));
|
|
223
|
+
}
|
|
224
|
+
function E(e) {
|
|
225
|
+
let t = e.trim().replace(/\(\)\s*;?$/, "");
|
|
226
|
+
return g.test(t) || y("executable expressions are not binding names"), t;
|
|
227
|
+
}
|
|
228
|
+
function D(t, n = {}) {
|
|
229
|
+
(typeof t != "string" || t.length > v || new TextEncoder().encode(t).byteLength > v || /<!\s*(?:DOCTYPE|ENTITY)\b/i.test(t)) && y("input is over 1 MiB or contains a DTD/entity declaration");
|
|
230
|
+
let r = f(t, { fail: (e) => y(`malformed XML, ${e}`) }).documentElement;
|
|
231
|
+
C(r, "scxml") || y("wrong root element or namespace");
|
|
232
|
+
let i = /* @__PURE__ */ new Set(), a = [[r, 0]], o = 0;
|
|
233
|
+
for (; a.length;) {
|
|
234
|
+
let [e, t] = a.pop();
|
|
235
|
+
(++o > 1e4 || t > 128) && y("document exceeds 10,000 elements or 128 nesting levels");
|
|
236
|
+
let n = e.getAttribute("id");
|
|
237
|
+
n !== null && ((!_.test(n) || i.has(n)) && y("duplicate or invalid XML id"), i.add(n));
|
|
238
|
+
for (let t of Array.from(e.childNodes)) t.nodeType === 7 && y("processing instructions"), (t.nodeType === 3 || t.nodeType === 4) && t.textContent?.trim() && !C(e, "script") && y(`unexpected text in ${e.localName}`);
|
|
239
|
+
for (let n of S(e)) a.push([n, t + 1]);
|
|
240
|
+
}
|
|
241
|
+
T(r, [
|
|
242
|
+
"version",
|
|
243
|
+
"initial",
|
|
244
|
+
"datamodel",
|
|
245
|
+
"name"
|
|
246
|
+
], ["initial", "transitions"]), r.getAttribute("version") !== "1.0" && y("version must be 1.0"), r.hasAttribute("datamodel") && !["null", "ecmascript"].includes(r.getAttribute("datamodel")) && y("unknown datamodel");
|
|
247
|
+
function s(e, t = !1) {
|
|
248
|
+
!t && C(e, "state") && T(e, ["id", "initial"], [
|
|
249
|
+
"name",
|
|
250
|
+
"wrapper",
|
|
251
|
+
"transitions"
|
|
252
|
+
]);
|
|
253
|
+
let n = Object.create(null), r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = [], o = [], c = (e, t) => {
|
|
254
|
+
let i = x(e), o = e.getAttribute("id") ?? y("state id is required");
|
|
255
|
+
Object.hasOwn(n, i) && y(`duplicate state name ${i}`), T(e, ["id", ...C(e, "state") ? ["initial"] : []], [
|
|
256
|
+
"name",
|
|
257
|
+
"initial",
|
|
258
|
+
"history",
|
|
259
|
+
"update",
|
|
260
|
+
"transitions",
|
|
261
|
+
...C(e, "parallel") ? ["regionHistory"] : ["wrapper"]
|
|
262
|
+
]);
|
|
263
|
+
let l = {};
|
|
264
|
+
if (t !== void 0 && (l.parent = t), n[i] = l, r.set(o, i), a.push([
|
|
265
|
+
e,
|
|
266
|
+
i,
|
|
267
|
+
l
|
|
268
|
+
]), C(e, "parallel")) {
|
|
269
|
+
let t = Object.create(null);
|
|
270
|
+
for (let n of S(e).filter(w)) {
|
|
271
|
+
let e = x(n);
|
|
272
|
+
Object.hasOwn(t, e) && y("duplicate region name"), t[e] = s(n, b(n, "wrapper") !== "true");
|
|
273
|
+
}
|
|
274
|
+
Object.keys(t).length || y("parallel has no regions"), l.regions = t;
|
|
275
|
+
let n = b(e, "regionHistory");
|
|
276
|
+
n !== null && (["true", "false"].includes(n) || y("invalid regionHistory"), l.regionHistory = n === "true");
|
|
277
|
+
} else for (let t of S(e).filter(w)) c(t, i);
|
|
278
|
+
};
|
|
279
|
+
if (t) c(e);
|
|
280
|
+
else for (let t of S(e).filter(w)) c(t);
|
|
281
|
+
let l = (e) => r.get(e) ?? i.get(e) ?? y(`target ${e} crosses a region or is unknown`), u = (e, t) => {
|
|
282
|
+
let n = S(e).filter((e) => C(e, "initial"));
|
|
283
|
+
(n.length > 1 || n.length && e.hasAttribute("initial")) && y("initial attribute and element conflict");
|
|
284
|
+
let r = b(e, "initial");
|
|
285
|
+
if (r !== null && r !== "none" && y("invalid native initial marker"), r === "none") return;
|
|
286
|
+
let i = e.getAttribute("initial");
|
|
287
|
+
if (n.length) {
|
|
288
|
+
T(n[0], []);
|
|
289
|
+
let e = S(n[0]);
|
|
290
|
+
(e.length !== 1 || !C(e[0], "transition")) && y("initial requires one transition"), T(e[0], ["target"]), S(e[0]).length && y("initial executable content"), i = e[0].getAttribute("target");
|
|
291
|
+
}
|
|
292
|
+
return !i && t && (i = S(e).find(w)?.getAttribute("id") ?? null), i ? l(i) : void 0;
|
|
293
|
+
};
|
|
294
|
+
for (let [e, t, n] of a) {
|
|
295
|
+
let r = S(e).filter((e) => C(e, "history"));
|
|
296
|
+
r.length > 1 && y("more than one history per state");
|
|
297
|
+
let a = b(e, "history");
|
|
298
|
+
a !== null && (a !== "shallow" && a !== "deep" && y("invalid native history"), n.history = a);
|
|
299
|
+
for (let e of r) {
|
|
300
|
+
T(e, ["id", "type"]);
|
|
301
|
+
let r = e.getAttribute("id") ?? y("history id is required"), a = e.getAttribute("type") ?? "shallow";
|
|
302
|
+
a !== "shallow" && a !== "deep" && y("invalid history type"), n.history = a, i.set(r, t);
|
|
303
|
+
let o = S(e);
|
|
304
|
+
(o.length !== 1 || !C(o[0], "transition")) && y("history requires one default transition"), T(o[0], ["target"]), S(o[0]).length && y("history executable content"), n.initial = l(o[0].getAttribute("target") ?? "");
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
for (let [e, t, n] of a) {
|
|
308
|
+
let r = C(e, "parallel") ? void 0 : u(e, !0);
|
|
309
|
+
r !== void 0 && (n.initial !== void 0 && n.initial !== r && y("history default differs from ordinary initial"), n.initial = r);
|
|
310
|
+
let i = b(e, "update");
|
|
311
|
+
i !== null && (n.update = i);
|
|
312
|
+
let a = /* @__PURE__ */ new Set();
|
|
313
|
+
for (let r of S(e)) if (!(w(r) || C(r, "history") || C(r, "initial"))) {
|
|
314
|
+
if (C(r, "onentry") || C(r, "onexit")) {
|
|
315
|
+
let e = C(r, "onentry") ? "enter" : "exit";
|
|
316
|
+
a.has(e) && y("multiple hook blocks"), a.add(e), T(r, []);
|
|
317
|
+
let t = S(r);
|
|
318
|
+
t.length !== 1 && y("a hook requires one named action");
|
|
319
|
+
let i = t[0];
|
|
320
|
+
i.namespaceURI === m && i.localName === "hook" ? (T(i, ["name"]), (S(i).length || i.textContent?.trim()) && y("hook content"), n[e] = i.getAttribute("name") ?? y("hook has no name")) : C(i, "script") ? (T(i, []), S(i).length && y("nested script content"), n[e] = E(i.textContent ?? "")) : y("executable content is not a named hook");
|
|
321
|
+
} else if (C(r, "transition")) {
|
|
322
|
+
T(r, [
|
|
323
|
+
"target",
|
|
324
|
+
"event",
|
|
325
|
+
"cond",
|
|
326
|
+
"type"
|
|
327
|
+
], [
|
|
328
|
+
"after",
|
|
329
|
+
"duration",
|
|
330
|
+
"guard",
|
|
331
|
+
"order",
|
|
332
|
+
"stay"
|
|
333
|
+
]), S(r).length && y("transition executable content"), r.hasAttribute("type") && !["internal", "external"].includes(r.getAttribute("type")) && y("invalid transition type");
|
|
334
|
+
let e = {
|
|
335
|
+
from: t,
|
|
336
|
+
to: l(r.getAttribute("target") ?? "")
|
|
337
|
+
}, n = r.getAttribute("event"), i = b(r, "after");
|
|
338
|
+
n !== null && ((!n || /\s|\*/.test(n)) && y("event lists or wildcards"), e.event = n), i !== null && (e.after = Number(i));
|
|
339
|
+
let a = b(r, "guard") ?? r.getAttribute("cond");
|
|
340
|
+
a !== null && (e.guard = b(r, "guard") === null ? E(a) : a);
|
|
341
|
+
let s = b(r, "duration");
|
|
342
|
+
s !== null && (e.duration = Number(s));
|
|
343
|
+
let c = b(r, "order");
|
|
344
|
+
c !== null && (!/^\d+$/.test(c) || !Number.isSafeInteger(Number(c))) && y("invalid transition order"), o.push({
|
|
345
|
+
transition: e,
|
|
346
|
+
order: c === null ? o.length : Number(c),
|
|
347
|
+
internal: r.getAttribute("type") === "internal",
|
|
348
|
+
stay: b(r, "stay") === "true"
|
|
349
|
+
});
|
|
350
|
+
} else y(`element ${r.localName} is outside the document subset`);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (!t) for (let t of S(e)) !w(t) && !C(t, "initial") && y("unsupported root/region content");
|
|
354
|
+
let d = t ? x(e) : u(e, !0);
|
|
355
|
+
d || y("machine has no initial state");
|
|
356
|
+
let f = {
|
|
357
|
+
states: n,
|
|
358
|
+
initial: d
|
|
359
|
+
}, p = (e) => {
|
|
360
|
+
let t = [];
|
|
361
|
+
for (let r = e; r !== void 0; r = n[r].parent) t.push(r);
|
|
362
|
+
return t;
|
|
363
|
+
};
|
|
364
|
+
for (let { transition: { from: e, to: t }, internal: n, stay: r } of o) r && (!n || e !== t && !p(e).includes(t)) && y("native stay marks only an internal transition to the source or its ancestor"), !r && ((e === t || p(e).includes(t)) && y(`transition from ${e} to itself or its ancestor ${t} would exit and re-enter ${t} in SCXML; target a child state, or use a native machine document`), !n && p(t).includes(e) && y(`external transition from ${e} to its descendant ${t} would exit and re-enter ${e}; add type="internal"`));
|
|
365
|
+
return (o.length || b(e, "transitions") === "empty") && (f.transitions = o.sort((e, t) => e.order - t.order).map((e) => e.transition)), f;
|
|
366
|
+
}
|
|
367
|
+
let c = {
|
|
368
|
+
version: 1,
|
|
369
|
+
...s(r)
|
|
370
|
+
}, l = e(c);
|
|
371
|
+
return l.length && y(l.map((e) => `${e.path}: ${e.message}`).join("; ")), c;
|
|
372
|
+
}
|
|
373
|
+
var O = (e) => (/[^\u0009\u000a\u000d\u0020-\ud7ff\ue000-\ufffd\u{10000}-\u{10ffff}]/u.test(e) && y("value contains a character forbidden by XML 1.0"), e.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\n", " ").replaceAll("\r", " ").replaceAll(" ", "	"));
|
|
374
|
+
function k(t, n = {}) {
|
|
375
|
+
let r = n.extensions !== !1, i = /* @__PURE__ */ new Set(), a = 0, o = (e, t) => {
|
|
376
|
+
(t > 64 || i.has(e)) && y("cyclic or overly nested document"), i.add(e), a += 1 + Object.keys(e.states ?? {}).length + (e.transitions?.length ?? 0), a > 1e4 && y("output exceeds 10,000 elements");
|
|
377
|
+
for (let n of Object.values(e.states ?? {})) for (let e of Object.values(n?.regions ?? {})) o(e, t + 1);
|
|
378
|
+
i.delete(e);
|
|
379
|
+
};
|
|
380
|
+
o(t, 0);
|
|
381
|
+
let s = e(t);
|
|
382
|
+
s.length && y(s.map((e) => `${e.path}: ${e.message}`).join("; "));
|
|
383
|
+
let c = /* @__PURE__ */ new Set(), l = 0, u = (e) => {
|
|
384
|
+
if (_.test(e) && !c.has(e)) return c.add(e), e;
|
|
385
|
+
r || y("non-XML or repeated state/region name; enable extensions");
|
|
386
|
+
let t;
|
|
387
|
+
do
|
|
388
|
+
t = `margo${l++}`;
|
|
389
|
+
while (c.has(t));
|
|
390
|
+
return c.add(t), t;
|
|
391
|
+
}, d = (e, t) => ` ${e}="${O(String(t))}"`, f = (e, t) => {
|
|
392
|
+
let n = [];
|
|
393
|
+
for (let r = t; r !== void 0; r = e.states[r].parent) n.push(r);
|
|
394
|
+
return n;
|
|
395
|
+
}, h = (e, t) => (r || y(`${e} requires namespaced extensions; enable extensions`), d(`m:${e}`, t));
|
|
396
|
+
function b(e, t, n, i) {
|
|
397
|
+
i > 128 && y("overly nested document");
|
|
398
|
+
let a = new Map(Object.keys(e.states).map((e) => [e, u(e)])), o = (e) => a.get(e), s = `<${t}${n}${d("initial", o(e.initial))}${e.transitions?.length === 0 && r ? d("m:transitions", "empty") : ""}>`, c = (t, n) => {
|
|
399
|
+
i + n + 3 > 128 && y("overly nested hierarchy");
|
|
400
|
+
let a = e.states[t], s = Object.keys(e.states).filter((n) => e.states[n].parent === t);
|
|
401
|
+
a.regions && s.length && y("parallel holder with separate hierarchical children");
|
|
402
|
+
let l = a.regions ? "parallel" : "state", p = `<${l}${d("id", o(t))}${o(t) === t ? "" : h("name", t)}`;
|
|
403
|
+
a.initial === void 0 ? s.length && (p += h("initial", "none")) : p += d("initial", o(a.initial)), a.history !== void 0 && (p += h("history", a.history)), a.update !== void 0 && (p += h("update", a.update)), a.regionHistory !== void 0 && (p += h("regionHistory", String(a.regionHistory))), p += ">";
|
|
404
|
+
for (let [e, t] of [["enter", "onentry"], ["exit", "onexit"]]) if (a[e] !== void 0) {
|
|
405
|
+
let n = a[e];
|
|
406
|
+
g.test(n) ? p += `<${t}><script>${O(n)}()<\/script></${t}>` : (r || y("hook name requires extensions"), p += `<${t}><m:hook${d("name", n)}/></${t}>`);
|
|
407
|
+
}
|
|
408
|
+
for (let [n, i] of (e.transitions ?? []).entries()) i.from === t && (p += `<transition${d("target", o(i.to))}${d("type", "internal")}`, i.event !== void 0 && (/\s|\*/.test(i.event) && y("event lists or wildcards"), p += d("event", i.event)), i.after !== void 0 && (p += h("after", i.after)), i.duration !== void 0 && (p += h("duration", i.duration)), i.guard !== void 0 && (p += g.test(i.guard) ? d("cond", `${i.guard}()`) : h("guard", i.guard)), r && (p += d("m:order", n)), (i.from === i.to || f(e, i.from).includes(i.to)) && (p += h("stay", "true")), p += "/>");
|
|
409
|
+
for (let e of s) p += c(e, n + 1);
|
|
410
|
+
for (let [e, t] of Object.entries(a.regions ?? {})) {
|
|
411
|
+
let a = u(e);
|
|
412
|
+
r || y("native parallel region boundaries require extensions"), p += b(t, "state", d("id", a) + d("m:name", e) + d("m:wrapper", "true"), i + n + 2);
|
|
413
|
+
}
|
|
414
|
+
return p + `</${l}>`;
|
|
415
|
+
};
|
|
416
|
+
for (let t of Object.keys(e.states)) e.states[t].parent === void 0 && (s += c(t, 0));
|
|
417
|
+
return s + `</${t}>`;
|
|
418
|
+
}
|
|
419
|
+
let x = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + b(t, "scxml", d("xmlns", p) + (r ? d("xmlns:m", m) : "") + d("version", "1.0"), 0);
|
|
420
|
+
return (x.length > v || new TextEncoder().encode(x).byteLength > v) && y("output exceeds 1 MiB"), (x.match(/<(?!!|\?|\/)/g) ?? []).length > 1e4 && y("output exceeds 10,000 elements"), x;
|
|
421
|
+
}
|
|
422
|
+
//#endregion
|
|
423
|
+
export { k as exportSCXML, D as importSCXML };
|
package/dist/trace.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { StateMachine } from './index.js';
|
|
2
|
+
/** Where parallel regions are: plain data, inside a {@link MachineSnapshot}. */
|
|
3
|
+
export interface RegionsSnapshot {
|
|
4
|
+
/** Whether the regions are running. */
|
|
5
|
+
running: boolean;
|
|
6
|
+
/** Where each region starts next, in region order. */
|
|
7
|
+
starts: string[];
|
|
8
|
+
/** Each region's machine, in region order. */
|
|
9
|
+
machines: MachineSnapshot[];
|
|
10
|
+
}
|
|
11
|
+
/** Where a machine is, as plain data, so it saves as JSON or goes over a network. The definition is not in it. */
|
|
12
|
+
export interface MachineSnapshot {
|
|
13
|
+
/** Format version; 1. */
|
|
14
|
+
version: 1;
|
|
15
|
+
/** Current state. */
|
|
16
|
+
state: string;
|
|
17
|
+
/** State being cross-faded from, or null. */
|
|
18
|
+
previous: string | null;
|
|
19
|
+
/** Seconds since the last state change. */
|
|
20
|
+
elapsed: number;
|
|
21
|
+
/** Length of the running cross-fade in seconds. */
|
|
22
|
+
duration: number;
|
|
23
|
+
/** The machine's running time. */
|
|
24
|
+
clock: number;
|
|
25
|
+
/** When each active state was entered, as name and clock pairs. */
|
|
26
|
+
entered: [string, number][];
|
|
27
|
+
/**
|
|
28
|
+
* Delayed transitions that started waiting again while their `from` stayed active, as index in `transitions` and
|
|
29
|
+
* clock pairs. Optional: a snapshot without it restores with none.
|
|
30
|
+
*/
|
|
31
|
+
waits?: [number, number][];
|
|
32
|
+
/** What states remember, as name and child pairs. */
|
|
33
|
+
memory: [string, string][];
|
|
34
|
+
/** The regions held by states, as state name and snapshot pairs. */
|
|
35
|
+
regions: [string, RegionsSnapshot][];
|
|
36
|
+
}
|
|
37
|
+
/** One state change seen by a {@link MachineTrace}. */
|
|
38
|
+
export interface TraceRecord {
|
|
39
|
+
/** Which machine: empty for the traced one, else the state and region names down to it, such as `alive/weapon`. */
|
|
40
|
+
machine: string;
|
|
41
|
+
/** That machine's `clock` at the change. */
|
|
42
|
+
clock: number;
|
|
43
|
+
/** State left, or null when regions started. */
|
|
44
|
+
from: string | null;
|
|
45
|
+
/** State reached, or null when regions stopped. */
|
|
46
|
+
to: string | null;
|
|
47
|
+
/** The event that caused it; null for `goto` and delays. */
|
|
48
|
+
event: string | null;
|
|
49
|
+
/** The delay that caused it; null for `goto` and events. */
|
|
50
|
+
after: number | null;
|
|
51
|
+
}
|
|
52
|
+
/** Options of {@link MachineTrace}. */
|
|
53
|
+
export interface TraceOptions {
|
|
54
|
+
/** Most records kept; the oldest go first. A whole number of at least 1. Default 256. */
|
|
55
|
+
capacity?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Where `machine` is now, parallel regions included.
|
|
59
|
+
* @throws MargoError `'state'` when called from a hook during a change, where the machine is half way.
|
|
60
|
+
*/
|
|
61
|
+
export declare function snapshotMachine<C>(machine: StateMachine<C>): MachineSnapshot;
|
|
62
|
+
/**
|
|
63
|
+
* Put `machine` where a snapshot says. Runs no hooks: the snapshot holds the machine, and what hooks did to your context
|
|
64
|
+
* is yours to save. The machine must have the definition the snapshot was taken from.
|
|
65
|
+
* @throws TypeError when the snapshot does not fit this machine; the machine is then unchanged.
|
|
66
|
+
* @throws MargoError `'state'` when called from a hook during a change.
|
|
67
|
+
*/
|
|
68
|
+
export declare function restoreMachine<C>(machine: StateMachine<C>, snapshot: MachineSnapshot): void;
|
|
69
|
+
/**
|
|
70
|
+
* A rolling record of a machine's state changes, parallel regions included, for debugging. Tracing costs one object per
|
|
71
|
+
* change; a machine that is not traced pays nothing. `dispose()` (or `using`) stops it.
|
|
72
|
+
*/
|
|
73
|
+
export declare class MachineTrace<C> {
|
|
74
|
+
private readonly ring;
|
|
75
|
+
private readonly capacity;
|
|
76
|
+
private readonly attached;
|
|
77
|
+
private next;
|
|
78
|
+
/**
|
|
79
|
+
* @throws RangeError for a bad `capacity`.
|
|
80
|
+
* @throws MargoError `'attached'` when a machine already has a `listener`.
|
|
81
|
+
*/
|
|
82
|
+
constructor(machine: StateMachine<C>, options?: TraceOptions);
|
|
83
|
+
/** The records kept, oldest first. */
|
|
84
|
+
get records(): TraceRecord[];
|
|
85
|
+
/** Forget the records kept so far. */
|
|
86
|
+
clear(): void;
|
|
87
|
+
/** Stop tracing. Safe to call twice. */
|
|
88
|
+
dispose(): void;
|
|
89
|
+
[Symbol.dispose]: () => void;
|
|
90
|
+
private attach;
|
|
91
|
+
private push;
|
|
92
|
+
}
|
package/dist/trace.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/trace.ts
|
|
3
|
+
var t = (e) => e.charCodeAt(0) === 0, n = (e) => {
|
|
4
|
+
let t = [];
|
|
5
|
+
for (let n of Object.keys(e.states)) {
|
|
6
|
+
let { regions: r } = e.states[n];
|
|
7
|
+
r && t.push([n, r]);
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
}, r = (e) => Object.values(e.machines);
|
|
11
|
+
function i(t) {
|
|
12
|
+
if (t.changing) throw new e("state", "Snapshot during a state change; take it after the hook returns");
|
|
13
|
+
return {
|
|
14
|
+
version: 1,
|
|
15
|
+
state: t.state,
|
|
16
|
+
previous: t.previous,
|
|
17
|
+
elapsed: t.elapsed,
|
|
18
|
+
duration: t.duration,
|
|
19
|
+
clock: t.clock,
|
|
20
|
+
entered: [...t.entered],
|
|
21
|
+
waits: [...t.waits],
|
|
22
|
+
memory: [...t.memory],
|
|
23
|
+
regions: n(t).map(([t, n]) => {
|
|
24
|
+
if (n.busy) throw new e("state", "Snapshot during a delivery; take it after the hook returns");
|
|
25
|
+
return [t, {
|
|
26
|
+
running: n.running,
|
|
27
|
+
starts: [...n.starts],
|
|
28
|
+
machines: r(n).map(i)
|
|
29
|
+
}];
|
|
30
|
+
})
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
var a = (e, t) => typeof t == "string" && Object.hasOwn(e.states, t), o = (e, t) => {
|
|
34
|
+
let n = [];
|
|
35
|
+
for (let r = t; r !== void 0; r = e.states[r].parent) n.unshift(r);
|
|
36
|
+
return n;
|
|
37
|
+
}, s = (e) => typeof e == "number" && Number.isFinite(e) && e >= 0;
|
|
38
|
+
function c(e, t, i) {
|
|
39
|
+
let l = (e) => {
|
|
40
|
+
throw TypeError(`Invalid snapshot of machine ${i}, ${e}; restore into the machine it came from`);
|
|
41
|
+
};
|
|
42
|
+
t?.version !== 1 && l("unknown version; this reader takes version 1"), a(e, t.state) || l(`unknown state ${t.state}`), t.previous !== null && !a(e, t.previous) && l(`unknown state ${t.previous}`), (!s(t.elapsed) || !s(t.duration) || !s(t.clock)) && l("times are numbers of 0 or more"), (!Array.isArray(t.entered) || !Array.isArray(t.memory) || !Array.isArray(t.regions)) && l("entered, memory and regions are lists");
|
|
43
|
+
let u = o(e, t.state);
|
|
44
|
+
t.entered.length !== u.length && l("entered must hold exactly the active states");
|
|
45
|
+
for (let [e, n] of t.entered) (!u.includes(e) || !s(n) || n > t.clock) && l(`bad entry for ${e}`);
|
|
46
|
+
t.waits !== void 0 && !Array.isArray(t.waits) && l("waits is a list");
|
|
47
|
+
for (let [n, r] of t.waits ?? []) (e.transitions[n]?.after === void 0 || !s(r) || r > t.clock) && l(`bad wait for transition ${n}`);
|
|
48
|
+
for (let [n, r] of t.memory) (!a(e, r) || !o(e, r).includes(n) || n === r) && l(`${n} cannot remember ${r}`);
|
|
49
|
+
let d = n(e);
|
|
50
|
+
d.length !== t.regions.length && l("it holds other regions than this machine"), d.forEach(([e, n], o) => {
|
|
51
|
+
let [s, d] = t.regions[o], f = r(n);
|
|
52
|
+
(s !== e || d?.machines?.length !== f.length || d.starts?.length !== f.length) && l(`it holds other regions than state ${e}`), d.running !== u.includes(e) && l(`regions of ${e} run only while it is active`), f.forEach((t, n) => {
|
|
53
|
+
a(t, d.starts[n]) || l(`unknown start ${d.starts[n]} of a region of ${e}`), c(t, d.machines[n], `${i}/${e}`);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function l(e, t) {
|
|
58
|
+
e.state = t.state, e.previous = t.previous, e.elapsed = t.elapsed, e.duration = t.duration, e.clock = t.clock, e.entered.clear();
|
|
59
|
+
for (let [n, r] of t.entered) e.entered.set(n, r);
|
|
60
|
+
e.waits.clear();
|
|
61
|
+
for (let [n, r] of t.waits ?? []) e.waits.set(n, r);
|
|
62
|
+
e.memory.clear();
|
|
63
|
+
for (let [n, r] of t.memory) e.memory.set(n, r);
|
|
64
|
+
n(e).forEach(([, e], n) => {
|
|
65
|
+
let i = t.regions[n][1];
|
|
66
|
+
e.running = i.running, i.starts.forEach((t, n) => {
|
|
67
|
+
e.starts[n] = t;
|
|
68
|
+
}), r(e).forEach((e, t) => l(e, i.machines[t]));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function u(t, n) {
|
|
72
|
+
if (t.changing) throw new e("state", "Restore during a state change; call it after the hook returns");
|
|
73
|
+
c(t, n, "root"), l(t, n);
|
|
74
|
+
}
|
|
75
|
+
var d = class {
|
|
76
|
+
constructor(e, t = {}) {
|
|
77
|
+
this.ring = [], this.attached = [], this.next = 0;
|
|
78
|
+
let { capacity: n = 256 } = t;
|
|
79
|
+
if (!Number.isInteger(n) || n < 1) throw RangeError("Invalid trace capacity; pass a whole number of at least 1");
|
|
80
|
+
this.capacity = n;
|
|
81
|
+
try {
|
|
82
|
+
this.attach(e, "");
|
|
83
|
+
} catch (e) {
|
|
84
|
+
throw this.dispose(), e;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
get records() {
|
|
88
|
+
return [...this.ring.slice(this.next), ...this.ring.slice(0, this.next)];
|
|
89
|
+
}
|
|
90
|
+
clear() {
|
|
91
|
+
this.ring.length = 0, this.next = 0;
|
|
92
|
+
}
|
|
93
|
+
dispose() {
|
|
94
|
+
for (let e of this.attached) e.listener = null;
|
|
95
|
+
this.attached.length = 0;
|
|
96
|
+
}
|
|
97
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
98
|
+
this.dispose();
|
|
99
|
+
}
|
|
100
|
+
attach(t, r) {
|
|
101
|
+
if (t.listener) throw new e("attached", "Machine already has a listener; dispose that trace first");
|
|
102
|
+
t.listener = (e, n, i) => this.push(r, t.clock, e, n, i), this.attached.push(t);
|
|
103
|
+
for (let [e, i] of n(t)) for (let t of Object.keys(i.machines)) this.attach(i.machines[t], `${r ? `${r}/` : ""}${e}/${t}`);
|
|
104
|
+
}
|
|
105
|
+
push(e, n, r, i, a) {
|
|
106
|
+
let o = {
|
|
107
|
+
machine: e,
|
|
108
|
+
clock: n,
|
|
109
|
+
from: t(r) ? null : r,
|
|
110
|
+
to: t(i) ? null : i,
|
|
111
|
+
event: a?.event ?? null,
|
|
112
|
+
after: a?.after ?? null
|
|
113
|
+
};
|
|
114
|
+
this.ring.length < this.capacity ? this.ring.push(o) : (this.ring[this.next] = o, this.next = (this.next + 1) % this.capacity);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
//#endregion
|
|
118
|
+
export { d as MachineTrace, u as restoreMachine, i as snapshotMachine };
|