Simplified Result
This commit is contained in:
parent
9cab6d266d
commit
f8bc7582a5
150
bind.go
150
bind.go
@ -11,8 +11,6 @@ func Bind[In, Out, T any](
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -20,22 +18,20 @@ func Bind[In, Out, T any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := f(val)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
return Succeed(anyConsumed, val2, next, MessageOK(s.Pos())), nil
|
||||
}
|
||||
@ -49,8 +45,6 @@ func Bind2[In, Out, T, T2 any](
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -58,33 +52,30 @@ func Bind2[In, Out, T, T2 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := f(val)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := f2(val2)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
return Succeed(anyConsumed, val3, next, MessageOK(s.Pos())), nil
|
||||
}
|
||||
@ -99,8 +90,6 @@ func Bind3[In, Out, T, T2, T3 any](
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -108,44 +97,40 @@ func Bind3[In, Out, T, T2, T3 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := f(val)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := f2(val2)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
r4, err := f3(val3)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r4.Consumed()
|
||||
failed, _, msg = r4.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r4.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||
}
|
||||
_, _, val4, next, _ := r4.Succeeded()
|
||||
_, val4, next := r4.Succeeded()
|
||||
|
||||
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] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -170,55 +153,50 @@ func Bind4[In, Out, T, T2, T3, T4 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := f(val)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := f2(val2)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
r4, err := f3(val3)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r4.Consumed()
|
||||
failed, _, msg = r4.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r4.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||
}
|
||||
_, _, val4, next, _ := r4.Succeeded()
|
||||
_, val4, next := r4.Succeeded()
|
||||
|
||||
r5, err := f4(val4)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r5.Consumed()
|
||||
failed, _, msg = r5.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r5.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||
}
|
||||
_, _, val5, next, _ := r5.Succeeded()
|
||||
_, val5, next := r5.Succeeded()
|
||||
|
||||
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] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -244,66 +220,60 @@ func Bind5[In, Out, T, T2, T3, T4, T5 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := f(val)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := f2(val2)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
r4, err := f3(val3)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r4.Consumed()
|
||||
failed, _, msg = r4.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r4.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||
}
|
||||
_, _, val4, next, _ := r4.Succeeded()
|
||||
_, val4, next := r4.Succeeded()
|
||||
|
||||
r5, err := f4(val4)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r5.Consumed()
|
||||
failed, _, msg = r5.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r5.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||
}
|
||||
_, _, val5, next, _ := r5.Succeeded()
|
||||
_, val5, next := r5.Succeeded()
|
||||
|
||||
r6, err := f5(val5)(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r6.Consumed()
|
||||
failed, _, msg = r6.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r6.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||
}
|
||||
_, _, val6, next, _ := r6.Succeeded()
|
||||
_, val6, next := r6.Succeeded()
|
||||
|
||||
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
|
||||
}
|
||||
|
@ -30,12 +30,11 @@ 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)
|
||||
failed, _, msg := result.Failed()
|
||||
must.False(t, failed, must.Sprint(msg))
|
||||
succeeded, consumed, value, _, _ := result.Succeeded()
|
||||
must.False(t, result.Failed(), must.Sprint(result.Message()))
|
||||
succeeded, value, _ := result.Succeeded()
|
||||
test.True(t, succeeded)
|
||||
test.True(t, consumed)
|
||||
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")
|
||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(ptest.ErrReaderAt(readErr))))
|
||||
test.ErrorIs(t, err, readErr)
|
||||
failed, consumed, _ := result.Failed()
|
||||
test.True(t, failed)
|
||||
test.False(t, consumed)
|
||||
test.True(t, result.Failed())
|
||||
test.False(t, result.Consumed())
|
||||
}))
|
||||
t.Run("does not succeed or consume on mismatch", rapid.MakeCheck(func(t *rapid.T) {
|
||||
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)))
|
||||
must.NoError(t, err)
|
||||
failed, consumed, _ := result.Failed()
|
||||
must.True(t, failed)
|
||||
test.False(t, consumed)
|
||||
must.True(t, result.Failed())
|
||||
test.False(t, result.Consumed())
|
||||
}))
|
||||
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")
|
||||
@ -81,9 +78,9 @@ func TestMatchString(t *testing.T) {
|
||||
s := string(input[:slen])
|
||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
||||
must.NoError(t, err)
|
||||
succeeded, consumed, value, next, _ := result.Succeeded()
|
||||
succeeded, value, next := result.Succeeded()
|
||||
must.True(t, succeeded)
|
||||
test.True(t, consumed)
|
||||
test.True(t, result.Consumed())
|
||||
test.EqOp(t, s, value)
|
||||
ptest.StateIsAt(t, next, uint64(slen))
|
||||
}))
|
||||
|
@ -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) {
|
||||
failed = !r.succeeded
|
||||
if failed {
|
||||
consumed = r.consumed
|
||||
msg = r.message
|
||||
}
|
||||
return
|
||||
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] {
|
||||
@ -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
|
||||
if succeeded {
|
||||
consumed = r.consumed
|
||||
value = r.value
|
||||
next = r.next
|
||||
msg = r.message
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -67,6 +60,10 @@ func (r Result[In, Out]) Consume(consumed bool) Result[In, Out] {
|
||||
return r
|
||||
}
|
||||
|
||||
func (r Result[In, Out]) Message() Message {
|
||||
return r.message
|
||||
}
|
||||
|
||||
func MakeMessage(pos uint64, got string, expected ...string) Message {
|
||||
return Message{
|
||||
pos: pos,
|
||||
@ -156,10 +153,10 @@ func (p Parser[In, Out]) Label(label string) Parser[In, Out] {
|
||||
if err != nil || result.Consumed() {
|
||||
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
|
||||
}
|
||||
_, _, msg := result.Failed()
|
||||
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)
|
||||
return
|
||||
}
|
||||
if failed, _, msg := result.Failed(); failed {
|
||||
err = ParseError(msg)
|
||||
if result.Failed() {
|
||||
err = ParseError(result.Message())
|
||||
return
|
||||
}
|
||||
_, _, out, _, _ = result.Succeeded()
|
||||
_, out, _ = result.Succeeded()
|
||||
return
|
||||
}
|
||||
|
||||
@ -271,11 +268,12 @@ func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Ou
|
||||
return result, nil
|
||||
}
|
||||
var qMsg Message
|
||||
if isFailure, _, msg := result.Failed(); isFailure {
|
||||
msg := result.Message()
|
||||
if result.Failed() {
|
||||
qMsg = msg
|
||||
failed = true
|
||||
} else {
|
||||
_, _, qValue, _, msg := result.Succeeded()
|
||||
_, qValue, _ := result.Succeeded()
|
||||
if failed {
|
||||
value = qValue
|
||||
failed = false
|
||||
@ -305,8 +303,8 @@ func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if failed, _, msg := result.Failed(); failed {
|
||||
return Fail[In, Out](false, msg), nil
|
||||
if result.Failed() {
|
||||
return Fail[In, Out](false, result.Message()), 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)
|
||||
}
|
||||
consumed = consumed || result.Consumed()
|
||||
if failed, _, msg := result.Failed(); failed {
|
||||
if result.Failed() {
|
||||
if len(values) >= minCount {
|
||||
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
|
||||
_, _, value, next, _ = result.Succeeded()
|
||||
_, value, next = result.Succeeded()
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
|
@ -12,8 +12,6 @@ func Bind{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
|
||||
{{template "fparams" .}}) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
{{range .Next.Count}}
|
||||
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
|
||||
}
|
||||
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
||||
failed, _, msg = r{{.}}.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r{{.}}.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
|
||||
}
|
||||
_, _, val{{.}}, next, _ := r{{.}}.Succeeded()
|
||||
_, val{{.}}, next := r{{.}}.Succeeded()
|
||||
{{end}}
|
||||
return Succeed(anyConsumed, val{{.Next}}, next, MessageOK(s.Pos())), nil
|
||||
}
|
||||
|
@ -8,8 +8,6 @@ func Seq{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
{{range .Count}}
|
||||
r{{.}}, err := p{{.}}(next)
|
||||
@ -17,11 +15,10 @@ func Seq{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
||||
failed, _, msg = r{{.}}.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r{{.}}.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
|
||||
}
|
||||
_, _, val{{.}}, next, _ := r{{.}}.Succeeded()
|
||||
_, val{{.}}, next := r{{.}}.Succeeded()
|
||||
{{end}}
|
||||
final := f({{range .Count}}{{if ne . 1}}, {{end}}val{{.}}{{end}})
|
||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||
|
@ -33,9 +33,8 @@ func TestSlice(t *testing.T) {
|
||||
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
||||
result, err := p(start)
|
||||
must.NoError(t, err)
|
||||
failed, consumed, _ := result.Failed()
|
||||
test.True(t, failed)
|
||||
test.False(t, consumed)
|
||||
test.True(t, result.Failed())
|
||||
test.False(t, result.Consumed())
|
||||
if t.Failed() {
|
||||
t.FailNow()
|
||||
}
|
||||
@ -58,9 +57,8 @@ func TestSlice(t *testing.T) {
|
||||
c := ptest.ErrCursor[byte](expectedErr)
|
||||
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
|
||||
result, err := gigaparsec.MatchSlice(s)(gigaparsec.MakeState(c))
|
||||
failed, _, _ := result.Failed()
|
||||
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) {
|
||||
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)
|
||||
must.NoError(t, err)
|
||||
succeeded, consumed, value, next, _ := result.Succeeded()
|
||||
succeeded, value, next := result.Succeeded()
|
||||
test.True(t, succeeded)
|
||||
test.True(t, consumed)
|
||||
test.True(t, result.Consumed())
|
||||
test.SliceEqOp(t, s, value)
|
||||
test.EqOp(t, uint64(len(s)), next.Pos())
|
||||
}))
|
||||
|
106
seq.go
106
seq.go
@ -11,8 +11,6 @@ func Seq2[In, Out, T, T2 any](
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -20,22 +18,20 @@ func Seq2[In, Out, T, T2 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := p2(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
final := f(val, val2)
|
||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||
@ -50,8 +46,6 @@ func Seq3[In, Out, T, T2, T3 any](
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -59,33 +53,30 @@ func Seq3[In, Out, T, T2, T3 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := p2(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := p3(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
final := f(val, val2, val3)
|
||||
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] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -110,44 +99,40 @@ func Seq4[In, Out, T, T2, T3, T4 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := p2(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := p3(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
r4, err := p4(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r4.Consumed()
|
||||
failed, _, msg = r4.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r4.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||
}
|
||||
_, _, val4, next, _ := r4.Succeeded()
|
||||
_, val4, next := r4.Succeeded()
|
||||
|
||||
final := f(val, val2, val3, val4)
|
||||
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] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
|
||||
r, err := p(next)
|
||||
@ -173,55 +156,50 @@ func Seq5[In, Out, T, T2, T3, T4, T5 any](
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r.Consumed()
|
||||
failed, _, msg = r.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||
}
|
||||
_, _, val, next, _ := r.Succeeded()
|
||||
_, val, next := r.Succeeded()
|
||||
|
||||
r2, err := p2(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r2.Consumed()
|
||||
failed, _, msg = r2.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r2.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||
}
|
||||
_, _, val2, next, _ := r2.Succeeded()
|
||||
_, val2, next := r2.Succeeded()
|
||||
|
||||
r3, err := p3(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r3.Consumed()
|
||||
failed, _, msg = r3.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r3.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||
}
|
||||
_, _, val3, next, _ := r3.Succeeded()
|
||||
_, val3, next := r3.Succeeded()
|
||||
|
||||
r4, err := p4(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r4.Consumed()
|
||||
failed, _, msg = r4.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r4.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||
}
|
||||
_, _, val4, next, _ := r4.Succeeded()
|
||||
_, val4, next := r4.Succeeded()
|
||||
|
||||
r5, err := p5(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r5.Consumed()
|
||||
failed, _, msg = r5.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
if r5.Failed() {
|
||||
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||
}
|
||||
_, _, val5, next, _ := r5.Succeeded()
|
||||
_, val5, next := r5.Succeeded()
|
||||
|
||||
final := f(val, val2, val3, val4, val5)
|
||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||
|
Loading…
Reference in New Issue
Block a user