5ab8877efc080a44a955175fb1f678e5f79a149d
[ghc-base.git] / Text / Read.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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.Base
49 import GHC.Read
50 import Data.Either
51 import Text.ParserCombinators.ReadP as P
52 #endif
53 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
54 import Text.ParserCombinators.ReadPrec
55 import qualified Text.Read.Lex as L
56 #endif
57
58 #ifdef __HUGS__
59 -- copied from GHC.Read
60
61 lexP :: ReadPrec L.Lexeme
62 lexP = lift L.lex
63
64 parens :: ReadPrec a -> ReadPrec a
65 parens p = optional
66  where
67   optional  = p +++ mandatory
68   mandatory = do
69     L.Punc "(" <- lexP
70     x          <- reset optional
71     L.Punc ")" <- lexP
72     return x
73 #endif
74
75 #ifdef __GLASGOW_HASKELL__
76 ------------------------------------------------------------------------
77 -- utility functions
78
79 -- | equivalent to 'readsPrec' with a precedence of 0.
80 reads :: Read a => ReadS a
81 reads = readsPrec minPrec
82
83 readEither :: Read a => String -> Either String a
84 readEither s =
85   case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
86     [x] -> Right x
87     []  -> Left "Prelude.read: no parse"
88     _   -> Left "Prelude.read: ambiguous parse"
89  where
90   read' =
91     do x <- readPrec
92        lift P.skipSpaces
93        return x
94
95 -- | The 'read' function reads input from a string, which must be
96 -- completely consumed by the input process.
97 read :: Read a => String -> a
98 read s = either error id (readEither s)
99 #endif
100