1use alloc::boxed::Box;
2use alloc::string::String;
3use core::fmt;
4use regex_automata::meta::BuildError as RaBuildError;
5
6pub type Result<T> = ::core::result::Result<T, Error>;
8
9pub type ParseErrorPosition = usize;
10
11#[derive(Clone, Debug)]
13#[non_exhaustive]
14pub enum Error {
15 ParseError(ParseErrorPosition, ParseError),
17 CompileError(Box<CompileError>),
19 RuntimeError(RuntimeError),
21}
22
23#[derive(Clone, Debug)]
25#[non_exhaustive]
26pub enum ParseError {
27 GeneralParseError(String),
29 UnclosedOpenParen,
31 InvalidRepeat,
33 RecursionExceeded,
35 TrailingBackslash,
37 InvalidEscape(String),
39 UnclosedUnicodeName,
41 InvalidHex,
43 InvalidCodepointValue,
45 InvalidClass,
47 UnknownFlag(String),
49 NonUnicodeUnsupported,
51 InvalidBackref,
53 TargetNotRepeatable,
55 InvalidGroupName,
57 InvalidGroupNameBackref(String),
59}
60
61#[derive(Clone, Debug)]
63#[non_exhaustive]
64pub enum CompileError {
65 InnerError(RaBuildError),
67 LookBehindNotConst,
69 VariableLookBehindRequiresFeature,
71 DfaBuildError(String),
73 InvalidGroupName,
75 InvalidGroupNameBackref(String),
77 InvalidBackref(usize),
79 NamedBackrefOnly,
81 FeatureNotYetSupported(String),
83 SubroutineCallTargetNotFound(String, usize),
85}
86
87#[derive(Clone, Debug)]
89#[non_exhaustive]
90pub enum RuntimeError {
91 StackOverflow,
93 BacktrackLimitExceeded,
97}
98
99#[cfg(feature = "std")]
100impl ::std::error::Error for Error {}
101
102impl fmt::Display for ParseError {
103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104 match self {
105 ParseError::GeneralParseError(s) => write!(f, "General parsing error: {}", s),
106 ParseError::UnclosedOpenParen => {
107 write!(f, "Opening parenthesis without closing parenthesis")
108 }
109 ParseError::InvalidRepeat => write!(f, "Invalid repeat syntax"),
110 ParseError::RecursionExceeded => write!(f, "Pattern too deeply nested"),
111 ParseError::TrailingBackslash => write!(f, "Backslash without following character"),
112 ParseError::InvalidEscape(s) => write!(f, "Invalid escape: {}", s),
113 ParseError::UnclosedUnicodeName => write!(f, "Unicode escape not closed"),
114 ParseError::InvalidHex => write!(f, "Invalid hex escape"),
115 ParseError::InvalidCodepointValue => {
116 write!(f, "Invalid codepoint for hex or unicode escape")
117 }
118 ParseError::InvalidClass => write!(f, "Invalid character class"),
119 ParseError::UnknownFlag(s) => write!(f, "Unknown group flag: {}", s),
120 ParseError::NonUnicodeUnsupported => write!(f, "Disabling Unicode not supported"),
121 ParseError::InvalidBackref => write!(f, "Invalid back reference"),
122 ParseError::InvalidGroupName => write!(f, "Could not parse group name"),
123 ParseError::InvalidGroupNameBackref(s) => {
124 write!(f, "Invalid group name in back reference: {}", s)
125 }
126 ParseError::TargetNotRepeatable => write!(f, "Target of repeat operator is invalid"),
127 }
128 }
129}
130
131impl fmt::Display for CompileError {
132 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133 match self {
134 CompileError::InnerError(e) => write!(f, "Regex error: {}", e),
135 CompileError::LookBehindNotConst => {
136 write!(f, "Look-behind assertion without constant size")
137 },
138 CompileError::VariableLookBehindRequiresFeature => {
139 write!(f, "Variable-length lookbehind requires the 'variable-lookbehinds' feature")
140 },
141 CompileError::DfaBuildError(s) => {
142 write!(f, "Failed to build reverse DFA for variable-length lookbehind: {}", s)
143 },
144 CompileError::InvalidGroupName => write!(f, "Could not parse group name"),
145 CompileError::InvalidGroupNameBackref(s) => write!(f, "Invalid group name in back reference: {}", s),
146 CompileError::InvalidBackref(g) => write!(f, "Invalid back reference to group {}", g),
147 CompileError::NamedBackrefOnly => write!(f, "Numbered backref/call not allowed because named group was used, use a named backref instead"),
148 CompileError::FeatureNotYetSupported(s) => write!(f, "Regex uses currently unimplemented feature: {}", s),
149 CompileError::SubroutineCallTargetNotFound(s, ix) => {
150 write!(f, "Subroutine call target not found at position {}: {}", ix, s)
151 }
152 }
153 }
154}
155
156impl fmt::Display for RuntimeError {
157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158 match self {
159 RuntimeError::StackOverflow => write!(f, "Max stack size exceeded for backtracking"),
160 RuntimeError::BacktrackLimitExceeded => {
161 write!(f, "Max limit for backtracking count exceeded")
162 }
163 }
164 }
165}
166
167impl fmt::Display for Error {
168 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169 match self {
170 Error::ParseError(position, parse_error) => {
171 write!(f, "Parsing error at position {}: {}", position, parse_error)
172 }
173 Error::CompileError(compile_error) => {
174 write!(f, "Error compiling regex: {}", compile_error)
175 }
176 Error::RuntimeError(runtime_error) => {
177 write!(f, "Error executing regex: {}", runtime_error)
178 }
179 }
180 }
181}
182
183impl From<CompileError> for Error {
184 fn from(compile_error: CompileError) -> Self {
185 Error::CompileError(Box::new(compile_error))
186 }
187}