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
48 import qualified Text.ParserCombinators.ReadP as P
50 import Text.ParserCombinators.ReadP
56 import qualified Text.Read.Lex as L
58 import Text.Read.Lex ( Lexeme(..) )
60 import Text.ParserCombinators.ReadPrec
65 import {-# SOURCE #-} GHC.Err ( error )
70 import GHC.Show -- isAlpha etc
73 ratioPrec = 7 -- Precedence of ':%' constructor
74 appPrec = 10 -- Precedence of applictaion
76 -------------------------------------------------------
77 TEMPORARY UNTIL I DO DERIVED READ
80 readParen :: Bool -> ReadS a -> ReadS a
81 readParen b g = if b then mandatory else optional
82 where optional r = g r ++ mandatory r
90 readList__ :: ReadS a -> ReadS [a]
93 = readParen False (\r -> do
97 (do { ("]",t) <- lex s ; return ([],t) }) ++
98 (do { (x,t) <- readx s ; (xs,u) <- readl2 t ; return (x:xs,u) })
101 (do { ("]",t) <- lex s ; return ([],t) }) ++
102 (do { (",",t) <- lex s ; (x,u) <- readx t ; (xs,v) <- readl2 u ; return (x:xs,v) })
106 %*********************************************************
108 \subsection{The @Read@ class and @ReadS@ type}
110 %*********************************************************
113 ------------------------------------------------------------------------
116 -- | A parser for a type @a@, represented as a function that takes a
117 -- 'String' and returns a list of possible parses @(a,'String')@ pairs.
118 type ReadS a = String -> [(a,String)]
120 ------------------------------------------------------------------------
124 readsPrec :: Int -> ReadS a
125 readList :: ReadS [a]
126 readPrec :: ReadPrec a
127 readListPrec :: ReadPrec [a]
129 -- default definitions
130 readsPrec = readPrec_to_S readPrec
131 readList = readPrec_to_S (list readPrec) 0
132 readPrec = readS_to_Prec readsPrec
133 readListPrec = readS_to_Prec (\_ -> readList)
135 readListDefault :: Read a => ReadS [a]
136 readListDefault = readPrec_to_S readListPrec 0
138 readListPrecDefault :: Read a => ReadPrec [a]
139 readListPrecDefault = list readPrec
141 ------------------------------------------------------------------------
144 reads :: Read a => ReadS a
145 reads = readsPrec minPrec
147 readp :: Read a => ReadP a
148 readp = readPrec_to_P readPrec minPrec
150 readEither :: Read a => String -> Either String a
152 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
154 [] -> Left "Prelude.read: no parse"
155 _ -> Left "Prelude.read: ambiguous parse"
162 read :: Read a => String -> a
163 read s = either error id (readEither s)
165 ------------------------------------------------------------------------
168 lex :: ReadS String -- As defined by H98
169 lex s = readP_to_S L.hsLex s
171 lexLitChar :: ReadS String -- As defined by H98
172 lexLitChar = readP_to_S (do { P.skipSpaces ;
173 (s, Char _) <- P.gather L.lex ;
176 readLitChar :: ReadS Char -- As defined by H98
177 readLitChar = readP_to_S (do { Char c <- L.lex ;
180 lexDigits :: ReadS String
181 lexDigits = readP_to_S (P.munch1 isDigit)
183 ------------------------------------------------------------------------
186 lexP :: ReadPrec Lexeme
189 paren :: ReadPrec a -> ReadPrec a
190 -- (paren p) parses (P0)
191 -- where p parses P0 in precedence context zero
192 paren p = do Punc "(" <- lexP
197 parens :: ReadPrec a -> ReadPrec a
198 -- (parens p) parses P, (P0), ((P0)), etc,
199 -- where p parses P in the current precedence context
200 -- parses P0 in precedence context zero
203 optional = p +++ mandatory
204 mandatory = paren optional
206 list :: ReadPrec a -> ReadPrec [a]
209 ( do Punc "[" <- lexP
210 (listRest False +++ listNext)
217 "," | started -> listNext
225 choose :: [(String, ReadPrec a)] -> ReadPrec a
226 -- Parse the specified lexeme and continue as specified
227 -- Esp useful for nullary constructors
228 choose sps = foldr ((+++) . try_one) pfail sps
230 try_one (s,p) = do { Ident s' <- lexP ;
231 if s == s' then p else pfail }
235 %*********************************************************
237 \subsection{Simple instances of Read}
239 %*********************************************************
242 instance Read Char where
251 ( do String s <- lexP -- Looks for "foo"
254 readListPrecDefault -- Looks for ['f','o','o']
255 ) -- (more generous than H98 spec)
257 readList = readListDefault
259 instance Read Bool where
264 "True" -> return True
265 "False" -> return False
269 readListPrec = readListPrecDefault
270 readList = readListDefault
272 instance Read Ordering where
283 readListPrec = readListPrecDefault
284 readList = readListDefault
288 %*********************************************************
290 \subsection{Structure instances of Read: Maybe, List etc}
292 %*********************************************************
294 For structured instances of Read we start using the precedences. The
295 idea is then that 'parens (prec k p)' will fail immediately when trying
296 to parse it in a context with a higher precedence level than k. But if
297 there is one parenthesis parsed, then the required precedence level
298 drops to 0 again, and parsing inside p may succeed.
300 'appPrec' is just the precedence level of function application (maybe
301 it should be called 'appPrec' instead). So, if we are parsing
302 function application, we'd better require the precedence level to be
303 at least 'appPrec'. Otherwise, we have to put parentheses around it.
305 'step' is used to increase the precedence levels inside a
306 parser, and can be used to express left- or right- associativity. For
307 example, % is defined to be left associative, so we only increase
308 precedence on the right hand side.
310 Note how step is used in for example the Maybe parser to increase the
311 precedence beyond appPrec, so that basically only literals and
312 parenthesis-like objects such as (...) and [...] can be an argument to
316 instance Read a => Read (Maybe a) where
320 ( do Ident "Nothing" <- lexP
323 do Ident "Just" <- lexP
329 readListPrec = readListPrecDefault
330 readList = readListDefault
332 instance (Read a, Read b) => Read (Either a b) where
336 ( do Ident "Left" <- lexP
340 do Ident "Right" <- lexP
346 readListPrec = readListPrecDefault
347 readList = readListDefault
349 instance Read a => Read [a] where
350 readPrec = readListPrec
351 readListPrec = readListPrecDefault
352 readList = readListDefault
354 instance Read Lexeme where
356 readListPrec = readListPrecDefault
357 readList = readListDefault
361 %*********************************************************
363 \subsection{Numeric instances of Read}
365 %*********************************************************
368 readNumber :: Num a => (Lexeme -> Maybe a) -> ReadPrec a
369 -- Read a signed number
374 Symbol "-" -> do n <- readNumber convert
377 _ -> case convert x of
382 convertInt :: Num a => Lexeme -> Maybe a
383 convertInt (Int i) = Just (fromInteger i)
384 convertInt _ = Nothing
386 convertFrac :: Fractional a => Lexeme -> Maybe a
387 convertFrac (Int i) = Just (fromInteger i)
388 convertFrac (Rat r) = Just (fromRational r)
389 convertFrac _ = Nothing
391 instance Read Int where
392 readPrec = readNumber convertInt
393 readListPrec = readListPrecDefault
394 readList = readListDefault
396 instance Read Integer where
397 readPrec = readNumber convertInt
398 readListPrec = readListPrecDefault
399 readList = readListDefault
401 instance Read Float where
402 readPrec = readNumber convertFrac
403 readListPrec = readListPrecDefault
404 readList = readListDefault
406 instance Read Double where
407 readPrec = readNumber convertFrac
408 readListPrec = readListPrecDefault
409 readList = readListDefault
411 instance (Integral a, Read a) => Read (Ratio a) where
415 ( do x <- step readPrec
422 readListPrec = readListPrecDefault
423 readList = readListDefault
427 %*********************************************************
429 \subsection{Tuple instances of Read}
431 %*********************************************************
434 instance Read () where
442 readListPrec = readListPrecDefault
443 readList = readListDefault
445 instance (Read a, Read b) => Read (a,b) where
456 readListPrec = readListPrecDefault
457 readList = readListDefault
460 instance (Read a, Read b, Read c) => Read (a, b, c) where
473 readListPrec = readListPrecDefault
474 readList = readListDefault
476 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
491 readListPrec = readListPrecDefault
492 readList = readListDefault
494 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
511 readListPrec = readListPrecDefault
512 readList = readListDefault