2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The FFI Task Force, 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The 'Read' class and instances for basic data types.
15 -----------------------------------------------------------------------------
21 , ReadS -- :: *; = String -> [(a,String)]
24 , reads -- :: Read a => ReadS a
25 , readp -- :: Read a => ReadP a
26 , readEither -- :: Read a => String -> Either String a
27 , read -- :: Read a => String -> a
30 , lex -- :: ReadS String
31 , lexLitChar -- :: ReadS String
32 , readLitChar -- :: ReadS Char
33 , lexDigits -- :: ReadS String
36 , lexP -- :: ReadPrec Lexeme
37 , paren -- :: ReadPrec a -> ReadPrec a
38 , parens -- :: ReadPrec a -> ReadPrec a
39 , list -- :: ReadPrec a -> ReadPrec [a]
40 , choose -- :: [(String, ReadPrec a)] -> ReadPrec a
41 , readListDefault, readListPrecDefault
48 import qualified Text.ParserCombinators.ReadP as P
50 import Text.ParserCombinators.ReadP
56 import qualified Text.Read.Lex as L
57 -- Lex exports 'lex', which is also defined here,
58 -- hence the qualified import.
59 -- We can't import *anything* unqualified, because that
62 import Text.ParserCombinators.ReadPrec
67 import {-# SOURCE #-} GHC.Err ( error )
72 import GHC.Show -- isAlpha etc
79 readParen :: Bool -> ReadS a -> ReadS a
80 -- A Haskell 98 function
81 readParen b g = if b then mandatory else optional
82 where optional r = g r ++ mandatory r
91 %*********************************************************
93 \subsection{The @Read@ class and @ReadS@ type}
95 %*********************************************************
98 ------------------------------------------------------------------------
101 -- | A parser for a type @a@, represented as a function that takes a
102 -- 'String' and returns a list of possible parses @(a,'String')@ pairs.
103 type ReadS a = String -> [(a,String)]
105 ------------------------------------------------------------------------
109 readsPrec :: Int -> ReadS a
110 readList :: ReadS [a]
111 readPrec :: ReadPrec a
112 readListPrec :: ReadPrec [a]
114 -- default definitions
115 readsPrec = readPrec_to_S readPrec
116 readList = readPrec_to_S (list readPrec) 0
117 readPrec = readS_to_Prec readsPrec
118 readListPrec = readS_to_Prec (\_ -> readList)
120 readListDefault :: Read a => ReadS [a]
121 -- ^ Use this to define the 'readList' method, if you
122 -- don't want a special case
123 readListDefault = readPrec_to_S readListPrec 0
125 readListPrecDefault :: Read a => ReadPrec [a]
126 -- ^ Use this to define the 'readListPrec' method, if you
127 -- don't want a special case
128 readListPrecDefault = list readPrec
130 ------------------------------------------------------------------------
133 reads :: Read a => ReadS a
134 reads = readsPrec minPrec
136 readp :: Read a => ReadP a
137 readp = readPrec_to_P readPrec minPrec
139 readEither :: Read a => String -> Either String a
141 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
143 [] -> Left "Prelude.read: no parse"
144 _ -> Left "Prelude.read: ambiguous parse"
151 read :: Read a => String -> a
152 read s = either error id (readEither s)
154 ------------------------------------------------------------------------
157 lex :: ReadS String -- As defined by H98
158 lex s = readP_to_S L.hsLex s
160 lexLitChar :: ReadS String -- As defined by H98
161 lexLitChar = readP_to_S (do { P.skipSpaces ;
162 (s, L.Char _) <- P.gather L.lex ;
165 readLitChar :: ReadS Char -- As defined by H98
166 readLitChar = readP_to_S (do { L.Char c <- L.lex ;
169 lexDigits :: ReadS String
170 lexDigits = readP_to_S (P.munch1 isDigit)
172 ------------------------------------------------------------------------
175 lexP :: ReadPrec L.Lexeme
176 -- ^ Parse a single lexeme
179 paren :: ReadPrec a -> ReadPrec a
180 -- ^ @(paren p)@ parses \"(P0)\"
181 -- where @p@ parses \"P0\" in precedence context zero
182 paren p = do L.Punc "(" <- lexP
187 parens :: ReadPrec a -> ReadPrec a
188 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
189 -- where @p@ parses \"P\" in the current precedence context
190 -- parses \"P0\" in precedence context zero
193 optional = p +++ mandatory
194 mandatory = paren optional
196 list :: ReadPrec a -> ReadPrec [a]
197 -- ^ @(list p)@ parses a list of things parsed by @p@,
198 -- using the usual square-bracket syntax.
201 ( do L.Punc "[" <- lexP
202 (listRest False +++ listNext)
209 "," | started -> listNext
217 choose :: [(String, ReadPrec a)] -> ReadPrec a
218 -- ^ Parse the specified lexeme and continue as specified.
219 -- Esp useful for nullary constructors; e.g.
220 -- @choose [(\"A\", return A), (\"B\", return B)]@
221 choose sps = foldr ((+++) . try_one) pfail sps
223 try_one (s,p) = do { L.Ident s' <- lexP ;
224 if s == s' then p else pfail }
228 %*********************************************************
230 \subsection{Simple instances of Read}
232 %*********************************************************
235 instance Read Char where
238 ( do L.Char c <- lexP
244 ( do L.String s <- lexP -- Looks for "foo"
247 readListPrecDefault -- Looks for ['f','o','o']
248 ) -- (more generous than H98 spec)
250 readList = readListDefault
252 instance Read Bool where
255 ( do L.Ident s <- lexP
257 "True" -> return True
258 "False" -> return False
262 readListPrec = readListPrecDefault
263 readList = readListDefault
265 instance Read Ordering where
268 ( do L.Ident s <- lexP
276 readListPrec = readListPrecDefault
277 readList = readListDefault
281 %*********************************************************
283 \subsection{Structure instances of Read: Maybe, List etc}
285 %*********************************************************
287 For structured instances of Read we start using the precedences. The
288 idea is then that 'parens (prec k p)' will fail immediately when trying
289 to parse it in a context with a higher precedence level than k. But if
290 there is one parenthesis parsed, then the required precedence level
291 drops to 0 again, and parsing inside p may succeed.
293 'appPrec' is just the precedence level of function application. So,
294 if we are parsing function application, we'd better require the
295 precedence level to be at least 'appPrec'. Otherwise, we have to put
296 parentheses around it.
298 'step' is used to increase the precedence levels inside a
299 parser, and can be used to express left- or right- associativity. For
300 example, % is defined to be left associative, so we only increase
301 precedence on the right hand side.
303 Note how step is used in for example the Maybe parser to increase the
304 precedence beyond appPrec, so that basically only literals and
305 parenthesis-like objects such as (...) and [...] can be an argument to
309 instance Read a => Read (Maybe a) where
312 (do L.Ident "Nothing" <- lexP
316 do L.Ident "Just" <- lexP
321 readListPrec = readListPrecDefault
322 readList = readListDefault
324 instance (Read a, Read b) => Read (Either a b) where
328 ( do L.Ident "Left" <- lexP
332 do L.Ident "Right" <- lexP
338 readListPrec = readListPrecDefault
339 readList = readListDefault
341 instance Read a => Read [a] where
342 readPrec = readListPrec
343 readListPrec = readListPrecDefault
344 readList = readListDefault
346 instance (Ix a, Read a, Read b) => Read (Array a b) where
347 readPrec = parens $ prec appPrec $
348 do L.Ident "array" <- lexP
349 bounds <- step readPrec
350 vals <- step readPrec
351 return (array bounds vals)
353 readListPrec = readListPrecDefault
354 readList = readListDefault
356 instance Read L.Lexeme where
358 readListPrec = readListPrecDefault
359 readList = readListDefault
363 %*********************************************************
365 \subsection{Numeric instances of Read}
367 %*********************************************************
370 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
371 -- Read a signed number
376 L.Symbol "-" -> do n <- readNumber convert
379 _ -> case convert x of
384 convertInt :: Num a => L.Lexeme -> Maybe a
385 convertInt (L.Int i) = Just (fromInteger i)
386 convertInt _ = Nothing
388 convertFrac :: Fractional a => L.Lexeme -> Maybe a
389 convertFrac (L.Int i) = Just (fromInteger i)
390 convertFrac (L.Rat r) = Just (fromRational r)
391 convertFrac _ = Nothing
393 instance Read Int where
394 readPrec = readNumber convertInt
395 readListPrec = readListPrecDefault
396 readList = readListDefault
398 instance Read Integer where
399 readPrec = readNumber convertInt
400 readListPrec = readListPrecDefault
401 readList = readListDefault
403 instance Read Float where
404 readPrec = readNumber convertFrac
405 readListPrec = readListPrecDefault
406 readList = readListDefault
408 instance Read Double where
409 readPrec = readNumber convertFrac
410 readListPrec = readListPrecDefault
411 readList = readListDefault
413 instance (Integral a, Read a) => Read (Ratio a) where
417 ( do x <- step readPrec
424 readListPrec = readListPrecDefault
425 readList = readListDefault
429 %*********************************************************
431 \subsection{Tuple instances of Read}
433 %*********************************************************
436 instance Read () where
444 readListPrec = readListPrecDefault
445 readList = readListDefault
447 instance (Read a, Read b) => Read (a,b) where
458 readListPrec = readListPrecDefault
459 readList = readListDefault
462 instance (Read a, Read b, Read c) => Read (a, b, c) where
475 readListPrec = readListPrecDefault
476 readList = readListDefault
478 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
493 readListPrec = readListPrecDefault
494 readList = readListDefault
496 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
513 readListPrec = readListPrecDefault
514 readList = readListDefault