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}
95 %*********************************************************
98 ------------------------------------------------------------------------
102 readsPrec :: Int -> ReadS a
103 readList :: ReadS [a]
104 -- | Proposed replacement for 'readsPrec' using new-style parsers (GHC only).
105 readPrec :: ReadPrec a
106 -- | Proposed replacement for 'readList' using new-style parsers (GHC only).
107 readListPrec :: ReadPrec [a]
109 -- default definitions
110 readsPrec = readPrec_to_S readPrec
111 readList = readPrec_to_S (list readPrec) 0
112 readPrec = readS_to_Prec readsPrec
113 readListPrec = readS_to_Prec (\_ -> readList)
115 readListDefault :: Read a => ReadS [a]
116 -- ^ Use this to define the 'readList' method, if you don't want a special
117 -- case (GHC only; for other systems the default suffices).
118 readListDefault = readPrec_to_S readListPrec 0
120 readListPrecDefault :: Read a => ReadPrec [a]
121 -- ^ Use this to define the 'readListPrec' method, if you
122 -- don't want a special case (GHC only).
123 readListPrecDefault = list readPrec
125 ------------------------------------------------------------------------
128 reads :: Read a => ReadS a
129 reads = readsPrec minPrec
131 readp :: Read a => ReadP a
132 readp = readPrec_to_P readPrec minPrec
134 readEither :: Read a => String -> Either String a
136 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
138 [] -> Left "Prelude.read: no parse"
139 _ -> Left "Prelude.read: ambiguous parse"
146 read :: Read a => String -> a
147 read s = either error id (readEither s)
149 ------------------------------------------------------------------------
152 lex :: ReadS String -- As defined by H98
153 lex s = readP_to_S L.hsLex s
155 lexLitChar :: ReadS String -- As defined by H98
156 lexLitChar = readP_to_S (do { (s, _) <- P.gather L.lexChar ;
158 -- There was a skipSpaces before the P.gather L.lexChar,
159 -- but that seems inconsistent with readLitChar
161 readLitChar :: ReadS Char -- As defined by H98
162 readLitChar = readP_to_S L.lexChar
164 lexDigits :: ReadS String
165 lexDigits = readP_to_S (P.munch1 isDigit)
167 ------------------------------------------------------------------------
170 lexP :: ReadPrec L.Lexeme
171 -- ^ Parse a single lexeme
174 paren :: ReadPrec a -> ReadPrec a
175 -- ^ @(paren p)@ parses \"(P0)\"
176 -- where @p@ parses \"P0\" in precedence context zero
177 paren p = do L.Punc "(" <- lexP
182 parens :: ReadPrec a -> ReadPrec a
183 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
184 -- where @p@ parses \"P\" in the current precedence context
185 -- parses \"P0\" in precedence context zero
188 optional = p +++ mandatory
189 mandatory = paren optional
191 list :: ReadPrec a -> ReadPrec [a]
192 -- ^ @(list p)@ parses a list of things parsed by @p@,
193 -- using the usual square-bracket syntax.
196 ( do L.Punc "[" <- lexP
197 (listRest False +++ listNext)
204 "," | started -> listNext
212 choose :: [(String, ReadPrec a)] -> ReadPrec a
213 -- ^ Parse the specified lexeme and continue as specified.
214 -- Esp useful for nullary constructors; e.g.
215 -- @choose [(\"A\", return A), (\"B\", return B)]@
216 choose sps = foldr ((+++) . try_one) pfail sps
218 try_one (s,p) = do { L.Ident s' <- lexP ;
219 if s == s' then p else pfail }
223 %*********************************************************
225 \subsection{Simple instances of Read}
227 %*********************************************************
230 instance Read Char where
233 ( do L.Char c <- lexP
239 ( do L.String s <- lexP -- Looks for "foo"
242 readListPrecDefault -- Looks for ['f','o','o']
243 ) -- (more generous than H98 spec)
245 readList = readListDefault
247 instance Read Bool where
250 ( do L.Ident s <- lexP
252 "True" -> return True
253 "False" -> return False
257 readListPrec = readListPrecDefault
258 readList = readListDefault
260 instance Read Ordering where
263 ( do L.Ident s <- lexP
271 readListPrec = readListPrecDefault
272 readList = readListDefault
276 %*********************************************************
278 \subsection{Structure instances of Read: Maybe, List etc}
280 %*********************************************************
282 For structured instances of Read we start using the precedences. The
283 idea is then that 'parens (prec k p)' will fail immediately when trying
284 to parse it in a context with a higher precedence level than k. But if
285 there is one parenthesis parsed, then the required precedence level
286 drops to 0 again, and parsing inside p may succeed.
288 'appPrec' is just the precedence level of function application. So,
289 if we are parsing function application, we'd better require the
290 precedence level to be at least 'appPrec'. Otherwise, we have to put
291 parentheses around it.
293 'step' is used to increase the precedence levels inside a
294 parser, and can be used to express left- or right- associativity. For
295 example, % is defined to be left associative, so we only increase
296 precedence on the right hand side.
298 Note how step is used in for example the Maybe parser to increase the
299 precedence beyond appPrec, so that basically only literals and
300 parenthesis-like objects such as (...) and [...] can be an argument to
304 instance Read a => Read (Maybe a) where
307 (do L.Ident "Nothing" <- lexP
311 do L.Ident "Just" <- lexP
316 readListPrec = readListPrecDefault
317 readList = readListDefault
319 instance (Read a, Read b) => Read (Either a b) where
323 ( do L.Ident "Left" <- lexP
327 do L.Ident "Right" <- lexP
333 readListPrec = readListPrecDefault
334 readList = readListDefault
336 instance Read a => Read [a] where
337 readPrec = readListPrec
338 readListPrec = readListPrecDefault
339 readList = readListDefault
341 instance (Ix a, Read a, Read b) => Read (Array a b) where
342 readPrec = parens $ prec appPrec $
343 do L.Ident "array" <- lexP
344 bounds <- step readPrec
345 vals <- step readPrec
346 return (array bounds vals)
348 readListPrec = readListPrecDefault
349 readList = readListDefault
351 instance Read L.Lexeme where
353 readListPrec = readListPrecDefault
354 readList = readListDefault
358 %*********************************************************
360 \subsection{Numeric instances of Read}
362 %*********************************************************
365 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
366 -- Read a signed number
371 L.Symbol "-" -> do n <- readNumber convert
374 _ -> case convert x of
379 convertInt :: Num a => L.Lexeme -> Maybe a
380 convertInt (L.Int i) = Just (fromInteger i)
381 convertInt _ = Nothing
383 convertFrac :: Fractional a => L.Lexeme -> Maybe a
384 convertFrac (L.Int i) = Just (fromInteger i)
385 convertFrac (L.Rat r) = Just (fromRational r)
386 convertFrac _ = Nothing
388 instance Read Int where
389 readPrec = readNumber convertInt
390 readListPrec = readListPrecDefault
391 readList = readListDefault
393 instance Read Integer where
394 readPrec = readNumber convertInt
395 readListPrec = readListPrecDefault
396 readList = readListDefault
398 instance Read Float where
399 readPrec = readNumber convertFrac
400 readListPrec = readListPrecDefault
401 readList = readListDefault
403 instance Read Double where
404 readPrec = readNumber convertFrac
405 readListPrec = readListPrecDefault
406 readList = readListDefault
408 instance (Integral a, Read a) => Read (Ratio a) where
412 ( do x <- step readPrec
419 readListPrec = readListPrecDefault
420 readList = readListDefault
424 %*********************************************************
426 \subsection{Tuple instances of Read}
428 %*********************************************************
431 instance Read () where
439 readListPrec = readListPrecDefault
440 readList = readListDefault
442 instance (Read a, Read b) => Read (a,b) where
453 readListPrec = readListPrecDefault
454 readList = readListDefault
457 instance (Read a, Read b, Read c) => Read (a, b, c) where
470 readListPrec = readListPrecDefault
471 readList = readListDefault
473 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
488 readListPrec = readListPrecDefault
489 readList = readListDefault
491 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
508 readListPrec = readListPrecDefault
509 readList = readListDefault