@@ -24,16 +24,18 @@ module Data.Text.Read
2424 ) where
2525
2626import Control.Monad (liftM)
27−import Data.Char (isDigit, isHexDigit, ord)
27+import Data.Char (isDigit, isHexDigit)
2828import Data.Int (Int8, Int16, Int32, Int64)
2929import Data.Ratio ((%))
3030import Data.Text as T
3131import Data.Text.Internal.Private (span_)
32+import Data.Text.Internal.Read
3233import Data.Word (Word, Word8, Word16, Word32, Word64)
3334
3435-- | Read some text. If the read succeeds, return its value and the
3536-- 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
3739
3840-- | Read a decimal integer. The input must begin with at least one
3941-- decimal digit, and is consumed until a non-digit or end of string
@@ -111,15 +113,6 @@ hex txt
111113 where (# h,t #) = span_ isHexDigit txt
112114 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
113115
114−hexDigitToInt :: Char -> Int
115−hexDigitToInt c
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)
119−
120−digitToInt :: Char -> Int
121−digitToInt c = ord c - ord '0'
122−
123116-- | Read an optional leading sign character (@\'-\'@ or @\'+\'@) and
124117-- apply it to the result of applying the given reader.
125118signed :: Num a => Reader a -> Reader a
@@ -179,31 +172,11 @@ signa p = do
179172 sign <- perhaps '+' $ char (\c -> c == '-' || c == '+')
180173 if sign == '+' then p else negate `liftM` p
181174
182−newtype Parser a = P {
183− runP :: Reader a
184− }
185−
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
194−
195−perhaps :: a -> Parser a -> Parser a
196−perhaps def m = P $ \t -> case runP m t of
197− Left _ -> Right (def,t)
198− r@(Right _) -> r
199−
200175char :: (Char -> Bool) -> Parser Char
201176char p = P $ \t -> case T.uncons t of
202177 Just (c,t') | p c -> Right (c,t')
203178 _ -> Left "character does not match"
204179
205−data T = T !Integer !Int
206−
207180floaty :: Fractional a => (Integer -> Integer -> Integer -> a) -> Reader a
208181{-# INLINE floaty #-}
209182floaty f = runP $ do