@@ -14,6 +14,7 @@ module Data.Attoparsec.Combinator
1414 choice
1515 , count
1616 , option
17+ , optionMaybe
1718 , many'
1819 , many1
1920 , many1'
@@ -66,6 +67,18 @@ option x p = p <|> pure x
6667{-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
6768#endif
6869
70+-- | @option@ specialized to @Maybe@. @optionMaybe p@ tries to apply
71+-- action @p@. If @p@ fails without consuming input, it returns
72+-- @Nothing@, otherwise @Just@ the value returned by @p@.
73+--
74+-- > mDecimal :: Integral a => Parser (Maybe a)
75+-- > mDecimal = optionMaybe decimal
76+optionMaybe :: Alternative f => f a -> f (Maybe a)
77+optionMaybe = option Nothing . fmap Just
78+{-# SPECIALIZE optionMaybe :: Parser ByteString a -> Parser ByteString (Maybe a) #-}
79+{-# SPECIALIZE optionMaybe :: Parser Text a -> Parser Text (Maybe a) #-}
80+{-# SPECIALIZE optionMaybe :: Z.Parser a -> Z.Parser (Maybe a) #-}
81+
6982-- | A version of 'liftM2' that is strict in the result of its first
7083-- action.
7184liftM2' :: (Monad m) => (a -> b -> c) -> m a -> m b -> m c