Class: ParserBuilder

Inherits:
StateMachineBuilder show all
Defined in:
parser_builder.rb

Overview

A StateMachineBuilder implementation which processes a set of tokens as defined in the User Manual

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ParserBuilder


18
19
20
21
# File 'parser_builder.rb', line 18

def initialize(args = {})
  self.logger = args.fetch(:logger)
  self.states = {}
end

Instance Attribute Details

#loggerLogger


16
17
18
# File 'parser_builder.rb', line 16

def logger
  @logger
end

#state_machineStateMachine


13
14
15
# File 'parser_builder.rb', line 13

def state_machine
  @state_machine
end

#statesHash<Symbol, State>


10
11
12
# File 'parser_builder.rb', line 10

def states
  @states
end

Instance Method Details

#add_transition(key, transition) ⇒ nil (protected)

Adds a transition to a state given an identifier


154
155
156
# File 'parser_builder.rb', line 154

def add_transition(key, transition)
  self.states[key].push_transition(transition)
end

#back_to_startObject (protected)


61
62
63
# File 'parser_builder.rb', line 61

def back_to_start
  @back_to_start ||= create_transition(:initial, /./, :initial)
end

#buildObject


24
25
26
27
28
29
# File 'parser_builder.rb', line 24

def build
  self.state_machine = StateMachine.new(logger: logger)
  create_states
  state_machine.current_state = get_state(:initial)
  state_machine
end

#create_state(key) ⇒ State (protected)

Creates a new State with the given identifier


161
162
163
# File 'parser_builder.rb', line 161

def create_state(key)
  self.states[key] = State.new(name: key)
end

#create_state_add(initial) ⇒ Object (protected)


102
103
104
105
106
# File 'parser_builder.rb', line 102

def create_state_add(initial)
  create_state(:add)
  add_transition initial, create_transition(:add, /\+/)
  add_transition :add, back_to_start
end

#create_state_comment(_initial) ⇒ Object (protected)


134
135
136
137
# File 'parser_builder.rb', line 134

def create_state_comment(_initial)
  create_state(:comment)
  add_transition :comment, create_transition(:comment, /./)
end

#create_state_divide(initial) ⇒ Object (protected)


121
122
123
124
125
126
# File 'parser_builder.rb', line 121

def create_state_divide(initial)
  create_state(:divide)
  add_transition initial, create_transition(:divide, /\//)
  add_transition :divide, create_transition(:comment, /\//)
  add_transition :divide, back_to_start
end

#create_state_float(_initial) ⇒ Object (protected)


93
94
95
96
97
98
99
100
# File 'parser_builder.rb', line 93

def create_state_float(_initial)
  create_state(:float)
  add_transition :float, create_transition(:float, /[0-9]/)
  add_transition :float, create_transition(:float, /E/)
  add_transition :float, create_transition(:float, /\./)
  add_transition :float, create_transition(:float, /\-/)
  add_transition :float, back_to_start
end

#create_state_int(initial) ⇒ Object (protected)


85
86
87
88
89
90
91
# File 'parser_builder.rb', line 85

def create_state_int(initial)
  create_state(:int)
  add_transition initial, create_transition(:int, /[0-9]/)
  add_transition :int, create_transition(:int, /[0-9]/)
  add_transition :int, create_transition(:float, /\./)
  add_transition :int, back_to_start
end

#create_state_minus(initial) ⇒ Object (protected)


108
109
110
111
112
113
# File 'parser_builder.rb', line 108

def create_state_minus(initial)
  create_state(:minus)
  add_transition initial, create_transition(:minus, /\-/)
  add_transition :minus, create_transition(:int, /[0-9]/)
  add_transition :minus, back_to_start
end

#create_state_multiply(initial) ⇒ Object (protected)


115
116
117
118
119
# File 'parser_builder.rb', line 115

def create_state_multiply(initial)
  create_state(:multiply)
  add_transition initial, create_transition(:multiply, /\*/)
  add_transition :multiply, back_to_start
end

#create_state_operators(initial) ⇒ Object (protected)


79
80
81
82
83
# File 'parser_builder.rb', line 79

def create_state_operators(initial)
  create_state(:operators)
  add_transition initial, create_transition(:operators, /=/)
  add_transition :operators, back_to_start
end

#create_state_power(initial) ⇒ Object (protected)


128
129
130
131
132
# File 'parser_builder.rb', line 128

def create_state_power(initial)
  create_state(:power)
  add_transition initial, create_transition(:power, /\^/)
  add_transition :power, back_to_start
end

#create_state_symbols(initial) ⇒ Object (protected)


72
73
74
75
76
77
# File 'parser_builder.rb', line 72

def create_state_symbols(initial)
  create_state(:symbols)
  add_transition initial, create_transition(:symbols, /[()]/)
  add_transition :symbols, create_transition(:symbols, /[()]/)
  add_transition :symbols, back_to_start
end

#create_state_variable(initial) ⇒ Object (protected)


65
66
67
68
69
70
# File 'parser_builder.rb', line 65

def create_state_variable(initial)
  create_state(:variable)
  add_transition initial, create_transition(:variable, /[a-zA-Z]/)
  add_transition :variable, create_transition(:variable, /[a-zA-Z]/)
  add_transition :variable, back_to_start
end

#create_statesObject (protected)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'parser_builder.rb', line 43

def create_states
  create_state(:initial)

  create_state_variable(:initial)
  create_state_operators(:initial)
  create_state_symbols(:initial)
  create_state_comment(:initial)
  create_state_float(:initial)
  create_state_int(:initial)
  create_state_minus(:initial)
  create_state_add(:initial)
  create_state_multiply(:initial)
  create_state_divide(:initial)
  create_state_power(:initial)

  add_transition :initial, create_transition(:initial, /./)
end

#create_transition(key, exp, type = :final) ⇒ Transition (protected)

Adds a transition to a state given an identifier the expression or characters for which the transition is valid


146
147
148
# File 'parser_builder.rb', line 146

def create_transition(key, exp, type = :final)
  Transition.new(to_state: states[key], valid_characters: exp, type: type)
end

#get_state(key) ⇒ Object (protected)


39
40
41
# File 'parser_builder.rb', line 39

def get_state(key)
  states[key]
end

#resetObject


32
33
34
35
# File 'parser_builder.rb', line 32

def reset
  state_machine.reset
  state_machine.current_state = get_state(:initial)
end