Skip to content

Commit

Permalink
syntax: add explicit error for \p\
Browse files Browse the repository at this point in the history
Fixes #594, Closes #622
  • Loading branch information
danieledapo authored and BurntSushi committed Jan 9, 2020
1 parent 6b1539a commit eff5348
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ New features:

Bug fixes:

* [BUG #594](https://github.com/rust-lang/regex/pull/594):
Improve error reporting when writing `\p\`.
* [BUG #633](https://github.com/rust-lang/regex/pull/633):
Squash deprecation warnings for the `std::error::Error::description` method.

Expand Down
7 changes: 7 additions & 0 deletions regex-syntax/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ pub enum ErrorKind {
/// `(?i)*`. It is, however, possible to create a repetition operating on
/// an empty sub-expression. For example, `()*` is still considered valid.
RepetitionMissing,
/// The Unicode class is not valid. This typically occurs when a `\p` is
/// followed by something other than a `{`.
UnicodeClassInvalid,
/// When octal support is disabled, this error is produced when an octal
/// escape is used. The octal escape is assumed to be an invocation of
/// a backreference, which is the common case.
Expand Down Expand Up @@ -208,6 +211,7 @@ impl error::Error for Error {
RepetitionCountInvalid => "invalid repetition count range",
RepetitionCountUnclosed => "unclosed counted repetition",
RepetitionMissing => "repetition operator missing expression",
UnicodeClassInvalid => "invalid Unicode character class",
UnsupportedBackreference => "backreferences are not supported",
UnsupportedLookAround => "look-around is not supported",
_ => unreachable!(),
Expand Down Expand Up @@ -295,6 +299,9 @@ impl fmt::Display for ErrorKind {
RepetitionMissing => {
write!(f, "repetition operator missing expression")
}
UnicodeClassInvalid => {
write!(f, "invalid Unicode character class")
}
UnsupportedBackreference => {
write!(f, "backreferences are not supported")
}
Expand Down
20 changes: 20 additions & 0 deletions regex-syntax/src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,12 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
} else {
let start = self.pos();
let c = self.char();
if c == '\\' {
return Err(self.error(
self.span_char(),
ast::ErrorKind::UnicodeClassInvalid,
));
}
self.bump_and_bump_space();
let kind = ast::ClassUnicodeKind::OneLetter(c);
(start, kind)
Expand Down Expand Up @@ -5713,6 +5719,20 @@ bar
],
}))
);
assert_eq!(
parser(r"\p\{").parse().unwrap_err(),
TestError {
span: span(2..3),
kind: ast::ErrorKind::UnicodeClassInvalid,
}
);
assert_eq!(
parser(r"\P\{").parse().unwrap_err(),
TestError {
span: span(2..3),
kind: ast::ErrorKind::UnicodeClassInvalid,
}
);
}

#[test]
Expand Down

0 comments on commit eff5348

Please sign in to comment.