1 % ------------------------------------------------------------------------------
2 % $Id: Read.lhs,v 1.4 2002/04/11 12:03:44 simonpj Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[GHC.Read]{Module @GHC.Read@}
9 Instances of the Read class.
12 {-# OPTIONS -fno-implicit-prelude #-}
18 , ReadS -- :: *; = String -> [(a,String)]
21 , reads -- :: Read a => ReadS a
22 , readp -- :: Read a => ReadP a
23 , readEither -- :: Read a => String -> Either String a
24 , read -- :: Read a => String -> a
27 , lex -- :: ReadS String
28 , lexLitChar -- :: ReadS String
29 , readLitChar -- :: ReadS Char
30 , lexDigits -- :: ReadS String
33 , lexP -- :: ReadPrec Lexeme
34 , paren -- :: ReadPrec a -> ReadPrec a
35 , parens -- :: ReadPrec a -> ReadPrec a
36 , list -- :: ReadPrec a -> ReadPrec [a]
37 , choose -- :: [(String, ReadPrec a)] -> ReadPrec a
45 import qualified Text.ParserCombinators.ReadP as P
47 import Text.ParserCombinators.ReadP
53 import qualified Text.Read.Lex as L
64 import Text.ParserCombinators.ReadPrec
69 import {-# SOURCE #-} GHC.Err ( error )
74 import GHC.Show -- isAlpha etc
77 ratioPrec = 7 -- Precedence of ':%' constructor
78 appPrec = 10 -- Precedence of applictaion
80 -------------------------------------------------------
81 TEMPORARY UNTIL I DO DERIVED READ
84 readParen :: Bool -> ReadS a -> ReadS a
85 readParen b g = if b then mandatory else optional
86 where optional r = g r ++ mandatory r
94 readList__ :: ReadS a -> ReadS [a]
97 = readParen False (\r -> do
101 (do { ("]",t) <- lex s ; return ([],t) }) ++
102 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
105 (do { ("]",t) <- lex s ; return ([],t) }) ++
106 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
110 %*********************************************************
112 \subsection{The @Read@ class and @ReadS@ type}
114 %*********************************************************
117 ------------------------------------------------------------------------
120 type ReadS a = String -> [(a,String)]
122 ------------------------------------------------------------------------
126 readsPrec :: Int -> ReadS a
127 readList :: ReadS [a]
128 readPrec :: ReadPrec a
129 readListPrec :: ReadPrec [a]
131 -- default definitions
132 readsPrec = readPrec_to_S readPrec
133 readList = readPrec_to_S (list readPrec) 0
134 readPrec = readS_to_Prec readsPrec
135 readListPrec = readS_to_Prec (\_ -> readList)
137 readListDefault :: Read a => ReadS [a]
138 readListDefault = readPrec_to_S readListPrec 0
140 readListPrecDefault :: Read a => ReadPrec [a]
141 readListPrecDefault = list readPrec
143 ------------------------------------------------------------------------
146 reads :: Read a => ReadS a
147 reads = readsPrec minPrec
149 readp :: Read a => ReadP a
150 readp = readPrec_to_P readPrec minPrec
152 readEither :: Read a => String -> Either String a
154 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
156 [] -> Left "Prelude.read: no parse"
157 _ -> Left "Prelude.read: ambiguous parse"
164 read :: Read a => String -> a
165 read s = either error id (readEither s)
167 ------------------------------------------------------------------------
170 lex :: ReadS String -- As defined by H98
171 lex = readP_to_S (do { lexeme <- L.lex ;
172 return (show lexeme) })
174 lexLitChar :: ReadS String -- As defined by H98
175 lexLitChar = readP_to_S (do { lexeme <- L.lexLitChar ;
176 return (show lexeme) })
178 readLitChar :: ReadS Char -- As defined by H98
179 readLitChar = readP_to_S (do { Char c <- L.lexLitChar ;
182 lexDigits :: ReadS String
183 lexDigits = readP_to_S (P.munch1 isDigit)
185 ------------------------------------------------------------------------
188 lexP :: ReadPrec Lexeme
191 paren :: ReadPrec a -> ReadPrec a
192 -- (paren p) parses (P0)
193 -- where p parses P0 in precedence context zero
195 do Single '(' <- lexP
200 parens :: ReadPrec a -> ReadPrec a
201 -- (parens p) parses P, (P0), ((P0)), etc,
202 -- where p parses P in the current precedence context
203 -- parses P0 in precedence context zero
206 optional = p +++ mandatory
207 mandatory = paren optional
209 list :: ReadPrec a -> ReadPrec [a]
212 ( do Single '[' <- lexP
213 (listRest False +++ listNext)
220 ',' | started -> listNext
228 choose :: [(String, ReadPrec a)] -> ReadPrec a
229 -- Parse the specified lexeme and continue as specified
230 -- Esp useful for nullary constructors
231 choose sps = foldr ((+++) . try_one) pfail sps
233 try_one (s,p) = do { Ident s' <- lexP ;
234 if s == s' then p else pfail }
238 %*********************************************************
240 \subsection{Simple instances of Read}
242 %*********************************************************
245 instance Read Char where
254 ( do String s <- lexP -- Looks for "foo"
257 readListPrecDefault -- Looks for ['f','o','o']
258 ) -- (more generous than H98 spec)
260 readList = readListDefault
262 instance Read Bool where
267 "True" -> return True
268 "False" -> return False
272 readListPrec = readListPrecDefault
273 readList = readListDefault
275 instance Read Ordering where
286 readListPrec = readListPrecDefault
287 readList = readListDefault
291 %*********************************************************
293 \subsection{Structure instances of Read: Maybe, List etc}
295 %*********************************************************
297 For structured instances of Read we start using the precedences. The
298 idea is then that 'parens (prec k p)' will fail immediately when trying
299 to parse it in a context with a higher precedence level than k. But if
300 there is one parenthesis parsed, then the required precedence level
301 drops to 0 again, and parsing inside p may succeed.
303 'appPrec' is just the precedence level of function application (maybe
304 it should be called 'appPrec' instead). So, if we are parsing
305 function application, we'd better require the precedence level to be
306 at least 'appPrec'. Otherwise, we have to put parentheses around it.
308 'step' is used to increase the precedence levels inside a
309 parser, and can be used to express left- or right- associativity. For
310 example, % is defined to be left associative, so we only increase
311 precedence on the right hand side.
313 Note how step is used in for example the Maybe parser to increase the
314 precedence beyond appPrec, so that basically only literals and
315 parenthesis-like objects such as (...) and [...] can be an argument to
319 instance Read a => Read (Maybe a) where
323 ( do Ident "Nothing" <- lexP
326 do Ident "Just" <- lexP
332 readListPrec = readListPrecDefault
333 readList = readListDefault
335 instance (Read a, Read b) => Read (Either a b) where
339 ( do Ident "Left" <- lexP
343 do Ident "Right" <- lexP
349 readListPrec = readListPrecDefault
350 readList = readListDefault
352 instance Read a => Read [a] where
353 readPrec = readListPrec
354 readListPrec = readListPrecDefault
355 readList = readListDefault
357 instance Read Lexeme where
359 readListPrec = readListPrecDefault
360 readList = readListDefault
364 %*********************************************************
366 \subsection{Numeric instances of Read}
368 %*********************************************************
371 readNumber :: Num a => (Number -> Maybe a) -> ReadPrec a
372 -- Read a signed number
377 Symbol "-" -> do n <- readNumber convert
380 Number y -> case convert y of
387 instance Read Int where
388 readPrec = readNumber numberToInt
389 readListPrec = readListPrecDefault
390 readList = readListDefault
392 instance Read Integer where
393 readPrec = readNumber numberToInteger
394 readListPrec = readListPrecDefault
395 readList = readListDefault
397 instance Read Float where
398 readPrec = readNumber numberToFloat
399 readListPrec = readListPrecDefault
400 readList = readListDefault
402 instance Read Double where
403 readPrec = readNumber numberToDouble
404 readListPrec = readListPrecDefault
405 readList = readListDefault
407 instance (Integral a, Read a) => Read (Ratio a) where
411 ( do x <- step readPrec
418 readListPrec = readListPrecDefault
419 readList = readListDefault
423 %*********************************************************
425 \subsection{Tuple instances of Read}
427 %*********************************************************
430 instance Read () where
438 readListPrec = readListPrecDefault
439 readList = readListDefault
441 instance (Read a, Read b) => Read (a,b) where
452 readListPrec = readListPrecDefault
453 readList = readListDefault
456 instance (Read a, Read b, Read c) => Read (a, b, c) where
469 readListPrec = readListPrecDefault
470 readList = readListDefault
472 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
487 readListPrec = readListPrecDefault
488 readList = readListDefault
490 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
507 readListPrec = readListPrecDefault
508 readList = readListDefault