rust-parser-combinator/src/map.rs

17 lines
471 B
Rust

use crate::{representation::Representation, Parser, ParserExtension};
pub fn map<P, F, I, O1, O2, E>(parser: P, map_fn: F) -> impl Parser<I, O2, E>
where
P: Parser<I, O1, E>,
F: Fn(O1) -> O2,
{
let production = parser.representation().production();
(move |input| {
parser
.parse(input)
.map(|(result, rest)| (map_fn(result), rest))
})
.to_anno()
.with_repr(Representation::new().with_production(production))
}