From b040fd21c39af992d48cc18334ffa9b08d4692a2 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 11 Sep 2024 15:27:10 -0600 Subject: [PATCH] Hide Message fields --- TODO.txt | 1 - bytes/regexp.go | 6 +----- gigaparsec.go | 53 +++++++++++++++++++++++++++++-------------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/TODO.txt b/TODO.txt index 4cd6371..d85353d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,3 +1,2 @@ -Hide Message fields Test RuneReader Test Regexp diff --git a/bytes/regexp.go b/bytes/regexp.go index c371aa0..a4b9498 100644 --- a/bytes/regexp.go +++ b/bytes/regexp.go @@ -72,11 +72,7 @@ func Regexp(str string) gigaparsec.Parser[byte, string] { if err != nil { return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err) } - return gigaparsec.Fail[byte, string](false, gigaparsec.Message{ - Pos: input.Pos(), - Got: string(got), - Expected: []string{expected}, - }), nil + return gigaparsec.Fail[byte, string](false, gigaparsec.MakeMessage(input.Pos(), string(got), expected)), nil } // Alas, this is a little wasteful because a Regexp can only return indices // when searching a RuneReader. diff --git a/gigaparsec.go b/gigaparsec.go index 0538ce3..2f181e6 100644 --- a/gigaparsec.go +++ b/gigaparsec.go @@ -63,20 +63,40 @@ func (r Result[In, Out]) Consume(consumed bool) Result[In, Out] { return r } +func MakeMessage(pos uint64, got string, expected ...string) Message { + return Message{ + pos: pos, + got: got, + expected: expected, + } +} + type Message struct { - Pos uint64 - Got string - Expected []string + pos uint64 + got string + expected []string +} + +func (m Message) PosMethod() uint64 { + return m.pos +} + +func (m Message) GotMethod() string { + return m.got +} + +func (m Message) ExpectedMethod() []string { + return m.expected } func (m Message) expect(s string) Message { - m.Expected = []string{s} + m.expected = []string{s} return m } -func MessageOK(pos uint64) Message { return Message{Pos: pos} } +func MessageOK(pos uint64) Message { return Message{pos: pos} } -func MessageEnd(pos uint64) Message { return Message{Pos: pos, Got: "end of input"} } +func MessageEnd(pos uint64) Message { return Message{pos: pos, got: "end of input"} } func MakeState[In any](c cursor.Cursor[In]) State[In] { return State[In]{cursor: c} @@ -127,10 +147,7 @@ func Satisfy[T any](pred func(T) bool) Parser[T, T] { if pred(token[0]) { return Succeed(true, token[0], next, MessageOK(state.Pos())), nil } - return Fail[T, T](false, Message{ - Pos: state.Pos(), - Got: fmt.Sprint(token), - }), nil + return Fail[T, T](false, MakeMessage(state.Pos(), fmt.Sprint(token))), nil } } @@ -146,11 +163,7 @@ func Slice[T comparable](s []T) Parser[T, []T] { return Result[T, []T]{}, err } if !slices.Equal(s, token) { - return Fail[T, []T](false, Message{ - Pos: state.Pos(), - Got: fmt.Sprint(token), - Expected: []string{expected}, - }), nil + return Fail[T, []T](false, MakeMessage(state.Pos(), fmt.Sprint(token), expected)), nil } return Succeed(true, token, next, MessageOK(state.Pos())), nil } @@ -204,10 +217,10 @@ func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Ou qMsg = msg } if got == "" { - got = qMsg.Got + got = qMsg.got } } - msg := Message{Pos: input.Pos(), Got: got, Expected: slices.Concat(expecteds...)} + msg := MakeMessage(input.Pos(), got, slices.Concat(expecteds...)...) if failed { return Fail[In, Out](false, msg), nil } @@ -261,11 +274,7 @@ func end[In any](s State[In]) (Result[In, struct{}], error) { if err != nil { return Result[In, struct{}]{}, fmt.Errorf("End: unexpected error: %w", err) } - return Fail[In, struct{}](false, Message{ - Pos: s.Pos(), - Got: "", - Expected: []string{"end of input"}, - }), nil + return Fail[In, struct{}](false, MakeMessage(s.Pos(), "", "end of input")), nil } func End[In any]() Parser[In, struct{}] {