diff --git a/Data/Attoparsec/ByteString.hs b/Data/Attoparsec/ByteString.hs index 0de9bc0..f8b2652 100644 --- a/Data/Attoparsec/ByteString.hs +++ b/Data/Attoparsec/ByteString.hs @@ -226,7 +226,8 @@ maybeResult _ = Nothing -- | Convert a 'Result' value to an 'Either' value. A 'T.Partial' -- result is treated as failure. eitherResult :: Result r -> Either String r -eitherResult (T.Done _ r) = Right r -eitherResult (T.Fail _ [] msg) = Left msg -eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg) -eitherResult _ = Left "Result: incomplete input" +eitherResult result = case feed result B.empty of + T.Done _ r -> Right r + T.Fail _ [] msg -> Left msg + T.Fail _ ctxs msg -> Left (intercalate " > " ctxs ++ ": " ++ msg) + T.Partial _ -> error "eitherResult: Partial" diff --git a/Data/Attoparsec/Text.hs b/Data/Attoparsec/Text.hs index bb8ef29..e020f37 100644 --- a/Data/Attoparsec/Text.hs +++ b/Data/Attoparsec/Text.hs @@ -266,10 +266,11 @@ maybeResult _ = Nothing -- | Convert a 'Result' value to an 'Either' value. A 'Partial' result -- is treated as failure. eitherResult :: Result r -> Either String r -eitherResult (T.Done _ r) = Right r -eitherResult (T.Fail _ [] msg) = Left msg -eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg) -eitherResult _ = Left "Result: incomplete input" +eitherResult result = case feed result T.empty of + T.Done _ r -> Right r + T.Fail _ [] msg -> Left msg + T.Fail _ ctxs msg -> Left (intercalate " > " ctxs ++ ": " ++ msg) + T.Partial _ -> error "eitherResult: Partial" -- | A predicate that matches either a carriage return @\'\\r\'@ or -- newline @\'\\n\'@ character. diff --git a/tests/QC/ByteString.hs b/tests/QC/ByteString.hs index dbf2974..f9e6d12 100644 --- a/tests/QC/ByteString.hs +++ b/tests/QC/ByteString.hs @@ -182,6 +182,13 @@ nonmembers :: [Word8] -> [Word8] -> Property nonmembers s s' = property . not . any (`S.memberWord8` set) $ filter (not . (`elem` s)) s' where set = S.fromList s +eitherResultPartialError :: Property +eitherResultPartialError = P.eitherResult (P.parse p input) === Left msg + where + p = (,) <$> P8.letter_ascii <*> P8.digit P. "thing" + input = "a" + msg = "thing > digit: not enough input" + tests :: [TestTree] tests = [ testProperty "anyWord8" anyWord8 @@ -211,4 +218,5 @@ tests = [ , testProperty "word8" word8 , testProperty "members" members , testProperty "nonmembers" nonmembers + , testProperty "eitherResultPartialError" eitherResultPartialError ] diff --git a/tests/QC/Text.hs b/tests/QC/Text.hs index 079dd6f..872a566 100644 --- a/tests/QC/Text.hs +++ b/tests/QC/Text.hs @@ -170,6 +170,13 @@ nonmembers :: String -> String -> Property nonmembers s s' = property . not . any (`S.member` set) $ filter (not . (`elem` s)) s' where set = S.fromList s +eitherResultPartialError :: Property +eitherResultPartialError = P.eitherResult (P.parse p input) === Left msg + where + input = "a" + p = (,) <$> P.letter <*> P.digit P. "thing" + msg = "thing > digit: not enough input" + tests :: [TestTree] tests = [ testProperty "anyChar" anyChar @@ -198,6 +205,7 @@ tests = [ , testProperty "takeWhile1_empty" takeWhile1_empty , testProperty "members" members , testProperty "nonmembers" nonmembers + , testProperty "eitherResultPartialError" eitherResultPartialError , testGroup "FastSet" FastSet.tests , testGroup "Regressions" Regressions.tests ]