This commit is contained in:
Greg Shuflin 2024-01-26 00:13:05 -08:00
parent cbb30d3e9f
commit 41829019b6
2 changed files with 25 additions and 0 deletions

View File

@ -1,9 +1,11 @@
#![allow(dead_code)] //TODO eventually turn this off
mod choice;
mod map;
mod primitives;
mod sequence;
pub use choice::*;
pub use map::*;
pub use primitives::*;
pub use sequence::*;
@ -53,4 +55,14 @@ mod tests {
let output = parser.parse("ara hajimete").unwrap();
assert_eq!(("ara", " hajimete"), output);
}
#[test]
fn test_map() {
let parser = map(
sequence(literal("a"), literal("b")),
|(_a, _b): (&str, &str)| 59,
);
let output = parser.parse("abcd").unwrap();
assert_eq!((59, "cd"), output);
}
}

13
src/map.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::Parser;
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,
{
move |input| {
parser
.parse(input)
.map(|(result, rest)| (map_fn(result), rest))
}
}