From f7ea409880702d7821fab134c3eab04eccb5b166 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 28 Apr 2018 09:50:25 -0400 Subject: [PATCH] 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 --- regex-syntax/src/ast/mod.rs | 8 ++++++++ regex-syntax/src/ast/parse.rs | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/regex-syntax/src/ast/mod.rs b/regex-syntax/src/ast/mod.rs index ad63a1b491..f4f732ec37 100644 --- a/regex-syntax/src/ast/mod.rs +++ b/regex-syntax/src/ast/mod.rs @@ -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. @@ -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", @@ -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") } diff --git a/regex-syntax/src/ast/parse.rs b/regex-syntax/src/ast/parse.rs index aead94757b..8d11f7c49b 100644 --- a/regex-syntax/src/ast/parse.rs +++ b/regex-syntax/src/ast/parse.rs @@ -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)), } } } @@ -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(),