From ee544cd121767b8af81164bbb447385ee09b1b46 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 11 Sep 2024 14:25:49 -0600 Subject: [PATCH] Make Regexp output a string --- bytes/regexp.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bytes/regexp.go b/bytes/regexp.go index 7b0b7c1..99ad5cc 100644 --- a/bytes/regexp.go +++ b/bytes/regexp.go @@ -48,21 +48,21 @@ func (rr *RuneReader) Error() error { return rr.err } -func Regexp(str string) gigaparsec.Parser[byte, []byte] { +func Regexp(str string) gigaparsec.Parser[byte, string] { if !strings.HasPrefix(str, "^") && !strings.HasPrefix(str, `\A`) { str = "^" + str } re := regexp.MustCompile(str) expected := fmt.Sprintf("match `%s`", str) - return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, []byte], error) { + return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, string], error) { r := NewRuneReader(input.Cursor()) idx := re.FindReaderIndex(r) err := r.Error() if err != nil && !errors.Is(err, io.EOF) { - return gigaparsec.Result[byte, []byte]{}, fmt.Errorf("Regexp: reader error: %w", err) + return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: reader error: %w", err) } if idx == nil { - return gigaparsec.Fail[byte, []byte](false, gigaparsec.Message{ + return gigaparsec.Fail[byte, string](false, gigaparsec.Message{ Pos: input.Pos(), Expected: []string{expected}, // TODO Not having a Got is unsatisfactory, but how do I extract useful information? @@ -76,9 +76,9 @@ func Regexp(str string) gigaparsec.Parser[byte, []byte] { n, _, err := input.Cursor().Read(dst) if err != nil { // If we can't access those same bytes again, something is wrong. - return gigaparsec.Result[byte, []byte]{}, fmt.Errorf("Regexp: unexpected error: %w", err) + return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err) } next := input.At(input.Pos() + n) - return gigaparsec.Succeed(true, dst, next, gigaparsec.MessageOK(input.Pos())), nil + return gigaparsec.Succeed(true, string(dst), next, gigaparsec.MessageOK(input.Pos())), nil } }