@@ -59,8 +59,8 @@ def move(t, full_string, start_index, end_index)
59
59
if previous_start . nil?
60
60
# In the simple case of a "default" param regex do this fast-path and add all
61
61
# next states.
62
- if token_matches_default_component && states = @stdparam_states [ s ]
63
- states . each { | re , v | next_states << [ v , nil ] . freeze if ! v . nil? }
62
+ if token_matches_default_component && std_state = @stdparam_states [ s ]
63
+ next_states << [ std_state , nil ] . freeze
64
64
end
65
65
66
66
# When we have a literal string, we can just pull the next state
@@ -163,54 +163,40 @@ def visualizer(paths, title = "FSM")
163
163
end
164
164
165
165
def []=( from , to , sym )
166
- to_mappings = states_hash_for ( sym ) [ from ] ||= { }
167
166
case sym
167
+ when String , Symbol
168
+ to_mapping = @string_states [ from ] ||= { }
169
+ # account for symbols in the constraints the same as strings
170
+ to_mapping [ sym . to_s ] = to
168
171
when Regexp
169
- # we must match the whole string to a token boundary
170
172
if sym == DEFAULT_EXP
171
- sym = DEFAULT_EXP_ANCHORED
173
+ @stdparam_states [ from ] = to
172
174
else
173
- sym = /\A #{ sym } \Z /
175
+ to_mapping = @regexp_states [ from ] ||= { }
176
+ # we must match the whole string to a token boundary
177
+ to_mapping [ /\A #{ sym } \Z / ] = to
174
178
end
175
- when Symbol
176
- # account for symbols in the constraints the same as strings
177
- sym = sym . to_s
179
+ else
180
+ raise ArgumentError , "unknown symbol: %s" % sym . class
178
181
end
179
- to_mappings [ sym ] = to
180
182
end
181
183
182
184
def states
183
185
ss = @string_states . keys + @string_states . values . flat_map ( &:values )
184
- ps = @stdparam_states . keys + @stdparam_states . values . flat_map ( & :values )
186
+ ps = @stdparam_states . keys + @stdparam_states . values
185
187
rs = @regexp_states . keys + @regexp_states . values . flat_map ( &:values )
186
188
( ss + ps + rs ) . uniq
187
189
end
188
190
189
191
def transitions
190
192
@string_states . flat_map { |from , hash |
191
193
hash . map { |s , to | [ from , s , to ] }
192
- } + @stdparam_states . flat_map { |from , hash |
193
- hash . map { | s , to | [ from , s , to ] }
194
+ } + @stdparam_states . map { |from , to |
195
+ [ from , DEFAULT_EXP_ANCHORED , to ]
194
196
} + @regexp_states . flat_map { |from , hash |
195
197
hash . map { |s , to | [ from , s , to ] }
196
198
}
197
199
end
198
-
199
- private
200
- def states_hash_for ( sym )
201
- case sym
202
- when String , Symbol
203
- @string_states
204
- when Regexp
205
- if sym == DEFAULT_EXP
206
- @stdparam_states
207
- else
208
- @regexp_states
209
- end
210
- else
211
- raise ArgumentError , "unknown symbol: %s" % sym . class
212
- end
213
- end
214
200
end
215
201
end
216
202
end
0 commit comments