@@ -43,10 +43,14 @@ module Data.Aeson.Types.Internal
44 , parseEither
44 , parseEither
45 , parseMaybe
45 , parseMaybe
46 , modifyFailure
48 , modifyFailure
47 , parserThrowError
49 , parserThrowError
48 , parserCatchError
50 , parserCatchError
49 , formatError
52 , formatError
51 -- * Constructors and accessors
55 -- * Constructors and accessors
@@ -87,6 +91,7 @@ import Data.Foldable (foldl')
87import Data.HashMap.Strict (HashMap)
91import Data.HashMap.Strict (HashMap)
88import Data.Hashable (Hashable(..))
92import Data.Hashable (Hashable(..))
89import Data.List (intercalate)
93import Data.List (intercalate)
94+import Data.List.NonEmpty (NonEmpty((:|)))
90import Data.Scientific (Scientific)
95import Data.Scientific (Scientific)
91import Data.Semigroup (Semigroup((<>)))
96import Data.Semigroup (Semigroup((<>)))
92import Data.String (IsString(..))
97import Data.String (IsString(..))
@@ -98,6 +103,7 @@ import Data.Vector (Vector)
98import GHC.Generics (Generic)
103import GHC.Generics (Generic)
99import qualified Control.Monad.Fail as Fail
104import qualified Control.Monad.Fail as Fail
100import qualified Data.HashMap.Strict as H
105import qualified Data.HashMap.Strict as H
106+import qualified Data.List.NonEmpty as NonEmpty
101import qualified Data.Scientific as S
107import qualified Data.Scientific as S
102import qualified Data.Vector as V
108import qualified Data.Vector as V
103import qualified Language.Haskell.TH.Syntax as TH
109import qualified Language.Haskell.TH.Syntax as TH
@@ -118,7 +124,7 @@ data JSONPathElement = Key Text
118type JSONPath = [JSONPathElement]
124type JSONPath = [JSONPathElement]
120-- | The internal result of running a 'Parser'.
126-- | The internal result of running a 'Parser'.
121−data IResult a = IError JSONPath String
127+data IResult a = IError (NonEmpty (JSONPath, String))
122 | ISuccess a
128 | ISuccess a
123 deriving (Eq, Show, Typeable)
129 deriving (Eq, Show, Typeable)
@@ -133,15 +139,15 @@ instance NFData JSONPathElement where
134instance (NFData a) => NFData (IResult a) where
140instance (NFData a) => NFData (IResult a) where
135 rnf (ISuccess a) = rnf a
141 rnf (ISuccess a) = rnf a
136− rnf (IError path err) = rnf path `seq` rnf err
142+ rnf (IError err) = rnf err
138instance (NFData a) => NFData (Result a) where
144instance (NFData a) => NFData (Result a) where
139 rnf (Success a) = rnf a
145 rnf (Success a) = rnf a
140 rnf (Error err) = rnf err
146 rnf (Error err) = rnf err
142instance Functor IResult where
148instance Functor IResult where
143− fmap f (ISuccess a) = ISuccess (f a)
149+ fmap f (ISuccess a) = ISuccess (f a)
144− fmap _ (IError path err) = IError path err
150+ fmap _ (IError err) = IError err
145 {-# INLINE fmap #-}
151 {-# INLINE fmap #-}
147instance Functor Result where
153instance Functor Result where
@@ -153,15 +159,15 @@ instance Monad IResult where
153 return = pure
159 return = pure
154 {-# INLINE return #-}
160 {-# INLINE return #-}
156− ISuccess a >>= k = k a
162+ ISuccess a >>= k = k a
157− IError path err >>= _ = IError path err
163+ IError err >>= _ = IError err
158 {-# INLINE (>>=) #-}
164 {-# INLINE (>>=) #-}
160 fail = Fail.fail
166 fail = Fail.fail
161 {-# INLINE fail #-}
167 {-# INLINE fail #-}
163instance Fail.MonadFail IResult where
169instance Fail.MonadFail IResult where
164− fail err = IError [] err
170+ fail err = IError (([], err) :| [])
165 {-# INLINE fail #-}
171 {-# INLINE fail #-}
167instance Monad Result where
173instance Monad Result where
@@ -238,11 +244,11 @@ instance Monoid (Result a) where
238 {-# INLINE mappend #-}
244 {-# INLINE mappend #-}
240instance Foldable IResult where
246instance Foldable IResult where
241− foldMap _ (IError _ _) = mempty
247+ foldMap _ (IError _) = mempty
242 foldMap f (ISuccess y) = f y
248 foldMap f (ISuccess y) = f y
243 {-# INLINE foldMap #-}
249 {-# INLINE foldMap #-}
245− foldr _ z (IError _ _) = z
251+ foldr _ z (IError _) = z
246 foldr f z (ISuccess y) = f y z
252 foldr f z (ISuccess y) = f y z
247 {-# INLINE foldr #-}
253 {-# INLINE foldr #-}
@@ -256,8 +262,8 @@ instance Foldable Result where
256 {-# INLINE foldr #-}
262 {-# INLINE foldr #-}
258instance Traversable IResult where
264instance Traversable IResult where
259− traverse _ (IError path err) = pure (IError path err)
265+ traverse _ (IError err) = pure (IError err)
260− traverse f (ISuccess a) = ISuccess <$> f a
266+ traverse f (ISuccess a) = ISuccess <$> f a
261 {-# INLINE traverse #-}
267 {-# INLINE traverse #-}
263instance Traversable Result where
269instance Traversable Result where
@@ -266,7 +272,7 @@ instance Traversable Result where
266 {-# INLINE traverse #-}
272 {-# INLINE traverse #-}
268-- | Failure continuation.
274-- | Failure continuation.
269−type Failure f r = JSONPath -> String -> f r
275+type Failure f r = NonEmpty (JSONPath, String) -> f r
270-- | Success continuation.
276-- | Success continuation.
271type Success a f r = a -> f r
277type Success a f r = a -> f r
@@ -289,7 +295,7 @@ instance Monad Parser where
289 {-# INLINE fail #-}
295 {-# INLINE fail #-}
291instance Fail.MonadFail Parser where
297instance Fail.MonadFail Parser where
292− fail msg = Parser $ \path kf _ks -> kf (reverse path) msg
298+ fail msg = Parser $ \path kf _ks -> kf ((reverse path, msg) :| [])
293 {-# INLINE fail #-}
299 {-# INLINE fail #-}
295instance Functor Parser where
301instance Functor Parser where
@@ -309,10 +315,11 @@ instance Alternative Parser where
309 (<|>) = mplus
315 (<|>) = mplus
310 {-# INLINE (<|>) #-}
316 {-# INLINE (<|>) #-}
318+{- TODO accumulate errors -}
312instance MonadPlus Parser where
319instance MonadPlus Parser where
313 mzero = fail "mzero"
320 mzero = fail "mzero"
314 {-# INLINE mzero #-}
321 {-# INLINE mzero #-}
315− mplus a b = Parser $ \path kf ks -> let kf' _ _ = runParser b path kf ks
322+ mplus a b = Parser $ \path kf ks -> let kf' _ = runParser b path kf ks
316 in runParser a path kf' ks
323 in runParser a path kf' ks
317 {-# INLINE mplus #-}
324 {-# INLINE mplus #-}
@@ -333,6 +340,22 @@ apP d e = do
333 return (b a)
340 return (b a)
334{-# INLINE apP #-}
341{-# INLINE apP #-}
343+-- | A variant of 'Control.Applicative.liftA2' that lazily accumulates errors
344+-- from both subparsers.
345+liftP2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
346+liftP2 f pa pb = Parser $ \path kf ks ->
348+ (\(e :| es) -> kf (e :| es ++ runParser pb path NonEmpty.toList (const [])))
349+ (\a -> runParser pb path kf (\b -> ks (f a b)))
350+{-# INLINE liftP2 #-}
354+-- | A variant of ('<*>') that lazily accumulates errors from both subparsers.
355+(<*>+) :: Parser (a -> b) -> Parser a -> Parser b
357+{-# INLINE (<*>+) #-}
336-- | A JSON \"object\" (key\/value map).
359-- | A JSON \"object\" (key\/value map).
337type Object = HashMap Text Value
360type Object = HashMap Text Value
@@ -423,7 +446,7 @@ emptyObject = Object H.empty
424-- | Run a 'Parser'.
447-- | Run a 'Parser'.
425parse :: (a -> Parser b) -> a -> Result b
448parse :: (a -> Parser b) -> a -> Result b
426−parse m v = runParser (m v) [] (const Error) Success
449+parse m v = runParser (m v) [] (Error . snd . NonEmpty.head) Success
427{-# INLINE parse #-}
450{-# INLINE parse #-}
429-- | Run a 'Parser'.
452-- | Run a 'Parser'.
@@ -433,14 +456,14 @@ iparse m v = runParser (m v) [] IError ISuccess
434-- | Run a 'Parser' with a 'Maybe' result type.
457-- | Run a 'Parser' with a 'Maybe' result type.
435parseMaybe :: (a -> Parser b) -> a -> Maybe b
458parseMaybe :: (a -> Parser b) -> a -> Maybe b
436−parseMaybe m v = runParser (m v) [] (\_ _ -> Nothing) Just
459+parseMaybe m v = runParser (m v) [] (const Nothing) Just
437{-# INLINE parseMaybe #-}
460{-# INLINE parseMaybe #-}
439-- | Run a 'Parser' with an 'Either' result type. If the parse fails,
462-- | Run a 'Parser' with an 'Either' result type. If the parse fails,
440-- the 'Left' payload will contain an error message.
463-- the 'Left' payload will contain an error message.
441parseEither :: (a -> Parser b) -> a -> Either String b
464parseEither :: (a -> Parser b) -> a -> Either String b
442parseEither m v = runParser (m v) [] onError Right
465parseEither m v = runParser (m v) [] onError Right
443− where onError path msg = Left (formatError path msg)
466+ where onError ((path, err) :| _) = Left (formatError path err)
444{-# INLINE parseEither #-}
467{-# INLINE parseEither #-}
446-- | Annotate an error message with a
469-- | Annotate an error message with a
@@ -471,6 +494,10 @@ formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg
471 escapeChar '\\' = "\\\\"
494 escapeChar '\\' = "\\\\"
472 escapeChar c = [c]
495 escapeChar c = [c]
497+-- | Annotate a list of error messages.
498+formatErrors :: Functor f => f (JSONPath, String) -> f String
499+formatErrors = fmap (uncurry formatError)
474-- | A key\/value pair for an 'Object'.
501-- | A key\/value pair for an 'Object'.
475type Pair = (Text, Value)
502type Pair = (Text, Value)
@@ -510,21 +537,26 @@ p <?> pathElem = Parser $ \path kf ks -> runParser p (pathElem:path) kf ks
510-- Since 0.6.2.0
537-- Since 0.6.2.0
511modifyFailure :: (String -> String) -> Parser a -> Parser a
538modifyFailure :: (String -> String) -> Parser a -> Parser a
512modifyFailure f (Parser p) = Parser $ \path kf ks ->
539modifyFailure f (Parser p) = Parser $ \path kf ks ->
513− p path (\p' m -> kf p' (f m)) ks
540+ p path (\m -> kf ((fmap . fmap) f m)) ks
515-- | Throw a parser error with an additional path.
542-- | Throw a parser error with an additional path.
517-- @since 1.2.1.0
544-- @since 1.2.1.0
518parserThrowError :: JSONPath -> String -> Parser a
545parserThrowError :: JSONPath -> String -> Parser a
519parserThrowError path' msg = Parser $ \path kf _ks ->
546parserThrowError path' msg = Parser $ \path kf _ks ->
520− kf (reverse path ++ path') msg
547+ kf ((reverse path ++ path', msg) :| [])
522-- | A handler function to handle previous errors and return to normal execution.
549-- | A handler function to handle previous errors and return to normal execution.
524-- @since 1.2.1.0
551-- @since 1.2.1.0
525parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
552parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
553+parserCatchError p handler = parserCatchErrors p (\((e, msg) :| _) -> handler e msg)
555+-- | A handler function to handle multiple previous errors and return to normal
557+parserCatchErrors :: Parser a -> (NonEmpty (JSONPath, String) -> Parser a) -> Parser a
526−parserCatchError (Parser p) handler = Parser $ \path kf ks ->
558+parserCatchErrors (Parser p) handler = Parser $ \path kf ks ->
527− p path (\e msg -> runParser (handler e msg) path kf ks) ks
559+ p path (\es -> runParser (handler es) path kf ks) ks
529--------------------------------------------------------------------------------
561--------------------------------------------------------------------------------
530-- Generic and TH encoding configuration
562-- Generic and TH encoding configuration