gigaparsec/bind.go

281 lines
6.6 KiB
Go
Raw Normal View History

2024-09-14 00:28:38 +00:00
// GENERATED FILE. DO NOT EDIT.
// SPDX-License-Identifier: Unlicense
2024-09-14 00:28:38 +00:00
package gigaparsec
2024-09-14 01:24:23 +00:00
// Bind combines p with a parser created by f that depends on p's result value.
2024-09-14 00:28:38 +00:00
func Bind[In, Out, T any](
p Parser[In, T],
f func(T) Parser[In, Out],
) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool
var next = s
r, err := p(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
2024-09-24 18:56:10 +00:00
if r.Failed() {
return Fail[In, Out](anyConsumed, r.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val, next := r.Succeeded()
2024-09-14 00:28:38 +00:00
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
2024-09-24 18:56:10 +00:00
if r2.Failed() {
return Fail[In, Out](anyConsumed, r2.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val2, next := r2.Succeeded()
2024-09-14 00:28:38 +00:00
return Succeed(anyConsumed, val2, next, MessageOK(s.Pos())), nil
}
}
2024-09-14 01:24:23 +00:00
// Bind2 is equivalent to 2 nested calls to Bind.
2024-09-14 00:28:38 +00:00
func Bind2[In, Out, T, T2 any](
p Parser[In, T],
f func(T) Parser[In, T2],
f2 func(T2) Parser[In, Out],
) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool
var next = s
r, err := p(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
2024-09-24 18:56:10 +00:00
if r.Failed() {
return Fail[In, Out](anyConsumed, r.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val, next := r.Succeeded()
2024-09-14 00:28:38 +00:00
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
2024-09-24 18:56:10 +00:00
if r2.Failed() {
return Fail[In, Out](anyConsumed, r2.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val2, next := r2.Succeeded()
2024-09-14 00:28:38 +00:00
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
2024-09-24 18:56:10 +00:00
if r3.Failed() {
return Fail[In, Out](anyConsumed, r3.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val3, next := r3.Succeeded()
2024-09-14 00:28:38 +00:00
return Succeed(anyConsumed, val3, next, MessageOK(s.Pos())), nil
}
}
2024-09-14 01:24:23 +00:00
// Bind3 is equivalent to 3 nested calls to Bind.
2024-09-14 00:28:38 +00:00
func Bind3[In, Out, T, T2, T3 any](
p Parser[In, T],
f func(T) Parser[In, T2],
f2 func(T2) Parser[In, T3],
f3 func(T3) Parser[In, Out],
) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool
var next = s
r, err := p(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
2024-09-24 18:56:10 +00:00
if r.Failed() {
return Fail[In, Out](anyConsumed, r.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val, next := r.Succeeded()
2024-09-14 00:28:38 +00:00
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
2024-09-24 18:56:10 +00:00
if r2.Failed() {
return Fail[In, Out](anyConsumed, r2.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val2, next := r2.Succeeded()
2024-09-14 00:28:38 +00:00
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
2024-09-24 18:56:10 +00:00
if r3.Failed() {
return Fail[In, Out](anyConsumed, r3.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val3, next := r3.Succeeded()
2024-09-14 00:28:38 +00:00
r4, err := f3(val3)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
2024-09-24 18:56:10 +00:00
if r4.Failed() {
return Fail[In, Out](anyConsumed, r4.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val4, next := r4.Succeeded()
2024-09-14 00:28:38 +00:00
return Succeed(anyConsumed, val4, next, MessageOK(s.Pos())), nil
}
}
2024-09-14 01:24:23 +00:00
// Bind4 is equivalent to 4 nested calls to Bind.
2024-09-14 00:28:38 +00:00
func Bind4[In, Out, T, T2, T3, T4 any](
p Parser[In, T],
f func(T) Parser[In, T2],
f2 func(T2) Parser[In, T3],
f3 func(T3) Parser[In, T4],
f4 func(T4) Parser[In, Out],
) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool
var next = s
r, err := p(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
2024-09-24 18:56:10 +00:00
if r.Failed() {
return Fail[In, Out](anyConsumed, r.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val, next := r.Succeeded()
2024-09-14 00:28:38 +00:00
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
2024-09-24 18:56:10 +00:00
if r2.Failed() {
return Fail[In, Out](anyConsumed, r2.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val2, next := r2.Succeeded()
2024-09-14 00:28:38 +00:00
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
2024-09-24 18:56:10 +00:00
if r3.Failed() {
return Fail[In, Out](anyConsumed, r3.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val3, next := r3.Succeeded()
2024-09-14 00:28:38 +00:00
r4, err := f3(val3)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
2024-09-24 18:56:10 +00:00
if r4.Failed() {
return Fail[In, Out](anyConsumed, r4.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val4, next := r4.Succeeded()
2024-09-14 00:28:38 +00:00
r5, err := f4(val4)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r5.Consumed()
2024-09-24 18:56:10 +00:00
if r5.Failed() {
return Fail[In, Out](anyConsumed, r5.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val5, next := r5.Succeeded()
2024-09-14 00:28:38 +00:00
return Succeed(anyConsumed, val5, next, MessageOK(s.Pos())), nil
}
}
2024-09-14 01:24:23 +00:00
// Bind5 is equivalent to 5 nested calls to Bind.
2024-09-14 00:28:38 +00:00
func Bind5[In, Out, T, T2, T3, T4, T5 any](
p Parser[In, T],
f func(T) Parser[In, T2],
f2 func(T2) Parser[In, T3],
f3 func(T3) Parser[In, T4],
f4 func(T4) Parser[In, T5],
f5 func(T5) Parser[In, Out],
) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool
var next = s
r, err := p(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
2024-09-24 18:56:10 +00:00
if r.Failed() {
return Fail[In, Out](anyConsumed, r.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val, next := r.Succeeded()
2024-09-14 00:28:38 +00:00
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
2024-09-24 18:56:10 +00:00
if r2.Failed() {
return Fail[In, Out](anyConsumed, r2.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val2, next := r2.Succeeded()
2024-09-14 00:28:38 +00:00
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
2024-09-24 18:56:10 +00:00
if r3.Failed() {
return Fail[In, Out](anyConsumed, r3.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val3, next := r3.Succeeded()
2024-09-14 00:28:38 +00:00
r4, err := f3(val3)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
2024-09-24 18:56:10 +00:00
if r4.Failed() {
return Fail[In, Out](anyConsumed, r4.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val4, next := r4.Succeeded()
2024-09-14 00:28:38 +00:00
r5, err := f4(val4)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r5.Consumed()
2024-09-24 18:56:10 +00:00
if r5.Failed() {
return Fail[In, Out](anyConsumed, r5.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val5, next := r5.Succeeded()
2024-09-14 00:28:38 +00:00
r6, err := f5(val5)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r6.Consumed()
2024-09-24 18:56:10 +00:00
if r6.Failed() {
return Fail[In, Out](anyConsumed, r6.Message()), nil
2024-09-14 00:28:38 +00:00
}
2024-09-24 18:56:10 +00:00
_, val6, next := r6.Succeeded()
2024-09-14 00:28:38 +00:00
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
}
}