1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Text.ParserCombinators.ReadPrec
5 -- Copyright : (c) The University of Glasgow 2002
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- This library defines parser combinators for precedence parsing.
14 -----------------------------------------------------------------------------
16 module Text.ParserCombinators.ReadPrec
18 ReadPrec, -- :: * -> *; instance Functor, Monad, MonadPlus
22 minPrec, -- :: Prec; = 0
24 -- * Precedence operations
25 lift, -- :: ReadP a -> ReadPrec a
26 prec, -- :: Prec -> ReadPrec a -> ReadPrec a
27 step, -- :: ReadPrec a -> ReadPrec a
28 reset, -- :: ReadPrec a -> ReadPrec a
31 -- All are based directly on their similarly-naned 'ReadP' counterparts.
32 get, -- :: ReadPrec Char
33 look, -- :: ReadPrec String
34 (+++), -- :: ReadPrec a -> ReadPrec a -> ReadPrec a
35 pfail, -- :: ReadPrec a
36 choice, -- :: [ReadPrec a] -> ReadPrec a
39 readPrec_to_P, -- :: ReadPrec a -> (Int -> ReadP a)
40 readP_to_Prec, -- :: (Int -> ReadP a) -> ReadPrec a
41 readPrec_to_S, -- :: ReadPrec a -> (Int -> ReadS a)
42 readS_to_Prec, -- :: (Int -> ReadS a) -> ReadPrec a
47 import Text.ParserCombinators.ReadP
53 import qualified Text.ParserCombinators.ReadP as ReadP
60 import Control.Monad( MonadPlus(..) )
61 import GHC.Num( Num(..) )
64 -- ---------------------------------------------------------------------------
67 newtype ReadPrec a = P { unP :: Prec -> ReadP a }
69 -- Functor, Monad, MonadPlus
71 instance Functor ReadPrec where
72 fmap h (P f) = P (\n -> fmap h (f n))
74 instance Monad ReadPrec where
75 return x = P (\_ -> return x)
76 fail s = P (\_ -> fail s)
77 P f >>= k = P (\n -> do a <- f n; let P f' = k a in f' n)
79 instance MonadPlus ReadPrec where
90 -- ---------------------------------------------------------------------------
91 -- Operations over ReadPrec
93 lift :: ReadP a -> ReadPrec a
94 -- ^ Lift a predence-insensitive 'ReadP' to a 'ReadPrec'
97 step :: ReadPrec a -> ReadPrec a
98 -- ^ Increases the precedence context by one
99 step (P f) = P (\n -> f (n+1))
101 reset :: ReadPrec a -> ReadPrec a
102 -- ^ Resets the precedence context to zero
103 reset (P f) = P (\n -> f minPrec)
105 prec :: Prec -> ReadPrec a -> ReadPrec a
106 -- ^ @(prec n p)@ checks that the precedence context is
107 -- less than or equal to n,
109 -- * if so, parses p in context n
110 prec n (P f) = P (\c -> if c <= n then f n else ReadP.pfail)
112 -- ---------------------------------------------------------------------------
113 -- Derived operations
118 look :: ReadPrec String
119 look = lift ReadP.look
121 (+++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
122 P f1 +++ P f2 = P (\n -> f1 n ReadP.+++ f2 n)
125 pfail = lift ReadP.pfail
127 choice :: [ReadPrec a] -> ReadPrec a
128 choice ps = foldr (+++) pfail ps
130 -- ---------------------------------------------------------------------------
131 -- Converting between ReadPrec and Read
133 -- We define a local version of ReadS here,
134 -- because its "real" definition site is in GHC.Read
135 type ReadS a = String -> [(a,String)]
137 readPrec_to_P :: ReadPrec a -> (Int -> ReadP a)
138 readPrec_to_P (P f) = f
140 readP_to_Prec :: (Int -> ReadP a) -> ReadPrec a
141 readP_to_Prec f = P f
143 readPrec_to_S :: ReadPrec a -> (Int -> ReadS a)
144 readPrec_to_S (P f) n = readP_to_S (f n)
146 readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
147 readS_to_Prec f = P (\n -> readS_to_P (f n))