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

Moduledata-serializer-0.3.5Haskell2010

Data.Deserializer

Deserialization monad and deserializable types.

  • 5 types
  • 5 classes
  • 68 values

Deserialization monad

3 declarations
classclass (Monad μ, Parsing μ) => Deserializer (μ :: Type -> Type) where
#

Deserialization monad.

Methods

  • endian :: Proxy μ -> Endian

    Default byte order of the deserializer.

  • word8 :: μ Word8

    Deserialze a byte.

  • word16 :: μ Word16

    Deserialize an unsigned 16-bit integer in default byte order.

  • word32 :: μ Word32

    Deserialize an unsigned 32-bit integer in default byte order.

  • word64 :: μ Word64

    Deserialize an unsigned 64-bit integer in default byte order.

  • word16L :: μ Word16

    Deserialize an unsigned 16-bit integer in little endian.

  • word16B :: μ Word16

    Deserialize an unsigned 16-bit integer in big endian.

  • word32L :: μ Word32

    Deserialize an unsigned 32-bit integer in little endian.

  • word32B :: μ Word32

    Deserialize an unsigned 32-bit integer in big endian.

  • word64L :: μ Word64

    Deserialize an unsigned 64-bit integer in little endian.

  • word64B :: μ Word64

    Deserialize an unsigned 64-bit integer in big endian.

  • satisfy :: (Word8 -> Bool) -> μ Word8

    satisfy p deserializes a byte that satisfies the predicate p, failing otherwise.

  • byte :: Word8 -> μ Word8

    Deserialize the specified byte value, failing on any other input.

  • notByte :: Word8 -> μ Word8

    notByte c deserializes any byte that is not equal to c, failing if c is encountered.

  • bytes :: ByteString -> μ ByteString

    bytes bs deserializes a sequence of bytes given by bs, failing on any other input.

  • skip :: Int -> μ ()

    Skip exactly the given number of bytes.

  • ensure :: Int -> μ ByteString

    ensure n checks that the input has at least n more bytes and returns a portion of the input of length greater or equal to n (without consuming it).

  • ensure_ :: Int -> μ ()

    ensure_ n fails if the input has less than n more bytes.

  • check :: Int -> μ Bool

    check n returns True if the input has at least n more bytes.

  • take :: Int -> μ ByteString

    Consume exactly the given number of bytes.

  • chunk :: μ ByteString

    Consume a portion of the input (the size of the returned ByteString is implementation dependent). Empty result means that the eof is reached.

  • isolate :: Int -> μ α -> μ α

    isolate n d feeds the next n bytes to the deserializer d. If d consumes less or more that n bytes, isolate will fail.

Instances4Deserializer
newtypenewtype BinaryDeserializer α
#

A wrapper around the Get monad (to avoid orphan instances).

Instances9Monad, Functor, Applicative, Alternative, Parsing, LookAheadParsing, …
newtypenewtype CerealDeserializer α
#

A wrapper around the Get monad (to avoid orphan instances).

Instances9Monad, Functor, Applicative, Alternative, Parsing, LookAheadParsing, …

Binary word parsing

valueword :: Deserializer μ => μ Word
#

Deserialize an unsigned native-sized integer in serializer default byte order.

valuewordH :: Deserializer μ => μ Word
#

Deserialize an unsigned native-sized integer in host byte order.

valueint16 :: Deserializer μ => μ Int16
#

Deserialize a signed 16-bit integer in serializer default byte order.

valueint32 :: Deserializer μ => μ Int32
#

Deserialize a signed 32-bit integer in serializer default byte order.

valueint64 :: Deserializer μ => μ Int64
#

Deserialize a signed 64-bit integer in serializer default byte order.

valueint :: Deserializer μ => μ Int
#

Deserialize a signed native-sized integer in serializer default byte order.

valueintL :: Deserializer μ => μ Int
#

Deserialize a signed native-sized integer in little endian.

valueintB :: Deserializer μ => μ Int
#

Deserialize a signed native-sized integer in big endian.

valueintH :: Deserializer μ => μ Int
#

Deserialize a signed native-sized integer in host byte order.

Parsing combinators

