[Haskell-cafe] Re: Network parsing and parsec

Andrew Pimlott andrew at pimlott.net
Thu Sep 22 18:54:30 EDT 2005


On Tue, Sep 20, 2005 at 12:54:26PM +0000, John Goerzen wrote:
> On 2005-09-16, Andrew Pimlott <andrew at pimlott.net> wrote:
> > On Thu, Sep 15, 2005 at 06:11:58PM -0700, Andrew Pimlott wrote:
> >> I don't see why this would be more error-prone than any other approach.
> >
> > Hmm... I take that back.  I don't know anything about the IMAP protocol,
> > but after imagining for a few moments what it might be like, I can see
> > how it could be more difficult than my example.
> >
> > The user state of the parser might help you...
> 
> Hmm, can you elaborate on that?

I was imagining that the IMAP protocol does not have delimiting
characters (like s-expressions), and that instead you are told to read a
certain number of bytes, tokens, lines, etc.  So you could maintain the
number in the state and check it before any parser that might read too
far.  Don't know if this has anything to do with your actual problem.

> Basically, I *really* want to get away frmo having to use hGetContents.

I found writing my own lazy read pretty easy, after having some problems
using handles with sockets:

  recv' s     = do  r <- IO.try (recv s (1024 * 8))
                    case r of
                      Left err  -> if isEOFError err then return ""
                                                     else ioError err
                      Right msg -> do msg' <- unsafeInterleaveIO (recv' s)
                                      return (msg ++ msg')

So even if you don't want to use handles, you can still use parsec
without too much trouble.

Andrew


More information about the Haskell-Cafe mailing list