Portability | unknown |
---|---|
Stability | experimental |
Maintainer | bos@serpentine.com |
Simple, efficient, character-oriented, and incremental parser
combinators for lazy L.ByteString
strings, loosely based on the
Parsec library.
Note: This module is intended for parsing text that is represented using an 8-bit character set, e.g. ASCII or ISO-8859-15. It does not deal with character encodings, multibyte characters, or wide characters. Any attempts to use characters above code point 255 will give wrong answers.
- data Parser r a
- data Result a
- = Failed String
- | Done ByteString a
- | Partial (ByteString -> Result a)
- parse :: Parser r r -> ByteString -> Result r
- parseWith :: Applicative f => f ByteString -> Parser r r -> ByteString -> f (Result r)
- parseTest :: Show r => Parser r r -> ByteString -> IO ()
- (<?>) :: Parser r a -> String -> Parser r a
- try :: Parser r a -> Parser r a
- satisfy :: (Char -> Bool) -> Parser r Char
- letter :: Parser r Char
- digit :: Parser r Char
- anyChar :: Parser r Char
- space :: Parser r Char
- char :: Char -> Parser r Char
- notChar :: Char -> Parser r Char
- inClass :: String -> Char -> Bool
- notInClass :: String -> Char -> Bool
- string :: ByteString -> Parser r ByteString
- skipSpace :: Parser r ()
- skipWhile :: (Char -> Bool) -> Parser r ()
- takeCount :: Int -> Parser r ByteString
- takeTill :: (Char -> Bool) -> Parser r ByteString
- takeWhile :: (Char -> Bool) -> Parser r ByteString
- endOfLine :: Parser r ()
- int :: Parser r Int
- integer :: Parser r Integer
- double :: Parser r Double
- endOfInput :: Parser r ()
- pushBack :: ByteString -> Parser r ()
- yield :: Parser r ()
- module Data.Attoparsec.Combinator
Parser types
The parser type.
Monad (Parser r) | |
Functor (Parser r) | |
MonadPlus (Parser r) | |
Applicative (Parser r) | |
Alternative (Parser r) |
The result of a partial parse.
Failed String | The parse failed, with the given error message. |
Done ByteString a | The parse succeeded, producing the given
result. The |
Partial (ByteString -> Result a) | The parse ran out of data before finishing. To resume the parse, pass more data to the given continuation. |
Running parsers
parse :: Parser r r -> ByteString -> Result rSource
Run a parser.
:: Applicative f | |
=> f ByteString | resupply parser with input |
-> Parser r r | parser to run |
-> ByteString | initial input |
-> f (Result r) |
Run a parser, using the given function to resupply it with input.
Here's an example that shows how to parse data from a socket, using
Johan Tibbell's network-bytestring
package.
import qualified Data.ByteString.Lazy as L import Data.Attoparsec.Incremental (Parser, Result, parseWith) import Network.Socket.ByteString.Lazy (recv_) import Network.Socket (Socket) netParse :: Parser r r -> Socket -> IO (Result r) netParse p sock = parseWith (recv_ sock 65536) p L.empty
parseTest :: Show r => Parser r r -> ByteString -> IO ()Source
Try out a parser, and print its result.
Combinators
Name the parser, in case failure occurs.
Parsing individual characters
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
string :: ByteString -> Parser r ByteStringSource
Match a literal string exactly.
takeCount :: Int -> Parser r ByteStringSource
Return exactly the given number of bytes. If not enough are available, fail.
takeTill :: (Char -> Bool) -> Parser r ByteStringSource
Consume characters while the predicate fails.
takeWhile :: (Char -> Bool) -> Parser r ByteStringSource
Consume characters while the predicate succeeds.
Text parsing
endOfLine :: Parser r ()Source
Match the end of a line. This may be any of a newline character, a carriage return character, or a carriage return followed by a newline.
Numeric parsers
State observation and manipulation functions
endOfInput :: Parser r ()Source
Succeed if we have reached the end of the input string.
pushBack :: ByteString -> Parser r ()Source
Force the given string to appear next in the input stream.
Resume our caller, handing back a Partial
result. This function
is probably not useful, but provided for completeness.
Combinators
module Data.Attoparsec.Combinator