Class: Dhaka::LexerSupport::State

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_machine, pattern) ⇒ State

Returns a new instance of State.



5
6
7
8
9
# File 'lib/lexer/state.rb', line 5

def initialize state_machine, pattern
  @state_machine = state_machine
  @pattern          = pattern
  @transitions      = {}
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



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

def transitions
  @transitions
end

Instance Method Details

#accepting?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/lexer/state.rb', line 11

def accepting?
  pattern
end

#compile_to_ruby_sourceObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lexer/state.rb', line 26

def compile_to_ruby_source
  result  = "  at_state(#{object_id}) {\n"
  result << "    recognize(#{pattern.inspect})\n" if accepting?
  transition_keys_by_destination_state = Hash.new {|hash, key| hash[key] = []}
  transitions.each do |key, dest_state|
    transition_keys_by_destination_state[dest_state.object_id] << key
  end

  transition_keys_by_destination_state.keys.each do |state_id|
    transition_keys = transition_keys_by_destination_state[state_id].collect {|transition_key| "#{transition_key.inspect}"}.join(', ')
    result << "    for_characters(#{transition_keys}) { switch_to #{state_id} }\n"
  end

  result << "  }"
  result
end

#for_characters(*characters, &blk) ⇒ Object



15
16
17
18
19
20
# File 'lib/lexer/state.rb', line 15

def for_characters *characters, &blk
  dest_state = @state_machine.instance_eval(&blk)
  characters.each do |char|
    transitions[char] = dest_state
  end
end

#recognize(pattern) ⇒ Object



22
23
24
# File 'lib/lexer/state.rb', line 22

def recognize pattern
  @pattern = pattern
end