git.iko.soy/typeable/attoparsec
on master help sign in
Report error contexts.

Include contexts in the Left value of eitherResult, parseOnly (all string types).

commit5b8d8d05d7a61af9a5cfe56bcee25e942f15634c authorMatthias Fischmann <mf@zerobuzz.net> dateThu 25 Sep 2014, 11:48 (12 years ago) parent05f019b
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
@@ -92,6 +92,7 @@ module Data.Attoparsec.ByteString
9292 ) where
9393
9494import Data.Attoparsec.Combinator
95+import Data.List (intercalate)
9596import qualified Data.Attoparsec.ByteString.Internal as I
9697import qualified Data.Attoparsec.Internal as I
9798import qualified Data.ByteString as B
@@ -218,6 +219,7 @@ maybeResult _ = Nothing
218219-- | Convert a 'Result' value to an 'Either' value. A 'T.Partial'
219220-- result is treated as failure.
220221eitherResult :: Result r -> Either String r
221−eitherResult (T.Done _ r) = Right r
222−eitherResult (T.Fail _ _ msg) = Left msg
223−eitherResult _ = Left "Result: incomplete input"
222+eitherResult (T.Done _ r) = Right r
223+eitherResult (T.Fail _ [] msg) = Left msg
224+eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
225+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)
@@ -416,9 +417,10 @@ parse m s = T.runParser m (buffer s) (Pos 0) Incomplete failK successK
416417-- @
417418parseOnly :: Parser a -> ByteString -> Either String a
418419parseOnly m s = case T.runParser m (buffer s) (Pos 0) Complete failK successK of
419− Fail _ _ err -> Left err
420− Done _ a -> Right a
421− _ -> error "parseOnly: impossible error!"
420+ Fail _ [] err -> Left err
421+ Fail _ ctxs err -> Left (intercalate " > " ctxs ++ ": " ++ err)
422+ Done _ a -> Right a
423+ _ -> error "parseOnly: impossible error!"
422424{-# INLINE parseOnly #-}
423425
424426get :: Parser ByteString
Data/Attoparsec/ByteString/Lazy.hs+4−2
@@ -36,6 +36,7 @@ module Data.Attoparsec.ByteString.Lazy
3636
3737import Control.DeepSeq (NFData(rnf))
3838import Data.ByteString.Lazy.Internal (ByteString(..), chunk)
39+import Data.List (intercalate)
3940import qualified Data.ByteString as B
4041import qualified Data.Attoparsec.ByteString as A
4142import qualified Data.Attoparsec.Internal.Types as T
@@ -99,5 +100,6 @@ maybeResult _ = Nothing
99100
100101-- | Convert a 'Result' value to an 'Either' value.
101102eitherResult :: Result r -> Either String r
102−eitherResult (Done _ r) = Right r
103−eitherResult (Fail _ _ msg) = Left msg
103+eitherResult (Done _ r) = Right r
104+eitherResult (Fail _ [] msg) = Left msg
105+eitherResult (Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
Data/Attoparsec/Text.hs+5−3
@@ -131,6 +131,7 @@ import Data.Attoparsec.Text.Internal (Parser, Result, parse, takeWhile1)
131131import Data.Bits (Bits, (.|.), shiftL)
132132import Data.Char (isAlpha, isDigit, isSpace, ord)
133133import Data.Int (Int8, Int16, Int32, Int64)
134+import Data.List (intercalate)
134135import Data.Text (Text)
135136import Data.Word (Word8, Word16, Word32, Word64, Word)
136137import qualified Data.Attoparsec.Internal as I
@@ -257,9 +258,10 @@ maybeResult _ = Nothing
257258-- | Convert a 'Result' value to an 'Either' value. A 'Partial' result
258259-- is treated as failure.
259260eitherResult :: Result r -> Either String r
260−eitherResult (T.Done _ r) = Right r
261−eitherResult (T.Fail _ _ msg) = Left msg
262−eitherResult _ = Left "Result: incomplete input"
261+eitherResult (T.Done _ r) = Right r
262+eitherResult (T.Fail _ [] msg) = Left msg
263+eitherResult (T.Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)
264+eitherResult _ = Left "Result: incomplete input"
263265
264266-- | A predicate that matches either a carriage return @\'\\r\'@ or
265267-- 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, length, succ, take, takeWhile)
@@ -429,9 +430,10 @@ parse m s = runParser m (buffer s) 0 Incomplete failK successK
429430-- @
430431parseOnly :: Parser a -> Text -> Either String a
431432parseOnly m s = case runParser m (buffer s) 0 Complete failK successK of
432− Fail _ _ err -> Left err
433− Done _ a -> Right a
434− _ -> error "parseOnly: impossible error!"
433+ Fail _ [] err -> Left err
434+ Fail _ ctxs err -> Left (intercalate " > " ctxs ++ ": " ++ err)
435+ Done _ a -> Right a
436+ _ -> error "parseOnly: impossible error!"
435437{-# INLINE parseOnly #-}
436438
437439get :: Parser Text
Data/Attoparsec/Text/Lazy.hs+4−2
@@ -36,6 +36,7 @@ module Data.Attoparsec.Text.Lazy
3636 ) where
3737
3838import Control.DeepSeq (NFData(rnf))
39+import Data.List (intercalate)
3940import Data.Text.Lazy.Internal (Text(..), chunk)
4041import qualified Data.Attoparsec.Internal.Types as T
4142import qualified Data.Attoparsec.Text as A
@@ -94,5 +95,6 @@ maybeResult _ = Nothing
9495
9596-- | Convert a 'Result' value to an 'Either' value.
9697eitherResult :: Result r -> Either String r
97−eitherResult (Done _ r) = Right r
98−eitherResult (Fail _ _ msg) = Left msg
98+eitherResult (Done _ r) = Right r
99+eitherResult (Fail _ [] msg) = Left msg
100+eitherResult (Fail _ ctxs msg) = Left (intercalate " > " ctxs ++ ": " ++ msg)