1 % ------------------------------------------------------------------------------
2 % $Id: PrelRead.lhs,v 1.19 2001/05/22 19:25:49 qrczak Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[PrelRead]{Module @PrelRead@}
9 Instances of the Read class.
12 {-# OPTIONS -fno-implicit-prelude #-}
16 import {-# SOURCE #-} PrelErr ( error )
17 import PrelEnum ( Enum(..), maxBound )
23 import PrelShow -- isAlpha etc
26 -- needed for readIO and instance Read Buffermode
27 --import PrelIOBase ( IO, userError, BufferMode(..) )
28 --import PrelException ( ioError )
31 %*********************************************************
33 \subsection{The @Read@ class}
35 %*********************************************************
37 Note: if you compile this with -DNEW_READS_REP, you'll get
38 a (simpler) ReadS representation that only allow one valid
39 parse of a string of characters, instead of a list of
42 [changing the ReadS rep has implications for the deriving
43 machinery for Read, a change that hasn't been made, so you
44 probably won't want to compile in this new rep. except
45 when in an experimental mood.]
50 type ReadS a = String -> [(a,String)]
52 type ReadS a = String -> Maybe (a,String)
56 readsPrec :: Int -> ReadS a
59 readList = readList__ reads
62 In this module we treat [(a,String)] as a monad in MonadPlus
63 But MonadPlus isn't defined yet, so we simply give local
64 declarations for mzero and guard suitable for this particular
65 type. It would also be reasonably to move MonadPlus to PrelBase
66 along with Monad and Functor, but that seems overkill for one
78 %*********************************************************
80 \subsection{Utility functions}
82 %*********************************************************
85 reads :: (Read a) => ReadS a
88 read :: (Read a) => String -> a
93 [] -> error "Prelude.read: no parse"
94 _ -> error "Prelude.read: ambiguous parse"
97 Nothing -> error "Prelude.read: no parse"
101 (x,str1) <- reads str
107 readParen :: Bool -> ReadS a -> ReadS a
108 readParen b g = if b then mandatory else optional
109 where optional r = g r ++ mandatory r
117 readList__ :: ReadS a -> ReadS [a]
120 = readParen False (\r -> do
124 (do { ("]",t) <- lex s ; return ([],t) }) ++
125 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
128 (do { ("]",t) <- lex s ; return ([],t) }) ++
129 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
134 %*********************************************************
136 \subsection{Lexical analysis}
138 %*********************************************************
140 This lexer is not completely faithful to the Haskell lexical syntax.
142 Qualified names are not handled properly
143 A `--' does not terminate a symbol
144 Octal and hexidecimal numerics are not recognized as a single token
149 lex "" = return ("","")
150 lex (c:s) | isSpace c = lex (dropWhile isSpace s)
152 (ch, '\'':t) <- lexLitChar s
154 return ('\'':ch++"'", t)
156 (str,t) <- lexString s
160 lexString ('"':s) = return ("\"",s)
162 (ch,t) <- lexStrItem s
163 (str,u) <- lexString t
167 lexStrItem ('\\':'&':s) = return ("\\&",s)
168 lexStrItem ('\\':c:s) | isSpace c = do
169 ('\\':t) <- return (dropWhile isSpace s)
171 lexStrItem s = lexLitChar s
173 lex (c:s) | isSingle c = return ([c],s)
175 (sym,t) <- return (span isSym s)
178 (nam,t) <- return (span isIdChar s)
181 {- Removed, 13/03/2000 by SDM.
182 Doesn't work, and not required by Haskell report.
186 ('o':rs) -> (isOctDigit, rs, False)
187 ('O':rs) -> (isOctDigit, rs, False)
188 ('x':rs) -> (isHexDigit, rs, False)
189 ('X':rs) -> (isHexDigit, rs, False)
190 _ -> (isDigit, s, True)
192 (ds,s) <- return (span isDigit s)
193 (fe,t) <- lexFracExp s
195 | otherwise = mzero -- bad character
197 isSingle c = c `elem` ",;()[]{}_`"
198 isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
199 isIdChar c = isAlphaNum c || c `elem` "_'"
201 lexFracExp ('.':c:cs) | isDigit c = do
202 (ds,t) <- lex0Digits cs
204 return ('.':c:ds++e,u)
205 lexFracExp s = return ("",s)
207 lexExp (e:s) | e `elem` "eE" =
210 guard (c `elem` "+-")
211 (ds,u) <- lexDecDigits t
212 return (e:c:ds,u)) ++
214 (ds,t) <- lexDecDigits s
217 lexExp s = return ("",s)
219 lexDigits :: ReadS String
220 lexDigits = lexDecDigits
222 lexDecDigits :: ReadS String
223 lexDecDigits = nonnull isDigit
225 lexOctDigits :: ReadS String
226 lexOctDigits = nonnull isOctDigit
228 lexHexDigits :: ReadS String
229 lexHexDigits = nonnull isHexDigit
232 lex0Digits :: ReadS String
233 lex0Digits s = return (span isDigit s)
235 nonnull :: (Char -> Bool) -> ReadS String
237 (cs@(_:_),t) <- return (span p s)
240 lexLitChar :: ReadS String
241 lexLitChar ('\\':s) = do
245 lexEsc (c:s) | c `elem` escChars = return ([c],s)
246 lexEsc s@(d:_) | isDigit d = checkSize 10 lexDecDigits s
247 lexEsc ('o':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
248 lexEsc ('O':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
249 lexEsc ('x':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
250 lexEsc ('X':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
251 lexEsc ('^':c:s) | c >= '@' && c <= '_' = [(['^',c],s)] -- cf. cntrl in 2.6 of H. report.
252 lexEsc s@(c:_) | isUpper c = fromAsciiLab s
255 escChars = "abfnrtv\\\"'"
257 fromAsciiLab (x:y:z:ls) | isUpper y && (isUpper z || isDigit z) &&
258 [x,y,z] `elem` asciiEscTab = return ([x,y,z], ls)
259 fromAsciiLab (x:y:ls) | isUpper y &&
260 [x,y] `elem` asciiEscTab = return ([x,y], ls)
261 fromAsciiLab _ = mzero
263 asciiEscTab = "DEL" : asciiTab
266 Check that the numerically escaped char literals are
267 within accepted boundaries.
269 Note: this allows char lits with leading zeros, i.e.,
270 \0000000000000000000000000000001.
272 checkSize base f str = do
274 if toAnInteger base num > toInteger (ord maxBound) then
278 8 -> return ('o':num, res)
279 16 -> return ('x':num, res)
280 _ -> return (num, res)
282 toAnInteger base = foldl (\ acc n -> acc*base + toInteger (digitToInt n)) 0
285 lexLitChar (c:s) = return ([c],s)
286 lexLitChar "" = mzero
288 digitToInt :: Char -> Int
290 | isDigit c = fromEnum c - fromEnum '0'
291 | c >= 'a' && c <= 'f' = fromEnum c - fromEnum 'a' + 10
292 | c >= 'A' && c <= 'F' = fromEnum c - fromEnum 'A' + 10
293 | otherwise = error ("Char.digitToInt: not a digit " ++ show c) -- sigh
296 %*********************************************************
298 \subsection{Instances of @Read@}
300 %*********************************************************
303 instance Read Char where
304 readsPrec _ = readParen False
307 (c,"\'") <- readLitChar s
310 readList = readParen False (\r -> do
314 where readl ('"':s) = return ("",s)
315 readl ('\\':'&':s) = readl s
317 (c,t) <- readLitChar s
321 instance Read Bool where
322 readsPrec _ = readParen False
325 (do { ("True", rest) <- return lr ; return (True, rest) }) ++
326 (do { ("False", rest) <- return lr ; return (False, rest) }))
329 instance Read Ordering where
330 readsPrec _ = readParen False
333 (do { ("LT", rest) <- return lr ; return (LT, rest) }) ++
334 (do { ("EQ", rest) <- return lr ; return (EQ, rest) }) ++
335 (do { ("GT", rest) <- return lr ; return (GT, rest) }))
337 instance Read a => Read (Maybe a) where
338 readsPrec _ = readParen False
341 (do { ("Nothing", rest) <- return lr ; return (Nothing, rest)}) ++
343 ("Just", rest1) <- return lr
344 (x, rest2) <- reads rest1
345 return (Just x, rest2)))
347 instance (Read a, Read b) => Read (Either a b) where
348 readsPrec _ = readParen False
352 ("Left", rest1) <- return lr
353 (x, rest2) <- reads rest1
354 return (Left x, rest2)) ++
356 ("Right", rest1) <- return lr
357 (x, rest2) <- reads rest1
358 return (Right x, rest2)))
360 instance Read Int where
361 readsPrec _ x = readSigned readDec x
363 instance Read Integer where
364 readsPrec _ x = readSigned readDec x
366 instance Read Float where
367 readsPrec _ x = readSigned readFloat x
369 instance Read Double where
370 readsPrec _ x = readSigned readFloat x
372 instance (Integral a, Read a) => Read (Ratio a) where
373 readsPrec p = readParen (p > ratio_prec)
380 instance (Read a) => Read [a] where
381 readsPrec _ = readList
383 instance Read () where
384 readsPrec _ = readParen False
390 instance (Read a, Read b) => Read (a,b) where
391 readsPrec _ = readParen False
394 (x,t) <- readsPrec 0 s
396 (y,v) <- readsPrec 0 u
400 instance (Read a, Read b, Read c) => Read (a, b, c) where
401 readsPrec _ = readParen False
404 (x,c) <- readsPrec 0 b
406 (y,e) <- readsPrec 0 d
408 (z,g) <- readsPrec 0 f
412 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
413 readsPrec _ = readParen False
416 (w,c) <- readsPrec 0 b
418 (x,e) <- readsPrec 0 d
420 (y,g) <- readsPrec 0 f
422 (z,h) <- readsPrec 0 h
424 return ((w,x,y,z), i))
426 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
427 readsPrec _ = readParen False
430 (v,c) <- readsPrec 0 b
432 (w,e) <- readsPrec 0 d
434 (x,g) <- readsPrec 0 f
436 (y,i) <- readsPrec 0 h
438 (z,k) <- readsPrec 0 j
440 return ((v,w,x,y,z), l))
444 %*********************************************************
446 \subsection{Reading characters}
448 %*********************************************************
451 readLitChar :: ReadS Char
453 readLitChar [] = mzero
454 readLitChar ('\\':s) = readEsc s
456 readEsc ('a':s) = return ('\a',s)
457 readEsc ('b':s) = return ('\b',s)
458 readEsc ('f':s) = return ('\f',s)
459 readEsc ('n':s) = return ('\n',s)
460 readEsc ('r':s) = return ('\r',s)
461 readEsc ('t':s) = return ('\t',s)
462 readEsc ('v':s) = return ('\v',s)
463 readEsc ('\\':s) = return ('\\',s)
464 readEsc ('"':s) = return ('"',s)
465 readEsc ('\'':s) = return ('\'',s)
466 readEsc ('^':c:s) | c >= '@' && c <= '_'
467 = return (chr (ord c - ord '@'), s)
468 readEsc s@(d:_) | isDigit d
479 readEsc s@(c:_) | isUpper c
480 = let table = ('\DEL', "DEL") : zip ['\NUL'..] asciiTab
481 in case [(c,s') | (c, mne) <- table,
482 ([],s') <- [match mne s]]
483 of (pr:_) -> return pr
487 readLitChar (c:s) = return (c,s)
489 match :: (Eq a) => [a] -> [a] -> ([a],[a])
490 match (x:xs) (y:ys) | x == y = match xs ys
491 match xs ys = (xs,ys)
496 %*********************************************************
498 \subsection{Reading numbers}
500 %*********************************************************
502 Note: reading numbers at bases different than 10, does not
503 include lexing common prefixes such as '0x' or '0o' etc.
506 {-# SPECIALISE readDec ::
509 readDec :: (Integral a) => ReadS a
510 readDec = readInt 10 isDigit (\d -> ord d - ord '0')
512 {-# SPECIALISE readOct ::
515 readOct :: (Integral a) => ReadS a
516 readOct = readInt 8 isOctDigit (\d -> ord d - ord '0')
518 {-# SPECIALISE readHex ::
521 readHex :: (Integral a) => ReadS a
522 readHex = readInt 16 isHexDigit hex
523 where hex d = ord d - (if isDigit d then ord '0'
524 else ord (if isUpper d then 'A' else 'a') - 10)
526 readInt :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
527 readInt radix isDig digToInt s = do
528 (ds,r) <- nonnull isDig s
529 return (foldl1 (\n d -> n * radix + d)
530 (map (fromInteger . toInteger . digToInt) ds), r)
532 {-# SPECIALISE readSigned ::
533 ReadS Int -> ReadS Int,
534 ReadS Integer -> ReadS Integer,
535 ReadS Double -> ReadS Double #-}
536 readSigned :: (Real a) => ReadS a -> ReadS a
537 readSigned readPos = readParen False read'
538 where read' r = read'' r ++
545 (n,"") <- readPos str
549 The functions readFloat below uses rational arithmetic
550 to ensure correct conversion between the floating-point radix and
551 decimal. It is often possible to use a higher-precision floating-
552 point type to obtain the same results.
555 {-# SPECIALISE readFloat ::
558 readFloat :: (RealFloat a) => ReadS a
560 (x,t) <- readRational r
561 return (fromRational x,t)
563 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
569 return ((n%1)*10^^(k-d), t )) ++
574 ("Infinity",t) <- lex r
578 (ds,s) <- lexDecDigits r
579 (ds',t) <- lexDotDigits s
580 return (read (ds++ds'), length ds', t)
582 readExp (e:s) | e `elem` "eE" = readExp' s
583 readExp s = return (0,s)
585 readExp' ('+':s) = readDec s
586 readExp' ('-':s) = do
589 readExp' s = readDec s
591 lexDotDigits ('.':s) = lex0Digits s
592 lexDotDigits s = return ("",s)
594 readRational__ :: String -> Rational -- we export this one (non-std)
595 -- NB: *does* handle a leading "-"
598 '-' : xs -> - (read_me xs)
602 = case (do { (x,t) <- readRational s ; ("","") <- lex t ; return x }) of
603 #ifndef NEW_READS_REP
605 [] -> error ("readRational__: no parse:" ++ top_s)
606 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
609 Nothing -> error ("readRational__: no parse:" ++ top_s)