jssm 5.85.6 → 5.85.9

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,7 +1,11 @@
1
1
  // whargarbl lots of these return arrays could/should be sets
2
- import { reduce as reduce_to_639 } from 'reduce-to-639-1';
3
2
  import { circular_buffer } from 'circular_buffer_js';
4
3
  import { FslDirections } from './jssm_types';
4
+ import { arrow_direction, arrow_left_kind, arrow_right_kind } from './jssm_arrow';
5
+ import { compile, make, wrap_parse } from './jssm_compiler';
6
+ // compile_rule_handler,
7
+ // compile_rule_transition_step,
8
+ // compile_rule_handle_transition,
5
9
  import { base_theme } from './themes/jssm_base_stylesheet';
6
10
  import { default_theme } from './themes/jssm_theme_default';
7
11
  import { modern_theme } from './themes/jssm_theme_modern';
@@ -17,547 +21,8 @@ theme_mapping.set('bold', bold_theme);
17
21
  import { seq, unique, find_repeated, weighted_rand_select, weighted_sample_select, histograph, weighted_histo_key, array_box_if_string, name_bind_prop_and_state, hook_name, named_hook_name } from './jssm_util';
18
22
  import * as constants from './jssm_constants';
19
23
  const { shapes, gviz_shapes, named_colors } = constants;
20
- import { parse } from './fsl_parser';
21
24
  import { version, build_time } from './version'; // replaced from package.js in build
22
25
  import { JssmError } from './jssm_error';
