From c1eae9fa3490fc71c7557a20c7bab83f99b0450a Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Tue, 10 Sep 2024 19:00:44 -0600 Subject: [PATCH] Added End parser --- gigaparsec.go | 19 +++++++++++++++++++ parser_test.go | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/gigaparsec.go b/gigaparsec.go index 1d2eaf8..5122b95 100644 --- a/gigaparsec.go +++ b/gigaparsec.go @@ -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"}, + } +} diff --git a/parser_test.go b/parser_test.go index 7e4e54d..88f8912 100644 --- a/parser_test.go +++ b/parser_test.go @@ -90,3 +90,7 @@ func Try(t *testing.T) { func TestLabel(t *testing.T) { Todo(t) } + +func TestEnd(t *testing.T) { + Todo(t) +}