Portability | unknown |
---|---|
Stability | experimental |
Maintainer | bos@serpentine.com |
Safe Haskell | Safe-Infered |
Data.Attoparsec.Text.Internal
Contents
Description
Simple, efficient parser combinators for Text
strings, loosely
based on the Parsec library.
- type Parser = Parser Text
- type Result = IResult Text
- parse :: Parser a -> Text -> Result a
- parseOnly :: Parser a -> Text -> Either String a
- (<?>) :: Parser a -> String -> Parser a
- try :: Parser a -> Parser a
- module Data.Attoparsec.Combinator
- satisfy :: (Char -> Bool) -> Parser Char
- satisfyWith :: (Char -> a) -> (a -> Bool) -> Parser a
- anyChar :: Parser Char
- skip :: (Char -> Bool) -> Parser ()
- char :: Char -> Parser Char
- notChar :: Char -> Parser Char
- inClass :: String -> Char -> Bool
- notInClass :: String -> Char -> Bool
- skipWhile :: (Char -> Bool) -> Parser ()
- string :: Text -> Parser Text
- stringTransform :: (Text -> Text) -> Text -> Parser Text
- take :: Int -> Parser Text
- scan :: s -> (s -> Char -> Maybe s) -> Parser Text
- takeWhile :: (Char -> Bool) -> Parser Text
- takeWhile1 :: (Char -> Bool) -> Parser Text
- takeTill :: (Char -> Bool) -> Parser Text
- takeText :: Parser Text
- takeLazyText :: Parser Text
- endOfInput :: Parser ()
- atEnd :: Parser Bool
- ensure :: Int -> Parser Text
- endOfLine :: Parser ()
Parser types
Running parsers
parseOnly :: Parser a -> Text -> 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 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.
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'
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.
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.
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
takeLazyText :: Parser TextSource
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 TextSource
If at least n
characters of input are available, return the
current input, otherwise fail.