Map
This commit is contained in:
parent
194e23a5be
commit
3bc608d4a7
32
src/lib.rs
32
src/lib.rs
@ -1,35 +1,51 @@
|
|||||||
#![feature(assert_matches)]
|
#![feature(assert_matches)]
|
||||||
#![allow(dead_code)] //TODO eventually turn this off
|
#![allow(dead_code)] //TODO eventually turn this off
|
||||||
|
|
||||||
|
|
||||||
type ParseResult<I, O, E> = Result<(O, I), E>;
|
type ParseResult<I, O, E> = Result<(O, I), E>;
|
||||||
|
|
||||||
trait Parser<I, O, E> {
|
trait Parser<I, O, E> {
|
||||||
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, O, E, F> Parser<I, O, E> for F where F: Fn(I) -> ParseResult<I, O, E> {
|
impl<I, O, E, F> Parser<I, O, E> for F
|
||||||
|
where
|
||||||
|
F: Fn(I) -> ParseResult<I, O, E>,
|
||||||
|
{
|
||||||
fn parse(&self, input: I) -> ParseResult<I, O, E> {
|
fn parse(&self, input: I) -> ParseResult<I, O, E> {
|
||||||
self(input)
|
self(input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, (), &str> {
|
fn literal(expected: &'static str) -> impl Fn(&str) -> ParseResult<&str, &str, &str> {
|
||||||
move |input| match input.get(0..expected.len()) {
|
move |input| match input.get(0..expected.len()) {
|
||||||
Some(next) if next == expected =>
|
Some(next) if next == expected => Ok((expected, &input[expected.len()..])),
|
||||||
Ok(((), &input[expected.len()..])),
|
_ => Err(input),
|
||||||
_ => Err(input)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::assert_matches::assert_matches;
|
use std::assert_matches::assert_matches;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parsing() {
|
fn test_parsing() {
|
||||||
let output = literal("a")("a yolo");
|
let output = literal("a")("a yolo");
|
||||||
assert_matches!(output.unwrap(), ((), " yolo"));
|
assert_matches!(output.unwrap(), ("a", " yolo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map() {
|
||||||
|
let lit_a = literal("a");
|
||||||
|
let output = map(lit_a, |s| s.to_uppercase()).parse("a yolo");
|
||||||
|
assert_matches!(output.unwrap(), (s, " yolo") if s == "A");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user