Get rid of binary Choose
This commit is contained in:
parent
183e4db928
commit
b06454a4bc
@ -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 Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Out] {
|
||||||
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] {
|
|
||||||
all := append([]Parser[In, Out]{p}, ps...)
|
all := append([]Parser[In, Out]{p}, ps...)
|
||||||
return func(input State[In]) (bool, Result[In, Out], error) {
|
return func(input State[In]) (bool, Result[In, Out], error) {
|
||||||
expecteds := make([][]string, 0, len(all))
|
expecteds := make([][]string, 0, len(all))
|
||||||
|
Loading…
Reference in New Issue
Block a user