network-attoparsec-0.9.1: Utility functions for running a parser against a socket

Safe HaskellNone
LanguageHaskell2010

Network.Attoparsec

Description

Utility functions for running a parser against a socket, without the need of a bigger framework such as Pipes or Conduit.

Synopsis

Documentation

parseMany Source

Arguments

:: (MonadIO m, MonadError String m) 
=> Socket

Socket to read data from

-> ParseC a

Initial parser state

-> ParseC a

Continuation parser state

-> m (ParseC a, [a])

Next parser state with parsed values

Consumes input from socket and attempts to parse as many objects from the socket as possible. Use this function only when you expect more than one parse operation to succeed.

The function is continuation based, so you must provide the next parser state in successing calls as follows:

doParse sock = do
  (p1, xs1) <- parseMany sock (AttoParsec.parse myParser) (AttoParsec.parse myParser)
  (_,  xs2) <- parseMany sock (AttoParsec.parse myPArser) p1
  return (xs1 ++ xs2)

For more usage examples, see the test directory.

parseOne Source

Arguments

:: (MonadIO m, MonadError String m) 
=> Socket

Socket to read data from

-> ParseC a

Initial parser state

-> m a

Parsed value

Similar to parseMany, but assumes that there will only be enough data for a single succesful parse on the socket, and guarantees that exactly one item will be parsed.

Warning: this function will not work correctly when input data is pipelined. The parser might consume more data than required from the socket, or a partial second object is parsed, and the parser state and buffer will be discarded.

The is typically used as follows:

doParse sock = parseOne sock (AttoParsec.parse myParser)