Combine Cursor with State
This commit is contained in:
@ -9,8 +9,6 @@ import (
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
||||
)
|
||||
|
||||
type Result[In, Out any] struct {
|
||||
@ -114,29 +112,33 @@ func MessageEnd(pos uint64, expected ...string) Message {
|
||||
return Message{pos: pos, got: "end of input", expected: expected}
|
||||
}
|
||||
|
||||
func MakeState[In any](c cursor.Cursor[In]) State[In] {
|
||||
return State[In]{cursor: c}
|
||||
type ReaderAt[T any] interface {
|
||||
ReadAt(p []T, off int64) (n int, err error)
|
||||
}
|
||||
|
||||
func MakeState[In any](r ReaderAt[In]) State[In] {
|
||||
return State[In]{r: r}
|
||||
}
|
||||
|
||||
type State[In any] struct {
|
||||
cursor cursor.Cursor[In]
|
||||
}
|
||||
|
||||
func (s State[In]) Cursor() cursor.Cursor[In] {
|
||||
return s.cursor
|
||||
r ReaderAt[In]
|
||||
pos uint64
|
||||
}
|
||||
|
||||
func (s State[In]) Read(dst []In) (n uint64, next State[In], err error) {
|
||||
n, c, err := s.cursor.Read(dst)
|
||||
return n, State[In]{cursor: c}, err
|
||||
nread, err := s.r.ReadAt(dst, int64(s.pos))
|
||||
if n > 0 {
|
||||
s.pos += uint64(n)
|
||||
}
|
||||
return uint64(nread), s, err
|
||||
}
|
||||
|
||||
func (s State[In]) Pos() uint64 {
|
||||
return s.cursor.Pos()
|
||||
return s.pos
|
||||
}
|
||||
|
||||
func (s State[In]) At(pos uint64) State[In] {
|
||||
return State[In]{cursor: s.cursor.At(pos)}
|
||||
return State[In]{r: s.r, pos: pos}
|
||||
}
|
||||
|
||||
type Parser[In, Out any] func(State[In]) (Result[In, Out], error)
|
||||
@ -163,8 +165,8 @@ func (pe ParseError) Error() string {
|
||||
return Message(pe).String()
|
||||
}
|
||||
|
||||
func Run[In, Out any](p Parser[In, Out], c cursor.Cursor[In]) (out Out, err error) {
|
||||
start := MakeState(c)
|
||||
func Run[In, Out any](p Parser[In, Out], r ReaderAt[In]) (out Out, err error) {
|
||||
start := MakeState(r)
|
||||
result, err := p(start)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Run: %w", err)
|
||||
@ -315,7 +317,7 @@ func Map[In, Out1, Out2 any](p Parser[In, Out1], f func(Out1) Out2) Parser[In, O
|
||||
}
|
||||
|
||||
func end[In any](s State[In]) (Result[In, struct{}], error) {
|
||||
_, _, err := s.cursor.Read([]In{})
|
||||
_, _, err := s.Read([]In{})
|
||||
if errors.Is(err, io.EOF) {
|
||||
return Succeed(true, struct{}{}, s, MessageOK(s.Pos())), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user