git.iko.soy/typeable/attoparsec
on master help sign in
Add some more combinator specialisations
commit9625564f55d8fa545ae3bb2aa189808436bc0c6f authorBryan O'Sullivan <bos@serpentine.com> dateWed 11 May 2011, 09:53 (15 years ago) parentaee9e9a
1 file changed+8
Data/Attoparsec/Combinator.hs+8
Data/Attoparsec/Combinator.hs+8
@@ -34,12 +34,14 @@ module Data.Attoparsec.Combinator
3434import Control.Applicative (Alternative, Applicative(..), empty, liftA2,
3535 (<|>), (*>), (<$>))
3636import Data.Attoparsec.Internal.Types (Parser)
37+import qualified Data.Attoparsec.Zepto as Z
3738
3839-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
3940-- until one of them succeeds. Returns the value of the succeeding
4041-- action.
4142choice :: Alternative f => [f a] -> f a
4243{-# SPECIALIZE choice :: [Parser a] -> Parser a #-}
44+{-# SPECIALIZE choice :: [Z.Parser a] -> Z.Parser a #-}
4345choice = foldr (<|>) empty
4446
4547-- | @option x p@ tries to apply action @p@. If @p@ fails without
@@ -49,6 +51,7 @@ choice = foldr (<|>) empty
4951-- > priority = option 0 (digitToInt <$> digit)
5052option :: Alternative f => a -> f a -> f a
5153{-# SPECIALIZE option :: a -> Parser a -> Parser a #-}
54+{-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
5255option x p = p <|> pure x
5356
5457-- | @many1 p@ applies the action @p@ /one/ or more times. Returns a
@@ -65,6 +68,7 @@ many1 p = liftA2 (:) p (many p)
6568-- > commaSep p = p `sepBy` (symbol ",")
6669sepBy :: Alternative f => f a -> f s -> f [a]
6770{-# SPECIALIZE sepBy :: Parser a -> Parser s -> Parser [a] #-}
71+{-# SPECIALIZE sepBy :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
6872sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
6973
7074-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
@@ -73,6 +77,7 @@ sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
7377-- > commaSep p = p `sepBy` (symbol ",")
7478sepBy1 :: Alternative f => f a -> f s -> f [a]
7579{-# SPECIALIZE sepBy1 :: Parser a -> Parser s -> Parser [a] #-}
80+{-# SPECIALIZE sepBy1 :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
7681sepBy1 p s = scan
7782 where scan = liftA2 (:) p ((s *> scan) <|> pure [])
7883
@@ -86,18 +91,21 @@ sepBy1 p s = scan
8691-- therefore the use of the 'try' combinator.
8792manyTill :: Alternative f => f a -> f b -> f [a]
8893{-# SPECIALIZE manyTill :: Parser a -> Parser b -> Parser [a] #-}
94+{-# SPECIALIZE manyTill :: Z.Parser a -> Z.Parser b -> Z.Parser [a] #-}
8995manyTill p end = scan
9096 where scan = (end *> pure []) <|> liftA2 (:) p scan
9197
9298-- | Skip zero or more instances of an action.
9399skipMany :: Alternative f => f a -> f ()
94100{-# SPECIALIZE skipMany :: Parser a -> Parser () #-}
101+{-# SPECIALIZE skipMany :: Z.Parser a -> Z.Parser () #-}
95102skipMany p = scan
96103 where scan = (p *> scan) <|> pure ()
97104
98105-- | Skip one or more instances of an action.
99106skipMany1 :: Alternative f => f a -> f ()
100107{-# SPECIALIZE skipMany1 :: Parser a -> Parser () #-}
108+{-# SPECIALIZE skipMany1 :: Z.Parser a -> Z.Parser () #-}
101109skipMany1 p = p *> skipMany p
102110
103111-- | Apply the given action repeatedly, returning every result.