Class: ScripTTY::Util::FSM::DefinitionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/util/fsm/definition_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(definition) ⇒ Object

Returns an array of hashes representing the state transition table for a given FSM definition.

Each table entry has the following keys:

:state

The state to which the entry applies. State 1 is the starting state.

:input

The input used to match this entry, or the symbol :any. The :any input represents any input that does not match a more specific entry.

:event_name

The name to be passed to the callback when this entry is reached. May be nil.

:next_state

The next state to move to after the callback is invoked. Note that the callback might modify this variable.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
# File 'lib/scriptty/util/fsm/definition_parser.rb', line 42

def parse(definition)
  parser = ScripTTYFSMDefinitionParser.new
  parse_tree = parser.parse(definition + "\n")    # The grammar requires a newline at the end of the file, so make sure it's there.
  raise ArgumentError.new(parser.failure_reason) unless parse_tree
  state_transition_table = []
  load_recursive(state_transition_table, parse_tree, :start, {})
  normalize_state_transition_table(state_transition_table)
  state_transition_table
end