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
49 import qualified Text.ParserCombinators.ReadP as P
51 import Text.ParserCombinators.ReadP
57 import qualified Text.Read.Lex as L
58 -- Lex exports 'lex', which is also defined here,
59 -- hence the qualified import.
60 -- We can't import *anything* unqualified, because that
63 import Text.ParserCombinators.ReadPrec
68 import {-# SOURCE #-} GHC.Err ( error )
73 import GHC.Show -- isAlpha etc
78 -------------------------------------------------------
79 TEMPORARY UNTIL I DO DERIVED READ
82 readParen :: Bool -> ReadS a -> ReadS a
83 readParen b g = if b then mandatory else optional
84 where optional r = g r ++ mandatory r
92 readList__ :: ReadS a -> ReadS [a]
95 = readParen False (\r -> do
99 (do { ("]",t) <- lex s ; return ([],t) }) ++
100 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
103 (do { ("]",t) <- lex s ; return ([],t) }) ++
104 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
108 %*********************************************************
110 \subsection{The @Read@ class and @ReadS@ type}
112 %*********************************************************
115 ------------------------------------------------------------------------
118 -- | A parser for a type @a@, represented as a function that takes a
119 -- 'String' and returns a list of possible parses @(a,'String')@ pairs.
120 type ReadS a = String -> [(a,String)]
122 ------------------------------------------------------------------------
126 readsPrec :: Int -> ReadS a
127 readList :: ReadS [a]
128 readPrec :: ReadPrec a
129 readListPrec :: ReadPrec [a]
131 -- default definitions
132 readsPrec = readPrec_to_S readPrec
133 readList = readPrec_to_S (list readPrec) 0
134 readPrec = readS_to_Prec readsPrec
135 readListPrec = readS_to_Prec (\_ -> readList)
137 readListDefault :: Read a => ReadS [a]
138 -- ^ Use this to define the 'readList' method, if you
139 -- don't want a special case
140 readListDefault = readPrec_to_S readListPrec 0
142 readListPrecDefault :: Read a => ReadPrec [a]
143 -- ^ Use this to define the 'readListPrec' method, if you
144 -- don't want a special case
145 readListPrecDefault = list readPrec
147 ------------------------------------------------------------------------
150 reads :: Read a => ReadS a
151 reads = readsPrec minPrec
153 readp :: Read a => ReadP a
154 readp = readPrec_to_P readPrec minPrec
156 readEither :: Read a => String -> Either String a
158 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
160 [] -> Left "Prelude.read: no parse"
161 _ -> Left "Prelude.read: ambiguous parse"
168 read :: Read a => String -> a
169 read s = either error id (readEither s)
171 ------------------------------------------------------------------------
174 lex :: ReadS String -- As defined by H98
175 lex s = readP_to_S L.hsLex s
177 lexLitChar :: ReadS String -- As defined by H98
178 lexLitChar = readP_to_S (do { P.skipSpaces ;
179 (s, L.Char _) <- P.gather L.lex ;
182 readLitChar :: ReadS Char -- As defined by H98
183 readLitChar = readP_to_S (do { L.Char c <- L.lex ;
186 lexDigits :: ReadS String
187 lexDigits = readP_to_S (P.munch1 isDigit)
189 ------------------------------------------------------------------------
192 lexP :: ReadPrec L.Lexeme
193 -- ^ Parse a single lexeme
196 paren :: ReadPrec a -> ReadPrec a
197 -- ^ @(paren p)@ parses "(P0)"
198 -- where @p@ parses "P0" in precedence context zero
199 paren p = do L.Punc "(" <- lexP
204 parens :: ReadPrec a -> ReadPrec a
205 -- ^ @(parens p)@ parses "P", "(P0)", "((P0))", etc,
206 -- where @p@ parses "P" in the current precedence context
207 -- parses "P0" in precedence context zero
210 optional = p +++ mandatory
211 mandatory = paren optional
213 list :: ReadPrec a -> ReadPrec [a]
214 -- ^ @(list p)@ parses a list of things parsed by @p@,
215 -- using the usual square-bracket syntax.
218 ( do L.Punc "[" <- lexP
219 (listRest False +++ listNext)
226 "," | started -> listNext
234 choose :: [(String, ReadPrec a)] -> ReadPrec a
235 -- ^ Parse the specified lexeme and continue as specified.
236 -- Esp useful for nullary constructors; e.g.
237 -- @choose [("A", return A), ("B", return B)]@
238 choose sps = foldr ((+++) . try_one) pfail sps
240 try_one (s,p) = do { L.Ident s' <- lexP ;
241 if s == s' then p else pfail }
245 %*********************************************************
247 \subsection{Simple instances of Read}
249 %*********************************************************
252 instance Read Char where
255 ( do L.Char c <- lexP
261 ( do L.String s <- lexP -- Looks for "foo"
264 readListPrecDefault -- Looks for ['f','o','o']
265 ) -- (more generous than H98 spec)
267 readList = readListDefault
269 instance Read Bool where
272 ( do L.Ident s <- lexP
274 "True" -> return True
275 "False" -> return False
279 readListPrec = readListPrecDefault
280 readList = readListDefault
282 instance Read Ordering where
285 ( do L.Ident s <- lexP
293 readListPrec = readListPrecDefault
294 readList = readListDefault
298 %*********************************************************
300 \subsection{Structure instances of Read: Maybe, List etc}
302 %*********************************************************
304 For structured instances of Read we start using the precedences. The
305 idea is then that 'parens (prec k p)' will fail immediately when trying
306 to parse it in a context with a higher precedence level than k. But if
307 there is one parenthesis parsed, then the required precedence level
308 drops to 0 again, and parsing inside p may succeed.
310 'appPrec' is just the precedence level of function application (maybe
311 it should be called 'appPrec' instead). So, if we are parsing
312 function application, we'd better require the precedence level to be
313 at least 'appPrec'. Otherwise, we have to put parentheses around it.
315 'step' is used to increase the precedence levels inside a
316 parser, and can be used to express left- or right- associativity. For
317 example, % is defined to be left associative, so we only increase
318 precedence on the right hand side.
320 Note how step is used in for example the Maybe parser to increase the
321 precedence beyond appPrec, so that basically only literals and
322 parenthesis-like objects such as (...) and [...] can be an argument to
326 instance Read a => Read (Maybe a) where
330 ( do L.Ident "Nothing" <- lexP
333 do L.Ident "Just" <- lexP
339 readListPrec = readListPrecDefault
340 readList = readListDefault
342 instance (Read a, Read b) => Read (Either a b) where
346 ( do L.Ident "Left" <- lexP
350 do L.Ident "Right" <- lexP
356 readListPrec = readListPrecDefault
357 readList = readListDefault
359 instance Read a => Read [a] where
360 readPrec = readListPrec
361 readListPrec = readListPrecDefault
362 readList = readListDefault
364 instance (Ix a, Read a, Read b) => Read (Array a b) where
365 readPrec = parens $ prec appPrec $
366 do L.Ident "array" <- lexP
367 bounds <- step readPrec
368 vals <- step readPrec
369 return (array bounds vals)
371 readListPrec = readListPrecDefault
372 readList = readListDefault
374 instance Read L.Lexeme where
376 readListPrec = readListPrecDefault
377 readList = readListDefault
381 %*********************************************************
383 \subsection{Numeric instances of Read}
385 %*********************************************************
388 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
389 -- Read a signed number
394 L.Symbol "-" -> do n <- readNumber convert
397 _ -> case convert x of
402 convertInt :: Num a => L.Lexeme -> Maybe a
403 convertInt (L.Int i) = Just (fromInteger i)
404 convertInt _ = Nothing
406 convertFrac :: Fractional a => L.Lexeme -> Maybe a
407 convertFrac (L.Int i) = Just (fromInteger i)
408 convertFrac (L.Rat r) = Just (fromRational r)
409 convertFrac _ = Nothing
411 instance Read Int where
412 readPrec = readNumber convertInt
413 readListPrec = readListPrecDefault
414 readList = readListDefault
416 instance Read Integer where
417 readPrec = readNumber convertInt
418 readListPrec = readListPrecDefault
419 readList = readListDefault
421 instance Read Float where
422 readPrec = readNumber convertFrac
423 readListPrec = readListPrecDefault
424 readList = readListDefault
426 instance Read Double where
427 readPrec = readNumber convertFrac
428 readListPrec = readListPrecDefault
429 readList = readListDefault
431 instance (Integral a, Read a) => Read (Ratio a) where
435 ( do x <- step readPrec
442 readListPrec = readListPrecDefault
443 readList = readListDefault
447 %*********************************************************
449 \subsection{Tuple instances of Read}
451 %*********************************************************
454 instance Read () where
462 readListPrec = readListPrecDefault
463 readList = readListDefault
465 instance (Read a, Read b) => Read (a,b) where
476 readListPrec = readListPrecDefault
477 readList = readListDefault
480 instance (Read a, Read b, Read c) => Read (a, b, c) where
493 readListPrec = readListPrecDefault
494 readList = readListDefault
496 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
511 readListPrec = readListPrecDefault
512 readList = readListDefault
514 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
531 readListPrec = readListPrecDefault
532 readList = readListDefault