{-# LANGUAGE Trustworthy #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE DeriveFunctor #-} ----------------------------------------------------------------------------- -- | -- Module : Text.ParserCombinators.ReadP -- Copyright : (c) The University of Glasgow 2002 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : libraries@haskell.org -- Stability : provisional -- Portability : non-portable (local universal quantification) -- -- This is a library of parser combinators, originally written by Koen Claessen. -- It parses all alternatives in parallel, so it never keeps hold of -- the beginning of the input string, a common source of space leaks with -- other parsers. The @('+++')@ choice combinator is genuinely commutative; -- it makes no difference which branch is \"shorter\". ----------------------------------------------------------------------------- module Text.ParserCombinators.ReadP ( -- * The 'ReadP' type ReadP, -- * Primitive operations get, look, (+++), (<++), gather, -- * Other operations pfail, eof, satisfy, char, string, munch, munch1, skipSpaces, choice, count, between, option, optional, many, many1, skipMany, skipMany1, sepBy, sepBy1, endBy, endBy1, chainr, chainl, chainl1, chainr1, manyTill, -- * Running a parser ReadS, readP_to_S, readS_to_P, -- * Properties -- $properties ) where import GHC.Unicode ( isSpace ) import GHC.List ( replicate, null ) import GHC.Base hiding ( many ) import Control.Monad.Fail infixr 5 +++, <++ ------------------------------------------------------------------------ -- ReadS -- | A parser for a type @a@, represented as a function that takes a -- 'String' and returns a list of possible parses as @(a,'String')@ pairs. -- -- Note that this kind of backtracking parser is very inefficient; -- reading a large structure may be quite slow (cf 'ReadP'). type ReadS a = String -> [(a,String)] -- --------------------------------------------------------------------------- -- The P type -- is representation type -- should be kept abstract data P a = Get (Char -> P a) | Look (String -> P a) | Fail | Result a (P a) | Final [(a,String)] -- invariant: list is non-empty! deriving Functor -- ^ @since 4.8.0.0 -- Monad, MonadPlus -- | @since 4.5.0.0 instance Applicative P where pure :: a -> P a pure x :: a x = a -> P a -> P a forall a. a -> P a -> P a Result a x P a forall a. P a Fail <*> :: P (a -> b) -> P a -> P b (<*>) = P (a -> b) -> P a -> P b forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b ap -- | @since 2.01 instance MonadPlus P -- | @since 2.01 instance Monad P where (Get f :: Char -> P a f) >>= :: P a -> (a -> P b) -> P b >>= k :: a -> P b k = (Char -> P b) -> P b forall a. (Char -> P a) -> P a Get (\c :: Char c -> Char -> P a f Char c P a -> (a -> P b) -> P b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>= a -> P b k) (Look f :: String -> P a f) >>= k :: a -> P b k = (String -> P b) -> P b forall a. (String -> P a) -> P a Look (\s :: String s -> String -> P a f String s P a -> (a -> P b) -> P b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>= a -> P b k) Fail >>= _ = P b forall a. P a Fail (Result x :: a x p :: P a p) >>= k :: a -> P b k = a -> P b k a x P b -> P b -> P b forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> (P a p P a -> (a -> P b) -> P b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>= a -> P b k) (Final r :: [(a, String)] r) >>= k :: a -> P b k = [(b, String)] -> P b forall a. [(a, String)] -> P a final [(b, String) ys' | (x :: a x,s :: String s) <- [(a, String)] r, (b, String) ys' <- P b -> ReadS b forall a. P a -> ReadS a run (a -> P b k a x) String s] -- | @since 4.9.0.0 instance MonadFail P where fail :: String -> P a fail _ = P a forall a. P a Fail -- | @since 4.5.0.0 instance Alternative P where empty :: P a empty = P a forall a. P a Fail -- most common case: two gets are combined Get f1 :: Char -> P a f1 <|> :: P a -> P a -> P a <|> Get f2 :: Char -> P a f2 = (Char -> P a) -> P a forall a. (Char -> P a) -> P a Get (\c :: Char c -> Char -> P a f1 Char c P a -> P a -> P a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> Char -> P a f2 Char c) -- results are delivered as soon as possible Result x :: a x p :: P a p <|> q :: P a q = a -> P a -> P a forall a. a -> P a -> P a Result a x (P a p P a -> P a -> P a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> P a q) p :: P a p <|> Result x :: a x q :: P a q = a -> P a -> P a forall a. a -> P a -> P a Result a x (P a p P a -> P a -> P a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> P a q) -- fail disappears Fail <|> p :: P a p = P a p p :: P a p <|> Fail = P a p -- two finals are combined -- final + look becomes one look and one final (=optimization) -- final + sthg else becomes one look and one final Final r :: [(a, String)] r <|> Final t :: [(a, String)] t = [(a, String)] -> P a forall a. [(a, String)] -> P a Final ([(a, String)] r [(a, String)] -> [(a, String)] -> [(a, String)] forall a. [a] -> [a] -> [a] ++ [(a, String)] t) Final r :: [(a, String)] r <|> Look f :: String -> P a f = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> [(a, String)] -> P a forall a. [(a, String)] -> P a Final ([(a, String)] r [(a, String)] -> [(a, String)] -> [(a, String)] forall a. [a] -> [a] -> [a] ++ P a -> ReadS a forall a. P a -> ReadS a run (String -> P a f String s) String s)) Final r :: [(a, String)] r <|> p :: P a p = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> [(a, String)] -> P a forall a. [(a, String)] -> P a Final ([(a, String)] r [(a, String)] -> [(a, String)] -> [(a, String)] forall a. [a] -> [a] -> [a] ++ P a -> ReadS a forall a. P a -> ReadS a run P a p String s)) Look f :: String -> P a f <|> Final r :: [(a, String)] r = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> [(a, String)] -> P a forall a. [(a, String)] -> P a Final (P a -> ReadS a forall a. P a -> ReadS a run (String -> P a f String s) String s [(a, String)] -> [(a, String)] -> [(a, String)] forall a. [a] -> [a] -> [a] ++ [(a, String)] r)) p :: P a p <|> Final r :: [(a, String)] r = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> [(a, String)] -> P a forall a. [(a, String)] -> P a Final (P a -> ReadS a forall a. P a -> ReadS a run P a p String s [(a, String)] -> [(a, String)] -> [(a, String)] forall a. [a] -> [a] -> [a] ++ [(a, String)] r)) -- two looks are combined (=optimization) -- look + sthg else floats upwards Look f :: String -> P a f <|> Look g :: String -> P a g = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> String -> P a f String s P a -> P a -> P a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> String -> P a g String s) Look f :: String -> P a f <|> p :: P a p = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> String -> P a f String s P a -> P a -> P a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> P a p) p :: P a p <|> Look f :: String -> P a f = (String -> P a) -> P a forall a. (String -> P a) -> P a Look (\s :: String s -> P a p P a -> P a -> P a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> String -> P a f String s) -- --------------------------------------------------------------------------- -- The ReadP type newtype ReadP a = R (forall b . (a -> P b) -> P b) -- | @since 2.01 instance Functor ReadP where fmap :: (a -> b) -> ReadP a -> ReadP b fmap h :: a -> b h (R f :: forall b. (a -> P b) -> P b f) = (forall b. (b -> P b) -> P b) -> ReadP b forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\k :: b -> P b k -> (a -> P b) -> P b forall b. (a -> P b) -> P b f (b -> P b k (b -> P b) -> (a -> b) -> a -> P b forall b c a. (b -> c) -> (a -> b) -> a -> c . a -> b h)) -- | @since 4.6.0.0 instance Applicative ReadP where pure :: a -> ReadP a pure x :: a x = (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\k :: a -> P b k -> a -> P b k a x) <*> :: ReadP (a -> b) -> ReadP a -> ReadP b (<*>) = ReadP (a -> b) -> ReadP a -> ReadP b forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b ap -- liftA2 = liftM2 -- | @since 2.01 instance Monad ReadP where R m :: forall b. (a -> P b) -> P b m >>= :: ReadP a -> (a -> ReadP b) -> ReadP b >>= f :: a -> ReadP b f = (forall b. (b -> P b) -> P b) -> ReadP b forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\k :: b -> P b k -> (a -> P b) -> P b forall b. (a -> P b) -> P b m (\a :: a a -> let R m' :: forall b. (b -> P b) -> P b m' = a -> ReadP b f a a in (b -> P b) -> P b forall b. (b -> P b) -> P b m' b -> P b k)) -- | @since 4.9.0.0 instance MonadFail ReadP where fail :: String -> ReadP a fail _ = (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\_ -> P b forall a. P a Fail) -- | @since 4.6.0.0 instance Alternative ReadP where empty :: ReadP a empty = ReadP a forall a. ReadP a pfail <|> :: ReadP a -> ReadP a -> ReadP a (<|>) = ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a (+++) -- | @since 2.01 instance MonadPlus ReadP -- --------------------------------------------------------------------------- -- Operations over P final :: [(a,String)] -> P a -- Maintains invariant for Final constructor final :: [(a, String)] -> P a final [] = P a forall a. P a Fail final r :: [(a, String)] r = [(a, String)] -> P a forall a. [(a, String)] -> P a Final [(a, String)] r run :: P a -> ReadS a run :: P a -> ReadS a run (Get f :: Char -> P a f) (c :: Char c:s :: String s) = P a -> ReadS a forall a. P a -> ReadS a run (Char -> P a f Char c) String s run (Look f :: String -> P a f) s :: String s = P a -> ReadS a forall a. P a -> ReadS a run (String -> P a f String s) String s run (Result x :: a x p :: P a p) s :: String s = (a x,String s) (a, String) -> [(a, String)] -> [(a, String)] forall a. a -> [a] -> [a] : P a -> ReadS a forall a. P a -> ReadS a run P a p String s run (Final r :: [(a, String)] r) _ = [(a, String)] r run _ _ = [] -- --------------------------------------------------------------------------- -- Operations over ReadP get :: ReadP Char -- ^ Consumes and returns the next character. -- Fails if there is no input left. get :: ReadP Char get = (forall a. (Char -> P a) -> P a) -> ReadP Char forall a. (forall b. (a -> P b) -> P b) -> ReadP a R forall a. (Char -> P a) -> P a Get look :: ReadP String -- ^ Look-ahead: returns the part of the input that is left, without -- consuming it. look :: ReadP String look = (forall a. (String -> P a) -> P a) -> ReadP String forall a. (forall b. (a -> P b) -> P b) -> ReadP a R forall a. (String -> P a) -> P a Look pfail :: ReadP a -- ^ Always fails. pfail :: ReadP a pfail = (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\_ -> P b forall a. P a Fail) (+++) :: ReadP a -> ReadP a -> ReadP a -- ^ Symmetric choice. R f1 :: forall b. (a -> P b) -> P b f1 +++ :: ReadP a -> ReadP a -> ReadP a +++ R f2 :: forall b. (a -> P b) -> P b f2 = (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\k :: a -> P b k -> (a -> P b) -> P b forall b. (a -> P b) -> P b f1 a -> P b k P b -> P b -> P b forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> (a -> P b) -> P b forall b. (a -> P b) -> P b f2 a -> P b k) (<++) :: ReadP a -> ReadP a -> ReadP a -- ^ Local, exclusive, left-biased choice: If left parser -- locally produces any result at all, then right parser is -- not used. R f0 :: forall b. (a -> P b) -> P b f0 <++ :: ReadP a -> ReadP a -> ReadP a <++ q :: ReadP a q = do String s <- ReadP String look P a -> String -> Int# -> ReadP a probe ((a -> P a) -> P a forall b. (a -> P b) -> P b f0 a -> P a forall (m :: * -> *) a. Monad m => a -> m a return) String s 0# where probe :: P a -> String -> Int# -> ReadP a probe (Get f :: Char -> P a f) (c :: Char c:s :: String s) n :: Int# n = P a -> String -> Int# -> ReadP a probe (Char -> P a f Char c) String s (Int# nInt# -> Int# -> Int# +#1#) probe (Look f :: String -> P a f) s :: String s n :: Int# n = P a -> String -> Int# -> ReadP a probe (String -> P a f String s) String s Int# n probe p :: P a p@(Result _ _) _ n :: Int# n = Int# -> ReadP () discard Int# n ReadP () -> ReadP a -> ReadP a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (P a p P a -> (a -> P b) -> P b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>=) probe (Final r :: [(a, String)] r) _ _ = (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R ([(a, String)] -> P a forall a. [(a, String)] -> P a Final [(a, String)] r P a -> (a -> P b) -> P b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>=) probe _ _ _ = ReadP a q discard :: Int# -> ReadP () discard 0# = () -> ReadP () forall (m :: * -> *) a. Monad m => a -> m a return () discard n :: Int# n = ReadP Char get ReadP Char -> ReadP () -> ReadP () forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> Int# -> ReadP () discard (Int# nInt# -> Int# -> Int# -#1#) gather :: ReadP a -> ReadP (String, 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. gather :: ReadP a -> ReadP (String, a) gather (R m :: forall b. (a -> P b) -> P b m) = (forall b. ((String, a) -> P b) -> P b) -> ReadP (String, a) forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\k :: (String, a) -> P b k -> (String -> String) -> P (String -> P b) -> P b forall b. (String -> String) -> P (String -> P b) -> P b gath String -> String forall a. a -> a id ((a -> P (String -> P b)) -> P (String -> P b) forall b. (a -> P b) -> P b m (\a :: a a -> (String -> P b) -> P (String -> P b) forall (m :: * -> *) a. Monad m => a -> m a return (\s :: String s -> (String, a) -> P b k (String s,a a))))) where gath :: (String -> String) -> P (String -> P b) -> P b gath :: (String -> String) -> P (String -> P b) -> P b gath l :: String -> String l (Get f :: Char -> P (String -> P b) f) = (Char -> P b) -> P b forall a. (Char -> P a) -> P a Get (\c :: Char c -> (String -> String) -> P (String -> P b) -> P b forall b. (String -> String) -> P (String -> P b) -> P b gath (String -> String l(String -> String) -> (String -> String) -> String -> String forall b c a. (b -> c) -> (a -> b) -> a -> c .(Char cChar -> String -> String forall a. a -> [a] -> [a] :)) (Char -> P (String -> P b) f Char c)) gath _ Fail = P b forall a. P a Fail gath l :: String -> String l (Look f :: String -> P (String -> P b) f) = (String -> P b) -> P b forall a. (String -> P a) -> P a Look (\s :: String s -> (String -> String) -> P (String -> P b) -> P b forall b. (String -> String) -> P (String -> P b) -> P b gath String -> String l (String -> P (String -> P b) f String s)) gath l :: String -> String l (Result k :: String -> P b k p :: P (String -> P b) p) = String -> P b k (String -> String l []) P b -> P b -> P b forall (f :: * -> *) a. Alternative f => f a -> f a -> f a <|> (String -> String) -> P (String -> P b) -> P b forall b. (String -> String) -> P (String -> P b) -> P b gath String -> String l P (String -> P b) p gath _ (Final _) = String -> P b forall a. String -> a errorWithoutStackTrace "do not use readS_to_P in gather!" -- --------------------------------------------------------------------------- -- Derived operations satisfy :: (Char -> Bool) -> ReadP Char -- ^ Consumes and returns the next character, if it satisfies the -- specified predicate. satisfy :: (Char -> Bool) -> ReadP Char satisfy p :: Char -> Bool p = do Char c <- ReadP Char get; if Char -> Bool p Char c then Char -> ReadP Char forall (m :: * -> *) a. Monad m => a -> m a return Char c else ReadP Char forall a. ReadP a pfail char :: Char -> ReadP Char -- ^ Parses and returns the specified character. char :: Char -> ReadP Char char c :: Char c = (Char -> Bool) -> ReadP Char satisfy (Char c Char -> Char -> Bool forall a. Eq a => a -> a -> Bool ==) eof :: ReadP () -- ^ Succeeds iff we are at the end of input eof :: ReadP () eof = do { String s <- ReadP String look ; if String -> Bool forall a. [a] -> Bool null String s then () -> ReadP () forall (m :: * -> *) a. Monad m => a -> m a return () else ReadP () forall a. ReadP a pfail } string :: String -> ReadP String -- ^ Parses and returns the specified string. string :: String -> ReadP String string this :: String this = do String s <- ReadP String look; String -> String -> ReadP String forall a. Eq a => [a] -> [a] -> ReadP String scan String this String s where scan :: [a] -> [a] -> ReadP String scan [] _ = do String -> ReadP String forall (m :: * -> *) a. Monad m => a -> m a return String this scan (x :: a x:xs :: [a] xs) (y :: a y:ys :: [a] ys) | a x a -> a -> Bool forall a. Eq a => a -> a -> Bool == a y = do Char _ <- ReadP Char get; [a] -> [a] -> ReadP String scan [a] xs [a] ys scan _ _ = do ReadP String forall a. ReadP a pfail munch :: (Char -> Bool) -> ReadP String -- ^ Parses the first zero or more characters satisfying the predicate. -- Always succeeds, exactly once having consumed all the characters -- Hence NOT the same as (many (satisfy p)) munch :: (Char -> Bool) -> ReadP String munch p :: Char -> Bool p = do String s <- ReadP String look String -> ReadP String scan String s where scan :: String -> ReadP String scan (c :: Char c:cs :: String cs) | Char -> Bool p Char c = do Char _ <- ReadP Char get; String s <- String -> ReadP String scan String cs; String -> ReadP String forall (m :: * -> *) a. Monad m => a -> m a return (Char cChar -> String -> String forall a. a -> [a] -> [a] :String s) scan _ = do String -> ReadP String forall (m :: * -> *) a. Monad m => a -> m a return "" munch1 :: (Char -> Bool) -> ReadP String -- ^ Parses the first one or more characters satisfying the predicate. -- Fails if none, else succeeds exactly once having consumed all the characters -- Hence NOT the same as (many1 (satisfy p)) munch1 :: (Char -> Bool) -> ReadP String munch1 p :: Char -> Bool p = do Char c <- ReadP Char get if Char -> Bool p Char c then do String s <- (Char -> Bool) -> ReadP String munch Char -> Bool p; String -> ReadP String forall (m :: * -> *) a. Monad m => a -> m a return (Char cChar -> String -> String forall a. a -> [a] -> [a] :String s) else ReadP String forall a. ReadP a pfail choice :: [ReadP a] -> ReadP a -- ^ Combines all parsers in the specified list. choice :: [ReadP a] -> ReadP a choice [] = ReadP a forall a. ReadP a pfail choice [p :: ReadP a p] = ReadP a p choice (p :: ReadP a p:ps :: [ReadP a] ps) = ReadP a p ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a +++ [ReadP a] -> ReadP a forall a. [ReadP a] -> ReadP a choice [ReadP a] ps skipSpaces :: ReadP () -- ^ Skips all whitespace. skipSpaces :: ReadP () skipSpaces = do String s <- ReadP String look String -> ReadP () skip String s where skip :: String -> ReadP () skip (c :: Char c:s :: String s) | Char -> Bool isSpace Char c = do Char _ <- ReadP Char get; String -> ReadP () skip String s skip _ = do () -> ReadP () forall (m :: * -> *) a. Monad m => a -> m a return () count :: Int -> ReadP a -> ReadP [a] -- ^ @count n p@ parses @n@ occurrences of @p@ in sequence. A list of -- results is returned. count :: Int -> ReadP a -> ReadP [a] count n :: Int n p :: ReadP a p = [ReadP a] -> ReadP [a] forall (m :: * -> *) a. Monad m => [m a] -> m [a] sequence (Int -> ReadP a -> [ReadP a] forall a. Int -> a -> [a] replicate Int n ReadP a p) between :: ReadP open -> ReadP close -> ReadP a -> ReadP a -- ^ @between open close p@ parses @open@, followed by @p@ and finally -- @close@. Only the value of @p@ is returned. between :: ReadP open -> ReadP close -> ReadP a -> ReadP a between open :: ReadP open open close :: ReadP close close p :: ReadP a p = do open _ <- ReadP open open a x <- ReadP a p close _ <- ReadP close close a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x option :: a -> ReadP a -> ReadP a -- ^ @option x p@ will either parse @p@ or return @x@ without consuming -- any input. option :: a -> ReadP a -> ReadP a option x :: a x p :: ReadP a p = ReadP a p ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a +++ a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x optional :: ReadP a -> ReadP () -- ^ @optional p@ optionally parses @p@ and always returns @()@. optional :: ReadP a -> ReadP () optional p :: ReadP a p = (ReadP a p ReadP a -> ReadP () -> ReadP () forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> () -> ReadP () forall (m :: * -> *) a. Monad m => a -> m a return ()) ReadP () -> ReadP () -> ReadP () forall a. ReadP a -> ReadP a -> ReadP a +++ () -> ReadP () forall (m :: * -> *) a. Monad m => a -> m a return () many :: ReadP a -> ReadP [a] -- ^ Parses zero or more occurrences of the given parser. many :: ReadP a -> ReadP [a] many p :: ReadP a p = [a] -> ReadP [a] forall (m :: * -> *) a. Monad m => a -> m a return [] ReadP [a] -> ReadP [a] -> ReadP [a] forall a. ReadP a -> ReadP a -> ReadP a +++ ReadP a -> ReadP [a] forall a. ReadP a -> ReadP [a] many1 ReadP a p many1 :: ReadP a -> ReadP [a] -- ^ Parses one or more occurrences of the given parser. many1 :: ReadP a -> ReadP [a] many1 p :: ReadP a p = (a -> [a] -> [a]) -> ReadP a -> ReadP [a] -> ReadP [a] forall (m :: * -> *) a1 a2 r. Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r liftM2 (:) ReadP a p (ReadP a -> ReadP [a] forall a. ReadP a -> ReadP [a] many ReadP a p) skipMany :: ReadP a -> ReadP () -- ^ Like 'many', but discards the result. skipMany :: ReadP a -> ReadP () skipMany p :: ReadP a p = ReadP a -> ReadP [a] forall a. ReadP a -> ReadP [a] many ReadP a p ReadP [a] -> ReadP () -> ReadP () forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> () -> ReadP () forall (m :: * -> *) a. Monad m => a -> m a return () skipMany1 :: ReadP a -> ReadP () -- ^ Like 'many1', but discards the result. skipMany1 :: ReadP a -> ReadP () skipMany1 p :: ReadP a p = ReadP a p ReadP a -> ReadP () -> ReadP () forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> ReadP a -> ReadP () forall a. ReadP a -> ReadP () skipMany ReadP a p sepBy :: ReadP a -> ReadP sep -> ReadP [a] -- ^ @sepBy p sep@ parses zero or more occurrences of @p@, separated by @sep@. -- Returns a list of values returned by @p@. sepBy :: ReadP a -> ReadP sep -> ReadP [a] sepBy p :: ReadP a p sep :: ReadP sep sep = ReadP a -> ReadP sep -> ReadP [a] forall a sep. ReadP a -> ReadP sep -> ReadP [a] sepBy1 ReadP a p ReadP sep sep ReadP [a] -> ReadP [a] -> ReadP [a] forall a. ReadP a -> ReadP a -> ReadP a +++ [a] -> ReadP [a] forall (m :: * -> *) a. Monad m => a -> m a return [] sepBy1 :: ReadP a -> ReadP sep -> ReadP [a] -- ^ @sepBy1 p sep@ parses one or more occurrences of @p@, separated by @sep@. -- Returns a list of values returned by @p@. sepBy1 :: ReadP a -> ReadP sep -> ReadP [a] sepBy1 p :: ReadP a p sep :: ReadP sep sep = (a -> [a] -> [a]) -> ReadP a -> ReadP [a] -> ReadP [a] forall (m :: * -> *) a1 a2 r. Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r liftM2 (:) ReadP a p (ReadP a -> ReadP [a] forall a. ReadP a -> ReadP [a] many (ReadP sep sep ReadP sep -> ReadP a -> ReadP a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> ReadP a p)) endBy :: ReadP a -> ReadP sep -> ReadP [a] -- ^ @endBy p sep@ parses zero or more occurrences of @p@, separated and ended -- by @sep@. endBy :: ReadP a -> ReadP sep -> ReadP [a] endBy p :: ReadP a p sep :: ReadP sep sep = ReadP a -> ReadP [a] forall a. ReadP a -> ReadP [a] many (do a x <- ReadP a p ; sep _ <- ReadP sep sep ; a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x) endBy1 :: ReadP a -> ReadP sep -> ReadP [a] -- ^ @endBy p sep@ parses one or more occurrences of @p@, separated and ended -- by @sep@. endBy1 :: ReadP a -> ReadP sep -> ReadP [a] endBy1 p :: ReadP a p sep :: ReadP sep sep = ReadP a -> ReadP [a] forall a. ReadP a -> ReadP [a] many1 (do a x <- ReadP a p ; sep _ <- ReadP sep sep ; a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x) chainr :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP 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. chainr :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a chainr p :: ReadP a p op :: ReadP (a -> a -> a) op x :: a x = ReadP a -> ReadP (a -> a -> a) -> ReadP a forall a. ReadP a -> ReadP (a -> a -> a) -> ReadP a chainr1 ReadP a p ReadP (a -> a -> a) op ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a +++ a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x chainl :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP 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. chainl :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a chainl p :: ReadP a p op :: ReadP (a -> a -> a) op x :: a x = ReadP a -> ReadP (a -> a -> a) -> ReadP a forall a. ReadP a -> ReadP (a -> a -> a) -> ReadP a chainl1 ReadP a p ReadP (a -> a -> a) op ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a +++ a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x chainr1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a -- ^ Like 'chainr', but parses one or more occurrences of @p@. chainr1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a chainr1 p :: ReadP a p op :: ReadP (a -> a -> a) op = ReadP a scan where scan :: ReadP a scan = ReadP a p ReadP a -> (a -> ReadP a) -> ReadP a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>= a -> ReadP a rest rest :: a -> ReadP a rest x :: a x = do a -> a -> a f <- ReadP (a -> a -> a) op a y <- ReadP a scan a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return (a -> a -> a f a x a y) ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a +++ a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x chainl1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a -- ^ Like 'chainl', but parses one or more occurrences of @p@. chainl1 :: ReadP a -> ReadP (a -> a -> a) -> ReadP a chainl1 p :: ReadP a p op :: ReadP (a -> a -> a) op = ReadP a p ReadP a -> (a -> ReadP a) -> ReadP a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>= a -> ReadP a rest where rest :: a -> ReadP a rest x :: a x = do a -> a -> a f <- ReadP (a -> a -> a) op a y <- ReadP a p a -> ReadP a rest (a -> a -> a f a x a y) ReadP a -> ReadP a -> ReadP a forall a. ReadP a -> ReadP a -> ReadP a +++ a -> ReadP a forall (m :: * -> *) a. Monad m => a -> m a return a x manyTill :: ReadP a -> ReadP end -> ReadP [a] -- ^ @manyTill p end@ parses zero or more occurrences of @p@, until @end@ -- succeeds. Returns a list of values returned by @p@. manyTill :: ReadP a -> ReadP end -> ReadP [a] manyTill p :: ReadP a p end :: ReadP end end = ReadP [a] scan where scan :: ReadP [a] scan = (ReadP end end ReadP end -> ReadP [a] -> ReadP [a] forall (m :: * -> *) a b. Monad m => m a -> m b -> m b >> [a] -> ReadP [a] forall (m :: * -> *) a. Monad m => a -> m a return []) ReadP [a] -> ReadP [a] -> ReadP [a] forall a. ReadP a -> ReadP a -> ReadP a <++ ((a -> [a] -> [a]) -> ReadP a -> ReadP [a] -> ReadP [a] forall (m :: * -> *) a1 a2 r. Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r liftM2 (:) ReadP a p ReadP [a] scan) -- --------------------------------------------------------------------------- -- Converting between ReadP and Read readP_to_S :: ReadP a -> ReadS a -- ^ Converts a parser into a Haskell ReadS-style function. -- This is the main way in which you can \"run\" a 'ReadP' parser: -- the expanded type is -- @ readP_to_S :: ReadP a -> String -> [(a,String)] @ readP_to_S :: ReadP a -> ReadS a readP_to_S (R f :: forall b. (a -> P b) -> P b f) = P a -> ReadS a forall a. P a -> ReadS a run ((a -> P a) -> P a forall b. (a -> P b) -> P b f a -> P a forall (m :: * -> *) a. Monad m => a -> m a return) readS_to_P :: ReadS a -> ReadP a -- ^ Converts a Haskell ReadS-style function into a parser. -- Warning: This introduces local backtracking in the resulting -- parser, and therefore a possible inefficiency. readS_to_P :: ReadS a -> ReadP a readS_to_P r :: ReadS a r = (forall b. (a -> P b) -> P b) -> ReadP a forall a. (forall b. (a -> P b) -> P b) -> ReadP a R (\k :: a -> P b k -> (String -> P b) -> P b forall a. (String -> P a) -> P a Look (\s :: String s -> [(b, String)] -> P b forall a. [(a, String)] -> P a final [(b, String) bs'' | (a :: a a,s' :: String s') <- ReadS a r String s, (b, String) bs'' <- P b -> ReadS b forall a. P a -> ReadS a run (a -> P b k a a) String s'])) -- --------------------------------------------------------------------------- -- QuickCheck properties that hold for the combinators {- $properties The following are QuickCheck specifications of what the combinators do. These can be seen as formal specifications of the behavior of the combinators. For some values, we only care about the lists contents, not their order, > (=~) :: Ord a => [a] -> [a] -> Bool > xs =~ ys = sort xs == sort ys Here follow the properties: >>> readP_to_S get [] [] prop> \c str -> readP_to_S get (c:str) == [(c, str)] prop> \str -> readP_to_S look str == [(str, str)] prop> \str -> readP_to_S pfail str == [] prop> \x str -> 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> \this str -> readP_to_S (string this) (this ++ str) == [(this,str)] > 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 -}