Skip to main content

fancy_regex/
error.rs

1use alloc::boxed::Box;
2use alloc::string::String;
3use core::fmt;
4use regex_automata::meta::BuildError as RaBuildError;
5
6/// Result type for this crate with specific error enum.
7pub type Result<T> = ::core::result::Result<T, Error>;
8
9pub type ParseErrorPosition = usize;
10
11/// An error as the result of parsing, compiling or running a regex.
12#[derive(Clone, Debug)]
13#[non_exhaustive]
14pub enum Error {
15    /// An error as a result of parsing a regex pattern, with the position where the error occurred
16    ParseError(ParseErrorPosition, ParseError),
17    /// An error as a result of compiling a regex
18    CompileError(Box<CompileError>),
19    /// An error as a result of running a regex
20    RuntimeError(RuntimeError),
21}
22
23/// An error for the result of parsing a regex pattern.
24#[derive(Clone, Debug)]
25#[non_exhaustive]
26pub enum ParseError {
27    /// General parsing error
28    GeneralParseError(String),
29    /// Opening parenthesis without closing parenthesis, e.g. `(a|b`
30    UnclosedOpenParen,
31    /// Invalid repeat syntax
32    InvalidRepeat,
33    /// Pattern too deeply nested
34    RecursionExceeded,
35    /// Backslash without following character
36    TrailingBackslash,
37    /// Invalid escape
38    InvalidEscape(String),
39    /// Unicode escape not closed
40    UnclosedUnicodeName,
41    /// Invalid hex escape
42    InvalidHex,
43    /// Invalid codepoint for hex or unicode escape
44    InvalidCodepointValue,
45    /// Invalid character class
46    InvalidClass,
47    /// Unknown group flag
48    UnknownFlag(String),
49    /// Disabling Unicode not supported
50    NonUnicodeUnsupported,
51    /// Invalid back reference
52    InvalidBackref,
53    /// Quantifier on lookaround or other zero-width assertion
54    TargetNotRepeatable,
55    /// Couldn't parse group name
56    InvalidGroupName,
57    /// Invalid group id in escape sequence
58    InvalidGroupNameBackref(String),
59}
60
61/// An error as the result of compiling a regex.
62#[derive(Clone, Debug)]
63#[non_exhaustive]
64pub enum CompileError {
65    /// Regex crate error
66    InnerError(RaBuildError),
67    /// Look-behind assertion without constant size
68    LookBehindNotConst,
69    /// Variable-length lookbehind requires feature flag
70    VariableLookBehindRequiresFeature,
71    /// Error building reverse DFA for variable-length lookbehind
72    DfaBuildError(String),
73    /// Couldn't parse group name
74    InvalidGroupName,
75    /// Invalid group id in escape sequence
76    InvalidGroupNameBackref(String),
77    /// Invalid back reference
78    InvalidBackref(usize),
79    /// Once named groups are used you cannot refer to groups by number
80    NamedBackrefOnly,
81    /// Feature not supported yet
82    FeatureNotYetSupported(String),
83    /// Subroutine call to non-existent group
84    SubroutineCallTargetNotFound(String, usize),
85    /// Left-recursive subroutine call detected
86    LeftRecursiveSubroutineCall(String),
87    /// Unbounded recursive subroutine call detected
88    NeverEndingRecursion,
89    /// Unexpected general error
90    UnexpectedGeneralError(String),
91    /// The pattern combined with the active options can never produce a match. For example,
92    /// setting `find_not_empty` on a pattern whose analysis shows it can only ever produce
93    /// zero-length matches.
94    PatternCanNeverMatch,
95    /// An unresolved AST node was encountered during analysis (internal error)
96    UnresolvedAstNode(usize, String),
97}
98
99/// An error as the result of executing a regex.
100#[derive(Clone, Debug)]
101#[non_exhaustive]
102pub enum RuntimeError {
103    /// Max stack size exceeded for backtracking while executing regex.
104    StackOverflow,
105    /// Max limit for backtracking count exceeded while executing the regex.
106    /// Configure using
107    /// [`RegexBuilder::backtrack_limit`](struct.RegexBuilder.html#method.backtrack_limit).
108    BacktrackLimitExceeded,
109}
110
111#[cfg(feature = "std")]
112impl ::std::error::Error for Error {}
113
114impl fmt::Display for ParseError {
115    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116        match self {
117            ParseError::GeneralParseError(s) => write!(f, "General parsing error: {}", s),
118            ParseError::UnclosedOpenParen => {
119                write!(f, "Opening parenthesis without closing parenthesis")
120            }
121            ParseError::InvalidRepeat => write!(f, "Invalid repeat syntax"),
122            ParseError::RecursionExceeded => write!(f, "Pattern too deeply nested"),
123            ParseError::TrailingBackslash => write!(f, "Backslash without following character"),
124            ParseError::InvalidEscape(s) => write!(f, "Invalid escape: {}", s),
125            ParseError::UnclosedUnicodeName => write!(f, "Unicode escape not closed"),
126            ParseError::InvalidHex => write!(f, "Invalid hex escape"),
127            ParseError::InvalidCodepointValue => {
128                write!(f, "Invalid codepoint for hex or unicode escape")
129            }
130            ParseError::InvalidClass => write!(f, "Invalid character class"),
131            ParseError::UnknownFlag(s) => write!(f, "Unknown group flag: {}", s),
132            ParseError::NonUnicodeUnsupported => write!(f, "Disabling Unicode not supported"),
133            ParseError::InvalidBackref => write!(f, "Invalid back reference"),
134            ParseError::InvalidGroupName => write!(f, "Could not parse group name"),
135            ParseError::InvalidGroupNameBackref(s) => {
136                write!(f, "Invalid group name in back reference: {}", s)
137            }
138            ParseError::TargetNotRepeatable => write!(f, "Target of repeat operator is invalid"),
139        }
140    }
141}
142
143impl fmt::Display for CompileError {
144    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145        match self {
146            CompileError::InnerError(e) => write!(f, "Regex error: {}", e),
147            CompileError::LookBehindNotConst => {
148                write!(f, "Look-behind assertion without constant size")
149            },
150            CompileError::VariableLookBehindRequiresFeature => {
151                write!(f, "Variable-length lookbehind requires the 'variable-lookbehinds' feature")
152            },
153            CompileError::DfaBuildError(s) => {
154                write!(f, "Failed to build reverse DFA for variable-length lookbehind: {}", s)
155            },
156            CompileError::InvalidGroupName => write!(f, "Could not parse group name"),
157            CompileError::InvalidGroupNameBackref(s) => write!(f, "Invalid group name in back reference: {}", s),
158            CompileError::InvalidBackref(g) => write!(f, "Invalid back reference to group {}", g),
159            CompileError::NamedBackrefOnly => write!(f, "Numbered backref/call not allowed because named group was used, use a named backref instead"),
160            CompileError::FeatureNotYetSupported(s) => write!(f, "Regex uses currently unimplemented feature: {}", s),
161            CompileError::SubroutineCallTargetNotFound(s, ix) => {
162                write!(f, "Subroutine call target not found at position {}: {}", ix, s)
163            }
164            CompileError::LeftRecursiveSubroutineCall(s) => {
165                write!(f, "Left-recursive subroutine call detected: {}", s)
166            }
167            CompileError::NeverEndingRecursion => {
168                write!(f, "Never-ending recursive subroutine call detected")
169            }
170            CompileError::UnexpectedGeneralError(s) => {
171                write!(f, "Unexpected general compilation error: {}", s)
172            }
173            CompileError::PatternCanNeverMatch => {
174                write!(f, "Pattern can never match: the regex is zero-width only but find_not_empty is set")
175            }
176            CompileError::UnresolvedAstNode(ix, node) => {
177                write!(f, "Unresolved AST node encountered during analysis at position {}: {}", ix, node)
178            }
179        }
180    }
181}
182
183impl fmt::Display for RuntimeError {
184    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185        match self {
186            RuntimeError::StackOverflow => write!(f, "Max stack size exceeded for backtracking"),
187            RuntimeError::BacktrackLimitExceeded => {
188                write!(f, "Max limit for backtracking count exceeded")
189            }
190        }
191    }
192}
193
194impl fmt::Display for Error {
195    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196        match self {
197            Error::ParseError(position, parse_error) => {
198                write!(f, "Parsing error at position {}: {}", position, parse_error)
199            }
200            Error::CompileError(compile_error) => {
201                write!(f, "Error compiling regex: {}", compile_error)
202            }
203            Error::RuntimeError(runtime_error) => {
204                write!(f, "Error executing regex: {}", runtime_error)
205            }
206        }
207    }
208}
209
210impl From<CompileError> for Error {
211    fn from(compile_error: CompileError) -> Self {
212        Error::CompileError(Box::new(compile_error))
213    }
214}