diff --git a/csexp/sexp.go b/csexp/sexp.go index 6f696e9..9876293 100644 --- a/csexp/sexp.go +++ b/csexp/sexp.go @@ -147,7 +147,12 @@ func parseList(input gigaparsec.State[byte]) (gigaparsec.Result[byte, Sexp], err } func Parse(data []byte) (Sexp, error) { - result, err := parseSexp(gigaparsec.MakeState(cursor.NewSlice(data))) + parser := gigaparsec.Seq2( + parseSexp, + gigaparsec.End[byte](), + func(s Sexp, _ struct{}) Sexp { return s }, + ) + result, err := parser(gigaparsec.MakeState(cursor.NewSlice(data))) if err != nil { return nil, fmt.Errorf("csexp.Parse: %w", err) } diff --git a/csexp/sexp_test.go b/csexp/sexp_test.go index a8126b7..851225e 100644 --- a/csexp/sexp_test.go +++ b/csexp/sexp_test.go @@ -6,6 +6,7 @@ import ( "git.codemonkeysoftware.net/b/peachy-go/csexp" "git.codemonkeysoftware.net/b/peachy-go/csexp/gen" + "github.com/shoenig/test" "github.com/shoenig/test/must" "pgregory.net/rapid" ) @@ -33,6 +34,19 @@ func TestStringAndParseEqual(t *testing.T) { }) } +func TestParseFails(t *testing.T) { + t.Run("with extra input", rapid.MakeCheck(func(t *rapid.T) { + sexp := gen.Sexp().Draw(t, "sexp") + b := []byte(sexp.String()) + extra := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "extra") + b = append(b, extra...) + parsed, err := csexp.Parse(b) + test.Error(t, err) + test.Nil(t, parsed) + })) + t.Run("with too little input", rapid.MakeCheck(func(t *rapid.T) {})) +} + func TestCloneEqual(t *testing.T) { rapid.Check(t, func(t *rapid.T) { sexp := gen.Sexp().Draw(t, "sexp")