2 % (c) The AQUA Project, Glasgow University, 1994-1998
5 \section[PrelRead]{Module @PrelRead@}
7 Instances of the Read class.
10 {-# OPTIONS -fno-implicit-prelude #-}
14 import PrelErr ( error )
15 import PrelEnum ( Enum(..) )
22 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 %*********************************************************
64 \subsection{Utility functions}
66 %*********************************************************
69 reads :: (Read a) => ReadS a
72 read :: (Read a) => String -> a
77 [] -> error "Prelude.read: no parse"
78 _ -> error "Prelude.read: ambiguous parse"
81 Nothing -> error "Prelude.read: no parse"
89 -- raises an exception instead of an error
90 readIO :: Read a => String -> IO a
91 readIO s = case (do { (x,t) <- reads s ; ("","") <- lex t ; return x }) of
94 [] -> ioError (userError "Prelude.readIO: no parse")
95 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
98 Nothing -> ioError (userError "Prelude.readIO: no parse")
104 readParen :: Bool -> ReadS a -> ReadS a
105 readParen b g = if b then mandatory else optional
106 where optional r = g r ++ mandatory r
114 readList__ :: ReadS a -> ReadS [a]
117 = readParen False (\r -> do
121 (do { ("]",t) <- lex s ; return ([],t) }) ++
122 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
125 (do { ("]",t) <- lex s ; return ([],t) }) ++
126 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
131 %*********************************************************
133 \subsection{Lexical analysis}
135 %*********************************************************
137 This lexer is not completely faithful to the Haskell lexical syntax.
139 Qualified names are not handled properly
140 A `--' does not terminate a symbol
141 Octal and hexidecimal numerics are not recognized as a single token
146 lex "" = return ("","")
147 lex (c:s) | isSpace c = lex (dropWhile isSpace s)
149 (ch, '\'':t) <- lexLitChar s
151 return ('\'':ch++"'", t)
153 (str,t) <- lexString s
157 lexString ('"':s) = return ("\"",s)
159 (ch,t) <- lexStrItem s
160 (str,u) <- lexString t
164 lexStrItem ('\\':'&':s) = return ("\\&",s)
165 lexStrItem ('\\':c:s) | isSpace c = do
166 ('\\':t) <- return (dropWhile isSpace s)
168 lexStrItem s = lexLitChar s
170 lex (c:s) | isSingle c = return ([c],s)
172 (sym,t) <- return (span isSym s)
175 (nam,t) <- return (span isIdChar s)
181 ('o':rs) -> (isOctDigit, rs, False)
182 ('O':rs) -> (isOctDigit, rs, False)
183 ('x':rs) -> (isHexDigit, rs, False)
184 ('X':rs) -> (isHexDigit, rs, False)
185 _ -> (isDigit, s, True)
187 (ds,s) <- return (span pred s')
188 (fe,t) <- lexFracExp isDec s
190 | otherwise = mzero -- bad character
192 isSingle c = c `elem` ",;()[]{}_`"
193 isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
194 isIdChar c = isAlphaNum c || c `elem` "_'"
196 lexFracExp True ('.':cs) = do
197 (ds,t) <- lex0Digits cs
200 lexFracExp _ s = return ("",s)
202 lexExp (e:s) | e `elem` "eE" =
205 guard (c `elem` "+-")
206 (ds,u) <- lexDecDigits t
207 return (e:c:ds,u)) ++
209 (ds,t) <- lexDecDigits s
212 lexExp s = return ("",s)
214 lexDigits :: ReadS String
215 lexDigits = lexDecDigits
217 lexDecDigits :: ReadS String
218 lexDecDigits = nonnull isDigit
220 lexOctDigits :: ReadS String
221 lexOctDigits = nonnull isOctDigit
223 lexHexDigits :: ReadS String
224 lexHexDigits = nonnull isHexDigit
227 lex0Digits :: ReadS String
228 lex0Digits s = return (span isDigit s)
230 nonnull :: (Char -> Bool) -> ReadS String
232 (cs@(_:_),t) <- return (span p s)
235 lexLitChar :: ReadS String
236 lexLitChar ('\\':s) = do
240 lexEsc (c:s) | c `elem` escChars = return ([c],s)
241 lexEsc s@(d:_) | isDigit d = checkSize 10 lexDecDigits s
242 lexEsc ('o':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
243 lexEsc ('O':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
244 lexEsc ('x':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
245 lexEsc ('X':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
246 lexEsc ('^':c:s) | c >= '@' && c <= '_' = [(['^',c],s)] -- cf. cntrl in 2.6 of H. report.
247 lexEsc s@(c:_) | isUpper c = fromAsciiLab s
250 escChars = "abfnrtv\\\"'"
252 fromAsciiLab (x:y:z:ls) | isUpper y && (isUpper z || isDigit z) &&
253 [x,y,z] `elem` asciiEscTab = return ([x,y,z], ls)
254 fromAsciiLab (x:y:ls) | isUpper y &&
255 [x,y] `elem` asciiEscTab = return ([x,y], ls)
256 fromAsciiLab _ = mzero
258 asciiEscTab = "DEL" : asciiTab
261 Check that the numerically escaped char literals are
262 within accepted boundaries.
264 Note: this allows char lits with leading zeros, i.e.,
265 \0000000000000000000000000000001.
267 checkSize base f str = do
269 -- Note: this is assumes that a Char is 8 bits long.
270 if (toAnInt base num) > 255 then
274 8 -> return ('o':num, res)
275 16 -> return ('x':num, res)
276 _ -> return (num, res)
278 toAnInt base xs = foldl (\ acc n -> acc*base + n) 0 (map digitToInt xs)
281 lexLitChar (c:s) = return ([c],s)
282 lexLitChar "" = mzero
284 digitToInt :: Char -> Int
286 | isDigit c = fromEnum c - fromEnum '0'
287 | c >= 'a' && c <= 'f' = fromEnum c - fromEnum 'a' + 10
288 | c >= 'A' && c <= 'F' = fromEnum c - fromEnum 'A' + 10
289 | otherwise = error ("Char.digitToInt: not a digit " ++ show c) -- sigh
292 %*********************************************************
294 \subsection{Instances of @Read@}
296 %*********************************************************
299 instance Read Char where
300 readsPrec _ = readParen False
303 (c,"\'") <- readLitChar s
306 readList = readParen False (\r -> do
310 where readl ('"':s) = return ("",s)
311 readl ('\\':'&':s) = readl s
313 (c,t) <- readLitChar s
317 instance Read Bool where
318 readsPrec _ = readParen False
321 (do { ("True", rest) <- return lr ; return (True, rest) }) ++
322 (do { ("False", rest) <- return lr ; return (False, rest) }))
325 instance Read Ordering where
326 readsPrec _ = readParen False
329 (do { ("LT", rest) <- return lr ; return (LT, rest) }) ++
330 (do { ("EQ", rest) <- return lr ; return (EQ, rest) }) ++
331 (do { ("GT", rest) <- return lr ; return (GT, rest) }))
333 instance Read a => Read (Maybe a) where
334 readsPrec _ = readParen False
337 (do { ("Nothing", rest) <- return lr ; return (Nothing, rest)}) ++
339 ("Just", rest1) <- return lr
340 (x, rest2) <- reads rest1
341 return (Just x, rest2)))
343 instance (Read a, Read b) => Read (Either a b) where
344 readsPrec _ = readParen False
348 ("Left", rest1) <- return lr
349 (x, rest2) <- reads rest1
350 return (Left x, rest2)) ++
352 ("Right", rest1) <- return lr
353 (x, rest2) <- reads rest1
354 return (Right x, rest2)))
356 instance Read Int where
357 readsPrec _ x = readSigned readDec x
359 instance Read Integer where
360 readsPrec _ x = readSigned readDec x
362 instance Read Float where
363 readsPrec _ x = readSigned readFloat x
365 instance Read Double where
366 readsPrec _ x = readSigned readFloat x
368 instance (Integral a, Read a) => Read (Ratio a) where
369 readsPrec p = readParen (p > ratio_prec)
376 instance (Read a) => Read [a] where
377 readsPrec _ = readList
379 instance Read () where
380 readsPrec _ = readParen False
386 instance (Read a, Read b) => Read (a,b) where
387 readsPrec _ = readParen False
390 (x,t) <- readsPrec 0 s
392 (y,v) <- readsPrec 0 u
396 instance (Read a, Read b, Read c) => Read (a, b, c) where
397 readsPrec _ = readParen False
400 (x,c) <- readsPrec 0 b
402 (y,e) <- readsPrec 0 d
404 (z,g) <- readsPrec 0 f
408 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
409 readsPrec _ = readParen False
412 (w,c) <- readsPrec 0 b
414 (x,e) <- readsPrec 0 d
416 (y,g) <- readsPrec 0 f
418 (z,h) <- readsPrec 0 h
420 return ((w,x,y,z), i))
422 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
423 readsPrec _ = readParen False
426 (v,c) <- readsPrec 0 b
428 (w,e) <- readsPrec 0 d
430 (x,g) <- readsPrec 0 f
432 (y,i) <- readsPrec 0 h
434 (z,k) <- readsPrec 0 j
436 return ((v,w,x,y,z), l))
440 %*********************************************************
442 \subsection{Reading characters}
444 %*********************************************************
447 readLitChar :: ReadS Char
449 readLitChar [] = mzero
450 readLitChar ('\\':s) = readEsc s
452 readEsc ('a':s) = return ('\a',s)
453 readEsc ('b':s) = return ('\b',s)
454 readEsc ('f':s) = return ('\f',s)
455 readEsc ('n':s) = return ('\n',s)
456 readEsc ('r':s) = return ('\r',s)
457 readEsc ('t':s) = return ('\t',s)
458 readEsc ('v':s) = return ('\v',s)
459 readEsc ('\\':s) = return ('\\',s)
460 readEsc ('"':s) = return ('"',s)
461 readEsc ('\'':s) = return ('\'',s)
462 readEsc ('^':c:s) | c >= '@' && c <= '_'
463 = return (chr (ord c - ord '@'), s)
464 readEsc s@(d:_) | isDigit d
475 readEsc s@(c:_) | isUpper c
476 = let table = ('\DEL', "DEL") : zip ['\NUL'..] asciiTab
477 in case [(c,s') | (c, mne) <- table,
478 ([],s') <- [match mne s]]
479 of (pr:_) -> return pr
483 readLitChar (c:s) = return (c,s)
485 match :: (Eq a) => [a] -> [a] -> ([a],[a])
486 match (x:xs) (y:ys) | x == y = match xs ys
487 match xs ys = (xs,ys)
492 %*********************************************************
494 \subsection{Reading numbers}
496 %*********************************************************
498 Note: reading numbers at bases different than 10, does not
499 include lexing common prefixes such as '0x' or '0o' etc.
502 {-# SPECIALISE readDec ::
505 readDec :: (Integral a) => ReadS a
506 readDec = readInt 10 isDigit (\d -> ord d - ord_0)
508 {-# SPECIALISE readOct ::
511 readOct :: (Integral a) => ReadS a
512 readOct = readInt 8 isOctDigit (\d -> ord d - ord_0)
514 {-# SPECIALISE readHex ::
517 readHex :: (Integral a) => ReadS a
518 readHex = readInt 16 isHexDigit hex
519 where hex d = ord d - (if isDigit d then ord_0
520 else ord (if isUpper d then 'A' else 'a') - 10)
522 readInt :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
523 readInt radix isDig digToInt s = do
524 (ds,r) <- nonnull isDig s
525 return (foldl1 (\n d -> n * radix + d) (map (fromInt . digToInt) ds), r)
527 {-# SPECIALISE readSigned ::
528 ReadS Int -> ReadS Int,
529 ReadS Integer -> ReadS Integer,
530 ReadS Double -> ReadS Double #-}
531 readSigned :: (Real a) => ReadS a -> ReadS a
532 readSigned readPos = readParen False read'
533 where read' r = read'' r ++
540 (n,"") <- readPos str
544 The functions readFloat below uses rational arithmetic
545 to ensure correct conversion between the floating-point radix and
546 decimal. It is often possible to use a higher-precision floating-
547 point type to obtain the same results.
550 {-# SPECIALISE readFloat ::
553 readFloat :: (RealFloat a) => ReadS a
555 (x,t) <- readRational r
556 return (fromRational x,t)
558 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
564 return ((n%1)*10^^(k-d), t )) ++
569 ("Infinity",t) <- lex r
573 (ds,s) <- lexDecDigits r
574 (ds',t) <- lexDotDigits s
575 return (read (ds++ds'), length ds', t)
577 readExp (e:s) | e `elem` "eE" = readExp' s
578 readExp s = return (0,s)
580 readExp' ('+':s) = readDec s
581 readExp' ('-':s) = do
584 readExp' s = readDec s
586 lexDotDigits ('.':s) = lex0Digits s
587 lexDotDigits s = return ("",s)
589 readRational__ :: String -> Rational -- we export this one (non-std)
590 -- NB: *does* handle a leading "-"
593 '-' : xs -> - (read_me xs)
597 = case (do { (x,t) <- readRational s ; ("","") <- lex t ; return x }) of
598 #ifndef NEW_READS_REP
600 [] -> error ("readRational__: no parse:" ++ top_s)
601 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
604 Nothing -> error ("readRational__: no parse:" ++ top_s)
609 %*********************************************************
611 \subsection{Reading BufferMode}
613 %*********************************************************
615 This instance decl is here rather than somewhere more appropriate in
616 order that we can avoid both orphan-instance modules and recursive
620 instance Read BufferMode where
623 (\r -> let lr = lex r
625 [(NoBuffering, rest) | ("NoBuffering", rest) <- lr] ++
626 [(LineBuffering,rest) | ("LineBuffering",rest) <- lr] ++
627 [(BlockBuffering mb,rest2) | ("BlockBuffering",rest1) <- lr,
628 (mb, rest2) <- reads rest1])