Skip to content

Commit

Permalink
Return the actual symbols in the reversed range error
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored and dlclark committed Jul 17, 2022
1 parent 8fc3b60 commit 304ee33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,3 +1241,14 @@ func TestConcatAccidentalPatternCharge(t *testing.T) {
t.Fatal("Expected non-nil, got nil")
}
}

func TestGoodReverseOrderMessage(t *testing.T) {
_, err := Compile(`[h-c]`, ECMAScript)
if err == nil {
t.Fatal("expected error")
}
expected := "error parsing regexp: [h-c] range in reverse order in `[h-c]`"
if err.Error() != expected {
t.Fatalf("expected %q got %q", expected, err.Error())
}
}
4 changes: 2 additions & 2 deletions syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const (
ErrBadClassInCharRange = "cannot include class \\%v in character range"
ErrUnterminatedBracket = "unterminated [] set"
ErrSubtractionMustBeLast = "a subtraction must be the last element in a character class"
ErrReversedCharRange = "[x-y] range in reverse order"
ErrReversedCharRange = "[%c-%c] range in reverse order"
)

func (e ErrorCode) String() string {
Expand Down Expand Up @@ -1571,7 +1571,7 @@ func (p *parser) scanCharSet(caseInsensitive, scanOnly bool) (*CharSet, error) {
} else {
// a regular range, like a-z
if chPrev > ch {
return nil, p.getErr(ErrReversedCharRange)
return nil, p.getErr(ErrReversedCharRange, chPrev, ch)
}
cc.addRange(chPrev, ch)
}
Expand Down

0 comments on commit 304ee33

Please sign in to comment.