Skip to content

Instantly share code, notes, and snippets.

@msassak
Created November 12, 2009 07:24
Show Gist options
  • Save msassak/232684 to your computer and use it in GitHub Desktop.
Save msassak/232684 to your computer and use it in GitHub Desktop.
require 'gherkin'
class TableListener
attr_reader :rows
def table(rows, line_number)
@rows = rows
end
end
class Parser
def initialize(state)
table_listener = TableListener.new
@parser = Gherkin::Parser['en'].new(table_listener)
@parser.scan(File.read('./root.txt'))
@state_machine = state_machine(table_listener.rows)
@current = state
end
def state_machine(table)
events = table.shift[1..-1]
table.inject({}) do |machine, actions|
state = actions.shift
machine[state] = Hash[*events.zip(actions).flatten]
machine
end
end
# Doesn't yet fall back to super
def method_missing(meth, *args)
meth = meth.to_s
case @state_machine[@current][meth]
when "E"
raise "Oh noes! #{expected.join(' ')}"
when "-"
# @listener.send(meth, *args)
else
@current = meth
# @listener.send(meth, *args)
end
end
def expected
allowed = @state_machine[@current].find_all { |_, action| action != "E" }
allowed.collect { |state| state[0] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment