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
329 (do L.Ident "Nothing" <- lexP
333 do L.Ident "Just" <- lexP
338 readListPrec = readListPrecDefault
339 readList = readListDefault
341 instance (Read a, Read b) => Read (Either a b) where
345 ( do L.Ident "Left" <- lexP
349 do L.Ident "Right" <- lexP
355 readListPrec = readListPrecDefault
356 readList = readListDefault
358 instance Read a => Read [a] where
359 readPrec = readListPrec
360 readListPrec = readListPrecDefault
361 readList = readListDefault
363 instance (Ix a, Read a, Read b) => Read (Array a b) where
364 readPrec = parens $ prec appPrec $
365 do L.Ident "array" <- lexP
366 bounds <- step readPrec
367 vals <- step readPrec
368 return (array bounds vals)
370 readListPrec = readListPrecDefault
371 readList = readListDefault
373 instance Read L.Lexeme where
375 readListPrec = readListPrecDefault
376 readList = readListDefault
380 %*********************************************************
382 \subsection{Numeric instances of Read}
384 %*********************************************************
387 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
388 -- Read a signed number
393 L.Symbol "-" -> do n <- readNumber convert
396 _ -> case convert x of
401 convertInt :: Num a => L.Lexeme -> Maybe a
402 convertInt (L.Int i) = Just (fromInteger i)
403 convertInt _ = Nothing
405 convertFrac :: Fractional a => L.Lexeme -> Maybe a
406 convertFrac (L.Int i) = Just (fromInteger i)
407 convertFrac (L.Rat r) = Just (fromRational r)
408 convertFrac _ = Nothing
410 instance Read Int where
411 readPrec = readNumber convertInt
412 readListPrec = readListPrecDefault
413 readList = readListDefault
415 instance Read Integer where
416 readPrec = readNumber convertInt
417 readListPrec = readListPrecDefault
418 readList = readListDefault
420 instance Read Float where
421 readPrec = readNumber convertFrac
422 readListPrec = readListPrecDefault
423 readList = readListDefault
425 instance Read Double where
426 readPrec = readNumber convertFrac
427 readListPrec = readListPrecDefault
428 readList = readListDefault
430 instance (Integral a, Read a) => Read (Ratio a) where
434 ( do x <- step readPrec
441 readListPrec = readListPrecDefault
442 readList = readListDefault
446 %*********************************************************
448 \subsection{Tuple instances of Read}
450 %*********************************************************
453 instance Read () where
461 readListPrec = readListPrecDefault
462 readList = readListDefault
464 instance (Read a, Read b) => Read (a,b) where
475 readListPrec = readListPrecDefault
476 readList = readListDefault
479 instance (Read a, Read b, Read c) => Read (a, b, c) where
492 readListPrec = readListPrecDefault
493 readList = readListDefault
495 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
510 readListPrec = readListPrecDefault
511 readList = readListDefault
513 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
530 readListPrec = readListPrecDefault
531 readList = readListDefault