Class: ActionDispatch::Journey::GTG::Simulator

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_dispatch/journey/gtg/simulator.rb

Overview

:nodoc:

Constant Summary collapse

STATIC_TOKENS =
Array.new(64)
INITIAL_STATE =
[0, nil].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transition_table) ⇒ Simulator

Returns a new instance of Simulator.



27
28
29
# File 'actionpack/lib/action_dispatch/journey/gtg/simulator.rb', line 27

def initialize(transition_table)
  @tt = transition_table
end

Instance Attribute Details

#ttObject (readonly)

Returns the value of attribute tt.



25
26
27
# File 'actionpack/lib/action_dispatch/journey/gtg/simulator.rb', line 25

def tt
  @tt
end

Instance Method Details

#memos(string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'actionpack/lib/action_dispatch/journey/gtg/simulator.rb', line 31

def memos(string)
  state = INITIAL_STATE

  pos = 0
  eos = string.bytesize

  while pos < eos
    start_index = pos
    pos += 1

    if (token = STATIC_TOKENS[string.getbyte(start_index)])
      state = tt.move(state, string, token, start_index, false)
    else
      while pos < eos && STATIC_TOKENS[string.getbyte(pos)].nil?
        pos += 1
      end

      token = string.byteslice(start_index, pos - start_index)
      state = tt.move(state, string, token, start_index, true)
    end
  end

  acceptance_states = []
  states_count = state.size
  i = 0
  while i < states_count
    if state[i + 1].nil?
      s = state[i]
      if tt.accepting?(s)
        acceptance_states.concat(tt.memo(s))
      end
    end
    i += 2
  end

  acceptance_states.empty? ? yield : acceptance_states
end