bcb2d0930ab395ab8f70bc0cfc99c723b5a906f9
[haskell-directory.git] / Text / Read.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Text.Read
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  non-portable (uses Text.ParserCombinators.ReadP)
11 --
12 -- Converting strings to values.
13 --
14 -- The "Text.Read" library is the canonical library to import for
15 -- 'Read'-class facilities.  For GHC only, it offers an extended and much
16 -- improved 'Read' class, which constitutes a proposed alternative to the 
17 -- Haskell 98 'Read'.  In particular, writing parsers is easier, and
18 -- the parsers are much more efficient.
19 --
20 -----------------------------------------------------------------------------
21
22 module Text.Read (
23    -- * The 'Read' class
24    Read(..),            -- The Read class
25    ReadS,               -- String -> Maybe (a,String)
26
27    -- * Haskell 98 functions
28    reads,               -- :: (Read a) => ReadS a
29    read,                -- :: (Read a) => String -> a
30    readParen,           -- :: Bool -> ReadS a -> ReadS a
31    lex,                 -- :: ReadS String
32
33 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
34    -- * New parsing functions
35    module Text.ParserCombinators.ReadPrec,
36    L.Lexeme(..),        
37    lexP,                -- :: ReadPrec Lexeme
38    parens,              -- :: ReadPrec a -> ReadPrec a
39 #endif
40 #ifdef __GLASGOW_HASKELL__
41    readListDefault,     -- :: Read a => ReadS [a]
42    readListPrecDefault, -- :: Read a => ReadPrec [a]
43 #endif
44
45  ) where
46
47 #ifdef __GLASGOW_HASKELL__
48 import GHC.Read
49 #endif   
50 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
51 import Text.ParserCombinators.ReadPrec
52 import qualified Text.Read.Lex as L
53 #endif   
54
55 #ifdef __HUGS__
56 -- copied from GHC.Read
57
58 lexP :: ReadPrec L.Lexeme
59 lexP = lift L.lex
60
61 parens :: ReadPrec a -> ReadPrec a
62 parens p = optional
63  where
64   optional  = p +++ mandatory
65   mandatory = do
66     L.Punc "(" <- lexP
67     x          <- reset optional
68     L.Punc ")" <- lexP
69     return x
70 #endif