Skip to content

Commit

Permalink
syntax: better error messages for '[\d-a]'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BurntSushi committed Apr 28, 2018
1 parent cf8acc7 commit f7ea409
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions regex-syntax/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub enum ErrorKind {
/// An invalid character class range was found. An invalid range is any
/// range where the start is greater than the end.
ClassRangeInvalid,
/// An invalid range boundary was found in a character class. Range
/// boundaries must be a single literal codepoint, but this error indicates
/// that something else was found, such as a nested class.
ClassRangeLiteral,
/// An opening `[` was found with no corresponding closing `]`.
ClassUnclosed,
/// An empty decimal number was given where one was expected.
Expand Down Expand Up @@ -182,6 +186,7 @@ impl error::Error for Error {
CaptureLimitExceeded => "capture group limit exceeded",
ClassEscapeInvalid => "invalid escape sequence in character class",
ClassRangeInvalid => "invalid character class range",
ClassRangeLiteral => "invalid range boundary, must be a literal",
ClassUnclosed => "unclosed character class",
DecimalEmpty => "empty decimal literal",
DecimalInvalid => "invalid decimal literal",
Expand Down Expand Up @@ -233,6 +238,9 @@ impl fmt::Display for ErrorKind {
write!(f, "invalid character class range, \
the start must be <= the end")
}
ClassRangeLiteral => {
write!(f, "invalid range boundary, must be a literal")
}
ClassUnclosed => {
write!(f, "unclosed character class")
}
Expand Down
6 changes: 3 additions & 3 deletions regex-syntax/src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Primitive {

match self {
Literal(lit) => Ok(lit),
x => Err(p.error(*x.span(), ast::ErrorKind::ClassEscapeInvalid)),
x => Err(p.error(*x.span(), ast::ErrorKind::ClassRangeLiteral)),
}
}
}
Expand Down Expand Up @@ -4677,13 +4677,13 @@ bar
parser(r"[\w-a]").parse().unwrap_err(),
TestError {
span: span(1..3),
kind: ast::ErrorKind::ClassEscapeInvalid,
kind: ast::ErrorKind::ClassRangeLiteral,
});
assert_eq!(
parser(r"[a-\w]").parse().unwrap_err(),
TestError {
span: span(3..5),
kind: ast::ErrorKind::ClassEscapeInvalid,
kind: ast::ErrorKind::ClassRangeLiteral,
});
assert_eq!(
parser(r"[z-a]").parse().unwrap_err(),
Expand Down

0 comments on commit f7ea409

Please sign in to comment.