Skip to content
Browse files

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

This should resolve gh-13.
  • Loading branch information...
1 parent 6145d2b commit c5ffb6c361621d6103820d6cf5918ebd3f46e9e9 @bos committed May 27, 2012
Showing with 66 additions and 2 deletions.
  1. +33 −1 Data/Attoparsec/ByteString/Char8.hs
  2. +33 −1 Data/Attoparsec/Text.hs
View
34 Data/Attoparsec/ByteString/Char8.hs
@@ -76,6 +76,11 @@ module Data.Attoparsec.ByteString.Char8
, takeWhile1
, takeTill
+ -- ** String combinators
+ -- $specalt
+ , (.*>)
+ , (<*.)
+
-- ** Consume all remaining input
, I.takeByteString
, I.takeLazyByteString
@@ -99,7 +104,7 @@ module Data.Attoparsec.ByteString.Char8
, 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
@@ -338,6 +343,33 @@ skipSpace :: Parser ()
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
View
34 Data/Attoparsec/Text.hs
@@ -73,6 +73,11 @@ module Data.Attoparsec.Text
, I.takeWhile1
, I.takeTill
+ -- ** String combinators
+ -- $specalt
+ , (.*>)
+ , (<*.)
+
-- ** Consume all remaining input
, I.takeText
, I.takeLazyText
@@ -96,7 +101,7 @@ module Data.Attoparsec.Text
, 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)
@@ -399,6 +404,33 @@ skipSpace :: Parser ()
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

0 comments on commit c5ffb6c

Please sign in to comment.
Something went wrong with that request. Please try again.