Class: Dhaka::LexerSupport::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/dhaka/lexer/state_machine.rb

Direct Known Subclasses

Dhaka::Lexer, DFA

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_key) ⇒ StateMachine

Returns a new instance of StateMachine.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dhaka/lexer/state_machine.rb', line 6

def initialize start_key
  @states = Hash.new do |hash, key|
    new_state = new_state_for_key key
    hash[key] = new_state
    transition_characters(key).each do |char|
      dest_key                    = dest_key_for(key, char)
      dest_state                  = hash[dest_key]
      new_state.transitions[char] = dest_state
    end
    new_state
  end
  @start_state = @states[start_key]
end

Instance Attribute Details

#start_stateObject (readonly)

Returns the value of attribute start_state.



4
5
6
# File 'lib/dhaka/lexer/state_machine.rb', line 4

def start_state
  @start_state
end

Instance Method Details

#to_dotObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dhaka/lexer/state_machine.rb', line 20

def to_dot
  Dot::Digraph.new(:fontsize => 10, :shape => :circle, :size => 5) do |g|
    start = 'Start'
    g.node(start, :label => start)
    g.edge(start, @start_state)
    @states.values.each do |state|
      state_attributes = {}
      state_attributes.merge!(:shape => :doublecircle, :label => state.action.to_dot) if state.accepting?
      g.node(state, state_attributes)
      state.transitions.each do |transition_key, dest_state|
        g.edge(state, dest_state, :label => transition_key.inspect)
      end
    end
  end.to_dot
end