Renamed Result.Succeeded to Status and removed Failed

This commit is contained in:
Brandon Dyck 2024-09-24 13:16:30 -06:00
parent f8bc7582a5
commit 9375b51a70
7 changed files with 114 additions and 113 deletions

80
bind.go
View File

@ -18,20 +18,20 @@ func Bind[In, Out, T any](
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
r4, err := f3(val3)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
if r4.Failed() {
success, val4, next := r4.Status()
if !success {
return Fail[In, Out](anyConsumed, r4.Message()), nil
}
_, val4, next := r4.Succeeded()
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
r4, err := f3(val3)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
if r4.Failed() {
success, val4, next := r4.Status()
if !success {
return Fail[In, Out](anyConsumed, r4.Message()), nil
}
_, val4, next := r4.Succeeded()
r5, err := f4(val4)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r5.Consumed()
if r5.Failed() {
success, val5, next := r5.Status()
if !success {
return Fail[In, Out](anyConsumed, r5.Message()), nil
}
_, val5, next := r5.Succeeded()
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := f(val)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := f2(val2)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
r4, err := f3(val3)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
if r4.Failed() {
success, val4, next := r4.Status()
if !success {
return Fail[In, Out](anyConsumed, r4.Message()), nil
}
_, val4, next := r4.Succeeded()
r5, err := f4(val4)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r5.Consumed()
if r5.Failed() {
success, val5, next := r5.Status()
if !success {
return Fail[In, Out](anyConsumed, r5.Message()), nil
}
_, val5, next := r5.Succeeded()
r6, err := f5(val5)(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r6.Consumed()
if r6.Failed() {
success, val6, next := r6.Status()
if !success {
return Fail[In, Out](anyConsumed, r6.Message()), nil
}
_, val6, next := r6.Succeeded()
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
}

View File

@ -30,9 +30,8 @@ func TestRegexp(t *testing.T) {
t.Run("basically works", func(t *testing.T) {
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(cursor.NewSlice([]byte("a"))))
must.NoError(t, err)
must.False(t, result.Failed(), must.Sprint(result.Message()))
succeeded, value, _ := result.Succeeded()
test.True(t, succeeded)
success, value, _ := result.Status()
test.True(t, success, test.Sprint(result.Message()))
test.EqOp(t, "a", value)
test.True(t, result.Consumed())
})
@ -58,7 +57,8 @@ func TestMatchString(t *testing.T) {
readErr := pgen.Error().Draw(t, "readErr")
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(ptest.ErrReaderAt(readErr))))
test.ErrorIs(t, err, readErr)
test.True(t, result.Failed())
success, _, _ := result.Status()
test.False(t, success)
test.False(t, result.Consumed())
}))
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"))
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
must.NoError(t, err)
must.True(t, result.Failed())
test.NoError(t, err)
success, _, _ := result.Status()
test.False(t, success)
test.False(t, result.Consumed())
}))
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])
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
must.NoError(t, err)
succeeded, value, next := result.Succeeded()
must.True(t, succeeded)
success, value, next := result.Status()
must.True(t, success)
test.True(t, result.Consumed())
test.EqOp(t, s, value)
ptest.StateIsAt(t, next, uint64(slen))

View File

