Safe Haskell | None |
---|

- data ReadP t a
- get :: ReadP t t
- look :: ReadP t [t]
- (+++) :: ReadP t a -> ReadP t a -> ReadP t a
- (<++) :: ReadP t a -> ReadP t a -> ReadP t a
- gather :: ReadP t a -> ReadP t ([t], a)
- parse :: ReadP t a -> [t] -> [a]
- parse' :: ReadP t a -> [t] -> Either a [t]
- pfail :: ReadP t a
- satisfy :: (t -> Bool) -> ReadP t t
- char :: Eq t => t -> ReadP t t
- string :: Eq t => [t] -> ReadP t [t]
- munch :: (t -> Bool) -> ReadP t [t]
- munch1 :: (t -> Bool) -> ReadP t [t]
- skipSpaces :: ReadP Char ()
- choice :: [ReadP t a] -> ReadP t a
- count :: Int -> ReadP t a -> ReadP t [a]
- between :: ReadP t open -> ReadP t close -> ReadP t a -> ReadP t a
- option :: a -> ReadP t a -> ReadP t a
- optional :: ReadP t a -> ReadP t ()
- many :: ReadP t a -> ReadP t [a]
- many1 :: ReadP t a -> ReadP t [a]
- skipMany :: ReadP t a -> ReadP t ()
- skipMany1 :: ReadP t a -> ReadP t ()
- sepBy :: ReadP t a -> ReadP t sep -> ReadP t [a]
- sepBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a]
- endBy :: ReadP t a -> ReadP t sep -> ReadP t [a]
- endBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a]
- chainr :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t a
- chainl :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t a
- chainl1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t a
- chainr1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t a
- manyTill :: ReadP t a -> ReadP t end -> ReadP t [a]

# The `ReadP`

type

# Primitive operations

Look-ahead: returns the part of the input that is left, without consuming it.

(<++) :: ReadP t a -> ReadP t a -> ReadP t aSource

Local, exclusive, left-biased choice: If left parser locally produces any result at all, then right parser is not used.

gather :: ReadP t a -> ReadP t ([t], a)Source

Transforms a parser into one that does the same, but
in addition returns the exact characters read.
IMPORTANT NOTE: `gather`

gives a runtime error if its first argument
is built using any occurrences of readS_to_P.

# Parse

parse :: ReadP t a -> [t] -> [a]Source

Run a parser on a list of tokens. Returns the list of complete matches.

# Other operations

satisfy :: (t -> Bool) -> ReadP t tSource

Consumes and returns the next character, if it satisfies the specified predicate.

munch :: (t -> Bool) -> ReadP t [t]Source

Parses the first zero or more characters satisfying the predicate.

munch1 :: (t -> Bool) -> ReadP t [t]Source

Parses the first one or more characters satisfying the predicate.

skipSpaces :: ReadP Char ()Source

Skips all whitespace.

count :: Int -> ReadP t a -> ReadP t [a]Source

`count n p`

parses `n`

occurrences of `p`

in sequence. A list of
results is returned.

between :: ReadP t open -> ReadP t close -> ReadP t a -> ReadP t aSource

`between open close p`

parses `open`

, followed by `p`

and finally
`close`

. Only the value of `p`

is returned.

option :: a -> ReadP t a -> ReadP t aSource

`option x p`

will either parse `p`

or return `x`

without consuming
any input.

sepBy :: ReadP t a -> ReadP t sep -> ReadP t [a]Source

`sepBy p sep`

parses zero or more occurrences of `p`

, separated by `sep`

.
Returns a list of values returned by `p`

.

sepBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a]Source

`sepBy1 p sep`

parses one or more occurrences of `p`

, separated by `sep`

.
Returns a list of values returned by `p`

.

endBy :: ReadP t a -> ReadP t sep -> ReadP t [a]Source

`endBy p sep`

parses zero or more occurrences of `p`

, separated and ended
by `sep`

.

endBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a]Source

`endBy p sep`

parses one or more occurrences of `p`

, separated and ended
by `sep`

.

chainr :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t aSource

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

chainl :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t aSource

`chainl 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 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t aSource

Like `chainl`

, but parses one or more occurrences of `p`

.

chainr1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t aSource

Like `chainr`

, but parses one or more occurrences of `p`

.

manyTill :: ReadP t a -> ReadP t end -> ReadP t [a]Source

`manyTill p end`

parses zero or more occurrences of `p`

, until `end`

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

.

# Properties

The following are QuickCheck specifications of what the combinators do. These can be seen as formal specifications of the behavior of the combinators.

We use bags to give semantics to the combinators.

type Bag a = [a]

Equality on bags does not care about the order of elements.

(=~) :: Ord a => Bag a -> Bag a -> Bool xs =~ ys = sort xs == sort ys

A special equality operator to avoid unresolved overloading when testing the properties.

(=~.) :: Bag (Int,String) -> Bag (Int,String) -> Bool (=~.) = (=~)

Here follow the properties:

prop_Get_Nil = readP_to_S get [] =~ [] prop_Get_Cons c s = readP_to_S get (c:s) =~ [(c,s)] prop_Look s = readP_to_S look s =~ [(s,s)] prop_Fail s = readP_to_S pfail s =~. [] prop_Return x s = readP_to_S (return x) s =~. [(x,s)] prop_Bind p k s = readP_to_S (p >>= k) s =~. [ ys'' | (x,s') <- readP_to_S p s , ys'' <- readP_to_S (k (x::Int)) s' ] prop_Plus p q s = readP_to_S (p +++ q) s =~. (readP_to_S p s ++ readP_to_S q s) prop_LeftPlus p q s = readP_to_S (p <++ q) s =~. (readP_to_S p s +<+ readP_to_S q s) where [] +<+ ys = ys xs +<+ _ = xs prop_Gather s = forAll readPWithoutReadS $ \p -> readP_to_S (gather p) s =~ [ ((pre,x::Int),s') | (x,s') <- readP_to_S p s , let pre = take (length s - length s') s ] prop_String_Yes this s = readP_to_S (string this) (this ++ s) =~ [(this,s)] prop_String_Maybe this s = readP_to_S (string this) s =~ [(this, drop (length this) s) | this `isPrefixOf` s] prop_Munch p s = readP_to_S (munch p) s =~ [(takeWhile p s, dropWhile p s)] prop_Munch1 p s = readP_to_S (munch1 p) s =~ [(res,s') | let (res,s') = (takeWhile p s, dropWhile p s), not (null res)] prop_Choice ps s = readP_to_S (choice ps) s =~. readP_to_S (foldr (+++) pfail ps) s prop_ReadS r s = readP_to_S (readS_to_P r) s =~. r s