state-machine-cat 12.0.6 → 12.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/cli/actions.mjs +34 -31
  2. package/dist/cli/attributes-parser.mjs +914 -889
  3. package/dist/cli/execute-command-line.mjs +98 -48
  4. package/dist/cli/file-name-to-stream.mjs +8 -8
  5. package/dist/cli/normalize.mjs +89 -70
  6. package/dist/cli/validations.mjs +72 -52
  7. package/dist/index-node.mjs +12 -9
  8. package/dist/index.mjs +10 -7
  9. package/dist/options.mjs +53 -53
  10. package/dist/parse/index.mjs +17 -17
  11. package/dist/parse/parser-helpers.mjs +159 -139
  12. package/dist/parse/scxml/index.mjs +152 -129
  13. package/dist/parse/scxml/normalize-machine.mjs +36 -35
  14. package/dist/parse/scxml/utl.mjs +1 -1
  15. package/dist/parse/smcat/smcat-parser.mjs +2794 -2844
  16. package/dist/parse/smcat-ast.schema.mjs +185 -168
  17. package/dist/render/dot/attributebuilder.mjs +40 -37
  18. package/dist/render/dot/counter.mjs +14 -14
  19. package/dist/render/dot/dot.states.template.js +1 -26
  20. package/dist/render/dot/dot.template.js +1 -14
  21. package/dist/render/dot/index.mjs +129 -82
  22. package/dist/render/dot/render-dot-from-ast.mjs +33 -16
  23. package/dist/render/dot/state-transformers.mjs +96 -85
  24. package/dist/render/dot/transition-transformers.mjs +39 -41
  25. package/dist/render/dot/utl.mjs +21 -19
  26. package/dist/render/index-node.mjs +16 -16
  27. package/dist/render/index.mjs +9 -9
  28. package/dist/render/scjson/index.mjs +111 -94
  29. package/dist/render/scjson/make-valid-event-names.mjs +21 -18
  30. package/dist/render/scjson/make-valid-xml-name.mjs +17 -13
  31. package/dist/render/scxml/index.mjs +2 -1
  32. package/dist/render/scxml/render-from-scjson.mjs +5 -2
  33. package/dist/render/scxml/scxml.states.template.js +1 -14
  34. package/dist/render/scxml/scxml.template.js +1 -6
  35. package/dist/render/smcat/index.mjs +54 -39
  36. package/dist/render/smcat/smcat.template.js +1 -13
  37. package/dist/render/vector/dot-to-vector-native.mjs +30 -26
  38. package/dist/render/vector/vector-native-dot-with-fallback.mjs +27 -20
  39. package/dist/render/vector/vector-with-wasm.mjs +13 -6
  40. package/dist/state-machine-model.mjs +67 -52
  41. package/dist/transform/desugar.mjs +115 -66
  42. package/dist/transform/utl.mjs +12 -12
  43. package/dist/version.mjs +1 -1
  44. package/package.json +74 -74
  45. package/types/state-machine-cat.d.mts +209 -209
@@ -7,155 +7,178 @@ import { castArray } from "./utl.mjs";
7
7
  import { normalizeMachine } from "./normalize-machine.mjs";
8
8
  const formatLabel = utl.formatLabel;
9
9
  function extractActions(pState, pActionType) {
10
- return castArray(pState[pActionType]).map((pAction) => ({
11
- type: pActionType === "onexit" ? "exit" : "entry",
12
- body: he.decode(pAction).trim(),
13
- }));
10
+ return castArray(pState[pActionType]).map((pAction) => ({
11
+ type: pActionType === "onexit" ? "exit" : "entry",
12
+ body: he.decode(pAction).trim(),
13
+ }));
14
14
  }
