jssm 5.62.0 → 5.63.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/dist/es6/jssm.js CHANGED
@@ -1,11 +1,9 @@
1
1
  // whargarbl lots of these return arrays could/should be sets
2
2
  import { reduce as reduce_to_639 } from 'reduce-to-639-1';
3
3
  import { seq, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key, array_box_if_string, hook_name, named_hook_name } from './jssm_util';
4
- import { parse } from './jssm-dot'; // TODO FIXME WHARGARBL this could be post-typed
4
+ import { parse } from './jssm-dot';
5
5
  import { version } from './version'; // replaced from package.js in build
6
- function xthrow(machine, message) {
7
- throw new Error(`${(machine.instance_name !== undefined) ? `[[${machine.instance_name}]]: ` : ''}${message}${machine.state !== undefined ? ` (at ${machine.state})` : ''}`);
8
- }
6
+ import { JssmError } from './jssm_error';
9
7
  /* eslint-disable complexity */
10
8
  function arrow_direction(arrow) {
11
9
  switch (String(arrow)) {
@@ -55,7 +53,7 @@ function arrow_direction(arrow) {
55
53
  case '<~⇒':
56
54
  return 'both';
57
55
  default:
58
- xthrow(this, `arrow_direction: unknown arrow type ${arrow}`);
56
+ throw new JssmError(undefined, `arrow_direction: unknown arrow type ${arrow}`);
59
57
  }
60
58
  }
61
59
  /* eslint-enable complexity */
@@ -97,7 +95,7 @@ function arrow_left_kind(arrow) {
97
95
  case '↚⇒':
98
96
  return 'forced';
99
97
  default:
100
- xthrow(this, `arrow_direction: unknown arrow type ${arrow}`);
98
+ throw new JssmError(undefined, `arrow_direction: unknown arrow type ${arrow}`);
101
99
  }
102
100
  }
103
101
  /* eslint-enable complexity */
@@ -139,7 +137,7 @@ function arrow_right_kind(arrow) {
139
137
  case '⇐↛':
140
138
  return 'forced';
141
139
  default:
142
- xthrow(this, `arrow_direction: unknown arrow type ${arrow}`);
140
+ throw new JssmError(undefined, `arrow_direction: unknown arrow type ${arrow}`);
143
141
  }
144
142
  }
145
143
  /* eslint-enable complexity */
@@ -151,13 +149,13 @@ function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
151
149
  forced_only: kind === 'forced',
152
150
  main_path: kind === 'main'
153
151
  };
