rust-bakery / nom

Rust parser combinator framework
MIT License
9.43k stars 805 forks source link

Add nom::IResult::map_rem (for debugging purposes) #408

Closed Centril closed 6 years ago

Centril commented 7 years ago

It would be nice to add to nom::IResult a function:

fn map_rem<H, F: FnOnce(I) -> H>(self, f: F) -> IResult<H, O, E> {
    match self {
        Done(i, o) => Done(f(i), o),
        Error(e)   => Error(e),
        Incomplete(n) => Incomplete(n),
    }
}

This is useful mainly for testing & debugging purposes while dealing with &[u8] in the terminal. As seen in the following "gist", http://play.integer32.com/?gist=68da7c56bb7f0dc4412b2ec42b6d5e21&version=stable , the output

Done([114, 101, 109, 97, 105, 110, 105, 110, 103], [109, 97, 116, 99, 104, 101, 100])

is harder to read compared to:

Done("remaining", [109, 97, 116, 99, 104, 101, 100])

It could also potentially be useful to add a function:

fn bimap<H, N, F: FnOnce(I) -> H, G: FnOnce(O) -> N>(self, f: F, g: G) -> IResult<H, N, E> {
    match self {
        Done(i, o) => Done(f(i), g(o)),
        Error(e)   => Error(e),
        Incomplete(n) => Incomplete(n),
    }
}
Geal commented 7 years ago

indeed, it would be useful. Can you make a pull request?

Centril commented 7 years ago

Sure thing, tho, not today =)

Geal commented 7 years ago

waiting for that PR ;)

Centril commented 7 years ago

Apologies =) I'm currently writing my Bachelors thesis, so I don't have a lot of time to work on this. So I'll postpone until I can fix it =)

Geal commented 6 years ago

so IResult is replaced with a Result, for which the Ok side contains a tuple of (I,O), so it should be much easier to work with? Do we still need a map_rem?

Centril commented 6 years ago

Probably not, so I'm closing then ;)