682e9b3732e7f56cbe06e3b73752bb99e16c5023
[packages/base.git] / Text / Read.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Text.Read
7 -- Copyright : (c) The University of Glasgow 2001
8 -- License : BSD-style (see the file libraries/base/LICENSE)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : provisional
12 -- Portability : non-portable (uses Text.ParserCombinators.ReadP)
13 --
14 -- Converting strings to values.
15 --
16 -- The "Text.Read" library is the canonical library to import for
17 -- 'Read'-class facilities. For GHC only, it offers an extended and much
18 -- improved 'Read' class, which constitutes a proposed alternative to the
19 -- Haskell 2010 'Read'. In particular, writing parsers is easier, and
20 -- the parsers are much more efficient.
21 --
22 -----------------------------------------------------------------------------
23
24 module Text.Read (
25 -- * The 'Read' class
26 Read(..),
27 ReadS,
28
29 -- * Haskell 2010 functions
30 reads,
31 read,
32 readParen,
33 lex,
34
35 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
36 -- * New parsing functions
37 module Text.ParserCombinators.ReadPrec,
38 L.Lexeme(..),
39 lexP,
40 parens,
41 #endif
42 #ifdef __GLASGOW_HASKELL__
43 readListDefault,
44 readListPrecDefault,
45 readEither,
46 readMaybe
47 #endif
48
49 ) where
50
51 #ifdef __GLASGOW_HASKELL__
52 import GHC.Base
53 import GHC.Read
54 import Data.Either
55 import Data.Maybe
56 import Text.ParserCombinators.ReadP as P
57 #endif
58 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
59 import Text.ParserCombinators.ReadPrec
60 import qualified Text.Read.Lex as L
61 #endif
62
63 #ifdef __HUGS__
64 -- copied from GHC.Read
65
66 lexP :: ReadPrec L.Lexeme
67 lexP = lift L.lex
68
69 parens :: ReadPrec a -> ReadPrec a
70 parens p = optional
71 where
72 optional = p +++ mandatory
73 mandatory = do
74 L.Punc "(" <- lexP
75 x <- reset optional
76 L.Punc ")" <- lexP
77 return x
78 #endif
79
80 #ifdef __GLASGOW_HASKELL__
81 ------------------------------------------------------------------------
82 -- utility functions
83
84 -- | equivalent to 'readsPrec' with a precedence of 0.
85 reads :: Read a => ReadS a
86 reads = readsPrec minPrec
87
88 -- | Parse a string using the 'Read' instance.
89 -- Succeeds if there is exactly one valid result.
90 -- A 'Left' value indicates a parse error.
91 readEither :: Read a => String -> Either String a
92 readEither s =
93 case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
94 [x] -> Right x
95 [] -> Left "Prelude.read: no parse"
96 _ -> Left "Prelude.read: ambiguous parse"
97 where
98 read' =
99 do x <- readPrec
100 lift P.skipSpaces
101 return x
102
103 -- | Parse a string using the 'Read' instance.
104 -- Succeeds if there is exactly one valid result.
105 readMaybe :: Read a => String -> Maybe a
106 readMaybe s = case readEither s of
107 Left _ -> Nothing
108 Right a -> Just a
109
110 -- | The 'read' function reads input from a string, which must be
111 -- completely consumed by the input process.
112 read :: Read a => String -> a
113 read s = either error id (readEither s)
114 #endif
115