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

    Function parse


    • This method wraps the parser call that comes from the peg grammar, 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 compile and Machine.constructor.

      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.

      Pass { locations: true } to attach source-span information to every object node in the AST. Each node gains a loc field of type FslSourceLocation covering its full statement span. Selected nodes also gain curated sub-span fields that pinpoint individual tokens within the statement:

      • Transition nodes: from_loc (source state), to_loc (target state, on the nested se object), l_action_loc / r_action_loc (action labels).
      • State-declaration nodes: name_loc (state name), plus value_loc on each color-bearing item inside the declaration block.
      • Machine-attribute nodes (machine_name, fsl_version, etc.): value_loc (the attribute value token).

      Without { locations: true } the AST is byte-for-byte identical to the default output; no loc or *_loc fields are present.

      const tree = wrap_parse('a -> b;', { locations: true });
      // tree[0].loc === { start: { offset: 0, line: 1, column: 1 },
      // end: { offset: 7, line: 1, column: 8 } }
      // tree[0].from_loc.start.offset === 0 // 'a'
      // tree[0].se.to_loc.start.offset === 5 // 'b'

      Type Parameters

      • StateType = string

        The type of state names in the resulting tree; the grammar itself always produces strings, so only override this when threading a caller's own state naming through to compile.

      • mDT = unknown

        The type of the machine data member; usually omitted.

      Parameters

      • input: string

        The FSL code to be evaluated

      • Optionaloptions: JssmParseOptions

        Things to control about the parse. Pass { locations: true } to enable opt-in source location tracking on every AST node. When omitted, an empty options object is passed through to the parser.

      Returns JssmParseTree<StateType, mDT>

      The machine's intermediate representation: a flat JssmParseTree with one node per top-level FSL statement.

      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;');

      wrap_parse itself is an internal convenience method for alting out an object as the options call. Not generally meant for external use.

      The generated PEG.js parser's SyntaxError when input is not valid FSL.