refact: make FialedParserResult to implement std::Error:Error.

This commit is contained in:
2024-11-19 21:34:32 +08:00
parent 91c6c42a02
commit e2c4388a0c
6 changed files with 56 additions and 45 deletions

View File

@@ -121,11 +121,9 @@ where
let (input, _) = self.parser.parse(context, input)?;
if origin_input.is_empty() {
return Err((
return Err(FailedParserResult::new_with_str(
input,
FailedParserResult::new(
"Empty input, the 'literal' parser cann't get length of elememt.".to_owned(),
),
"Empty input, the 'literal' parser cann't get length of elememt.",
));
}
@@ -156,11 +154,9 @@ where
let (input, _) = self.parser.parse(context, input)?;
if origin_input.is_empty() {
return Err((
return Err(FailedParserResult::new_with_str(
input,
FailedParserResult::new(
"Empty input, the 'literal' parser cann't get length of elememt.".to_owned(),
),
"Empty input, the 'literal' parser cann't get length of elememt.",
));
}
@@ -212,11 +208,11 @@ where
input: &'a [TToken],
) -> ParserResult<'a, TToken, TResult> {
match self.parser.parse(context, input) {
Ok((input, _)) => Err((
Ok(_) => Err(FailedParserResult::new_with_str(
input,
FailedParserResult::new("Reverse failed succeeded.".to_owned()),
"Reverse successful parser.",
)),
Err((input, _)) => Ok((input, self.result.clone())),
Err(_) => Ok((input, self.result.clone())),
}
}
}
@@ -269,7 +265,7 @@ mod test {
let (input, result) = satisfy(|c: &char| c.is_numeric()).parse(context, input)?;
match result.to_digit(10) {
None => Err((input, FailedParserResult::new("What?".to_string()))),
None => Err(FailedParserResult::new_with_str(input, "What?")),
Some(r) => Ok((input, Number(r as i32))),
}
}
@@ -300,7 +296,7 @@ mod test {
satisfy(|c: &char| c.is_numeric())
.next(|r| match r {
Ok((input, result)) => match result.to_digit(10) {
None => Err((input, FailedParserResult::new("What?".to_string()))),
None => Err(FailedParserResult::new_with_str(input, "What?")),
Some(r) => Ok((input, Number(r as i32))),
},
Err(r) => Err(r),

View File

@@ -35,9 +35,9 @@ where
_: Rc<RefCell<ParserContext<TToken, TContext>>>,
input: &'a [TToken],
) -> ParserResult<'a, TToken, T> {
Err((
Err(FailedParserResult::new_with_str(
input,
FailedParserResult::new("Default failed parser.".to_owned()),
"Default failed parser.",
))
}
}
@@ -56,7 +56,7 @@ where
_: Rc<RefCell<ParserContext<TToken, TContext>>>,
input: &'a [TToken],
) -> ParserResult<'a, TToken, T> {
Err((input, FailedParserResult::new(self.message.clone())))
Err(FailedParserResult::new(input, self.message.clone()))
}
}
@@ -75,16 +75,13 @@ where
input: &'a [TToken],
) -> ParserResult<'a, TToken, TToken> {
if input.is_empty() {
return Err((input, FailedParserResult::new("Input is empty.".to_owned())));
return Err(FailedParserResult::new_with_str(input, "Input is empty"));
}
if (self.predicate)(&input[0]) {
Ok((&input[1..], input[0].clone()))
} else {
Err((
input,
FailedParserResult::new("Predicate failed.".to_owned()),
))
Err(FailedParserResult::new_with_str(input, "Predicate failed."))
}
}
}
@@ -101,7 +98,7 @@ where
input: &'a [TToken],
) -> ParserResult<'a, TToken, TToken> {
if input.is_empty() {
Err((input, FailedParserResult::new("Input is empty.".to_owned())))
Err(FailedParserResult::new_with_str(input, "Input is empty"))
} else {
Ok((&input[1..], input[0].clone()))
}
@@ -122,12 +119,12 @@ where
input: &'a [TToken],
) -> ParserResult<'a, TToken, Vec<TToken>> {
if input.len() < self.count {
Err((
Err(FailedParserResult::new(
input,
FailedParserResult::new(format!(
format!(
"The input doesn't contain enough {} elemements.",
self.count
)),
),
))
} else {
Ok((
@@ -152,12 +149,12 @@ where
input: &'a [TToken],
) -> ParserResult<'a, TToken, ()> {
if input.len() < self.count {
Err((
Err(FailedParserResult::new(
input,
FailedParserResult::new(format!(
format!(
"The input doesn't contain enough {} elemements.",
self.count
)),
),
))
} else {
Ok((&input[self.count..], ()))