15
15
  function extractActionsFromInvokes(pInvokeTriggers) {
16
- return castArray(pInvokeTriggers).map((pInvokeTrigger) => {
17
- const lId = he.decode(pInvokeTrigger.id || "").trim();
18
- return {
19
- type: "activity",
20
- body: lId || he.decode(pInvokeTrigger || "").trim(),
21
- };
22
- });
16
+ return castArray(pInvokeTriggers).map((pInvokeTrigger) => {
17
+ const lId = he.decode(pInvokeTrigger.id || "").trim();
18
+ return {
19
+ type: "activity",
20
+ body: lId || he.decode(pInvokeTrigger || "").trim(),
21
+ };
22
+ });
23
23
  }
24
24
  function deriveActions(pState) {
25
- let lReturnValue = [];
26
- if (pState.onentry) {
27
- lReturnValue = lReturnValue.concat(extractActions(pState, "onentry"));
28
- }
29
- if (pState.invoke) {
30
- lReturnValue = lReturnValue.concat(extractActionsFromInvokes(pState.invoke));
31
- }
32
- if (pState.onexit) {
33
- lReturnValue = lReturnValue.concat(extractActions(pState, "onexit"));
34
- }
35
- return lReturnValue;
25
+ let lReturnValue = [];
26
+ if (pState.onentry) {
27
+ lReturnValue = lReturnValue.concat(extractActions(pState, "onentry"));
28
+ }
29
+ if (pState.invoke) {
30
+ lReturnValue = lReturnValue.concat(
31
+ extractActionsFromInvokes(pState.invoke),
32
+ );
33
+ }
34
+ if (pState.onexit) {
35
+ lReturnValue = lReturnValue.concat(extractActions(pState, "onexit"));
36
+ }
37
+ return lReturnValue;
36
38
  }
37
39
  function deriveStateType(pType, pState) {
38
- return pType === "history" && pState.type === "deep" ? "deephistory" : pType;
40
+ return pType === "history" && pState.type === "deep" ? "deephistory" : pType;
39
41
  }
40
42
  function mapState(pType) {
41
- return (pState) => {
42
- const lReturnValue = {
43
- name: pState.id,
44
- type: deriveStateType(pType, pState),
45
- };
46
- if (parserHelpers.getStateType(pState.id) !== lReturnValue.type) {
47
- lReturnValue.typeExplicitlySet = true;
48
- }
49
- if (pState.onentry || pState.onexit || pState.invoke) {
50
- lReturnValue.actions = deriveActions(pState);
51
- }
52
- if (Object.keys(pState).some((pKey) => ["initial", "state", "history", "parallel", "final"].includes(pKey))) {
53
- lReturnValue.statemachine = mapMachine(pState);
54
- }
55
- return lReturnValue;
56
- };
43
+ return (pState) => {
44
+ const lReturnValue = {
45
+ name: pState.id,
46
+ type: deriveStateType(pType, pState),
47
+ };
48
+ if (parserHelpers.getStateType(pState.id) !== lReturnValue.type) {
49
+ lReturnValue.typeExplicitlySet = true;
50
+ }
51
+ if (pState.onentry || pState.onexit || pState.invoke) {
52
+ lReturnValue.actions = deriveActions(pState);
53
+ }
54
+ if (
55
+ Object.keys(pState).some((pKey) =>
56
+ ["initial", "state", "history", "parallel", "final"].includes(pKey),
57
+ )
58
+ ) {
59
+ lReturnValue.statemachine = mapMachine(pState);
60
+ }
61
+ return lReturnValue;
62
+ };
57
63
  }
58
64
  function extractTransitionAttributesFromObject(pTransition) {
59
- const lReturnValue = {};
60
- if (pTransition.event) {
61
- lReturnValue.event = pTransition.event.split(/\s+/).join("\n");
62
- }
63
- if (pTransition.cond) {
64
- lReturnValue.cond = pTransition.cond;
65
- }
66
- if (pTransition["#text"]) {
67
- lReturnValue.action = he.decode(pTransition["#text"]).trim();
68
- }
69
- if (pTransition.type) {
70
- lReturnValue.type = pTransition.type;
71
- }
72
- return lReturnValue;
65
+ const lReturnValue = {};
66
+ if (pTransition.event) {
67
+ lReturnValue.event = pTransition.event.split(/\s+/).join("\n");
68
+ }
69
+ if (pTransition.cond) {
70
+ lReturnValue.cond = pTransition.cond;
71
+ }
72
+ if (pTransition["#text"]) {
73
+ lReturnValue.action = he.decode(pTransition["#text"]).trim();
74
+ }
75
+ if (pTransition.type) {
76
+ lReturnValue.type = pTransition.type;
77
+ }
78
+ return lReturnValue;
73
79
  }
