Combine Cursor with State
This commit is contained in:
@ -11,19 +11,18 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"git.codemonkeysoftware.net/b/gigaparsec"
|
||||
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
||||
)
|
||||
|
||||
// RuneReader is an io.RuneReader backed by a Cursor, for compatibility
|
||||
// with the regexp package.
|
||||
type RuneReader struct {
|
||||
cursor cursor.Cursor[byte]
|
||||
start uint64
|
||||
err error
|
||||
state gigaparsec.State[byte]
|
||||
start uint64
|
||||
err error
|
||||
}
|
||||
|
||||
func NewRuneReader(c cursor.Cursor[byte]) *RuneReader {
|
||||
return &RuneReader{cursor: c, start: c.Pos()}
|
||||
func NewRuneReader(state gigaparsec.State[byte]) *RuneReader {
|
||||
return &RuneReader{state: state, start: state.Pos()}
|
||||
}
|
||||
|
||||
func (rr *RuneReader) ReadRune() (r rune, size int, err error) {
|
||||
@ -32,9 +31,9 @@ func (rr *RuneReader) ReadRune() (r rune, size int, err error) {
|
||||
}()
|
||||
var b [4]byte
|
||||
s := b[:]
|
||||
n, next, err := rr.cursor.Read(s)
|
||||
n, next, err := rr.state.Read(s)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
rr.cursor = next
|
||||
rr.state = next
|
||||
return 0, 0, fmt.Errorf("ReadRune: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
@ -42,12 +41,12 @@ func (rr *RuneReader) ReadRune() (r rune, size int, err error) {
|
||||
}
|
||||
s = s[:n]
|
||||
r, size = utf8.DecodeRune(s)
|
||||
rr.cursor = rr.cursor.At(rr.cursor.Pos() + uint64(size))
|
||||
rr.state = rr.state.At(rr.state.Pos() + uint64(size))
|
||||
return r, size, nil
|
||||
}
|
||||
|
||||
func (rr *RuneReader) Cursor() cursor.Cursor[byte] {
|
||||
return rr.cursor
|
||||
func (rr *RuneReader) State() gigaparsec.State[byte] {
|
||||
return rr.state
|
||||
}
|
||||
|
||||
func (rr *RuneReader) Error() error {
|
||||
@ -55,7 +54,7 @@ func (rr *RuneReader) Error() error {
|
||||
}
|
||||
|
||||
func (rr *RuneReader) Count() uint64 {
|
||||
return rr.cursor.Pos() - rr.start
|
||||
return rr.state.Pos() - rr.start
|
||||
}
|
||||
|
||||
func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
||||
@ -63,7 +62,7 @@ func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
||||
re := regexp.MustCompile(pattern)
|
||||
expected := fmt.Sprintf("match `%s`", pattern)
|
||||
return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, string], error) {
|
||||
r := NewRuneReader(input.Cursor())
|
||||
r := NewRuneReader(input)
|
||||
idx := re.FindReaderIndex(r)
|
||||
err := r.Error()
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
@ -71,7 +70,7 @@ func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
||||
}
|
||||
if idx == nil {
|
||||
got := make([]byte, r.Count())
|
||||
_, _, err = input.Cursor().Read(got)
|
||||
_, _, err = input.Read(got)
|
||||
if err != nil {
|
||||
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err)
|
||||
}
|
||||
@ -80,7 +79,7 @@ func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
||||
// Alas, this is a little wasteful because a Regexp can only return indices
|
||||
// when searching a RuneReader.
|
||||
dst := make([]byte, idx[1]-idx[0])
|
||||
n, _, err := input.Cursor().Read(dst)
|
||||
n, _, err := input.Read(dst)
|
||||
if err != nil {
|
||||
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err)
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
|
||||
"git.codemonkeysoftware.net/b/gigaparsec"
|
||||
pbytes "git.codemonkeysoftware.net/b/gigaparsec/bytes"
|
||||
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
||||
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
|
||||
pgen "git.codemonkeysoftware.net/b/gigaparsec/test/generator"
|
||||
"github.com/shoenig/test"
|
||||
@ -29,7 +28,7 @@ func TestRegexp(t *testing.T) {
|
||||
|
||||
}))
|
||||
t.Run("basically works", func(t *testing.T) {
|
||||
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(cursor.NewReaderAt(strings.NewReader("a"))))
|
||||
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(strings.NewReader("a")))
|
||||
must.NoError(t, err)
|
||||
success, value, _ := result.Status()
|
||||
test.True(t, success, test.Sprint(result.Message()))
|
||||
@ -40,7 +39,7 @@ func TestRegexp(t *testing.T) {
|
||||
|
||||
func TestRuneReader(t *testing.T) {
|
||||
var s = "abcdefghijklmnopqrstuvwxyz"
|
||||
rr := pbytes.NewRuneReader(cursor.NewReaderAt(strings.NewReader(s)))
|
||||
rr := pbytes.NewRuneReader(gigaparsec.MakeState(strings.NewReader(s)))
|
||||
for i, b := range s {
|
||||
r, n, err := rr.ReadRune()
|
||||
test.NoError(t, err)
|
||||
@ -56,7 +55,7 @@ func TestMatchString(t *testing.T) {
|
||||
t.Run("fails on unexpected error", rapid.MakeCheck(func(t *rapid.T) {
|
||||
s := rapid.StringN(-1, -1, 100).Draw(t, "s")
|
||||
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(ptest.ErrReaderAt(readErr)))
|
||||
test.ErrorIs(t, err, readErr)
|
||||
success, _, _ := result.Status()
|
||||
test.False(t, success)
|
||||
@ -68,7 +67,7 @@ func TestMatchString(t *testing.T) {
|
||||
notPrefix := func(b []byte) bool { return !bytes.HasPrefix(input, b) }
|
||||
s := string(bgen.Filter(notPrefix).Draw(t, "s"))
|
||||
|
||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input))))
|
||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(bytes.NewReader(input)))
|
||||
test.NoError(t, err)
|
||||
success, _, _ := result.Status()
|
||||
test.False(t, success)
|
||||
@ -78,7 +77,7 @@ func TestMatchString(t *testing.T) {
|
||||
input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input")
|
||||
slen := rapid.IntRange(0, len(input)).Draw(t, "slen")
|
||||
s := string(input[:slen])
|
||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input))))
|
||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(bytes.NewReader(input)))
|
||||
must.NoError(t, err)
|
||||
success, value, next := result.Status()
|
||||
must.True(t, success)
|
||||
|
Reference in New Issue
Block a user