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 )
69 import {-# SOURCE #-} GHC.Unicode ( isDigit )
82 readParen :: Bool -> ReadS a -> ReadS a
83 -- A Haskell 98 function
84 readParen b g = if b then mandatory else optional
85 where optional r = g r ++ mandatory r
94 %*********************************************************
96 \subsection{The @Read@ class}
98 %*********************************************************
101 ------------------------------------------------------------------------
105 readsPrec :: Int -> ReadS a
106 readList :: ReadS [a]
107 -- | Proposed replacement for 'readsPrec' using new-style parsers (GHC only).
108 readPrec :: ReadPrec a
109 -- | Proposed replacement for 'readList' using new-style parsers (GHC only).
110 readListPrec :: ReadPrec [a]
112 -- default definitions
113 readsPrec = readPrec_to_S readPrec
114 readList = readPrec_to_S (list readPrec) 0
115 readPrec = readS_to_Prec readsPrec
116 readListPrec = readS_to_Prec (\_ -> readList)
118 readListDefault :: Read a => ReadS [a]
119 -- ^ Use this to define the 'readList' method, if you don't want a special
120 -- case (GHC only; for other systems the default suffices).
121 readListDefault = readPrec_to_S readListPrec 0
123 readListPrecDefault :: Read a => ReadPrec [a]
124 -- ^ Use this to define the 'readListPrec' method, if you
125 -- don't want a special case (GHC only).
126 readListPrecDefault = list readPrec
128 ------------------------------------------------------------------------
131 reads :: Read a => ReadS a
132 reads = readsPrec minPrec
134 readp :: Read a => ReadP a
135 readp = readPrec_to_P readPrec minPrec
137 readEither :: Read a => String -> Either String a
139 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
141 [] -> Left "Prelude.read: no parse"
142 _ -> Left "Prelude.read: ambiguous parse"
149 read :: Read a => String -> a
150 read s = either error id (readEither s)
152 ------------------------------------------------------------------------
155 lex :: ReadS String -- As defined by H98
156 lex s = readP_to_S L.hsLex s
158 lexLitChar :: ReadS String -- As defined by H98
159 lexLitChar = readP_to_S (do { (s, _) <- P.gather L.lexChar ;
161 -- There was a skipSpaces before the P.gather L.lexChar,
162 -- but that seems inconsistent with readLitChar
164 readLitChar :: ReadS Char -- As defined by H98
165 readLitChar = readP_to_S L.lexChar
167 lexDigits :: ReadS String
168 lexDigits = readP_to_S (P.munch1 isDigit)
170 ------------------------------------------------------------------------
173 lexP :: ReadPrec L.Lexeme
174 -- ^ Parse a single lexeme
177 paren :: ReadPrec a -> ReadPrec a
178 -- ^ @(paren p)@ parses \"(P0)\"
179 -- where @p@ parses \"P0\" in precedence context zero
180 paren p = do L.Punc "(" <- lexP
185 parens :: ReadPrec a -> ReadPrec a
186 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
187 -- where @p@ parses \"P\" in the current precedence context
188 -- parses \"P0\" in precedence context zero
191 optional = p +++ mandatory
192 mandatory = paren optional
194 list :: ReadPrec a -> ReadPrec [a]
195 -- ^ @(list p)@ parses a list of things parsed by @p@,
196 -- using the usual square-bracket syntax.
199 ( do L.Punc "[" <- lexP
200 (listRest False +++ listNext)
207 "," | started -> listNext
215 choose :: [(String, ReadPrec a)] -> ReadPrec a
216 -- ^ Parse the specified lexeme and continue as specified.
217 -- Esp useful for nullary constructors; e.g.
218 -- @choose [(\"A\", return A), (\"B\", return B)]@
219 choose sps = foldr ((+++) . try_one) pfail sps
221 try_one (s,p) = do { L.Ident s' <- lexP ;
222 if s == s' then p else pfail }
226 %*********************************************************
228 \subsection{Simple instances of Read}
230 %*********************************************************
233 instance Read Char where
236 ( do L.Char c <- lexP
242 ( do L.String s <- lexP -- Looks for "foo"
245 readListPrecDefault -- Looks for ['f','o','o']
246 ) -- (more generous than H98 spec)
248 readList = readListDefault
250 instance Read Bool where
253 ( do L.Ident s <- lexP
255 "True" -> return True
256 "False" -> return False
260 readListPrec = readListPrecDefault
261 readList = readListDefault
263 instance Read Ordering where
266 ( do L.Ident s <- lexP
274 readListPrec = readListPrecDefault
275 readList = readListDefault
279 %*********************************************************
281 \subsection{Structure instances of Read: Maybe, List etc}
283 %*********************************************************
285 For structured instances of Read we start using the precedences. The
286 idea is then that 'parens (prec k p)' will fail immediately when trying
287 to parse it in a context with a higher precedence level than k. But if
288 there is one parenthesis parsed, then the required precedence level
289 drops to 0 again, and parsing inside p may succeed.
291 'appPrec' is just the precedence level of function application. So,
292 if we are parsing function application, we'd better require the
293 precedence level to be at least 'appPrec'. Otherwise, we have to put
294 parentheses around it.
296 'step' is used to increase the precedence levels inside a
297 parser, and can be used to express left- or right- associativity. For
298 example, % is defined to be left associative, so we only increase
299 precedence on the right hand side.
301 Note how step is used in for example the Maybe parser to increase the
302 precedence beyond appPrec, so that basically only literals and
303 parenthesis-like objects such as (...) and [...] can be an argument to
307 instance Read a => Read (Maybe a) where
310 (do L.Ident "Nothing" <- lexP
314 do L.Ident "Just" <- lexP
319 readListPrec = readListPrecDefault
320 readList = readListDefault
322 instance (Read a, Read b) => Read (Either a b) where
326 ( do L.Ident "Left" <- lexP
330 do L.Ident "Right" <- lexP
336 readListPrec = readListPrecDefault
337 readList = readListDefault
339 instance Read a => Read [a] where
340 readPrec = readListPrec
341 readListPrec = readListPrecDefault
342 readList = readListDefault
344 instance (Ix a, Read a, Read b) => Read (Array a b) where
345 readPrec = parens $ prec appPrec $
346 do L.Ident "array" <- lexP
347 bounds <- step readPrec
348 vals <- step readPrec
349 return (array bounds vals)
351 readListPrec = readListPrecDefault
352 readList = readListDefault
354 instance Read L.Lexeme where
356 readListPrec = readListPrecDefault
357 readList = readListDefault
361 %*********************************************************
363 \subsection{Numeric instances of Read}
365 %*********************************************************
368 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
369 -- Read a signed number
374 L.Symbol "-" -> do n <- readNumber convert
377 _ -> case convert x of
382 convertInt :: Num a => L.Lexeme -> Maybe a
383 convertInt (L.Int i) = Just (fromInteger i)
384 convertInt _ = Nothing
386 convertFrac :: Fractional a => L.Lexeme -> Maybe a
387 convertFrac (L.Int i) = Just (fromInteger i)
388 convertFrac (L.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