lib/dolos.rb in dolos-0.1.1 vs lib/dolos.rb in dolos-0.1.2
- old
+ new
@@ -107,26 +107,28 @@
# rep(n = 2) # exactly 2
# repeat(n_min: 2, n_max: 4) # 2 to 4
# repeat(n_min: 2) # 2 or more
def repeat(n_min:, n_max: Float::INFINITY)
Parser.new do |state|
- results = []
+ values = []
+ captures = []
count = 0
while count < n_max
result = run_with_state(state.dup)
break if result.failure?
- results << result.value
+ values << result.value
+ captures.concat(result.captures)
state.input.advance(result.length)
count += 1
end
if count < n_min
Failure.new("Expected parser to match at least #{n_min} times but matched only #{count} times", false)
else
- Success.new(results, 0) # Passing 0, because we already advanced the input and flatmap will advance it again
+ Success.new(values, 0, captures)
end
end
end
def zero_or_more