23
- /* eslint-disable complexity */
24
- /*********
25
- *
26
- * Return the direction of an arrow - `right`, `left`, or `both`.
27
- *
28
- * ```typescript
29
- * import { arrow_direction } from 'jssm';
30
- *
31
- * arrow_direction('->'); // 'right'
32
- * arrow_direction('<~=>'); // 'both'
33
- * ```
34
- *
35
- * @param arrow The arrow to be evaluated
36
- *
37
- */
38
- function arrow_direction(arrow) {
39
- switch (String(arrow)) {
40
- case '->':
41
- case '→':
42
- case '=>':
43
- case '⇒':
44
- case '~>':
45
- case '↛':
46
- return 'right';
47
- case '<-':
48
- case '←':
49
- case '<=':
50
- case '⇐':
51
- case '<~':
52
- case '↚':
53
- return 'left';
54
- case '<->':
55
- case '↔':
56
- case '<-=>':
57
- case '←⇒':
58
- case '←=>':
59
- case '<-⇒':
60
- case '<-~>':
61
- case '←↛':
62
- case '←~>':
63
- case '<-↛':
64
- case '<=>':
65
- case '⇔':
66
- case '<=->':
67
- case '⇐→':
68
- case '⇐->':
69
- case '<=→':
70
- case '<=~>':
71
- case '⇐↛':
72
- case '⇐~>':
73
- case '<=↛':
74
- case '<~>':
75
- case '↮':
76
- case '<~->':
77
- case '↚→':
78
- case '↚->':
79
- case '<~→':
80
- case '<~=>':
81
- case '↚⇒':
82
- case '↚=>':
83
- case '<~⇒':
84
- return 'both';
85
- default:
86
- throw new JssmError(undefined, `arrow_direction: unknown arrow type ${arrow}`);
87
- }
88
- }
89
- /* eslint-enable complexity */
90
- /* eslint-disable complexity */
91
- /*********
92
- *
93
- * Return the direction of an arrow - `right`, `left`, or `both`.
94
- *
95
- * ```typescript
96
- * import { arrow_left_kind } from 'jssm';
97
- *
98
- * arrow_left_kind('<-'); // 'legal'
99
- * arrow_left_kind('<='); // 'main'
100
- * arrow_left_kind('<~'); // 'forced'
101
- * arrow_left_kind('<->'); // 'legal'
102
- * arrow_left_kind('->'); // 'none'
103
- * ```
104
- *
105
- * @param arrow The arrow to be evaluated
106
- *
107
- */
108
- function arrow_left_kind(arrow) {
109
- switch (String(arrow)) {
110
- case '->':
111
- case '→':
112
- case '=>':
113
- case '⇒':
114
- case '~>':
115
- case '↛':
116
- return 'none';
117
- case '<-':
118
- case '←':
119
- case '<->':
120
- case '↔':
121
- case '<-=>':
122
- case '←⇒':
123
- case '<-~>':
124
- case '←↛':
125
- return 'legal';
126
- case '<=':
127
- case '⇐':
128
- case '<=>':
129
- case '⇔':
130
- case '<=->':
131
- case '⇐→':
132
- case '<=~>':
133
- case '⇐↛':
134
- return 'main';
135
- case '<~':
136
- case '↚':
137
- case '<~>':
138
- case '↮':
139
- case '<~->':
140
- case '↚→':
141
- case '<~=>':
142
- case '↚⇒':
143
- return 'forced';
144
- default:
145
- throw new JssmError(undefined, `arrow_direction: unknown arrow type ${arrow}`);
146
- }
147
- }
148
- /* eslint-enable complexity */
149
- /* eslint-disable complexity */
150
- /*********
151
- *
152
- * Return the direction of an arrow - `right`, `left`, or `both`.
153
- *
154
- * ```typescript
155
- * import { arrow_left_kind } from 'jssm';
156
- *
157
- * arrow_left_kind('->'); // 'legal'
158
- * arrow_left_kind('=>'); // 'main'
159
- * arrow_left_kind('~>'); // 'forced'
160
- * arrow_left_kind('<->'); // 'legal'
161
- * arrow_left_kind('<-'); // 'none'
162
- * ```
163
- *
164
- * @param arrow The arrow to be evaluated
165
- *
166
- */
167
- function arrow_right_kind(arrow) {
168
- switch (String(arrow)) {
169
- case '<-':
170
- case '←':
171
- case '<=':
172
- case '⇐':
173
- case '<~':
174
- case '↚':
175
- return 'none';
176
- case '->':
177
- case '→':
178
- case '<->':
179
- case '↔':
180
- case '<=->':
181
- case '⇐→':
182
- case '<~->':
183
- case '↚→':
184
- return 'legal';
185
- case '=>':
186
- case '⇒':
187
- case '<=>':
188
- case '⇔':
189
- case '<-=>':
190
- case '←⇒':
191
- case '<~=>':
192
- case '↚⇒':
193
- return 'main';
194
- case '~>':
195
- case '↛':
196
- case '<~>':
197
- case '↮':
198
- case '<-~>':
199
- case '←↛':
200
- case '<=~>':
201
- case '⇐↛':
202
- return 'forced';
203
- default:
204
- throw new JssmError(undefined, `arrow_direction: unknown arrow type ${arrow}`);
205
- }
206
- }
207
- /* eslint-enable complexity */
208
- /*********
209
- *
210
- * Internal method meant to perform factory assembly of an edge. Not meant for
211
- * external use.
212
- *
213
- * @internal
214
- *
215
- * @typeparam mDT The type of the machine data member; usually omitted
216
- *
217
- */
218
- // TODO add at-param to docblock
219
- function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
220
- const kind = isRight
221
- ? arrow_right_kind(this_se.kind)
222
- : arrow_left_kind(this_se.kind), edge = {
223
- from,
224
- to,
225
- kind,
226
- forced_only: kind === 'forced',
227
- main_path: kind === 'main'
228
- };
229
- // if ((wasList !== undefined) && (wasIndex === undefined)) { throw new JssmError(undefined, `Must have an index if transition was in a list"); }
230
- // if ((wasIndex !== undefined) && (wasList === undefined)) { throw new JssmError(undefined, `Must be in a list if transition has an index"); }
231
- /*
232
- if (typeof edge.to === 'object') {
233
-
234
- if (edge.to.key === 'cycle') {
235
- if (wasList === undefined) { throw new JssmError(undefined, "Must have a waslist if a to is type cycle"); }
236
- const nextIndex = wrapBy(wasIndex, edge.to.value, wasList.length);
237
- edge.to = wasList[nextIndex];
238
- }
239
-
240
- }
241
- */
242
- const action = isRight ? 'r_action' : 'l_action', probability = isRight ? 'r_probability' : 'l_probability';
243
- if (this_se[action]) {
244
- edge.action = this_se[action];
245
- }
246
- if (this_se[probability]) {
247
- edge.probability = this_se[probability];
248
- }
249
- return edge;
250
- }
251
- /*********
252
- *
253
- * This method wraps the parser call that comes from the peg grammar,
254
- * {@link parse}. Generally neither this nor that should be used directly
255
- * unless you mean to develop plugins or extensions for the machine.
256
- *
257
- * Parses the intermediate representation of a compiled string down to a
258
- * machine configuration object. If you're using this (probably don't,) you're
259
- * probably also using {@link compile} and {@link Machine.constructor}.
260
- *
261
- * ```typescript
262
- * import { parse, compile, Machine } from 'jssm';
263
- *
264
- * const intermediate = wrap_parse('a -> b;', {});
265
- * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
266
- *
267
- * const cfg = compile(intermediate);
268
- * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
269
- *
270
- * const machine = new Machine(cfg);
271
- * // Machine { _instance_name: undefined, _state: 'a', ...
272
- * ```
273
- *
274
- * This method is mostly for plugin and intermediate tool authors, or people
275
- * who need to work with the machine's intermediate representation.
276
- *
277
- * # Hey!
278
- *
279
- * Most people looking at this want either the `sm` operator or method `from`,
280
- * which perform all the steps in the chain. The library's author mostly uses
281
- * operator `sm`, and mostly falls back to `.from` when needing to parse
282
- * strings dynamically instead of from template literals.
283
- *
284
- * Operator {@link sm}:
285
- *
286
- * ```typescript
287
- * import { sm } from 'jssm';
288
- *
289
- * const lswitch = sm`on <=> off;`;
290
- * ```
291
- *
292
- * Method {@link from}:
293
- *
294
- * ```typescript
295
- * import * as jssm from 'jssm';
296
- *
297
- * const toggle = jssm.from('up <=> down;');
298
- * ```
299
- *
300
- * `wrap_parse` itself is an internal convenience method for alting out an
301
- * object as the options call. Not generally meant for external use.
302
- *
303
- * @param input The FSL code to be evaluated
304
- *
305
- * @param options Things to control about the instance
306
- *
307
- */
308
- function wrap_parse(input, options) {
309
- return parse(input, options || {});
310
- }
311
- /*********
312
- *
313
- * Internal method performing one step in compiling rules for transitions. Not
314
- * generally meant for external use.
315
- *
316
- * @internal
317
- *
318
- * @typeparam mDT The type of the machine data member; usually omitted
319
- *
320
- */
321
- function compile_rule_transition_step(acc, from, to, this_se, next_se) {
322
- const edges = [];
323
- const uFrom = (Array.isArray(from) ? from : [from]), uTo = (Array.isArray(to) ? to : [to]);
324
- uFrom.map((f) => {
325
- uTo.map((t) => {
326
- const right = makeTransition(this_se, f, t, true);
327
- if (right.kind !== 'none') {
328
- edges.push(right);
329
- }
330
- const left = makeTransition(this_se, t, f, false);
331
- if (left.kind !== 'none') {
332
- edges.push(left);
333
- }
334
- });
335
- });
336
- const new_acc = acc.concat(edges);
337
- if (next_se) {
338
- return compile_rule_transition_step(new_acc, to, next_se.to, next_se, next_se.se);
339
- }
340
- else {
341
- return new_acc;
342
- }
343
- }
344
- /*********
345
- *
346
- * Internal method performing one step in compiling rules for transitions. Not
347
- * generally meant for external use.
348
- *
349
- * @internal
350
- *
351
- */
352
- function compile_rule_handle_transition(rule) {
353
- return compile_rule_transition_step([], rule.from, rule.se.to, rule.se, rule.se.se);
354
- }
355
- /*********
356
- *
357
- * Internal method performing one step in compiling rules for transitions. Not
358
- * generally meant for external use.
359
- *
360
- * @internal
361
- *
362
- */
363
- function compile_rule_handler(rule) {
364
- if (rule.key === 'transition') {
365
- return { agg_as: 'transition', val: compile_rule_handle_transition(rule) };
366
- }
367
- if (rule.key === 'machine_language') {
368
- return { agg_as: 'machine_language', val: reduce_to_639(rule.value) };
369
- }
370
- // manually rehandled to make `undefined` as a property safe
371
- if (rule.key === 'property_definition') {
372
- const ret = { agg_as: 'property_definition', val: { name: rule.name } };
373
- if (rule.hasOwnProperty('default_value')) {
374
- ret.val.default_value = rule.default_value;
375
- }
376
- if (rule.hasOwnProperty('required')) {
377
- ret.val.required = rule.required;
378
- }
379
- return ret;
380
- }
381
- // state properties are in here
382
- if (rule.key === 'state_declaration') {
383
- if (!rule.name) {
384
- throw new JssmError(undefined, 'State declarations must have a name');
385
- }
386
- return { agg_as: 'state_declaration', val: { state: rule.name, declarations: rule.value } };
387
- }
388
- if (['arrange_declaration', 'arrange_start_declaration',
389
- 'arrange_end_declaration'].includes(rule.key)) {
390
- return { agg_as: rule.key, val: [rule.value] };
391
- }
392
- // things that can only exist once and are just a value under their own name
393
- const tautologies = [
394
- 'graph_layout', 'start_states', 'end_states', 'machine_name', 'machine_version',
395
- 'machine_comment', 'machine_author', 'machine_contributor', 'machine_definition',
396
- 'machine_reference', 'machine_license', 'fsl_version', 'state_config', 'theme',
397
- 'flow', 'dot_preamble', 'default_state_config', 'default_start_state_config',
398
- 'default_end_state_config', 'default_hooked_state_config',
399
- 'default_active_state_config', 'default_terminal_state_config'
400
- ];
401
- if (tautologies.includes(rule.key)) {
402
- return { agg_as: rule.key, val: rule.value };
403
- }
404
- throw new JssmError(undefined, `compile_rule_handler: Unknown rule: ${JSON.stringify(rule)}`);
405
- }
406
- /*********
407
- *
408
- * Compile a machine's JSON intermediate representation to a config object. If
409
- * you're using this (probably don't,) you're probably also using
410
- * {@link parse} to get the IR, and the object constructor
411
- * {@link Machine.construct} to turn the config object into a workable machine.
412
- *
413
- * ```typescript
414
- * import { parse, compile, Machine } from 'jssm';
415
- *
416
- * const intermediate = parse('a -> b;');
417
- * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
418
- *
419
- * const cfg = compile(intermediate);
420
- * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
421
- *
422
- * const machine = new Machine(cfg);
423
- * // Machine { _instance_name: undefined, _state: 'a', ...
424
- * ```
425
- *
426
- * This method is mostly for plugin and intermediate tool authors, or people
427
- * who need to work with the machine's intermediate representation.
428
- *
429
- * # Hey!
430
- *
431
- * Most people looking at this want either the `sm` operator or method `from`,
432
- * which perform all the steps in the chain. The library's author mostly uses
433
- * operator `sm`, and mostly falls back to `.from` when needing to parse
434
- * strings dynamically instead of from template literals.
435
- *
436
- * Operator {@link sm}:
437
- *
438
- * ```typescript
439
- * import { sm } from 'jssm';
440
- *
441
- * const lswitch = sm`on <=> off;`;
442
- * ```
443
- *
444
- * Method {@link from}:
445
- *
446
- * ```typescript
447
- * import * as jssm from 'jssm';
448
- *
449
- * const toggle = jssm.from('up <=> down;');
450
- * ```
451
- *
452
- * @typeparam mDT The type of the machine data member; usually omitted
453
- *
454
- * @param tree The parse tree to be boiled down into a machine config
455
- *
456
- */
457
- function compile(tree) {
458
- const results = {
459
- graph_layout: [],
460
- transition: [],
461
- start_states: [],
462
- end_states: [],
463
- state_config: [],
464
- state_declaration: [],
465
- fsl_version: [],
466
- machine_author: [],
467
- machine_comment: [],
468
- machine_contributor: [],
469
- machine_definition: [],
470
- machine_language: [],
471
- machine_license: [],
472
- machine_name: [],
473
- machine_reference: [],
474
- property_definition: [],
475
- state_property: {},
476
- theme: [],
477
- flow: [],
478
- dot_preamble: [],
479
- arrange_declaration: [],
480
- arrange_start_declaration: [],
481
- arrange_end_declaration: [],
482
- machine_version: [],
483
- default_state_config: [],
484
- default_active_state_config: [],
485
- default_hooked_state_config: [],
486
- default_terminal_state_config: [],
487
- default_start_state_config: [],
488
- default_end_state_config: [],
489
- };
490
- tree.map((tr) => {
491
- const rule = compile_rule_handler(tr), agg_as = rule.agg_as, val = rule.val; // TODO FIXME no any
492
- results[agg_as] = results[agg_as].concat(val);
493
- });
494
- const property_keys = results['property_definition'].map(pd => pd.name), repeat_props = find_repeated(property_keys);
495
- if (repeat_props.length) {
496
- throw new JssmError(undefined, `Cannot repeat property definitions. Saw ${JSON.stringify(repeat_props)}`);
497
- }
498
- const assembled_transitions = [].concat(...results['transition']);
499
- const result_cfg = {
500
- start_states: results.start_states.length ? results.start_states : [assembled_transitions[0].from],
501
- end_states: results.end_states,
502
- transitions: assembled_transitions,
503
- state_property: [],
504
- };
505
- const oneOnlyKeys = [
506
- 'graph_layout', 'machine_name', 'machine_version', 'machine_comment',
507
- 'fsl_version', 'machine_license', 'machine_definition', 'machine_language',
508
- 'flow', 'dot_preamble'
509
- ];
510
- oneOnlyKeys.map((oneOnlyKey) => {
511
- if (results[oneOnlyKey].length > 1) {
512
- throw new JssmError(undefined, `May only have one ${oneOnlyKey} statement maximum: ${JSON.stringify(results[oneOnlyKey])}`);
513
- }
514
- else {
515
- if (results[oneOnlyKey].length) {
516
- result_cfg[oneOnlyKey] = results[oneOnlyKey][0];
517
- }
518
- }
519
- });
520
- ['arrange_declaration', 'arrange_start_declaration', 'arrange_end_declaration',
521
- 'machine_author', 'machine_contributor', 'machine_reference', 'theme',
522
- 'state_declaration', 'property_definition', 'default_state_config',
523
- 'default_start_state_config', 'default_end_state_config',
524
- 'default_hooked_state_config', 'default_terminal_state_config',
525
- 'default_active_state_config'].map((multiKey) => {
526
- if (results[multiKey].length) {
527
- result_cfg[multiKey] = results[multiKey];
528
- }
529
- });
530
- // re-walk state declarations, already wrapped up, to get state properties,
531
- // which go out in a different datastructure
532
- results.state_declaration.forEach(sd => {
533
- sd.declarations.forEach(decl => {
534
- if (decl.key === 'state_property') {
535
- const label = name_bind_prop_and_state(decl.name, sd.state);
536
- if (result_cfg.state_property.findIndex(c => c.name === label) !== -1) {
537
- throw new JssmError(undefined, `A state may only bind a property once (${sd.state} re-binds ${decl.name})`);
538
- }
539
- else {
540
- result_cfg.state_property.push({ name: label, default_value: decl.value });
541
- }
542
- }
543
- });
544
- });
545
- return result_cfg;
546
- }
547
- /*********
548
- *
549
- * An internal convenience wrapper for parsing then compiling a machine string.
550
- * Not generally meant for external use. Please see {@link compile} or
551
- * {@link sm}.
552
- *
553
- * @typeparam mDT The type of the machine data member; usually omitted
554
- *
555
- * @param plan The FSL code to be evaluated and built into a machine config
556
- *
557
- */
558
- function make(plan) {
559
- return compile(wrap_parse(plan));
560
- }
561
26
  /*********
562
27
  *
563
28
  * An internal method meant to take a series of declarations and fold them into
@@ -0,0 +1,53 @@
1
+ import { JssmArrow, JssmArrowDirection, JssmArrowKind } from './jssm_types';
2
+ /*********
3
+ *
4
+ * Return the direction of an arrow - `right`, `left`, or `both`.
5
+ *
6
+ * ```typescript
7
+ * import { arrow_direction } from 'jssm';
8
+ *
9
+ * arrow_direction('->'); // 'right'
10
+ * arrow_direction('<~=>'); // 'both'
11
+ * ```
12
+ *
13
+ * @param arrow The arrow to be evaluated
14
+ *
15
+ */
16
+ declare function arrow_direction(arrow: JssmArrow): JssmArrowDirection;
17
+ /*********
18
+ *
19
+ * Return the direction of an arrow - `right`, `left`, or `both`.
20
+ *
21
+ * ```typescript
22
+ * import { arrow_left_kind } from 'jssm';
23
+ *
24
+ * arrow_left_kind('<-'); // 'legal'
25
+ * arrow_left_kind('<='); // 'main'
26
+ * arrow_left_kind('<~'); // 'forced'
27
+ * arrow_left_kind('<->'); // 'legal'
28
+ * arrow_left_kind('->'); // 'none'
29
+ * ```
30
+ *
31
+ * @param arrow The arrow to be evaluated
32
+ *
33
+ */
34
+ declare function arrow_left_kind(arrow: JssmArrow): JssmArrowKind;
35
+ /*********
36
+ *
37
+ * Return the direction of an arrow - `right`, `left`, or `both`.
38
+ *
39
+ * ```typescript
40
+ * import { arrow_left_kind } from 'jssm';
41
+ *
42
+ * arrow_left_kind('->'); // 'legal'
43
+ * arrow_left_kind('=>'); // 'main'
44
+ * arrow_left_kind('~>'); // 'forced'
45
+ * arrow_left_kind('<->'); // 'legal'
46
+ * arrow_left_kind('<-'); // 'none'
47
+ * ```
48
+ *
49
+ * @param arrow The arrow to be evaluated
50
+ *
51
+ */
52
+ declare function arrow_right_kind(arrow: JssmArrow): JssmArrowKind;
53
+ export { arrow_direction, arrow_left_kind, arrow_right_kind };