Commits

Bryan O'Sullivan  committed 87a37ff

Try using Monoid everywhere

  • Participants
  • Parent commits 0050491

Comments (0)

Files changed (3)

File Data/Attoparsec/ByteString/Internal.hs

 prompt i0 a0 _m0 kf ks = Partial $ \s ->
     if B.null s
     then kf i0 a0 Complete
-    else ks (I (unI i0 <> s)) (A (unA a0 <> s)) Incomplete
+    else ks (i0 <> I s) (a0 <> A s) Incomplete
 
 -- | Immediately demand more input via a 'Partial' continuation
 -- result.

File Data/Attoparsec/Internal/Types.hs

-{-# LANGUAGE BangPatterns, Rank2Types, OverloadedStrings, RecordWildCards, CPP #-}
+{-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving, OverloadedStrings,
+    Rank2Types, RecordWildCards #-}
 -- |
 -- Module      :  Data.Attoparsec.Internal.Types
 -- Copyright   :  Bryan O'Sullivan 2007-2011
     fmap = fmapR
     {-# INLINE fmap #-}
 
-newtype Input t = I {unI :: t}
-newtype Added t = A {unA :: t}
+newtype Input t = I {unI :: t} deriving (Monoid)
+newtype Added t = A {unA :: t} deriving (Monoid)
 
 -- | The core parser type.  This is parameterised over the type @t@ of
 -- string being processed.
 data More = Complete | Incomplete
             deriving (Eq, Show)
 
+instance Monoid More where
+    mappend c@Complete _ = c
+    mappend _ m          = m
+    mempty               = Incomplete
+
 addS :: (Monoid t) =>
         Input t -> Added t -> More
      -> Input t -> Added t -> More
      -> (Input t -> Added t -> More -> r) -> r
 addS i0 a0 m0 _i1 a1 m1 f =
-    let !i = I (unI i0 <> unA a1)
-        a  = A (unA a0 <> unA a1)
-        !m = m0 <?> m1
+    let !i = i0 <> I (unA a1)
+        a  = a0 <> a1
+        !m = m0 <> m1
     in f i a m
-  where
-    Complete <?> _ = Complete
-    _ <?> Complete = Complete
-    _ <?> _        = Incomplete
 {-# INLINE addS #-}
 
 bindP :: Parser t a -> (a -> Parser t b) -> Parser t b
 plus a b = Parser $ \i0 a0 m0 kf ks ->
            let kf' i1 a1 m1 _ _ = addS i0 a0 m0 i1 a1 m1 $
                                   \ i2 a2 m2 -> runParser b i2 a2 m2 kf ks
-               ks' i1 a1 m1 = ks i1 (A (unA a0 <> unA a1)) m1
+               ks' i1 a1 m1 = ks i1 (a0 <> a1) m1
            in  noAdds i0 a0 m0 $ \i2 a2 m2 -> runParser a i2 a2 m2 kf' ks'
 {-# INLINE plus #-}
 

File Data/Attoparsec/Text/Internal.hs

 prompt i0 a0 _m0 kf ks = Partial $ \s ->
     if T.null s
     then kf i0 a0 Complete
-    else ks (I (unI i0 <> s)) (A (unA a0 <> s)) Incomplete
+    else ks (i0 <> I s) (a0 <> A s) Incomplete
 
 -- | Immediately demand more input via a 'Partial' continuation
 -- result.