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
61 import Control.Monad( MonadPlus(..) )
62 import GHC.Num( Num(..) )
65 -- ---------------------------------------------------------------------------
68 newtype ReadPrec a = P { unP :: Prec -> ReadP a }
70 -- Functor, Monad, MonadPlus
72 instance Functor ReadPrec where
73 fmap h (P f) = P (\n -> fmap h (f n))
75 instance Monad ReadPrec where
76 return x = P (\_ -> return x)
77 fail s = P (\_ -> fail s)
78 P f >>= k = P (\n -> do a <- f n; let P f' = k a in f' n)
80 instance MonadPlus ReadPrec where
91 -- ---------------------------------------------------------------------------
92 -- Operations over ReadPrec
94 lift :: ReadP a -> ReadPrec a
95 -- ^ Lift a predence-insensitive 'ReadP' to a 'ReadPrec'
98 step :: ReadPrec a -> ReadPrec a
99 -- ^ Increases the precedence context by one
100 step (P f) = P (\n -> f (n+1))
102 reset :: ReadPrec a -> ReadPrec a
103 -- ^ Resets the precedence context to zero
104 reset (P f) = P (\n -> f minPrec)
106 prec :: Prec -> ReadPrec a -> ReadPrec a
107 -- ^ @(prec n p)@ checks that the precedence context is
108 -- less than or equal to n,
110 -- * if so, parses p in context n
111 prec n (P f) = P (\c -> if c <= n then f n else ReadP.pfail)
113 -- ---------------------------------------------------------------------------
114 -- Derived operations
119 look :: ReadPrec String
120 look = lift ReadP.look
122 (+++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
123 P f1 +++ P f2 = P (\n -> f1 n ReadP.+++ f2 n)
126 pfail = lift ReadP.pfail
128 choice :: [ReadPrec a] -> ReadPrec a
129 choice ps = foldr (+++) pfail ps
131 -- ---------------------------------------------------------------------------
132 -- Converting between ReadPrec and Read
134 -- We define a local version of ReadS here,
135 -- because its "real" definition site is in GHC.Read
136 type ReadS a = String -> [(a,String)]
138 readPrec_to_P :: ReadPrec a -> (Int -> ReadP a)
139 readPrec_to_P (P f) = f
141 readP_to_Prec :: (Int -> ReadP a) -> ReadPrec a
142 readP_to_Prec f = P f
144 readPrec_to_S :: ReadPrec a -> (Int -> ReadS a)
145 readPrec_to_S (P f) n = readP_to_S (f n)
147 readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
148 readS_to_Prec f = P (\n -> readS_to_P (f n))