Class: Resyma::Core::AutomatonBuilder
- Inherits:
-
Object
- Object
- Resyma::Core::AutomatonBuilder
- Defined in:
- lib/resyma/core/automaton/builder.rb
Defined Under Namespace
Classes: NoStartError
Instance Method Summary collapse
-
#accept!(state) ⇒ nil
Add a state to the accept set of the automaton.
-
#add_transition!(from_state, matchable, to_state) ⇒ nil
Adds a new transition to the automaton.
- #build ⇒ Object
-
#initialize ⇒ AutomatonBuilder
constructor
A new instance of AutomatonBuilder.
-
#new_state!(start: false, accept: false) ⇒ Resyma::Core::State
Adds a new state to the automaton and returns it.
-
#start!(state) ⇒ nil
Specify a starting state for the automaton.
Constructor Details
#initialize ⇒ AutomatonBuilder
Returns a new instance of AutomatonBuilder.
11 12 13 14 15 16 |
# File 'lib/resyma/core/automaton/builder.rb', line 11 def initialize @next_id = 0 @start = nil @accept_set = Set[] @transition_table = TransitionTable.new end |
Instance Method Details
#accept!(state) ⇒ nil
Add a state to the accept set of the automaton
63 64 65 66 |
# File 'lib/resyma/core/automaton/builder.rb', line 63 def accept!(state) @accept_set.add(state) nil end |
#add_transition!(from_state, matchable, to_state) ⇒ nil
Adds a new transition to the automaton
40 41 42 |
# File 'lib/resyma/core/automaton/builder.rb', line 40 def add_transition!(from_state, matchable, to_state) @transition_table.add_transition!(from_state, matchable, to_state) end |
#build ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/resyma/core/automaton/builder.rb', line 68 def build if @start.nil? raise NoStartError, "Cannot build a automaton without a start state" end Automaton.new(@start, @accept_set, @transition_table) end |
#new_state!(start: false, accept: false) ⇒ Resyma::Core::State
Adds a new state to the automaton and returns it
23 24 25 26 27 28 29 |
# File 'lib/resyma/core/automaton/builder.rb', line 23 def new_state!(start: false, accept: false) inst = State.with_id(@next_id) @next_id += 1 start! inst if start accept! inst if accept inst end |
#start!(state) ⇒ nil
Specify a starting state for the automaton
51 52 53 54 |
# File 'lib/resyma/core/automaton/builder.rb', line 51 def start!(state) @start = state nil end |