@@ -24,16 +24,18 @@ module Data.Text.Read
26import Control.Monad (liftM)
26import Control.Monad (liftM)
27−import Data.Char (isDigit, isHexDigit, ord)
27+import Data.Char (isDigit, isHexDigit)
28import Data.Int (Int8, Int16, Int32, Int64)
28import Data.Int (Int8, Int16, Int32, Int64)
29import Data.Ratio ((%))
29import Data.Ratio ((%))
30import Data.Text as T
30import Data.Text as T
31import Data.Text.Internal.Private (span_)
31import Data.Text.Internal.Private (span_)
32+import Data.Text.Internal.Read
32import Data.Word (Word, Word8, Word16, Word32, Word64)
33import Data.Word (Word, Word8, Word16, Word32, Word64)
34-- | Read some text. If the read succeeds, return its value and the
35-- | Read some text. If the read succeeds, return its value and the
35-- remaining text, otherwise an error message.
36-- remaining text, otherwise an error message.
36−type Reader a = Text -> Either String (a,Text)
37+type Reader a = IReader Text a
38+type Parser a = IParser Text a
38-- | Read a decimal integer. The input must begin with at least one
40-- | Read a decimal integer. The input must begin with at least one
39-- decimal digit, and is consumed until a non-digit or end of string
41-- decimal digit, and is consumed until a non-digit or end of string
@@ -111,15 +113,6 @@ hex txt
111 where (# h,t #) = span_ isHexDigit txt
113 where (# h,t #) = span_ isHexDigit txt
112 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
114 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
114−hexDigitToInt :: Char -> Int
116− | c >= '0' && c <= '9' = ord c - ord '0'
117− | c >= 'a' && c <= 'f' = ord c - (ord 'a' - 10)
118− | otherwise = ord c - (ord 'A' - 10)
120−digitToInt :: Char -> Int
121−digitToInt c = ord c - ord '0'
123-- | Read an optional leading sign character (@\'-\'@ or @\'+\'@) and
116-- | Read an optional leading sign character (@\'-\'@ or @\'+\'@) and
124-- apply it to the result of applying the given reader.
117-- apply it to the result of applying the given reader.
125signed :: Num a => Reader a -> Reader a
118signed :: Num a => Reader a -> Reader a
@@ -179,31 +172,11 @@ signa p = do
179 sign <- perhaps '+' $ char (\c -> c == '-' || c == '+')
172 sign <- perhaps '+' $ char (\c -> c == '-' || c == '+')
180 if sign == '+' then p else negate `liftM` p
173 if sign == '+' then p else negate `liftM` p
182−newtype Parser a = P {
186−instance Monad Parser where
187− return a = P $ \t -> Right (a,t)
188− {-# INLINE return #-}
189− m >>= k = P $ \t -> case runP m t of
190− Left err -> Left err
191− Right (a,t') -> runP (k a) t'
192− {-# INLINE (>>=) #-}
193− fail msg = P $ \_ -> Left msg
195−perhaps :: a -> Parser a -> Parser a
196−perhaps def m = P $ \t -> case runP m t of
197− Left _ -> Right (def,t)
200char :: (Char -> Bool) -> Parser Char
175char :: (Char -> Bool) -> Parser Char
201char p = P $ \t -> case T.uncons t of
176char p = P $ \t -> case T.uncons t of
202 Just (c,t') | p c -> Right (c,t')
177 Just (c,t') | p c -> Right (c,t')
203 _ -> Left "character does not match"
178 _ -> Left "character does not match"
205−data T = T !Integer !Int
207floaty :: Fractional a => (Integer -> Integer -> Integer -> a) -> Reader a
180floaty :: Fractional a => (Integer -> Integer -> Integer -> a) -> Reader a
208{-# INLINE floaty #-}
181{-# INLINE floaty #-}
209floaty f = runP $ do
182floaty f = runP $ do