Class: Resyma::Core::AutomatonBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/resyma/core/automaton/builder.rb

Defined Under Namespace

Classes: NoStartError

Instance Method Summary collapse

Constructor Details

#initializeAutomatonBuilder

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

Parameters:

Returns:

  • (nil)

    Nothing



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

Parameters:

Returns:

  • (nil)

    Nothing



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

#buildObject



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

Returns:



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

Parameters:

Returns:

  • (nil)

    Nothing



51
52
53
54
# File 'lib/resyma/core/automaton/builder.rb', line 51

def start!(state)
  @start = state
  nil
end