1 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : Text.ParserCombinators.ReadPrec
6 -- Copyright : (c) The University of Glasgow 2002
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : non-portable (uses Text.ParserCombinators.ReadP)
13 -- This library defines parser combinators for precedence parsing.
15 -----------------------------------------------------------------------------
17 module Text.ParserCombinators.ReadPrec
19 ReadPrec, -- :: * -> *; instance Functor, Monad, MonadPlus
23 minPrec, -- :: Prec; = 0
25 -- * Precedence operations
26 lift, -- :: ReadP a -> ReadPrec a
27 prec, -- :: Prec -> ReadPrec a -> ReadPrec a
28 step, -- :: ReadPrec a -> ReadPrec a
29 reset, -- :: ReadPrec a -> ReadPrec a
32 -- | All are based directly on their similarly-named 'ReadP' counterparts.
33 get, -- :: ReadPrec Char
34 look, -- :: ReadPrec String
35 (+++), -- :: ReadPrec a -> ReadPrec a -> ReadPrec a
36 (<++), -- :: ReadPrec a -> ReadPrec a -> ReadPrec a
37 pfail, -- :: ReadPrec a
38 choice, -- :: [ReadPrec a] -> ReadPrec a
41 readPrec_to_P, -- :: ReadPrec a -> (Int -> ReadP a)
42 readP_to_Prec, -- :: (Int -> ReadP a) -> ReadPrec a
43 readPrec_to_S, -- :: ReadPrec a -> (Int -> ReadS a)
44 readS_to_Prec, -- :: (Int -> ReadS a) -> ReadPrec a
49 import Text.ParserCombinators.ReadP
56 import qualified Text.ParserCombinators.ReadP as ReadP
63 import Control.Monad( MonadPlus(..) )
64 #ifdef __GLASGOW_HASKELL__
65 import GHC.Num( Num(..) )
69 -- ---------------------------------------------------------------------------
72 newtype ReadPrec a = P (Prec -> ReadP a)
74 -- Functor, Monad, MonadPlus
76 instance Functor ReadPrec where
77 fmap h (P f) = P (\n -> fmap h (f n))
79 instance Monad ReadPrec where
80 return x = P (\_ -> return x)
81 fail s = P (\_ -> fail s)
82 P f >>= k = P (\n -> do a <- f n; let P f' = k a in f' n)
84 instance MonadPlus ReadPrec where
95 -- ---------------------------------------------------------------------------
96 -- Operations over ReadPrec
98 lift :: ReadP a -> ReadPrec a
99 -- ^ Lift a precedence-insensitive 'ReadP' to a 'ReadPrec'.
102 step :: ReadPrec a -> ReadPrec a
103 -- ^ Increases the precedence context by one.
104 step (P f) = P (\n -> f (n+1))
106 reset :: ReadPrec a -> ReadPrec a
107 -- ^ Resets the precedence context to zero.
108 reset (P f) = P (\_ -> f minPrec)
110 prec :: Prec -> ReadPrec a -> ReadPrec a
111 -- ^ @(prec n p)@ checks whether the precedence context is
112 -- less than or equal to @n@, and
116 -- * if so, parses @p@ in context @n@.
117 prec n (P f) = P (\c -> if c <= n then f n else ReadP.pfail)
119 -- ---------------------------------------------------------------------------
120 -- Derived operations
123 -- ^ Consumes and returns the next character.
124 -- Fails if there is no input left.
127 look :: ReadPrec String
128 -- ^ Look-ahead: returns the part of the input that is left, without
130 look = lift ReadP.look
132 (+++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
133 -- ^ Symmetric choice.
134 P f1 +++ P f2 = P (\n -> f1 n ReadP.+++ f2 n)
136 (<++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
137 -- ^ Local, exclusive, left-biased choice: If left parser
138 -- locally produces any result at all, then right parser is
140 P f1 <++ P f2 = P (\n -> f1 n ReadP.<++ f2 n)
144 pfail = lift ReadP.pfail
146 choice :: [ReadPrec a] -> ReadPrec a
147 -- ^ Combines all parsers in the specified list.
148 choice ps = foldr (+++) pfail ps
150 -- ---------------------------------------------------------------------------
151 -- Converting between ReadPrec and Read
153 readPrec_to_P :: ReadPrec a -> (Int -> ReadP a)
154 readPrec_to_P (P f) = f
156 readP_to_Prec :: (Int -> ReadP a) -> ReadPrec a
157 readP_to_Prec f = P f
159 readPrec_to_S :: ReadPrec a -> (Int -> ReadS a)
160 readPrec_to_S (P f) n = readP_to_S (f n)
162 readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
163 readS_to_Prec f = P (\n -> readS_to_P (f n))