classclass Alternative m => Parsing (m :: Type -> Type) where
#

Additional functionality needed to describe parsers independent of input type.

Methods

  • try :: m a -> m a

    Take a parser that may consume input, and on failure, go back to where we started and fail as if we didn't consume input.

  • (<?>) :: m a -> String -> m ainfixr 0

    Give a parser a name

  • skipMany :: m a -> m ()

    A version of many that discards its input. Specialized because it can often be implemented more cheaply.

  • skipSome :: m a -> m ()

    skipSome p applies the parser p one or more times, skipping its result. (aka skipMany1 in parsec)

  • unexpected :: String -> m a

    Used to emit an error on an unexpected token

  • eof :: m ()

    This parser only succeeds at the end of the input. This is not a primitive parser but it is defined using notFollowedBy.

     eof  = notFollowedBy anyChar <?> "end of input"
  • notFollowedBy :: Show a => m a -> m ()

    notFollowedBy p only succeeds when parser p fails. This parser does not consume any input. This parser can be used to implement the 'longest match' rule. For example, when recognizing keywords (for example let), we want to make sure that a keyword is not followed by a legal identifier character, in which case the keyword is actually an identifier (for example lets). We can program this behaviour as follows:

     keywordLet  = try $ string "let" <* notFollowedBy alphaNum
Instances19Parsing, …
valuecount :: Applicative m => Int -> m a -> m [a]
#

count n p parses n occurrences of p. If n is smaller or equal to zero, the parser equals to return []. Returns a list of n values returned by p.

methodmany :: f a -> f [a]
#

Zero or more.

Examples
Example1 expression
many (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression
many NothingJust []
Example1 expression
take 5 <$> many (Just 1)* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case many parser will attempt to parse parser zero or more times until it fails.

methodsome :: f a -> f [a]
#

One or more.

Examples
Example1 expression
some (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression
some Nothingnothing
Example1 expression
take 5 <$> some (Just 1)* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case some parser will attempt to parse parser one or more times until it fails.

valueendBy :: Alternative m => m a -> m sep -> m [a]
#

endBy p sep parses zero or more occurrences of p, separated and ended by sep. Returns a list of values returned by p.

  cStatements  = cStatement `endBy` semi
valuesepBy :: Alternative m => m a -> m sep -> m [a]
#

sepBy p sep parses zero or more occurrences of p, separated by sep. Returns a list of values returned by p.

 commaSep p  = p `sepBy` (symbol ",")
valueoptional :: Alternative f => f a -> f (Maybe a)
#

One or none.

It is useful for modelling any computation that is allowed to fail.

Examples

Using the Alternative instance of Control.Monad.Except, the following functions:

Example1 expression
import Control.Monad.Except
Example2 expressions
canFail = throwError "it failed" :: Except String Intfinal = return 42                :: Except String Int

Can be combined by allowing the first function to fail:

Example1 expression
runExcept $ canFail *> finalLeft "it failed"
Example1 expression
runExcept $ optional canFail *> finalRight 42
valuebetween :: Applicative m => m bra -> m ket -> m a -> m a
#

between open close p parses open, followed by p and close. Returns the value returned by p.

 braces  = between (symbol "{") (symbol "}")
valuechainl :: Alternative m => m a -> m (a -> a -> a) -> a -> m a
#

chainl p op x parses zero or more occurrences of p, separated by op. Returns a value obtained by a left associative application of all functions returned by op to the values returned by p. If there are zero occurrences of p, the value x is returned.

valuechainl1 :: Alternative m => m a -> m (a -> a -> a) -> m a
#

chainl1 p op x parses one or more occurrences of p, separated by op Returns a value obtained by a left associative application of all functions returned by op to the values returned by p. . This parser can for example be used to eliminate left recursion which typically occurs in expression grammars.

 expr   = term   `chainl1` addop
 term   = factor `chainl1` mulop
 factor = parens expr <|> integer

 mulop  = (*) <$ symbol "*"
      <|> div <$ symbol "/"

 addop  = (+) <$ symbol "+"
      <|> (-) <$ symbol "-"
valuechainr :: Alternative m => m a -> m (a -> a -> a) -> a -> m a
#

chainr p op x parses zero or more occurrences of p, separated by op Returns a value obtained by a right associative application of all functions returned by op to the values returned by p. If there are no occurrences of p, the value x is returned.

valuechainr1 :: Alternative m => m a -> m (a -> a -> a) -> m a
#

chainr1 p op x parses one or more occurrences of p, separated by op Returns a value obtained by a right associative application of all functions returned by op to the values returned by p.

valuechoice :: Alternative m => [m a] -> m a
#

choice ps tries to apply the parsers in the list ps in order, until one of them succeeds. Returns the value of the succeeding parser.

valueendBy1 :: Alternative m => m a -> m sep -> m [a]
#

endBy1 p sep parses one or more occurrences of p, separated and ended by sep. Returns a list of values returned by p.

valueendByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a)
#

endByNonEmpty p sep parses one or more occurrences of p, separated and ended by sep. Returns a non-empty list of values returned by p.

valuemanyTill :: Alternative m => m a -> m end -> m [a]
#

manyTill p end applies parser p zero or more times until parser end succeeds. Returns the list of values returned by p. This parser can be used to scan comments:

 simpleComment   = do{ string "<!--"
                     ; manyTill anyChar (try (string "-->"))
                     }

Note the overlapping parsers anyChar and string "-->", and therefore the use of the try combinator.

valueoption :: Alternative m => a -> m a -> m a
#

option x p tries to apply parser p. If p fails without consuming input, it returns the value x, otherwise the value returned by p.

 priority = option 0 (digitToInt <$> digit)
valuesepBy1 :: Alternative m => m a -> m sep -> m [a]
#

sepBy1 p sep parses one or more occurrences of p, separated by sep. Returns a list of values returned by p.

valuesepByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a)
#