154
- // if ((wasList !== undefined) && (wasIndex === undefined)) { xthrow(this, `Must have an index if transition was in a list"); }
155
- // if ((wasIndex !== undefined) && (wasList === undefined)) { xthrow(this, `Must be in a list if transition has an index"); }
152
+ // if ((wasList !== undefined) && (wasIndex === undefined)) { throw new JssmError(undefined, `Must have an index if transition was in a list"); }
153
+ // if ((wasIndex !== undefined) && (wasList === undefined)) { throw new JssmError(undefined, `Must be in a list if transition has an index"); }
156
154
  /*
157
155
  if (typeof edge.to === 'object') {
158
156
 
159
157
  if (edge.to.key === 'cycle') {
160
- if (wasList === undefined) { xthrow(this, "Must have a waslist if a to is type cycle"); }
158
+ if (wasList === undefined) { throw new JssmError(undefined, "Must have a waslist if a to is type cycle"); }
161
159
  const nextIndex = wrapBy(wasIndex, edge.to.value, wasList.length);
162
160
  edge.to = wasList[nextIndex];
163
161
  }
@@ -211,7 +209,7 @@ function compile_rule_handler(rule) {
211
209
  }
212
210
  if (rule.key === 'state_declaration') {
213
211
  if (!rule.name) {
214
- xthrow(this, 'State declarations must have a name');
212
+ throw new JssmError(undefined, 'State declarations must have a name');
215
213
  }
216
214
  return { agg_as: 'state_declaration', val: { state: rule.name, declarations: rule.value } };
217
215
  }
@@ -228,7 +226,7 @@ function compile_rule_handler(rule) {
228
226
  if (tautologies.includes(rule.key)) {
229
227
  return { agg_as: rule.key, val: rule.value };
230
228
  }
231
- xthrow(this, `compile_rule_handler: Unknown rule: ${JSON.stringify(rule)}`);
229
+ throw new JssmError(undefined, `compile_rule_handler: Unknown rule: ${JSON.stringify(rule)}`);
232
230
  }
233
231
  function compile(tree) {
234
232
  const results = {
@@ -271,7 +269,7 @@ function compile(tree) {
271
269
  ];
272
270
  oneOnlyKeys.map((oneOnlyKey) => {
273
271
  if (results[oneOnlyKey].length > 1) {
274
- xthrow(this, `May only have one ${oneOnlyKey} statement maximum: ${JSON.stringify(results[oneOnlyKey])}`);
272
+ throw new JssmError(undefined, `May only have one ${oneOnlyKey} statement maximum: ${JSON.stringify(results[oneOnlyKey])}`);
275
273
  }
276
274
  else {
277
275
  if (results[oneOnlyKey].length) {
@@ -314,7 +312,7 @@ function transfer_state_properties(state_decl) {
314
312
  case 'border-color':
315
313
  state_decl.borderColor = d.value;
316
314
  break;
317
- default: xthrow(this, `Unknown state property: '${JSON.stringify(d)}'`);
315
+ default: throw new JssmError(undefined, `Unknown state property: '${JSON.stringify(d)}'`);
318
316
  }
319
317
  });
320
318
  return state_decl;
@@ -371,17 +369,17 @@ class Machine {
371
369
  if (state_declaration) {
372
370
  state_declaration.map((state_decl) => {
373
371
  if (this._state_declarations.has(state_decl.state)) { // no repeats
374
- xthrow(this, `Added the same state declaration twice: ${JSON.stringify(state_decl.state)}`);
372
+ throw new JssmError(this, `Added the same state declaration twice: ${JSON.stringify(state_decl.state)}`);
375
373
  }
376
374
  this._state_declarations.set(state_decl.state, transfer_state_properties(state_decl));
377
375
  });
378
376
  }
379
377
  transitions.map((tr) => {
380
378
  if (tr.from === undefined) {
381
- xthrow(this, `transition must define 'from': ${JSON.stringify(tr)}`);
379
+ throw new JssmError(this, `transition must define 'from': ${JSON.stringify(tr)}`);
382
380
  }
383
381
  if (tr.to === undefined) {
384
- xthrow(this, `transition must define 'to': ${JSON.stringify(tr)}`);
382
+ throw new JssmError(this, `transition must define 'to': ${JSON.stringify(tr)}`);
385
383
  }
386
384
  // get the cursors. what a mess
387
385
  const cursor_from = this._states.get(tr.from)
@@ -396,7 +394,7 @@ class Machine {
396
394
  }
397
395
  // guard against existing connections being re-added
398
396
  if (cursor_from.to.includes(tr.to)) {
399
- xthrow(this, `already has ${JSON.stringify(tr.from)} to ${JSON.stringify(tr.to)}`);
397
+ throw new JssmError(this, `already has ${JSON.stringify(tr.from)} to ${JSON.stringify(tr.to)}`);
400
398
  }
401
399
  else {
402
400
  cursor_from.to.push(tr.to);
@@ -408,7 +406,7 @@ class Machine {
408
406
  // guard against repeating a transition name
409
407
  if (tr.name) {
410
408
  if (this._named_transitions.has(tr.name)) {
411
- xthrow(this, `named transition "${JSON.stringify(tr.name)}" already created`);
409
+ throw new JssmError(this, `named transition "${JSON.stringify(tr.name)}" already created`);
412
410
  }
413
411
  else {
414
412
  this._named_transitions.set(tr.name, thisEdgeId);
@@ -430,7 +428,7 @@ class Machine {
430
428
  this._actions.set(tr.action, actionMap);
431
429
  }
432
430
  if (actionMap.has(tr.from)) {
433
- xthrow(this, `action ${JSON.stringify(tr.action)} already attached to origin ${JSON.stringify(tr.from)}`);
431
+ throw new JssmError(this, `action ${JSON.stringify(tr.action)} already attached to origin ${JSON.stringify(tr.from)}`);
434
432
  }
435
433
  else {
436
434
  actionMap.set(tr.from, thisEdgeId);
@@ -453,12 +451,12 @@ class Machine {
453
451
  const roActionMap = this._reverse_action_targets.get(tr.to); // wasteful - already did has - refactor
454
452
  if (roActionMap) {
455
453
  if (roActionMap.has(tr.action)) {
456
- xthrow(this, `ro-action ${tr.to} already attached to action ${tr.action}`);
454
+ throw new JssmError(this, `ro-action ${tr.to} already attached to action ${tr.action}`);
457
455
  } else {
458
456
  roActionMap.set(tr.action, thisEdgeId);
459
457
  }
460
458
  } else {
461
- xthrow(this, `should be impossible - flow doesn\'t know .set precedes .get yet again. severe error?');
459
+ throw new JssmError(this, `should be impossible - flow doesn\'t know .set precedes .get yet again. severe error?');
462
460
  }
463
461
  */
