@@ -33,16 +33,20 @@ module Data.Attoparsec.Combinator
34import Control.Applicative (Alternative, Applicative(..), empty, liftA2,
34import Control.Applicative (Alternative, Applicative(..), empty, liftA2,
35 (<|>), (*>), (<$>))
35 (<|>), (*>), (<$>))
36+#if __GLASGOW_HASKELL__ >= 700
36import Data.Attoparsec.Internal.Types (Parser)
37import Data.Attoparsec.Internal.Types (Parser)
37import qualified Data.Attoparsec.Zepto as Z
38import qualified Data.Attoparsec.Zepto as Z
39-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
41-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
40-- until one of them succeeds. Returns the value of the succeeding
42-- until one of them succeeds. Returns the value of the succeeding
42choice :: Alternative f => [f a] -> f a
44choice :: Alternative f => [f a] -> f a
45+choice = foldr (<|>) empty
46+#if __GLASGOW_HASKELL__ >= 700
43{-# SPECIALIZE choice :: [Parser a] -> Parser a #-}
47{-# SPECIALIZE choice :: [Parser a] -> Parser a #-}
44{-# SPECIALIZE choice :: [Z.Parser a] -> Z.Parser a #-}
48{-# SPECIALIZE choice :: [Z.Parser a] -> Z.Parser a #-}
45−choice = foldr (<|>) empty
49+#endif
47-- | @option x p@ tries to apply action @p@. If @p@ fails without
51-- | @option x p@ tries to apply action @p@. If @p@ fails without
48-- consuming input, it returns the value @x@, otherwise the value
52-- consuming input, it returns the value @x@, otherwise the value
@@ -50,9 +54,11 @@ choice = foldr (<|>) empty
51-- > priority = option 0 (digitToInt <$> digit)
55-- > priority = option 0 (digitToInt <$> digit)
52option :: Alternative f => a -> f a -> f a
56option :: Alternative f => a -> f a -> f a
57+option x p = p <|> pure x
58+#if __GLASGOW_HASKELL__ >= 700
53{-# SPECIALIZE option :: a -> Parser a -> Parser a #-}
59{-# SPECIALIZE option :: a -> Parser a -> Parser a #-}
54{-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
60{-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
55−option x p = p <|> pure x
61+#endif
57-- | @many1 p@ applies the action @p@ /one/ or more times. Returns a
63-- | @many1 p@ applies the action @p@ /one/ or more times. Returns a
58-- list of the returned values of @p@.
64-- list of the returned values of @p@.
@@ -67,19 +73,23 @@ many1 p = liftA2 (:) p (many p)
68-- > commaSep p = p `sepBy` (symbol ",")
74-- > commaSep p = p `sepBy` (symbol ",")
69sepBy :: Alternative f => f a -> f s -> f [a]
75sepBy :: 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
70{-# SPECIALIZE sepBy :: Parser a -> Parser s -> Parser [a] #-}
78{-# SPECIALIZE sepBy :: Parser a -> Parser s -> Parser [a] #-}
71{-# SPECIALIZE sepBy :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
79{-# 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
74-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
82-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
75-- by @sep@. Returns a list of the values returned by @p@.
83-- by @sep@. Returns a list of the values returned by @p@.
77-- > commaSep p = p `sepBy` (symbol ",")
85-- > commaSep p = p `sepBy` (symbol ",")
78sepBy1 :: Alternative f => f a -> f s -> f [a]
86sepBy1 :: 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] #-}
81sepBy1 p s = scan
87sepBy1 p s = scan
82 where scan = liftA2 (:) p ((s *> scan) <|> pure [])
88 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] #-}
84-- | @manyTill p end@ applies action @p@ /zero/ or more times until
94-- | @manyTill p end@ applies action @p@ /zero/ or more times until
85-- action @end@ succeeds, and returns the list of values returned by
95-- action @end@ succeeds, and returns the list of values returned by
@@ -90,23 +100,29 @@ sepBy1 p s = scan
90-- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
100-- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
91-- therefore the use of the 'try' combinator.
101-- therefore the use of the 'try' combinator.
92manyTill :: Alternative f => f a -> f b -> f [a]
102manyTill :: 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] #-}
95manyTill p end = scan
103manyTill p end = scan
96 where scan = (end *> pure []) <|> liftA2 (:) p scan
104 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] #-}
98-- | Skip zero or more instances of an action.
110-- | Skip zero or more instances of an action.
99skipMany :: Alternative f => f a -> f ()
111skipMany :: Alternative f => f a -> f ()
100−{-# SPECIALIZE skipMany :: Parser a -> Parser () #-}
101−{-# SPECIALIZE skipMany :: Z.Parser a -> Z.Parser () #-}
102skipMany p = scan
112skipMany p = scan
103 where scan = (p *> scan) <|> pure ()
113 where scan = (p *> scan) <|> pure ()
114+#if __GLASGOW_HASKELL__ >= 700
115+{-# SPECIALIZE skipMany :: Parser a -> Parser () #-}
116+{-# SPECIALIZE skipMany :: Z.Parser a -> Z.Parser () #-}
105-- | Skip one or more instances of an action.
119-- | Skip one or more instances of an action.
106skipMany1 :: Alternative f => f a -> f ()
120skipMany1 :: Alternative f => f a -> f ()
121+skipMany1 p = p *> skipMany p
122+#if __GLASGOW_HASKELL__ >= 700
107{-# SPECIALIZE skipMany1 :: Parser a -> Parser () #-}
123{-# SPECIALIZE skipMany1 :: Parser a -> Parser () #-}
108{-# SPECIALIZE skipMany1 :: Z.Parser a -> Z.Parser () #-}
124{-# SPECIALIZE skipMany1 :: Z.Parser a -> Z.Parser () #-}
109−skipMany1 p = p *> skipMany p
125+#endif
111-- | Apply the given action repeatedly, returning every result.
127-- | Apply the given action repeatedly, returning every result.
112count :: Monad m => Int -> m a -> m [a]
128count :: Monad m => Int -> m a -> m [a]