Z-Data-0.9.0.0: Array, vector and text
Copyright(c) Dong Han 2017-2019
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.Data.Parser.Base

Description

This module provide internal data types for a simple resumable Parser, which is suitable for binary protocol and simple textual protocol parsing. Parser extensively works on on Bytes, which is same to Text representation.

Synopsis

Parser types

data Result e r Source #

Simple parsing result, that represent respectively:

  • Success: the remaining unparsed data and the parsed value
  • Failure: the remaining unparsed data and the error message
  • Partial: that need for more input data, supply empty bytes to indicate endOfInput

Constructors

Success r !Bytes 
Failure e !Bytes 
Partial (ParseStep e r) 

Instances

Instances details
Functor (Result e) Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

fmap :: (a -> b) -> Result e a -> Result e b #

(<$) :: a -> Result e b -> Result e a #

(Show e, Show a) => Show (Result e a) Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

showsPrec :: Int -> Result e a -> ShowS #

show :: Result e a -> String #

showList :: [Result e a] -> ShowS #

type ParseError = [Text] Source #

Type alias for error message

type ParseStep e r = Bytes -> Result e r Source #

A parse step consumes Bytes and produce Result.

newtype Parser a Source #

Simple CPSed parser

A parser takes a failure continuation, and a success one, while the success continuation is usually composed by Monad instance, the failure one is more like a reader part, which can be modified via <?>. If you build parsers from ground, a pattern like this can be used:

   xxParser = do
     ensureN errMsg ...            -- make sure we have some bytes
     Parser $  kf k s inp ->      -- fail continuation, success continuation, state token and input
       ...
       ... kf errMsg (if input not OK)
       ... k s ... (if we get something useful for next parser)
 

Constructors

Parser 

Fields

Instances

Instances details
Monad Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

(>>=) :: Parser a -> (a -> Parser b) -> Parser b #

(>>) :: Parser a -> Parser b -> Parser b #

return :: a -> Parser a #

Functor Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

fmap :: (a -> b) -> Parser a -> Parser b #

(<$) :: a -> Parser b -> Parser a #

MonadFail Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

fail :: String -> Parser a #

Applicative Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

pure :: a -> Parser a #

(<*>) :: Parser (a -> b) -> Parser a -> Parser b #

liftA2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c #

(*>) :: Parser a -> Parser b -> Parser b #

(<*) :: Parser a -> Parser b -> Parser a #

Alternative Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

empty :: Parser a #

(<|>) :: Parser a -> Parser a -> Parser a #

some :: Parser a -> Parser [a] #

many :: Parser a -> Parser [a] #

MonadPlus Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Methods

mzero :: Parser a #

mplus :: Parser a -> Parser a -> Parser a #

PrimMonad Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

Associated Types

type PrimState Parser #

Methods

primitive :: (State# (PrimState Parser) -> (# State# (PrimState Parser), a #)) -> Parser a #

type PrimState Parser Source # 
Instance details

Defined in Z.Data.Parser.Base

(<?>) :: Text -> Parser a -> Parser a infixr 0 Source #

Running a parser

parse :: Parser a -> Bytes -> (Bytes, Either ParseError a) Source #

Parse the complete input, without resupplying, return the rest bytes

parse' :: Parser a -> Bytes -> Either ParseError a Source #

Parse the complete input, without resupplying

parseChunk :: Parser a -> Bytes -> Result ParseError a Source #

Parse an input chunk

type ParseChunks m err x = m Bytes -> Bytes -> m (Bytes, Either err x) Source #

Type alias for a streaming parser, draw chunk from Monad m with a initial chunk, return result in Either err x.

parseChunks :: Monad m => (Bytes -> Result e a) -> ParseChunks m e a Source #

Run a chunk parser with an initial input string, and a monadic action that can supply more input if needed.

finishParsing :: Result ParseError a -> (Bytes, Either ParseError a) Source #

Finish parsing and fetch result, feed empty bytes if it's Partial result.

runAndKeepTrack :: Parser a -> Parser (Result ParseError a, [Bytes]) Source #

Run a parser and keep track of all the input chunks it consumes. Once it's finished, return the final result (always Success or Failure) and all consumed chunks.

match :: Parser a -> Parser (Bytes, a) Source #

Return both the result of a parse and the portion of the input that was consumed while it was being parsed.

Basic parsers

ensureN :: Int -> Text -> Parser () Source #

Ensure that there are at least n bytes available. If not, the computation will escape with Partial.

Since this parser is used in many other parsers, an extra error param is provide to attach custom error info.

endOfInput :: Parser () Source #

Test whether all input has been consumed, i.e. there are no remaining undecoded bytes. Fail if not atEnd.

currentChunk :: Parser Bytes Source #

Get current input chunk, draw new chunk if neccessary. null means EOF.

Note this is different from takeRemaining, currentChunk only return what's left in current input chunk.

atEnd :: Parser Bool Source #

Test whether all input has been consumed, i.e. there are no remaining undecoded bytes.

Primitive decoders

decodePrim :: forall a. Unaligned a => Parser a Source #

Decode a primitive type in host byte order.

newtype BE a Source #

big endianess wrapper

Constructors

BE 

Fields

Instances

Instances details
Eq a => Eq (BE a) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Methods

(==) :: BE a -> BE a -> Bool #

(/=) :: BE a -> BE a -> Bool #

Show a => Show (BE a) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Methods

showsPrec :: Int -> BE a -> ShowS #

show :: BE a -> String #

showList :: [BE a] -> ShowS #

Unaligned (BE Char) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Double) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Float) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Int) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Int16) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Int32) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Int64) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Word) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Word16) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Word32) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (BE Word64) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

