zevv / npeg

PEGs for Nim, another take
MIT License
330 stars 22 forks source link

Can You Add Some Example Binary Structures? #23

Closed gitjeff2 closed 4 years ago

gitjeff2 commented 4 years ago

I realize that you specifically indicate that npeg was extended over time and parsing binary data structures is a bit experimental at this point. However, it would be wonderful to have some examples of npeg parsed binary structures just to help potential users of the library wrap their heads around how it should work given that use-case.

I could be wrong, but I think a heavily commented working parser for Ethernet frames and IPv4 packets would probably be enough of a reference for a large number of binary use cases to help people get the ball rolling.

This is a very cool project. Writing parsers by hand is indeed quite a pain.

zevv commented 4 years ago

I'm sorry, but NPeg is really ment to work on text input and is not really suited for handling binary data. It lives in the same domain as traditional lexers and parsers - it eats text and changes it into something that is easier to work with for computer programs.

Parsing binary protocols requires a few things which are pretty hard or impossible to do with NPeg, like handling variable length blocks of data, parsing binary numbers of varying word sizes and endianess, or extracting specific bit groups from input data.

I have not worked with it myself, but you might look into @sealmove's https://github.com/sealmove/nimitai