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 -- | Read a string representation of a character, using Haskell
159 -- source-language escape conventions. For example:
161 -- > lexLitChar "\\nHello" = [("\\n", "Hello")]
163 lexLitChar :: ReadS String -- As defined by H98
164 lexLitChar = readP_to_S (do { (s, _) <- P.gather L.lexChar ;
166 -- There was a skipSpaces before the P.gather L.lexChar,
167 -- but that seems inconsistent with readLitChar
169 -- | Read a string representation of a character, using Haskell
170 -- source-language escape conventions, and convert it to the character
171 -- that it encodes. For example:
173 -- > readLitChar "\\nHello" = [('\n', "Hello")]
175 readLitChar :: ReadS Char -- As defined by H98
176 readLitChar = readP_to_S L.lexChar
178 -- | Reads a non-empty string of decimal digits.
179 lexDigits :: ReadS String
180 lexDigits = readP_to_S (P.munch1 isDigit)
182 ------------------------------------------------------------------------
185 lexP :: ReadPrec L.Lexeme
186 -- ^ Parse a single 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 L.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]
207 -- ^ @(list p)@ parses a list of things parsed by @p@,
208 -- using the usual square-bracket syntax.
211 ( do L.Punc "[" <- lexP
212 (listRest False +++ listNext)
219 "," | started -> listNext
227 choose :: [(String, ReadPrec a)] -> ReadPrec a
228 -- ^ Parse the specified lexeme and continue as specified.
229 -- Esp useful for nullary constructors; e.g.
230 -- @choose [(\"A\", return A), (\"B\", return B)]@
231 choose sps = foldr ((+++) . try_one) pfail sps
233 try_one (s,p) = do { L.Ident s' <- lexP ;
234 if s == s' then p else pfail }
238 %*********************************************************
240 \subsection{Simple instances of Read}
242 %*********************************************************
245 instance Read Char where
248 ( do L.Char c <- lexP
254 ( do L.String s <- lexP -- Looks for "foo"
257 readListPrecDefault -- Looks for ['f','o','o']
258 ) -- (more generous than H98 spec)
260 readList = readListDefault
262 instance Read Bool where
265 ( do L.Ident s <- lexP
267 "True" -> return True
268 "False" -> return False
272 readListPrec = readListPrecDefault
273 readList = readListDefault
275 instance Read Ordering where
278 ( do L.Ident s <- lexP
286 readListPrec = readListPrecDefault
287 readList = readListDefault
291 %*********************************************************
293 \subsection{Structure instances of Read: Maybe, List etc}
295 %*********************************************************
297 For structured instances of Read we start using the precedences. The
298 idea is then that 'parens (prec k p)' will fail immediately when trying
299 to parse it in a context with a higher precedence level than k. But if
300 there is one parenthesis parsed, then the required precedence level
301 drops to 0 again, and parsing inside p may succeed.
303 'appPrec' is just the precedence level of function application. So,
304 if we are parsing function application, we'd better require the
305 precedence level to be at least 'appPrec'. Otherwise, we have to put
306 parentheses around it.
308 'step' is used to increase the precedence levels inside a
309 parser, and can be used to express left- or right- associativity. For
310 example, % is defined to be left associative, so we only increase
311 precedence on the right hand side.
313 Note how step is used in for example the Maybe parser to increase the
314 precedence beyond appPrec, so that basically only literals and
315 parenthesis-like objects such as (...) and [...] can be an argument to
319 instance Read a => Read (Maybe a) where
322 (do L.Ident "Nothing" <- lexP
326 do L.Ident "Just" <- lexP
331 readListPrec = readListPrecDefault
332 readList = readListDefault
334 instance (Read a, Read b) => Read (Either a b) where
338 ( do L.Ident "Left" <- lexP
342 do L.Ident "Right" <- lexP
348 readListPrec = readListPrecDefault
349 readList = readListDefault
351 instance Read a => Read [a] where
352 readPrec = readListPrec
353 readListPrec = readListPrecDefault
354 readList = readListDefault
356 instance (Ix a, Read a, Read b) => Read (Array a b) where
357 readPrec = parens $ prec appPrec $
358 do L.Ident "array" <- lexP
359 bounds <- step readPrec
360 vals <- step readPrec
361 return (array bounds vals)
363 readListPrec = readListPrecDefault
364 readList = readListDefault
366 instance Read L.Lexeme where
368 readListPrec = readListPrecDefault
369 readList = readListDefault
373 %*********************************************************
375 \subsection{Numeric instances of Read}
377 %*********************************************************
380 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
381 -- Read a signed number
386 L.Symbol "-" -> do n <- readNumber convert
389 _ -> case convert x of
394 convertInt :: Num a => L.Lexeme -> Maybe a
395 convertInt (L.Int i) = Just (fromInteger i)
396 convertInt _ = Nothing
398 convertFrac :: Fractional a => L.Lexeme -> Maybe a
399 convertFrac (L.Int i) = Just (fromInteger i)
400 convertFrac (L.Rat r) = Just (fromRational r)
401 convertFrac _ = Nothing
403 instance Read Int where
404 readPrec = readNumber convertInt
405 readListPrec = readListPrecDefault
406 readList = readListDefault
408 instance Read Integer where
409 readPrec = readNumber convertInt
410 readListPrec = readListPrecDefault
411 readList = readListDefault
413 instance Read Float where
414 readPrec = readNumber convertFrac
415 readListPrec = readListPrecDefault
416 readList = readListDefault
418 instance Read Double where
419 readPrec = readNumber convertFrac
420 readListPrec = readListPrecDefault
421 readList = readListDefault
423 instance (Integral a, Read a) => Read (Ratio a) where
427 ( do x <- step readPrec
434 readListPrec = readListPrecDefault
435 readList = readListDefault
439 %*********************************************************
441 \subsection{Tuple instances of Read}
443 %*********************************************************
446 instance Read () where
454 readListPrec = readListPrecDefault
455 readList = readListDefault
457 instance (Read a, Read b) => Read (a,b) where
468 readListPrec = readListPrecDefault
469 readList = readListDefault
472 instance (Read a, Read b, Read c) => Read (a, b, c) where
485 readListPrec = readListPrecDefault
486 readList = readListDefault
488 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
503 readListPrec = readListPrecDefault
504 readList = readListDefault
506 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
523 readListPrec = readListPrecDefault
524 readList = readListDefault