1use alloc::string::String;
2use core::fmt;
3use regex_automata::meta::BuildError as RaBuildError;
4
5pub type Result<T> = ::core::result::Result<T, Error>;
7
8pub type ParseErrorPosition = usize;
9
10#[derive(Clone, Debug)]
12#[non_exhaustive]
13pub enum Error {
14 ParseError(ParseErrorPosition, ParseError),
16 CompileError(CompileError),
18 RuntimeError(RuntimeError),
20}
21
22#[derive(Clone, Debug)]
24#[non_exhaustive]
25pub enum ParseError {
26 GeneralParseError(String),
28 UnclosedOpenParen,
30 InvalidRepeat,
32 RecursionExceeded,
34 TrailingBackslash,
36 InvalidEscape(String),
38 UnclosedUnicodeName,
40 InvalidHex,
42 InvalidCodepointValue,
44 InvalidClass,
46 UnknownFlag(String),
48 NonUnicodeUnsupported,
50 InvalidBackref,
52 TargetNotRepeatable,
54 InvalidGroupName,
56 InvalidGroupNameBackref(String),
58}
59
60#[derive(Clone, Debug)]
62#[non_exhaustive]
63pub enum CompileError {
64 InnerError(RaBuildError),
66 LookBehindNotConst,
68 InvalidGroupName,
70 InvalidGroupNameBackref(String),
72 InvalidBackref,
74 NamedBackrefOnly,
76 FeatureNotYetSupported(String),
78 SubroutineCallTargetNotFound(String, usize),
80}
81
82#[derive(Clone, Debug)]
84#[non_exhaustive]
85pub enum RuntimeError {
86 StackOverflow,
88 BacktrackLimitExceeded,
92}
93
94#[cfg(feature = "std")]
95impl ::std::error::Error for Error {}
96
97impl fmt::Display for ParseError {
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99 match self {
100 ParseError::GeneralParseError(s) => write!(f, "General parsing error: {}", s),
101 ParseError::UnclosedOpenParen => {
102 write!(f, "Opening parenthesis without closing parenthesis")
103 }
104 ParseError::InvalidRepeat => write!(f, "Invalid repeat syntax"),
105 ParseError::RecursionExceeded => write!(f, "Pattern too deeply nested"),
106 ParseError::TrailingBackslash => write!(f, "Backslash without following character"),
107 ParseError::InvalidEscape(s) => write!(f, "Invalid escape: {}", s),
108 ParseError::UnclosedUnicodeName => write!(f, "Unicode escape not closed"),
109 ParseError::InvalidHex => write!(f, "Invalid hex escape"),
110 ParseError::InvalidCodepointValue => {
111 write!(f, "Invalid codepoint for hex or unicode escape")
112 }
113 ParseError::InvalidClass => write!(f, "Invalid character class"),
114 ParseError::UnknownFlag(s) => write!(f, "Unknown group flag: {}", s),
115 ParseError::NonUnicodeUnsupported => write!(f, "Disabling Unicode not supported"),
116 ParseError::InvalidBackref => write!(f, "Invalid back reference"),
117 ParseError::InvalidGroupName => write!(f, "Could not parse group name"),
118 ParseError::InvalidGroupNameBackref(s) => {
119 write!(f, "Invalid group name in back reference: {}", s)
120 }
121 ParseError::TargetNotRepeatable => write!(f, "Target of repeat operator is invalid"),
122 }
123 }
124}
125
126impl fmt::Display for CompileError {
127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128 match self {
129 CompileError::InnerError(e) => write!(f, "Regex error: {}", e),
130 CompileError::LookBehindNotConst => {
131 write!(f, "Look-behind assertion without constant size")
132 },
133 CompileError::InvalidGroupName => write!(f, "Could not parse group name"),
134 CompileError::InvalidGroupNameBackref(s) => write!(f, "Invalid group name in back reference: {}", s),
135 CompileError::InvalidBackref => write!(f, "Invalid back reference"),
136 CompileError::NamedBackrefOnly => write!(f, "Numbered backref/call not allowed because named group was used, use a named backref instead"),
137 CompileError::FeatureNotYetSupported(s) => write!(f, "Regex uses currently unimplemented feature: {}", s),
138 CompileError::SubroutineCallTargetNotFound(s, ix) => {
139 write!(f, "Subroutine call target not found at position {}: {}", ix, s)
140 }
141 }
142 }
143}
144
145impl fmt::Display for RuntimeError {
146 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147 match self {
148 RuntimeError::StackOverflow => write!(f, "Max stack size exceeded for backtracking"),
149 RuntimeError::BacktrackLimitExceeded => {
150 write!(f, "Max limit for backtracking count exceeded")
151 }
152 }
153 }
154}
155
156impl fmt::Display for Error {
157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158 match self {
159 Error::ParseError(position, parse_error) => {
160 write!(f, "Parsing error at position {}: {}", position, parse_error)
161 }
162 Error::CompileError(compile_error) => {
163 write!(f, "Error compiling regex: {}", compile_error)
164 }
165 Error::RuntimeError(runtime_error) => {
166 write!(f, "Error executing regex: {}", runtime_error)
167 }
168 }
169 }
170}
171
172impl From<CompileError> for Error {
173 fn from(compile_error: CompileError) -> Self {
174 Error::CompileError(compile_error)
175 }
176}