Skip to content

Commit c59be96

Browse files
danieledapoBurntSushi
authored andcommitted
syntax: add explicit error for \p\
Fixes #594, Closes #622
1 parent 2909aee commit c59be96

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ New features:
99

1010
Bug fixes:
1111

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

regex-syntax/src/ast/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ pub enum ErrorKind {
156156
/// `(?i)*`. It is, however, possible to create a repetition operating on
157157
/// an empty sub-expression. For example, `()*` is still considered valid.
158158
RepetitionMissing,
159+
/// The Unicode class is not valid. This typically occurs when a `\p` is
160+
/// followed by something other than a `{`.
161+
UnicodeClassInvalid,
159162
/// When octal support is disabled, this error is produced when an octal
160163
/// escape is used. The octal escape is assumed to be an invocation of
161164
/// a backreference, which is the common case.
@@ -208,6 +211,7 @@ impl error::Error for Error {
208211
RepetitionCountInvalid => "invalid repetition count range",
209212
RepetitionCountUnclosed => "unclosed counted repetition",
210213
RepetitionMissing => "repetition operator missing expression",
214+
UnicodeClassInvalid => "invalid Unicode character class",
211215
UnsupportedBackreference => "backreferences are not supported",
212216
UnsupportedLookAround => "look-around is not supported",
213217
_ => unreachable!(),
@@ -295,6 +299,9 @@ impl fmt::Display for ErrorKind {
295299
RepetitionMissing => {
296300
write!(f, "repetition operator missing expression")
297301
}
302+
UnicodeClassInvalid => {
303+
write!(f, "invalid Unicode character class")
304+
}
298305
UnsupportedBackreference => {
299306
write!(f, "backreferences are not supported")
300307
}

regex-syntax/src/ast/parse.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,12 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
20952095
} else {
20962096
let start = self.pos();
20972097
let c = self.char();
2098+
if c == '\\' {
2099+
return Err(self.error(
2100+
self.span_char(),
2101+
ast::ErrorKind::UnicodeClassInvalid,
2102+
));
2103+
}
20982104
self.bump_and_bump_space();
20992105
let kind = ast::ClassUnicodeKind::OneLetter(c);
21002106
(start, kind)
@@ -5713,6 +5719,20 @@ bar
57135719
],
57145720
}))
57155721
);
5722+
assert_eq!(
5723+
parser(r"\p\{").parse().unwrap_err(),
5724+
TestError {
5725+
span: span(2..3),
5726+
kind: ast::ErrorKind::UnicodeClassInvalid,
5727+
}
5728+
);
5729+
assert_eq!(
5730+
parser(r"\P\{").parse().unwrap_err(),
5731+
TestError {
5732+
span: span(2..3),
5733+
kind: ast::ErrorKind::UnicodeClassInvalid,
5734+
}
5735+
);
57165736
}
57175737

57185738
#[test]

0 commit comments

Comments
 (0)