@ -14,7 +14,7 @@ import (
)
type Result[In, Out any] struct {
consumed, succeeded bool
consumed, success bool
value Out
next State[In]
message Message
@ -23,18 +23,14 @@ type Result[In, Out any] struct {
func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
return Result[In, Out]{
consumed: consumed,
succeeded: false,
success: false,
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] {
return Result[In, Out]{
succeeded: true,
success: true,
value: value,
consumed: consumed,
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]) {
succeeded = r.succeeded
if succeeded {
func (r Result[In, Out]) Status() (success bool, value Out, next State[In]) {
success = r.success
if success {
value = r.value
next = r.next
}
@ -154,7 +150,7 @@ func (p Parser[In, Out]) Label(label string) Parser[In, Out] {
return result, err
}
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 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)
return
}
if result.Failed() {
success, out, _ := result.Status()
if !success {
err = ParseError(result.Message())
return
}
_, out, _ = result.Succeeded()
return
}
@ -269,11 +265,11 @@ func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Ou
}
var qMsg Message
msg := result.Message()
if result.Failed() {
success, qValue, _ := result.Status()
if !success {
qMsg = msg
failed = true
} else {
_, qValue, _ := result.Succeeded()
if failed {
value = qValue
failed = false
@ -303,7 +299,8 @@ func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
if err != nil {
return result, err
}
if result.Failed() {
success, _, _ := result.Status()
if !success {
return Fail[In, Out](false, result.Message()), 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
next := s
for {
result, err := p(s)
result, err := p(next)
if err != nil {
return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err)
}
consumed = consumed || result.Consumed()
if result.Failed() {
var value Out
var success bool
success, value, next = result.Status()
if !success {
if len(values) >= minCount {
return Succeed(consumed, values, next, MessageOK(s.Pos())), nil
}
return Fail[In, []Out](consumed, result.Message()), nil
}
var value Out
_, value, next = result.Succeeded()
values = append(values, value)
}
}

View File

@ -19,10 +19,10 @@ func Bind{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r{{.}}.Consumed()
if r{{.}}.Failed() {
success, val{{.}}, next := r{{.}}.Status()
if !success {
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
}
_, val{{.}}, next := r{{.}}.Succeeded()
{{end}}
return Succeed(anyConsumed, val{{.Next}}, next, MessageOK(s.Pos())), nil
}

View File

@ -15,10 +15,10 @@ func Seq{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r{{.}}.Consumed()
if r{{.}}.Failed() {
success, val{{.}}, next := r{{.}}.Status()
if !success {
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
}
_, val{{.}}, next := r{{.}}.Succeeded()
{{end}}
final := f({{range .Count}}{{if ne . 1}}, {{end}}val{{.}}{{end}})
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil

View File

@ -33,7 +33,8 @@ func TestSlice(t *testing.T) {
start := gigaparsec.MakeState(cursor.NewSlice(input))
result, err := p(start)
must.NoError(t, err)
test.True(t, result.Failed())
success, _, _ := result.Status()
test.False(t, success)
test.False(t, result.Consumed())
if t.Failed() {
t.FailNow()
@ -58,7 +59,8 @@ func TestSlice(t *testing.T) {
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
result, err := gigaparsec.MatchSlice(s)(gigaparsec.MakeState(c))
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) {
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)
must.NoError(t, err)
succeeded, value, next := result.Succeeded()
test.True(t, succeeded)
success, value, next := result.Status()
test.True(t, success)
test.True(t, result.Consumed())
test.SliceEqOp(t, s, value)
test.EqOp(t, uint64(len(s)), next.Pos())

56
seq.go
View File

@ -18,20 +18,20 @@ func Seq2[In, Out, T, T2 any](
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := p2(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
final := f(val, val2)
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := p2(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := p3(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
final := f(val, val2, val3)
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := p2(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := p3(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
r4, err := p4(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
if r4.Failed() {
success, val4, next := r4.Status()
if !success {
return Fail[In, Out](anyConsumed, r4.Message()), nil
}
_, val4, next := r4.Succeeded()
final := f(val, val2, val3, val4)
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
}
anyConsumed = anyConsumed || r.Consumed()
if r.Failed() {
success, val, next := r.Status()
if !success {
return Fail[In, Out](anyConsumed, r.Message()), nil
}
_, val, next := r.Succeeded()
r2, err := p2(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r2.Consumed()
if r2.Failed() {
success, val2, next := r2.Status()
if !success {
return Fail[In, Out](anyConsumed, r2.Message()), nil
}
_, val2, next := r2.Succeeded()
r3, err := p3(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r3.Consumed()
if r3.Failed() {
success, val3, next := r3.Status()
if !success {
return Fail[In, Out](anyConsumed, r3.Message()), nil
}
_, val3, next := r3.Succeeded()
r4, err := p4(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r4.Consumed()
if r4.Failed() {
success, val4, next := r4.Status()
if !success {
return Fail[In, Out](anyConsumed, r4.Message()), nil
}
_, val4, next := r4.Succeeded()
r5, err := p5(next)
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r5.Consumed()
if r5.Failed() {
success, val5, next := r5.Status()
if !success {
return Fail[In, Out](anyConsumed, r5.Message()), nil
}
_, val5, next := r5.Succeeded()
final := f(val, val2, val3, val4, val5)
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil