git.iko.soy/typeable/attoparsec
on master help sign in
Specialise generic combinators, yielding 15-40% speedups for aeson.
4a3f7aeBryan O'Sullivan
Data/Attoparsec/Combinator.hs+8
@@ -33,11 +33,13 @@ module Data.Attoparsec.Combinator
33
33
34import Control.Applicative (Alternative, Applicative(..), empty, liftA2,
34import Control.Applicative (Alternative, Applicative(..), empty, liftA2,
35 (<|>), (*>), (<$>))
35 (<|>), (*>), (<$>))
36+import Data.Attoparsec.Internal.Types (Parser)
36
37
37-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
38-- | @choice ps@ tries to apply the actions in the list @ps@ in order,
38-- until one of them succeeds. Returns the value of the succeeding
39-- until one of them succeeds. Returns the value of the succeeding
39-- action.
40-- action.
40choice :: Alternative f => [f a] -> f a
41choice :: Alternative f => [f a] -> f a
42+{-# SPECIALIZE choice :: [Parser a] -> Parser a #-}
41choice = foldr (<|>) empty
43choice = foldr (<|>) empty
42
44
43-- | @option x p@ tries to apply action @p@. If @p@ fails without
45-- | @option x p@ tries to apply action @p@. If @p@ fails without
@@ -46,6 +48,7 @@ choice = foldr (<|>) empty
46--
48--
47-- > priority = option 0 (digitToInt <$> digit)
49-- > priority = option 0 (digitToInt <$> digit)
48option :: Alternative f => a -> f a -> f a
50option :: Alternative f => a -> f a -> f a
51+{-# SPECIALIZE option :: a -> Parser a -> Parser a #-}
49option x p = p <|> pure x
52option x p = p <|> pure x
50
53
51-- | @many1 p@ applies the action @p@ /one/ or more times. Returns a
54-- | @many1 p@ applies the action @p@ /one/ or more times. Returns a
@@ -61,6 +64,7 @@ many1 p = liftA2 (:) p (many p)
61--
64--
62-- > commaSep p = p `sepBy` (symbol ",")
65-- > commaSep p = p `sepBy` (symbol ",")
63sepBy :: Alternative f => f a -> f s -> f [a]
66sepBy :: Alternative f => f a -> f s -> f [a]
67+{-# SPECIALIZE sepBy :: Parser a -> Parser s -> Parser [a] #-}
64sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
68sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
65
69
66-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
70-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
@@ -68,6 +72,7 @@ sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
68--
72--
69-- > commaSep p = p `sepBy` (symbol ",")
73-- > commaSep p = p `sepBy` (symbol ",")
70sepBy1 :: Alternative f => f a -> f s -> f [a]
74sepBy1 :: Alternative f => f a -> f s -> f [a]
75+{-# SPECIALIZE sepBy1 :: Parser a -> Parser s -> Parser [a] #-}
71sepBy1 p s = scan
76sepBy1 p s = scan
72 where scan = liftA2 (:) p ((s *> scan) <|> pure [])
77 where scan = liftA2 (:) p ((s *> scan) <|> pure [])
73
78
@@ -80,16 +85,19 @@ sepBy1 p s = scan
80-- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
85-- Note the overlapping parsers @anyChar@ and @string \"<!--\"@, and
81-- therefore the use of the 'try' combinator.
86-- therefore the use of the 'try' combinator.
82manyTill :: Alternative f => f a -> f b -> f [a]
87manyTill :: Alternative f => f a -> f b -> f [a]
88+{-# SPECIALIZE manyTill :: Parser a -> Parser b -> Parser [a] #-}
83manyTill p end = scan
89manyTill p end = scan
84 where scan = (end *> pure []) <|> liftA2 (:) p scan
90 where scan = (end *> pure []) <|> liftA2 (:) p scan
85
91
86-- | Skip zero or more instances of an action.
92-- | Skip zero or more instances of an action.
87skipMany :: Alternative f => f a -> f ()
93skipMany :: Alternative f => f a -> f ()
94+{-# SPECIALIZE skipMany :: Parser a -> Parser () #-}
88skipMany p = scan
95skipMany p = scan
89 where scan = (p *> scan) <|> pure ()
96 where scan = (p *> scan) <|> pure ()
90
97
91-- | Skip one or more instances of an action.
98-- | Skip one or more instances of an action.
92skipMany1 :: Alternative f => f a -> f ()
99skipMany1 :: Alternative f => f a -> f ()
100+{-# SPECIALIZE skipMany1 :: Parser a -> Parser () #-}
93skipMany1 p = p *> skipMany p
101skipMany1 p = p *> skipMany p
94
102
95-- | Apply the given action repeatedly, returning every result.
103-- | Apply the given action repeatedly, returning every result.