Get rid of binary Choose

This commit is contained in:
Brandon Dyck 2024-09-02 21:20:13 -06:00
parent 183e4db928
commit b06454a4bc

View File

@ -82,25 +82,7 @@ func Bind[In, A, B any](p Parser[In, A], f func(A) Parser[In, B]) Parser[In, B]
}
}
// TODO Add error propagation
func Choose[In, Out any](p, q Parser[In, Out]) Parser[In, Out] {
return func(input State[In]) (bool, Result[In, Out], error) {
consumedP, replyP, errP := p(input)
if consumedP {
return consumedP, replyP, errP
}
if errP != nil {
return q(input)
}
consumedQ, replyQ, errQ := q(input)
if consumedQ {
return consumedQ, replyQ, errQ
}
return consumedP, replyP, errP
}
}
func ChooseMany[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Out] {
func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Out] {
all := append([]Parser[In, Out]{p}, ps...)
return func(input State[In]) (bool, Result[In, Out], error) {
expecteds := make([][]string, 0, len(all))