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

Copyright(c) Leon Mergen, 2015
LicenseMIT
Maintainerleon@solatis.com
Stabilityexperimental
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

type ParseC a = ByteString -> Result a Source

The parsing continuation form of a Data.Attoparsec parser. This is typically created by running the attoparsec "parse" function:

createParser = AttoParsec.parse myParser

parseMany Source

Arguments

:: (MonadIO m, MonadMask m, Show a) 
=> 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, MonadMask m, Show a) 
=> 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)