Portability | unknown |
---|---|
Stability | experimental |
Maintainer | bos@serpentine.com |
Safe Haskell | Safe-Infered |
Data.Attoparsec.ByteString.Internal
Contents
Description
Simple, efficient parser combinators for ByteString
strings,
loosely based on the Parsec library.
- type Parser = Parser ByteString
- type Result = IResult ByteString
- parse :: Parser a -> ByteString -> Result a
- parseOnly :: Parser a -> ByteString -> Either String a
- (<?>) :: Parser a -> String -> Parser a
- try :: Parser a -> Parser a
- module Data.Attoparsec.Combinator
- satisfy :: (Word8 -> Bool) -> Parser Word8
- satisfyWith :: (Word8 -> a) -> (a -> Bool) -> Parser a
- anyWord8 :: Parser Word8
- skip :: (Word8 -> Bool) -> Parser ()
- word8 :: Word8 -> Parser Word8
- notWord8 :: Word8 -> Parser Word8
- inClass :: String -> Word8 -> Bool
- notInClass :: String -> Word8 -> Bool
- storable :: Storable a => Parser a
- skipWhile :: (Word8 -> Bool) -> Parser ()
- string :: ByteString -> Parser ByteString
- stringTransform :: (ByteString -> ByteString) -> ByteString -> Parser ByteString
- take :: Int -> Parser ByteString
- scan :: s -> (s -> Word8 -> Maybe s) -> Parser ByteString
- takeWhile :: (Word8 -> Bool) -> Parser ByteString
- takeWhile1 :: (Word8 -> Bool) -> Parser ByteString
- takeTill :: (Word8 -> Bool) -> Parser ByteString
- takeByteString :: Parser ByteString
- takeLazyByteString :: Parser ByteString
- endOfInput :: Parser ()
- atEnd :: Parser Bool
- ensure :: Int -> Parser ByteString
- endOfLine :: Parser ()
Parser types
type Parser = Parser ByteStringSource
type Result = IResult ByteStringSource
Running parsers
parse :: Parser a -> ByteString -> Result aSource
Run a parser.
parseOnly :: Parser a -> ByteString -> Either String aSource
Run a parser that cannot be resupplied via a Partial
result.
Combinators
Name the parser, in case failure occurs.
try :: Parser a -> Parser aSource
Attempt a parse, and if it fails, rewind the input so that no input appears to have been consumed.
This combinator is provided for compatibility with Parsec. Attoparsec parsers always backtrack on failure.
module Data.Attoparsec.Combinator
Parsing individual bytes
satisfy :: (Word8 -> Bool) -> Parser Word8Source
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 aSource
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.
skip :: (Word8 -> Bool) -> Parser ()Source
The parser skip p
succeeds for any byte for which the predicate
p
returns True
.
skipDigit = skip isDigit where isDigit w = w >= 48 && w <= 57
Byte classes
inClass :: String -> Word8 -> BoolSource
Match any byte in a set.
vowel = inClass "aeiou"
Range notation is supported.
halfAlphabet = inClass "a-nA-N"
To add a literal '-'
to a set, place it at the beginning or end
of the string.
notInClass :: String -> Word8 -> BoolSource
Match any byte not in a set.
Parsing more complicated structures
Efficient string handling
skipWhile :: (Word8 -> Bool) -> Parser ()Source
Skip past input for as long as the predicate returns True
.
string :: ByteString -> Parser ByteStringSource
string s
parses a sequence of bytes that identically match
s
. Returns the parsed string (i.e. s
). This parser consumes no
input if it fails (even if a partial match).
Note: The behaviour of this parser is different to that of the
similarly-named parser in Parsec, as this one is all-or-nothing.
To illustrate the difference, the following parser will fail under
Parsec given an input of for
:
string "foo" <|> string "for"
The reason for its failure is that that the first branch is a
partial match, and will consume the letters 'f'
and 'o'
before failing. In Attoparsec, the above parser will succeed on
that input, because the failed first branch will consume nothing.
stringTransform :: (ByteString -> ByteString) -> ByteString -> Parser ByteStringSource
take :: Int -> Parser ByteStringSource
Consume exactly n
bytes of input.
scan :: s -> (s -> Word8 -> Maybe s) -> Parser ByteStringSource
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.
Note: Because this parser does not fail, do not use it with
combinators such as many
, because such parsers loop until a
failure occurs. Careless use will thus result in an infinite loop.
takeWhile :: (Word8 -> Bool) -> Parser ByteStringSource
Consume input as long as the predicate returns True
, and return
the consumed input.
This parser does not fail. It will return an empty string if the
predicate returns False
on the first byte of input.
Note: Because this parser does not fail, do not use it with
combinators such as many
, because such parsers loop until a
failure occurs. Careless use will thus result in an infinite loop.
takeWhile1 :: (Word8 -> Bool) -> Parser ByteStringSource
takeTill :: (Word8 -> Bool) -> Parser ByteStringSource
Consume input as long as the predicate returns False
(i.e. until it returns True
), and return the consumed input.
This parser does not fail. It will return an empty string if the
predicate returns True
on the first byte of input.
Note: Because this parser does not fail, do not use it with
combinators such as many
, because such parsers loop until a
failure occurs. Careless use will thus result in an infinite loop.
Consume all remaining input
takeByteString :: Parser ByteStringSource
Consume all remaining input and return it as a single string.
takeLazyByteString :: Parser ByteStringSource
Consume all remaining input and return it as a single string.
State observation and manipulation functions
Match only if all input has been consumed.
ensure :: Int -> Parser ByteStringSource
If at least n
bytes of input are available, return the current
input, otherwise fail.