2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[PrelRead]{Module @PrelRead@}
7 Instances of the Read class.
10 {-# OPTIONS -fno-implicit-prelude #-}
14 import {-# SOURCE #-} Error ( error )
21 %*********************************************************
23 \subsection{The @Read@ class}
25 %*********************************************************
28 type ReadS a = String -> [(a,String)]
31 readsPrec :: Int -> ReadS a
34 readList = readList__ reads
37 %*********************************************************
39 \subsection{Utility functions}
41 %*********************************************************
44 reads :: (Read a) => ReadS a
47 read :: (Read a) => String -> a
48 read s = case [x | (x,t) <- reads s, ("","") <- lex t] of
50 [] -> error "PreludeText.read: no parse"
51 _ -> error "PreludeText.read: ambiguous parse"
53 readParen :: Bool -> ReadS a -> ReadS a
54 readParen b g = if b then mandatory else optional
55 where optional r = g r ++ mandatory r
56 mandatory r = [(x,u) | ("(",s) <- lex r,
61 {-# GENERATE_SPECS readList__ a #-}
62 readList__ :: ReadS a -> ReadS [a]
65 = readParen False (\r -> [pr | ("[",s) <- lex r, pr <- readl s])
66 where readl s = [([],t) | ("]",t) <- lex s] ++
67 [(x:xs,u) | (x,t) <- readx s,
69 readl2 s = [([],t) | ("]",t) <- lex s] ++
70 [(x:xs,v) | (",",t) <- lex s,
76 %*********************************************************
78 \subsection{Lexical analysis}
80 %*********************************************************
82 This lexer is not completely faithful to the Haskell lexical syntax.
84 Qualified names are not handled properly
85 A `--' does not terminate a symbol
86 Octal and hexidecimal numerics are not recognized as a single token
92 lex (c:s) | isSpace c = lex (dropWhile isSpace s)
93 lex ('\'':s) = [('\'':ch++"'", t) | (ch,'\'':t) <- lexLitChar s,
95 lex ('"':s) = [('"':str, t) | (str,t) <- lexString s]
97 lexString ('"':s) = [("\"",s)]
98 lexString s = [(ch++str, u)
99 | (ch,t) <- lexStrItem s,
100 (str,u) <- lexString t ]
102 lexStrItem ('\\':'&':s) = [("\\&",s)]
103 lexStrItem ('\\':c:s) | isSpace c
104 = [("\\&",t) | '\\':t <- [dropWhile isSpace s]]
105 lexStrItem s = lexLitChar s
107 lex (c:s) | isSingle c = [([c],s)]
108 | isSym c = [(c:sym,t) | (sym,t) <- [span isSym s]]
109 | isAlpha c = [(c:nam,t) | (nam,t) <- [span isIdChar s]]
110 | isDigit c = [(c:ds++fe,t) | (ds,s) <- [span isDigit s],
111 (fe,t) <- lexFracExp s ]
112 | otherwise = [] -- bad character
114 isSingle c = c `elem` ",;()[]{}_`"
115 isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"
116 isIdChar c = isAlphanum c || c `elem` "_'"
118 lexFracExp ('.':cs) = [('.':ds++e,u) | (ds,t) <- lex0Digits cs,
120 lexFracExp s = [("",s)]
122 lexExp (e:s) | e `elem` "eE"
123 = [(e:c:ds,u) | (c:t) <- [s], c `elem` "+-",
124 (ds,u) <- lexDigits t] ++
125 [(e:ds,t) | (ds,t) <- lexDigits s]
128 lexDigits :: ReadS String
129 lexDigits = nonnull isDigit
132 lex0Digits :: ReadS String
133 lex0Digits s = [span isDigit s]
135 nonnull :: (Char -> Bool) -> ReadS String
136 nonnull p s = [(cs,t) | (cs@(_:_),t) <- [span p s]]
138 lexLitChar :: ReadS String
139 lexLitChar ('\\':s) = [('\\':esc, t) | (esc,t) <- lexEsc s]
141 lexEsc (c:s) | c `elem` "abfnrtv\\\"'" = [([c],s)]
142 lexEsc s@(d:_) | isDigit d = lexDigits s
144 lexLitChar (c:s) = [([c],s)]
148 %*********************************************************
150 \subsection{Instances of @Read@}
152 %*********************************************************
155 instance Read Char where
156 readsPrec p = readParen False
157 (\r -> [(c,t) | ('\'':s,t)<- lex r,
158 (c,_) <- readLitChar s])
160 readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
162 where readl ('"':s) = [("",s)]
163 readl ('\\':'&':s) = readl s
164 readl s = [(c:cs,u) | (c ,t) <- readLitChar s,
167 instance Read Bool where
168 readsPrec p = readParen False
169 (\r -> let lr = lex r
171 [(True, rest) | ("True", rest) <- lr] ++
172 [(False,rest) | ("False",rest) <- lr])
175 instance Read Ordering where
176 readsPrec p = readParen False
177 (\r -> let lr = lex r
179 [(LT, rest) | ("LT", rest) <- lr] ++
180 [(EQ, rest) | ("EQ", rest) <- lr] ++
181 [(GT, rest) | ("GT", rest) <- lr])
183 instance Read a => Read (Maybe a) where
184 readsPrec p = readParen False
185 (\r -> let lr = lex r
187 [(Nothing, rest) | ("Nothing", rest) <- lr] ++
188 [(Just x, rest2) | ("Just", rest1) <- lr,
189 (x, rest2) <- reads rest1])
191 instance (Read a, Read b) => Read (Either a b) where
192 readsPrec p = readParen False
193 (\r -> let lr = lex r
195 [(Left x, rest2) | ("Left", rest1) <- lr,
196 (x, rest2) <- reads rest1] ++
197 [(Right x, rest2) | ("Right", rest1) <- lr,
198 (x, rest2) <- reads rest1])
200 instance Read Int where
201 readsPrec p x = readSigned readDec x
203 instance Read Integer where
204 readsPrec p x = readSigned readDec x
206 instance Read Float where
207 readsPrec p x = readSigned readFloat x
209 instance Read Double where
210 readsPrec p x = readSigned readFloat x
212 instance (Integral a, Read a) => Read (Ratio a) where
213 readsPrec p = readParen (p > ratio_prec)
214 (\r -> [(x%y,u) | (x,s) <- reads r,
218 instance (Read a) => Read [a] where
219 readsPrec p = readList
221 instance Read () where
222 readsPrec p = readParen False
223 (\r -> [((),t) | ("(",s) <- lex r,
226 instance (Read a, Read b) => Read (a,b) where
227 readsPrec p = readParen False
228 (\r -> [((x,y), w) | ("(",s) <- lex r,
234 instance (Read a, Read b, Read c) => Read (a, b, c) where
235 readsPrec p = readParen False
236 (\a -> [((x,y,z), h) | ("(",b) <- lex a,
237 (x,c) <- readsPrec 0 b,
239 (y,e) <- readsPrec 0 d,
241 (z,g) <- readsPrec 0 f,
244 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
245 readsPrec p = readParen False
246 (\a -> [((w,x,y,z), j) | ("(",b) <- lex a,
247 (w,c) <- readsPrec 0 b,
249 (x,e) <- readsPrec 0 d,
251 (y,g) <- readsPrec 0 f,
253 (z,i) <- readsPrec 0 h,
256 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
257 readsPrec p = readParen False
258 (\a -> [((w,x,y,z,v), l) | ("(",b) <- lex a,
259 (w,c) <- readsPrec 0 b,
261 (x,e) <- readsPrec 0 d,
263 (y,g) <- readsPrec 0 f,
265 (z,i) <- readsPrec 0 h,
267 (v,k) <- readsPrec 0 j,
272 %*********************************************************
274 \subsection{Reading characters}
276 %*********************************************************
279 readLitChar :: ReadS Char
281 readLitChar ('\\':s) = readEsc s
283 readEsc ('a':s) = [('\a',s)]
284 readEsc ('b':s) = [('\b',s)]
285 readEsc ('f':s) = [('\f',s)]
286 readEsc ('n':s) = [('\n',s)]
287 readEsc ('r':s) = [('\r',s)]
288 readEsc ('t':s) = [('\t',s)]
289 readEsc ('v':s) = [('\v',s)]
290 readEsc ('\\':s) = [('\\',s)]
291 readEsc ('"':s) = [('"',s)]
292 readEsc ('\'':s) = [('\'',s)]
293 readEsc ('^':c:s) | c >= '@' && c <= '_'
294 = [(chr (ord c - ord '@'), s)]
295 readEsc s@(d:_) | isDigit d
296 = [(chr n, t) | (n,t) <- readDec s]
297 readEsc ('o':s) = [(chr n, t) | (n,t) <- readOct s]
298 readEsc ('x':s) = [(chr n, t) | (n,t) <- readHex s]
299 readEsc s@(c:_) | isUpper c
300 = let table = ('\DEL', "DEL") : zip ['\NUL'..] asciiTab
301 in case [(c,s') | (c, mne) <- table,
302 ([],s') <- [match mne s]]
306 readLitChar (c:s) = [(c,s)]
308 match :: (Eq a) => [a] -> [a] -> ([a],[a])
309 match (x:xs) (y:ys) | x == y = match xs ys
310 match xs ys = (xs,ys)
315 %*********************************************************
317 \subsection{Reading numbers}
319 %*********************************************************
322 {-# GENERATE_SPECS readDec a{Int#,Int,Integer} #-}
323 readDec :: (Integral a) => ReadS a
324 readDec = readInt 10 isDigit (\d -> ord d - ord_0)
326 {-# GENERATE_SPECS readOct a{Int#,Int,Integer} #-}
327 readOct :: (Integral a) => ReadS a
328 readOct = readInt 8 isOctDigit (\d -> ord d - ord_0)
330 {-# GENERATE_SPECS readHex a{Int#,Int,Integer} #-}
331 readHex :: (Integral a) => ReadS a
332 readHex = readInt 16 isHexDigit hex
333 where hex d = ord d - (if isDigit d then ord_0
334 else ord (if isUpper d then 'A' else 'a') - 10)
336 {-# GENERATE_SPECS readInt a{Int#,Int,Integer} #-}
337 readInt :: (Integral a) => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
338 readInt radix isDig digToInt s =
339 [(foldl1 (\n d -> n * radix + d) (map (fromInt . digToInt) ds), r)
340 | (ds,r) <- nonnull isDig s ]
342 {-# GENERATE_SPECS readSigned a{Int#,Double#,Int,Integer,Double} #-}
343 readSigned :: (Real a) => ReadS a -> ReadS a
344 readSigned readPos = readParen False read'
345 where read' r = read'' r ++
346 [(-x,t) | ("-",s) <- lex r,
348 read'' r = [(n,s) | (str,s) <- lex r,
349 (n,"") <- readPos str]
352 The functions readFloat below uses rational arithmetic
353 to insure correct conversion between the floating-point radix and
354 decimal. It is often possible to use a higher-precision floating-
355 point type to obtain the same results.
358 {-# GENERATE_SPECS readFloat a{Double#,Double} #-}
359 readFloat :: (RealFloat a) => ReadS a
360 readFloat r = [(fromRational x, t) | (x, t) <- readRational r]
362 readRational :: ReadS Rational -- NB: doesn't handle leading "-"
365 = [ ( (n%1)*10^^(k-d), t ) | (n,d,s) <- readFix r,
366 (k,t) <- readExp s] ++
367 [(0/0, t) | ("NaN", t) <- lex r] ++
368 [(1/0, t) | ("Infinity", t) <- lex r]
369 where readFix r = [(read (ds++ds'), length ds', t)
370 | (ds,s) <- lexDigits r,
371 (ds',t) <- lexDotDigits s ]
373 readExp (e:s) | e `elem` "eE" = readExp' s
376 readExp' ('-':s) = [(-k,t) | (k,t) <- readDec s]
377 readExp' ('+':s) = readDec s
378 readExp' s = readDec s
380 lexDotDigits ('.':s) = lex0Digits s
381 lexDotDigits s = [("",s)]
383 readRational__ :: String -> Rational -- we export this one (non-std)
384 -- NB: *does* handle a leading "-"
387 '-' : xs -> - (read_me xs)
391 = case [x | (x,t) <- readRational s, ("","") <- lex t] of
393 [] -> error ("readRational__: no parse:" ++ top_s)
394 _ -> error ("readRational__: ambiguous parse:" ++ top_s)
396 -- The number of decimal digits m below is chosen to guarantee
397 -- read (show x) == x. See
398 -- Matula, D. W. A formalization of floating-point numeric base
399 -- conversion. IEEE Transactions on Computers C-19, 8 (1970 August),