@@ -78,6 +78,9 @@ instance Functor m => Functor (Step a m) where
78 fmap f (Continue n k) = Continue n (fmap (fmap f) . k)
78 fmap f (Continue n k) = Continue n (fmap (fmap f) . k)
79 fmap _ (Error n e) = Error n e
79 fmap _ (Error n e) = Error n e
81+-- Note: Passing position index separately instead of passing it with the
82+-- result causes huge regression in expression parsing becnhmarks.
81-- | The parser's result.
84-- | The parser's result.
83-- Int is the position index into the current input array. Could be negative.
86-- Int is the position index into the current input array. Could be negative.
@@ -114,6 +117,8 @@ newtype Parser a m b = MkParser
114 -- one more function call to get the input. This could be helpful
117 -- one more function call to get the input. This could be helpful
115 -- for cases where we process just one element per call.
118 -- for cases where we process just one element per call.
120+ -- Do not eta reduce the applications of this continuation.
117 (ParseResult b -> Int -> Input a -> m (Step a m r))
122 (ParseResult b -> Int -> Input a -> m (Step a m r))
118 -- XXX Maintain and pass the original position in the stream. that
123 -- XXX Maintain and pass the original position in the stream. that
119 -- way we can also report better errors. Use a Context structure for
124 -- way we can also report better errors. Use a Context structure for
@@ -196,8 +201,8 @@ instance Monad m => Applicative (Parser a m) where
196 {-# INLINE (<*) #-}
201 {-# INLINE (<*) #-}
197 p1 <* p2 = MkParser $ \k n st arr ->
202 p1 <* p2 = MkParser $ \k n st arr ->
198 let k1 (Success n1 b) s1 input =
203 let k1 (Success n1 b) s1 input =
199− let k2 (Success n2 _) = k (Success n2 b)
204+ let k2 (Success n2 _) s2 input2 = k (Success n2 b) s2 input2
200− k2 (Failure n2 e) = k (Failure n2 e)
205+ k2 (Failure n2 e) s2 input2 = k (Failure n2 e) s2 input2
201 in runParser p2 k2 n1 s1 input
206 in runParser p2 k2 n1 s1 input
202 k1 (Failure n1 e) s1 input = k (Failure n1 e) s1 input
207 k1 (Failure n1 e) s1 input = k (Failure n1 e) s1 input
203 in runParser p1 k1 n st arr
208 in runParser p1 k1 n st arr