pipes-attoparsec-0.2.0.0: Attoparsec and Pipes integration.

Safe HaskellNone

Control.Proxy.Attoparsec

Contents

Description

This module allows you to run Attoparsec parsers on input flowing downstream through Pipes streams, possibly interleaving other stream effects.

This module builds on top of the pipes-parse package and assumes you have read Control.Proxy.Parse.Tutorial.

Synopsis

Parsing

There are two basic parsing facilities exported by this module, and choosing between them is easy: If you need to interleave Attoparsec parsing with other stream effects you must use parse, otherwise you may use the simpler parseD.

These proxies use the EitherP proxy transformer to report parsing errors, you might use any of the facilities exported by Control.Proxy.Trans.Either to recover from them.

parseSource

Arguments

:: (ParserInput a, Monad m, Proxy p) 
=> Parser a r

Attoparsec parser to run on the input stream.

-> EitherP ParsingError (StateP [a] p) () (Maybe a) y' y m r

Proxy compatible with the facilities provided by Control.Proxy.Parse.

Parses one element flowing downstream.

  • In case of parsing errors, a ParsingError exception is thrown in the EitherP proxy transformer.
  • Requests more input from upstream using draw when needed.
  • Do not use this proxy if isEndOfParserInput returns True, otherwise you may get unexpected parsing errors.

Here is an example parsing loop that allows interleaving stream effects together with parse:

 loop = do
     eof <- liftP isEndOfParserInput
     unless eof $ do
         -- 1. Possibly perform some stream effects here.
         -- 2. Parse one element from the stream.
         exampleElement <- parse myExampleParser
         -- 3. Do something with exampleElement and possibly perform
         --    some more stream effects.
         -- 4. Start all over again.
         loop

parseDSource

Arguments

:: (ParserInput a, Monad m, Proxy p) 
=> Parser a b

Attoparsec parser to run on the input stream.

-> () 
-> Pipe (EitherP ParsingError (StateP [a] p)) (Maybe a) b m ()

Proxy compatible with the facilities provided by Control.Proxy.Parse.

Parses consecutive elements flowing downstream until end of input.

  • In case of parsing errors, a ParsingError exception is thrown in the EitherP proxy transformer.
  • Requests more input from upstream using draw when needed.
  • Empty input chunks flowing downstream will be discarded.

isEndOfParserInput :: (ParserInput a, Monad m, Proxy p) => StateP [a] p () (Maybe a) y' y m BoolSource

Like isEndOfInput, except it also consumes and discards leading empty ParserInput chunks.

Types

class Monoid a => ParserInput a whereSource

A class for valid Attoparsec input types: strict Text and strict ByteString.

Methods

null :: a -> BoolSource

Tests whether a is empty.

data ParsingError Source

A parsing error report, as provided by Attoparsec's Fail.

Constructors

ParsingError 

Fields

peContexts :: [String]

Contexts where the parsing error occurred.

peMessage :: String

Parsing error description message.