Skip to content

add explicit error for \ as unicode class #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions regex-syntax/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ 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 represented by a single character is not valid.
UnicodeCharacterClassInvalid,
/// 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 @@ -206,6 +208,7 @@ impl error::Error for Error {
RepetitionCountInvalid => "invalid repetition count range",
RepetitionCountUnclosed => "unclosed counted repetition",
RepetitionMissing => "repetition operator missing expression",
UnicodeCharacterClassInvalid => "invalid unicode character class",
UnsupportedBackreference => "backreferences are not supported",
UnsupportedLookAround => "look-around is not supported",
_ => unreachable!(),
Expand Down Expand Up @@ -293,6 +296,9 @@ impl fmt::Display for ErrorKind {
RepetitionMissing => {
write!(f, "repetition operator missing expression")
}
UnicodeCharacterClassInvalid => {
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::UnicodeCharacterClassInvalid,
));
}
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::UnicodeCharacterClassInvalid,
}
);
assert_eq!(
parser(r"\P\{").parse().unwrap_err(),
TestError {
span: span(2..3),
kind: ast::ErrorKind::UnicodeCharacterClassInvalid,
}
);
}

#[test]
Expand Down