Check that Slice has correct position after matching

This commit is contained in:
Brandon Dyck 2024-09-09 15:00:17 -06:00
parent f75904ca93
commit 8943da5aee
2 changed files with 26 additions and 20 deletions

View File

@ -31,30 +31,27 @@ func (pe ParseError) Error() string {
return fmt.Sprintf("parse error: %d: %s", pe.Pos, pe.Got) return fmt.Sprintf("parse error: %d: %s", pe.Pos, pe.Got)
} }
type State[T any] struct { func MakeState[In any](c cursor.Cursor[In]) State[In] {
cursor cursor.Cursor[T] return State[In]{cursor: c}
} }
func (s State[T]) Cursor() cursor.Cursor[T] { type State[In any] struct {
cursor cursor.Cursor[In]
}
func (s State[In]) Cursor() cursor.Cursor[In] {
return s.cursor return s.cursor
} }
func (s State[T]) Read(dst []T) (n uint64, next State[T], err error) { func (s State[In]) Read(dst []In) (n uint64, next State[In], err error) {
n, c, err := s.cursor.Read(dst) n, c, err := s.cursor.Read(dst)
return n, State[T]{cursor: c}, err return n, State[In]{cursor: c}, err
} }
func (s State[T]) Pos() uint64 { func (s State[In]) Pos() uint64 {
return s.cursor.Pos() return s.cursor.Pos()
} }
func Parse[In, Out any](p Parser[In, Out], c cursor.Cursor[In]) (result Out, err error) {
st := State[In]{cursor: c}
var reply Result[In, Out]
_, reply, err = p(st)
return reply.Value, err
}
type Parser[In, Out any] func(State[In]) (consumed bool, reply Result[In, Out], err error) type Parser[In, Out any] func(State[In]) (consumed bool, reply Result[In, Out], err error)
func Return[In, Out any](value Out) Parser[In, Out] { func Return[In, Out any](value Out) Parser[In, Out] {

View File

@ -27,26 +27,35 @@ func TestSlice(t *testing.T) {
s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s") s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s")
input := rapid.SliceOfN(rapid.Byte(), len(s), -1). input := rapid.SliceOfN(rapid.Byte(), len(s), -1).
Filter(notP(hasPrefix(s))).Draw(t, "input") Filter(notP(hasPrefix(s))).Draw(t, "input")
out, err := gigaparsec.Parse(gigaparsec.Slice(s), cursor.NewSlice(input)) start := gigaparsec.MakeState(cursor.NewSlice(input))
consumed, result, err := gigaparsec.Slice(s)(start)
test.ErrorAs(t, err, &gigaparsec.ParseError{}) test.ErrorAs(t, err, &gigaparsec.ParseError{})
test.SliceEmpty(t, out) test.False(t, consumed, test.Sprint("expected consumed to be false"))
test.SliceEmpty(t, result.Value)
})) }))
t.Run("fails at end of input", rapid.MakeCheck(func(t *rapid.T) { t.Run("fails at end of input", rapid.MakeCheck(func(t *rapid.T) {
s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s") s := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "s")
inputLen := rapid.IntRange(0, len(s)-1).Draw(t, "inputLen") inputLen := rapid.IntRange(0, len(s)-1).Draw(t, "inputLen")
input := s[:inputLen] input := s[:inputLen]
out, err := gigaparsec.Parse(gigaparsec.Slice(s), cursor.NewSlice(input)) start := gigaparsec.MakeState(cursor.NewSlice(input))
consumed, result, err := gigaparsec.Slice(s)(start)
test.ErrorAs(t, err, &gigaparsec.ParseError{}) test.ErrorAs(t, err, &gigaparsec.ParseError{})
test.SliceEmpty(t, out) test.False(t, consumed, test.Sprint("expected consumed to be false"))
test.SliceEmpty(t, result.Value)
})) }))
t.Run("fails when read fails", Todo) t.Run("fails when read fails", Todo)
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")
sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen") sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen")
s := input[:sLen] s := input[:sLen]
out, err := gigaparsec.Parse(gigaparsec.Slice(s), cursor.NewSlice(input)) start := gigaparsec.MakeState(cursor.NewSlice(input))
consumed, result, err := gigaparsec.Slice(s)(start)
test.NoError(t, err) test.NoError(t, err)
test.SliceEqOp(t, s, out) test.True(t, consumed, test.Sprint("expected consumed to be true"))
test.SliceEqOp(t, s, result.Value)
test.EqOp(t, uint64(len(s)), result.State.Pos())
})) }))
t.Run("next state has correct position", Todo)
} }