on master help sign in
Drop the CPS patch, since it seems to slow things down
3d73573Bryan O'Sullivan
Data/Text/Lex.hs+19−26
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, Rank2Types #-}
1+{-# LANGUAGE OverloadedStrings #-}
2
2
3-- |
3-- |
4-- Module : Data.Text.Lex
4-- Module : Data.Text.Lex
@@ -69,7 +69,7 @@ hexDigitToInt c
69-- to the result of applying the given reader.
69-- to the result of applying the given reader.
70signed :: Num a => Lexer a -> Lexer a
70signed :: Num a => Lexer a -> Lexer a
71{-# INLINE signed #-}
71{-# INLINE signed #-}
72−signed f = parse (signa (asP f))
72+signed f = runP (signa (P f))
73
73
74signa :: Num a => Parser a -> Parser a
74signa :: Num a => Parser a -> Parser a
75{-# SPECIALIZE signa :: Parser Int -> Parser Int #-}
75{-# SPECIALIZE signa :: Parser Int -> Parser Int #-}
@@ -79,46 +79,39 @@ signa p = do
79 if sign == '+' then p else negate `liftM` p
79 if sign == '+' then p else negate `liftM` p
80
80
81newtype Parser a = P {
81newtype Parser a = P {
82− runP :: forall r.
83− (String -> Either String (r,Text))
82+ runP :: Text -> Either String (a,Text)
84− -> (a -> Text -> Either String (r,Text))
85− -> Text -> Either String (r,Text)
86 }
83 }
87
84
88−parse :: Parser a -> Text -> Either String (a,Text)
89−parse p t = runP p Left (\a t' -> Right (a,t')) t
90−
91instance Monad Parser where
85instance Monad Parser where
92− return a = P $ \kf ks t -> ks a t
86+ return a = P $ \t -> Right (a,t)
93 {-# INLINE return #-}
87 {-# INLINE return #-}
88+ m >>= k = P $ \t -> case runP m t of
89+ Left err -> Left err
94− m >>= k = P $ \kf ks t -> runP m kf (\a t' -> runP (k a) kf ks t') t
90+ Right (a,t') -> runP (k a) t'
95 {-# INLINE (>>=) #-}
91 {-# INLINE (>>=) #-}
96− fail msg = P $ \kf ks t -> kf msg
92+ fail msg = P $ \_ -> Left msg
97
93
98perhaps :: a -> Parser a -> Parser a
94perhaps :: a -> Parser a -> Parser a
99−perhaps def m = P $ \kf ks t -> runP m (\_ -> ks def t) ks t
95+perhaps def m = P $ \t -> case runP m t of
96+ Left _ -> Right (def,t)
97+ r@(Right _) -> r
100
98
101char :: (Char -> Bool) -> Parser Char
99char :: (Char -> Bool) -> Parser Char
102−char p = P $ \kf ks t -> case T.uncons t of
100+char p = P $ \t -> case T.uncons t of
103− Just (c,t') | p c -> ks c t'
101+ Just (c,t') | p c -> Right (c,t')
104− _ -> kf "char"
102+ _ -> Left "char"
105−
106−asP :: (Text -> Either String (a,Text)) -> Parser a
107−asP p = P $ \kf ks t -> case p t of
108− Left err -> kf err
109− Right (a,t') -> ks a t'
110
103
111-- | Read a rational number.
104-- | Read a rational number.
112rational :: RealFloat a => Lexer a
105rational :: RealFloat a => Lexer a
113{-# SPECIALIZE rational :: Lexer Double #-}
106{-# SPECIALIZE rational :: Lexer Double #-}
114−rational = parse $ do
107+rational = runP $ do
115− real <- signa (asP decimal)
108+ real <- signa (P decimal)
116 (fraction,fracDigits) <- perhaps (0,0) $ do
109 (fraction,fracDigits) <- perhaps (0,0) $ do
117 _ <- char (=='.')
110 _ <- char (=='.')
118− digits <- P $ \kf ks t -> ks (T.length $ T.takeWhile isDigit t) t
111+ digits <- P $ \t -> Right (T.length $ T.takeWhile isDigit t, t)
119− n <- asP decimal
112+ n <- P decimal
120 return (n, digits)
113 return (n, digits)
121− power <- perhaps 0 (char (`elem` "eE") >> signa (asP decimal) :: Parser Int)
114+ power <- perhaps 0 (char (`elem` "eE") >> signa (P decimal) :: Parser Int)
122 return $! if fraction == 0
115 return $! if fraction == 0
123 then if power == 0
116 then if power == 0
124 then fromIntegral real
117 then fromIntegral real