@@ -1,7 +1,7 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
4−-- Module : Data.Text.Lex
4+-- Module : Data.Text.Read
5-- Copyright : (c) 2010 Bryan O'Sullivan
5-- Copyright : (c) 2010 Bryan O'Sullivan
7-- License : BSD-style
7-- License : BSD-style
@@ -10,10 +10,10 @@
10-- Stability : experimental
10-- Stability : experimental
11-- Portability : GHC
11-- Portability : GHC
13−-- Lexing functions used frequently when handling textual data.
13+-- Reading functions used frequently when handling textual data.
14−module Data.Text.Lex
14+module Data.Text.Read
18 , hexadecimal
18 , hexadecimal
@@ -28,15 +28,15 @@ import Data.Text as T
29-- | Read some text, and if the read succeeds, return its value and
29-- | Read some text, and if the read succeeds, return its value and
30-- the remaining text.
30-- the remaining text.
31−type Lexer a = Text -> Either String (a,Text)
31+type Reader a = Text -> Either String (a,Text)
33-- | Read a decimal integer.
33-- | Read a decimal integer.
35-- This function does not handle leading sign characters. If you need
35-- This function does not handle leading sign characters. If you need
36-- to handle signed input, use @'signed' 'decimal'@.
36-- to handle signed input, use @'signed' 'decimal'@.
37−decimal :: Integral a => Lexer a
37+decimal :: Integral a => Reader a
38−{-# SPECIALIZE decimal :: Lexer Int #-}
38+{-# SPECIALIZE decimal :: Reader Int #-}
39−{-# SPECIALIZE decimal :: Lexer Integer #-}
39+{-# SPECIALIZE decimal :: Reader Integer #-}
40decimal txt
40decimal txt
41 | T.null h = Left "no digits in input"
41 | T.null h = Left "no digits in input"
42 | otherwise = Right (T.foldl' go 0 h, t)
42 | otherwise = Right (T.foldl' go 0 h, t)
@@ -48,9 +48,9 @@ decimal txt
49-- This function does not handle leading sign characters. If you need
49-- This function does not handle leading sign characters. If you need
50-- to handle signed input, use @'signed' 'hexadecimal'@.
50-- to handle signed input, use @'signed' 'hexadecimal'@.
51−hexadecimal :: Integral a => Lexer a
51+hexadecimal :: Integral a => Reader a
52−{-# SPECIALIZE hex :: Lexer Int #-}
52+{-# SPECIALIZE hex :: Reader Int #-}
53−{-# SPECIALIZE hex :: Lexer Integer #-}
53+{-# SPECIALIZE hex :: Reader Integer #-}
54hexadecimal txt
54hexadecimal txt
55 | T.toLower h == "0x" = hex t
55 | T.toLower h == "0x" = hex t
56 | otherwise = hex txt
56 | otherwise = hex txt
@@ -58,15 +58,15 @@ hexadecimal txt
59-- | Read a leading sign character (@\'-\'@ or @\'+\'@) and apply it
59-- | Read a leading sign character (@\'-\'@ or @\'+\'@) and apply it
60-- to the result of applying the given reader.
60-- to the result of applying the given reader.
61−signed :: Num a => Lexer a -> Lexer a
61+signed :: Num a => Reader a -> Reader a
62{-# INLINE signed #-}
62{-# INLINE signed #-}
63signed f = runP (signa (P f))
63signed f = runP (signa (P f))
65-- | Read a rational number.
65-- | Read a rational number.
67-- This function accepts an optional leading sign character.
67-- This function accepts an optional leading sign character.
68−rational :: RealFloat a => Lexer a
68+rational :: RealFloat a => Reader a
69−{-# SPECIALIZE rational :: Lexer Double #-}
69+{-# SPECIALIZE rational :: Reader Double #-}
70rational = floaty $ \real frac fracDenom -> fromRational $
70rational = floaty $ \real frac fracDenom -> fromRational $
71 real % 1 + frac % fracDenom
71 real % 1 + frac % fracDenom
@@ -82,14 +82,14 @@ rational = floaty $ \real frac fracDenom -> fromRational $
82-- results, but for the remaining 5.8%, this function loses precision
82-- results, but for the remaining 5.8%, this function loses precision
83-- around the 15th decimal place. For 0.001% of numbers, this
83-- around the 15th decimal place. For 0.001% of numbers, this
84-- function will lose precision at the 13th or 14th decimal place.
84-- function will lose precision at the 13th or 14th decimal place.
85−double :: Lexer Double
85+double :: Reader Double
86double = floaty $ \real frac fracDenom ->
86double = floaty $ \real frac fracDenom ->
87 fromIntegral real +
87 fromIntegral real +
88 fromIntegral frac / fromIntegral fracDenom
88 fromIntegral frac / fromIntegral fracDenom
90−hex :: Integral a => Lexer a
90+hex :: Integral a => Reader a
91−{-# SPECIALIZE hex :: Lexer Int #-}
91+{-# SPECIALIZE hex :: Reader Int #-}
92−{-# SPECIALIZE hex :: Lexer Integer #-}
92+{-# SPECIALIZE hex :: Reader Integer #-}
94 | T.null h = Left "no digits in input"
94 | T.null h = Left "no digits in input"
95 | otherwise = Right (T.foldl' go 0 h, t)
95 | otherwise = Right (T.foldl' go 0 h, t)
@@ -135,7 +135,7 @@ char p = P $ \t -> case T.uncons t of
136data T = T !Integer !Int
136data T = T !Integer !Int
138−floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Lexer a
138+floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Reader a
139{-# INLINE floaty #-}
139{-# INLINE floaty #-}
140floaty f = runP $ do
140floaty f = runP $ do
141 real <- signa (P decimal)
141 real <- signa (P decimal)