jssm 5.85.8 → 5.85.10

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,379 +1,14 @@
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';
5
4
  import { arrow_direction, arrow_left_kind, arrow_right_kind } from './jssm_arrow';
6
- import { base_theme } from './themes/jssm_base_stylesheet';
7
- import { default_theme } from './themes/jssm_theme_default';
8
- import { modern_theme } from './themes/jssm_theme_modern';
9
- import { ocean_theme } from './themes/jssm_theme_ocean';
10
- import { plain_theme } from './themes/jssm_theme_plain';
11
- import { bold_theme } from './themes/jssm_theme_bold';
12
- const theme_mapping = new Map();
13
- theme_mapping.set('default', default_theme);
14
- theme_mapping.set('modern', modern_theme);
15
- theme_mapping.set('ocean', ocean_theme);
16
- theme_mapping.set('plain', plain_theme);
17
- theme_mapping.set('bold', bold_theme);
5
+ import { compile, make, wrap_parse } from './jssm_compiler';
6
+ import { theme_mapping, base_theme } from './jssm_theme';
18
7
  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';
19
8
  import * as constants from './jssm_constants';
20
9
  const { shapes, gviz_shapes, named_colors } = constants;
21
- import { parse } from './fsl_parser';
22
10
  import { version, build_time } from './version'; // replaced from package.js in build
23
11
  import { JssmError } from './jssm_error';
