2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The FFI Task Force, 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The 'Read' class and instances for basic data types.
15 -----------------------------------------------------------------------------
21 , ReadS -- :: *; = String -> [(a,String)]
24 , reads -- :: Read a => ReadS a
25 , readp -- :: Read a => ReadP a
26 , readEither -- :: Read a => String -> Either String a
27 , read -- :: Read a => String -> a
30 , lex -- :: ReadS String
31 , lexLitChar -- :: ReadS String
32 , readLitChar -- :: ReadS Char
33 , lexDigits -- :: ReadS String
36 , lexP -- :: ReadPrec Lexeme
37 , paren -- :: ReadPrec a -> ReadPrec a
38 , parens -- :: ReadPrec a -> ReadPrec a
39 , list -- :: ReadPrec a -> ReadPrec [a]
40 , choose -- :: [(String, ReadPrec a)] -> ReadPrec a
48 import qualified Text.ParserCombinators.ReadP as P
50 import Text.ParserCombinators.ReadP
56 import qualified Text.Read.Lex as L
67 import Text.ParserCombinators.ReadPrec
72 import {-# SOURCE #-} GHC.Err ( error )
77 import GHC.Show -- isAlpha etc
80 ratioPrec = 7 -- Precedence of ':%' constructor
81 appPrec = 10 -- Precedence of applictaion
83 -------------------------------------------------------
84 TEMPORARY UNTIL I DO DERIVED READ
87 readParen :: Bool -> ReadS a -> ReadS a
88 readParen b g = if b then mandatory else optional
89 where optional r = g r ++ mandatory r
97 readList__ :: ReadS a -> ReadS [a]
100 = readParen False (\r -> do
104 (do { ("]",t) <- lex s ; return ([],t) }) ++
105 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
108 (do { ("]",t) <- lex s ; return ([],t) }) ++
109 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
113 %*********************************************************
115 \subsection{The @Read@ class and @ReadS@ type}
117 %*********************************************************
120 ------------------------------------------------------------------------
123 type ReadS a = String -> [(a,String)]
125 ------------------------------------------------------------------------
129 readsPrec :: Int -> ReadS a
130 readList :: ReadS [a]
131 readPrec :: ReadPrec a
132 readListPrec :: ReadPrec [a]
134 -- default definitions
135 readsPrec = readPrec_to_S readPrec
136 readList = readPrec_to_S (list readPrec) 0
137 readPrec = readS_to_Prec readsPrec
138 readListPrec = readS_to_Prec (\_ -> readList)
140 readListDefault :: Read a => ReadS [a]
141 readListDefault = readPrec_to_S readListPrec 0
143 readListPrecDefault :: Read a => ReadPrec [a]
144 readListPrecDefault = list readPrec
146 ------------------------------------------------------------------------
149 reads :: Read a => ReadS a
150 reads = readsPrec minPrec
152 readp :: Read a => ReadP a
153 readp = readPrec_to_P readPrec minPrec
155 readEither :: Read a => String -> Either String a
157 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
159 [] -> Left "Prelude.read: no parse"
160 _ -> Left "Prelude.read: ambiguous parse"
167 read :: Read a => String -> a
168 read s = either error id (readEither s)
170 ------------------------------------------------------------------------
173 lex :: ReadS String -- As defined by H98
174 lex "" = [("","")] -- ugly hack
175 lex s = readP_to_S (do { lexeme <- L.lex ;
176 return (show lexeme) }) s
178 lexLitChar :: ReadS String -- As defined by H98
179 lexLitChar = readP_to_S (do { lexeme <- L.lexLitChar ;
180 return (show lexeme) })
182 readLitChar :: ReadS Char -- As defined by H98
183 readLitChar = readP_to_S (do { Char c <- L.lexLitChar ;
186 lexDigits :: ReadS String
187 lexDigits = readP_to_S (P.munch1 isDigit)
189 ------------------------------------------------------------------------
192 lexP :: ReadPrec Lexeme
195 paren :: ReadPrec a -> ReadPrec a
196 -- (paren p) parses (P0)
197 -- where p parses P0 in precedence context zero
199 do Single '(' <- lexP
204 parens :: ReadPrec a -> ReadPrec a
205 -- (parens p) parses P, (P0), ((P0)), etc,
206 -- where p parses P in the current precedence context
207 -- parses P0 in precedence context zero
210 optional = p +++ mandatory
211 mandatory = paren optional
213 list :: ReadPrec a -> ReadPrec [a]
216 ( do Single '[' <- lexP
217 (listRest False +++ listNext)
224 ',' | started -> listNext
232 choose :: [(String, ReadPrec a)] -> ReadPrec a
233 -- Parse the specified lexeme and continue as specified
234 -- Esp useful for nullary constructors
235 choose sps = foldr ((+++) . try_one) pfail sps
237 try_one (s,p) = do { Ident s' <- lexP ;
238 if s == s' then p else pfail }
242 %*********************************************************
244 \subsection{Simple instances of Read}
246 %*********************************************************
249 instance Read Char where
258 ( do String s <- lexP -- Looks for "foo"
261 readListPrecDefault -- Looks for ['f','o','o']
262 ) -- (more generous than H98 spec)
264 readList = readListDefault
266 instance Read Bool where
271 "True" -> return True
272 "False" -> return False
276 readListPrec = readListPrecDefault
277 readList = readListDefault
279 instance Read Ordering where
290 readListPrec = readListPrecDefault
291 readList = readListDefault
295 %*********************************************************
297 \subsection{Structure instances of Read: Maybe, List etc}
299 %*********************************************************
301 For structured instances of Read we start using the precedences. The
302 idea is then that 'parens (prec k p)' will fail immediately when trying
303 to parse it in a context with a higher precedence level than k. But if
304 there is one parenthesis parsed, then the required precedence level
305 drops to 0 again, and parsing inside p may succeed.
307 'appPrec' is just the precedence level of function application (maybe
308 it should be called 'appPrec' instead). So, if we are parsing
309 function application, we'd better require the precedence level to be
310 at least 'appPrec'. Otherwise, we have to put parentheses around it.
312 'step' is used to increase the precedence levels inside a
313 parser, and can be used to express left- or right- associativity. For
314 example, % is defined to be left associative, so we only increase
315 precedence on the right hand side.
317 Note how step is used in for example the Maybe parser to increase the
318 precedence beyond appPrec, so that basically only literals and
319 parenthesis-like objects such as (...) and [...] can be an argument to
323 instance Read a => Read (Maybe a) where
327 ( do Ident "Nothing" <- lexP
330 do Ident "Just" <- lexP
336 readListPrec = readListPrecDefault
337 readList = readListDefault
339 instance (Read a, Read b) => Read (Either a b) where
343 ( do Ident "Left" <- lexP
347 do Ident "Right" <- lexP
353 readListPrec = readListPrecDefault
354 readList = readListDefault
356 instance Read a => Read [a] where
357 readPrec = readListPrec
358 readListPrec = readListPrecDefault
359 readList = readListDefault
361 instance Read Lexeme where
363 readListPrec = readListPrecDefault
364 readList = readListDefault
368 %*********************************************************
370 \subsection{Numeric instances of Read}
372 %*********************************************************
375 readNumber :: Num a => (Number -> Maybe a) -> ReadPrec a
376 -- Read a signed number
381 Symbol "-" -> do n <- readNumber convert
384 Number y -> case convert y of
391 readIEEENumber :: (RealFloat a) => (Number -> Maybe a) -> ReadPrec a
392 -- Read a Float/Double.
393 readIEEENumber convert =
397 Ident "NaN" -> return (0/0)
398 Ident "Infinity" -> return (1/0)
399 Symbol "-" -> do n <- readIEEENumber convert
402 Number y -> case convert y of
409 instance Read Int where
410 readPrec = readNumber numberToInt
411 readListPrec = readListPrecDefault
412 readList = readListDefault
414 instance Read Integer where
415 readPrec = readNumber numberToInteger
416 readListPrec = readListPrecDefault
417 readList = readListDefault
419 instance Read Float where
420 readPrec = readIEEENumber numberToFloat
421 readListPrec = readListPrecDefault
422 readList = readListDefault
424 instance Read Double where
425 readPrec = readIEEENumber numberToDouble
426 readListPrec = readListPrecDefault
427 readList = readListDefault
429 instance (Integral a, Read a) => Read (Ratio a) where
433 ( do x <- step readPrec
440 readListPrec = readListPrecDefault
441 readList = readListDefault
445 %*********************************************************
447 \subsection{Tuple instances of Read}
449 %*********************************************************
452 instance Read () where
460 readListPrec = readListPrecDefault
461 readList = readListDefault
463 instance (Read a, Read b) => Read (a,b) where
474 readListPrec = readListPrecDefault
475 readList = readListDefault
478 instance (Read a, Read b, Read c) => Read (a, b, c) where
491 readListPrec = readListPrecDefault
492 readList = readListDefault
494 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
509 readListPrec = readListPrecDefault
510 readList = readListDefault
512 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
529 readListPrec = readListPrecDefault
530 readList = readListDefault