Use a lot less MakeState
Test and check / test-and-check (push) Canceled after 0s

This commit is contained in:
b
2026-08-21 23:08:51 -06:00
parent 17b2a0eee5
commit 9b25034a01
4 changed files with 26 additions and 25 deletions
+10 -4
View File
@@ -133,7 +133,8 @@ func MessageEnd(pos uint64, expected ...string) Message {
return Message{pos: pos, got: "end of input", expected: expected}
}
// MakeState creates a new State
// MakeState creates a new State.
// It's mostly useful for testing. You probably don't need it.
func MakeState[In any](r ReaderAt[In]) State[In] {
return State[In]{r: r}
}
@@ -225,10 +226,15 @@ func (p Parser[In, Out]) Map[Out2 any](f func(Out) Out2) Parser[In, Out2] {
})
}
// Run executes a parser on r and returns the result.
func (p Parser[In, Out]) Run(r ReaderAt[In]) (out Out, err error) {
// RunToResult executes a parser on r and returns the result, or an error if reading the inputs fails.
func (p Parser[In, Out]) RunToResult(r ReaderAt[In]) (Result[In, Out], error) {
start := MakeState(r)
result, err := p(start)
return p(start)
}
// Run executes a parser on r and returns the output, or an an error if the parse fails.
func (p Parser[In, Out]) Run(r ReaderAt[In]) (out Out, err error) {
result, err := p.RunToResult(r)
if err != nil {
err = fmt.Errorf("Run: %w", err)
return