git.iko.soy/typeable/attoparsec
on master help sign in
Merge pull request #79 from zerobuzz/better-errors+1

Report error contexts.

commitbb8808131d51b55dd543f6a8006f53f338c1cfd6 authorBryan O'Sullivan <bos@serpentine.com> dateFri 27 Mar 2015, 22:02 (12 years ago) parent6350d7d5b8d8d0
6 files changed+28−16
Data/Attoparsec/ByteString.hs+5−3 Data/Attoparsec/ByteString/Internal.hs+5−3 Data/Attoparsec/ByteString/Lazy.hs+4−2 Data/Attoparsec/Text.hs+5−3 Data/Attoparsec/Text/Internal.hs+5−3 Data/Attoparsec/Text/Lazy.hs+4−2
Data/Attoparsec/ByteString.hs+5−3
@@ -96,6 +96,7 @@ module Data.Attoparsec.ByteString
9696 ) where
9797
9898import Data.Attoparsec.Combinator
99+import Data.List (intercalate)
99100import qualified Data.Attoparsec.ByteString.Internal as I
100101import qualified Data.Attoparsec.Internal as I
101102import qualified Data.ByteString as B
@@ -222,6 +223,7 @@ maybeResult _ = Nothing
222223-- | Convert a 'Result' value to an 'Either' value. A 'T.Partial'
223224-- result is treated as failure.
224225eitherResult :: Result r -> Either String r
225−eitherResult (T.Done _ r) = Right r
226−eitherResult (T.Fail _ _ msg) = Left msg
227−eitherResult _ = Left "Result: incomplete input"
226+eitherResult (T.Done _ r) = Right r
227+eitherResult (T.Fail _ [] msg) = Left msg
228+eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
229+eitherResult _ = Left "Result: incomplete input"
Data/Attoparsec/ByteString/Internal.hs+5−3
@@ -74,6 +74,7 @@ import Data.Attoparsec.Internal
7474import Data.Attoparsec.Internal.Fhthagn (inlinePerformIO)
7575import Data.Attoparsec.Internal.Types hiding (Parser, Failure, Success)
7676import Data.ByteString (ByteString)
77+import Data.List (intercalate)
7778import Data.Word (Word8)
7879import Foreign.ForeignPtr (withForeignPtr)
7980import Foreign.Ptr (castPtr, minusPtr, plusPtr)
@@ -452,9 +453,10 @@ parse m s = T.runParser m (buffer s) (Pos 0) Incomplete failK successK
452453-- @
453454parseOnly :: Parser a -> ByteString -> Either String a
454455parseOnly m s = case T.runParser m (buffer s) (Pos 0) Complete failK successK of
455− Fail _ _ err -> Left err
456− Done _ a -> Right a
457− _ -> error "parseOnly: impossible error!"
456+ Fail _ [] err -> Left err
457+ Fail _ ctxs err -> Left (intercalate " > " ctxs ++ ": " ++ err)
458+ Done _ a -> Right a
459+ _ -> error "parseOnly: impossible error!"
458460{-# INLINE parseOnly #-}
459461
460462get :: Parser ByteString
Data/Attoparsec/ByteString/Lazy.hs+4−2
@@ -41,6 +41,7 @@ module Data.Attoparsec.ByteString.Lazy
4141
4242import Control.DeepSeq (NFData(rnf))
4343import Data.ByteString.Lazy.Internal (ByteString(..), chunk)
44+import Data.List (intercalate)
4445import qualified Data.ByteString as B
4546import qualified Data.Attoparsec.ByteString as A
4647import qualified Data.Attoparsec.Internal.Types as T
@@ -104,5 +105,6 @@ maybeResult _ = Nothing
104105
105106-- | Convert a 'Result' value to an 'Either' value.
106107eitherResult :: Result r -> Either String r
107−eitherResult (Done _ r) = Right r
108−eitherResult (Fail _ _ msg) = Left msg
108+eitherResult (Done _ r) = Right r
109+eitherResult (Fail _ [] msg) = Left msg
110+eitherResult (Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
Data/Attoparsec/Text.hs+5−3
@@ -138,6 +138,7 @@ import Data.Attoparsec.Text.Internal (Parser, Result, parse, takeWhile1)
138138import Data.Bits (Bits, (.|.), shiftL)
139139import Data.Char (isAlpha, isDigit, isSpace, ord)
140140import Data.Int (Int8, Int16, Int32, Int64)
141+import Data.List (intercalate)
141142import Data.Text (Text)
142143import Data.Word (Word8, Word16, Word32, Word64)
143144import qualified Data.Attoparsec.Internal as I
@@ -264,9 +265,10 @@ maybeResult _ = Nothing
264265-- | Convert a 'Result' value to an 'Either' value. A 'Partial' result
265266-- is treated as failure.
266267eitherResult :: Result r -> Either String r
267−eitherResult (T.Done _ r) = Right r
268−eitherResult (T.Fail _ _ msg) = Left msg
269−eitherResult _ = Left "Result: incomplete input"
268+eitherResult (T.Done _ r) = Right r
269+eitherResult (T.Fail _ [] msg) = Left msg
270+eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
271+eitherResult _ = Left "Result: incomplete input"
270272
271273-- | A predicate that matches either a carriage return @\'\\r\'@ or
272274-- newline @\'\\n\'@ character.
Data/Attoparsec/Text/Internal.hs+5−3
@@ -73,6 +73,7 @@ import Data.Attoparsec.Internal.Types hiding (Parser, Failure, Success)
7373import qualified Data.Attoparsec.Text.Buffer as Buf
7474import Data.Attoparsec.Text.Buffer (Buffer, buffer)
7575import Data.Char (chr, ord)
76+import Data.List (intercalate)
7677import Data.String (IsString(..))
7778import Data.Text.Internal (Text(..))
7879import Prelude hiding (getChar, succ, take, takeWhile)
@@ -458,9 +459,10 @@ parse m s = runParser m (buffer s) 0 Incomplete failK successK
458459-- @
459460parseOnly :: Parser a -> Text -> Either String a
460461parseOnly m s = case runParser m (buffer s) 0 Complete failK successK of
461− Fail _ _ err -> Left err
462− Done _ a -> Right a
463− _ -> error "parseOnly: impossible error!"
462+ Fail _ [] err -> Left err
463+ Fail _ ctxs err -> Left (intercalate " > " ctxs ++ ": " ++ err)
464+ Done _ a -> Right a
465+ _ -> error "parseOnly: impossible error!"
464466{-# INLINE parseOnly #-}
465467
466468get :: Parser Text
Data/Attoparsec/Text/Lazy.hs+4−2
@@ -41,6 +41,7 @@ module Data.Attoparsec.Text.Lazy
4141 ) where
4242
4343import Control.DeepSeq (NFData(rnf))
44+import Data.List (intercalate)
4445import Data.Text.Lazy.Internal (Text(..), chunk)
4546import qualified Data.Attoparsec.Internal.Types as T
4647import qualified Data.Attoparsec.Text as A
@@ -95,5 +96,6 @@ maybeResult _ = Nothing
9596
9697-- | Convert a 'Result' value to an 'Either' value.
9798eitherResult :: Result r -> Either String r
98−eitherResult (Done _ r) = Right r
99−eitherResult (Fail _ _ msg) = Left msg
99+eitherResult (Done _ r) = Right r
100+eitherResult (Fail _ [] msg) = Left msg
101+eitherResult (Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)