Copyright | (c) Daan Leijen 1999-2001 (c) Paolo Martini 2007 |
---|---|

License | BSD-style (see the LICENSE file) |

Maintainer | derek.a.elkins@gmail.com |

Stability | provisional |

Portability | portable |

Safe Haskell | Trustworthy |

Language | Haskell2010 |

Commonly used generic combinators.

See also the parser-combinators package for additional (and generalised) combinators.

## Synopsis

- choice :: forall s (m :: Type -> Type) t u a. Stream s m t => [ParsecT s u m a] -> ParsecT s u m a
- count :: forall s (m :: Type -> Type) t u a. Stream s m t => Int -> ParsecT s u m a -> ParsecT s u m [a]
- between :: forall s (m :: Type -> Type) t u open close a. Stream s m t => ParsecT s u m open -> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
- option :: forall s (m :: Type -> Type) t a u. Stream s m t => a -> ParsecT s u m a -> ParsecT s u m a
- optionMaybe :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (Maybe a)
- optional :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m ()
- skipMany1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m ()
- many1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m [a]
- sepBy :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- sepBy1 :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- endBy :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- endBy1 :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- sepEndBy :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- sepEndBy1 :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
- chainl :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a
- chainl1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a
- chainr :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a
- chainr1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a
- eof :: forall s (m :: Type -> Type) t u. (Stream s m t, Show t) => ParsecT s u m ()
- notFollowedBy :: forall s (m :: Type -> Type) t a u. (Stream s m t, Show a) => ParsecT s u m a -> ParsecT s u m ()
- manyTill :: forall s (m :: Type -> Type) t u a end. Stream s m t => ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
- lookAhead :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m a
- anyToken :: forall s (m :: Type -> Type) t u. (Stream s m t, Show t) => ParsecT s u m t
- parserTrace :: forall t s (m :: Type -> Type) u. (Show t, Stream s m t) => String -> ParsecT s u m ()
- parserTraced :: forall s (m :: Type -> Type) t u b. (Stream s m t, Show t) => String -> ParsecT s u m b -> ParsecT s u m b

# Documentation

choice :: forall s (m :: Type -> Type) t u a. Stream s m t => [ParsecT s u m a] -> ParsecT s u m a Source #

`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.

count :: forall s (m :: Type -> Type) t u a. Stream s m t => Int -> ParsecT s u m a -> ParsecT s u 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`

.

between :: forall s (m :: Type -> Type) t u open close a. Stream s m t => ParsecT s u m open -> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a Source #

`between open close p`

parses `open`

, followed by `p`

and `close`

.
Returns the value returned by `p`

.

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

option :: forall s (m :: Type -> Type) t a u. Stream s m t => a -> ParsecT s u m a -> ParsecT s u m a Source #

`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) })

optionMaybe :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (Maybe a) Source #

optional :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m () Source #

`optional 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`

.

skipMany1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m () Source #

`skipMany1 p`

applies the parser `p`

*one* or more times, skipping
its result.

many1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m [a] Source #

`many1 p`

applies the parser `p`

*one* or more times. Returns a
list of the returned values of `p`

.

word = many1 letter

sepBy :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u 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 :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] Source #

`sepBy1 p sep`

parses *one* or more occurrences of `p`

, separated
by `sep`

. Returns a list of values returned by `p`

.

endBy :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] Source #

`endBy p sep`

parses *zero* or more occurrences of `p`

, separated
and ended by `sep`

. Returns a list of values returned by `p`

.

cStatements = cStatement `endBy` semi

endBy1 :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a] Source #

`endBy1 p sep`

parses *one* or more occurrences of `p`

, separated
and ended by `sep`

. Returns a list of values returned by `p`

.

sepEndBy :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u 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

sepEndBy1 :: forall s (m :: Type -> Type) t u a sep. Stream s m t => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u 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`

.

chainl :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a Source #

`chainl p op x`

parses *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.

chainl1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a Source #

`chainl1 p op`

parses *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 (-) }

chainr :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a Source #

`chainr p op x`

parses *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.

chainr1 :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a Source #

`chainr1 p op x`

parses *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 :: forall s (m :: Type -> Type) t u. (Stream s m t, Show t) => ParsecT s u 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 anyToken <?> "end of input"

notFollowedBy :: forall s (m :: Type -> Type) t a u. (Stream s m t, Show a) => ParsecT s u m a -> ParsecT s u 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 })

**NOTE**: Currently, `notFollowedBy`

exhibits surprising behaviour
when applied to a parser `p`

that doesn't consume any input;
specifically

is`notFollowedBy`

.`notFollowedBy`

*not*equivalent to`lookAhead`

, and`notFollowedBy`

`eof`

*never*fails.

See haskell/parsec#8 for more details.

manyTill :: forall s (m :: Type -> Type) t u a end. Stream s m t => ParsecT s u m a -> ParsecT s u m end -> ParsecT s u 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.

lookAhead :: forall s (m :: Type -> Type) t u a. Stream s m t => ParsecT s u m a -> ParsecT s u m a Source #

`lookAhead p`

parses `p`

without consuming any input.

If `p`

fails and consumes some input, so does `lookAhead`

. Combine with `try`

if this is undesirable.

anyToken :: forall s (m :: Type -> Type) t u. (Stream s m t, Show t) => ParsecT s u m t Source #

The parser `anyToken`

accepts any kind of token. It is for example
used to implement `eof`

. Returns the accepted token.

# Debugging

As a more comprehensive alternative for debugging Parsec parsers, there's also the parsec-free package.

parserTrace :: forall t s (m :: Type -> Type) u. (Show t, Stream s m t) => String -> ParsecT s u m () Source #

`parserTrace label`

is an impure function, implemented with Debug.Trace that
prints to the console the remaining parser state at the time it is invoked.
It is intended to be used for debugging parsers by inspecting their intermediate states.

*> parseTest (oneOf "aeiou" >> parserTrace "label") "atest" label: "test" ...

*Since: parsec-3.1.12.0*

parserTraced :: forall s (m :: Type -> Type) t u b. (Stream s m t, Show t) => String -> ParsecT s u m b -> ParsecT s u m b Source #

`parserTraced label p`

is an impure function, implemented with Debug.Trace that
prints to the console the remaining parser state at the time it is invoked.
It then continues to apply parser `p`

, and if `p`

fails will indicate that
the label has been backtracked.
It is intended to be used for debugging parsers by inspecting their intermediate states.

*> parseTest (oneOf "aeiou" >> parserTraced "label" (oneOf "nope")) "atest" label: "test" label backtracked parse error at (line 1, column 2): ...

*Since: parsec-3.1.12.0*