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
55 import qualified Text.Read.Lex as L
56 -- Lex exports 'lex', which is also defined here,
57 -- hence the qualified import.
58 -- We can't import *anything* unqualified, because that
61 import Text.ParserCombinators.ReadPrec
66 import {-# SOURCE #-} GHC.Err ( error )
71 import GHC.Show -- isAlpha etc
78 readParen :: Bool -> ReadS a -> ReadS a
79 -- A Haskell 98 function
80 readParen b g = if b then mandatory else optional
81 where optional r = g r ++ mandatory r
90 %*********************************************************
92 \subsection{The @Read@ class and @ReadS@ type}
94 %*********************************************************
97 ------------------------------------------------------------------------
100 -- | A parser for a type @a@, represented as a function that takes a
101 -- 'String' and returns a list of possible parses @(a,'String')@ pairs.
102 type ReadS a = String -> [(a,String)]
104 ------------------------------------------------------------------------
108 readsPrec :: Int -> ReadS a
109 readList :: ReadS [a]
110 -- | Proposed replacement for 'readsPrec' using new-style parsers (GHC only).
111 readPrec :: ReadPrec a
112 -- | Proposed replacement for 'readList' using new-style parsers (GHC only).
113 readListPrec :: ReadPrec [a]
115 -- default definitions
116 readsPrec = readPrec_to_S readPrec
117 readList = readPrec_to_S (list readPrec) 0
118 readPrec = readS_to_Prec readsPrec
119 readListPrec = readS_to_Prec (\_ -> readList)
121 readListDefault :: Read a => ReadS [a]
122 -- ^ Use this to define the 'readList' method, if you don't want a special
123 -- case (GHC only; for other systems the default suffices).
124 readListDefault = readPrec_to_S readListPrec 0
126 readListPrecDefault :: Read a => ReadPrec [a]
127 -- ^ Use this to define the 'readListPrec' method, if you
128 -- don't want a special case (GHC only).
129 readListPrecDefault = list readPrec
131 ------------------------------------------------------------------------
134 reads :: Read a => ReadS a
135 reads = readsPrec minPrec
137 readp :: Read a => ReadP a
138 readp = readPrec_to_P readPrec minPrec
140 readEither :: Read a => String -> Either String a
142 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
144 [] -> Left "Prelude.read: no parse"
145 _ -> Left "Prelude.read: ambiguous parse"
152 read :: Read a => String -> a
153 read s = either error id (readEither s)
155 ------------------------------------------------------------------------
158 lex :: ReadS String -- As defined by H98
159 lex s = readP_to_S L.hsLex s
161 lexLitChar :: ReadS String -- As defined by H98
162 lexLitChar = readP_to_S (do { (s, _) <- P.gather L.lexChar ;
164 -- There was a skipSpaces before the P.gather L.lexChar,
165 -- but that seems inconsistent with readLitChar
167 readLitChar :: ReadS Char -- As defined by H98
168 readLitChar = readP_to_S L.lexChar
170 lexDigits :: ReadS String
171 lexDigits = readP_to_S (P.munch1 isDigit)
173 ------------------------------------------------------------------------
176 lexP :: ReadPrec L.Lexeme
177 -- ^ Parse a single lexeme
180 paren :: ReadPrec a -> ReadPrec a
181 -- ^ @(paren p)@ parses \"(P0)\"
182 -- where @p@ parses \"P0\" in precedence context zero
183 paren p = do L.Punc "(" <- lexP
188 parens :: ReadPrec a -> ReadPrec a
189 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
190 -- where @p@ parses \"P\" in the current precedence context
191 -- parses \"P0\" in precedence context zero
194 optional = p +++ mandatory
195 mandatory = paren optional
197 list :: ReadPrec a -> ReadPrec [a]
198 -- ^ @(list p)@ parses a list of things parsed by @p@,
199 -- using the usual square-bracket syntax.
202 ( do L.Punc "[" <- lexP
203 (listRest False +++ listNext)
210 "," | started -> listNext
218 choose :: [(String, ReadPrec a)] -> ReadPrec a
219 -- ^ Parse the specified lexeme and continue as specified.
220 -- Esp useful for nullary constructors; e.g.
221 -- @choose [(\"A\", return A), (\"B\", return B)]@
222 choose sps = foldr ((+++) . try_one) pfail sps
224 try_one (s,p) = do { L.Ident s' <- lexP ;
225 if s == s' then p else pfail }
229 %*********************************************************
231 \subsection{Simple instances of Read}
233 %*********************************************************
236 instance Read Char where
239 ( do L.Char c <- lexP
245 ( do L.String s <- lexP -- Looks for "foo"
248 readListPrecDefault -- Looks for ['f','o','o']
249 ) -- (more generous than H98 spec)
251 readList = readListDefault
253 instance Read Bool where
256 ( do L.Ident s <- lexP
258 "True" -> return True
259 "False" -> return False
263 readListPrec = readListPrecDefault
264 readList = readListDefault
266 instance Read Ordering where
269 ( do L.Ident s <- lexP
277 readListPrec = readListPrecDefault
278 readList = readListDefault
282 %*********************************************************
284 \subsection{Structure instances of Read: Maybe, List etc}
286 %*********************************************************
288 For structured instances of Read we start using the precedences. The
289 idea is then that 'parens (prec k p)' will fail immediately when trying
290 to parse it in a context with a higher precedence level than k. But if
291 there is one parenthesis parsed, then the required precedence level
292 drops to 0 again, and parsing inside p may succeed.
294 'appPrec' is just the precedence level of function application. So,
295 if we are parsing function application, we'd better require the
296 precedence level to be at least 'appPrec'. Otherwise, we have to put
297 parentheses around it.
299 'step' is used to increase the precedence levels inside a
300 parser, and can be used to express left- or right- associativity. For
301 example, % is defined to be left associative, so we only increase
302 precedence on the right hand side.
304 Note how step is used in for example the Maybe parser to increase the
305 precedence beyond appPrec, so that basically only literals and
306 parenthesis-like objects such as (...) and [...] can be an argument to
310 instance Read a => Read (Maybe a) where
313 (do L.Ident "Nothing" <- lexP
317 do L.Ident "Just" <- lexP
322 readListPrec = readListPrecDefault
323 readList = readListDefault
325 instance (Read a, Read b) => Read (Either a b) where
329 ( do L.Ident "Left" <- lexP
333 do L.Ident "Right" <- lexP
339 readListPrec = readListPrecDefault
340 readList = readListDefault
342 instance Read a => Read [a] where
343 readPrec = readListPrec
344 readListPrec = readListPrecDefault
345 readList = readListDefault
347 instance (Ix a, Read a, Read b) => Read (Array a b) where
348 readPrec = parens $ prec appPrec $
349 do L.Ident "array" <- lexP
350 bounds <- step readPrec
351 vals <- step readPrec
352 return (array bounds vals)
354 readListPrec = readListPrecDefault
355 readList = readListDefault
357 instance Read L.Lexeme where
359 readListPrec = readListPrecDefault
360 readList = readListDefault
364 %*********************************************************
366 \subsection{Numeric instances of Read}
368 %*********************************************************
371 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
372 -- Read a signed number
377 L.Symbol "-" -> do n <- readNumber convert
380 _ -> case convert x of
385 convertInt :: Num a => L.Lexeme -> Maybe a
386 convertInt (L.Int i) = Just (fromInteger i)
387 convertInt _ = Nothing
389 convertFrac :: Fractional a => L.Lexeme -> Maybe a
390 convertFrac (L.Int i) = Just (fromInteger i)
391 convertFrac (L.Rat r) = Just (fromRational r)
392 convertFrac _ = Nothing
394 instance Read Int where
395 readPrec = readNumber convertInt
396 readListPrec = readListPrecDefault
397 readList = readListDefault
399 instance Read Integer where
400 readPrec = readNumber convertInt
401 readListPrec = readListPrecDefault
402 readList = readListDefault
404 instance Read Float where
405 readPrec = readNumber convertFrac
406 readListPrec = readListPrecDefault
407 readList = readListDefault
409 instance Read Double where
410 readPrec = readNumber convertFrac
411 readListPrec = readListPrecDefault
412 readList = readListDefault
414 instance (Integral a, Read a) => Read (Ratio a) where
418 ( do x <- step readPrec
425 readListPrec = readListPrecDefault
426 readList = readListDefault
430 %*********************************************************
432 \subsection{Tuple instances of Read}
434 %*********************************************************
437 instance Read () where
445 readListPrec = readListPrecDefault
446 readList = readListDefault
448 instance (Read a, Read b) => Read (a,b) where
459 readListPrec = readListPrecDefault
460 readList = readListDefault
463 instance (Read a, Read b, Read c) => Read (a, b, c) where
476 readListPrec = readListPrecDefault
477 readList = readListDefault
479 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
494 readListPrec = readListPrecDefault
495 readList = readListDefault
497 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
514 readListPrec = readListPrecDefault
515 readList = readListDefault