Added End parser

This commit is contained in:
Brandon Dyck 2024-09-10 19:00:44 -06:00
parent 3864fad99d
commit c1eae9fa34
2 changed files with 23 additions and 0 deletions

View File

@ -222,3 +222,22 @@ func Map[In, Out1, Out2 any](p Parser[In, Out1], f func(Out1) Out2) Parser[In, O
return Return[In](f(out))
})
}
func End[In any](s State[In]) (consumed bool, reply Result[In, struct{}], err error) {
_, _, err = s.cursor.Read([]In{})
if errors.Is(err, io.EOF) {
reply := Result[In, struct{}]{
State: s,
Message: MessageOK(s.Pos()),
}
return true, reply, nil
}
if err != nil {
return false, Result[In, struct{}]{}, fmt.Errorf("End: unexpected error: %w", err)
}
return false, Result[In, struct{}]{}, ParseError{
Pos: s.Pos(),
Got: "",
Expected: []string{"end of input"},
}
}

View File

@ -90,3 +90,7 @@ func Try(t *testing.T) {
func TestLabel(t *testing.T) {
Todo(t)
}
func TestEnd(t *testing.T) {
Todo(t)
}