Test Repeat success and next state

This commit is contained in:
2024-10-16 09:06:25 -06:00
parent 59903ba151
commit bfc9a9ae58
2 changed files with 19 additions and 17 deletions

View File

@ -379,25 +379,26 @@ func Pipe[In, Ignore, Through any](p Parser[In, Ignore]) func(Through) Parser[In
// It succeeds if and only if p succeeds at least minCount times.
// It consumes if and only if at least one of the applications of p consumes.
func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
return func(s State[In]) (Result[In, []Out], error) {
return func(state State[In]) (Result[In, []Out], error) {
var values []Out
var consumed bool
next := s
currState := state
for {
result, err := p(next)
result, err := p(currState)
if err != nil {
return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err)
}
consumed = consumed || result.Consumed()
var value Out
var success bool
success, value, next = result.Status()
success, value, nextState := result.Status()
if !success {
if len(values) >= minCount {
return Succeed(consumed, values, next, MessageOK(s.Pos())), nil
return Succeed(consumed, values, currState, MessageOK(state.Pos())), nil
}
return Fail[In, []Out](consumed, result.Message()), nil
}
currState = nextState
values = append(values, value)
}
}