This commit is contained in:
+10
-4
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user