Skip to content

Commit f7ea409

Browse files
committed
syntax: better error messages for '[\d-a]'
This commit adds a new type of error message that is used whenever a character class escape sequence is used as the start or end of a character class range. Fixes #461
1 parent cf8acc7 commit f7ea409

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

regex-syntax/src/ast/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ pub enum ErrorKind {
8989
/// An invalid character class range was found. An invalid range is any
9090
/// range where the start is greater than the end.
9191
ClassRangeInvalid,
92+
/// An invalid range boundary was found in a character class. Range
93+
/// boundaries must be a single literal codepoint, but this error indicates
94+
/// that something else was found, such as a nested class.
95+
ClassRangeLiteral,
9296
/// An opening `[` was found with no corresponding closing `]`.
9397
ClassUnclosed,
9498
/// An empty decimal number was given where one was expected.
@@ -182,6 +186,7 @@ impl error::Error for Error {
182186
CaptureLimitExceeded => "capture group limit exceeded",
183187
ClassEscapeInvalid => "invalid escape sequence in character class",
184188
ClassRangeInvalid => "invalid character class range",
189+
ClassRangeLiteral => "invalid range boundary, must be a literal",
185190
ClassUnclosed => "unclosed character class",
186191
DecimalEmpty => "empty decimal literal",
187192
DecimalInvalid => "invalid decimal literal",
@@ -233,6 +238,9 @@ impl fmt::Display for ErrorKind {
233238
write!(f, "invalid character class range, \
234239
the start must be <= the end")
235240
}
241+
ClassRangeLiteral => {
242+
write!(f, "invalid range boundary, must be a literal")
243+
}
236244
ClassUnclosed => {
237245
write!(f, "unclosed character class")
238246
}

regex-syntax/src/ast/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Primitive {
9595

9696
match self {
9797
Literal(lit) => Ok(lit),
98-
x => Err(p.error(*x.span(), ast::ErrorKind::ClassEscapeInvalid)),
98+
x => Err(p.error(*x.span(), ast::ErrorKind::ClassRangeLiteral)),
9999
}
100100
}
101101
}
@@ -4677,13 +4677,13 @@ bar
46774677
parser(r"[\w-a]").parse().unwrap_err(),
46784678
TestError {
46794679
span: span(1..3),
4680-
kind: ast::ErrorKind::ClassEscapeInvalid,
4680+
kind: ast::ErrorKind::ClassRangeLiteral,
46814681
});
46824682
assert_eq!(
46834683
parser(r"[a-\w]").parse().unwrap_err(),
46844684
TestError {
46854685
span: span(3..5),
4686-
kind: ast::ErrorKind::ClassEscapeInvalid,
4686+
kind: ast::ErrorKind::ClassRangeLiteral,
46874687
});
46884688
assert_eq!(
46894689
parser(r"[z-a]").parse().unwrap_err(),

0 commit comments

Comments
 (0)