Commits

wren romano committed d3d0d12

Added (<!>) for changing the error message when a parser fails.

Comments (0)

Files changed (2)

Data/Attoparsec.hs

 
     -- * Combinators
     , (I.<?>)
+    , (I.<!>)
     , I.try
     , module Data.Attoparsec.Combinator
 

Data/Attoparsec/Internal.hs

 
     -- * Combinators
     , (<?>)
+    , (<!>)
     , try
     , module Data.Attoparsec.Combinator
 
 (<?>) :: Parser a
       -> String                 -- ^ the name to use if parsing fails
       -> Parser a
-p <?> msg0 = Parser $ \i0 a0 m0 kf ks ->
-             let kf' i a m strs msg = kf i a m (msg0:strs) msg
-             in runParser p i0 a0 m0 kf' ks
+p <?> str =
+    Parser $ \i0 a0 m0 kf ks ->
+    let kf' i a m strs msg = kf i a m (str:strs) msg
+    in runParser p i0 a0 m0 kf' ks
 {-# INLINE (<?>) #-}
 infix 0 <?>
 
+-- | Change the error message if the parser fails. This is different
+-- than '(<?>)' which pushes a name of the parser onto a context
+-- stack. It's similar to @p <|> fail msg@ except that @fail@
+-- prepends some text to the @msg@.
+(<!>) :: Parser a
+      -> String                 -- ^ the error message to use if parsing fails
+      -> Parser a
+p <!> msg =
+    Parser $ \i0 a0 m0 kf ks ->
+    let kf' i a m strs _ = kf i a m strs msg
+    in runParser p i0 a0 m0 kf' ks
+{-# INLINE (<!>) #-}
+infix 0 <!>
+
 -- | Terminal failure continuation.
 failK :: Failure a
 failK i0 _a0 _m0 stack msg = Fail (unI i0) stack msg