newtype LE a Source #

little endianess wrapper

Constructors

LE 

Fields

Instances

Instances details
Eq a => Eq (LE a) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Methods

(==) :: LE a -> LE a -> Bool #

(/=) :: LE a -> LE a -> Bool #

Show a => Show (LE a) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Methods

showsPrec :: Int -> LE a -> ShowS #

show :: LE a -> String #

showList :: [LE a] -> ShowS #

Unaligned (LE Char) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Double) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Float) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Int) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Int16) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Int32) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Int64) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Word) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Word16) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Word32) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

Unaligned (LE Word64) Source # 
Instance details

Defined in Z.Data.Array.Unaligned

decodePrimLE :: forall a. Unaligned (LE a) => Parser a Source #

Decode a primitive type in little endian.

decodePrimBE :: forall a. Unaligned (BE a) => Parser a Source #

Decode a primitive type in big endian.

More parsers

scan :: s -> (s -> Word8 -> Maybe s) -> Parser (Bytes, s) Source #

A stateful scanner. The predicate consumes and transforms a state argument, and each transformed state is passed to successive invocations of the predicate on each byte of the input until one returns Nothing or the input ends.

This parser does not fail. It will return an empty string if the predicate returns Nothing on the first byte of input.

scanChunks :: forall s. s -> (s -> Bytes -> Either s (Bytes, Bytes, s)) -> Parser (Bytes, s) Source #

Similar to scan, but working on Bytes chunks, The predicate consumes a Bytes chunk and transforms a state argument, and each transformed state is passed to successive invocations of the predicate on each chunk of the input until one chunk got splited to Right (V.Bytes, V.Bytes) or the input ends.

peekMaybe :: Parser (Maybe Word8) Source #

Match any byte, to perform lookahead. Returns Nothing if end of input has been reached. Does not consume any input.

peek :: Parser Word8 Source #

Match any byte, to perform lookahead. Does not consume any input, but will fail if end of input has been reached.

satisfy :: (Word8 -> Bool) -> Parser Word8 Source #

The parser satisfy p succeeds for any byte for which the predicate p returns True. Returns the byte that is actually parsed.

digit = satisfy isDigit
    where isDigit w = w >= 48 && w <= 57

satisfyWith :: (Word8 -> a) -> (a -> Bool) -> Parser a Source #

The parser satisfyWith f p transforms a byte, and succeeds if the predicate p returns True on the transformed value. The parser returns the transformed byte that was parsed.

anyWord8 :: Parser Word8 Source #

Return a byte, this is an alias to decodePrim Word8@.

word8 :: Word8 -> Parser () Source #

Match a specific byte.

char8 :: Char -> Parser () Source #

Match a specific 8bit char.

anyChar8 :: Parser Char Source #

Take a byte and return as a 8bit char.

anyCharUTF8 :: Parser Char Source #

Decode next few bytes as an UTF8 char.

Don't use this method as UTF8 decoder, it's slower than validate.

charUTF8 :: Char -> Parser () Source #

Match a specific UTF8 char.

char7 :: Char -> Parser () Source #

Match a specific 7bit char.

anyChar7 :: Parser Char Source #

Take a byte and return as a 7bit char, fail if exceeds 0x7F.

skipWord8 :: Parser () Source #

Skip a byte.

endOfLine :: Parser () Source #

Match either a single newline byte '\n', or a carriage return followed by a newline byte "\r\n".

