Parser Extension

This commit is contained in:
Greg Shuflin 2024-01-27 16:00:29 -08:00
parent 2ad7707349
commit afae0d0840

View File

@ -26,6 +26,35 @@ where
}
}
pub trait ParserExtension<I, O, E>: Parser<I, O, E> {
fn map<F, O2>(self, map_fn: F) -> impl Parser<I, O2, E>
where
F: Fn(O) -> O2;
fn to<O2: Clone>(self, item: O2) -> impl Parser<I, O2, E>;
fn then<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, (O, O2), E>;
}
impl<T, I, O, E> ParserExtension<I, O, E> for T
where
T: Parser<I, O, E>,
{
fn map<F, O2>(self, map_fn: F) -> impl Parser<I, O2, E>
where
F: Fn(O) -> O2,
{
map(self, map_fn)
}
fn to<O2: Clone>(self, item: O2) -> impl Parser<I, O2, E> {
self.map(move |_| item.clone())
}
fn then<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, (O, O2), E> {
seq2(self, next)
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -60,17 +89,20 @@ mod tests {
#[test]
fn test_map() {
let parser = map(
seq2(literal("a"), literal("b")),
|(_a, _b): (&str, &str)| 59,
);
let parser =
seq2(literal("a"), literal("b")).map(|(a, _b): (&str, &str)| (a.to_uppercase(), 59));
let output = parser.parse("abcd").unwrap();
assert_eq!((59, "cd"), output);
assert_eq!((("A".to_owned(), 59), "cd"), output);
let spaces = repeated(literal_char(' ')).at_least(1);
let parser = seq2(literal("lute"), spaces).to(500);
assert_eq!(parser.parse("lute "), Ok((500, "")));
assert_eq!(parser.parse("lute"), Err(((), "")));
}
#[test]
fn test_combinators() {
let parser = seq2(map(repeated(literal_char('a')), |_| 10), literal_char('b'));
let parser = repeated(literal_char('a')).to(10).then(literal_char('b'));
let output = parser.parse("aaaaaaaabcd").unwrap();
assert_eq! {((10, 'b'), "cd"), output};
}