464
462
  }
@@ -466,7 +464,7 @@ class Machine {
466
464
  }
467
465
  _new_state(state_config) {
468
466
  if (this._states.has(state_config.name)) {
469
- xthrow(this, `state ${JSON.stringify(state_config.name)} already exists`);
467
+ throw new JssmError(this, `state ${JSON.stringify(state_config.name)} already exists`);
470
468
  }
471
469
  this._states.set(state_config.name, state_config);
472
470
  return state_config.name;
@@ -538,7 +536,7 @@ class Machine {
538
536
  edges: this._edges,
539
537
  named_transitions: this._named_transitions,
540
538
  reverse_actions: this._reverse_actions,
541
- // reverse_action_targets : this._reverse_action_targets,
539
+ // reverse_action_targets : this._reverse_action_targets,
542
540
  state: this._state,
543
541
  states: this._states
544
542
  };
@@ -557,7 +555,7 @@ class Machine {
557
555
  return state;
558
556
  }
559
557
  else {
560
- xthrow(this, `no such state ${JSON.stringify(state)}`);
558
+ throw new JssmError(this, 'No such state', { requested_state: whichState });
561
559
  }
562
560
  }
563
561
  has_state(whichState) {
@@ -607,7 +605,7 @@ class Machine {
607
605
  probable_exits_for(whichState) {
608
606
  const wstate = this._states.get(whichState);
609
607
  if (!(wstate)) {
610
- xthrow(this, `No such state ${JSON.stringify(whichState)} in probable_exits_for`);
608
+ throw new JssmError(this, `No such state ${JSON.stringify(whichState)} in probable_exits_for`);
611
609
  }
612
610
  const wstate_to = wstate.to, wtf = wstate_to
613
611
  .map((ws) => this.lookup_transition_for(this.state(), ws))
@@ -636,7 +634,7 @@ class Machine {
636
634
  return Array.from(wstate.keys());
637
635
  }
638
636
  else {
639
- xthrow(this, `No such state ${JSON.stringify(whichState)}`);
637
+ throw new JssmError(this, `No such state ${JSON.stringify(whichState)}`);
640
638
  }
641
639
  }
642
640
  list_states_having_action(whichState) {
@@ -645,7 +643,7 @@ class Machine {
645
643
  return Array.from(wstate.keys());
646
644
  }
647
645
  else {
648
- xthrow(this, `No such state ${JSON.stringify(whichState)}`);
646
+ throw new JssmError(this, `No such state ${JSON.stringify(whichState)}`);
649
647
  }
650
648
  }
651
649
  // comeback
@@ -660,7 +658,7 @@ class Machine {
660
658
  list_exit_actions(whichState = this.state()) {
661
659
  const ra_base = this._reverse_actions.get(whichState);
662
660
  if (!(ra_base)) {
663
- xthrow(this, `No such state ${JSON.stringify(whichState)}`);
661
+ throw new JssmError(this, `No such state ${JSON.stringify(whichState)}`);
664
662
  }
665
663
  return Array.from(ra_base.values())
666
664
  .map((edgeId) => this._edges[edgeId])
@@ -670,7 +668,7 @@ class Machine {
670
668
  probable_action_exits(whichState = this.state()) {
671
669
  const ra_base = this._reverse_actions.get(whichState);
672
670
  if (!(ra_base)) {
673
- xthrow(this, `No such state ${JSON.stringify(whichState)}`);
671
+ throw new JssmError(this, `No such state ${JSON.stringify(whichState)}`);
674
672
  }
675
673
  return Array.from(ra_base.values())
676
674
  .map((edgeId) => this._edges[edgeId])
@@ -683,7 +681,7 @@ class Machine {
683
681
  // TODO FIXME test that is_unenterable on non-state throws
684
682
  is_unenterable(whichState) {
685
683
  if (!(this.has_state(whichState))) {
686
- xthrow(this, `No such state ${whichState}`);
684
+ throw new JssmError(this, `No such state ${whichState}`);
687
685
  }
688
686
  return this.list_entrances(whichState).length === 0;
689
687
  }
@@ -696,7 +694,7 @@ class Machine {
696
694
  // TODO FIXME test that state_is_terminal on non-state throws
697
695
  state_is_terminal(whichState) {
698
696
  if (!(this.has_state(whichState))) {
699
- xthrow(this, `No such state ${whichState}`);
697
+ throw new JssmError(this, `No such state ${whichState}`);
700
698
  }
701
699
  return this.list_exits(whichState).length === 0;
702
700
  }
@@ -712,7 +710,7 @@ class Machine {
712
710
  return wstate.complete;
713
711
  }
714
712
  else {
715
- xthrow(this, `No such state ${JSON.stringify(whichState)}`);
713
+ throw new JssmError(this, `No such state ${JSON.stringify(whichState)}`);
716
714
  }
717
715
  }
718
716
  has_completes() {
@@ -771,7 +769,7 @@ class Machine {
771
769
  this._has_exit_hooks = true;
772
770
  break;
773
771
  default:
774
- xthrow(this, `Unknown hook type ${HookDesc.kind}, should be impossible`);
772
+ throw new JssmError(this, `Unknown hook type ${HookDesc.kind}, should be impossible`);
775
773
  }
776
774
  }
777
775
  hook(from, to, handler) {
@@ -825,7 +823,7 @@ class Machine {
825
823
  return this;
826
824
  }
827
825
  // remove_hook(HookDesc: HookDescription) {
828
- // xthrow(this, 'TODO: Should remove hook here');
826
+ // throw new JssmError(this, 'TODO: Should remove hook here');
829
827
  // }
830
828
  edges_between(from, to) {
831
829
  return this._edges.filter(edge => ((edge.from === from) && (edge.to === to)));
@@ -985,7 +983,7 @@ class Machine {
985
983
  current_action_edge_for(action) {
986
984
  const idx = this.current_action_for(action);
987
985
  if ((idx === undefined) || (idx === null)) {
988
- xthrow(this, `No such action ${JSON.stringify(action)}`);
986
+ throw new JssmError(this, `No such action ${JSON.stringify(action)}`);
989
987
  }
990
988
  return this._edges[idx];
991
989
  }
@@ -0,0 +1,8 @@
1
+ import { JssmErrorExtendedInfo } from './jssm_types';
2
+ declare class JssmError extends Error {
3
+ message: string;
4
+ base_message: string;
5
+ requested_state: string | undefined;
6
+ constructor(machine: any, message: string, JEEI?: JssmErrorExtendedInfo);
7
+ }
8
+ export { JssmError };
@@ -0,0 +1,28 @@
1
+ class JssmError extends Error {
2
+ constructor(machine, message, JEEI) {
3
+ const { requested_state } = (JEEI === undefined)
4
+ ? { requested_state: undefined }
5
+ : JEEI;
6
+ const follow_ups = [];
7
+ if (machine) {
8
+ if (machine.state() !== undefined) {
9
+ follow_ups.push(`at "${machine.state()}"`);
10
+ }
11
+ }
12
+ if (requested_state !== undefined) {
13
+ follow_ups.push(`requested "${requested_state}"`);
14
+ }
15
+ const complex_msg = `${((machine === null || machine === void 0 ? void 0 : machine.instance_name()) !== undefined)
16
+ ? `[[${machine.instance_name()}]]: `
17
+ : ''}${message}${follow_ups.length
18
+ ? ` (${follow_ups.join(', ')})`
19
+ : ''}`;
20
+ super(complex_msg);
21
+ this.name = 'JssmError';
22
+ this.message = complex_msg;
23
+ this.base_message = message;
24
+ this.requested_state = requested_state;
25
+ }
26
+ }
27
+ ;
28
+ export { JssmError };
@@ -199,4 +199,7 @@ declare type ExitHook = {
199
199
  handler: HookHandler;
200
200
  };
201
201
  declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | GlobalActionHook | AnyActionHook | StandardTransitionHook | MainTransitionHook | ForcedTransitionHook | AnyTransitionHook | EntryHook | ExitHook;
202
- export { JssmColor, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmLayout, JssmParseFunctionType, JssmMachineInternalState, FslDirection, FslTheme, HookDescription, HookHandler };
202
+ declare type JssmErrorExtendedInfo = {
203
+ requested_state?: StateType | undefined;
204
+ };
205
+ export { JssmColor, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmLayout, JssmParseFunctionType, JssmMachineInternalState, JssmErrorExtendedInfo, FslDirection, FslTheme, HookDescription, HookHandler };
@@ -1,2 +1,2 @@
1
- const version = "5.62.0";
1
+ const version = "5.63.0";
2
2
  export { version };