Commits

Bryan O'Sullivan committed 93803d0

Add a compareResults function

  • Participants
  • Parent commits e41e7b0

Comments (0)

Files changed (5)

File Data/Attoparsec/ByteString.hs

       I.Parser
     , Result
     , T.IResult(..)
+    , I.compareResults
 
     -- * Running parsers
     , parse
 
 import Data.Attoparsec.Combinator
 import qualified Data.Attoparsec.ByteString.Internal as I
+import qualified Data.Attoparsec.Internal as I
 import qualified Data.ByteString as B
 import Data.Attoparsec.ByteString.Internal (Result, parse)
 import qualified Data.Attoparsec.Internal.Types as T

File Data/Attoparsec/ByteString/Char8.hs

       Parser
     , A.Result
     , A.IResult(..)
+    , I.compareResults
 
     -- * Running parsers
     , A.parse
 import Prelude hiding (takeWhile)
 import qualified Data.Attoparsec.ByteString as A
 import qualified Data.Attoparsec.ByteString.Internal as I
+import qualified Data.Attoparsec.Internal as I
 import qualified Data.ByteString as B8
 import qualified Data.ByteString.Char8 as B
 

File Data/Attoparsec/Internal.hs

+-- |
+-- Module      :  Data.Attoparsec.Internal
+-- Copyright   :  Bryan O'Sullivan 2012
+-- License     :  BSD3
+--
+-- Maintainer  :  bos@serpentine.com
+-- Stability   :  experimental
+-- Portability :  unknown
+--
+-- Simple, efficient parser combinators, loosely based on the Parsec
+-- library.
+
+module Data.Attoparsec.Internal
+    (
+      compareResults
+    ) where
+
+import Data.Attoparsec.Internal.Types (IResult(..))
+
+-- | Compare two 'IResult' values for equality.
+--
+-- If both 'IResult's are 'Partial', the result will be 'Nothing', as
+-- they are incomplete and hence their equality cannot be known.
+-- (This is why there is no 'Eq' instance for 'IResult'.)
+compareResults :: (Eq t, Eq r) => IResult t r -> IResult t r -> Maybe Bool
+compareResults (Fail i0 ctxs0 msg0) (Fail i1 ctxs1 msg1) =
+    Just (i0 == i1 && ctxs0 == ctxs1 && msg0 == msg1)
+compareResults (Done i0 r0) (Done i1 r1) =
+    Just (i0 == i1 && r0 == r1)
+compareResults (Partial _) (Partial _) = Nothing
+compareResults _ _ = Just False

File Data/Attoparsec/Text.hs

       Parser
     , Result
     , T.IResult(..)
+    , I.compareResults
 
     -- * Running parsers
     , parse
 import Data.Ratio ((%))
 import Data.Text (Text)
 import Data.Word (Word8, Word16, Word32, Word64, Word)
+import qualified Data.Attoparsec.Internal as I
 import qualified Data.Attoparsec.Internal.Types as T
 import qualified Data.Attoparsec.Text.Internal as I
 import qualified Data.Text as T

File attoparsec.cabal

                    Data.Attoparsec.Zepto
   other-modules:   Data.Attoparsec.ByteString.FastSet
                    Data.Attoparsec.ByteString.Internal
+                   Data.Attoparsec.Internal
                    Data.Attoparsec.Internal.Types
                    Data.Attoparsec.Text.FastSet
                    Data.Attoparsec.Text.Internal