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
77 ratioPrec = 7 -- Precedence of ':%' constructor
78 appPrec = 10 -- Precedence of applictaion
80 -------------------------------------------------------
81 TEMPORARY UNTIL I DO DERIVED READ
84 readParen :: Bool -> ReadS a -> ReadS a
85 readParen b g = if b then mandatory else optional
86 where optional r = g r ++ mandatory r
94 readList__ :: ReadS a -> ReadS [a]
97 = readParen False (\r -> do
101 (do { ("]",t) <- lex s ; return ([],t) }) ++
102 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
105 (do { ("]",t) <- lex s ; return ([],t) }) ++
106 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
110 %*********************************************************
112 \subsection{The @Read@ class and @ReadS@ type}
114 %*********************************************************
117 ------------------------------------------------------------------------
120 -- | A parser for a type @a@, represented as a function that takes a
121 -- 'String' and returns a list of possible parses @(a,'String')@ pairs.
122 type ReadS a = String -> [(a,String)]
124 ------------------------------------------------------------------------
128 readsPrec :: Int -> ReadS a
129 readList :: ReadS [a]
130 readPrec :: ReadPrec a
131 readListPrec :: ReadPrec [a]
133 -- default definitions
134 readsPrec = readPrec_to_S readPrec
135 readList = readPrec_to_S (list readPrec) 0
136 readPrec = readS_to_Prec readsPrec
137 readListPrec = readS_to_Prec (\_ -> readList)
139 readListDefault :: Read a => ReadS [a]
140 -- ^ Use this to define the 'readList' method, if you
141 -- don't want a special case
142 readListDefault = readPrec_to_S readListPrec 0
144 readListPrecDefault :: Read a => ReadPrec [a]
145 -- ^ Use this to define the 'readListPrec' method, if you
146 -- don't want a special case
147 readListPrecDefault = list readPrec
149 ------------------------------------------------------------------------
152 reads :: Read a => ReadS a
153 reads = readsPrec minPrec
155 readp :: Read a => ReadP a
156 readp = readPrec_to_P readPrec minPrec
158 readEither :: Read a => String -> Either String a
160 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
162 [] -> Left "Prelude.read: no parse"
163 _ -> Left "Prelude.read: ambiguous parse"
170 read :: Read a => String -> a
171 read s = either error id (readEither s)
173 ------------------------------------------------------------------------
176 lex :: ReadS String -- As defined by H98
177 lex s = readP_to_S L.hsLex s
179 lexLitChar :: ReadS String -- As defined by H98
180 lexLitChar = readP_to_S (do { P.skipSpaces ;
181 (s, L.Char _) <- P.gather L.lex ;
184 readLitChar :: ReadS Char -- As defined by H98
185 readLitChar = readP_to_S (do { L.Char c <- L.lex ;
188 lexDigits :: ReadS String
189 lexDigits = readP_to_S (P.munch1 isDigit)
191 ------------------------------------------------------------------------
194 lexP :: ReadPrec L.Lexeme
195 -- ^ Parse a single lexeme
198 paren :: ReadPrec a -> ReadPrec a
199 -- ^ @(paren p)@ parses "(P0)"
200 -- where @p@ parses "P0" in precedence context zero
201 paren p = do L.Punc "(" <- lexP
206 parens :: ReadPrec a -> ReadPrec a
207 -- ^ @(parens p)@ parses "P", "(P0)", "((P0))", etc,
208 -- where @p@ parses "P" in the current precedence context
209 -- parses "P0" in precedence context zero
212 optional = p +++ mandatory
213 mandatory = paren optional
215 list :: ReadPrec a -> ReadPrec [a]
216 -- ^ @(list p)@ parses a list of things parsed by @p@,
217 -- using the usual square-bracket syntax.
220 ( do L.Punc "[" <- lexP
221 (listRest False +++ listNext)
228 "," | started -> listNext
236 choose :: [(String, ReadPrec a)] -> ReadPrec a
237 -- ^ Parse the specified lexeme and continue as specified.
238 -- Esp useful for nullary constructors; e.g.
239 -- @choose [("A", return A), ("B", return B)]@
240 choose sps = foldr ((+++) . try_one) pfail sps
242 try_one (s,p) = do { L.Ident s' <- lexP ;
243 if s == s' then p else pfail }
247 %*********************************************************
249 \subsection{Simple instances of Read}
251 %*********************************************************
254 instance Read Char where
257 ( do L.Char c <- lexP
263 ( do L.String s <- lexP -- Looks for "foo"
266 readListPrecDefault -- Looks for ['f','o','o']
267 ) -- (more generous than H98 spec)
269 readList = readListDefault
271 instance Read Bool where
274 ( do L.Ident s <- lexP
276 "True" -> return True
277 "False" -> return False
281 readListPrec = readListPrecDefault
282 readList = readListDefault
284 instance Read Ordering where
287 ( do L.Ident s <- lexP
295 readListPrec = readListPrecDefault
296 readList = readListDefault
300 %*********************************************************
302 \subsection{Structure instances of Read: Maybe, List etc}
304 %*********************************************************
306 For structured instances of Read we start using the precedences. The
307 idea is then that 'parens (prec k p)' will fail immediately when trying
308 to parse it in a context with a higher precedence level than k. But if
309 there is one parenthesis parsed, then the required precedence level
310 drops to 0 again, and parsing inside p may succeed.
312 'appPrec' is just the precedence level of function application (maybe
313 it should be called 'appPrec' instead). So, if we are parsing
314 function application, we'd better require the precedence level to be
315 at least 'appPrec'. Otherwise, we have to put parentheses around it.
317 'step' is used to increase the precedence levels inside a
318 parser, and can be used to express left- or right- associativity. For
319 example, % is defined to be left associative, so we only increase
320 precedence on the right hand side.
322 Note how step is used in for example the Maybe parser to increase the
323 precedence beyond appPrec, so that basically only literals and
324 parenthesis-like objects such as (...) and [...] can be an argument to
328 instance Read a => Read (Maybe a) where
332 ( do L.Ident "Nothing" <- lexP
335 do L.Ident "Just" <- lexP
341 readListPrec = readListPrecDefault
342 readList = readListDefault
344 instance (Read a, Read b) => Read (Either a b) where
348 ( do L.Ident "Left" <- lexP
352 do L.Ident "Right" <- lexP
358 readListPrec = readListPrecDefault
359 readList = readListDefault
361 instance Read a => Read [a] where
362 readPrec = readListPrec
363 readListPrec = readListPrecDefault
364 readList = readListDefault
366 instance (Ix a, Read a, Read b) => Read (Array a b) where
367 readPrec = parens $ prec appPrec $
368 do L.Ident "array" <- lexP
369 bounds <- step readPrec
370 vals <- step readPrec
371 return (array bounds vals)
373 readListPrec = readListPrecDefault
374 readList = readListDefault
376 instance Read L.Lexeme where
378 readListPrec = readListPrecDefault
379 readList = readListDefault
383 %*********************************************************
385 \subsection{Numeric instances of Read}
387 %*********************************************************
390 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
391 -- Read a signed number
396 L.Symbol "-" -> do n <- readNumber convert
399 _ -> case convert x of
404 convertInt :: Num a => L.Lexeme -> Maybe a
405 convertInt (L.Int i) = Just (fromInteger i)
406 convertInt _ = Nothing
408 convertFrac :: Fractional a => L.Lexeme -> Maybe a
409 convertFrac (L.Int i) = Just (fromInteger i)
410 convertFrac (L.Rat r) = Just (fromRational r)
411 convertFrac _ = Nothing
413 instance Read Int where
414 readPrec = readNumber convertInt
415 readListPrec = readListPrecDefault
416 readList = readListDefault
418 instance Read Integer where
419 readPrec = readNumber convertInt
420 readListPrec = readListPrecDefault
421 readList = readListDefault
423 instance Read Float where
424 readPrec = readNumber convertFrac
425 readListPrec = readListPrecDefault
426 readList = readListDefault
428 instance Read Double where
429 readPrec = readNumber convertFrac
430 readListPrec = readListPrecDefault
431 readList = readListDefault
433 instance (Integral a, Read a) => Read (Ratio a) where
437 ( do x <- step readPrec
444 readListPrec = readListPrecDefault
445 readList = readListDefault
449 %*********************************************************
451 \subsection{Tuple instances of Read}
453 %*********************************************************
456 instance Read () where
464 readListPrec = readListPrecDefault
465 readList = readListDefault
467 instance (Read a, Read b) => Read (a,b) where
478 readListPrec = readListPrecDefault
479 readList = readListDefault
482 instance (Read a, Read b, Read c) => Read (a, b, c) where
495 readListPrec = readListPrecDefault
496 readList = readListDefault
498 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
513 readListPrec = readListPrecDefault
514 readList = readListDefault
516 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
533 readListPrec = readListPrecDefault
534 readList = readListDefault