{-# 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 (NonEmpty (a,String))
  deriving Functor -- ^ @since 4.8.0.0

-- Monad, MonadPlus

-- | @since 4.5.0.0
instance Applicative P where
  pure :: a -> P a
pure 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 Char -> P a
f)         >>= :: P a -> (a -> P b) -> P b
>>= a -> P b
k = (Char -> P b) -> P b
forall a. (Char -> P a) -> P a
Get (\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 String -> P a
f)        >>= a -> P b
k = (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\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)
  P a
Fail            >>= a -> P b
_ = P b
forall a. P a
Fail
  (Result a
x P a
p)    >>= 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 ((a, String)
r:|[(a, String)]
rs)) >>= a -> P b
k = [(b, String)] -> P b
forall a. [(a, String)] -> P a
final [(b, String)
ys' | (a
x,String
s) <- ((a, String)
r(a, String) -> [(a, String)] -> [(a, String)]
forall a. a -> [a] -> [a]
:[(a, String)]
rs), (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 String
_ = 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 Char -> P a
f1     <|> :: P a -> P a -> P a
<|> Get Char -> P a
f2     = (Char -> P a) -> P a
forall a. (Char -> P a) -> P a
Get (\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 a
x P a
p <|> 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 a
p          <|> Result a
x 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
  P a
Fail       <|> P a
p          = P a
p
  P a
p          <|> P a
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 NonEmpty (a, String)
r       <|> Final NonEmpty (a, String)
t = NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final (NonEmpty (a, String)
r NonEmpty (a, String)
-> NonEmpty (a, String) -> NonEmpty (a, String)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, String)
t)
  Final ((a, String)
r:|[(a, String)]
rs) <|> Look String -> P a
f  = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final ((a, String)
r(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|([(a, String)]
rs [(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 ((a, String)
r:|[(a, String)]
rs) <|> P a
p       = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final ((a, String)
r(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|([(a, String)]
rs [(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 String -> P a
f        <|> Final NonEmpty (a, String)
r = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final (case P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s of
                                []     -> NonEmpty (a, String)
r
                                ((a, String)
x:[(a, String)]
xs) -> ((a, String)
x(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|[(a, String)]
xs) NonEmpty (a, String)
-> NonEmpty (a, String) -> NonEmpty (a, String)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, String)
r))
  P a
p             <|> Final NonEmpty (a, String)
r = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\String
s -> NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final (case P a -> ReadS a
forall a. P a -> ReadS a
run P a
p String
s of
                                []     -> NonEmpty (a, String)
r
                                ((a, String)
x:[(a, String)]
xs) -> ((a, String)
x(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|[(a, String)]
xs) NonEmpty (a, String)
-> NonEmpty (a, String) -> NonEmpty (a, String)
forall a. Semigroup a => a -> a -> a
<> NonEmpty (a, String)
r))

  -- two looks are combined (=optimization)
  -- look + sthg else floats upwards
  Look String -> P a
f     <|> Look String -> P a
g     = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\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 String -> P a
f     <|> P a
p          = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\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 a
p          <|> Look String -> P a
f     = (String -> P a) -> P a
forall a. (String -> P a) -> P a
Look (\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 a -> b
h (R 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 (\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 a
x = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\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 forall b. (a -> P b) -> P b
m >>= :: ReadP a -> (a -> ReadP b) -> ReadP b
>>= 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 (\b -> P b
k -> (a -> P b) -> P b
forall b. (a -> P b) -> P b
m (\a
a -> let R 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 String
_    = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\a -> P b
_ -> 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
final :: [(a, String)] -> P a
final []     = P a
forall a. P a
Fail
final ((a, String)
r:[(a, String)]
rs) = NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final ((a, String)
r(a, String) -> [(a, String)] -> NonEmpty (a, String)
forall a. a -> [a] -> NonEmpty a
:|[(a, String)]
rs)

run :: P a -> ReadS a
run :: P a -> ReadS a
run (Get Char -> P a
f)         (Char
c:String
s) = P a -> ReadS a
forall a. P a -> ReadS a
run (Char -> P a
f Char
c) String
s
run (Look String -> P a
f)        String
s     = P a -> ReadS a
forall a. P a -> ReadS a
run (String -> P a
f String
s) String
s
run (Result a
x P a
p)    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 ((a, String)
r:|[(a, String)]
rs)) String
_     = ((a, String)
r(a, String) -> [(a, String)] -> [(a, String)]
forall a. a -> [a] -> [a]
:[(a, String)]
rs)
run P a
_               String
_     = []

-- ---------------------------------------------------------------------------
-- 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 (\a -> P b
_ -> P b
forall a. P a
Fail)

(+++) :: ReadP a -> ReadP a -> ReadP a
-- ^ Symmetric choice.
R forall b. (a -> P b) -> P b
f1 +++ :: ReadP a -> ReadP a -> ReadP a
+++ R 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 (\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 forall b. (a -> P b) -> P b
f0 <++ :: ReadP a -> ReadP a -> ReadP a
<++ 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 Int#
0#
 where
  probe :: P a -> String -> Int# -> ReadP a
probe (Get Char -> P a
f)        (Char
c:String
s) Int#
n = P a -> String -> Int# -> ReadP a
probe (Char -> P a
f Char
c) String
s (Int#
nInt# -> Int# -> Int#
+#Int#
1#)
  probe (Look String -> P a
f)       String
s     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 a
_ P a
_) String
_     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 NonEmpty (a, String)
r)      String
_     Int#
_ = (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (NonEmpty (a, String) -> P a
forall a. NonEmpty (a, String) -> P a
Final NonEmpty (a, String)
r P a -> (a -> P b) -> P b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=)
  probe P a
_              String
_     Int#
_ = ReadP a
q

  discard :: Int# -> ReadP ()
discard Int#
0# = () -> ReadP ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  discard 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#
-#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 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 (\(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 -> (String -> P b) -> P (String -> P b)
forall (m :: * -> *) a. Monad m => a -> m a
return (\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 String -> String
l (Get Char -> P (String -> P b)
f)      = (Char -> P b) -> P b
forall a. (Char -> P a) -> P a
Get (\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 String -> String
_ P (String -> P b)
Fail         = P b
forall a. P a
Fail
  gath String -> String
l (Look String -> P (String -> P b)
f)     = (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\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 String -> String
l (Result String -> P b
k 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 String -> String
_ (Final NonEmpty (String -> P b, String)
_)    = String -> P b
forall a. String -> a
errorWithoutStackTrace String
"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 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 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 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 []     [a]
_               = do String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return String
this
  scan (a
x:[a]
xs) (a
y:[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 [a]
_      [a]
_               = 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 Char -> Bool
p =
  do String
s <- ReadP String
look
     String -> ReadP String
scan String
s
 where
  scan :: String -> ReadP String
scan (Char
c: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 String
_            = do String -> ReadP String
forall (m :: * -> *) a. Monad m => a -> m a
return String
""

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 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 [ReadP a
p]    = ReadP a
p
choice (ReadP a
p:[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 (Char
c:String
s) | Char -> Bool
isSpace Char
c = do Char
_ <- ReadP Char
get; String -> ReadP ()
skip String
s
  skip String
_                 = 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 Int
n 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 ReadP open
open ReadP close
close 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 a
x 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 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 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 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 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 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 ReadP a
p 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 ReadP a
p 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 ReadP a
p 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 ReadP a
p 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 ReadP a
p ReadP (a -> a -> a)
op 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 ReadP a
p ReadP (a -> a -> a)
op 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 ReadP a
p 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 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 ReadP a
p 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 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 ReadP a
p 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 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 ReadS a
r =
  (forall b. (a -> P b) -> P b) -> ReadP a
forall a. (forall b. (a -> P b) -> P b) -> ReadP a
R (\a -> P b
k -> (String -> P b) -> P b
forall a. (String -> P a) -> P a
Look (\String
s -> [(b, String)] -> P b
forall a. [(a, String)] -> P a
final [(b, String)
bs'' | (a
a,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
-}