Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
ByteString.Parser.Fast
Description
A fast parser combinators module.
This module is extremely bare-bones, and provides only very limited functionality.
Sample usage:
module Syslog where import ByteString.Parser.Fast import qualified Data.ByteString as BS import Data.Thyme.Clock import Control.Applicative data SyslogMsg = SyslogMsg { _syslogPrio :: {-# UNPACK #-} !Int , _syslogTS :: {-# UNPACK #-} !UTCTime , _syslogHost :: !BS.ByteString , _syslogProgram :: !BS.ByteString , _syslogPID :: !(Maybe Int) , _syslogData :: !BS.ByteString } deriving (Show, Eq) syslogMsg :: Parser SyslogMsg syslogMsg = do char '<' prio <- decimal char '>' ts <- rfc3339 char ' ' host <- charTakeWhile1 (/= ' ') char ' ' program <- charTakeWhile1 (\x -> x /= ':' && x /= '[') pid' <- optional (char '[' *> decimal <* char ']') char ':' dt <- remaining return (SyslogMsg prio ts host program pid' dt) test :: BS.ByteString -> Either ParseError SyslogMsg test = parseOnly syslogMsg
Synopsis
- type Parser = Codensity ParserM
- newtype ParserM a = Parser {
- runParser :: forall r. ByteString -> (ParseError -> r) -> (ByteString -> a -> r) -> r
- parseOnly :: Parser a -> ByteString -> Either ParseError a
- data ParseError = ParseError {
- errorUnexpected :: !(Set ErrorItem)
- errorExpected :: !(Set ErrorItem)
- data ErrorItem
- ueof :: ParseError
- ufail :: String -> ParseError
- parseError :: ByteString -> ByteString -> ParseError
- decimal :: Parser Int
- num :: Num n => Parser n
- hnum :: Num n => Parser n
- onum :: Num n => Parser n
- frac :: Fractional a => Parser a
- scientific :: Parser Double
- satisfy :: (Char -> Bool) -> Parser Char
- anyChar :: Parser Char
- char :: Char -> Parser ()
- anyWord8 :: Parser Word8
- word8 :: Word8 -> Parser ()
- string :: ByteString -> Parser ()
- quotedString :: Parser ByteString
- takeN :: Int -> Parser ByteString
- dropN :: Int -> Parser ()
- remaining :: Parser ByteString
- charTakeWhile :: (Char -> Bool) -> Parser ByteString
- charTakeWhile1 :: (Char -> Bool) -> Parser ByteString
- takeWhile :: (Word8 -> Bool) -> Parser ByteString
- takeWhile1 :: (Word8 -> Bool) -> Parser ByteString
- skipWhile :: (Word8 -> Bool) -> Parser ()
- parseYMD :: Parser Day
- parseDTime :: Parser DiffTime
- timestamp :: Parser UTCTime
- rfc3339 :: Parser UTCTime
- wlex :: (ByteString -> Maybe (a, ByteString)) -> Parser a
- pFold :: Parser a -> SimpleFold ByteString a
- isLower :: Word8 -> Bool
- getOctal :: ByteString -> Int
- getInt :: ByteString -> Int
Documentation
A parser, church encoded. The arguments to the wrapped function are:
- Input ByteString.
- A function that handles parse errors.
- A function that handles success, taking as argument the remaining input and the parser result.
Constructors
Parser | |
Fields
|
parseOnly :: Parser a -> ByteString -> Either ParseError a Source #
Runs the parser. Will return a parse error if the parser fails or if the input is not completely consumed.
Error handling
data ParseError Source #
Constructors
ParseError | |
Fields
|
Instances
Monoid ParseError Source # | |
Defined in ByteString.Parser.Fast Methods mempty :: ParseError # mappend :: ParseError -> ParseError -> ParseError # mconcat :: [ParseError] -> ParseError # | |
Semigroup ParseError Source # | |
Defined in ByteString.Parser.Fast Methods (<>) :: ParseError -> ParseError -> ParseError # sconcat :: NonEmpty ParseError -> ParseError # stimes :: Integral b => b -> ParseError -> ParseError # | |
Show ParseError Source # | |
Defined in ByteString.Parser.Fast Methods showsPrec :: Int -> ParseError -> ShowS # show :: ParseError -> String # showList :: [ParseError] -> ShowS # | |
Eq ParseError Source # | |
Defined in ByteString.Parser.Fast |
Constructors
Tokens ByteString | |
Label String |
Instances
Show ErrorItem Source # | |
Eq ErrorItem Source # | |
Ord ErrorItem Source # | |
ueof :: ParseError Source #
An error representing the unexpected end of input.
Arguments
:: ByteString | Unexpected content |
-> ByteString | Expected content |
-> ParseError |
Creates a generic parse error.
Parsing numerical values
frac :: Fractional a => Parser a Source #
Parses Fractional
numbers.
scientific :: Parser Double Source #
A fast parser for numbers of the form 5.123. Contrary to what its name
implies, it parses to Double
.
Parsing characters
string :: ByteString -> Parser () Source #
Parses the supplied string.
quotedString :: Parser ByteString Source #
Parses strings between double quotes. This functions handles the following escape sequences: \r, \n, \t, \a, \b, \", \\.
Various combinators
remaining :: Parser ByteString Source #
Parses the remaining input.
charTakeWhile :: (Char -> Bool) -> Parser ByteString Source #
Consumes the input as long as the predicate remains true.
charTakeWhile1 :: (Char -> Bool) -> Parser ByteString Source #
takeWhile :: (Word8 -> Bool) -> Parser ByteString Source #
Consumes the input as long as the predicate remains true.
takeWhile1 :: (Word8 -> Bool) -> Parser ByteString Source #
skipWhile :: (Word8 -> Bool) -> Parser () Source #
Discards the input as long as the predicate remains true.
Parsing time-related values
parseDTime :: Parser DiffTime Source #
Parses a difftime, with format HH:MM:SS
timestamp :: Parser UTCTime Source #
Parses a whole timestamp, with format YYYY-MM-DD+HH:MM:SS+CEST. This is very much *not* robust, as it only handles CET and CEST.
Interfacing with other libraries
wlex :: (ByteString -> Maybe (a, ByteString)) -> Parser a Source #
Creates a parser from the supplied function.
The first argument to the supplied function is the remaining input, and
it should return Nothing
when parsing failes, or Just
the result
along with the non-consumed input.
It works well with the bytestring-lexing library.
pFold :: Parser a -> SimpleFold ByteString a Source #
Turns any parser into a SimpleFold
.
Hacks and bits
isLower :: Word8 -> Bool Source #
Returns true when the character represents an ASCII lowercase letter.
getOctal :: ByteString -> Int Source #
Parses bytestrings as if they were representing an octal number in ASCII.
getInt :: ByteString -> Int Source #
Parses bytestrings as if they were representing a decimal number in ASCII.