Class: Dhaka::LexerSupport::State

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_machine, action = nil) ⇒ State

Returns a new instance of State.



6
7
8
9
10
11
# File 'lib/dhaka/lexer/state.rb', line 6

def initialize(state_machine, action=nil)
  @state_machine      = state_machine
  @transitions        = {}
  @checkpoint_actions = []
  @action             = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



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

def action
  @action
end

#checkpoint_actionsObject (readonly)

Returns the value of attribute checkpoint_actions.



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

def checkpoint_actions
  @checkpoint_actions
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



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

def transitions
  @transitions
end

Instance Method Details

#accept(pattern) ⇒ Object



33
34
35
# File 'lib/dhaka/lexer/state.rb', line 33

def accept(pattern)
  @action = AcceptAction.new(pattern)
end

#accept_with_lookahead(pattern) ⇒ Object



37
38
39
# File 'lib/dhaka/lexer/state.rb', line 37

def accept_with_lookahead(pattern)
  @action = LookaheadAcceptAction.new(pattern)
end

#accepting?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/dhaka/lexer/state.rb', line 13

def accepting?
  @action
end

#add_checkpoint(pattern) ⇒ Object



29
30
31
# File 'lib/dhaka/lexer/state.rb', line 29

def add_checkpoint(pattern)
  checkpoint_actions << LexerSupport::CheckpointAction.new(pattern)
end

#compile_to_ruby_sourceObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dhaka/lexer/state.rb', line 45

def compile_to_ruby_source
  result  = "  at_state(#{object_id}) {\n"
  result << "    #{action.compile_to_ruby_source}\n" if action
  checkpoint_actions.each do |checkpoint_action|
    result << "    #{checkpoint_action.compile_to_ruby_source}\n"
  end
  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



22
23
24
25
26
27
# File 'lib/dhaka/lexer/state.rb', line 22

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

#process(lexer_run) ⇒ Object



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

def process lexer_run
  checkpoint_actions.each {|action| action.call(lexer_run)}
  action.call(lexer_run) if accepting?
end

#recognize(pattern) ⇒ Object



41
42
43
# File 'lib/dhaka/lexer/state.rb', line 41

def recognize pattern
  @pattern = pattern
end