darcs-2.18.5: a distributed, interactive, smart revision control system

Safe Haskell | None
Language | Haskell2010

Darcs.Util.Parser

Synopsis

- type Parser = Parser ByteString
- anyChar :: Parser Char
- char :: Char -> Parser ()
- checkConsumes :: Parser a -> Parser a
- choice :: Alternative f => [f a] -> f a
- endOfInput :: Chunk t => Parser t ()
- int :: Parser Int
- lexChar :: Char -> Parser ()
- lexString :: ByteString -> Parser ()
- linesStartingWith :: Char -> Parser [ByteString]
- linesStartingWithEndingWith :: Char -> Char -> Parser [ByteString]
- lexWord :: Parser ByteString
- lookAhead :: Parser i a -> Parser i a
- many :: Alternative f => f a -> f [a]
- option :: Alternative f => a -> f a -> f a
- optional :: Alternative f => f a -> f (Maybe a)
- parse :: Parser a -> ByteString -> Either String (a, ByteString)
- parseAll :: Parser a -> ByteString -> Either String a
- skipSpace :: Parser ()
- skipWhile :: (Char -> Bool) -> Parser ()
- string :: ByteString -> Parser ()
- take :: Int -> Parser ByteString
- takeTill :: (Char -> Bool) -> Parser ByteString
- takeTillChar :: Char -> Parser ByteString
- unsigned :: Integral a => Parser a
- withPath :: FilePath -> Either String a -> Either String a
- (<|>) :: Alternative f => f a -> f a -> f a

# Documentation

type Parser = Parser ByteString #

anyChar :: Parser Char #

Match any character.

char :: Char -> Parser () #

checkConsumes :: Parser a -> Parser a #

choice :: Alternative f => [f a] -> f a #

`choice ps` tries to apply the actions in the list `ps` in order, until one of them succeeds. Returns the value of the succeeding action.

endOfInput :: Chunk t => Parser t () #

Match only if all input has been consumed.

int :: Parser Int #

lexChar :: Char -> Parser () #

lexString :: ByteString -> Parser () #

linesStartingWith :: Char -> Parser [ByteString] #

linesStartingWithEndingWith :: Char -> Char -> Parser [ByteString] #

lexWord :: Parser ByteString #

lookAhead :: Parser i a -> Parser i a #

Apply a parser without consuming any input.

many :: Alternative f => f a -> f [a] #

Zero or more.

#### Examples

Expand

    >>> many (putStr "la")
    lalalalalalalalala... * goes on forever *

    >>> many Nothing
    Just []

    >>> take 5 <$> many (Just 1)
    * hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case `many parser` will attempt to parse `parser` zero or more times until it fails.

option :: Alternative f => a -> f a -> f a #

`option x p` tries to apply action `p`. If `p` fails without consuming input, it returns the value `x`, otherwise the value returned by `p`.

    priority  = option 0 (digitToInt <$> digit)

optional :: Alternative f => f a -> f (Maybe a) #

One or none.

It is useful for modelling any computation that is allowed to fail.

#### Examples

Expand

Using the `Alternative` instance of Control.Monad.Except, the following functions:

    >>> import Control.Monad.Except

    >>> canFail = throwError "it failed" :: Except String Int
    >>> final = return 42                :: Except String Int

Can be combined by allowing the first function to fail:

    >>> runExcept $ canFail *> final
    Left "it failed"

    >>> runExcept $ optional canFail *> final
    Right 42

parse :: Parser a -> ByteString -> Either String (a, ByteString) #

parseAll :: Parser a -> ByteString -> Either String a #

skipSpace :: Parser () #

Skip over white space.

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

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

string :: ByteString -> Parser () #

take :: Int -> Parser ByteString #

Consume exactly `n` bytes of input.

takeTill :: (Char -> 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.

takeTillChar :: Char -> Parser ByteString #

unsigned :: Integral a => Parser a #

withPath :: FilePath -> Either String a -> Either String a #

(<|>) :: Alternative f => f a -> f a -> f a infixl 3 #

An associative binary operation