attoparsec-0.10.1.1: Fast combinator parsing for bytestrings

Portabilityunknown
Stabilityexperimental
Maintainerbos@serpentine.com
Safe HaskellSafe-Infered

Data.Attoparsec.Text.Internal

Contents

Description

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

Synopsis

Parser types

Running parsers

parse :: Parser a -> Text -> Result aSource

Run a parser.

parseOnly :: Parser a -> Text -> 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 characters

satisfy :: (Char -> Bool) -> Parser CharSource

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

digit = satisfy isDigit
    where isDigit c = c >= '0' && c <= '9'

satisfyWith :: (Char -> a) -> (a -> Bool) -> Parser aSource

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

anyChar :: Parser CharSource

Match any character.

skip :: (Char -> Bool) -> Parser ()Source

The parser skip p succeeds for any character for which the predicate p returns True.

skipDigit = skip isDigit
    where isDigit c = c >= '0' && c <= '9'

char :: Char -> Parser CharSource

Match a specific character.

notChar :: Char -> Parser CharSource

Match any character except the given one.

Character classes

inClass :: String -> Char -> BoolSource

Match any character 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 -> Char -> BoolSource

Match any character not in a set.

Efficient string handling

skipWhile :: (Char -> Bool) -> Parser ()Source

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

string :: Text -> Parser TextSource

string s parses a sequence of characters 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 TextSource

Consume exactly n characters of input.

scan :: s -> (s -> Char -> Maybe s) -> Parser TextSource

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 character 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 character 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 :: (Char -> Bool) -> Parser TextSource

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 character 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 :: (Char -> Bool) -> Parser TextSource

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 character of input: it will fail if the predicate never returns True or if there is no input left.

takeTill :: (Char -> Bool) -> Parser TextSource

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 character 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

takeText :: Parser TextSource

Consume all remaining input and return it as a single string.

takeLazyText :: Parser TextSource

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 TextSource

If at least n characters 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".