HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleio-streams-1.5.2.2Haskell2010

System.IO.Streams.Attoparsec.ByteString

This module provides support for parsing values from ByteString InputStreams using attoparsec. Since: 1.4.0.0.

  • 1 type
  • 2 values
  • Packageio-streams-1.5.2.2
  • Exports3
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceByteString.hs

Parsing

3 declarations

Supplies an attoparsec Parser with an InputStream, returning the final parsed value or throwing a ParseException if parsing fails.

parseFromStream consumes only as much input as necessary to satisfy the Parser: any unconsumed input is pushed back onto the InputStream.

If the Parser exhausts the InputStream, the end-of-stream signal is sent to attoparsec.

Example:

ghci> import Data.Attoparsec.ByteString.Char8
ghci> is <- System.IO.Streams.fromList ["12345xxx" :: ByteString]
ghci> parseFromStream (takeWhile isDigit) is
"12345"
ghci> System.IO.Streams.read is
Just "xxx"

Given a Parser yielding values of type Maybe r, transforms an InputStream over byte strings to an InputStream yielding values of type r.

If the parser yields Just x, then x will be passed along downstream, and if the parser yields Nothing, that will be interpreted as end-of-stream.

Upon a parse error, parserToInputStream will throw a ParseException.

Example:

ghci> import Control.Applicative
ghci> import Data.Attoparsec.ByteString.Char8
ghci> is <- System.IO.Streams.fromList ["1 2 3 4 5" :: ByteString]
ghci> let parser = (Data.Attoparsec.ByteString.Char8.endOfInput >> pure Nothing) <|> (Just <$> (skipWhile isSpace *> decimal))
ghci> parserToInputStream parser is >>= System.IO.Streams.toList
[1,2,3,4,5]
ghci> is' <- System.IO.Streams.fromList ["1 2xx3 4 5" :: ByteString] >>= parserToInputStream parser
ghci> read is'
Just 1
ghci> read is'
Just 2
ghci> read is'
*** Exception: Parse exception: Failed reading: takeWhile1