then_ignore, ignore_then

This commit is contained in:
Greg Shuflin 2022-10-19 22:23:52 -07:00
parent 0d4ea42678
commit e6ce2cf34d
2 changed files with 39 additions and 13 deletions

View File

@ -52,21 +52,21 @@ mod tests {
let digit = || one_of("1234567890");
assert_eq!(digit().parse("3"), Ok(("3", "")));
let digits = || repeated(digit()).at_least(1);
let json_number_inner = choice((
seq((
repeated(digit()).at_least(1),
literal(".").then(repeated(digit())).optional(),
))
.map(|(mut digits, maybe_more)| {
if let Some((point, more)) = maybe_more {
digits.push(point);
digits.extend(more.into_iter());
seq((digits(), literal(".").ignore_then(digits()).optional())).map(
|(mut digits, maybe_decimal)| {
if let Some(decimal_digits) = maybe_decimal {
digits.push(".");
digits.extend(decimal_digits.into_iter());
}
digits.into_iter().collect::<String>()
}),
seq((literal("."), repeated(digit()).at_least(1))).map(|(_point, digits)| {
},
),
literal(".").ignore_then(digits()).map(|decimal_digits| {
let mut d = vec!["."];
d.extend(digits.into_iter());
d.extend(decimal_digits.into_iter());
d.into_iter().collect::<String>()
}),
))

View File

@ -44,6 +44,32 @@ pub trait Parser<I, O, E> {
BoxedParser::new(crate::sequence::tuple2(self, next_parser))
}
fn ignore_then<'a, P, O2>(self, next_parser: P) -> BoxedParser<'a, I, O2, E>
where
Self: Sized + 'a,
I: 'a,
O: 'a,
O2: 'a,
E: 'a,
P: Parser<I, O2, E> + 'a,
{
BoxedParser::new(crate::sequence::tuple2(self, next_parser))
.map(|(_, next_output)| next_output)
}
fn then_ignore<'a, P, O2>(self, next_parser: P) -> BoxedParser<'a, I, O, E>
where
Self: Sized + 'a,
I: 'a,
O: 'a,
O2: 'a,
E: 'a,
P: Parser<I, O, E> + 'a,
{
BoxedParser::new(crate::sequence::tuple2(self, next_parser))
.map(|(this_output, _)| this_output)
}
fn optional<'a>(self) -> BoxedParser<'a, I, Option<O>, E>
where
I: Clone + 'a,