From 952bffc33c13279f23b56f0c0202a042bfafc3c2 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 21 May 2021 14:27:42 -0400 Subject: [PATCH] Move DFA#nil_transition_states to NFA::State --- lib/imbecile/regex/dfa.rb | 25 +------------------------ lib/imbecile/regex/nfa.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/imbecile/regex/dfa.rb b/lib/imbecile/regex/dfa.rb index cbdefa1..b1ca0b7 100644 --- a/lib/imbecile/regex/dfa.rb +++ b/lib/imbecile/regex/dfa.rb @@ -8,30 +8,7 @@ module Imbecile nfas.each do |nfa| start_nfa.start_state.add_transition(nil, nfa.start_state) end - nil_transition_states = nil_transition_states(start_nfa.start_state) - end - - private - - # Determine the set of states that can be reached by nil transitions - # from the given state. - # - # @return [Set] - # Set of states. - def nil_transition_states(state) - states = Set[state] - analyze_state = lambda do |state| - state.transitions.each do |range, dest_state| - if range.nil? - unless states.include?(dest_state) - states << dest_state - analyze_state[dest_state] - end - end - end - end - analyze_state[state] - states + nil_transition_states = start_nfa.start_state.nil_transition_states end end diff --git a/lib/imbecile/regex/nfa.rb b/lib/imbecile/regex/nfa.rb index 3964edd..8b2632f 100644 --- a/lib/imbecile/regex/nfa.rb +++ b/lib/imbecile/regex/nfa.rb @@ -16,6 +16,27 @@ module Imbecile @transitions << [code_point, destination_state] end + # Determine the set of states that can be reached by nil transitions. + # from this state. + # + # @return [Set] + # Set of states. + def nil_transition_states + states = Set[self] + analyze_state = lambda do |state| + state.transitions.each do |range, dest_state| + if range.nil? + unless states.include?(dest_state) + states << dest_state + analyze_state[dest_state] + end + end + end + end + analyze_state[self] + states + end + end attr_accessor :start_state