Class: MetaParse::RepetitionMatcher
- Defined in:
- lib/meta_parse.rb
Overview
Matcher subclass matching sub_match repeatedly.
Instance Attribute Summary collapse
-
#max ⇒ Object
Match at most max times, if max is present.
-
#min ⇒ Object
Match at least min times, if min is present.
-
#reducer ⇒ Object
A proc used to combine match results as by inject, if present.
Attributes inherited from Matcher
Instance Method Summary collapse
-
#initialize(sub_match, min = 0, max = nil, reducer = nil, initial_value = []) ⇒ RepetitionMatcher
constructor
A new instance of RepetitionMatcher.
-
#match?(scanner, context = nil) ⇒ Boolean
unless min && (matches.count < min) case finalizer when Proc finalizer.call(matches, *finalizer_args) when Symbol send(finalizer, matches, *finalizer_args) when nil matches end end end.
- #show ⇒ Object
-
#stateful ⇒ Object
RepetitionMatcher is stateful.
Methods inherited from Matcher
compile, #inspect, #m, #m?, #match
Constructor Details
#initialize(sub_match, min = 0, max = nil, reducer = nil, initial_value = []) ⇒ RepetitionMatcher
Returns a new instance of RepetitionMatcher.
265 266 267 |
# File 'lib/meta_parse.rb', line 265 def initialize(sub_match, min=0, max=nil, reducer=nil, initial_value=[]) @spec, @min, @max, @reducer, @initial_value = sub_match, min, max, reducer, initial_value end |
Instance Attribute Details
#max ⇒ Object
Match at most max times, if max is present.
260 261 262 |
# File 'lib/meta_parse.rb', line 260 def max @max end |
#min ⇒ Object
Match at least min times, if min is present.
257 258 259 |
# File 'lib/meta_parse.rb', line 257 def min @min end |
#reducer ⇒ Object
A proc used to combine match results as by inject, if present.
263 264 265 |
# File 'lib/meta_parse.rb', line 263 def reducer @reducer end |
Instance Method Details
#match?(scanner, context = nil) ⇒ Boolean
unless min && (matches.count < min)
case finalizer
when Proc
finalizer.call(matches, *finalizer_args)
when Symbol
send(finalizer, matches, *finalizer_args)
when nil
matches
end
end
end
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/meta_parse.rb', line 295 def match?(scanner, context=nil) # Need to copy the initial value since it is potentially destructively modified (if an array, for example). acc = begin @initial_value.dup rescue TypeError @initial_value end match_count = 0 while (!max || (match_count < max)) && (one_match = spec.match(scanner)) match_count += 1 if reducer acc = reducer.call(acc, one_match) else acc << one_match end end unless min && (match_count < min) acc end end |
#show ⇒ Object
316 317 318 |
# File 'lib/meta_parse.rb', line 316 def show "[#{min}, #{max}] #{ spec.show }" end |
#stateful ⇒ Object
RepetitionMatcher is stateful.
272 273 274 |
# File 'lib/meta_parse.rb', line 272 def stateful true end |