74
80
  function extractTransitionAttributes(pTransition) {
75
- const lReturnValue = {};
76
- if (typeof pTransition === "string") {
77
- lReturnValue.action = he.decode(pTransition).trim();
78
- }
79
- else {
80
- Object.assign(lReturnValue, extractTransitionAttributesFromObject(pTransition));
81
- }
82
- const lLabel = formatLabel(lReturnValue.event, lReturnValue.cond, lReturnValue.action);
83
- if (lLabel) {
84
- lReturnValue.label = lLabel;
85
- }
86
- return lReturnValue;
81
+ const lReturnValue = {};
82
+ if (typeof pTransition === "string") {
83
+ lReturnValue.action = he.decode(pTransition).trim();
84
+ } else {
85
+ Object.assign(
86
+ lReturnValue,
87
+ extractTransitionAttributesFromObject(pTransition),
88
+ );
89
+ }
90
+ const lLabel = formatLabel(
91
+ lReturnValue.event,
92
+ lReturnValue.cond,
93
+ lReturnValue.action,
94
+ );
95
+ if (lLabel) {
96
+ lReturnValue.label = lLabel;
97
+ }
98
+ return lReturnValue;
87
99
  }
88
100
  function reduceTransition(pState) {
89
- return (pAllTransitions, pTransition) => {
90
- const lTargets = (pTransition?.target ?? pState.id).split(/\s+/);
91
- const lTransitionAttributes = extractTransitionAttributes(pTransition);
92
- return pAllTransitions.concat(lTargets.map((pTarget) => ({
93
- from: pState.id,
94
- to: pTarget,
95
- ...lTransitionAttributes,
96
- })));
97
- };
101
+ return (pAllTransitions, pTransition) => {
102
+ const lTargets = (pTransition?.target ?? pState.id).split(/\s+/);
103
+ const lTransitionAttributes = extractTransitionAttributes(pTransition);
104
+ return pAllTransitions.concat(
105
+ lTargets.map((pTarget) => ({
106
+ from: pState.id,
107
+ to: pTarget,
108
+ ...lTransitionAttributes,
109
+ })),
110
+ );
111
+ };
98
112
  }
99
113
  function extractTransitions(pStates) {
100
- return pStates
101
- .filter((pState) => Object.prototype.hasOwnProperty.call(pState, "transition"))
102
- .reduce((pAllTransitions, pThisState) => {
103
- const lTransitionAsArray = castArray(pThisState.transition);
104
- return pAllTransitions.concat(lTransitionAsArray.reduce(reduceTransition(pThisState), []));
105
- }, []);
114
+ return pStates
115
+ .filter((pState) =>
116
+ Object.prototype.hasOwnProperty.call(pState, "transition"),
117
+ )
118
+ .reduce((pAllTransitions, pThisState) => {
119
+ const lTransitionAsArray = castArray(pThisState.transition);
120
+ return pAllTransitions.concat(
121
+ lTransitionAsArray.reduce(reduceTransition(pThisState), []),
122
+ );
123
+ }, []);
106
124
  }
