[Haskell-cafe] Haskell poker server
Miles Sabin
miles at milessabin.com
Tue Aug 30 17:37:22 EDT 2005
Joel Reymont wrote,
> Can I beg for examples?
I've been using parsec for binary parsing (Java class files in my case)
as a first exercise with both Haskell and combinator parsing, with a
view to applying same to network protocols.
The experience has been surprisingly pleasant. In particular, it handles
the common "count, count*tokens" idiom that's found in many binary
formats and network protocols very smoothly ... something I've always
had to code by hand.
Being so new to Haskell I'm hesitant to give possibly bad examples, but
how about something like this,
constantPoolEntry :: Parser ConstantPoolEntry
constantPoolEntry =
do
{
u1Literal 1 ;
bytes <- u2 >>= (flip count $ u1) ;
return (ConstantUTF8 (decodeUTF8 bytes))
}
<|>
-- etc. ...
where u1Literal matches the byte '1' from the input, and u2 parses a
following 2 byte unsigned integer byte-count which is then used to
construct a parser of exactly byte-count bytes, ie. it matches the byte
sequence,
'1', n, b0 ... bn-1
I'm not aware of any other general purpose parsing framework which can
do this anything like as deftly.
Cheers,
Miles
More information about the Haskell-Cafe
mailing list