A mixin to add instance methods for setting up pushdown states.
Overload the initializer to push the initial state object for any pushdown states.
# File lib/pushdown/automaton.rb, line 36
def initialize( * )
super if defined?( super )
self.class.pushdown_states.each do |name, config|
self.log.debug "Pushing initial %s" % [ name ]
state_class = self.class.public_send( "initial_#{name}" ) or
raise "unset initial_%s while pushing initial state" % [ name ]
data = if self.respond_to?( "initial_#{name}_data" )
self.public_send( "initial_#{name}_data" )
else
nil
end
self.log.info "Pushing an instance of %p as the initial state." % [ state_class ]
transition = Pushdown::Transition.create( :push, :initial, state_class, data )
self.log.debug " applying %p to an new empty stack" % [ transition ]
stack = transition.apply( [] )
self.instance_variable_set( "@#{name}_stack", stack )
end
end
The body of the event handler, called by the handle_{name}_event.
# File lib/pushdown/automaton.rb, line 61
def handle_pushdown_result( stack, result, name )
if result.is_a?( Symbol )
current_state = stack.last
result = current_state.transition( result, self, name )
end
if result.is_a?( Pushdown::Transition )
new_stack = result.apply( stack )
stack.replace( new_stack )
end
return result
end