Commits

Bryan O'Sullivan  committed f0bc0f7

Add type-specialized .*> and <*. combinators

This should resolve gh-13.

  • Participants
  • Parent commits 4bb4a5a
  • Tags 0.10.2.0

Comments (0)

Files changed (2)

File Data/Attoparsec/ByteString/Char8.hs

     , takeWhile1
     , takeTill
 
+    -- ** String combinators
+    -- $specalt
+    , (.*>)
+    , (<*.)
+
     -- ** Consume all remaining input
     , I.takeByteString
     , I.takeLazyByteString
     , I.atEnd
     ) where
 
-import Control.Applicative ((*>), (<$>), (<|>))
+import Control.Applicative ((*>), (<*), (<$>), (<|>))
 import Data.Attoparsec.ByteString.FastSet (charClass, memberChar)
 import Data.Attoparsec.ByteString.Internal (Parser, (<?>))
 import Data.Attoparsec.Combinator
 skipSpace = I.skipWhile isSpace_w8
 {-# INLINE skipSpace #-}
 
+-- $specalt
+--
+-- The '.*>' and '<*.' combinators are intended for use with the
+-- @OverloadedStrings@ language extension.  They simplify the common
+-- task of matching a statically known string, then immediately
+-- parsing something else.
+--
+-- An example makes this easier to understand:
+--
+-- @{-\# LANGUAGE OverloadedStrings #-}
+--
+-- shoeSize = \"Shoe size: \" '.*>' 'decimal'
+-- @
+--
+-- If we were to try to use '*>' above instead, the type checker would
+-- not be able to tell which 'IsString' instance to use for the text
+-- in quotes.  We would have to be explicit, using either a type
+-- signature or the 'I.string' parser.
+
+-- | Type-specialized version of '*>' for 'B.ByteString'.
+(.*>) :: B.ByteString -> Parser a -> Parser a
+s .*> f = I.string s *> f
+
+-- | Type-specialized version of '<*' for 'B.ByteString'.
+(<*.) :: Parser a -> B.ByteString -> Parser a
+f <*. s = f <* I.string s
+
 -- | A predicate that matches either a carriage return @\'\\r\'@ or
 -- newline @\'\\n\'@ character.
 isEndOfLine :: Word8 -> Bool

File Data/Attoparsec/Text.hs

     , I.takeWhile1
     , I.takeTill
 
+    -- ** String combinators
+    -- $specalt
+    , (.*>)
+    , (<*.)
+
     -- ** Consume all remaining input
     , I.takeText
     , I.takeLazyText
     , I.atEnd
     ) where
 
-import Control.Applicative ((<$>), (*>), (<|>))
+import Control.Applicative ((<$>), (*>), (<*), (<|>))
 import Data.Attoparsec.Combinator
 import Data.Attoparsec.Number (Number(..))
 import Data.Attoparsec.Text.Internal ((<?>), Parser, Result, parse, takeWhile1)
 skipSpace = I.skipWhile isSpace
 {-# INLINE skipSpace #-}
 
+-- $specalt
+--
+-- The '.*>' and '<*.' combinators are intended for use with the
+-- @OverloadedStrings@ language extension.  They simplify the common
+-- task of matching a statically known string, then immediately
+-- parsing something else.
+--
+-- An example makes this easier to understand:
+--
+-- @{-\# LANGUAGE OverloadedStrings #-}
+--
+-- shoeSize = \"Shoe size: \" '.*>' 'decimal'
+-- @
+--
+-- If we were to try to use '*>' above instead, the type checker would
+-- not be able to tell which 'IsString' instance to use for the text
+-- in quotes.  We would have to be explicit, using either a type
+-- signature or the 'I.string' parser.
+
+-- | Type-specialized version of '*>' for 'Text'.
+(.*>) :: Text -> Parser a -> Parser a
+s .*> f = I.string s *> f
+
+-- | Type-specialized version of '<*' for 'Text'.
+(<*.) :: Parser a -> Text -> Parser a
+f <*. s = f <* I.string s
+
 data T = T !Integer !Int
 
 floaty :: Fractional a => (Integer -> Integer -> Integer -> a) -> Parser a