[Haskell-cafe] Parsing binary 'hierachical' objects for lazy developers
Stephen Tetley
stephen.tetley at gmail.com
Wed Apr 27 23:18:47 CEST 2011
On 27 April 2011 21:28, Alexander Solla <alex.solla at gmail.com> wrote:
>
> On Wed, Apr 27, 2011 at 11:16 AM, John Obbele <john.obbele at gmail.com> wrote:
>>
>> Second issue, I would like to find a way to dispatch parsers. I'm
>> not very good at expressing my problem in english, so I will use
>> another code example:
>
> This sounds very hard in the general case. Others have shown you how to
> dispatch on two types. But there is no general data type which combines all
> (or even arbitrarily many) types. Somehow, "Read" is able to do this, but I
> don't know what kind of magic it uses.
>
Read always "demands its type" so it doesn't use any magic - if the
input string doesn't conform it will throw an error.
Any sensible binary format will have a scheme such as tag byte
prefixes to control choice in parsing (binary parsing generally avoids
all backtracking). If your binary data doesn't have a proper scheme it
will be hard to parse for any language (or cast-to in the case of C),
so the most sensible answer is to revise the format.
More information about the Haskell-Cafe
mailing list