107
125
  function mapMachine(pSCXMLStateMachine) {
108
- const lNormalizedMachine = normalizeMachine(pSCXMLStateMachine);
109
- const lReturnValue = {
110
- states: lNormalizedMachine.initial
111
- .map(mapState("initial"))
112
- .concat(lNormalizedMachine.state.map(mapState("regular")))
113
- .concat(lNormalizedMachine.parallel.map(mapState("parallel")))
114
- .concat(lNormalizedMachine.history.map(mapState("history")))
115
- .concat(lNormalizedMachine.final.map(mapState("final"))),
116
- };
117
- const lTransitions = extractTransitions(lNormalizedMachine.initial)
118
- .concat(extractTransitions(lNormalizedMachine.state))
119
- .concat(extractTransitions(lNormalizedMachine.parallel));
120
- if (lTransitions.length > 0) {
121
- lReturnValue.transitions = lTransitions;
122
- }
123
- return lReturnValue;
126
+ const lNormalizedMachine = normalizeMachine(pSCXMLStateMachine);
127
+ const lReturnValue = {
128
+ states: lNormalizedMachine.initial
129
+ .map(mapState("initial"))
130
+ .concat(lNormalizedMachine.state.map(mapState("regular")))
131
+ .concat(lNormalizedMachine.parallel.map(mapState("parallel")))
132
+ .concat(lNormalizedMachine.history.map(mapState("history")))
133
+ .concat(lNormalizedMachine.final.map(mapState("final"))),
134
+ };
135
+ const lTransitions = extractTransitions(lNormalizedMachine.initial)
136
+ .concat(extractTransitions(lNormalizedMachine.state))
137
+ .concat(extractTransitions(lNormalizedMachine.parallel));
138
+ if (lTransitions.length > 0) {
139
+ lReturnValue.transitions = lTransitions;
140
+ }
141
+ return lReturnValue;
124
142
  }
125
143
  function deDuplicateAttributesAndTags(pObject, pAttributeNamePrefix) {
126
- return traverse(pObject).map(function deDuplicate() {
127
- if (this.key?.startsWith(pAttributeNamePrefix)) {
128
- const pUnPrefixedAttributeName = this.key.slice(pAttributeNamePrefix.length);
129
- if (this.parent.keys.includes(pUnPrefixedAttributeName)) {
130
- this.remove();
131
- }
132
- else {
133
- this.parent.node[pUnPrefixedAttributeName] = this.node;
134
- this.remove();
135
- }
136
- }
137
- });
144
+ return traverse(pObject).map(function deDuplicate() {
145
+ if (this.key?.startsWith(pAttributeNamePrefix)) {
146
+ const pUnPrefixedAttributeName = this.key.slice(
147
+ pAttributeNamePrefix.length,
148
+ );
149
+ if (this.parent.keys.includes(pUnPrefixedAttributeName)) {
150
+ this.remove();
151
+ } else {
152
+ this.parent.node[pUnPrefixedAttributeName] = this.node;
153
+ this.remove();
154
+ }
155
+ }
156
+ });
138
157
  }
139
158
  export function parse(pSCXMLString) {
140
- const lTrimmedSCXMLString = pSCXMLString.trim();
141
- const lAttributeNamePrefix = "@_";
142
- let lXMLAsJSON = {};
143
- const lXMLParser = new fastxml.XMLParser({
144
- attributeNamePrefix: lAttributeNamePrefix,
145
- ignoreAttributes: false,
146
- parseTagValue: true,
147
- processEntities: false,
148
- tagValueProcessor: (_pTagName, pTagValue) => he.decode(pTagValue),
149
- stopNodes: ["*.onentry", "*.onexit", "*.transition"],
150
- });
151
- try {
152
- lXMLAsJSON = deDuplicateAttributesAndTags(lXMLParser.parse(lTrimmedSCXMLString, true), lAttributeNamePrefix);
153
- }
154
- catch (pError) {
155
- throw new Error("That doesn't look like valid xml ...\n");
156
- }
157
- return mapMachine(lXMLAsJSON?.scxml ?? {
158
- xmlns: "http://www.w3.org/2005/07/scxml",
159
- version: "1.0",
160
- });
159
+ const lTrimmedSCXMLString = pSCXMLString.trim();
160
+ const lAttributeNamePrefix = "@_";
161
+ let lXMLAsJSON = {};
162
+ const lXMLParser = new fastxml.XMLParser({
163
+ attributeNamePrefix: lAttributeNamePrefix,
164
+ ignoreAttributes: false,
165
+ parseTagValue: true,
166
+ processEntities: false,
167
+ tagValueProcessor: (_pTagName, pTagValue) => he.decode(pTagValue),
168
+ stopNodes: ["*.onentry", "*.onexit", "*.transition"],
169
+ });
170
+ try {
171
+ lXMLAsJSON = deDuplicateAttributesAndTags(
172
+ lXMLParser.parse(lTrimmedSCXMLString, true),
173
+ lAttributeNamePrefix,
174
+ );
175
+ } catch (pError) {
176
+ throw new Error("That doesn't look like valid xml ...\n");
177
+ }
178
+ return mapMachine(
179
+ lXMLAsJSON?.scxml ?? {
180
+ xmlns: "http://www.w3.org/2005/07/scxml",
181
+ version: "1.0",
182
+ },
183
+ );
161
184
  }
