Portability | non-portable |
---|---|

Stability | experimental |

Maintainer | ekmett@gmail.com |

Commonly used generic combinators

- choice :: Alternative m => [m a] -> m a
- option :: Alternative m => a -> m a -> m a
- optional :: Alternative f => f a -> f (Maybe a)
- skipOptional :: Alternative m => m a -> m ()
- between :: Applicative m => m bra -> m ket -> m a -> m a
- skipSome :: MonadParser m => m a -> m ()
- some :: Alternative f => forall a. f a -> f [a]
- many :: Alternative f => forall a. f a -> f [a]
- sepBy :: Alternative m => m a -> m sep -> m [a]
- sepBy1 :: Alternative m => m a -> m sep -> m [a]
- sepEndBy1 :: Alternative m => m a -> m sep -> m [a]
- sepEndBy :: Alternative m => m a -> m sep -> m [a]
- endBy1 :: Alternative m => m a -> m sep -> m [a]
- endBy :: Alternative m => m a -> m sep -> m [a]
- count :: Applicative m => Int -> m a -> m [a]
- chainl :: Alternative m => m a -> m (a -> a -> a) -> a -> m a
- chainr :: Alternative m => m a -> m (a -> a -> a) -> a -> m a
- chainl1 :: Alternative m => m a -> m (a -> a -> a) -> m a
- chainr1 :: Alternative m => m a -> m (a -> a -> a) -> m a
- eof :: MonadParser m => m ()
- manyTill :: (Alternative m, MonadPlus m) => m a -> m end -> m [a]
- notFollowedBy :: (MonadParser m, Show a) => m a -> m ()
- lookAhead :: MonadParser m => m a -> m a

# Documentation

choice :: Alternative m => [m a] -> m aSource

`choice ps`

tries to apply the parsers in the list `ps`

in order,
until one of them succeeds. Returns the value of the succeeding
parser.

option :: Alternative m => a -> m a -> m aSource

`option x p`

tries to apply parser `p`

. If `p`

fails without
consuming input, it returns the value `x`

, otherwise the value
returned by `p`

.

priority = option 0 (do{ d <- digit ; return (digitToInt d) })

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

One or none.

skipOptional :: Alternative m => m a -> m ()Source

`skipOptional p`

tries to apply parser `p`

. It will parse `p`

or nothing.
It only fails if `p`

fails after consuming input. It discards the result
of `p`

. (Plays the role of parsec's optional, which conflicts with Applicative's optional)

between :: Applicative m => m bra -> m ket -> m a -> m aSource

`between open close p`

parses `open`

, followed by `p`

and `close`

.
Returns the value returned by `p`

.

braces = between (symbol "{") (symbol "}")

skipSome :: MonadParser m => m a -> m ()Source

`skipSome p`

applies the parser `p`

*one* or more times, skipping
its result. (aka skipMany1 in parsec)

some :: Alternative f => forall a. f a -> f [a]

One or more.

many :: Alternative f => forall a. f a -> f [a]

Zero or more.

sepBy :: Alternative m => m a -> m sep -> m [a]Source

`sepBy p sep`

parses *zero* or more occurrences of `p`

, separated
by `sep`

. Returns a list of values returned by `p`

.

commaSep p = p `sepBy` (symbol ",")

sepBy1 :: Alternative m => m a -> m sep -> m [a]Source

`sepBy1 p sep`

parses *one* or more occurrences of `p`

, separated
by `sep`

. Returns a list of values returned by `p`

.

sepEndBy1 :: Alternative m => m a -> m sep -> m [a]Source

`sepEndBy1 p sep`

parses *one* or more occurrences of `p`

,
separated and optionally ended by `sep`

. Returns a list of values
returned by `p`

.

sepEndBy :: Alternative m => m a -> m sep -> m [a]Source

`sepEndBy p sep`

parses *zero* or more occurrences of `p`

,
separated and optionally ended by `sep`

, ie. haskell style
statements. Returns a list of values returned by `p`

.

haskellStatements = haskellStatement `sepEndBy` semi

endBy1 :: Alternative m => m a -> m sep -> m [a]Source

`endBy1 p sep`

parses *one* or more occurrences of `p`

, seperated
and ended by `sep`

. Returns a list of values returned by `p`

.

endBy :: Alternative m => m a -> m sep -> m [a]Source

`endBy p sep`

parses *zero* or more occurrences of `p`

, seperated
and ended by `sep`

. Returns a list of values returned by `p`

.

cStatements = cStatement `endBy` semi

count :: Applicative m => Int -> m a -> m [a]Source

`count n p`

parses `n`

occurrences of `p`

. If `n`

is smaller or
equal to zero, the parser equals to `return []`

. Returns a list of
`n`

values returned by `p`

.

chainl :: Alternative m => m a -> m (a -> a -> a) -> a -> m aSource

`chainl p op x`

parser *zero* or more occurrences of `p`

,
separated by `op`

. Returns a value obtained by a *left* associative
application of all functions returned by `op`

to the values returned
by `p`

. If there are zero occurrences of `p`

, the value `x`

is
returned.

chainr :: Alternative m => m a -> m (a -> a -> a) -> a -> m aSource

`chainr p op x`

parser *zero* or more occurrences of `p`

,
separated by `op`

Returns a value obtained by a *right* associative
application of all functions returned by `op`

to the values returned
by `p`

. If there are no occurrences of `p`

, the value `x`

is
returned.

chainl1 :: Alternative m => m a -> m (a -> a -> a) -> m aSource

`chainl1 p op x`

parser *one* or more occurrences of `p`

,
separated by `op`

Returns a value obtained by a *left* associative
application of all functions returned by `op`

to the values returned
by `p`

. . This parser can for example be used to eliminate left
recursion which typically occurs in expression grammars.

expr = term `chainl1` addop term = factor `chainl1` mulop factor = parens expr <|> integer mulop = do{ symbol "*"; return (*) } <|> do{ symbol "/"; return (div) } addop = do{ symbol "+"; return (+) } <|> do{ symbol "-"; return (-) }

chainr1 :: Alternative m => m a -> m (a -> a -> a) -> m aSource

`chainr1 p op x`

parser *one* or more occurrences of |p|,
separated by `op`

Returns a value obtained by a *right* associative
application of all functions returned by `op`

to the values returned
by `p`

.

eof :: MonadParser m => m ()Source

This parser only succeeds at the end of the input. This is not a
primitive parser but it is defined using `notFollowedBy`

.

eof = notFollowedBy anyChar <?> "end of input"

manyTill :: (Alternative m, MonadPlus m) => m a -> m end -> m [a]Source

`manyTill p end`

applies parser `p`

*zero* or more times until
parser `end`

succeeds. Returns the list of values returned by `p`

.
This parser can be used to scan comments:

simpleComment = do{ string "<!--" ; manyTill anyChar (try (string "-->")) }

Note the overlapping parsers `anyChar`

and `string "-->"`

, and
therefore the use of the `try`

combinator.

notFollowedBy :: (MonadParser m, Show a) => m a -> m ()Source

`notFollowedBy p`

only succeeds when parser `p`

fails. This parser
does not consume any input. This parser can be used to implement the
'longest match' rule. For example, when recognizing keywords (for
example `let`

), we want to make sure that a keyword is not followed
by a legal identifier character, in which case the keyword is
actually an identifier (for example `lets`

). We can program this
behaviour as follows:

keywordLet = try (do{ string "let" ; notFollowedBy alphaNum })

lookAhead :: MonadParser m => m a -> m aSource

`lookAhead p`

parses `p`

without consuming any input.