Regexp: put pattern in a group to fix associativity

This commit is contained in:
Brandon Dyck 2024-09-17 21:33:39 -06:00
parent 9171809e2b
commit 9673903dd7
2 changed files with 7 additions and 7 deletions

View File

@ -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 Rename Seq2 to Seq
Document Seq Document Seq
Add SPDX tags Add SPDX tags

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"regexp" "regexp"
"strings"
"unicode/utf8" "unicode/utf8"
"git.codemonkeysoftware.net/b/gigaparsec" "git.codemonkeysoftware.net/b/gigaparsec"
@ -56,12 +55,10 @@ func (rr *RuneReader) Count() uint64 {
return rr.cursor.Pos() - rr.start return rr.cursor.Pos() - rr.start
} }
func Regexp(str string) gigaparsec.Parser[byte, string] { func Regexp(pattern string) gigaparsec.Parser[byte, string] {
if !strings.HasPrefix(str, "^") && !strings.HasPrefix(str, `\A`) { pattern = fmt.Sprintf(`^(?:%s)`, pattern)
str = "^" + str re := regexp.MustCompile(pattern)
} expected := fmt.Sprintf("match `%s`", pattern)
re := regexp.MustCompile(str)
expected := fmt.Sprintf("match `%s`", str)
return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, string], error) { return func(input gigaparsec.State[byte]) (gigaparsec.Result[byte, string], error) {
r := NewRuneReader(input.Cursor()) r := NewRuneReader(input.Cursor())
idx := re.FindReaderIndex(r) idx := re.FindReaderIndex(r)