Carry state within nom parser


My nom parser results reflect to a data state. Here’s a piece of what I’ve written:

named!(pub parse_dex<&mut State, Dex>, do_parse!(
    tag!(b"dex\n") >>
    parse_ver >>
    char!('\0') >>
    checksum: call!(le_u32) >>
    signature: take!(20) >>
    file_size: call!(le_u32) >>
    header_size: call!(le_u32) >>
    endian: call!(parse_endian) >>
    state: call!(update_state) >>
    eof!() >>
    (Dex { version: state.ver, ... })

By having an input type &mut State must State implement iterators and essential methods for nom, just like &[u8]? Which traits must be implemented?


The easiest way to carry state is to create a type that contains all your nom functionality as methods, as opposed to having all your nom functionality as free functions. You can do this by using the method macro instead of the named macro. See e.g.

1 Like

@jethrogb This’d lead to least 2 copies per user-defined parser method (parser data and input), but I think it doesn’t matter.

Things like call_m! and method! deprecated and the module nom::methods is empty, but if they change it might be easy to re-adapt macro calls.