1 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : non-portable (uses Text.ParserCombinators.ReadP)
13 -- Converting strings to values.
15 -- The "Text.Read" library is the canonical library to import for
16 -- 'Read'-class facilities. For GHC only, it offers an extended and much
17 -- improved 'Read' class, which constitutes a proposed alternative to the
18 -- Haskell 98 'Read'. In particular, writing parsers is easier, and
19 -- the parsers are much more efficient.
21 -----------------------------------------------------------------------------
25 Read(..), -- The Read class
26 ReadS, -- String -> Maybe (a,String)
28 -- * Haskell 98 functions
29 reads, -- :: (Read a) => ReadS a
30 read, -- :: (Read a) => String -> a
31 readParen, -- :: Bool -> ReadS a -> ReadS a
32 lex, -- :: ReadS String
34 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
35 -- * New parsing functions
36 module Text.ParserCombinators.ReadPrec,
38 lexP, -- :: ReadPrec Lexeme
39 parens, -- :: ReadPrec a -> ReadPrec a
41 #ifdef __GLASGOW_HASKELL__
42 readListDefault, -- :: Read a => ReadS [a]
43 readListPrecDefault, -- :: Read a => ReadPrec [a]
48 #ifdef __GLASGOW_HASKELL__
52 import Text.ParserCombinators.ReadP as P
54 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
55 import Text.ParserCombinators.ReadPrec
56 import qualified Text.Read.Lex as L
60 -- copied from GHC.Read
62 lexP :: ReadPrec L.Lexeme
65 parens :: ReadPrec a -> ReadPrec a
68 optional = p +++ mandatory
76 #ifdef __GLASGOW_HASKELL__
77 ------------------------------------------------------------------------
80 -- | equivalent to 'readsPrec' with a precedence of 0.
81 reads :: Read a => ReadS a
82 reads = readsPrec minPrec
84 readEither :: Read a => String -> Either String a
86 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
88 [] -> Left "Prelude.read: no parse"
89 _ -> Left "Prelude.read: ambiguous parse"
96 -- | The 'read' function reads input from a string, which must be
97 -- completely consumed by the input process.
98 read :: Read a => String -> a
99 read s = either error id (readEither s)