diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/journey/gtg')
3 files changed, 72 insertions, 70 deletions
diff --git a/actionpack/lib/action_dispatch/journey/gtg/builder.rb b/actionpack/lib/action_dispatch/journey/gtg/builder.rb index 5f2e7b92ae..7d2791714b 100644 --- a/actionpack/lib/action_dispatch/journey/gtg/builder.rb +++ b/actionpack/lib/action_dispatch/journey/gtg/builder.rb @@ -8,10 +8,10 @@ module ActionDispatch attr_reader :root, :ast, :endpoints - def initialize root - @root = root - @ast = Nodes::Cat.new root, DUMMY - @followpos = nil + def initialize(root) + @root = root + @ast = Nodes::Cat.new root, DUMMY + @followpos = nil end def transition_table @@ -35,21 +35,21 @@ module ActionDispatch to = state_id[Object.new] dtrans[from, to] = sym - dtrans.add_accepting to - ps.each { |state| dtrans.add_memo to, state.memo } + dtrans.add_accepting(to) + ps.each { |state| dtrans.add_memo(to, state.memo) } else dtrans[state_id[s], state_id[u]] = sym - if u.include? DUMMY + if u.include?(DUMMY) to = state_id[u] - accepting = ps.find_all { |l| followpos(l).include? DUMMY } + accepting = ps.find_all { |l| followpos(l).include?(DUMMY) } accepting.each { |accepting_state| - dtrans.add_memo to, accepting_state.memo + dtrans.add_memo(to, accepting_state.memo) } - dtrans.add_accepting state_id[u] + dtrans.add_accepting(state_id[u]) end end @@ -60,7 +60,7 @@ module ActionDispatch dtrans end - def nullable? node + def nullable?(node) case node when Nodes::Group true @@ -73,18 +73,18 @@ module ActionDispatch when Nodes::Terminal !node.left when Nodes::Unary - nullable? node.left + nullable?(node.left) else raise ArgumentError, 'unknown nullable: %s' % node.class.name end end - def firstpos node + def firstpos(node) case node when Nodes::Star firstpos(node.left) when Nodes::Cat - if nullable? node.left + if nullable?(node.left) firstpos(node.left) | firstpos(node.right) else firstpos(node.left) @@ -100,14 +100,14 @@ module ActionDispatch end end - def lastpos node + def lastpos(node) case node when Nodes::Star firstpos(node.left) when Nodes::Or node.children.map { |c| lastpos(c) }.flatten.uniq when Nodes::Cat - if nullable? node.right + if nullable?(node.right) lastpos(node.left) | lastpos(node.right) else lastpos(node.right) @@ -121,40 +121,41 @@ module ActionDispatch end end - def followpos node + def followpos(node) followpos_table[node] end private - def followpos_table - @followpos ||= build_followpos - end - def build_followpos - table = Hash.new { |h,k| h[k] = [] } - @ast.each do |n| - case n - when Nodes::Cat - lastpos(n.left).each do |i| - table[i] += firstpos(n.right) - end - when Nodes::Star - lastpos(n).each do |i| - table[i] += firstpos(n) + def followpos_table + @followpos ||= build_followpos + end + + def build_followpos + table = Hash.new { |h, k| h[k] = [] } + @ast.each do |n| + case n + when Nodes::Cat + lastpos(n.left).each do |i| + table[i] += firstpos(n.right) + end + when Nodes::Star + lastpos(n).each do |i| + table[i] += firstpos(n) + end end end + table end - table - end - def symbol edge - case edge - when Journey::Nodes::Symbol - edge.regexp - else - edge.left + def symbol(edge) + case edge + when Journey::Nodes::Symbol + edge.regexp + else + edge.left + end end - end end end end diff --git a/actionpack/lib/action_dispatch/journey/gtg/simulator.rb b/actionpack/lib/action_dispatch/journey/gtg/simulator.rb index 802d692dd5..58ad803841 100644 --- a/actionpack/lib/action_dispatch/journey/gtg/simulator.rb +++ b/actionpack/lib/action_dispatch/journey/gtg/simulator.rb @@ -6,7 +6,7 @@ module ActionDispatch class MatchData # :nodoc: attr_reader :memos - def initialize memos + def initialize(memos) @memos = memos end end @@ -14,12 +14,12 @@ module ActionDispatch class Simulator # :nodoc: attr_reader :tt - def initialize transition_table + def initialize(transition_table) @tt = transition_table end - def simulate string - input = StringScanner.new string + def simulate(string) + input = StringScanner.new(string) state = [0] while sym = input.scan(%r([/.?]|[^/.?]+)) state = tt.move(state, sym) @@ -31,9 +31,9 @@ module ActionDispatch return if acceptance_states.empty? - memos = acceptance_states.map { |x| tt.memo x }.flatten.compact + memos = acceptance_states.map { |x| tt.memo(x) }.flatten.compact - MatchData.new memos + MatchData.new(memos) end alias :=~ :simulate diff --git a/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb b/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb index 2c34826bb0..da0cddd93c 100644 --- a/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb +++ b/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb @@ -15,7 +15,7 @@ module ActionDispatch @memos = Hash.new { |h,k| h[k] = [] } end - def add_accepting state + def add_accepting(state) @accepting[state] = true end @@ -23,24 +23,24 @@ module ActionDispatch @accepting.keys end - def accepting? state + def accepting?(state) @accepting[state] end - def add_memo idx, memo + def add_memo(idx, memo) @memos[idx] << memo end - def memo idx + def memo(idx) @memos[idx] end - def eclosure t + def eclosure(t) Array(t) end - def move t, a - move_string(t, a).concat move_regexp(t, a) + def move(t, a) + move_string(t, a).concat(move_regexp(t, a)) end def to_json @@ -55,15 +55,15 @@ module ActionDispatch end JSON.dump({ - :regexp_states => simple_regexp, - :string_states => @string_states, - :accepting => @accepting + regexp_states: simple_regexp, + string_states: @string_states, + accepting: @accepting }) end def to_svg - svg = IO.popen("dot -Tsvg", 'w+') { |f| - f.write to_dot + svg = IO.popen('dot -Tsvg', 'w+') { |f| + f.write(to_dot) f.close_write f.readlines } @@ -71,7 +71,7 @@ module ActionDispatch svg.join.sub(/width="[^"]*"/, '').sub(/height="[^"]*"/, '') end - def visualizer paths, title = 'FSM' + def visualizer(paths, title = 'FSM') viz_dir = File.join File.dirname(__FILE__), '..', 'visualizer' fsm_js = File.read File.join(viz_dir, 'fsm.js') fsm_css = File.read File.join(viz_dir, 'fsm.css') @@ -110,7 +110,7 @@ module ActionDispatch template.result(binding) end - def []= from, to, sym + def []=(from, to, sym) case sym when String @string_states[from][sym] = to @@ -136,19 +136,20 @@ module ActionDispatch end private - def move_regexp t, a - return [] if t.empty? - t.map { |s| - @regexp_states[s].map { |re,v| re === a ? v : nil } - }.flatten.compact.uniq - end + def move_regexp(t, a) + return [] if t.empty? - def move_string t, a - return [] if t.empty? + t.map { |s| + @regexp_states[s].map { |re, v| re === a ? v : nil } + }.flatten.compact.uniq + end - t.map { |s| @string_states[s][a] }.compact - end + def move_string(t, a) + return [] if t.empty? + + t.map { |s| @string_states[s][a] }.compact + end end end end |