Simplified Result

This commit is contained in:
Brandon Dyck 2024-09-24 12:56:10 -06:00
parent 9cab6d266d
commit f8bc7582a5
7 changed files with 142 additions and 207 deletions

150
bind.go
View File

@ -11,8 +11,6 @@ func Bind[In, Out, T any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -20,22 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, val2, next := r2.Succeeded()
return Succeed(anyConsumed, val2, next, MessageOK(s.Pos())), nil return Succeed(anyConsumed, val2, next, MessageOK(s.Pos())), nil
} }
@ -49,8 +45,6 @@ func Bind2[In, Out, T, T2 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -58,33 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, val3, next := r3.Succeeded()
return Succeed(anyConsumed, val3, next, MessageOK(s.Pos())), nil return Succeed(anyConsumed, val3, next, MessageOK(s.Pos())), nil
} }
@ -99,8 +90,6 @@ func Bind3[In, Out, T, T2, T3 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -108,44 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, 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()
failed, _, msg = r4.Failed() if r4.Failed() {
if failed { return Fail[In, Out](anyConsumed, r4.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val4, next, _ := r4.Succeeded() _, val4, next := r4.Succeeded()
return Succeed(anyConsumed, val4, next, MessageOK(s.Pos())), nil return Succeed(anyConsumed, val4, next, MessageOK(s.Pos())), nil
} }
@ -161,8 +146,6 @@ func Bind4[In, Out, T, T2, T3, T4 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -170,55 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, 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()
failed, _, msg = r4.Failed() if r4.Failed() {
if failed { return Fail[In, Out](anyConsumed, r4.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val4, next, _ := r4.Succeeded() _, 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()
failed, _, msg = r5.Failed() if r5.Failed() {
if failed { return Fail[In, Out](anyConsumed, r5.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val5, next, _ := r5.Succeeded() _, val5, next := r5.Succeeded()
return Succeed(anyConsumed, val5, next, MessageOK(s.Pos())), nil return Succeed(anyConsumed, val5, next, MessageOK(s.Pos())), nil
} }
@ -235,8 +213,6 @@ func Bind5[In, Out, T, T2, T3, T4, T5 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -244,66 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, 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()
failed, _, msg = r4.Failed() if r4.Failed() {
if failed { return Fail[In, Out](anyConsumed, r4.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val4, next, _ := r4.Succeeded() _, 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()
failed, _, msg = r5.Failed() if r5.Failed() {
if failed { return Fail[In, Out](anyConsumed, r5.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val5, next, _ := r5.Succeeded() _, 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()
failed, _, msg = r6.Failed() if r6.Failed() {
if failed { return Fail[In, Out](anyConsumed, r6.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val6, next, _ := r6.Succeeded() _, val6, next := r6.Succeeded()
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
} }

View File

@ -30,12 +30,11 @@ 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)
failed, _, msg := result.Failed() must.False(t, result.Failed(), must.Sprint(result.Message()))
must.False(t, failed, must.Sprint(msg)) succeeded, value, _ := result.Succeeded()
succeeded, consumed, value, _, _ := result.Succeeded()
test.True(t, succeeded) test.True(t, succeeded)
test.True(t, consumed)
test.EqOp(t, "a", value) test.EqOp(t, "a", value)
test.True(t, result.Consumed())
}) })
} }
@ -59,9 +58,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)
failed, consumed, _ := result.Failed() test.True(t, result.Failed())
test.True(t, failed) test.False(t, result.Consumed())
test.False(t, 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) {
bgen := rapid.SliceOfN(rapid.Byte(), 1, 100) bgen := rapid.SliceOfN(rapid.Byte(), 1, 100)
@ -71,9 +69,8 @@ func TestMatchString(t *testing.T) {
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)
failed, consumed, _ := result.Failed() must.True(t, result.Failed())
must.True(t, failed) test.False(t, result.Consumed())
test.False(t, 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) {
input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input") input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input")
@ -81,9 +78,9 @@ 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, consumed, value, next, _ := result.Succeeded() succeeded, value, next := result.Succeeded()
must.True(t, succeeded) must.True(t, succeeded)
test.True(t, 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))
})) }))

View File

@ -28,13 +28,8 @@ func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
} }
} }
func (r Result[In, Out]) Failed() (failed, consumed bool, msg Message) { func (r Result[In, Out]) Failed() bool {
failed = !r.succeeded return !r.succeeded
if failed {
consumed = r.consumed
msg = r.message
}
return
} }
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] {
@ -47,13 +42,11 @@ func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message)
} }
} }
func (r Result[In, Out]) Succeeded() (succeeded, 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 succeeded = r.succeeded
if succeeded { if succeeded {
consumed = r.consumed
value = r.value value = r.value
next = r.next next = r.next
msg = r.message
} }
return return
} }
@ -67,6 +60,10 @@ func (r Result[In, Out]) Consume(consumed bool) Result[In, Out] {
return r return r
} }
func (r Result[In, Out]) Message() Message {
return r.message
}
func MakeMessage(pos uint64, got string, expected ...string) Message { func MakeMessage(pos uint64, got string, expected ...string) Message {
return Message{ return Message{
pos: pos, pos: pos,
@ -156,10 +153,10 @@ func (p Parser[In, Out]) Label(label string) Parser[In, Out] {
if err != nil || result.Consumed() { if err != nil || result.Consumed() {
return result, err return result, err
} }
if succeeded, _, value, next, msg := result.Succeeded(); succeeded { msg := result.Message()
if succeeded, value, next := result.Succeeded(); succeeded {
return Succeed(false, value, next, msg.expect(label)), nil return Succeed(false, value, next, msg.expect(label)), nil
} }
_, _, msg := result.Failed()
return Fail[In, Out](false, msg.expect(label)), nil return Fail[In, Out](false, msg.expect(label)), nil
} }
} }
@ -177,11 +174,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 failed, _, msg := result.Failed(); failed { if result.Failed() {
err = ParseError(msg) err = ParseError(result.Message())
return return
} }
_, _, out, _, _ = result.Succeeded() _, out, _ = result.Succeeded()
return return
} }
@ -271,11 +268,12 @@ func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Ou
return result, nil return result, nil
} }
var qMsg Message var qMsg Message
if isFailure, _, msg := result.Failed(); isFailure { msg := result.Message()
if result.Failed() {
qMsg = msg qMsg = msg
failed = true failed = true
} else { } else {
_, _, qValue, _, msg := result.Succeeded() _, qValue, _ := result.Succeeded()
if failed { if failed {
value = qValue value = qValue
failed = false failed = false
@ -305,8 +303,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 failed, _, msg := result.Failed(); failed { if result.Failed() {
return Fail[In, Out](false, msg), nil return Fail[In, Out](false, result.Message()), nil
} }
return result, nil return result, nil
} }
@ -357,14 +355,14 @@ func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
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 failed, _, msg := result.Failed(); failed { if result.Failed() {
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, msg), nil return Fail[In, []Out](consumed, result.Message()), nil
} }
var value Out var value Out
_, _, value, next, _ = result.Succeeded() _, value, next = result.Succeeded()
values = append(values, value) values = append(values, value)
} }
} }

