Copyright | (c) 1999-2004 Wolfgang Lux 2016 Jan Tikovsky |
---|---|

License | BSD-3-clause |

Maintainer | bjp@informatik.uni-kiel.de |

Stability | experimental |

Portability | portable |

Safe Haskell | Safe |

Language | Haskell2010 |

The parsing combinators implemented in this module are based on the LL(1) parsing combinators developed by Swierstra and Duponcheel. They have been adapted to using continuation passing style in order to work with the lexing combinators described in the previous section. In addition, the facilities for error correction are omitted in this implementation.

The two functions `applyParser`

and `prefixParser`

use the specified
parser for parsing a string. When `applyParser`

is used, an error is
reported if the parser does not consume the whole string,
whereas `prefixParser`

discards the rest of the input string in this case.

## Synopsis

- data Parser a s b
- fullParser :: Symbol s => Parser a s a -> Lexer s a -> FilePath -> String -> CYM a
- prefixParser :: Symbol s => Parser a s a -> Lexer s a -> FilePath -> String -> CYM a
- position :: Parser a s Position
- spanPosition :: Symbol s => Parser a s Span
- succeed :: b -> Parser a s b
- failure :: String -> Parser a s b
- symbol :: s -> Parser a s s
- (<?>) :: Symbol s => Parser a s b -> String -> Parser a s b
- (<|>) :: Symbol s => Parser a s b -> Parser a s b -> Parser a s b
- (<|?>) :: Symbol s => Parser a s b -> Parser a s b -> Parser a s b
- (<*>) :: Applicative f => f (a -> b) -> f a -> f b
- (<\>) :: Symbol s => Parser a s b -> Parser a s c -> Parser a s b
- (<\\>) :: Symbol s => Parser a s b -> [s] -> Parser a s b
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<$->) :: Symbol s => a -> Parser b s c -> Parser b s a
- (<*->) :: Symbol s => Parser a s b -> Parser a s c -> Parser a s b
- (<-*>) :: Symbol s => Parser a s b -> Parser a s c -> Parser a s c
- (<**>) :: Symbol s => Parser a s b -> Parser a s (b -> c) -> Parser a s c
- (<??>) :: Symbol s => Parser a s b -> Parser a s (b -> b) -> Parser a s b
- (<.>) :: Symbol s => Parser a s (b -> c) -> Parser a s (c -> d) -> Parser a s (b -> d)
- opt :: Symbol s => Parser a s b -> b -> Parser a s b
- choice :: Symbol s => [Parser a s b] -> Parser a s b
- flag :: Symbol s => Parser a s b -> Parser a s Bool
- optional :: Symbol s => Parser a s b -> Parser a s ()
- option :: Symbol s => Parser a s b -> Parser a s (Maybe b)
- many :: Symbol s => Parser a s b -> Parser a s [b]
- many1 :: Symbol s => Parser a s b -> Parser a s [b]
- sepBy :: Symbol s => Parser a s b -> Parser a s c -> Parser a s [b]
- sepBy1 :: Symbol s => Parser a s b -> Parser a s c -> Parser a s [b]
- sepBySp :: Symbol s => Parser a s b -> Parser a s c -> Parser a s ([b], [Span])
- sepBy1Sp :: Symbol s => Parser a s b -> Parser a s c -> Parser a s ([b], [Span])
- chainr :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> b -> Parser a s b
- chainr1 :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> Parser a s b
- chainl :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> b -> Parser a s b
- chainl1 :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> Parser a s b
- between :: Symbol s => Parser a s b -> Parser a s c -> Parser a s b -> Parser a s c
- ops :: Symbol s => [(s, b)] -> Parser a s b
- layoutOn :: Symbol s => Parser a s b
- layoutOff :: Symbol s => Parser a s b
- layoutEnd :: Symbol s => Parser a s b

# Data types

CPS-Parser type

# Parser application

# Basic parsers

# parser combinators

(<?>) :: Symbol s => Parser a s b -> String -> Parser a s b infixl 2 Source #

Behave like the given parser, but use the given `String`

as the error
message if the parser fails

(<|>) :: Symbol s => Parser a s b -> Parser a s b -> Parser a s b infixl 3 Source #

Deterministic choice between two parsers.
The appropriate parser is chosen based on the next `Symbol`

(<|?>) :: Symbol s => Parser a s b -> Parser a s b -> Parser a s b infixl 3 Source #

Non-deterministic choice between two parsers.

The other parsing combinators require that the grammar being parsed is LL(1). In some cases it may be difficult or even impossible to transform a grammar into LL(1) form. As a remedy, we include a non-deterministic version of the choice combinator in addition to the deterministic combinator adapted from the paper. For every symbol from the intersection of the parser's first sets, the combinator '(|?)' applies both parsing functions to the input stream and uses that one which processes the longer prefix of the input stream irrespective of whether it succeeds or fails. If both functions recognize the same prefix, we choose the one that succeeds and report an ambiguous parse error if both succeed.

