{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Text.ParserCombinators.ReadPrec
-- 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 (uses Text.ParserCombinators.ReadP)
--
-- This library defines parser combinators for precedence parsing.

-----------------------------------------------------------------------------

module Text.ParserCombinators.ReadPrec
  (
  ReadPrec,

  -- * Precedences
  Prec,
  minPrec,

  -- * Precedence operations
  lift,
  prec,
  step,
  reset,

  -- * Other operations
  -- | All are based directly on their similarly-named 'ReadP' counterparts.
  get,
  look,
  (+++),
  (<++),
  pfail,
  choice,

  -- * Converters
  readPrec_to_P,
  readP_to_Prec,
  readPrec_to_S,
  readS_to_Prec,
  )
 where


import Text.ParserCombinators.ReadP
  ( ReadP
  , ReadS
  , readP_to_S
  , readS_to_P
  )

import qualified Text.ParserCombinators.ReadP as ReadP
  ( get
  , look
  , (+++), (<++)
  , pfail
  )

import GHC.Num( Num(..) )
import GHC.Base

import Control.Monad.Fail

-- ---------------------------------------------------------------------------
-- The readPrec type

newtype ReadPrec a = P (Prec -> ReadP a)

-- Functor, Monad, MonadPlus

-- | @since 2.01
instance Functor ReadPrec where
  fmap :: forall a b. (a -> b) -> ReadPrec a -> ReadPrec b
fmap a -> b
h (P Prec -> ReadP a
f) = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
h (Prec -> ReadP a
f Prec
n))

-- | @since 4.6.0.0
instance Applicative ReadPrec where
    pure :: forall a. a -> ReadPrec a
pure a
x  = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    <*> :: forall a b. ReadPrec (a -> b) -> ReadPrec a -> ReadPrec b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
    liftA2 :: forall a b c.
(a -> b -> c) -> ReadPrec a -> ReadPrec b -> ReadPrec c
liftA2 = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2

-- | @since 2.01
instance Monad ReadPrec where
  P Prec -> ReadP a
f >>= :: forall a b. ReadPrec a -> (a -> ReadPrec b) -> ReadPrec b
>>= a -> ReadPrec b
k = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> do a
a <- Prec -> ReadP a
f Prec
n; let P Prec -> ReadP b
f' = a -> ReadPrec b
k a
a in Prec -> ReadP b
f' Prec
n)

-- | @since 4.9.0.0
instance MonadFail ReadPrec where
  fail :: forall a. String -> ReadPrec a
fail String
s    = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
s)

-- | @since 2.01
instance MonadPlus ReadPrec

-- | @since 4.6.0.0
instance Alternative ReadPrec where
  empty :: forall a. ReadPrec a
empty = forall a. ReadPrec a
pfail
  <|> :: forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
(<|>) = forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
(+++)

-- precedences
type Prec = Int

minPrec :: Prec
minPrec :: Prec
minPrec = Prec
0

-- ---------------------------------------------------------------------------
-- Operations over ReadPrec

lift :: ReadP a -> ReadPrec a
-- ^ Lift a precedence-insensitive 'ReadP' to a 'ReadPrec'.
lift :: forall a. ReadP a -> ReadPrec a
lift ReadP a
m = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> ReadP a
m)

step :: ReadPrec a -> ReadPrec a
-- ^ Increases the precedence context by one.
step :: forall a. ReadPrec a -> ReadPrec a
step (P Prec -> ReadP a
f) = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> Prec -> ReadP a
f (Prec
nforall a. Num a => a -> a -> a
+Prec
1))

reset :: ReadPrec a -> ReadPrec a
-- ^ Resets the precedence context to zero.
reset :: forall a. ReadPrec a -> ReadPrec a
reset (P Prec -> ReadP a
f) = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
_ -> Prec -> ReadP a
f Prec
minPrec)

prec :: Prec -> ReadPrec a -> ReadPrec a
-- ^ @(prec n p)@ checks whether the precedence context is
--   less than or equal to @n@, and
--
--   * if not, fails
--
--   * if so, parses @p@ in context @n@.
prec :: forall a. Prec -> ReadPrec a -> ReadPrec a
prec Prec
n (P Prec -> ReadP a
f) = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
c -> if Prec
c forall a. Ord a => a -> a -> Bool
<= Prec
n then Prec -> ReadP a
f Prec
n else forall a. ReadP a
ReadP.pfail)

-- ---------------------------------------------------------------------------
-- Derived operations

get :: ReadPrec Char
-- ^ Consumes and returns the next character.
--   Fails if there is no input left.
get :: ReadPrec Char
get = forall a. ReadP a -> ReadPrec a
lift ReadP Char
ReadP.get

look :: ReadPrec String
-- ^ Look-ahead: returns the part of the input that is left, without
--   consuming it.
look :: ReadPrec String
look = forall a. ReadP a -> ReadPrec a
lift ReadP String
ReadP.look

(+++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
-- ^ Symmetric choice.
P Prec -> ReadP a
f1 +++ :: forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
+++ P Prec -> ReadP a
f2 = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> Prec -> ReadP a
f1 Prec
n forall a. ReadP a -> ReadP a -> ReadP a
ReadP.+++ Prec -> ReadP a
f2 Prec
n)

(<++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
-- ^ Local, exclusive, left-biased choice: If left parser
--   locally produces any result at all, then right parser is
--   not used.
P Prec -> ReadP a
f1 <++ :: forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
<++ P Prec -> ReadP a
f2 = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> Prec -> ReadP a
f1 Prec
n forall a. ReadP a -> ReadP a -> ReadP a
ReadP.<++ Prec -> ReadP a
f2 Prec
n)

pfail :: ReadPrec a
-- ^ Always fails.
pfail :: forall a. ReadPrec a
pfail = forall a. ReadP a -> ReadPrec a
lift forall a. ReadP a
ReadP.pfail

choice :: [ReadPrec a] -> ReadPrec a
-- ^ Combines all parsers in the specified list.
choice :: forall a. [ReadPrec a] -> ReadPrec a
choice [ReadPrec a]
ps = forall a b. (a -> b -> b) -> b -> [a] -> b
foldr forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
(+++) forall a. ReadPrec a
pfail [ReadPrec a]
ps

-- ---------------------------------------------------------------------------
-- Converting between ReadPrec and Read

readPrec_to_P :: ReadPrec a -> (Int -> ReadP a)
readPrec_to_P :: forall a. ReadPrec a -> Prec -> ReadP a
readPrec_to_P (P Prec -> ReadP a
f) = Prec -> ReadP a
f

readP_to_Prec :: (Int -> ReadP a) -> ReadPrec a
readP_to_Prec :: forall a. (Prec -> ReadP a) -> ReadPrec a
readP_to_Prec Prec -> ReadP a
f = forall a. (Prec -> ReadP a) -> ReadPrec a
P Prec -> ReadP a
f

readPrec_to_S :: ReadPrec a -> (Int -> ReadS a)
readPrec_to_S :: forall a. ReadPrec a -> Prec -> ReadS a
readPrec_to_S (P Prec -> ReadP a
f) Prec
n = forall a. ReadP a -> ReadS a
readP_to_S (Prec -> ReadP a
f Prec
n)

readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
readS_to_Prec :: forall a. (Prec -> ReadS a) -> ReadPrec a
readS_to_Prec Prec -> ReadS a
f = forall a. (Prec -> ReadP a) -> ReadPrec a
P (\Prec
n -> forall a. ReadS a -> ReadP a
readS_to_P (Prec -> ReadS a
f Prec
n))