Skip to content

Commit 6b853ee

Browse files
committed
Add regex error source to RegexError
1 parent 68f7d47 commit 6b853ee

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lrlex/src/lib/mod.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct LexBuildError {
4646
impl Error for LexBuildError {}
4747

4848
/// The various different possible Lex parser errors.
49-
#[derive(Debug, PartialEq, Eq, Clone)]
49+
#[derive(Debug, Clone)]
5050
pub enum LexErrorKind {
5151
PrematureEnd,
5252
RoutinesNotSupported,
@@ -58,13 +58,40 @@ pub enum LexErrorKind {
5858
InvalidStartState,
5959
InvalidStartStateName,
6060
DuplicateName,
61-
RegexError,
61+
RegexError(regex::Error),
6262
InvalidGrmtoolsSectionValue,
6363
InvalidNumber,
6464
DuplicateGrmtoolsSectionValue,
6565
VerbatimNotSupported,
6666
}
6767

68+
impl PartialEq for LexErrorKind {
69+
fn eq(&self, other: &Self) -> bool {
70+
use LexErrorKind as EK;
71+
match (self, other) {
72+
(EK::PrematureEnd, EK::PrematureEnd)
73+
| (EK::RoutinesNotSupported, EK::RoutinesNotSupported)
74+
| (EK::UnknownDeclaration, EK::UnknownDeclaration)
75+
| (EK::MissingSpace, EK::MissingSpace)
76+
| (EK::InvalidName, EK::InvalidName)
77+
| (EK::UnknownStartState, EK::UnknownStartState)
78+
| (EK::DuplicateStartState, EK::DuplicateStartState)
79+
| (EK::InvalidStartState, EK::InvalidStartState)
80+
| (EK::InvalidStartStateName, EK::InvalidStartStateName)
81+
| (EK::DuplicateName, EK::DuplicateName)
82+
| (EK::InvalidGrmtoolsSectionValue, EK::InvalidGrmtoolsSectionValue)
83+
| (EK::InvalidNumber, EK::InvalidNumber)
84+
| (EK::DuplicateGrmtoolsSectionValue, EK::DuplicateGrmtoolsSectionValue)
85+
| (EK::VerbatimNotSupported, EK::VerbatimNotSupported) => true,
86+
(EK::RegexError(e1), EK::RegexError(e2)) => e1.to_string() == e2.to_string(),
87+
88+
_ => false,
89+
}
90+
}
91+
}
92+
93+
impl Eq for LexErrorKind {}
94+
6895
impl Spanned for LexBuildError {
6996
fn spans(&self) -> &[Span] {
7097
self.spans.as_slice()
@@ -83,7 +110,7 @@ impl Spanned for LexBuildError {
83110
| LexErrorKind::InvalidGrmtoolsSectionValue
84111
| LexErrorKind::InvalidNumber
85112
| LexErrorKind::VerbatimNotSupported
86-
| LexErrorKind::RegexError => SpansKind::Error,
113+
| LexErrorKind::RegexError(_) => SpansKind::Error,
87114
LexErrorKind::DuplicateName
88115
| LexErrorKind::DuplicateStartState
89116
| LexErrorKind::DuplicateGrmtoolsSectionValue => SpansKind::DuplicationError,
@@ -93,7 +120,7 @@ impl Spanned for LexBuildError {
93120

94121
impl fmt::Display for LexBuildError {
95122
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96-
let s = match self.kind {
123+
let s = match &self.kind {
97124
LexErrorKind::VerbatimNotSupported => "Verbatim code not supported",
98125
LexErrorKind::PrematureEnd => "File ends prematurely",
99126
LexErrorKind::RoutinesNotSupported => "Routines not currently supported",
@@ -108,7 +135,7 @@ impl fmt::Display for LexBuildError {
108135
LexErrorKind::InvalidNumber => "Invalid number",
109136
LexErrorKind::DuplicateGrmtoolsSectionValue => "Duplicate grmtools section value",
110137
LexErrorKind::DuplicateName => "Rule name already exists",
111-
LexErrorKind::RegexError => "Invalid regular expression",
138+
LexErrorKind::RegexError(e) => return write!(f, "Invalid regular expression: {e}"),
112139
};
113140
write!(f, "{s}")
114141
}

lrlex/src/lib/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ where
606606
target_state,
607607
&self.lex_flags,
608608
)
609-
.map_err(|_| self.mk_error(LexErrorKind::RegexError, i))?;
609+
.map_err(|e| self.mk_error(LexErrorKind::RegexError(e), i))?;
610610
self.rules.push(rule);
611611
}
612612
Ok(i + line_len)

0 commit comments

Comments
 (0)