2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 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
56 import qualified Text.Read.Lex as L
57 -- Lex exports 'lex', which is also defined here,
58 -- hence the qualified import.
59 -- We can't import *anything* unqualified, because that
62 import Text.ParserCombinators.ReadPrec
67 import {-# SOURCE #-} GHC.Err ( error )
68 import {-# SOURCE #-} GHC.Unicode ( isDigit )
80 readParen :: Bool -> ReadS a -> ReadS a
81 -- A Haskell 98 function
82 readParen b g = if b then mandatory else optional
83 where optional r = g r ++ mandatory r
92 %*********************************************************
94 \subsection{The @Read@ class}
96 %*********************************************************
99 ------------------------------------------------------------------------
103 readsPrec :: Int -> ReadS a
104 readList :: ReadS [a]
105 -- | Proposed replacement for 'readsPrec' using new-style parsers (GHC only).
106 readPrec :: ReadPrec a
107 -- | Proposed replacement for 'readList' using new-style parsers (GHC only).
108 readListPrec :: ReadPrec [a]
110 -- default definitions
111 readsPrec = readPrec_to_S readPrec
112 readList = readPrec_to_S (list readPrec) 0
113 readPrec = readS_to_Prec readsPrec
114 readListPrec = readS_to_Prec (\_ -> readList)
116 readListDefault :: Read a => ReadS [a]
117 -- ^ Use this to define the 'readList' method, if you don't want a special
118 -- case (GHC only; for other systems the default suffices).
119 readListDefault = readPrec_to_S readListPrec 0
121 readListPrecDefault :: Read a => ReadPrec [a]
122 -- ^ Use this to define the 'readListPrec' method, if you
123 -- don't want a special case (GHC only).
124 readListPrecDefault = list readPrec
126 ------------------------------------------------------------------------
129 reads :: Read a => ReadS a
130 reads = readsPrec minPrec
132 readp :: Read a => ReadP a
133 readp = readPrec_to_P readPrec minPrec
135 readEither :: Read a => String -> Either String a
137 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
139 [] -> Left "Prelude.read: no parse"
140 _ -> Left "Prelude.read: ambiguous parse"
147 read :: Read a => String -> a
148 read s = either error id (readEither s)
150 ------------------------------------------------------------------------
153 lex :: ReadS String -- As defined by H98
154 lex s = readP_to_S L.hsLex s
156 lexLitChar :: ReadS String -- As defined by H98
157 lexLitChar = readP_to_S (do { (s, _) <- P.gather L.lexChar ;
159 -- There was a skipSpaces before the P.gather L.lexChar,
160 -- but that seems inconsistent with readLitChar
162 readLitChar :: ReadS Char -- As defined by H98
163 readLitChar = readP_to_S L.lexChar
165 lexDigits :: ReadS String
166 lexDigits = readP_to_S (P.munch1 isDigit)
168 ------------------------------------------------------------------------
171 lexP :: ReadPrec L.Lexeme
172 -- ^ Parse a single lexeme
175 paren :: ReadPrec a -> ReadPrec a
176 -- ^ @(paren p)@ parses \"(P0)\"
177 -- where @p@ parses \"P0\" in precedence context zero
178 paren p = do L.Punc "(" <- lexP
183 parens :: ReadPrec a -> ReadPrec a
184 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
185 -- where @p@ parses \"P\" in the current precedence context
186 -- parses \"P0\" in precedence context zero
189 optional = p +++ mandatory
190 mandatory = paren optional
192 list :: ReadPrec a -> ReadPrec [a]
193 -- ^ @(list p)@ parses a list of things parsed by @p@,
194 -- using the usual square-bracket syntax.
197 ( do L.Punc "[" <- lexP
198 (listRest False +++ listNext)
205 "," | started -> listNext
213 choose :: [(String, ReadPrec a)] -> ReadPrec a
214 -- ^ Parse the specified lexeme and continue as specified.
215 -- Esp useful for nullary constructors; e.g.
216 -- @choose [(\"A\", return A), (\"B\", return B)]@
217 choose sps = foldr ((+++) . try_one) pfail sps
219 try_one (s,p) = do { L.Ident s' <- lexP ;
220 if s == s' then p else pfail }
224 %*********************************************************
226 \subsection{Simple instances of Read}
228 %*********************************************************
231 instance Read Char where
234 ( do L.Char c <- lexP
240 ( do L.String s <- lexP -- Looks for "foo"
243 readListPrecDefault -- Looks for ['f','o','o']
244 ) -- (more generous than H98 spec)
246 readList = readListDefault
248 instance Read Bool where
251 ( do L.Ident s <- lexP
253 "True" -> return True
254 "False" -> return False
258 readListPrec = readListPrecDefault
259 readList = readListDefault
261 instance Read Ordering where
264 ( do L.Ident s <- lexP
272 readListPrec = readListPrecDefault
273 readList = readListDefault
277 %*********************************************************
279 \subsection{Structure instances of Read: Maybe, List etc}
281 %*********************************************************
283 For structured instances of Read we start using the precedences. The
284 idea is then that 'parens (prec k p)' will fail immediately when trying
285 to parse it in a context with a higher precedence level than k. But if
286 there is one parenthesis parsed, then the required precedence level
287 drops to 0 again, and parsing inside p may succeed.
289 'appPrec' is just the precedence level of function application. So,
290 if we are parsing function application, we'd better require the
291 precedence level to be at least 'appPrec'. Otherwise, we have to put
292 parentheses around it.
294 'step' is used to increase the precedence levels inside a
295 parser, and can be used to express left- or right- associativity. For
296 example, % is defined to be left associative, so we only increase
297 precedence on the right hand side.
299 Note how step is used in for example the Maybe parser to increase the
300 precedence beyond appPrec, so that basically only literals and
301 parenthesis-like objects such as (...) and [...] can be an argument to
305 instance Read a => Read (Maybe a) where
308 (do L.Ident "Nothing" <- lexP
312 do L.Ident "Just" <- lexP
317 readListPrec = readListPrecDefault
318 readList = readListDefault
320 instance (Read a, Read b) => Read (Either a b) where
324 ( do L.Ident "Left" <- lexP
328 do L.Ident "Right" <- lexP
334 readListPrec = readListPrecDefault
335 readList = readListDefault
337 instance Read a => Read [a] where
338 readPrec = readListPrec
339 readListPrec = readListPrecDefault
340 readList = readListDefault
342 instance (Ix a, Read a, Read b) => Read (Array a b) where
343 readPrec = parens $ prec appPrec $
344 do L.Ident "array" <- lexP
345 bounds <- step readPrec
346 vals <- step readPrec
347 return (array bounds vals)
349 readListPrec = readListPrecDefault
350 readList = readListDefault
352 instance Read L.Lexeme where
354 readListPrec = readListPrecDefault
355 readList = readListDefault
359 %*********************************************************
361 \subsection{Numeric instances of Read}
363 %*********************************************************
366 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
367 -- Read a signed number
372 L.Symbol "-" -> do n <- readNumber convert
375 _ -> case convert x of
380 convertInt :: Num a => L.Lexeme -> Maybe a
381 convertInt (L.Int i) = Just (fromInteger i)
382 convertInt _ = Nothing
384 convertFrac :: Fractional a => L.Lexeme -> Maybe a
385 convertFrac (L.Int i) = Just (fromInteger i)
386 convertFrac (L.Rat r) = Just (fromRational r)
387 convertFrac _ = Nothing
389 instance Read Int where
390 readPrec = readNumber convertInt
391 readListPrec = readListPrecDefault
392 readList = readListDefault
394 instance Read Integer where
395 readPrec = readNumber convertInt
396 readListPrec = readListPrecDefault
397 readList = readListDefault
399 instance Read Float where
400 readPrec = readNumber convertFrac
401 readListPrec = readListPrecDefault
402 readList = readListDefault
404 instance Read Double where
405 readPrec = readNumber convertFrac
406 readListPrec = readListPrecDefault
407 readList = readListDefault
409 instance (Integral a, Read a) => Read (Ratio a) where
413 ( do x <- step readPrec
420 readListPrec = readListPrecDefault
421 readList = readListDefault
425 %*********************************************************
427 \subsection{Tuple instances of Read}
429 %*********************************************************
432 instance Read () where
440 readListPrec = readListPrecDefault
441 readList = readListDefault
443 instance (Read a, Read b) => Read (a,b) where
454 readListPrec = readListPrecDefault
455 readList = readListDefault
458 instance (Read a, Read b, Read c) => Read (a, b, c) where
471 readListPrec = readListPrecDefault
472 readList = readListDefault
474 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
489 readListPrec = readListPrecDefault
490 readList = readListDefault
492 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
509 readListPrec = readListPrecDefault
510 readList = readListDefault