Regexp: put pattern in a group to fix associativity

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

View File

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