24
- /*********
25
- *
26
- * Internal method meant to perform factory assembly of an edge. Not meant for
27
- * external use.
28
- *
29
- * @internal
30
- *
31
- * @typeparam mDT The type of the machine data member; usually omitted
32
- *
33
- */
34
- // TODO add at-param to docblock
35
- function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
36
- const kind = isRight
37
- ? arrow_right_kind(this_se.kind)
38
- : arrow_left_kind(this_se.kind), edge = {
39
- from,
40
- to,
41
- kind,
42
- forced_only: kind === 'forced',
43
- main_path: kind === 'main'
44
- };
45
- // if ((wasList !== undefined) && (wasIndex === undefined)) { throw new JssmError(undefined, `Must have an index if transition was in a list"); }
46
- // if ((wasIndex !== undefined) && (wasList === undefined)) { throw new JssmError(undefined, `Must be in a list if transition has an index"); }
47
- /*
48
- if (typeof edge.to === 'object') {
49
-
50
- if (edge.to.key === 'cycle') {
51
- if (wasList === undefined) { throw new JssmError(undefined, "Must have a waslist if a to is type cycle"); }
52
- const nextIndex = wrapBy(wasIndex, edge.to.value, wasList.length);
53
- edge.to = wasList[nextIndex];
54
- }
55
-
56
- }
57
- */
58
- const action = isRight ? 'r_action' : 'l_action', probability = isRight ? 'r_probability' : 'l_probability';
59
- if (this_se[action]) {
60
- edge.action = this_se[action];
61
- }
62
- if (this_se[probability]) {
63
- edge.probability = this_se[probability];
64
- }
65
- return edge;
66
- }
67
- /*********
68
- *
69
- * This method wraps the parser call that comes from the peg grammar,
70
- * {@link parse}. Generally neither this nor that should be used directly
71
- * unless you mean to develop plugins or extensions for the machine.
72
- *
73
- * Parses the intermediate representation of a compiled string down to a
74
- * machine configuration object. If you're using this (probably don't,) you're
75
- * probably also using {@link compile} and {@link Machine.constructor}.
76
- *
77
- * ```typescript
78
- * import { parse, compile, Machine } from 'jssm';
79
- *
80
- * const intermediate = wrap_parse('a -> b;', {});
81
- * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
82
- *
83
- * const cfg = compile(intermediate);
84
- * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
85
- *
86
- * const machine = new Machine(cfg);
87
- * // Machine { _instance_name: undefined, _state: 'a', ...
88
- * ```
89
- *
90
- * This method is mostly for plugin and intermediate tool authors, or people
91
- * who need to work with the machine's intermediate representation.
92
- *
93
- * # Hey!
94
- *
95
- * Most people looking at this want either the `sm` operator or method `from`,
96
- * which perform all the steps in the chain. The library's author mostly uses
97
- * operator `sm`, and mostly falls back to `.from` when needing to parse
98
- * strings dynamically instead of from template literals.
99
- *
100
- * Operator {@link sm}:
101
- *
102
- * ```typescript
103
- * import { sm } from 'jssm';
104
- *
105
- * const lswitch = sm`on <=> off;`;
106
- * ```
107
- *
108
- * Method {@link from}:
109
- *
110
- * ```typescript
111
- * import * as jssm from 'jssm';
112
- *
113
- * const toggle = jssm.from('up <=> down;');
114
- * ```
115
- *
116
- * `wrap_parse` itself is an internal convenience method for alting out an
117
- * object as the options call. Not generally meant for external use.
118
- *
119
- * @param input The FSL code to be evaluated
120
- *
121
- * @param options Things to control about the instance
122
- *
123
- */
124
- function wrap_parse(input, options) {
125
- return parse(input, options || {});
126
- }
127
- /*********
128
- *
129
- * Internal method performing one step in compiling rules for transitions. Not
130
- * generally meant for external use.
131
- *
132
- * @internal
133
- *
134
- * @typeparam mDT The type of the machine data member; usually omitted
135
- *
136
- */
137
- function compile_rule_transition_step(acc, from, to, this_se, next_se) {
138
- const edges = [];
139
- const uFrom = (Array.isArray(from) ? from : [from]), uTo = (Array.isArray(to) ? to : [to]);
140
- uFrom.map((f) => {
141
- uTo.map((t) => {
142
- const right = makeTransition(this_se, f, t, true);
143
- if (right.kind !== 'none') {
144
- edges.push(right);
145
- }
146
- const left = makeTransition(this_se, t, f, false);
147
- if (left.kind !== 'none') {
148
- edges.push(left);
149
- }
150
- });
151
- });
152
- const new_acc = acc.concat(edges);
153
- if (next_se) {
154
- return compile_rule_transition_step(new_acc, to, next_se.to, next_se, next_se.se);
155
- }
156
- else {
157
- return new_acc;
158
- }
159
- }
160
- /*********
161
- *
162
- * Internal method performing one step in compiling rules for transitions. Not
163
- * generally meant for external use.
164
- *
165
- * @internal
166
- *
167
- */
168
- function compile_rule_handle_transition(rule) {
169
- return compile_rule_transition_step([], rule.from, rule.se.to, rule.se, rule.se.se);
170
- }
171
- /*********
172
- *
173
- * Internal method performing one step in compiling rules for transitions. Not
174
- * generally meant for external use.
175
- *
176
- * @internal
177
- *
178
- */
179
- function compile_rule_handler(rule) {
180
- if (rule.key === 'transition') {
181
- return { agg_as: 'transition', val: compile_rule_handle_transition(rule) };
182
- }
183
- if (rule.key === 'machine_language') {
184
- return { agg_as: 'machine_language', val: reduce_to_639(rule.value) };
185
- }
186
- // manually rehandled to make `undefined` as a property safe
187
- if (rule.key === 'property_definition') {
188
- const ret = { agg_as: 'property_definition', val: { name: rule.name } };
189
- if (rule.hasOwnProperty('default_value')) {
190
- ret.val.default_value = rule.default_value;
191
- }
192
- if (rule.hasOwnProperty('required')) {
193
- ret.val.required = rule.required;
194
- }
195
- return ret;
196
- }
197
- // state properties are in here
198
- if (rule.key === 'state_declaration') {
199
- if (!rule.name) {
200
- throw new JssmError(undefined, 'State declarations must have a name');
201
- }
202
- return { agg_as: 'state_declaration', val: { state: rule.name, declarations: rule.value } };
203
- }
204
- if (['arrange_declaration', 'arrange_start_declaration',
205
- 'arrange_end_declaration'].includes(rule.key)) {
206
- return { agg_as: rule.key, val: [rule.value] };
207
- }
208
- // things that can only exist once and are just a value under their own name
209
- const tautologies = [
210
- 'graph_layout', 'start_states', 'end_states', 'machine_name', 'machine_version',
211
- 'machine_comment', 'machine_author', 'machine_contributor', 'machine_definition',
212
- 'machine_reference', 'machine_license', 'fsl_version', 'state_config', 'theme',
213
- 'flow', 'dot_preamble', 'default_state_config', 'default_start_state_config',
214
- 'default_end_state_config', 'default_hooked_state_config',
215
- 'default_active_state_config', 'default_terminal_state_config'
216
- ];
217
- if (tautologies.includes(rule.key)) {
218
- return { agg_as: rule.key, val: rule.value };
219
- }
220
- throw new JssmError(undefined, `compile_rule_handler: Unknown rule: ${JSON.stringify(rule)}`);
221
- }
222
- /*********
223
- *
224
- * Compile a machine's JSON intermediate representation to a config object. If
225
- * you're using this (probably don't,) you're probably also using
226
- * {@link parse} to get the IR, and the object constructor
227
- * {@link Machine.construct} to turn the config object into a workable machine.
228
- *
229
- * ```typescript
230
- * import { parse, compile, Machine } from 'jssm';
231
- *
232
- * const intermediate = parse('a -> b;');
233
- * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
234
- *
235
- * const cfg = compile(intermediate);
236
- * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
237
- *
238
- * const machine = new Machine(cfg);
239
- * // Machine { _instance_name: undefined, _state: 'a', ...
240
- * ```
241
- *
242
- * This method is mostly for plugin and intermediate tool authors, or people
243
- * who need to work with the machine's intermediate representation.
244
- *
245
- * # Hey!
246
- *
247
- * Most people looking at this want either the `sm` operator or method `from`,
248
- * which perform all the steps in the chain. The library's author mostly uses
249
- * operator `sm`, and mostly falls back to `.from` when needing to parse
250
- * strings dynamically instead of from template literals.
251
- *
252
- * Operator {@link sm}:
253
- *
254
- * ```typescript
255
- * import { sm } from 'jssm';
256
- *
257
- * const lswitch = sm`on <=> off;`;
258
- * ```
259
- *
260
- * Method {@link from}:
261
- *
262
- * ```typescript
263
- * import * as jssm from 'jssm';
264
- *
265
- * const toggle = jssm.from('up <=> down;');
266
- * ```
267
- *
268
- * @typeparam mDT The type of the machine data member; usually omitted
269
- *
270
- * @param tree The parse tree to be boiled down into a machine config
271
- *
272
- */
273
- function compile(tree) {
274
- const results = {
275
- graph_layout: [],
276
- transition: [],
277
- start_states: [],
278
- end_states: [],
279
- state_config: [],
280
- state_declaration: [],
281
- fsl_version: [],
282
- machine_author: [],
283
- machine_comment: [],
284
- machine_contributor: [],
285
- machine_definition: [],
286
- machine_language: [],
287
- machine_license: [],
288
- machine_name: [],
289
- machine_reference: [],
290
- property_definition: [],
291
- state_property: {},
292
- theme: [],
293
- flow: [],
294
- dot_preamble: [],
295
- arrange_declaration: [],
296
- arrange_start_declaration: [],
297
- arrange_end_declaration: [],
298
- machine_version: [],
299
- default_state_config: [],
300
- default_active_state_config: [],
301
- default_hooked_state_config: [],
302
- default_terminal_state_config: [],
303
- default_start_state_config: [],
304
- default_end_state_config: [],
305
- };
306
- tree.map((tr) => {
307
- const rule = compile_rule_handler(tr), agg_as = rule.agg_as, val = rule.val; // TODO FIXME no any
308
- results[agg_as] = results[agg_as].concat(val);
309
- });
310
- const property_keys = results['property_definition'].map(pd => pd.name), repeat_props = find_repeated(property_keys);
311
- if (repeat_props.length) {
312
- throw new JssmError(undefined, `Cannot repeat property definitions. Saw ${JSON.stringify(repeat_props)}`);
313
- }
314
- const assembled_transitions = [].concat(...results['transition']);
315
- const result_cfg = {
316
- start_states: results.start_states.length ? results.start_states : [assembled_transitions[0].from],
317
- end_states: results.end_states,
318
- transitions: assembled_transitions,
319
- state_property: [],
320
- };
321
- const oneOnlyKeys = [
322
- 'graph_layout', 'machine_name', 'machine_version', 'machine_comment',
323
- 'fsl_version', 'machine_license', 'machine_definition', 'machine_language',
324
- 'flow', 'dot_preamble'
325
- ];
326
- oneOnlyKeys.map((oneOnlyKey) => {
327
- if (results[oneOnlyKey].length > 1) {
328
- throw new JssmError(undefined, `May only have one ${oneOnlyKey} statement maximum: ${JSON.stringify(results[oneOnlyKey])}`);
329
- }
330
- else {
331
- if (results[oneOnlyKey].length) {
332
- result_cfg[oneOnlyKey] = results[oneOnlyKey][0];
333
- }
334
- }
335
- });
336
- ['arrange_declaration', 'arrange_start_declaration', 'arrange_end_declaration',
337
- 'machine_author', 'machine_contributor', 'machine_reference', 'theme',
338
- 'state_declaration', 'property_definition', 'default_state_config',
339
- 'default_start_state_config', 'default_end_state_config',
340
- 'default_hooked_state_config', 'default_terminal_state_config',
341
- 'default_active_state_config'].map((multiKey) => {
342
- if (results[multiKey].length) {
343
- result_cfg[multiKey] = results[multiKey];
344
- }
345
- });
346
- // re-walk state declarations, already wrapped up, to get state properties,
347
- // which go out in a different datastructure
348
- results.state_declaration.forEach(sd => {
349
- sd.declarations.forEach(decl => {
350
- if (decl.key === 'state_property') {
351
- const label = name_bind_prop_and_state(decl.name, sd.state);
352
- if (result_cfg.state_property.findIndex(c => c.name === label) !== -1) {
353
- throw new JssmError(undefined, `A state may only bind a property once (${sd.state} re-binds ${decl.name})`);
354
- }
355
- else {
356
- result_cfg.state_property.push({ name: label, default_value: decl.value });
357
- }
358
- }
359
- });
360
- });
361
- return result_cfg;
362
- }
363
- /*********
364
- *
365
- * An internal convenience wrapper for parsing then compiling a machine string.
366
- * Not generally meant for external use. Please see {@link compile} or
367
- * {@link sm}.
368
- *
369
- * @typeparam mDT The type of the machine data member; usually omitted
370
- *
371
- * @param plan The FSL code to be evaluated and built into a machine config
372
- *
373
- */
374
- function make(plan) {
375
- return compile(wrap_parse(plan));
376
- }
377
12
  /*********
378
13
  *
379
14
  * An internal method meant to take a series of declarations and fold them into
@@ -0,0 +1,135 @@
1
+ import { JssmTransition, JssmCompileSe, JssmParseTree, JssmGenericConfig } from './jssm_types';
2
+ /*********
3
+ *
4
+ * Internal method meant to perform factory assembly of an edge. Not meant for
5
+ * external use.
6
+ *
7
+ * @internal
8
+ *
9
+ * @typeparam mDT The type of the machine data member; usually omitted
10
+ *
11
+ */
12
+ declare function makeTransition<StateType, mDT>(this_se: JssmCompileSe<StateType, mDT>, from: StateType, to: StateType, isRight: boolean, _wasList?: Array<StateType>, _wasIndex?: number): JssmTransition<StateType, mDT>;
13
+ /*********
14
+ *
15
+ * This method wraps the parser call that comes from the peg grammar,
16
+ * {@link parse}. Generally neither this nor that should be used directly
17
+ * unless you mean to develop plugins or extensions for the machine.
18
+ *
19
+ * Parses the intermediate representation of a compiled string down to a
20
+ * machine configuration object. If you're using this (probably don't,) you're
21
+ * probably also using {@link compile} and {@link Machine.constructor}.
22
+ *
23
+ * ```typescript
24
+ * import { parse, compile, Machine } from 'jssm';
25
+ *
26
+ * const intermediate = wrap_parse('a -> b;', {});
27
+ * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
28
+ *
29
+ * const cfg = compile(intermediate);
30
+ * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
31
+ *
32
+ * const machine = new Machine(cfg);
33
+ * // Machine { _instance_name: undefined, _state: 'a', ...
34
+ * ```
35
+ *
36
+ * This method is mostly for plugin and intermediate tool authors, or people
37
+ * who need to work with the machine's intermediate representation.
38
+ *
39
+ * # Hey!
40
+ *
41
+ * Most people looking at this want either the `sm` operator or method `from`,
42
+ * which perform all the steps in the chain. The library's author mostly uses
43
+ * operator `sm`, and mostly falls back to `.from` when needing to parse
44
+ * strings dynamically instead of from template literals.
45
+ *
46
+ * Operator {@link sm}:
47
+ *
48
+ * ```typescript
49
+ * import { sm } from 'jssm';
50
+ *
51
+ * const lswitch = sm`on <=> off;`;
52
+ * ```
53
+ *
54
+ * Method {@link from}:
55
+ *
56
+ * ```typescript
57
+ * import * as jssm from 'jssm';
58
+ *
59
+ * const toggle = jssm.from('up <=> down;');
60
+ * ```
61
+ *
62
+ * `wrap_parse` itself is an internal convenience method for alting out an
63
+ * object as the options call. Not generally meant for external use.
64
+ *
65
+ * @param input The FSL code to be evaluated
66
+ *
67
+ * @param options Things to control about the instance
68
+ *
69
+ */
70
+ declare function wrap_parse(input: string, options?: Object): any;
71
+ /*********
72
+ *
73
+ * Compile a machine's JSON intermediate representation to a config object. If
74
+ * you're using this (probably don't,) you're probably also using
75
+ * {@link parse} to get the IR, and the object constructor
76
+ * {@link Machine.construct} to turn the config object into a workable machine.
77
+ *
78
+ * ```typescript
79
+ * import { parse, compile, Machine } from 'jssm';
80
+ *
81
+ * const intermediate = parse('a -> b;');
82
+ * // [ {key:'transition', from:'a', se:{kind:'->',to:'b'}} ]
83
+ *
84
+ * const cfg = compile(intermediate);
85
+ * // { start_states:['a'], transitions: [{ from:'a', to:'b', kind:'legal', forced_only:false, main_path:false }] }
86
+ *
87
+ * const machine = new Machine(cfg);
88
+ * // Machine { _instance_name: undefined, _state: 'a', ...
89
+ * ```
90
+ *
91
+ * This method is mostly for plugin and intermediate tool authors, or people
92
+ * who need to work with the machine's intermediate representation.
93
+ *
94
+ * # Hey!
95
+ *
96
+ * Most people looking at this want either the `sm` operator or method `from`,
97
+ * which perform all the steps in the chain. The library's author mostly uses
98
+ * operator `sm`, and mostly falls back to `.from` when needing to parse
99
+ * strings dynamically instead of from template literals.
100
+ *
101
+ * Operator {@link sm}:
102
+ *
103
+ * ```typescript
104
+ * import { sm } from 'jssm';
105
+ *
106
+ * const lswitch = sm`on <=> off;`;
107
+ * ```
108
+ *
109
+ * Method {@link from}:
110
+ *
111
+ * ```typescript
112
+ * import * as jssm from 'jssm';
113
+ *
114
+ * const toggle = jssm.from('up <=> down;');
115
+ * ```
116
+ *
117
+ * @typeparam mDT The type of the machine data member; usually omitted
118
+ *
119
+ * @param tree The parse tree to be boiled down into a machine config
120
+ *
121
+ */
122
+ declare function compile<StateType, mDT>(tree: JssmParseTree<StateType, mDT>): JssmGenericConfig<StateType, mDT>;
123
+ /*********
124
+ *
125
+ * An internal convenience wrapper for parsing then compiling a machine string.
126
+ * Not generally meant for external use. Please see {@link compile} or
127
+ * {@link sm}.
128
+ *
129
+ * @typeparam mDT The type of the machine data member; usually omitted
130
+ *
131
+ * @param plan The FSL code to be evaluated and built into a machine config
132
+ *
133
+ */
134
+ declare function make<StateType, mDT>(plan: string): JssmGenericConfig<StateType, mDT>;
135
+ export { compile, make, makeTransition, wrap_parse };