-{-# OPTIONS -fglasgow-exts -fno-implicit-prelude #-}
+{-# LANGUAGE CPP, NoImplicitPrelude #-}
+#ifndef __NHC__
+{-# LANGUAGE Rank2Types #-}
+#endif
+#ifdef __GLASGOW_HASKELL__
+{-# LANGUAGE MagicHash #-}
+#endif
+
-----------------------------------------------------------------------------
-- |
-- Module : Text.ParserCombinators.ReadP
module Text.ParserCombinators.ReadP
(
-- * The 'ReadP' type
+#ifndef __NHC__
ReadP, -- :: * -> *; instance Functor, Monad, MonadPlus
+#else
+ ReadPN, -- :: * -> * -> *; instance Functor, Monad, MonadPlus
+#endif
-- * Primitive operations
get, -- :: ReadP Char
-- * Other operations
pfail, -- :: ReadP a
+ eof, -- :: ReadP ()
satisfy, -- :: (Char -> Bool) -> ReadP Char
char, -- :: Char -> ReadP Char
string, -- :: String -> ReadP String
#ifndef __HADDOCK__
import {-# SOURCE #-} GHC.Unicode ( isSpace )
#endif
-import GHC.List ( replicate )
+import GHC.List ( replicate, null )
import GHC.Base
#else
import Data.Char( isSpace )
(Get f) >>= k = Get (\c -> f c >>= k)
(Look f) >>= k = Look (\s -> f s >>= k)
- Fail >>= k = Fail
+ Fail >>= _ = Fail
(Result x p) >>= k = k x `mplus` (p >>= k)
(Final r) >>= k = final [ys' | (x,s) <- r, ys' <- run (k x) s]
-- ---------------------------------------------------------------------------
-- The ReadP type
+#ifndef __NHC__
newtype ReadP a = R (forall b . (a -> P b) -> P b)
+#else
+#define ReadP (ReadPN b)
+newtype ReadPN b a = R ((a -> P b) -> P b)
+#endif
-- Functor, Monad, MonadPlus
-- ^ Symmetric choice.
R f1 +++ R f2 = R (\k -> f1 k `mplus` f2 k)
+#ifndef __NHC__
(<++) :: ReadP a -> ReadP a -> ReadP a
+#else
+(<++) :: ReadPN a a -> ReadPN a a -> ReadPN a a
+#endif
-- ^ Local, exclusive, left-biased choice: If left parser
-- locally produces any result at all, then right parser is
-- not used.
#ifdef __GLASGOW_HASKELL__
-R f <++ q =
+R f0 <++ q =
do s <- look
- probe (f return) s 0#
+ probe (f0 return) s 0#
where
probe (Get f) (c:s) n = probe (f c) s (n+#1#)
probe (Look f) s n = probe (f s) s n
discard n = get >> discard (n-1)
#endif
+#ifndef __NHC__
gather :: ReadP a -> ReadP (String, a)
+#else
+-- gather :: ReadPN (String->P b) a -> ReadPN (String->P b) (String, a)
+#endif
-- ^ 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 (R m) =
- R (\k -> gath id (m (\a -> return (\s -> k (s,a)))))
+gather (R m)
+ = R (\k -> gath id (m (\a -> return (\s -> k (s,a)))))
where
+ gath :: (String -> String) -> P (String -> P b) -> P b
gath l (Get f) = Get (\c -> gath (l.(c:)) (f c))
- gath l Fail = Fail
+ gath _ Fail = Fail
gath l (Look f) = Look (\s -> gath l (f s))
gath l (Result k p) = k (l []) `mplus` gath l p
- gath l (Final r) = error "do not use readS_to_P in gather!"
+ gath _ (Final _) = error "do not use readS_to_P in gather!"
-- ---------------------------------------------------------------------------
-- Derived operations
-- ^ Parses and returns the specified character.
char c = satisfy (c ==)
+eof :: ReadP ()
+-- ^ Succeeds iff we are at the end of input
+eof = do { s <- look
+ ; if null s then return ()
+ else pfail }
+
string :: String -> ReadP String
-- ^ Parses and returns the specified string.
string this = do s <- look; scan this s
where
scan [] _ = do return this
- scan (x:xs) (y:ys) | x == y = do get; scan xs ys
+ scan (x:xs) (y:ys) | x == y = do _ <- get; scan xs ys
scan _ _ = do pfail
munch :: (Char -> Bool) -> ReadP String
-- ^ Parses the first zero or more characters satisfying the predicate.
+-- Always succeds, exactly once having consumed all the characters
+-- Hence NOT the same as (many (satisfy p))
munch p =
do s <- look
scan s
where
- scan (c:cs) | p c = do get; s <- scan cs; return (c:s)
+ scan (c:cs) | p c = do _ <- get; s <- scan cs; return (c:s)
scan _ = do 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 p =
do c <- get
- if p c then do s <- munch p; return (c:s) else pfail
+ if p c then do s <- munch p; return (c:s)
+ else pfail
choice :: [ReadP a] -> ReadP a
-- ^ Combines all parsers in the specified list.
do s <- look
skip s
where
- skip (c:s) | isSpace c = do get; skip s
+ skip (c:s) | isSpace c = do _ <- get; skip s
skip _ = do return ()
count :: Int -> ReadP a -> ReadP [a]
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 open close p = do open
+between open close p = do _ <- open
x <- p
- close
+ _ <- close
return x
option :: a -> ReadP a -> ReadP a
endBy :: ReadP a -> ReadP sep -> ReadP [a]
-- ^ @endBy p sep@ parses zero or more occurrences of @p@, separated and ended
-- by @sep@.
-endBy p sep = many (do x <- p ; sep ; return x)
+endBy p sep = many (do x <- p ; _ <- sep ; return x)
endBy1 :: ReadP a -> ReadP sep -> ReadP [a]
-- ^ @endBy p sep@ parses one or more occurrences of @p@, separated and ended
-- by @sep@.
-endBy1 p sep = many1 (do x <- p ; sep ; return x)
+endBy1 p sep = many1 (do x <- p ; _ <- sep ; return 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@.
rest (f x y)
+++ return x
+#ifndef __NHC__
manyTill :: ReadP a -> ReadP end -> ReadP [a]
+#else
+manyTill :: ReadPN [a] a -> ReadPN [a] end -> ReadPN [a] [a]
+#endif
-- ^ @manyTill p end@ parses zero or more occurrences of @p@, until @end@
-- succeeds. Returns a list of values returned by @p@.
manyTill p end = scan
-- ---------------------------------------------------------------------------
-- Converting between ReadP and Read
+#ifndef __NHC__
readP_to_S :: ReadP a -> ReadS a
+#else
+readP_to_S :: ReadPN a a -> ReadS a
+#endif
-- ^ 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