skip :: Int -> Parser () Source #

skip N bytes.

skipWhile :: (Word8 -> Bool) -> Parser () Source #

Skip past input for as long as the predicate returns True.

skipSpaces :: Parser () Source #

Skip over white space using isSpace.

takeN :: (Word8 -> Bool) -> Int -> Parser Bytes Source #

Similar to take, but requires the predicate to succeed on next N bytes of input, and take N bytes(no matter if N+1 byte satisfy predicate or not).

takeTill :: (Word8 -> Bool) -> Parser Bytes Source #

Consume input as long as the predicate returns False or reach the end of input, and return the consumed input.

takeWhile :: (Word8 -> Bool) -> Parser Bytes Source #

Consume input as long as the predicate returns True or reach the end of input, and return the consumed input.

takeWhile1 :: (Word8 -> Bool) -> Parser Bytes Source #

Similar to takeWhile, but requires the predicate to succeed on at least one byte of input: it will fail if the predicate never returns True or reach the end of input

takeRemaining :: Parser Bytes Source #

Take all the remaining input chunks and return as Bytes.

bytes :: Bytes -> Parser () Source #

bytes s parses a sequence of bytes that identically match s.

bytesCI :: Bytes -> Parser () Source #

Same as bytes but ignoring ASCII case.

text :: Text -> Parser () Source #

text s parses a sequence of UTF8 bytes that identically match s.

Error reporting

fail' :: Text -> Parser a Source #

Text version of fail.

failWithInput :: (Bytes -> Text) -> Parser a Source #

Similar to fail`, but can produce error message with current input chunk.

unsafeLiftIO :: IO a -> Parser a Source #

Unsafely lifted an IO action into Parser.

This is only for debugging purpose(logging, etc). Don't mix compuation from realworld to parsing result, otherwise parsing is not deterministic.

Specialized primitive parser

decodeWord :: Parser Word Source #

Decode Word in host endian order.

decodeWord64 :: Parser Word64 Source #

Decode Word64 in host endian order.

decodeWord32 :: Parser Word32 Source #

Decode Word32 in host endian order.

decodeWord16 :: Parser Word16 Source #

Decode Word16 in host endian order.

decodeWord8 :: Parser Word8 Source #

Decode Word8 in host endian order.

decodeInt :: Parser Int Source #

Decode Int in host endian order.

decodeInt64 :: Parser Int64 Source #

Decode Int64 in host endian order.

decodeInt32 :: Parser Int32 Source #

Decode Int32 in host endian order.

decodeInt16 :: Parser Int16 Source #

Decode Int16 in host endian order.

decodeInt8 :: Parser Int8 Source #

Decode Int8 in host endian order.

decodeDouble :: Parser Double Source #

Decode Double in host endian order.

decodeFloat :: Parser Float Source #

Decode Float in host endian order.

decodeWordLE :: Parser Word Source #

Decode Word in little endian order.

decodeWord64LE :: Parser Word64 Source #

Decode Word64 in little endian order.

decodeWord32LE :: Parser Word32 Source #

Decode Word32 in little endian order.

decodeWord16LE :: Parser Word16 Source #

Decode Word16 in little endian order.

decodeIntLE :: Parser Int Source #

Decode Int in little endian order.

decodeInt64LE :: Parser Int64 Source #

Decode Int64 in little endian order.

decodeInt32LE :: Parser Int32 Source #

Decode Int32 in little endian order.

decodeInt16LE :: Parser Int16 Source #

Decode Int16 in little endian order.

decodeDoubleLE :: Parser Double Source #

Decode Double in little endian order.

decodeFloatLE :: Parser Float Source #

Decode Float in little endian order.

decodeWordBE :: Parser Word Source #

Decode Word in big endian order.

decodeWord64BE :: Parser Word64 Source #

Decode Word64 in big endian order.

decodeWord32BE :: Parser Word32 Source #

Decode Word32 in big endian order.

decodeWord16BE :: Parser Word16 Source #

Decode Word16 in big endian order.

decodeIntBE :: Parser Int Source #

Decode Int in big endian order.

decodeInt64BE :: Parser Int64 Source #

Decode Int64 in big endian order.

decodeInt32BE :: Parser Int32 Source #

Decode Int32 in big endian order.

decodeInt16BE :: Parser Int16 Source #

Decode Int16 in big endian order.

decodeDoubleBE :: Parser Double Source #

Decode Double in big endian order.

decodeFloatBE :: Parser Float Source #

Decode Float in big endian order.