| ||||||||||||
| ||||||||||||
| ||||||||||||
Description | ||||||||||||
Simple, efficient combinator parsing for ByteString strings, loosely based on the Parsec library. | ||||||||||||
Synopsis | ||||||||||||
Differences from Parsec | ||||||||||||
Compared to Parsec 3, Attoparsec makes several tradeoffs. It is not intended for, or ideal for, all possible uses.
| ||||||||||||
Performance considerations | ||||||||||||
If you write an Attoparsec-based parser carefully, it can be realistic to expect it to perform within a factor of 2 of a hand-rolled C parser (measuring megabytes parsed per second). To actually achieve high performance, there are a few guidelines that it is useful to follow. Use the ByteString-oriented parsers whenever possible, e.g. takeWhile1 instead of many1 anyWord8. There is about a factor of 100 difference in performance between the two kinds of parser. For very simple byte-testing predicates, write them by hand instead of using inClass or notInClass. For instance, both of these predicates test for an end-of-line byte, but the first is much faster than the second: endOfLine_fast w = w == 13 || w == 10 endOfLine_slow = inClass "\r\n" Make active use of benchmarking and profiling tools to measure, find the problems with, and improve the performance of your parser. | ||||||||||||
Parser types | ||||||||||||
data Parser a | ||||||||||||
| ||||||||||||
data Result r | ||||||||||||
| ||||||||||||
Typeclass instances | ||||||||||||
The Parser type is an instance of the following classes:
The Result type is an instance of Functor, where fmap transforms the value in a Done result. | ||||||||||||
Running parsers | ||||||||||||
parse :: Parser a -> ByteString -> Result a | ||||||||||||
Run a parser and return its result. | ||||||||||||
feed :: Result r -> ByteString -> Result r | ||||||||||||
If a parser has returned a Partial result, supply it with more input. | ||||||||||||
parseWith | ||||||||||||
| ||||||||||||
parseTest :: Show a => Parser a -> ByteString -> IO () | ||||||||||||
Run a parser and print its result to standard output. | ||||||||||||
Result conversion | ||||||||||||
maybeResult :: Result r -> Maybe r | ||||||||||||
Convert a Result value to a Maybe value. A Partial result is treated as failure. | ||||||||||||
eitherResult :: Result r -> Either String r | ||||||||||||
Convert a Result value to an Either value. A Partial result is treated as failure. | ||||||||||||
Combinators | ||||||||||||
(<?>) | ||||||||||||
| ||||||||||||
try :: Parser a -> Parser a | ||||||||||||
Attempt a parse, and if it fails, rewind the input so that no input appears to have been consumed. This combinator is useful in cases where a parser might consume some input before failing, i.e. the parser needs arbitrary lookahead. The downside to using this combinator is that it can retain input for longer than is desirable. | ||||||||||||
module Data.Attoparsec.Combinator | ||||||||||||
Parsing individual bytes | ||||||||||||
word8 :: Word8 -> Parser Word8 | ||||||||||||
Match a specific byte. | ||||||||||||
anyWord8 :: Parser Word8 | ||||||||||||
Match any byte. | ||||||||||||
notWord8 :: Word8 -> Parser Word8 | ||||||||||||
Match any byte except the given one. | ||||||||||||
satisfy :: (Word8 -> Bool) -> Parser Word8 | ||||||||||||
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 | ||||||||||||
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 () | ||||||||||||
The parser skip p succeeds for any byte for which the predicate p returns True. digit = satisfy isDigit where isDigit w = w >= 48 && w <= 57 | ||||||||||||
Byte classes | ||||||||||||
inClass :: String -> Word8 -> Bool | ||||||||||||
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 -> Bool | ||||||||||||
Match any byte not in a set. | ||||||||||||
Efficient string handling | ||||||||||||
string :: ByteString -> Parser ByteString | ||||||||||||
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. | ||||||||||||
skipWhile :: (Word8 -> Bool) -> Parser () | ||||||||||||
Skip past input for as long as the predicate returns True. | ||||||||||||
take :: Int -> Parser ByteString | ||||||||||||
Consume exactly n bytes of input. | ||||||||||||
takeWhile :: (Word8 -> Bool) -> Parser ByteString | ||||||||||||
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 ByteString | ||||||||||||
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 ByteString | ||||||||||||
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. | ||||||||||||
State observation and manipulation functions | ||||||||||||
endOfInput :: Parser () | ||||||||||||
Match only if all input has been consumed. | ||||||||||||
ensure :: Int -> Parser () | ||||||||||||
Succeed only if at least n bytes of input are available. | ||||||||||||
Produced by Haddock version 2.7.2 |