Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 83 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,89 @@ 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)?

re := MustCompile(`(?<\u03C0>a)`, ECMAScript)
names := re.GetGroupNames()
if len(names) != 2 || names[1] != "π" {
t.Fatalf("Group names: %v", names)
}
})

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) {
_, err := Compile(`(?<a>a)(?<a>a)`, ECMAScript)
Copy link
Owner

Choose a reason for hiding this comment

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

Lets make sure the non-ECMAScript behavior is unchanged

if err == nil {
t.Fatal("Expected error")
}
})

}

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 != "0" || groups[0].Index != 0 || groups[0].String() != "baba" {
t.Fatalf("Groups[0]: %v", groups[0])
}

for _, group := range m.Groups() {
t.Log(group.Index, group.Name, group.String())
}
}

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