Consume entire input in sexp parser

This commit is contained in:
Brandon Dyck 2024-09-18 11:25:19 -06:00
parent 2d3c8748be
commit c770c1ca8b
2 changed files with 20 additions and 1 deletions

View File

@ -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)
}

View File

@ -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")