Skip to content

Commit 683d4fe

Browse files
authored
Merge pull request #502 from ratmice/regex_error
Add regex error source to `RegexError`
2 parents aba828e + 967c26b commit 683d4fe

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

lrlex/src/lib/mod.rs

Lines changed: 29 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
#[non_exhaustive]
5151
pub enum LexErrorKind {
5252
PrematureEnd,
@@ -59,13 +59,37 @@ pub enum LexErrorKind {
5959
InvalidStartState,
6060
InvalidStartStateName,
6161
DuplicateName,
62-
RegexError,
62+
RegexError(regex::Error),
6363
InvalidGrmtoolsSectionValue,
6464
InvalidNumber,
6565
DuplicateGrmtoolsSectionValue,
6666
VerbatimNotSupported,
6767
}
6868

69+
impl LexErrorKind {
70+
fn is_same_kind(&self, other: &Self) -> bool {
71+
use LexErrorKind as EK;
72+
match (self, other) {
73+
(EK::PrematureEnd, EK::PrematureEnd)
74+
| (EK::RoutinesNotSupported, EK::RoutinesNotSupported)
75+
| (EK::UnknownDeclaration, EK::UnknownDeclaration)
76+
| (EK::MissingSpace, EK::MissingSpace)
77+
| (EK::InvalidName, EK::InvalidName)
78+
| (EK::UnknownStartState, EK::UnknownStartState)
79+
| (EK::DuplicateStartState, EK::DuplicateStartState)
80+
| (EK::InvalidStartState, EK::InvalidStartState)
81+
| (EK::InvalidStartStateName, EK::InvalidStartStateName)
82+
| (EK::DuplicateName, EK::DuplicateName)
83+
| (EK::InvalidGrmtoolsSectionValue, EK::InvalidGrmtoolsSectionValue)
84+
| (EK::InvalidNumber, EK::InvalidNumber)
85+
| (EK::DuplicateGrmtoolsSectionValue, EK::DuplicateGrmtoolsSectionValue)
86+
| (EK::RegexError(_), EK::RegexError(_))
87+
| (EK::VerbatimNotSupported, EK::VerbatimNotSupported) => true,
88+
_ => false,
89+
}
90+
}
91+
}
92+
6993
impl Spanned for LexBuildError {
7094
fn spans(&self) -> &[Span] {
7195
self.spans.as_slice()
@@ -84,7 +108,7 @@ impl Spanned for LexBuildError {
84108
| LexErrorKind::InvalidGrmtoolsSectionValue
85109
| LexErrorKind::InvalidNumber
86110
| LexErrorKind::VerbatimNotSupported
87-
| LexErrorKind::RegexError => SpansKind::Error,
111+
| LexErrorKind::RegexError(_) => SpansKind::Error,
88112
LexErrorKind::DuplicateName
89113
| LexErrorKind::DuplicateStartState
90114
| LexErrorKind::DuplicateGrmtoolsSectionValue => SpansKind::DuplicationError,
@@ -94,7 +118,7 @@ impl Spanned for LexBuildError {
94118

95119
impl fmt::Display for LexBuildError {
96120
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97-
let s = match self.kind {
121+
let s = match &self.kind {
98122
LexErrorKind::VerbatimNotSupported => "Verbatim code not supported",
99123
LexErrorKind::PrematureEnd => "File ends prematurely",
100124
LexErrorKind::RoutinesNotSupported => "Routines not currently supported",
@@ -109,7 +133,7 @@ impl fmt::Display for LexBuildError {
109133
LexErrorKind::InvalidNumber => "Invalid number",
110134
LexErrorKind::DuplicateGrmtoolsSectionValue => "Duplicate grmtools section value",
111135
LexErrorKind::DuplicateName => "Rule name already exists",
112-
LexErrorKind::RegexError => "Invalid regular expression",
136+
LexErrorKind::RegexError(e) => return write!(f, "Invalid regular expression: {e}"),
113137
};
114138
write!(f, "{s}")
115139
}

lrlex/src/lib/parser.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn add_duplicate_occurrence(
125125
dup_span: Span,
126126
) {
127127
if !errs.iter_mut().any(|e| {
128-
if e.kind == kind && e.spans[0] == orig_span {
128+
if e.kind.is_same_kind(&kind) && e.spans[0] == orig_span {
129129
e.spans.push(dup_span);
130130
true
131131
} else {
@@ -618,7 +618,7 @@ where
618618
target_state,
619619
&self.lex_flags,
620620
)
621-
.map_err(|_| self.mk_error(LexErrorKind::RegexError, i))?;
621+
.map_err(|e| self.mk_error(LexErrorKind::RegexError(e), i))?;
622622
self.rules.push(rule);
623623
}
624624
Ok(i + line_len)
@@ -864,7 +864,7 @@ mod test {
864864
.expect_err("Parsed ok while expecting error");
865865
assert_eq!(errs.len(), 1);
866866
let e = &errs[0];
867-
assert_eq!(e.kind, kind);
867+
assert!(e.kind.is_same_kind(&kind));
868868
assert_eq!(
869869
e.spans()
870870
.iter()
@@ -892,20 +892,22 @@ mod test {
892892
}
893893
}
894894

895-
assert_eq!(
896-
errs.iter()
897-
.map(|e| {
898-
(
899-
e.kind.clone(),
900-
e.spans()
901-
.iter()
902-
.map(|span| line_col!(src, span))
903-
.collect::<Vec<_>>(),
904-
)
905-
})
906-
.collect::<Vec<_>>(),
907-
expected.collect::<Vec<_>>()
908-
);
895+
for ((ek1, es1), (ek2, es2)) in errs
896+
.iter()
897+
.map(|e| {
898+
(
899+
e.kind.clone(),
900+
e.spans()
901+
.iter()
902+
.map(|span| line_col!(src, span))
903+
.collect::<Vec<_>>(),
904+
)
905+
})
906+
.zip(expected)
907+
{
908+
assert!(ek1.is_same_kind(&ek2));
909+
assert_eq!(es1, es2);
910+
}
909911
}
910912
}
911913

0 commit comments

Comments
 (0)