[project @ 2003-04-17 15:17:07 by simonpj]
[haskell-directory.git] / Text / ParserCombinators / ReadPrec.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Text.ParserCombinators.ReadPrec
5 -- Copyright   :  (c) The University of Glasgow 2002
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
12 -- This library defines parser combinators for precedence parsing.
13
14 -----------------------------------------------------------------------------
15
16 module Text.ParserCombinators.ReadPrec
17   ( 
18   ReadPrec,      -- :: * -> *; instance Functor, Monad, MonadPlus
19   
20   -- * Precedences
21   Prec,          -- :: *; = Int
22   minPrec,       -- :: Prec; = 0
23
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
29
30   -- * Other operations
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
37
38   -- * Converters
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
43   )
44  where
45
46
47 import Text.ParserCombinators.ReadP
48   ( ReadP
49   , readP_to_S
50   , readS_to_P
51   )
52
53 import qualified Text.ParserCombinators.ReadP as ReadP
54   ( get
55   , look
56   , (+++)
57   , pfail
58   )
59
60 import Control.Monad( MonadPlus(..) )
61 import GHC.Num( Num(..) )
62 import GHC.Base
63
64 -- ---------------------------------------------------------------------------
65 -- The readPrec type
66
67 newtype ReadPrec a = P { unP :: Prec -> ReadP a }
68
69 -- Functor, Monad, MonadPlus
70
71 instance Functor ReadPrec where
72   fmap h (P f) = P (\n -> fmap h (f n))
73
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)
78   
79 instance MonadPlus ReadPrec where
80   mzero = pfail
81   mplus = (+++)
82
83 -- precedences
84   
85 type Prec = Int
86
87 minPrec :: Prec
88 minPrec = 0
89
90 -- ---------------------------------------------------------------------------
91 -- Operations over ReadPrec
92
93 lift :: ReadP a -> ReadPrec a
94 -- ^ Lift a predence-insensitive 'ReadP' to a 'ReadPrec'
95 lift m = P (\_ -> m)
96
97 step :: ReadPrec a -> ReadPrec a
98 -- ^ Increases the precedence context by one
99 step (P f) = P (\n -> f (n+1))
100
101 reset :: ReadPrec a -> ReadPrec a
102 -- ^ Resets the precedence context to zero
103 reset (P f) = P (\n -> f minPrec)
104
105 prec :: Prec -> ReadPrec a -> ReadPrec a
106 -- ^ @(prec n p)@ checks that the precedence context is 
107 --                        less than or equal to n,
108 --   * if not, fails
109 --   * if so, parses p in context n
110 prec n (P f) = P (\c -> if c <= n then f n else ReadP.pfail)
111
112 -- ---------------------------------------------------------------------------
113 -- Derived operations
114
115 get :: ReadPrec Char
116 get = lift ReadP.get
117
118 look :: ReadPrec String
119 look = lift ReadP.look
120
121 (+++) :: ReadPrec a -> ReadPrec a -> ReadPrec a
122 P f1 +++ P f2 = P (\n -> f1 n ReadP.+++ f2 n)
123
124 pfail :: ReadPrec a
125 pfail = lift ReadP.pfail
126
127 choice :: [ReadPrec a] -> ReadPrec a
128 choice ps = foldr (+++) pfail ps
129
130 -- ---------------------------------------------------------------------------
131 -- Converting between ReadPrec and Read
132
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)]
136
137 readPrec_to_P :: ReadPrec a -> (Int -> ReadP a)
138 readPrec_to_P (P f) = f
139
140 readP_to_Prec :: (Int -> ReadP a) -> ReadPrec a
141 readP_to_Prec f = P f
142
143 readPrec_to_S :: ReadPrec a -> (Int -> ReadS a)
144 readPrec_to_S (P f) n = readP_to_S (f n)
145
146 readS_to_Prec :: (Int -> ReadS a) -> ReadPrec a
147 readS_to_Prec f = P (\n -> readS_to_P (f n))