View File

@ -12,8 +12,6 @@ func Bind{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
{{template "fparams" .}}) Parser[In, Out] { {{template "fparams" .}}) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
{{range .Next.Count}} {{range .Next.Count}}
r{{.}}, err := {{if eq . 1}}p(next){{else}}f{{.Prev}}(val{{.Prev}})(next){{end}} r{{.}}, err := {{if eq . 1}}p(next){{else}}f{{.Prev}}(val{{.Prev}})(next){{end}}
@ -21,11 +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()
failed, _, msg = r{{.}}.Failed() if r{{.}}.Failed() {
if failed { return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val{{.}}, next, _ := r{{.}}.Succeeded() _, 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
} }

View File

@ -8,8 +8,6 @@ func Seq{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
{{range .Count}} {{range .Count}}
r{{.}}, err := p{{.}}(next) r{{.}}, err := p{{.}}(next)
@ -17,11 +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()
failed, _, msg = r{{.}}.Failed() if r{{.}}.Failed() {
if failed { return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val{{.}}, next, _ := r{{.}}.Succeeded() _, 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

View File

@ -33,9 +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)
failed, consumed, _ := result.Failed() test.True(t, result.Failed())
test.True(t, failed) test.False(t, result.Consumed())
test.False(t, consumed)
if t.Failed() { if t.Failed() {
t.FailNow() t.FailNow()
} }
@ -58,9 +57,8 @@ func TestSlice(t *testing.T) {
c := ptest.ErrCursor[byte](expectedErr) c := ptest.ErrCursor[byte](expectedErr)
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))
failed, _, _ := result.Failed()
test.ErrorIs(t, err, expectedErr) test.ErrorIs(t, err, expectedErr)
test.True(t, failed) test.True(t, result.Failed())
})) }))
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")
@ -70,9 +68,9 @@ 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, consumed, value, next, _ := result.Succeeded() succeeded, value, next := result.Succeeded()
test.True(t, succeeded) test.True(t, succeeded)
test.True(t, 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())
})) }))

106
seq.go
View File

@ -11,8 +11,6 @@ func Seq2[In, Out, T, T2 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -20,22 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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
@ -50,8 +46,6 @@ func Seq3[In, Out, T, T2, T3 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -59,33 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, 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
@ -101,8 +92,6 @@ func Seq4[In, Out, T, T2, T3, T4 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -110,44 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, 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()
failed, _, msg = r4.Failed() if r4.Failed() {
if failed { return Fail[In, Out](anyConsumed, r4.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val4, next, _ := r4.Succeeded() _, 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
@ -164,8 +149,6 @@ func Seq5[In, Out, T, T2, T3, T4, T5 any](
) Parser[In, Out] { ) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) { return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool var anyConsumed bool
var failed bool
var msg Message
var next = s var next = s
r, err := p(next) r, err := p(next)
@ -173,55 +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()
failed, _, msg = r.Failed() if r.Failed() {
if failed { return Fail[In, Out](anyConsumed, r.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val, next, _ := r.Succeeded() _, 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()
failed, _, msg = r2.Failed() if r2.Failed() {
if failed { return Fail[In, Out](anyConsumed, r2.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val2, next, _ := r2.Succeeded() _, 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()
failed, _, msg = r3.Failed() if r3.Failed() {
if failed { return Fail[In, Out](anyConsumed, r3.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val3, next, _ := r3.Succeeded() _, 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()
failed, _, msg = r4.Failed() if r4.Failed() {
if failed { return Fail[In, Out](anyConsumed, r4.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val4, next, _ := r4.Succeeded() _, 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()
failed, _, msg = r5.Failed() if r5.Failed() {
if failed { return Fail[In, Out](anyConsumed, r5.Message()), nil
return Fail[In, Out](anyConsumed, msg), nil
} }
_, _, val5, next, _ := r5.Succeeded() _, 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