# File lib/gherkin/parser/parser.rb, line 87 def initialize(parser, name) @parser = parser @name = name @transition_map = transition_map(name) @state = name end
# File lib/gherkin/parser/parser.rb, line 94 def event(ev, line) states = @transition_map[@state] raise "Unknown state: #{@state.inspect} for machine #{@name}" if states.nil? new_state = states[ev] case new_state when "E" yield @state, expected when /push\((.+)\)/ @parser.push_machine($1) @parser.event(ev, line) when "pop()" @parser.pop_machine() @parser.event(ev, line) else raise "Unknown transition: #{ev.inspect} among #{states.inspect} for machine #{@name}" if new_state.nil? @state = new_state end end
# File lib/gherkin/parser/parser.rb, line 113 def expected allowed = @transition_map[@state].find_all { |_, action| action != "E" } allowed.collect { |state| state[0] }.sort - ['eof'] end
# File lib/gherkin/parser/parser.rb, line 126 def build_transition_map(name) table = transition_table(name) events = table.shift[1..-1] table.inject({}) do |machine, actions| state = actions.shift machine[state] = Hash[*events.zip(actions).flatten] machine end end
# File lib/gherkin/parser/parser.rb, line 122 def transition_map(name) @@transition_maps[name] ||= build_transition_map(name) end
# File lib/gherkin/parser/parser.rb, line 136 def transition_table(name) state_machine_reader = StateMachineReader.new lexer = Gherkin::I18n.new('en').lexer(state_machine_reader) machine = File.dirname(__FILE__) + "/#{name}.txt" lexer.scan(File.read(machine)) state_machine_reader.rows end