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 -- | A parser for a type @a@, represented as a function that takes a
124 -- 'String' and returns a list of possible parses @(a,'String')@ pairs.
125 type ReadS a = String -> [(a,String)]
127 ------------------------------------------------------------------------
131 readsPrec :: Int -> ReadS a
132 readList :: ReadS [a]
133 readPrec :: ReadPrec a
134 readListPrec :: ReadPrec [a]
136 -- default definitions
137 readsPrec = readPrec_to_S readPrec
138 readList = readPrec_to_S (list readPrec) 0
139 readPrec = readS_to_Prec readsPrec
140 readListPrec = readS_to_Prec (\_ -> readList)
142 readListDefault :: Read a => ReadS [a]
143 readListDefault = readPrec_to_S readListPrec 0
145 readListPrecDefault :: Read a => ReadPrec [a]
146 readListPrecDefault = list readPrec
148 ------------------------------------------------------------------------
151 reads :: Read a => ReadS a
152 reads = readsPrec minPrec
154 readp :: Read a => ReadP a
155 readp = readPrec_to_P readPrec minPrec
157 readEither :: Read a => String -> Either String a
159 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
161 [] -> Left "Prelude.read: no parse"
162 _ -> Left "Prelude.read: ambiguous parse"
169 read :: Read a => String -> a
170 read s = either error id (readEither s)
172 ------------------------------------------------------------------------
175 lex :: ReadS String -- As defined by H98
176 lex "" = [("","")] -- ugly hack
177 lex s = readP_to_S (do { lexeme <- L.lex ;
178 return (show lexeme) }) s
180 lexLitChar :: ReadS String -- As defined by H98
181 lexLitChar = readP_to_S (do { lexeme <- L.lexLitChar ;
182 return (show lexeme) })
184 readLitChar :: ReadS Char -- As defined by H98
185 readLitChar = readP_to_S (do { Char c <- L.lexLitChar ;
188 lexDigits :: ReadS String
189 lexDigits = readP_to_S (P.munch1 isDigit)
191 ------------------------------------------------------------------------
194 lexP :: ReadPrec Lexeme
197 paren :: ReadPrec a -> ReadPrec a
198 -- (paren p) parses (P0)
199 -- where p parses P0 in precedence context zero
201 do Single '(' <- lexP
206 parens :: ReadPrec a -> ReadPrec a
207 -- (parens p) parses P, (P0), ((P0)), etc,
208 -- where p parses P in the current precedence context
209 -- parses P0 in precedence context zero
212 optional = p +++ mandatory
213 mandatory = paren optional
215 list :: ReadPrec a -> ReadPrec [a]
218 ( do Single '[' <- lexP
219 (listRest False +++ listNext)
226 ',' | started -> listNext
234 choose :: [(String, ReadPrec a)] -> ReadPrec a
235 -- Parse the specified lexeme and continue as specified
236 -- Esp useful for nullary constructors
237 choose sps = foldr ((+++) . try_one) pfail sps
239 try_one (s,p) = do { Ident s' <- lexP ;
240 if s == s' then p else pfail }
244 %*********************************************************
246 \subsection{Simple instances of Read}
248 %*********************************************************
251 instance Read Char where
260 ( do String s <- lexP -- Looks for "foo"
263 readListPrecDefault -- Looks for ['f','o','o']
264 ) -- (more generous than H98 spec)
266 readList = readListDefault
268 instance Read Bool where
273 "True" -> return True
274 "False" -> return False
278 readListPrec = readListPrecDefault
279 readList = readListDefault
281 instance Read Ordering where
292 readListPrec = readListPrecDefault
293 readList = readListDefault
297 %*********************************************************
299 \subsection{Structure instances of Read: Maybe, List etc}
301 %*********************************************************
303 For structured instances of Read we start using the precedences. The
304 idea is then that 'parens (prec k p)' will fail immediately when trying
305 to parse it in a context with a higher precedence level than k. But if
306 there is one parenthesis parsed, then the required precedence level
307 drops to 0 again, and parsing inside p may succeed.
309 'appPrec' is just the precedence level of function application (maybe
310 it should be called 'appPrec' instead). So, if we are parsing
311 function application, we'd better require the precedence level to be
312 at least 'appPrec'. Otherwise, we have to put parentheses around it.
314 'step' is used to increase the precedence levels inside a
315 parser, and can be used to express left- or right- associativity. For
316 example, % is defined to be left associative, so we only increase
317 precedence on the right hand side.
319 Note how step is used in for example the Maybe parser to increase the
320 precedence beyond appPrec, so that basically only literals and
321 parenthesis-like objects such as (...) and [...] can be an argument to
325 instance Read a => Read (Maybe a) where
329 ( do Ident "Nothing" <- lexP
332 do Ident "Just" <- lexP
338 readListPrec = readListPrecDefault
339 readList = readListDefault
341 instance (Read a, Read b) => Read (Either a b) where
345 ( do Ident "Left" <- lexP
349 do Ident "Right" <- lexP
355 readListPrec = readListPrecDefault
356 readList = readListDefault
358 instance Read a => Read [a] where
359 readPrec = readListPrec
360 readListPrec = readListPrecDefault
361 readList = readListDefault
363 instance Read Lexeme where
365 readListPrec = readListPrecDefault
366 readList = readListDefault
370 %*********************************************************
372 \subsection{Numeric instances of Read}
374 %*********************************************************
377 readNumber :: Num a => (Number -> Maybe a) -> ReadPrec a
378 -- Read a signed number
383 Symbol "-" -> do n <- readNumber convert
386 Number y -> case convert y of
393 readIEEENumber :: (RealFloat a) => (Number -> Maybe a) -> ReadPrec a
394 -- Read a Float/Double.
395 readIEEENumber convert =
399 Ident "NaN" -> return (0/0)
400 Ident "Infinity" -> return (1/0)
401 Symbol "-" -> do n <- readIEEENumber convert
404 Number y -> case convert y of
411 instance Read Int where
412 readPrec = readNumber numberToInt
413 readListPrec = readListPrecDefault
414 readList = readListDefault
416 instance Read Integer where
417 readPrec = readNumber numberToInteger
418 readListPrec = readListPrecDefault
419 readList = readListDefault
421 instance Read Float where
422 readPrec = readIEEENumber numberToFloat
423 readListPrec = readListPrecDefault
424 readList = readListDefault
426 instance Read Double where
427 readPrec = readIEEENumber numberToDouble
428 readListPrec = readListPrecDefault
429 readList = readListDefault
431 instance (Integral a, Read a) => Read (Ratio a) where
435 ( do x <- step readPrec
442 readListPrec = readListPrecDefault
443 readList = readListDefault
447 %*********************************************************
449 \subsection{Tuple instances of Read}
451 %*********************************************************
454 instance Read () where
462 readListPrec = readListPrecDefault
463 readList = readListDefault
465 instance (Read a, Read b) => Read (a,b) where
476 readListPrec = readListPrecDefault
477 readList = readListDefault
480 instance (Read a, Read b, Read c) => Read (a, b, c) where
493 readListPrec = readListPrecDefault
494 readList = readListDefault
496 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
511 readListPrec = readListPrecDefault
512 readList = readListDefault
514 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
531 readListPrec = readListPrecDefault
532 readList = readListDefault