JSSM, a JavaScript state machine - the FSM for FSL
    Preparing search index...

    Function compile


    • Compile a machine's JSON intermediate representation to a config object. If you're using this (probably don't,) you're probably also using parse to get the IR, and the object constructor Machine.constructor to turn the config object into a workable machine.

      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.

      compile() ignores loc and *_loc fields during machine construction — the resulting config is identical whether or not the tree was parsed with { locations: true }. However, when those fields are present, compile() attaches the offending node's source span to any semantic JssmError it throws, via the error's source_location field (type FslSourceLocation). This lets downstream tooling (e.g. a CodeMirror 6 linter) map the error to a precise editor range without any additional source-scanning.

      import { parse, compile } from 'jssm';

      try {
      compile(parse('fsl_version: 1.0.0;\nfsl_version: 2.0.0;\na -> b;',
      { locations: true }));
      } catch (err) {
      // err.source_location.start.offset points at the second fsl_version line
      console.log(err.source_location);
      }

      Type Parameters

      • StateType
      • mDT

        The type of the machine data member; usually omitted

      Parameters

      • tree: JssmParseTree<StateType, mDT>

        The parse tree to be boiled down into a machine config. If the tree was produced with parse(input, { locations: true }), any semantic error thrown will carry a source_location span pointing at the offending statement.

      Returns JssmGenericConfig<StateType, mDT>

      FslSourceLocation

      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 sm:

      import { sm } from 'jssm';

      const lswitch = sm`on <=> off;`;

      Method from:

      import * as jssm from 'jssm';

      const toggle = jssm.from('up <=> down;');

      If the document declares no transitions (for example a states-first document of only state blocks) — a machine requires at least one transition; also for repeated property definitions, group errors, and other semantic problems noted throughout.