sepByNonEmpty p sep parses one or more occurrences of p, separated by sep. Returns a non-empty list of values returned by p.

valuesepEndBy :: Alternative m => m a -> m sep -> m [a]
#

sepEndBy p sep parses zero or more occurrences of p, separated and optionally ended by sep, ie. haskell style statements. Returns a list of values returned by p.

 haskellStatements  = haskellStatement `sepEndBy` semi
valuesepEndBy1 :: Alternative m => m a -> m sep -> m [a]
#

sepEndBy1 p sep parses one or more occurrences of p, separated and optionally ended by sep. Returns a list of values returned by p.

valuesepEndByNonEmpty :: Alternative m => m a -> m sep -> m (NonEmpty a)
#

sepEndByNonEmpty p sep parses one or more occurrences of p, separated and optionally ended by sep. Returns a non-empty list of values returned by p.

valueskipOptional :: Alternative m => m a -> m ()
#

skipOptional p tries to apply parser p. It will parse p or nothing. It only fails if p fails after consuming input. It discards the result of p. (Plays the role of parsec's optional, which conflicts with Applicative's optional)

valuesurroundedBy :: Applicative m => m a -> m sur -> m a
#

p `surroundedBy` f is p surrounded by f. Shortcut for between f f p. As in between, returns the value returned by p.

classclass Parsing m => LookAheadParsing (m :: Type -> Type) where
#

Additional functionality needed to describe parsers independent of input type.

Methods

  • lookAhead :: m a -> m a

    lookAhead p parses p without consuming any input.

Instances16LookAheadParsing, …

Endian deserializers

newtypenewtype LittleEndianDeserializer (μ :: Type -> Type) α
#

Deserializer wrapper with little endian default byte order.

Instances10Monad, Functor, Applicative, Alternative, Parsing, LookAheadParsing, …
newtypenewtype BigEndianDeserializer (μ :: Type -> Type) α
#

Deserializer wrapper with big endian default byte order.

Instances10Monad, Functor, Applicative, Alternative, Parsing, LookAheadParsing, …

Default deserializer

Deserializable types

18 declarations
classclass Deserializable α where
#

Deserializable type. get must not rely on eof.

Methods

Instances16Deserializable, …
classclass RestDeserializable α where
#

Deserializable type. getRest must consume all the remaining input or fail.

Methods

Instances7RestDeserializable, …