attoparsec-0.10.1.1: Fast combinator parsing for bytestrings

Portabilityunknown
Stabilityexperimental
Maintainerbos@serpentine.com
Safe HaskellSafe-Infered

Data.Attoparsec.ByteString.Internal

Contents

Description

Simple, efficient parser combinators for ByteString strings, loosely based on the Parsec library.

Synopsis

Parser types

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

(<?>)Source

Arguments

:: Parser a 
-> String

the name to use if parsing fails

-> Parser a 

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.

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.

anyWord8 :: Parser Word8Source

Match any byte.

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

word8 :: Word8 -> Parser Word8Source

Match a specific byte.

notWord8 :: Word8 -> Parser Word8Source

Match any byte except the given one.

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.

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

Consume input as long as the predicate returns True, and return the consumed input.

This parser requires the predicate to succeed on at least one byte of input: it will fail if the predicate never returns True or if there is no input left.

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

endOfInput :: Parser ()Source

Match only if all input has been consumed.

atEnd :: Parser BoolSource

Return an indication of whether the end of input has been reached.

ensure :: Int -> Parser ByteStringSource

If at least n bytes of input are available, return the current input, otherwise fail.

Utilities

endOfLine :: Parser ()Source

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