on master help sign in
Drop the CPS patch, since it seems to slow things down

The difference between CPS and plain is small, but the plain code is simpler. rolling back: Fri Oct 8 13:14:59 PDT 2010 Bryan O'Sullivan <bos@serpentine.com> * Switch over to a CPS-based parsing monad Doesn't actually seem to make a difference to performance :-\ M ./Data/Text/Lex.hs -20 +26 --HG-- extra : convert_revision : d6e48a5602fbaee676fc6172923002b3e3ee6d15

commit3d73573e459236428836bee112818bf289901b25 authorBryan O'Sullivan <bos@serpentine.com> dateFri 8 Oct 2010, 20:22 (16 years ago) parentdf83380
1 file changed+19−26
Data/Text/Lex.hs+19−26
Data/Text/Lex.hs+19−26
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, Rank2Types #-}
1+{-# LANGUAGE OverloadedStrings #-}
22
33-- |
44-- Module : Data.Text.Lex
@@ -69,7 +69,7 @@ hexDigitToInt c
6969-- to the result of applying the given reader.
7070signed :: Num a => Lexer a -> Lexer a
7171{-# INLINE signed #-}
72−signed f = parse (signa (asP f))
72+signed f = runP (signa (P f))
7373
7474signa :: Num a => Parser a -> Parser a
7575{-# SPECIALIZE signa :: Parser Int -> Parser Int #-}
@@ -79,46 +79,39 @@ signa p = do
7979 if sign == '+' then p else negate `liftM` p
8080
8181newtype Parser a = P {
82− runP :: forall r.
83− (String -> Either String (r,Text))
84− -> (a -> Text -> Either String (r,Text))
85− -> Text -> Either String (r,Text)
82+ runP :: Text -> Either String (a,Text)
8683 }
8784
88−parse :: Parser a -> Text -> Either String (a,Text)
89−parse p t = runP p Left (\a t' -> Right (a,t')) t
90−
9185instance Monad Parser where
92− return a = P $ \kf ks t -> ks a t
86+ return a = P $ \t -> Right (a,t)
9387 {-# INLINE return #-}
94− m >>= k = P $ \kf ks t -> runP m kf (\a t' -> runP (k a) kf ks t') t
88+ m >>= k = P $ \t -> case runP m t of
89+ Left err -> Left err
90+ Right (a,t') -> runP (k a) t'
9591 {-# INLINE (>>=) #-}
96− fail msg = P $ \kf ks t -> kf msg
92+ fail msg = P $ \_ -> Left msg
9793
9894perhaps :: 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
10098
10199char :: (Char -> Bool) -> Parser Char
102−char p = P $ \kf ks t -> case T.uncons t of
103− Just (c,t') | p c -> ks c t'
104− _ -> kf "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'
100+char p = P $ \t -> case T.uncons t of
101+ Just (c,t') | p c -> Right (c,t')
102+ _ -> Left "char"
110103
111104-- | Read a rational number.
112105rational :: RealFloat a => Lexer a
113106{-# SPECIALIZE rational :: Lexer Double #-}
114−rational = parse $ do
115− real <- signa (asP decimal)
107+rational = runP $ do
108+ real <- signa (P decimal)
116109 (fraction,fracDigits) <- perhaps (0,0) $ do
117110 _ <- char (=='.')
118− digits <- P $ \kf ks t -> ks (T.length $ T.takeWhile isDigit t) t
119− n <- asP decimal
111+ digits <- P $ \t -> Right (T.length $ T.takeWhile isDigit t, t)
112+ n <- P decimal
120113 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)
122115 return $! if fraction == 0
123116 then if power == 0
124117 then fromIntegral real