From 9673903dd7090f3801fc783adce18a70059363a0 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Tue, 17 Sep 2024 21:33:39 -0600 Subject: [PATCH] Regexp: put pattern in a group to fix associativity --- TODO.txt | 3 +++ bytes/regexp.go | 11 ++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/TODO.txt b/TODO.txt index f8b1934..d36a81f 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,3 +1,6 @@ +Add a convenient way to run parsers +Add repetition parsers to avoid some recursion +Think about not requiring so much Pos() when making messages Rename Seq2 to Seq Document Seq Add SPDX tags diff --git a/bytes/regexp.go b/bytes/regexp.go index dd3d7d6..aec7c53 100644 --- a/bytes/regexp.go +++ b/bytes/regexp.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "regexp" - "strings" "unicode/utf8" "git.codemonkeysoftware.net/b/gigaparsec" @@ -56,12 +55,10 @@ func (rr *RuneReader) Count() uint64 { return rr.cursor.Pos() - rr.start } -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) +func Regexp(pattern string) gigaparsec.Parser[byte, string] { + pattern = fmt.Sprintf(`^(?:%s)`, pattern) + re := regexp.MustCompile(pattern) + expected := fmt.Sprintf("match `%s`", pattern) return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, string], error) { r := NewRuneReader(input.Cursor()) idx := re.FindReaderIndex(r)