Renamed Result.Succeeded to Status and removed Failed
This commit is contained in:
parent
f8bc7582a5
commit
9375b51a70
80
bind.go
80
bind.go
@ -18,20 +18,20 @@ func Bind[In, Out, T any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := f(val)(next)
|
r2, err := f(val)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
return Succeed(anyConsumed, val2, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val2, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
@ -52,30 +52,30 @@ func Bind2[In, Out, T, T2 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := f(val)(next)
|
r2, err := f(val)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := f2(val2)(next)
|
r3, err := f2(val2)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
return Succeed(anyConsumed, val3, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val3, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
@ -97,40 +97,40 @@ func Bind3[In, Out, T, T2, T3 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := f(val)(next)
|
r2, err := f(val)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := f2(val2)(next)
|
r3, err := f2(val2)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
r4, err := f3(val3)(next)
|
r4, err := f3(val3)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r4.Consumed()
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
if r4.Failed() {
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
}
|
}
|
||||||
_, val4, next := r4.Succeeded()
|
|
||||||
|
|
||||||
return Succeed(anyConsumed, val4, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val4, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
@ -153,50 +153,50 @@ func Bind4[In, Out, T, T2, T3, T4 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := f(val)(next)
|
r2, err := f(val)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := f2(val2)(next)
|
r3, err := f2(val2)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
r4, err := f3(val3)(next)
|
r4, err := f3(val3)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r4.Consumed()
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
if r4.Failed() {
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
}
|
}
|
||||||
_, val4, next := r4.Succeeded()
|
|
||||||
|
|
||||||
r5, err := f4(val4)(next)
|
r5, err := f4(val4)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r5.Consumed()
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
if r5.Failed() {
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
}
|
}
|
||||||
_, val5, next := r5.Succeeded()
|
|
||||||
|
|
||||||
return Succeed(anyConsumed, val5, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val5, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
@ -220,60 +220,60 @@ func Bind5[In, Out, T, T2, T3, T4, T5 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := f(val)(next)
|
r2, err := f(val)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := f2(val2)(next)
|
r3, err := f2(val2)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
r4, err := f3(val3)(next)
|
r4, err := f3(val3)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r4.Consumed()
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
if r4.Failed() {
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
}
|
}
|
||||||
_, val4, next := r4.Succeeded()
|
|
||||||
|
|
||||||
r5, err := f4(val4)(next)
|
r5, err := f4(val4)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r5.Consumed()
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
if r5.Failed() {
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
}
|
}
|
||||||
_, val5, next := r5.Succeeded()
|
|
||||||
|
|
||||||
r6, err := f5(val5)(next)
|
r6, err := f5(val5)(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r6.Consumed()
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
if r6.Failed() {
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
}
|
}
|
||||||
_, val6, next := r6.Succeeded()
|
|
||||||
|
|
||||||
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
|
@ -30,9 +30,8 @@ func TestRegexp(t *testing.T) {
|
|||||||
t.Run("basically works", func(t *testing.T) {
|
t.Run("basically works", func(t *testing.T) {
|
||||||
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(cursor.NewSlice([]byte("a"))))
|
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(cursor.NewSlice([]byte("a"))))
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
must.False(t, result.Failed(), must.Sprint(result.Message()))
|
success, value, _ := result.Status()
|
||||||
succeeded, value, _ := result.Succeeded()
|
test.True(t, success, test.Sprint(result.Message()))
|
||||||
test.True(t, succeeded)
|
|
||||||
test.EqOp(t, "a", value)
|
test.EqOp(t, "a", value)
|
||||||
test.True(t, result.Consumed())
|
test.True(t, result.Consumed())
|
||||||
})
|
})
|
||||||
@ -58,7 +57,8 @@ func TestMatchString(t *testing.T) {
|
|||||||
readErr := pgen.Error().Draw(t, "readErr")
|
readErr := pgen.Error().Draw(t, "readErr")
|
||||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(ptest.ErrReaderAt(readErr))))
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(ptest.ErrReaderAt(readErr))))
|
||||||
test.ErrorIs(t, err, readErr)
|
test.ErrorIs(t, err, readErr)
|
||||||
test.True(t, result.Failed())
|
success, _, _ := result.Status()
|
||||||
|
test.False(t, success)
|
||||||
test.False(t, result.Consumed())
|
test.False(t, result.Consumed())
|
||||||
}))
|
}))
|
||||||
t.Run("does not succeed or consume on mismatch", rapid.MakeCheck(func(t *rapid.T) {
|
t.Run("does not succeed or consume on mismatch", rapid.MakeCheck(func(t *rapid.T) {
|
||||||
@ -68,8 +68,9 @@ func TestMatchString(t *testing.T) {
|
|||||||
s := string(bgen.Filter(notPrefix).Draw(t, "s"))
|
s := string(bgen.Filter(notPrefix).Draw(t, "s"))
|
||||||
|
|
||||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
||||||
must.NoError(t, err)
|
test.NoError(t, err)
|
||||||
must.True(t, result.Failed())
|
success, _, _ := result.Status()
|
||||||
|
test.False(t, success)
|
||||||
test.False(t, result.Consumed())
|
test.False(t, result.Consumed())
|
||||||
}))
|
}))
|
||||||
t.Run("succeeds with correct value, consumption, and position", rapid.MakeCheck(func(t *rapid.T) {
|
t.Run("succeeds with correct value, consumption, and position", rapid.MakeCheck(func(t *rapid.T) {
|
||||||
@ -78,8 +79,8 @@ func TestMatchString(t *testing.T) {
|
|||||||
s := string(input[:slen])
|
s := string(input[:slen])
|
||||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
succeeded, value, next := result.Succeeded()
|
success, value, next := result.Status()
|
||||||
must.True(t, succeeded)
|
must.True(t, success)
|
||||||
test.True(t, result.Consumed())
|
test.True(t, result.Consumed())
|
||||||
test.EqOp(t, s, value)
|
test.EqOp(t, s, value)
|
||||||
ptest.StateIsAt(t, next, uint64(slen))
|
ptest.StateIsAt(t, next, uint64(slen))
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Result[In, Out any] struct {
|
type Result[In, Out any] struct {
|
||||||
consumed, succeeded bool
|
consumed, success bool
|
||||||
value Out
|
value Out
|
||||||
next State[In]
|
next State[In]
|
||||||
message Message
|
message Message
|
||||||
@ -23,18 +23,14 @@ type Result[In, Out any] struct {
|
|||||||
func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
|
func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
|
||||||
return Result[In, Out]{
|
return Result[In, Out]{
|
||||||
consumed: consumed,
|
consumed: consumed,
|
||||||
succeeded: false,
|
success: false,
|
||||||
message: msg,
|
message: msg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Result[In, Out]) Failed() bool {
|
|
||||||
return !r.succeeded
|
|
||||||
}
|
|
||||||
|
|
||||||
func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message) Result[In, Out] {
|
func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message) Result[In, Out] {
|
||||||
return Result[In, Out]{
|
return Result[In, Out]{
|
||||||
succeeded: true,
|
success: true,
|
||||||
value: value,
|
value: value,
|
||||||
consumed: consumed,
|
consumed: consumed,
|
||||||
next: next,
|
next: next,
|
||||||
@ -42,9 +38,9 @@ func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Result[In, Out]) Succeeded() (succeeded bool, value Out, next State[In]) {
|
func (r Result[In, Out]) Status() (success bool, value Out, next State[In]) {
|
||||||
succeeded = r.succeeded
|
success = r.success
|
||||||
if succeeded {
|
if success {
|
||||||
value = r.value
|
value = r.value
|
||||||
next = r.next
|
next = r.next
|
||||||
}
|
}
|
||||||
@ -154,7 +150,7 @@ func (p Parser[In, Out]) Label(label string) Parser[In, Out] {
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
msg := result.Message()
|
msg := result.Message()
|
||||||
if succeeded, value, next := result.Succeeded(); succeeded {
|
if success, value, next := result.Status(); success {
|
||||||
return Succeed(false, value, next, msg.expect(label)), nil
|
return Succeed(false, value, next, msg.expect(label)), nil
|
||||||
}
|
}
|
||||||
return Fail[In, Out](false, msg.expect(label)), nil
|
return Fail[In, Out](false, msg.expect(label)), nil
|
||||||
@ -174,11 +170,11 @@ func Run[In, Out any](p Parser[In, Out], c cursor.Cursor[In]) (out Out, err erro
|
|||||||
err = fmt.Errorf("Run: %w", err)
|
err = fmt.Errorf("Run: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if result.Failed() {
|
success, out, _ := result.Status()
|
||||||
|
if !success {
|
||||||
err = ParseError(result.Message())
|
err = ParseError(result.Message())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, out, _ = result.Succeeded()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,11 +265,11 @@ func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Ou
|
|||||||
}
|
}
|
||||||
var qMsg Message
|
var qMsg Message
|
||||||
msg := result.Message()
|
msg := result.Message()
|
||||||
if result.Failed() {
|
success, qValue, _ := result.Status()
|
||||||
|
if !success {
|
||||||
qMsg = msg
|
qMsg = msg
|
||||||
failed = true
|
failed = true
|
||||||
} else {
|
} else {
|
||||||
_, qValue, _ := result.Succeeded()
|
|
||||||
if failed {
|
if failed {
|
||||||
value = qValue
|
value = qValue
|
||||||
failed = false
|
failed = false
|
||||||
@ -303,7 +299,8 @@ func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
if result.Failed() {
|
success, _, _ := result.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](false, result.Message()), nil
|
return Fail[In, Out](false, result.Message()), nil
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
@ -350,19 +347,20 @@ func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
|
|||||||
var consumed bool
|
var consumed bool
|
||||||
next := s
|
next := s
|
||||||
for {
|
for {
|
||||||
result, err := p(s)
|
result, err := p(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err)
|
return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err)
|
||||||
}
|
}
|
||||||
consumed = consumed || result.Consumed()
|
consumed = consumed || result.Consumed()
|
||||||
if result.Failed() {
|
var value Out
|
||||||
|
var success bool
|
||||||
|
success, value, next = result.Status()
|
||||||
|
if !success {
|
||||||
if len(values) >= minCount {
|
if len(values) >= minCount {
|
||||||
return Succeed(consumed, values, next, MessageOK(s.Pos())), nil
|
return Succeed(consumed, values, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
return Fail[In, []Out](consumed, result.Message()), nil
|
return Fail[In, []Out](consumed, result.Message()), nil
|
||||||
}
|
}
|
||||||
var value Out
|
|
||||||
_, value, next = result.Succeeded()
|
|
||||||
values = append(values, value)
|
values = append(values, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,10 +19,10 @@ func Bind{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
||||||
if r{{.}}.Failed() {
|
success, val{{.}}, next := r{{.}}.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
|
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
|
||||||
}
|
}
|
||||||
_, val{{.}}, next := r{{.}}.Succeeded()
|
|
||||||
{{end}}
|
{{end}}
|
||||||
return Succeed(anyConsumed, val{{.Next}}, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val{{.Next}}, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,10 @@ func Seq{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
||||||
if r{{.}}.Failed() {
|
success, val{{.}}, next := r{{.}}.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
|
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
|
||||||
}
|
}
|
||||||
_, val{{.}}, next := r{{.}}.Succeeded()
|
|
||||||
{{end}}
|
{{end}}
|
||||||
final := f({{range .Count}}{{if ne . 1}}, {{end}}val{{.}}{{end}})
|
final := f({{range .Count}}{{if ne . 1}}, {{end}}val{{.}}{{end}})
|
||||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
|
@ -33,7 +33,8 @@ func TestSlice(t *testing.T) {
|
|||||||
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
||||||
result, err := p(start)
|
result, err := p(start)
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
test.True(t, result.Failed())
|
success, _, _ := result.Status()
|
||||||
|
test.False(t, success)
|
||||||
test.False(t, result.Consumed())
|
test.False(t, result.Consumed())
|
||||||
if t.Failed() {
|
if t.Failed() {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -58,7 +59,8 @@ func TestSlice(t *testing.T) {
|
|||||||
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
|
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
|
||||||
result, err := gigaparsec.MatchSlice(s)(gigaparsec.MakeState(c))
|
result, err := gigaparsec.MatchSlice(s)(gigaparsec.MakeState(c))
|
||||||
test.ErrorIs(t, err, expectedErr)
|
test.ErrorIs(t, err, expectedErr)
|
||||||
test.True(t, result.Failed())
|
success, _, _ := result.Status()
|
||||||
|
test.False(t, success)
|
||||||
}))
|
}))
|
||||||
t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {
|
t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {
|
||||||
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")
|
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")
|
||||||
@ -68,8 +70,8 @@ func TestSlice(t *testing.T) {
|
|||||||
|
|
||||||
result, err := gigaparsec.MatchSlice(s)(start)
|
result, err := gigaparsec.MatchSlice(s)(start)
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
succeeded, value, next := result.Succeeded()
|
success, value, next := result.Status()
|
||||||
test.True(t, succeeded)
|
test.True(t, success)
|
||||||
test.True(t, result.Consumed())
|
test.True(t, result.Consumed())
|
||||||
test.SliceEqOp(t, s, value)
|
test.SliceEqOp(t, s, value)
|
||||||
test.EqOp(t, uint64(len(s)), next.Pos())
|
test.EqOp(t, uint64(len(s)), next.Pos())
|
||||||
|
56
seq.go
56
seq.go
@ -18,20 +18,20 @@ func Seq2[In, Out, T, T2 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := p2(next)
|
r2, err := p2(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
final := f(val, val2)
|
final := f(val, val2)
|
||||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
@ -53,30 +53,30 @@ func Seq3[In, Out, T, T2, T3 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := p2(next)
|
r2, err := p2(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := p3(next)
|
r3, err := p3(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
final := f(val, val2, val3)
|
final := f(val, val2, val3)
|
||||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
@ -99,40 +99,40 @@ func Seq4[In, Out, T, T2, T3, T4 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := p2(next)
|
r2, err := p2(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := p3(next)
|
r3, err := p3(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
r4, err := p4(next)
|
r4, err := p4(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r4.Consumed()
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
if r4.Failed() {
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
}
|
}
|
||||||
_, val4, next := r4.Succeeded()
|
|
||||||
|
|
||||||
final := f(val, val2, val3, val4)
|
final := f(val, val2, val3, val4)
|
||||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
@ -156,50 +156,50 @@ func Seq5[In, Out, T, T2, T3, T4, T5 any](
|
|||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r.Consumed()
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
if r.Failed() {
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
}
|
}
|
||||||
_, val, next := r.Succeeded()
|
|
||||||
|
|
||||||
r2, err := p2(next)
|
r2, err := p2(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r2.Consumed()
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
if r2.Failed() {
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
}
|
}
|
||||||
_, val2, next := r2.Succeeded()
|
|
||||||
|
|
||||||
r3, err := p3(next)
|
r3, err := p3(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r3.Consumed()
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
if r3.Failed() {
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
}
|
}
|
||||||
_, val3, next := r3.Succeeded()
|
|
||||||
|
|
||||||
r4, err := p4(next)
|
r4, err := p4(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r4.Consumed()
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
if r4.Failed() {
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
}
|
}
|
||||||
_, val4, next := r4.Succeeded()
|
|
||||||
|
|
||||||
r5, err := p5(next)
|
r5, err := p5(next)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result[In, Out]{}, err
|
return Result[In, Out]{}, err
|
||||||
}
|
}
|
||||||
anyConsumed = anyConsumed || r5.Consumed()
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
if r5.Failed() {
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
}
|
}
|
||||||
_, val5, next := r5.Succeeded()
|
|
||||||
|
|
||||||
final := f(val, val2, val3, val4, val5)
|
final := f(val, val2, val3, val4, val5)
|
||||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
|
Loading…
Reference in New Issue
Block a user