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 readPrec :: ReadPrec a
111 readListPrec :: ReadPrec [a]
113 -- default definitions
114 readsPrec = readPrec_to_S readPrec
115 readList = readPrec_to_S (list readPrec) 0
116 readPrec = readS_to_Prec readsPrec
117 readListPrec = readS_to_Prec (\_ -> readList)
119 readListDefault :: Read a => ReadS [a]
120 -- ^ Use this to define the 'readList' method, if you
121 -- don't want a special case
122 readListDefault = readPrec_to_S readListPrec 0
124 readListPrecDefault :: Read a => ReadPrec [a]
125 -- ^ Use this to define the 'readListPrec' method, if you
126 -- don't want a special case
127 readListPrecDefault = list readPrec
129 ------------------------------------------------------------------------
132 reads :: Read a => ReadS a
133 reads = readsPrec minPrec
135 readp :: Read a => ReadP a
136 readp = readPrec_to_P readPrec minPrec
138 readEither :: Read a => String -> Either String a
140 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
142 [] -> Left "Prelude.read: no parse"
143 _ -> Left "Prelude.read: ambiguous parse"
150 read :: Read a => String -> a
151 read s = either error id (readEither s)
153 ------------------------------------------------------------------------
156 lex :: ReadS String -- As defined by H98
157 lex s = readP_to_S L.hsLex s
159 lexLitChar :: ReadS String -- As defined by H98
160 lexLitChar = readP_to_S (do { (s, _) <- P.gather L.lexChar ;
162 -- There was a skipSpaces before the P.gather L.lexChar,
163 -- but that seems inconsistent with readLitChar
165 readLitChar :: ReadS Char -- As defined by H98
166 readLitChar = readP_to_S L.lexChar
168 lexDigits :: ReadS String
169 lexDigits = readP_to_S (P.munch1 isDigit)
171 ------------------------------------------------------------------------
174 lexP :: ReadPrec L.Lexeme
175 -- ^ Parse a single lexeme
178 paren :: ReadPrec a -> ReadPrec a
179 -- ^ @(paren p)@ parses \"(P0)\"
180 -- where @p@ parses \"P0\" in precedence context zero
181 paren p = do L.Punc "(" <- lexP
186 parens :: ReadPrec a -> ReadPrec a
187 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
188 -- where @p@ parses \"P\" in the current precedence context
189 -- parses \"P0\" in precedence context zero
192 optional = p +++ mandatory
193 mandatory = paren optional
195 list :: ReadPrec a -> ReadPrec [a]
196 -- ^ @(list p)@ parses a list of things parsed by @p@,
197 -- using the usual square-bracket syntax.
200 ( do L.Punc "[" <- lexP
201 (listRest False +++ listNext)
208 "," | started -> listNext
216 choose :: [(String, ReadPrec a)] -> ReadPrec a
217 -- ^ Parse the specified lexeme and continue as specified.
218 -- Esp useful for nullary constructors; e.g.
219 -- @choose [(\"A\", return A), (\"B\", return B)]@
220 choose sps = foldr ((+++) . try_one) pfail sps
222 try_one (s,p) = do { L.Ident s' <- lexP ;
223 if s == s' then p else pfail }
227 %*********************************************************
229 \subsection{Simple instances of Read}
231 %*********************************************************
234 instance Read Char where
237 ( do L.Char c <- lexP
243 ( do L.String s <- lexP -- Looks for "foo"
246 readListPrecDefault -- Looks for ['f','o','o']
247 ) -- (more generous than H98 spec)
249 readList = readListDefault
251 instance Read Bool where
254 ( do L.Ident s <- lexP
256 "True" -> return True
257 "False" -> return False
261 readListPrec = readListPrecDefault
262 readList = readListDefault
264 instance Read Ordering where
267 ( do L.Ident s <- lexP
275 readListPrec = readListPrecDefault
276 readList = readListDefault
280 %*********************************************************
282 \subsection{Structure instances of Read: Maybe, List etc}
284 %*********************************************************
286 For structured instances of Read we start using the precedences. The
287 idea is then that 'parens (prec k p)' will fail immediately when trying
288 to parse it in a context with a higher precedence level than k. But if
289 there is one parenthesis parsed, then the required precedence level
290 drops to 0 again, and parsing inside p may succeed.
292 'appPrec' is just the precedence level of function application. So,
293 if we are parsing function application, we'd better require the
294 precedence level to be at least 'appPrec'. Otherwise, we have to put
295 parentheses around it.
297 'step' is used to increase the precedence levels inside a
298 parser, and can be used to express left- or right- associativity. For
299 example, % is defined to be left associative, so we only increase
300 precedence on the right hand side.
302 Note how step is used in for example the Maybe parser to increase the
303 precedence beyond appPrec, so that basically only literals and
304 parenthesis-like objects such as (...) and [...] can be an argument to
308 instance Read a => Read (Maybe a) where
311 (do L.Ident "Nothing" <- lexP
315 do L.Ident "Just" <- lexP
320 readListPrec = readListPrecDefault
321 readList = readListDefault
323 instance (Read a, Read b) => Read (Either a b) where
327 ( do L.Ident "Left" <- lexP
331 do L.Ident "Right" <- lexP
337 readListPrec = readListPrecDefault
338 readList = readListDefault
340 instance Read a => Read [a] where
341 readPrec = readListPrec
342 readListPrec = readListPrecDefault
343 readList = readListDefault
345 instance (Ix a, Read a, Read b) => Read (Array a b) where
346 readPrec = parens $ prec appPrec $
347 do L.Ident "array" <- lexP
348 bounds <- step readPrec
349 vals <- step readPrec
350 return (array bounds vals)
352 readListPrec = readListPrecDefault
353 readList = readListDefault
355 instance Read L.Lexeme where
357 readListPrec = readListPrecDefault
358 readList = readListDefault
362 %*********************************************************
364 \subsection{Numeric instances of Read}
366 %*********************************************************
369 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
370 -- Read a signed number
375 L.Symbol "-" -> do n <- readNumber convert
378 _ -> case convert x of
383 convertInt :: Num a => L.Lexeme -> Maybe a
384 convertInt (L.Int i) = Just (fromInteger i)
385 convertInt _ = Nothing
387 convertFrac :: Fractional a => L.Lexeme -> Maybe a
388 convertFrac (L.Int i) = Just (fromInteger i)
389 convertFrac (L.Rat r) = Just (fromRational r)
390 convertFrac _ = Nothing
392 instance Read Int where
393 readPrec = readNumber convertInt
394 readListPrec = readListPrecDefault
395 readList = readListDefault
397 instance Read Integer where
398 readPrec = readNumber convertInt
399 readListPrec = readListPrecDefault
400 readList = readListDefault
402 instance Read Float where
403 readPrec = readNumber convertFrac
404 readListPrec = readListPrecDefault
405 readList = readListDefault
407 instance Read Double where
408 readPrec = readNumber convertFrac
409 readListPrec = readListPrecDefault
410 readList = readListDefault
412 instance (Integral a, Read a) => Read (Ratio a) where
416 ( do x <- step readPrec
423 readListPrec = readListPrecDefault
424 readList = readListDefault
428 %*********************************************************
430 \subsection{Tuple instances of Read}
432 %*********************************************************
435 instance Read () where
443 readListPrec = readListPrecDefault
444 readList = readListDefault
446 instance (Read a, Read b) => Read (a,b) where
457 readListPrec = readListPrecDefault
458 readList = readListDefault
461 instance (Read a, Read b, Read c) => Read (a, b, c) where
474 readListPrec = readListPrecDefault
475 readList = readListDefault
477 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
492 readListPrec = readListPrecDefault
493 readList = readListDefault
495 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
512 readListPrec = readListPrecDefault
513 readList = readListDefault