Skip to content

Improved compatibility for named capturing groups for ECMA mode #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion match.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func newMatch(regex *Regexp, capcount int, text []rune, startpos int) *Match {
textstart: startpos,
balancing: false,
}
m.Name = "0"
if regex.options|ECMAScript == 0 {
m.Name = "0"
}
m.text = text
m.matches[0] = make([]int, 2)
return &m
Expand Down
101 changes: 101 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,107 @@ func TestECMANamedGroup(t *testing.T) {
}
}

func TestECMAGroupNameUnicode(t *testing.T) {
t.Run("unicode-escape", func(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is an ECMA-script only change can we get an inverse test (i.e. make sure this errors without the ECMAScript flag)?

const RE = `(?<\u03C0>a)`
re := MustCompile(RE, ECMAScript)
names := re.GetGroupNames()
if len(names) != 2 || names[1] != "π" {
t.Fatalf("Group names: %v", names)
}
_, err := Compile(RE, 0)
if err == nil {
t.Fatal("Expected error")
}
})

t.Run("extended-unicode-escape", func(t *testing.T) {
re := MustCompile(`(?<\u{03C0}>a)`, ECMAScript|Unicode)
names := re.GetGroupNames()
if len(names) != 2 || names[1] != "π" {
t.Fatalf("Group names: %v", names)
}
m, err := re.FindStringMatch("bab")
if err != nil {
t.Fatal(err)
}
if m == nil {
t.Fatal("Expected match")
}
if g := m.GroupByName("π"); g != nil {
if s := g.Capture.String(); s != "a" {
t.Fatalf("Group capture != a ('%s')", s)
}
} else {
t.Fatal("No group capture by name")
}
if g := m.GroupByNumber(1); g != nil {
if s := g.Capture.String(); s != "a" {
t.Fatalf("Group capture != a ('%s')", s)
}
} else {
t.Fatal("No group capture by number")
}
})

t.Run("invalid-escape-x", func(t *testing.T) {
_, err := Compile(`(?<\x68>>a)`, ECMAScript)
if err == nil {
t.Fatal("Expected error")
}
})

t.Run("invalid-escape-u", func(t *testing.T) {
_, err := Compile(`(?<\ubob>>a)`, ECMAScript)
if err == nil {
t.Fatal("Expected error")
}
})

t.Run("duplicate-name", func(t *testing.T) {
const RE = `(?<a>a)(?<a>a)`
_, err := Compile(RE, ECMAScript)
if err == nil {
t.Fatal("Expected error")
}
_, err = Compile(RE, 0)
if err != nil {
t.Fatal(err)
}
})

}

func TestECMANamedGroupNumberAssignment(t *testing.T) {
re := MustCompile(`(.)(?<x>a)(?<y>\1)(\k<x>)`, ECMAScript)
m, err := re.FindStringMatch("baba")
if err != nil {
t.Fatal(err)
}
if m == nil {
t.Fatal("Expected match")
}
groups := m.Groups()
if len(groups) != 5 {
t.Fatalf("Groups: %v", groups)
}
if groups[0].Name != "" || groups[0].Index != 0 || groups[0].String() != "baba" {
t.Fatalf("Groups[0]: %v", groups[0])
}
if groups[1].Name != "" || groups[1].Index != 0 || groups[1].String() != "b" {
t.Fatalf("Groups[1]: %v", groups[1])
}
if groups[2].Name != "x" || groups[2].Index != 1 || groups[2].String() != "a" {
t.Fatalf("Groups[2]: %v", groups[1])
}
if groups[3].Name != "y" || groups[3].Index != 2 || groups[3].String() != "b" {
t.Fatalf("Groups[3]: %v", groups[3])
}
if groups[4].Name != "" || groups[4].Index != 3 || groups[4].String() != "a" {
t.Fatalf("Groups[4]: %v", groups[4])
}
}

func TestECMAInvalidEscapeCharClass(t *testing.T) {
re := MustCompile(`[\x0]`, ECMAScript)
if m, err := re.MatchString("x"); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions syntax/charclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ func IsECMAWordChar(r rune) bool {
//return 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z' || '0' <= r && r <= '9' || r == '_'
}

func IsECMAIdentifierStartChar(r rune) bool {
return r == '$' || r == '_' || unicode.In(r, unicode.L, unicode.Nl, unicode.Other_ID_Start)
}

func IsECMAIdentifierChar(r rune) bool {
return IsECMAIdentifierStartChar(r) || r == '\u200C' || r == '\u200D' ||
unicode.In(r, unicode.Mn, unicode.Mc, unicode.Nd, unicode.Pc, unicode.Other_ID_Continue)
}

// SingletonChar will return the char from the first range without validation.
// It assumes you have checked for IsSingleton or IsSingletonInverse and will panic given bad input
func (c CharSet) SingletonChar() rune {
Expand Down
Loading