@@ -33,16 +33,20 @@ module Data.Attoparsec.Combinator
3333
3434import Control.Applicative (Alternative, Applicative(..), empty, liftA2,
3535 (<|>), (*>), (<$>))
36+#if __GLASGOW_HASKELL__ >= 700
3637import Data.Attoparsec.Internal.Types (Parser)
3738import qualified Data.Attoparsec.Zepto as Z
39+#endif
3840
3941-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
4042-- until one of them succeeds. Returns the value of the succeeding
4143-- action.
4244choice :: Alternative f => [f a] -> f a
45+choice = foldr (<|>) empty
46+#if __GLASGOW_HASKELL__ >= 700
4347{-# SPECIALIZE choice :: [Parser a] -> Parser a #-}
4448{-# SPECIALIZE choice :: [Z.Parser a] -> Z.Parser a #-}
45−choice = foldr (<|>) empty
49+#endif
4650
4751-- | @option x p@ tries to apply action @p@. If @p@ fails without
4852-- consuming input, it returns the value @x@, otherwise the value
@@ -50,9 +54,11 @@ choice = foldr (<|>) empty
5054--
5155-- > priority = option 0 (digitToInt <$> digit)
5256option :: Alternative f => a -> f a -> f a
57+option x p = p <|> pure x
58+#if __GLASGOW_HASKELL__ >= 700
5359{-# SPECIALIZE option :: a -> Parser a -> Parser a #-}
5460{-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
55−option x p = p <|> pure x
61+#endif
5662
5763-- | @many1 p@ applies the action @p@ /one/ or more times. Returns a
5864-- list of the returned values of @p@.
@@ -67,19 +73,23 @@ many1 p = liftA2 (:) p (many p)
6773--
6874-- > commaSep p = p `sepBy` (symbol ",")
6975sepBy :: Alternative f => f a -> f s -> f [a]
76+sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
77+#if __GLASGOW_HASKELL__ >= 700
7078{-# SPECIALIZE sepBy :: Parser a -> Parser s -> Parser [a] #-}
7179{-# SPECIALIZE sepBy :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
72−sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
80+#endif
7381
7482-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
7583-- by @sep@. Returns a list of the values returned by @p@.
7684--
7785-- > commaSep p = p `sepBy` (symbol ",")
7886sepBy1 :: Alternative f => f a -> f s -> f [a]
79−{-# SPECIALIZE sepBy1 :: Parser a -> Parser s -> Parser [a] #-}
80−{-# SPECIALIZE sepBy1 :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
8187sepBy1 p s = scan
8288 where scan = liftA2 (:) p ((s *> scan) <|> pure [])
89+#if __GLASGOW_HASKELL__ >= 700
90+{-# SPECIALIZE sepBy1 :: Parser a -> Parser s -> Parser [a] #-}
91+{-# SPECIALIZE sepBy1 :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
92+#endif
8393
8494-- | @manyTill p end@ applies action @p@ /zero/ or more times until
8595-- action @end@ succeeds, and returns the list of values returned by
@@ -90,23 +100,29 @@ sepBy1 p s = scan
90100-- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
91101-- therefore the use of the 'try' combinator.
92102manyTill :: Alternative f => f a -> f b -> f [a]
93−{-# SPECIALIZE manyTill :: Parser a -> Parser b -> Parser [a] #-}
94−{-# SPECIALIZE manyTill :: Z.Parser a -> Z.Parser b -> Z.Parser [a] #-}
95103manyTill p end = scan
96104 where scan = (end *> pure []) <|> liftA2 (:) p scan
105+#if __GLASGOW_HASKELL__ >= 700
106+{-# SPECIALIZE manyTill :: Parser a -> Parser b -> Parser [a] #-}
107+{-# SPECIALIZE manyTill :: Z.Parser a -> Z.Parser b -> Z.Parser [a] #-}
108+#endif
97109
98110-- | Skip zero or more instances of an action.
99111skipMany :: Alternative f => f a -> f ()
100−{-# SPECIALIZE skipMany :: Parser a -> Parser () #-}
101−{-# SPECIALIZE skipMany :: Z.Parser a -> Z.Parser () #-}
102112skipMany p = scan
103113 where scan = (p *> scan) <|> pure ()
114+#if __GLASGOW_HASKELL__ >= 700
115+{-# SPECIALIZE skipMany :: Parser a -> Parser () #-}
116+{-# SPECIALIZE skipMany :: Z.Parser a -> Z.Parser () #-}
117+#endif
104118
105119-- | Skip one or more instances of an action.
106120skipMany1 :: Alternative f => f a -> f ()
121+skipMany1 p = p *> skipMany p
122+#if __GLASGOW_HASKELL__ >= 700
107123{-# SPECIALIZE skipMany1 :: Parser a -> Parser () #-}
108124{-# SPECIALIZE skipMany1 :: Z.Parser a -> Z.Parser () #-}
109−skipMany1 p = p *> skipMany p
125+#endif
110126
111127-- | Apply the given action repeatedly, returning every result.
112128count :: Monad m => Int -> m a -> m [a]