Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | None
Language | Haskell2010

Agda.Utils.Parser.ReadP

Contents

- The `ReadP` type
- Primitive operations
- Parse
- Other operations
- Properties

Description

Synopsis

- 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

data ReadP t a #

Instances

Monad (ReadP t) # |
Methods (>>=) :: ReadP t a -> (a -> ReadP t b) -> ReadP t b # (>>) :: ReadP t a -> ReadP t b -> ReadP t b # return :: a -> ReadP t a # fail :: String -> ReadP t a #
Functor (ReadP t) # |
Methods fmap :: (a -> b) -> ReadP t a -> ReadP t b # (<$) :: a -> ReadP t b -> ReadP t a #
Applicative (ReadP t) # |
Methods pure :: a -> ReadP t a # (<*>) :: ReadP t (a -> b) -> ReadP t a -> ReadP t b # (*>) :: ReadP t a -> ReadP t b -> ReadP t b # (<*) :: ReadP t a -> ReadP t b -> ReadP t a #
Alternative (ReadP t) # |
Methods empty :: ReadP t a # (<|>) :: ReadP t a -> ReadP t a -> ReadP t a # some :: ReadP t a -> ReadP t [a] # many :: ReadP t a -> ReadP t [a] #
MonadPlus (ReadP t) # |
Methods mzero :: ReadP t a # mplus :: ReadP t a -> ReadP t a -> ReadP t a #

# Primitive operations

get :: ReadP t t #

Consumes and returns the next character. Fails if there is no input left.

look :: ReadP t [t] #

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

(+++) :: ReadP t a -> ReadP t a -> ReadP t a infixr 5 #

Symmetric choice.

(<++) :: ReadP t a -> ReadP t a -> ReadP t a infixr 5 #

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) #

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] #

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

parse' :: ReadP t a -> [t] -> Either a [t] #

# Other operations

pfail :: ReadP t a #

Always fails.

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

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

char :: Eq t => t -> ReadP t t #

Parses and returns the specified character.

string :: Eq t => [t] -> ReadP t [t] #

Parses and returns the specified string.

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

Parses the first zero or more characters satisfying the predicate.

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

Parses the first one or more characters satisfying the predicate.

skipSpaces :: ReadP Char () #

Skips all whitespace.

choice :: [ReadP t a] -> ReadP t a #

Combines all parsers in the specified list.

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

`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 a #

`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 a #

`option x p` will either parse `p` or return `x` without consuming any input.

optional :: ReadP t a -> ReadP t () #

`optional p` optionally parses `p` and always returns `()`.

many :: ReadP t a -> ReadP t [a] #

Parses zero or more occurrences of the given parser.

many1 :: ReadP t a -> ReadP t [a] #

Parses one or more occurrences of the given parser.

skipMany :: ReadP t a -> ReadP t () #

Like `many`, but discards the result.

skipMany1 :: ReadP t a -> ReadP t () #

Like `many1`, but discards the result.

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

`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] #

`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] #

`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] #

`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 a #

`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 a #

`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 a #

Like `chainl`, but parses one or more occurrences of `p`.

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

Like `chainr`, but parses one or more occurrences of `p`.

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

`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