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 lexDigits :: ReadS String
179 lexDigits = readP_to_S (P.munch1 isDigit)
181 ------------------------------------------------------------------------
184 lexP :: ReadPrec L.Lexeme
185 -- ^ Parse a single lexeme
188 paren :: ReadPrec a -> ReadPrec a
189 -- ^ @(paren p)@ parses \"(P0)\"
190 -- where @p@ parses \"P0\" in precedence context zero
191 paren p = do L.Punc "(" <- lexP
196 parens :: ReadPrec a -> ReadPrec a
197 -- ^ @(parens p)@ parses \"P\", \"(P0)\", \"((P0))\", etc,
198 -- where @p@ parses \"P\" in the current precedence context
199 -- parses \"P0\" in precedence context zero
202 optional = p +++ mandatory
203 mandatory = paren optional
205 list :: ReadPrec a -> ReadPrec [a]
206 -- ^ @(list p)@ parses a list of things parsed by @p@,
207 -- using the usual square-bracket syntax.
210 ( do L.Punc "[" <- lexP
211 (listRest False +++ listNext)
218 "," | started -> listNext
226 choose :: [(String, ReadPrec a)] -> ReadPrec a
227 -- ^ Parse the specified lexeme and continue as specified.
228 -- Esp useful for nullary constructors; e.g.
229 -- @choose [(\"A\", return A), (\"B\", return B)]@
230 choose sps = foldr ((+++) . try_one) pfail sps
232 try_one (s,p) = do { L.Ident s' <- lexP ;
233 if s == s' then p else pfail }
237 %*********************************************************
239 \subsection{Simple instances of Read}
241 %*********************************************************
244 instance Read Char where
247 ( do L.Char c <- lexP
253 ( do L.String s <- lexP -- Looks for "foo"
256 readListPrecDefault -- Looks for ['f','o','o']
257 ) -- (more generous than H98 spec)
259 readList = readListDefault
261 instance Read Bool where
264 ( do L.Ident s <- lexP
266 "True" -> return True
267 "False" -> return False
271 readListPrec = readListPrecDefault
272 readList = readListDefault
274 instance Read Ordering where
277 ( do L.Ident s <- lexP
285 readListPrec = readListPrecDefault
286 readList = readListDefault
290 %*********************************************************
292 \subsection{Structure instances of Read: Maybe, List etc}
294 %*********************************************************
296 For structured instances of Read we start using the precedences. The
297 idea is then that 'parens (prec k p)' will fail immediately when trying
298 to parse it in a context with a higher precedence level than k. But if
299 there is one parenthesis parsed, then the required precedence level
300 drops to 0 again, and parsing inside p may succeed.
302 'appPrec' is just the precedence level of function application. So,
303 if we are parsing function application, we'd better require the
304 precedence level to be at least 'appPrec'. Otherwise, we have to put
305 parentheses around it.
307 'step' is used to increase the precedence levels inside a
308 parser, and can be used to express left- or right- associativity. For
309 example, % is defined to be left associative, so we only increase
310 precedence on the right hand side.
312 Note how step is used in for example the Maybe parser to increase the
313 precedence beyond appPrec, so that basically only literals and
314 parenthesis-like objects such as (...) and [...] can be an argument to
318 instance Read a => Read (Maybe a) where
321 (do L.Ident "Nothing" <- lexP
325 do L.Ident "Just" <- lexP
330 readListPrec = readListPrecDefault
331 readList = readListDefault
333 instance (Read a, Read b) => Read (Either a b) where
337 ( do L.Ident "Left" <- lexP
341 do L.Ident "Right" <- lexP
347 readListPrec = readListPrecDefault
348 readList = readListDefault
350 instance Read a => Read [a] where
351 readPrec = readListPrec
352 readListPrec = readListPrecDefault
353 readList = readListDefault
355 instance (Ix a, Read a, Read b) => Read (Array a b) where
356 readPrec = parens $ prec appPrec $
357 do L.Ident "array" <- lexP
358 bounds <- step readPrec
359 vals <- step readPrec
360 return (array bounds vals)
362 readListPrec = readListPrecDefault
363 readList = readListDefault
365 instance Read L.Lexeme where
367 readListPrec = readListPrecDefault
368 readList = readListDefault
372 %*********************************************************
374 \subsection{Numeric instances of Read}
376 %*********************************************************
379 readNumber :: Num a => (L.Lexeme -> Maybe a) -> ReadPrec a
380 -- Read a signed number
385 L.Symbol "-" -> do n <- readNumber convert
388 _ -> case convert x of
393 convertInt :: Num a => L.Lexeme -> Maybe a
394 convertInt (L.Int i) = Just (fromInteger i)
395 convertInt _ = Nothing
397 convertFrac :: Fractional a => L.Lexeme -> Maybe a
398 convertFrac (L.Int i) = Just (fromInteger i)
399 convertFrac (L.Rat r) = Just (fromRational r)
400 convertFrac _ = Nothing
402 instance Read Int where
403 readPrec = readNumber convertInt
404 readListPrec = readListPrecDefault
405 readList = readListDefault
407 instance Read Integer where
408 readPrec = readNumber convertInt
409 readListPrec = readListPrecDefault
410 readList = readListDefault
412 instance Read Float where
413 readPrec = readNumber convertFrac
414 readListPrec = readListPrecDefault
415 readList = readListDefault
417 instance Read Double where
418 readPrec = readNumber convertFrac
419 readListPrec = readListPrecDefault
420 readList = readListDefault
422 instance (Integral a, Read a) => Read (Ratio a) where
426 ( do x <- step readPrec
433 readListPrec = readListPrecDefault
434 readList = readListDefault
438 %*********************************************************
440 \subsection{Tuple instances of Read}
442 %*********************************************************
445 instance Read () where
453 readListPrec = readListPrecDefault
454 readList = readListDefault
456 instance (Read a, Read b) => Read (a,b) where
467 readListPrec = readListPrecDefault
468 readList = readListDefault
471 instance (Read a, Read b, Read c) => Read (a, b, c) where
484 readListPrec = readListPrecDefault
485 readList = readListDefault
487 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
502 readListPrec = readListPrecDefault
503 readList = readListDefault
505 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
522 readListPrec = readListPrecDefault
523 readList = readListDefault