1 % ------------------------------------------------------------------------------
2 % $Id: Read.lhs,v 1.6 2002/04/13 14:59:06 panne 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 "" = [("","")] -- ugly hack
172 lex s = readP_to_S (do { lexeme <- L.lex ;
173 return (show lexeme) }) s
175 lexLitChar :: ReadS String -- As defined by H98
176 lexLitChar = readP_to_S (do { lexeme <- L.lexLitChar ;
177 return (show lexeme) })
179 readLitChar :: ReadS Char -- As defined by H98
180 readLitChar = readP_to_S (do { Char c <- L.lexLitChar ;
183 lexDigits :: ReadS String
184 lexDigits = readP_to_S (P.munch1 isDigit)
186 ------------------------------------------------------------------------
189 lexP :: ReadPrec Lexeme
192 paren :: ReadPrec a -> ReadPrec a
193 -- (paren p) parses (P0)
194 -- where p parses P0 in precedence context zero
196 do Single '(' <- lexP
201 parens :: ReadPrec a -> ReadPrec a
202 -- (parens p) parses P, (P0), ((P0)), etc,
203 -- where p parses P in the current precedence context
204 -- parses P0 in precedence context zero
207 optional = p +++ mandatory
208 mandatory = paren optional
210 list :: ReadPrec a -> ReadPrec [a]
213 ( do Single '[' <- lexP
214 (listRest False +++ listNext)
221 ',' | started -> listNext
229 choose :: [(String, ReadPrec a)] -> ReadPrec a
230 -- Parse the specified lexeme and continue as specified
231 -- Esp useful for nullary constructors
232 choose sps = foldr ((+++) . try_one) pfail sps
234 try_one (s,p) = do { Ident s' <- lexP ;
235 if s == s' then p else pfail }
239 %*********************************************************
241 \subsection{Simple instances of Read}
243 %*********************************************************
246 instance Read Char where
255 ( do String s <- lexP -- Looks for "foo"
258 readListPrecDefault -- Looks for ['f','o','o']
259 ) -- (more generous than H98 spec)
261 readList = readListDefault
263 instance Read Bool where
268 "True" -> return True
269 "False" -> return False
273 readListPrec = readListPrecDefault
274 readList = readListDefault
276 instance Read Ordering where
287 readListPrec = readListPrecDefault
288 readList = readListDefault
292 %*********************************************************
294 \subsection{Structure instances of Read: Maybe, List etc}
296 %*********************************************************
298 For structured instances of Read we start using the precedences. The
299 idea is then that 'parens (prec k p)' will fail immediately when trying
300 to parse it in a context with a higher precedence level than k. But if
301 there is one parenthesis parsed, then the required precedence level
302 drops to 0 again, and parsing inside p may succeed.
304 'appPrec' is just the precedence level of function application (maybe
305 it should be called 'appPrec' instead). So, if we are parsing
306 function application, we'd better require the precedence level to be
307 at least 'appPrec'. Otherwise, we have to put parentheses around it.
309 'step' is used to increase the precedence levels inside a
310 parser, and can be used to express left- or right- associativity. For
311 example, % is defined to be left associative, so we only increase
312 precedence on the right hand side.
314 Note how step is used in for example the Maybe parser to increase the
315 precedence beyond appPrec, so that basically only literals and
316 parenthesis-like objects such as (...) and [...] can be an argument to
320 instance Read a => Read (Maybe a) where
324 ( do Ident "Nothing" <- lexP
327 do Ident "Just" <- lexP
333 readListPrec = readListPrecDefault
334 readList = readListDefault
336 instance (Read a, Read b) => Read (Either a b) where
340 ( do Ident "Left" <- lexP
344 do Ident "Right" <- lexP
350 readListPrec = readListPrecDefault
351 readList = readListDefault
353 instance Read a => Read [a] where
354 readPrec = readListPrec
355 readListPrec = readListPrecDefault
356 readList = readListDefault
358 instance Read Lexeme where
360 readListPrec = readListPrecDefault
361 readList = readListDefault
365 %*********************************************************
367 \subsection{Numeric instances of Read}
369 %*********************************************************
372 readNumber :: Num a => (Number -> Maybe a) -> ReadPrec a
373 -- Read a signed number
378 Symbol "-" -> do n <- readNumber convert
381 Number y -> case convert y of
388 readIEEENumber :: (RealFloat a) => (Number -> Maybe a) -> ReadPrec a
389 -- Read a Float/Double.
390 readIEEENumber convert =
394 Ident "NaN" -> return (0/0)
395 Ident "Infinity" -> return (1/0)
396 Symbol "-" -> do n <- readIEEENumber convert
399 Number y -> case convert y of
406 instance Read Int where
407 readPrec = readNumber numberToInt
408 readListPrec = readListPrecDefault
409 readList = readListDefault
411 instance Read Integer where
412 readPrec = readNumber numberToInteger
413 readListPrec = readListPrecDefault
414 readList = readListDefault
416 instance Read Float where
417 readPrec = readIEEENumber numberToFloat
418 readListPrec = readListPrecDefault
419 readList = readListDefault
421 instance Read Double where
422 readPrec = readIEEENumber numberToDouble
423 readListPrec = readListPrecDefault
424 readList = readListDefault
426 instance (Integral a, Read a) => Read (Ratio a) where
430 ( do x <- step readPrec
437 readListPrec = readListPrecDefault
438 readList = readListDefault
442 %*********************************************************
444 \subsection{Tuple instances of Read}
446 %*********************************************************
449 instance Read () where
457 readListPrec = readListPrecDefault
458 readList = readListDefault
460 instance (Read a, Read b) => Read (a,b) where
471 readListPrec = readListPrecDefault
472 readList = readListDefault
475 instance (Read a, Read b, Read c) => Read (a, b, c) where
488 readListPrec = readListPrecDefault
489 readList = readListDefault
491 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
506 readListPrec = readListPrecDefault
507 readList = readListDefault
509 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
526 readListPrec = readListPrecDefault
527 readList = readListDefault