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