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}
77
78#[derive(Clone, Debug)]
80#[non_exhaustive]
81pub enum RuntimeError {
82 StackOverflow,
84 BacktrackLimitExceeded,
88}
89
90#[cfg(feature = "std")]
91impl ::std::error::Error for Error {}
92
93impl fmt::Display for ParseError {
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 match self {
96 ParseError::GeneralParseError(s) => write!(f, "General parsing error: {}", s),
97 ParseError::UnclosedOpenParen => {
98 write!(f, "Opening parenthesis without closing parenthesis")
99 }
100 ParseError::InvalidRepeat => write!(f, "Invalid repeat syntax"),
101 ParseError::RecursionExceeded => write!(f, "Pattern too deeply nested"),
102 ParseError::TrailingBackslash => write!(f, "Backslash without following character"),
103 ParseError::InvalidEscape(s) => write!(f, "Invalid escape: {}", s),
104 ParseError::UnclosedUnicodeName => write!(f, "Unicode escape not closed"),
105 ParseError::InvalidHex => write!(f, "Invalid hex escape"),
106 ParseError::InvalidCodepointValue => {
107 write!(f, "Invalid codepoint for hex or unicode escape")
108 }
109 ParseError::InvalidClass => write!(f, "Invalid character class"),
110 ParseError::UnknownFlag(s) => write!(f, "Unknown group flag: {}", s),
111 ParseError::NonUnicodeUnsupported => write!(f, "Disabling Unicode not supported"),
112 ParseError::InvalidBackref => write!(f, "Invalid back reference"),
113 ParseError::InvalidGroupName => write!(f, "Could not parse group name"),
114 ParseError::InvalidGroupNameBackref(s) => {
115 write!(f, "Invalid group name in back reference: {}", s)
116 }
117 ParseError::TargetNotRepeatable => write!(f, "Target of repeat operator is invalid"),
118 }
119 }
120}
121
122impl fmt::Display for CompileError {
123 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124 match self {
125 CompileError::InnerError(e) => write!(f, "Regex error: {}", e),
126 CompileError::LookBehindNotConst => {
127 write!(f, "Look-behind assertion without constant size")
128 },
129 CompileError::InvalidGroupName => write!(f, "Could not parse group name"),
130 CompileError::InvalidGroupNameBackref(s) => write!(f, "Invalid group name in back reference: {}", s),
131 CompileError::InvalidBackref => write!(f, "Invalid back reference"),
132 CompileError::NamedBackrefOnly => write!(f, "Numbered backref/call not allowed because named group was used, use a named backref instead"),
133 }
134 }
135}
136
137impl fmt::Display for RuntimeError {
138 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 match self {
140 RuntimeError::StackOverflow => write!(f, "Max stack size exceeded for backtracking"),
141 RuntimeError::BacktrackLimitExceeded => {
142 write!(f, "Max limit for backtracking count exceeded")
143 }
144 }
145 }
146}
147
148impl fmt::Display for Error {
149 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150 match self {
151 Error::ParseError(position, parse_error) => {
152 write!(f, "Parsing error at position {}: {}", position, parse_error)
153 }
154 Error::CompileError(compile_error) => {
155 write!(f, "Error compiling regex: {}", compile_error)
156 }
157 Error::RuntimeError(runtime_error) => {
158 write!(f, "Error executing regex: {}", runtime_error)
159 }
160 }
161 }
162}
163
164impl From<CompileError> for Error {
165 fn from(compile_error: CompileError) -> Self {
166 Error::CompileError(compile_error)
167 }
168}