git.iko.soy/typeable/attoparsec
on master help sign in
Hide the internal state type parameter

I'm finally making a tiny bit of progress in my understanding of type families. This makes me happy.

commite22d19512c9f606e4ea83f62b009c5d6cb608559 authorBryan O'Sullivan <bos@serpentine.com> dateSat 31 May 2014, 00:12 (12 years ago) parentb018c63
4 files changed+59−44
Data/Attoparsec/ByteString/Internal.hs+1−1 Data/Attoparsec/Combinator.hs+30−30 Data/Attoparsec/Internal/Types.hs+27−12 Data/Attoparsec/Text/Internal.hs+1−1
Data/Attoparsec/ByteString/Internal.hs+1−1
@@ -86,7 +86,7 @@ import qualified Data.ByteString.Internal as B
8686import qualified Data.ByteString.Lazy as L
8787import qualified Data.ByteString.Unsafe as B
8888
89−type Parser = T.Parser ByteString Buffer
89+type Parser = T.Parser ByteString
9090type Result = IResult ByteString
9191type Failure r = T.Failure ByteString Buffer r
9292type Success a r = T.Success ByteString Buffer a r
Data/Attoparsec/Combinator.hs+30−30
@@ -47,14 +47,14 @@ import Prelude hiding (succ)
4747--
4848-- This combinator is provided for compatibility with Parsec.
4949-- attoparsec parsers always backtrack on failure.
50−try :: Parser i t a -> Parser i t a
50+try :: Parser i a -> Parser i a
5151try p = p
5252{-# INLINE try #-}
5353
5454-- | Name the parser, in case failure occurs.
55−(<?>) :: Parser i t a
55+(<?>) :: Parser i a
5656 -> String -- ^ the name to use if parsing fails
57− -> Parser i t a
57+ -> Parser i a
5858p <?> msg0 = Parser $ \t pos more lose succ ->
5959 let lose' t' pos' more' strs msg = lose t' pos' more' (msg0:strs) msg
6060 in runParser p t pos more lose' succ
@@ -66,9 +66,9 @@ infix 0 <?>
6666-- action.
6767choice :: Alternative f => [f a] -> f a
6868choice = foldr (<|>) empty
69−{-# SPECIALIZE choice :: [Parser ByteString ByteString a]
70− -> Parser ByteString ByteString a #-}
71−{-# SPECIALIZE choice :: [Parser Text Text a] -> Parser Text Text a #-}
69+{-# SPECIALIZE choice :: [Parser ByteString a]
70+ -> Parser ByteString a #-}
71+{-# SPECIALIZE choice :: [Parser Text a] -> Parser Text a #-}
7272{-# SPECIALIZE choice :: [Z.Parser a] -> Z.Parser a #-}
7373
7474-- | @option x p@ tries to apply action @p@. If @p@ fails without
@@ -78,8 +78,8 @@ choice = foldr (<|>) empty
7878-- > priority = option 0 (digitToInt <$> digit)
7979option :: Alternative f => a -> f a -> f a
8080option x p = p <|> pure x
81−{-# SPECIALIZE option :: a -> Parser ByteString ByteString a -> Parser ByteString ByteString a #-}
82−{-# SPECIALIZE option :: a -> Parser Text Text a -> Parser Text Text a #-}
81+{-# SPECIALIZE option :: a -> Parser ByteString a -> Parser ByteString a #-}
82+{-# SPECIALIZE option :: a -> Parser Text a -> Parser Text a #-}
8383{-# SPECIALIZE option :: a -> Z.Parser a -> Z.Parser a #-}
8484
8585-- | A version of 'liftM2' that is strict in the result of its first
@@ -125,9 +125,9 @@ many1' p = liftM2' (:) p (many' p)
125125-- > commaSep p = p `sepBy` (symbol ",")
126126sepBy :: Alternative f => f a -> f s -> f [a]
127127sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
128−{-# SPECIALIZE sepBy :: Parser ByteString ByteString a -> Parser ByteString ByteString s
129− -> Parser ByteString ByteString [a] #-}
130−{-# SPECIALIZE sepBy :: Parser Text Text a -> Parser Text Text s -> Parser Text Text [a] #-}
128+{-# SPECIALIZE sepBy :: Parser ByteString a -> Parser ByteString s
129+ -> Parser ByteString [a] #-}
130+{-# SPECIALIZE sepBy :: Parser Text a -> Parser Text s -> Parser Text [a] #-}
131131{-# SPECIALIZE sepBy :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
132132
133133-- | @sepBy' p sep@ applies /zero/ or more occurrences of @p@, separated
@@ -138,9 +138,9 @@ sepBy p s = liftA2 (:) p ((s *> sepBy1 p s) <|> pure []) <|> pure []
138138sepBy' :: (MonadPlus m) => m a -> m s -> m [a]
139139sepBy' p s = scan `mplus` return []
140140 where scan = liftM2' (:) p ((s >> sepBy1' p s) `mplus` return [])
141−{-# SPECIALIZE sepBy' :: Parser ByteString ByteString a -> Parser ByteString ByteString s
142− -> Parser ByteString ByteString [a] #-}
143−{-# SPECIALIZE sepBy' :: Parser Text Text a -> Parser Text Text s -> Parser Text Text [a] #-}
141+{-# SPECIALIZE sepBy' :: Parser ByteString a -> Parser ByteString s
142+ -> Parser ByteString [a] #-}
143+{-# SPECIALIZE sepBy' :: Parser Text a -> Parser Text s -> Parser Text [a] #-}
144144{-# SPECIALIZE sepBy' :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
145145
146146-- | @sepBy1 p sep@ applies /one/ or more occurrences of @p@, separated
@@ -150,9 +150,9 @@ sepBy' p s = scan `mplus` return []
150150sepBy1 :: Alternative f => f a -> f s -> f [a]
151151sepBy1 p s = scan
152152 where scan = liftA2 (:) p ((s *> scan) <|> pure [])
153−{-# SPECIALIZE sepBy1 :: Parser ByteString ByteString a -> Parser ByteString ByteString s
154− -> Parser ByteString ByteString [a] #-}
155−{-# SPECIALIZE sepBy1 :: Parser Text Text a -> Parser Text Text s -> Parser Text Text [a] #-}
153+{-# SPECIALIZE sepBy1 :: Parser ByteString a -> Parser ByteString s
154+ -> Parser ByteString [a] #-}
155+{-# SPECIALIZE sepBy1 :: Parser Text a -> Parser Text s -> Parser Text [a] #-}
156156{-# SPECIALIZE sepBy1 :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
157157
158158-- | @sepBy1' p sep@ applies /one/ or more occurrences of @p@, separated
@@ -163,9 +163,9 @@ sepBy1 p s = scan
163163sepBy1' :: (MonadPlus m) => m a -> m s -> m [a]
164164sepBy1' p s = scan
165165 where scan = liftM2' (:) p ((s >> scan) `mplus` return [])
166−{-# SPECIALIZE sepBy1' :: Parser ByteString ByteString a -> Parser ByteString ByteString s
167− -> Parser ByteString ByteString [a] #-}
168−{-# SPECIALIZE sepBy1' :: Parser Text Text a -> Parser Text Text s -> Parser Text Text [a] #-}
166+{-# SPECIALIZE sepBy1' :: Parser ByteString a -> Parser ByteString s
167+ -> Parser ByteString [a] #-}
168+{-# SPECIALIZE sepBy1' :: Parser Text a -> Parser Text s -> Parser Text [a] #-}
169169{-# SPECIALIZE sepBy1' :: Z.Parser a -> Z.Parser s -> Z.Parser [a] #-}
170170
171171-- | @manyTill p end@ applies action @p@ /zero/ or more times until
@@ -180,9 +180,9 @@ sepBy1' p s = scan
180180manyTill :: Alternative f => f a -> f b -> f [a]
181181manyTill p end = scan
182182 where scan = (end *> pure []) <|> liftA2 (:) p scan
183−{-# SPECIALIZE manyTill :: Parser ByteString ByteString a -> Parser ByteString ByteString b
184− -> Parser ByteString ByteString [a] #-}
185−{-# SPECIALIZE manyTill :: Parser Text Text a -> Parser Text Text b -> Parser Text Text [a] #-}
183+{-# SPECIALIZE manyTill :: Parser ByteString a -> Parser ByteString b
184+ -> Parser ByteString [a] #-}
185+{-# SPECIALIZE manyTill :: Parser Text a -> Parser Text b -> Parser Text [a] #-}
186186{-# SPECIALIZE manyTill :: Z.Parser a -> Z.Parser b -> Z.Parser [a] #-}
187187
188188-- | @manyTill' p end@ applies action @p@ /zero/ or more times until
@@ -199,24 +199,24 @@ manyTill p end = scan
199199manyTill' :: (MonadPlus m) => m a -> m b -> m [a]
200200manyTill' p end = scan
201201 where scan = (end >> return []) `mplus` liftM2' (:) p scan
202−{-# SPECIALIZE manyTill' :: Parser ByteString ByteString a -> Parser ByteString ByteString b
203− -> Parser ByteString ByteString [a] #-}
204−{-# SPECIALIZE manyTill' :: Parser Text Text a -> Parser Text Text b -> Parser Text Text [a] #-}
202+{-# SPECIALIZE manyTill' :: Parser ByteString a -> Parser ByteString b
203+ -> Parser ByteString [a] #-}
204+{-# SPECIALIZE manyTill' :: Parser Text a -> Parser Text b -> Parser Text [a] #-}
205205{-# SPECIALIZE manyTill' :: Z.Parser a -> Z.Parser b -> Z.Parser [a] #-}
206206
207207-- | Skip zero or more instances of an action.
208208skipMany :: Alternative f => f a -> f ()
209209skipMany p = scan
210210 where scan = (p *> scan) <|> pure ()
211−{-# SPECIALIZE skipMany :: Parser ByteString ByteString a -> Parser ByteString ByteString () #-}
212−{-# SPECIALIZE skipMany :: Parser Text Text a -> Parser Text Text () #-}
211+{-# SPECIALIZE skipMany :: Parser ByteString a -> Parser ByteString () #-}
212+{-# SPECIALIZE skipMany :: Parser Text a -> Parser Text () #-}
213213{-# SPECIALIZE skipMany :: Z.Parser a -> Z.Parser () #-}
214214
215215-- | Skip one or more instances of an action.
216216skipMany1 :: Alternative f => f a -> f ()
217217skipMany1 p = p *> skipMany p
218−{-# SPECIALIZE skipMany1 :: Parser ByteString ByteString a -> Parser ByteString ByteString () #-}
219−{-# SPECIALIZE skipMany1 :: Parser Text Text a -> Parser Text Text () #-}
218+{-# SPECIALIZE skipMany1 :: Parser ByteString a -> Parser ByteString () #-}
219+{-# SPECIALIZE skipMany1 :: Parser Text a -> Parser Text () #-}
220220{-# SPECIALIZE skipMany1 :: Z.Parser a -> Z.Parser () #-}
221221
222222-- | Apply the given action repeatedly, returning every result.
Data/Attoparsec/Internal/Types.hs+27−12
@@ -15,6 +15,7 @@
1515module Data.Attoparsec.Internal.Types
1616 (
1717 Parser(..)
18+ , Input(..)
1819 , Failure
1920 , Success
2021 , Pos(..)
@@ -26,8 +27,12 @@ module Data.Attoparsec.Internal.Types
2627import Control.Applicative (Alternative(..), Applicative(..), (<$>))
2728import Control.DeepSeq (NFData(rnf))
2829import Control.Monad (MonadPlus(..))
30+import Data.ByteString (ByteString)
2931import Data.Monoid (Monoid(..))
32+import Data.Text (Text)
3033import Prelude hiding (getChar, succ)
34+import qualified Data.Attoparsec.ByteString.Buffer as B
35+import qualified Data.Attoparsec.Text.Buffer as T
3136
3237newtype Pos = Pos { fromPos :: Int }
3338 deriving (Eq, Ord, Show, Num)
@@ -90,13 +95,23 @@ instance Functor (IResult i) where
9095-- arbitrary lookahead.)
9196--
9297-- * 'Alternative', which follows 'MonadPlus'.
93−newtype Parser i t a = Parser {
94− runParser :: forall r. t -> Pos -> More
95− -> Failure i t r
96− -> Success i t a r
98+newtype Parser i a = Parser {
99+ runParser :: forall r. Input i =>
100+ State i -> Pos -> More
101+ -> Failure i (State i) r
102+ -> Success i (State i) a r
97103 -> IResult i r
98104 }
99105
106+class Input i where
107+ type State i :: *
108+
109+instance Input ByteString where
110+ type State ByteString = B.Buffer
111+
112+instance Input Text where
113+ type State Text = T.Buffer
114+
100115type Failure i t r = t -> Pos -> More -> [String] -> String
101116 -> IResult i r
102117type Success i t a r = t -> Pos -> More -> a -> IResult i r
@@ -110,7 +125,7 @@ instance Monoid More where
110125 mappend _ m = m
111126 mempty = Incomplete
112127
113−instance Monad (Parser i t) where
128+instance Monad (Parser i) where
114129 fail err = Parser $ \t pos more lose _succ -> lose t pos more [] msg
115130 where msg = "Failed reading: " ++ err
116131 {-# INLINE fail #-}
@@ -123,30 +138,30 @@ instance Monad (Parser i t) where
123138 in runParser m t pos more lose succ'
124139 {-# INLINE (>>=) #-}
125140
126−plus :: Parser i t a -> Parser i t a -> Parser i t a
141+plus :: Parser i a -> Parser i a -> Parser i a
127142plus f g = Parser $ \t pos more lose succ ->
128143 let lose' t' _pos' more' _ctx _msg = runParser g t' pos more' lose succ
129144 in runParser f t pos more lose' succ
130145
131−instance MonadPlus (Parser i t) where
146+instance MonadPlus (Parser i) where
132147 mzero = fail "mzero"
133148 {-# INLINE mzero #-}
134149 mplus = plus
135150
136−instance Functor (Parser i t) where
151+instance Functor (Parser i) where
137152 fmap f p = Parser $ \t pos more lose succ ->
138153 let succ' t' pos' more' a = succ t' pos' more' (f a)
139154 in runParser p t pos more lose succ'
140155 {-# INLINE fmap #-}
141156
142−apP :: Parser i t (a -> b) -> Parser i t a -> Parser i t b
157+apP :: Parser i (a -> b) -> Parser i a -> Parser i b
143158apP d e = do
144159 b <- d
145160 a <- e
146161 return (b a)
147162{-# INLINE apP #-}
148163
149−instance Applicative (Parser i t) where
164+instance Applicative (Parser i) where
150165 pure = return
151166 {-# INLINE pure #-}
152167 (<*>) = apP
@@ -160,13 +175,13 @@ instance Applicative (Parser i t) where
160175 x <* y = x >>= \a -> y >> return a
161176 {-# INLINE (<*) #-}
162177
163−instance Monoid (Parser i t a) where
178+instance Monoid (Parser i a) where
164179 mempty = fail "mempty"
165180 {-# INLINE mempty #-}
166181 mappend = plus
167182 {-# INLINE mappend #-}
168183
169−instance Alternative (Parser i t) where
184+instance Alternative (Parser i) where
170185 empty = fail "empty"
171186 {-# INLINE empty #-}
172187
Data/Attoparsec/Text/Internal.hs+1−1
@@ -81,7 +81,7 @@ import qualified Data.Text as T
8181import qualified Data.Text.Lazy as L
8282import qualified Data.Text.Unsafe as T
8383
84−type Parser = T.Parser Text Buffer
84+type Parser = T.Parser Text
8585type Result = IResult Text
8686type Failure r = T.Failure Text Buffer r
8787type Success a r = T.Success Text Buffer a r