1 % ------------------------------------------------------------------------------
2 % $Id: PrelRead.lhs,v 1.16 2000/08/07 23:37:23 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 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
105 -- raises an exception instead of an error
106 readIO :: Read a => String -> IO a
107 readIO s = case (do { (x,t) <- reads s ; ("","") <- lex t ; return x }) of
108 #ifndef NEW_READS_REP
110 [] -> ioError (userError "Prelude.readIO: no parse")
111 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
114 Nothing -> ioError (userError "Prelude.readIO: no parse")
120 readParen :: Bool -> ReadS a -> ReadS a
121 readParen b g = if b then mandatory else optional
122 where optional r = g r ++ mandatory r
130 readList__ :: ReadS a -> ReadS [a]
133 = readParen False (\r -> do
137 (do { ("]",t) <- lex s ; return ([],t) }) ++
138 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
141 (do { ("]",t) <- lex s ; return ([],t) }) ++
142 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
147 %*********************************************************
149 \subsection{Lexical analysis}
151 %*********************************************************
153 This lexer is not completely faithful to the Haskell lexical syntax.
155 Qualified names are not handled properly
156 A `--' does not terminate a symbol
157 Octal and hexidecimal numerics are not recognized as a single token
162 lex "" = return ("","")
163 lex (c:s) | isSpace c = lex (dropWhile isSpace s)
165 (ch, '\'':t) <- lexLitChar s
167 return ('\'':ch++"'", t)
169 (str,t) <- lexString s
173 lexString ('"':s) = return ("\"",s)
175 (ch,t) <- lexStrItem s
176 (str,u) <- lexString t
180 lexStrItem ('\\':'&':s) = return ("\\&",s)
181 lexStrItem ('\\':c:s) | isSpace c = do
182 ('\\':t) <- return (dropWhile isSpace s)
184 lexStrItem s = lexLitChar s
186 lex (c:s) | isSingle c = return ([c],s)
188 (sym,t) <- return (span isSym s)
191 (nam,t) <- return (span isIdChar s)
194 {- Removed, 13/03/2000 by SDM.
195 Doesn't work, and not required by Haskell report.
199 ('o':rs) -> (isOctDigit, rs, False)
200 ('O':rs) -> (isOctDigit, rs, False)
201 ('x':rs) -> (isHexDigit, rs, False)
202 ('X':rs) -> (isHexDigit, rs, False)
203 _ -> (isDigit, s, True)
205 (ds,s) <- return (span isDigit s)
206 (fe,t) <- lexFracExp s
208 | otherwise = mzero -- bad character
210 isSingle c = c `elem` ",;()[]{}_`"
211 isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
212 isIdChar c = isAlphaNum c || c `elem` "_'"
214 lexFracExp ('.':c:cs) | isDigit c = do
215 (ds,t) <- lex0Digits cs
217 return ('.':c:ds++e,u)
218 lexFracExp s = return ("",s)
220 lexExp (e:s) | e `elem` "eE" =
223 guard (c `elem` "+-")
224 (ds,u) <- lexDecDigits t
225 return (e:c:ds,u)) ++
227 (ds,t) <- lexDecDigits s
230 lexExp s = return ("",s)
232 lexDigits :: ReadS String
233 lexDigits = lexDecDigits
235 lexDecDigits :: ReadS String
236 lexDecDigits = nonnull isDigit
238 lexOctDigits :: ReadS String
239 lexOctDigits = nonnull isOctDigit
241 lexHexDigits :: ReadS String
242 lexHexDigits = nonnull isHexDigit
245 lex0Digits :: ReadS String
246 lex0Digits s = return (span isDigit s)
248 nonnull :: (Char -> Bool) -> ReadS String
250 (cs@(_:_),t) <- return (span p s)
253 lexLitChar :: ReadS String
254 lexLitChar ('\\':s) = do
258 lexEsc (c:s) | c `elem` escChars = return ([c],s)
259 lexEsc s@(d:_) | isDigit d = checkSize 10 lexDecDigits s
260 lexEsc ('o':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
261 lexEsc ('O':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
262 lexEsc ('x':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
263 lexEsc ('X':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
264 lexEsc ('^':c:s) | c >= '@' && c <= '_' = [(['^',c],s)] -- cf. cntrl in 2.6 of H. report.
265 lexEsc s@(c:_) | isUpper c = fromAsciiLab s
268 escChars = "abfnrtv\\\"'"
270 fromAsciiLab (x:y:z:ls) | isUpper y && (isUpper z || isDigit z) &&
271 [x,y,z] `elem` asciiEscTab = return ([x,y,z], ls)
272 fromAsciiLab (x:y:ls) | isUpper y &&
273 [x,y] `elem` asciiEscTab = return ([x,y], ls)
274 fromAsciiLab _ = mzero
276 asciiEscTab = "DEL" : asciiTab
279 Check that the numerically escaped char literals are
280 within accepted boundaries.
282 Note: this allows char lits with leading zeros, i.e.,
283 \0000000000000000000000000000001.
285 checkSize base f str = do
287 if toAnInteger base num > toInteger (ord maxBound) then
291 8 -> return ('o':num, res)
292 16 -> return ('x':num, res)
293 _ -> return (num, res)
295 toAnInteger base = foldl (\ acc n -> acc*base + toInteger (digitToInt n)) 0
298 lexLitChar (c:s) = return ([c],s)
299 lexLitChar "" = mzero
301 digitToInt :: Char -> Int
303 | isDigit c = fromEnum c - fromEnum '0'
304 | c >= 'a' && c <= 'f' = fromEnum c - fromEnum 'a' + 10
305 | c >= 'A' && c <= 'F' = fromEnum c - fromEnum 'A' + 10
306 | otherwise = error ("Char.digitToInt: not a digit " ++ show c) -- sigh
309 %*********************************************************
311 \subsection{Instances of @Read@}
313 %*********************************************************
316 instance Read Char where
317 readsPrec _ = readParen False
320 (c,"\'") <- readLitChar s
323 readList = readParen False (\r -> do
327 where readl ('"':s) = return ("",s)
328 readl ('\\':'&':s) = readl s
330 (c,t) <- readLitChar s
334 instance Read Bool where
335 readsPrec _ = readParen False
338 (do { ("True", rest) <- return lr ; return (True, rest) }) ++
339 (do { ("False", rest) <- return lr ; return (False, rest) }))
342 instance Read Ordering where
343 readsPrec _ = readParen False
346 (do { ("LT", rest) <- return lr ; return (LT, rest) }) ++
347 (do { ("EQ", rest) <- return lr ; return (EQ, rest) }) ++
348 (do { ("GT", rest) <- return lr ; return (GT, rest) }))
350 instance Read a => Read (Maybe a) where
351 readsPrec _ = readParen False
354 (do { ("Nothing", rest) <- return lr ; return (Nothing, rest)}) ++
356 ("Just", rest1) <- return lr
357 (x, rest2) <- reads rest1
358 return (Just x, rest2)))
360 instance (Read a, Read b) => Read (Either a b) where
361 readsPrec _ = readParen False
365 ("Left", rest1) <- return lr
366 (x, rest2) <- reads rest1
367 return (Left x, rest2)) ++
369 ("Right", rest1) <- return lr
370 (x, rest2) <- reads rest1
371 return (Right x, rest2)))
373 instance Read Int where
374 readsPrec _ x = readSigned readDec x
376 instance Read Integer where
377 readsPrec _ x = readSigned readDec x
379 instance Read Float where
380 readsPrec _ x = readSigned readFloat x
382 instance Read Double where
383 readsPrec _ x = readSigned readFloat x
385 instance (Integral a, Read a) => Read (Ratio a) where
386 readsPrec p = readParen (p > ratio_prec)
393 instance (Read a) => Read [a] where
394 readsPrec _ = readList
396 instance Read () where
397 readsPrec _ = readParen False
403 instance (Read a, Read b) => Read (a,b) where
404 readsPrec _ = readParen False
407 (x,t) <- readsPrec 0 s
409 (y,v) <- readsPrec 0 u
413 instance (Read a, Read b, Read c) => Read (a, b, c) where
414 readsPrec _ = readParen False
417 (x,c) <- readsPrec 0 b
419 (y,e) <- readsPrec 0 d
421 (z,g) <- readsPrec 0 f
425 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
426 readsPrec _ = readParen False
429 (w,c) <- readsPrec 0 b
431 (x,e) <- readsPrec 0 d
433 (y,g) <- readsPrec 0 f
435 (z,h) <- readsPrec 0 h
437 return ((w,x,y,z), i))
439 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
440 readsPrec _ = readParen False
443 (v,c) <- readsPrec 0 b
445 (w,e) <- readsPrec 0 d
447 (x,g) <- readsPrec 0 f
449 (y,i) <- readsPrec 0 h
451 (z,k) <- readsPrec 0 j
453 return ((v,w,x,y,z), l))
457 %*********************************************************
459 \subsection{Reading characters}
461 %*********************************************************
464 readLitChar :: ReadS Char
466 readLitChar [] = mzero
467 readLitChar ('\\':s) = readEsc s
469 readEsc ('a':s) = return ('\a',s)
470 readEsc ('b':s) = return ('\b',s)
471 readEsc ('f':s) = return ('\f',s)
472 readEsc ('n':s) = return ('\n',s)
473 readEsc ('r':s) = return ('\r',s)
474 readEsc ('t':s) = return ('\t',s)
475 readEsc ('v':s) = return ('\v',s)
476 readEsc ('\\':s) = return ('\\',s)
477 readEsc ('"':s) = return ('"',s)
478 readEsc ('\'':s) = return ('\'',s)
479 readEsc ('^':c:s) | c >= '@' && c <= '_'
480 = return (chr (ord c - ord '@'), s)
481 readEsc s@(d:_) | isDigit d
492 readEsc s@(c:_) | isUpper c
493 = let table = ('\DEL', "DEL") : zip ['\NUL'..] asciiTab
494 in case [(c,s') | (c, mne) <- table,
495 ([],s') <- [match mne s]]
496 of (pr:_) -> return pr
500 readLitChar (c:s) = return (c,s)
502 match :: (Eq a) => [a] -> [a] -> ([a],[a])
503 match (x:xs) (y:ys) | x == y = match xs ys
504 match xs ys = (xs,ys)
509 %*********************************************************
511 \subsection{Reading numbers}
513 %*********************************************************
515 Note: reading numbers at bases different than 10, does not
516 include lexing common prefixes such as '0x' or '0o' etc.
519 {-# SPECIALISE readDec ::
522 readDec :: (Integral a) => ReadS a
523 readDec = readInt 10 isDigit (\d -> ord d - ord_0)
525 {-# SPECIALISE readOct ::
528 readOct :: (Integral a) => ReadS a
529 readOct = readInt 8 isOctDigit (\d -> ord d - ord_0)
531 {-# SPECIALISE readHex ::
534 readHex :: (Integral a) => ReadS a
535 readHex = readInt 16 isHexDigit hex
536 where hex d = ord d - (if isDigit d then ord_0
537 else ord (if isUpper d then 'A' else 'a') - 10)
539 readInt :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
540 readInt radix isDig digToInt s = do
541 (ds,r) <- nonnull isDig s
542 return (foldl1 (\n d -> n * radix + d) (map (fromInt . digToInt) ds), r)
544 {-# SPECIALISE readSigned ::
545 ReadS Int -> ReadS Int,
546 ReadS Integer -> ReadS Integer,
547 ReadS Double -> ReadS Double #-}
548 readSigned :: (Real a) => ReadS a -> ReadS a
549 readSigned readPos = readParen False read'
550 where read' r = read'' r ++
557 (n,"") <- readPos str
561 The functions readFloat below uses rational arithmetic
562 to ensure correct conversion between the floating-point radix and
563 decimal. It is often possible to use a higher-precision floating-
564 point type to obtain the same results.
567 {-# SPECIALISE readFloat ::
570 readFloat :: (RealFloat a) => ReadS a
572 (x,t) <- readRational r
573 return (fromRational x,t)
575 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
581 return ((n%1)*10^^(k-d), t )) ++
586 ("Infinity",t) <- lex r
590 (ds,s) <- lexDecDigits r
591 (ds',t) <- lexDotDigits s
592 return (read (ds++ds'), length ds', t)
594 readExp (e:s) | e `elem` "eE" = readExp' s
595 readExp s = return (0,s)
597 readExp' ('+':s) = readDec s
598 readExp' ('-':s) = do
601 readExp' s = readDec s
603 lexDotDigits ('.':s) = lex0Digits s
604 lexDotDigits s = return ("",s)
606 readRational__ :: String -> Rational -- we export this one (non-std)
607 -- NB: *does* handle a leading "-"
610 '-' : xs -> - (read_me xs)
614 = case (do { (x,t) <- readRational s ; ("","") <- lex t ; return x }) of
615 #ifndef NEW_READS_REP
617 [] -> error ("readRational__: no parse:" ++ top_s)
618 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
621 Nothing -> error ("readRational__: no parse:" ++ top_s)
626 %*********************************************************
628 \subsection{Reading BufferMode}
630 %*********************************************************
632 This instance decl is here rather than somewhere more appropriate in
633 order that we can avoid both orphan-instance modules and recursive
637 instance Read BufferMode where
640 (\r -> let lr = lex r
642 [(NoBuffering, rest) | ("NoBuffering", rest) <- lr] ++
643 [(LineBuffering,rest) | ("LineBuffering",rest) <- lr] ++
644 [(BlockBuffering mb,rest2) | ("BlockBuffering",rest1) <- lr,
645 (mb, rest2) <- reads rest1])