(<*>) :: Applicative f => f (a -> b) -> f a -> f b infixl 4 #

Sequential application.

A few functors support an implementation of `<*>`

that is more
efficient than the default one.

(<\>) :: Symbol s => Parser a s b -> Parser a s c -> Parser a s b infixl 5 Source #

Restrict the first parser by the first `Symbol`

s of the second

(<\\>) :: Symbol s => Parser a s b -> [s] -> Parser a s b infixl 5 Source #

Restrict a parser by a list of first `Symbol`

s

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #

An infix synonym for `fmap`

.

The name of this operator is an allusion to `$`

.
Note the similarities between their types:

($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b

Whereas `$`

is function application, `<$>`

is function
application lifted over a `Functor`

.

#### Examples

Convert from a

to a `Maybe`

`Int`

using `Maybe`

`String`

`show`

:

`>>>`

Nothing`show <$> Nothing`

`>>>`

Just "3"`show <$> Just 3`

Convert from an

to an `Either`

`Int`

`Int`

`Either`

`Int`

`String`

using `show`

:

`>>>`

Left 17`show <$> Left 17`

`>>>`

Right "17"`show <$> Right 17`

Double each element of a list:

`>>>`

[2,4,6]`(*2) <$> [1,2,3]`

Apply `even`

to the second element of a pair:

`>>>`

(2,True)`even <$> (2,2)`

(<$->) :: Symbol s => a -> Parser b s c -> Parser b s a infixl 4 Source #

Replace the result of the parser with the first argument

(<*->) :: Symbol s => Parser a s b -> Parser a s c -> Parser a s b infixl 4 Source #

Apply two parsers in sequence, but return only the result of the first parser

(<-*>) :: Symbol s => Parser a s b -> Parser a s c -> Parser a s c infixl 4 Source #

Apply two parsers in sequence, but return only the result of the second parser

(<**>) :: Symbol s => Parser a s b -> Parser a s (b -> c) -> Parser a s c infixl 4 Source #

Apply the parsers in sequence and apply the result function of the second parse to the result of the first

(<??>) :: Symbol s => Parser a s b -> Parser a s (b -> b) -> Parser a s b infixl 4 Source #

Same as (**), but only applies the function if the second parser succeeded.

(<.>) :: Symbol s => Parser a s (b -> c) -> Parser a s (c -> d) -> Parser a s (b -> d) infixl 4 Source #

Flipped function composition on parsers

opt :: Symbol s => Parser a s b -> b -> Parser a s b infixl 2 Source #

Try the first parser, but return the second argument if it didn't succeed

choice :: Symbol s => [Parser a s b] -> Parser a s b Source #

Choose the first succeeding parser from a non-empty list of parsers

flag :: Symbol s => Parser a s b -> Parser a s Bool Source #

Try to apply a given parser and return a boolean value if the parser succeeded.

optional :: Symbol s => Parser a s b -> Parser a s () Source #

Try to apply a parser but forget if it succeeded

option :: Symbol s => Parser a s b -> Parser a s (Maybe b) Source #

Try to apply a parser and return its result in a `Maybe`

type

many :: Symbol s => Parser a s b -> Parser a s [b] Source #

Repeatedly apply a parser for 0 or more occurences

many1 :: Symbol s => Parser a s b -> Parser a s [b] Source #

Repeatedly apply a parser for 1 or more occurences

sepBy :: Symbol s => Parser a s b -> Parser a s c -> Parser a s [b] Source #

Parse a list with is separated by a seperator

sepBy1 :: Symbol s => Parser a s b -> Parser a s c -> Parser a s [b] Source #

Parse a non-empty list with is separated by a seperator

sepBySp :: Symbol s => Parser a s b -> Parser a s c -> Parser a s ([b], [Span]) Source #

Parse a list with is separated by a seperator

chainr :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> b -> Parser a s b Source #

`chainr p op x`

parses zero or more occurrences of `p`

, separated by `op`

.
Returns a value produced by a *right* associative application of all
functions returned by op. If there are no occurrences of `p`

, `x`

is
returned.

chainr1 :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> Parser a s b Source #

Like `chainr`

, but parses one or more occurrences of p.

chainl :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> b -> Parser a s b Source #

`chainr p op x`

parses zero or more occurrences of `p`

, separated by `op`

.
Returns a value produced by a *left* associative application of all
functions returned by op. If there are no occurrences of `p`

, `x`

is
returned.

chainl1 :: Symbol s => Parser a s b -> Parser a s (b -> b -> b) -> Parser a s b Source #

Like `chainl`

, but parses one or more occurrences of p.

between :: Symbol s => Parser a s b -> Parser a s c -> Parser a s b -> Parser a s c Source #

Parse an expression between an opening and a closing part.