1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : non-portable (uses Text.ParserCombinators.ReadP)
12 -- Converting strings to values.
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.
20 -----------------------------------------------------------------------------
24 Read(..), -- The Read class
25 ReadS, -- String -> Maybe (a,String)
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
33 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
34 -- * New parsing functions
35 module Text.ParserCombinators.ReadPrec,
37 lexP, -- :: ReadPrec Lexeme
38 parens, -- :: ReadPrec a -> ReadPrec a
40 #ifdef __GLASGOW_HASKELL__
41 readListDefault, -- :: Read a => ReadS [a]
42 readListPrecDefault, -- :: Read a => ReadPrec [a]
47 #ifdef __GLASGOW_HASKELL__
51 import Text.ParserCombinators.ReadP as P
53 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
54 import Text.ParserCombinators.ReadPrec
55 import qualified Text.Read.Lex as L
59 -- copied from GHC.Read
61 lexP :: ReadPrec L.Lexeme
64 parens :: ReadPrec a -> ReadPrec a
67 optional = p +++ mandatory
75 #ifdef __GLASGOW_HASKELL__
76 ------------------------------------------------------------------------
79 -- | equivalent to 'readsPrec' with a precedence of 0.
80 reads :: Read a => ReadS a
81 reads = readsPrec minPrec
83 readEither :: Read a => String -> Either String a
85 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
87 [] -> Left "Prelude.read: no parse"
88 _ -> Left "Prelude.read: ambiguous parse"
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)