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(..) )
21 import PrelShow -- isAlpha etc
25 -- needed for readIO and instance Read Buffermode
26 import PrelIOBase ( IO, userError, BufferMode(..) )
27 import PrelException ( ioError )
30 %*********************************************************
32 \subsection{The @Read@ class}
34 %*********************************************************
36 Note: if you compile this with -DNEW_READS_REP, you'll get
37 a (simpler) ReadS representation that only allow one valid
38 parse of a string of characters, instead of a list of
41 [changing the ReadS rep has implications for the deriving
42 machinery for Read, a change that hasn't been made, so you
43 probably won't want to compile in this new rep. except
44 when in an experimental mood.]
49 type ReadS a = String -> [(a,String)]
51 type ReadS a = String -> Maybe (a,String)
55 readsPrec :: Int -> ReadS a
58 readList = readList__ reads
61 %*********************************************************
63 \subsection{Utility functions}
65 %*********************************************************
68 reads :: (Read a) => ReadS a
71 read :: (Read a) => String -> a
76 [] -> error "Prelude.read: no parse"
77 _ -> error "Prelude.read: ambiguous parse"
80 Nothing -> error "Prelude.read: no parse"
88 -- raises an exception instead of an error
89 readIO :: Read a => String -> IO a
90 readIO s = case (do { (x,t) <- reads s ; ("","") <- lex t ; return x }) of
93 [] -> ioError (userError "Prelude.readIO: no parse")
94 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
97 Nothing -> ioError (userError "Prelude.readIO: no parse")
103 readParen :: Bool -> ReadS a -> ReadS a
104 readParen b g = if b then mandatory else optional
105 where optional r = g r ++ mandatory r
113 readList__ :: ReadS a -> ReadS [a]
116 = readParen False (\r -> do
120 (do { ("]",t) <- lex s ; return ([],t) }) ++
121 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
124 (do { ("]",t) <- lex s ; return ([],t) }) ++
125 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
130 %*********************************************************
132 \subsection{Lexical analysis}
134 %*********************************************************
136 This lexer is not completely faithful to the Haskell lexical syntax.
138 Qualified names are not handled properly
139 A `--' does not terminate a symbol
140 Octal and hexidecimal numerics are not recognized as a single token
145 lex "" = return ("","")
146 lex (c:s) | isSpace c = lex (dropWhile isSpace s)
148 (ch, '\'':t) <- lexLitChar s
150 return ('\'':ch++"'", t)
152 (str,t) <- lexString s
156 lexString ('"':s) = return ("\"",s)
158 (ch,t) <- lexStrItem s
159 (str,u) <- lexString t
163 lexStrItem ('\\':'&':s) = return ("\\&",s)
164 lexStrItem ('\\':c:s) | isSpace c = do
165 ('\\':t) <- return (dropWhile isSpace s)
167 lexStrItem s = lexLitChar s
169 lex (c:s) | isSingle c = return ([c],s)
171 (sym,t) <- return (span isSym s)
174 (nam,t) <- return (span isIdChar s)
180 ('o':rs) -> (isOctDigit, rs, False)
181 ('O':rs) -> (isOctDigit, rs, False)
182 ('x':rs) -> (isHexDigit, rs, False)
183 ('X':rs) -> (isHexDigit, rs, False)
184 _ -> (isDigit, s, True)
186 (ds,s) <- return (span pred s')
187 (fe,t) <- lexFracExp isDec s
189 | otherwise = mzero -- bad character
191 isSingle c = c `elem` ",;()[]{}_`"
192 isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
193 isIdChar c = isAlphaNum c || c `elem` "_'"
195 lexFracExp True ('.':cs) = do
196 (ds,t) <- lex0Digits cs
199 lexFracExp _ s = return ("",s)
201 lexExp (e:s) | e `elem` "eE" =
204 guard (c `elem` "+-")
205 (ds,u) <- lexDecDigits t
206 return (e:c:ds,u)) ++
208 (ds,t) <- lexDecDigits s
211 lexExp s = return ("",s)
213 lexDigits :: ReadS String
214 lexDigits = lexDecDigits
216 lexDecDigits :: ReadS String
217 lexDecDigits = nonnull isDigit
219 lexOctDigits :: ReadS String
220 lexOctDigits = nonnull isOctDigit
222 lexHexDigits :: ReadS String
223 lexHexDigits = nonnull isHexDigit
226 lex0Digits :: ReadS String
227 lex0Digits s = return (span isDigit s)
229 nonnull :: (Char -> Bool) -> ReadS String
231 (cs@(_:_),t) <- return (span p s)
234 lexLitChar :: ReadS String
235 lexLitChar ('\\':s) = do
239 lexEsc (c:s) | c `elem` escChars = return ([c],s)
240 lexEsc s@(d:_) | isDigit d = checkSize 10 lexDecDigits s
241 lexEsc ('o':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
242 lexEsc ('O':d:s) | isOctDigit d = checkSize 8 lexOctDigits (d:s)
243 lexEsc ('x':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
244 lexEsc ('X':d:s) | isHexDigit d = checkSize 16 lexHexDigits (d:s)
245 lexEsc ('^':c:s) | c >= '@' && c <= '_' = [(['^',c],s)] -- cf. cntrl in 2.6 of H. report.
246 lexEsc s@(c:_) | isUpper c = fromAsciiLab s
249 escChars = "abfnrtv\\\"'"
251 fromAsciiLab (x:y:z:ls) | isUpper y && (isUpper z || isDigit z) &&
252 [x,y,z] `elem` asciiEscTab = return ([x,y,z], ls)
253 fromAsciiLab (x:y:ls) | isUpper y &&
254 [x,y] `elem` asciiEscTab = return ([x,y], ls)
255 fromAsciiLab _ = mzero
257 asciiEscTab = "DEL" : asciiTab
260 Check that the numerically escaped char literals are
261 within accepted boundaries.
263 Note: this allows char lits with leading zeros, i.e.,
264 \0000000000000000000000000000001.
266 checkSize base f str = do
268 -- Note: this is assumes that a Char is 8 bits long.
269 if (toAnInt base num) > 255 then
273 8 -> return ('o':num, res)
274 16 -> return ('x':num, res)
275 _ -> return (num, res)
277 toAnInt base xs = foldl (\ acc n -> acc*base + n) 0 (map digitToInt xs)
280 lexLitChar (c:s) = return ([c],s)
281 lexLitChar "" = mzero
283 digitToInt :: Char -> Int
285 | isDigit c = fromEnum c - fromEnum '0'
286 | c >= 'a' && c <= 'f' = fromEnum c - fromEnum 'a' + 10
287 | c >= 'A' && c <= 'F' = fromEnum c - fromEnum 'A' + 10
288 | otherwise = error ("Char.digitToInt: not a digit " ++ show c) -- sigh
291 %*********************************************************
293 \subsection{Instances of @Read@}
295 %*********************************************************
298 instance Read Char where
299 readsPrec _ = readParen False
302 (c,"\'") <- readLitChar s
305 readList = readParen False (\r -> do
309 where readl ('"':s) = return ("",s)
310 readl ('\\':'&':s) = readl s
312 (c,t) <- readLitChar s
316 instance Read Bool where
317 readsPrec _ = readParen False
320 (do { ("True", rest) <- return lr ; return (True, rest) }) ++
321 (do { ("False", rest) <- return lr ; return (False, rest) }))
324 instance Read Ordering where
325 readsPrec _ = readParen False
328 (do { ("LT", rest) <- return lr ; return (LT, rest) }) ++
329 (do { ("EQ", rest) <- return lr ; return (EQ, rest) }) ++
330 (do { ("GT", rest) <- return lr ; return (GT, rest) }))
332 instance Read a => Read (Maybe a) where
333 readsPrec _ = readParen False
336 (do { ("Nothing", rest) <- return lr ; return (Nothing, rest)}) ++
338 ("Just", rest1) <- return lr
339 (x, rest2) <- reads rest1
340 return (Just x, rest2)))
342 instance (Read a, Read b) => Read (Either a b) where
343 readsPrec _ = readParen False
347 ("Left", rest1) <- return lr
348 (x, rest2) <- reads rest1
349 return (Left x, rest2)) ++
351 ("Right", rest1) <- return lr
352 (x, rest2) <- reads rest1
353 return (Right x, rest2)))
355 instance Read Int where
356 readsPrec _ x = readSigned readDec x
358 instance Read Integer where
359 readsPrec _ x = readSigned readDec x
361 instance Read Float where
362 readsPrec _ x = readSigned readFloat x
364 instance Read Double where
365 readsPrec _ x = readSigned readFloat x
367 instance (Integral a, Read a) => Read (Ratio a) where
368 readsPrec p = readParen (p > ratio_prec)
375 instance (Read a) => Read [a] where
376 readsPrec _ = readList
378 instance Read () where
379 readsPrec _ = readParen False
385 instance (Read a, Read b) => Read (a,b) where
386 readsPrec _ = readParen False
389 (x,t) <- readsPrec 0 s
391 (y,v) <- readsPrec 0 u
395 instance (Read a, Read b, Read c) => Read (a, b, c) where
396 readsPrec _ = readParen False
399 (x,c) <- readsPrec 0 b
401 (y,e) <- readsPrec 0 d
403 (z,g) <- readsPrec 0 f
407 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
408 readsPrec _ = readParen False
411 (w,c) <- readsPrec 0 b
413 (x,e) <- readsPrec 0 d
415 (y,g) <- readsPrec 0 f
417 (z,h) <- readsPrec 0 h
419 return ((w,x,y,z), i))
421 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
422 readsPrec _ = readParen False
425 (v,c) <- readsPrec 0 b
427 (w,e) <- readsPrec 0 d
429 (x,g) <- readsPrec 0 f
431 (y,i) <- readsPrec 0 h
433 (z,k) <- readsPrec 0 j
435 return ((v,w,x,y,z), l))
439 %*********************************************************
441 \subsection{Reading characters}
443 %*********************************************************
446 readLitChar :: ReadS Char
448 readLitChar [] = mzero
449 readLitChar ('\\':s) = readEsc s
451 readEsc ('a':s) = return ('\a',s)
452 readEsc ('b':s) = return ('\b',s)
453 readEsc ('f':s) = return ('\f',s)
454 readEsc ('n':s) = return ('\n',s)
455 readEsc ('r':s) = return ('\r',s)
456 readEsc ('t':s) = return ('\t',s)
457 readEsc ('v':s) = return ('\v',s)
458 readEsc ('\\':s) = return ('\\',s)
459 readEsc ('"':s) = return ('"',s)
460 readEsc ('\'':s) = return ('\'',s)
461 readEsc ('^':c:s) | c >= '@' && c <= '_'
462 = return (chr (ord c - ord '@'), s)
463 readEsc s@(d:_) | isDigit d
474 readEsc s@(c:_) | isUpper c
475 = let table = ('\DEL', "DEL") : zip ['\NUL'..] asciiTab
476 in case [(c,s') | (c, mne) <- table,
477 ([],s') <- [match mne s]]
478 of (pr:_) -> return pr
482 readLitChar (c:s) = return (c,s)
484 match :: (Eq a) => [a] -> [a] -> ([a],[a])
485 match (x:xs) (y:ys) | x == y = match xs ys
486 match xs ys = (xs,ys)
491 %*********************************************************
493 \subsection{Reading numbers}
495 %*********************************************************
497 Note: reading numbers at bases different than 10, does not
498 include lexing common prefixes such as '0x' or '0o' etc.
501 {-# SPECIALISE readDec ::
504 readDec :: (Integral a) => ReadS a
505 readDec = readInt 10 isDigit (\d -> ord d - ord_0)
507 {-# SPECIALISE readOct ::
510 readOct :: (Integral a) => ReadS a
511 readOct = readInt 8 isOctDigit (\d -> ord d - ord_0)
513 {-# SPECIALISE readHex ::
516 readHex :: (Integral a) => ReadS a
517 readHex = readInt 16 isHexDigit hex
518 where hex d = ord d - (if isDigit d then ord_0
519 else ord (if isUpper d then 'A' else 'a') - 10)
521 readInt :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
522 readInt radix isDig digToInt s = do
523 (ds,r) <- nonnull isDig s
524 return (foldl1 (\n d -> n * radix + d) (map (fromInt . digToInt) ds), r)
526 {-# SPECIALISE readSigned ::
527 ReadS Int -> ReadS Int,
528 ReadS Integer -> ReadS Integer,
529 ReadS Double -> ReadS Double #-}
530 readSigned :: (Real a) => ReadS a -> ReadS a
531 readSigned readPos = readParen False read'
532 where read' r = read'' r ++
539 (n,"") <- readPos str
543 The functions readFloat below uses rational arithmetic
544 to ensure correct conversion between the floating-point radix and
545 decimal. It is often possible to use a higher-precision floating-
546 point type to obtain the same results.
549 {-# SPECIALISE readFloat ::
552 readFloat :: (RealFloat a) => ReadS a
554 (x,t) <- readRational r
555 return (fromRational x,t)
557 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
563 return ((n%1)*10^^(k-d), t )) ++
568 ("Infinity",t) <- lex r
572 (ds,s) <- lexDecDigits r
573 (ds',t) <- lexDotDigits s
574 return (read (ds++ds'), length ds', t)
576 readExp (e:s) | e `elem` "eE" = readExp' s
577 readExp s = return (0,s)
579 readExp' ('+':s) = readDec s
580 readExp' ('-':s) = do
583 readExp' s = readDec s
585 lexDotDigits ('.':s) = lex0Digits s
586 lexDotDigits s = return ("",s)
588 readRational__ :: String -> Rational -- we export this one (non-std)
589 -- NB: *does* handle a leading "-"
592 '-' : xs -> - (read_me xs)
596 = case (do { (x,t) <- readRational s ; ("","") <- lex t ; return x }) of
597 #ifndef NEW_READS_REP
599 [] -> error ("readRational__: no parse:" ++ top_s)
600 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
603 Nothing -> error ("readRational__: no parse:" ++ top_s)
608 %*********************************************************
610 \subsection{Reading BufferMode}
612 %*********************************************************
614 This instance decl is here rather than somewhere more appropriate in
615 order that we can avoid both orphan-instance modules and recursive
619 instance Read BufferMode where
622 (\r -> let lr = lex r
624 [(NoBuffering, rest) | ("NoBuffering", rest) <- lr] ++
625 [(LineBuffering,rest) | ("LineBuffering",rest) <- lr] ++
626 [(BlockBuffering mb,rest2) | ("BlockBuffering",rest1) <- lr,
627 (mb, rest2) <- reads rest1])