Nom -- how to raise an error / convert other errors to nom errors?


When calling partial functions like str::from_utf8 and str::parse from nom
parsers how do I convert the errors returned by those functions to nom errors
and raise them as parse errors? I tried making my parsers return Result, but
then combinators like many0 returns Vec<Result<...>> instead of Vec<...>
which doesn’t make any sense – any errors raised when parsing a sequence of
things should be raised as parse errors. How do I do this with nom?



It’s not clear to me exactly what you’re asking.

If you are using nom's named! macro to create your parse functions, the types should match up in a sane way. If you’re inserting your own functions at some points (sometimes necessary), then you have to do some of the plumbing work that is otherwise handled by the macros. You may wish to use return_error! to cause earlier returns in the error case. Normally, nom will backtrack through parsers in an attempt to find one which succeeds in parsing. return_error! should prevent this backtracking. I’m not sure it will help if you aren’t using nom's IResult type.

Note that the vector of results makes a lot of sense if you are returning just Result<T,Err> from your functions - you haven’t given many0! anything else to work with, so it’s acting like a simple vec! macro, except it will accept zero successful arguments. nom returns IResult<I,O>, which is an alias for Result<(I, O), nom::Err<I, E>>. It returns this from parsers, because it passes the remaining input through the parse chain. If you aren’t using nom's IResult type, then you have to write out the signature I provided above yourself to get more-idiomatic results from nom.

Without code, I’m not sure we can provide very specific help.