@@ -1,44 +1,45 @@
1
1
  import { castArray } from "./utl.mjs";
2
2
  function normalizeInitialFromObject(pInitialObject, pId) {
3
- const lReturnValue = {
4
- id: pId ? `${pId}.initial` : "initial",
5
- };
6
- if (pInitialObject.transition) {
7
- Object.assign(lReturnValue, {
8
- transition: [pInitialObject.transition],
9
- });
10
- }
11
- return lReturnValue;
3
+ const lReturnValue = {
4
+ id: pId ? `${pId}.initial` : "initial",
5
+ };
6
+ if (pInitialObject.transition) {
7
+ Object.assign(lReturnValue, {
8
+ transition: [pInitialObject.transition],
9
+ });
10
+ }
11
+ return lReturnValue;
12
12
  }
13
13
  function normalizeInitialFromString(pString) {
14
- return {
15
- id: "initial",
16
- transition: [
17
- {
18
- target: pString,
19
- },
20
- ],
21
- };
14
+ return {
15
+ id: "initial",
16
+ transition: [
17
+ {
18
+ target: pString,
19
+ },
20
+ ],
21
+ };
22
22
  }
23
23
  function normalizeInitial(pMachine) {
24
- const lReturnValue = [];
25
- if (pMachine.initial) {
26
- if (typeof pMachine.initial === "string") {
27
- lReturnValue.push(normalizeInitialFromString(pMachine.initial));
28
- }
29
- else {
30
- lReturnValue.push(normalizeInitialFromObject(pMachine.initial, pMachine.id));
31
- }
32
- }
33
- return lReturnValue;
24
+ const lReturnValue = [];
25
+ if (pMachine.initial) {
26
+ if (typeof pMachine.initial === "string") {
27
+ lReturnValue.push(normalizeInitialFromString(pMachine.initial));
28
+ } else {
29
+ lReturnValue.push(
30
+ normalizeInitialFromObject(pMachine.initial, pMachine.id),
31
+ );
32
+ }
33
+ }
34
+ return lReturnValue;
34
35
  }
35
36
  export function normalizeMachine(pMachine) {
36
- return {
37
- ...pMachine,
38
- initial: normalizeInitial(pMachine),
39
- state: castArray(pMachine?.state ?? []),
40
- parallel: castArray(pMachine?.parallel ?? []),
41
- history: castArray(pMachine?.history ?? []),
42
- final: castArray(pMachine?.final ?? []),
43
- };
37
+ return {
38
+ ...pMachine,
39
+ initial: normalizeInitial(pMachine),
40
+ state: castArray(pMachine?.state ?? []),
41
+ parallel: castArray(pMachine?.parallel ?? []),
42
+ history: castArray(pMachine?.history ?? []),
43
+ final: castArray(pMachine?.final ?? []),
44
+ };
44
45
  }
@@ -1,3 +1,3 @@
1
1
  export function castArray(pAThing) {
2
- return Array.isArray(pAThing) ? pAThing : [pAThing];
2
+ return Array.isArray(pAThing) ? pAThing : [pAThing];
3
3
  }