Hide Message fields

This commit is contained in:
Brandon Dyck 2024-09-11 15:27:10 -06:00
parent 3222d1cfe7
commit b040fd21c3
3 changed files with 32 additions and 28 deletions

View File

@ -1,3 +1,2 @@
Hide Message fields
Test RuneReader Test RuneReader
Test Regexp Test Regexp

View File

@ -72,11 +72,7 @@ func Regexp(str string) gigaparsec.Parser[byte, string] {
if err != nil { if err != nil {
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err) return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err)
} }
return gigaparsec.Fail[byte, string](false, gigaparsec.Message{ return gigaparsec.Fail[byte, string](false, gigaparsec.MakeMessage(input.Pos(), string(got), expected)), nil
Pos: input.Pos(),
Got: string(got),
Expected: []string{expected},
}), nil
} }
// Alas, this is a little wasteful because a Regexp can only return indices // Alas, this is a little wasteful because a Regexp can only return indices
// when searching a RuneReader. // when searching a RuneReader.

View File

@ -63,20 +63,40 @@ func (r Result[In, Out]) Consume(consumed bool) Result[In, Out] {
return r return r
} }
func MakeMessage(pos uint64, got string, expected ...string) Message {
return Message{
pos: pos,
got: got,
expected: expected,
}
}
type Message struct { type Message struct {
Pos uint64 pos uint64
Got string got string
Expected []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 { func (m Message) expect(s string) Message {
m.Expected = []string{s} m.expected = []string{s}
return m 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] { func MakeState[In any](c cursor.Cursor[In]) State[In] {
return State[In]{cursor: c} return State[In]{cursor: c}
@ -127,10 +147,7 @@ func Satisfy[T any](pred func(T) bool) Parser[T, T] {
if pred(token[0]) { if pred(token[0]) {
return Succeed(true, token[0], next, MessageOK(state.Pos())), nil return Succeed(true, token[0], next, MessageOK(state.Pos())), nil
} }
return Fail[T, T](false, Message{ return Fail[T, T](false, MakeMessage(state.Pos(), fmt.Sprint(token))), nil
Pos: state.Pos(),
Got: fmt.Sprint(token),
}), nil
} }
} }
@ -146,11 +163,7 @@ func Slice[T comparable](s []T) Parser[T, []T] {
return Result[T, []T]{}, err return Result[T, []T]{}, err
} }
if !slices.Equal(s, token) { if !slices.Equal(s, token) {
return Fail[T, []T](false, Message{ return Fail[T, []T](false, MakeMessage(state.Pos(), fmt.Sprint(token), expected)), nil
Pos: state.Pos(),
Got: fmt.Sprint(token),
Expected: []string{expected},
}), nil
} }
return Succeed(true, token, next, MessageOK(state.Pos())), 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 qMsg = msg
} }
if got == "" { 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 { if failed {
return Fail[In, Out](false, msg), nil 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 { if err != nil {
return Result[In, struct{}]{}, fmt.Errorf("End: unexpected error: %w", err) return Result[In, struct{}]{}, fmt.Errorf("End: unexpected error: %w", err)
} }
return Fail[In, struct{}](false, Message{ return Fail[In, struct{}](false, MakeMessage(s.Pos(), "", "end of input")), nil
Pos: s.Pos(),
Got: "",
Expected: []string{"end of input"},
}), nil
} }
func End[In any]() Parser[In, struct{}] { func End[In any]() Parser[In, struct{}] {