@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12-----------------------------------------------------------------------------
23-- |
34-- Module : Data.ParserCombinators.Attoparsec.Internal
@@ -28,7 +29,6 @@ module Data.ParserCombinators.Attoparsec.Internal
2829
2930 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
3031 , try
31− , many
3232 , many1
3333 , manyTill
3434 , eof
@@ -69,8 +69,8 @@ module Data.ParserCombinators.Attoparsec.Internal
6969 , (+:)
7070 ) where
7171
72−import Control.Applicative (Alternative(..), Applicative(..), (<$>))
73−import Control.Monad (MonadPlus(..), ap, liftM2)
72+import Control.Applicative
73+import Control.Monad (MonadPlus(..), ap)
7474import Control.Monad.Fix (MonadFix(..))
7575import qualified Data.ByteString as SB
7676import qualified Data.ByteString.Lazy as LB
@@ -132,18 +132,6 @@ plus p1 p2 =
132132 ok -> ok
133133{-# INLINE plus #-}
134134
135−instance MonadPlus Parser where
136− mzero = zero
137− mplus = plus
138−
139−instance Applicative Parser where
140− pure = return
141− (<*>) = ap
142−
143−instance Alternative Parser where
144− empty = zero
145− (<|>) = plus
146−
147135mkState :: LB.ByteString -> Int64 -> S
148136mkState s = case s of
149137 LB.Empty -> S SB.empty s
@@ -194,26 +182,6 @@ satisfy p =
194182 Nothing -> unParser (nextChunk >> satisfy p) s
195183{-# INLINE satisfy #-}
196184
197−anyWord8 :: Parser Word8
198−anyWord8 = satisfy $ const True
199−{-# INLINE anyWord8 #-}
200−
201−-- | Satisfy a specific character.
202−word8 :: Word8 -> Parser Word8
203−word8 c = satisfy (== c) <?> show c
204−{-# INLINE word8 #-}
205−
206−-- | Satisfy a specific character.
207−notWord8 :: Word8 -> Parser Word8
208−notWord8 c = satisfy (/= c) <?> "not " ++ show c
209−{-# INLINE notWord8 #-}
210−
211−sepBy :: Parser a -> Parser s -> Parser [a]
212−sepBy p s = liftM2 (:) p ((s >> sepBy1 p s) <|> return []) <|> return []
213−
214−sepBy1 :: Parser a -> Parser s -> Parser [a]
215−sepBy1 p s = liftM2 (:) p ((s >> sepBy1 p s) <|> return [])
216−
217185-- | Satisfy a literal string.
218186string :: LB.ByteString -> Parser LB.ByteString
219187string s = Parser $ \(S sb lb n) ->
@@ -253,11 +221,6 @@ stringTransform f s = Parser $ \(S sb lb n) ->
253221 where fs = f s
254222{-# INLINE stringTransform #-}
255223
256−-- | Apply the given parser repeatedly, returning every parse result.
257−count :: Int -> Parser a -> Parser [a]
258−count n p = sequence (replicate n p)
259−{-# INLINE count #-}
260−
261224try :: Parser a -> Parser a
262225try p = Parser $ \s@(S sb lb _) ->
263226 case unParser p s of
@@ -275,13 +238,14 @@ takeAll = Parser $ \(S sb lb n) ->
275238 let bs = sb +: lb
276239 in Right (bs, mkState LB.empty (n + LB.length bs))
277240
278−takeCount :: Int64 -> Parser LB.ByteString
241+takeCount :: Int -> Parser LB.ByteString
279242takeCount k =
280243 Parser $ \(S sb lb n) ->
281244 let bs = sb +: lb
282− (h,t) = LB.splitAt k bs
283− in if LB.length h == k
284− then Right (h, mkState t (n + k))
245+ k' = fromIntegral k
246+ (h,t) = LB.splitAt k' bs
247+ in if LB.length h == k'
248+ then Right (h, mkState t (n + k'))
285249 else Left (bs, [show k ++ " bytes"])
286250
287251-- | Consume characters while the predicate is true.
@@ -308,30 +272,6 @@ takeWhile1 p =
308272 | otherwise -> Right (h, mkState t (n + LB.length h))
309273{-# INLINE takeWhile1 #-}
310274
311−-- | Skip over characters while the predicate is true.
312−skipWhile :: (Word8 -> Bool) -> Parser ()
313−skipWhile p = takeWhile p >> return ()
314−{-# INLINE skipWhile #-}
315−
316−manyTill :: Parser a -> Parser b -> Parser [a]
317−manyTill p end = scan
318− where scan = (end >> return []) <|> liftM2 (:) p scan
319−
320−many :: Parser a -> Parser [a]
321−many p = ((:) <$> p <*> many p) <|> return []
322−
323−many1 :: Parser a -> Parser [a]
324−many1 p = (:) <$> p <*> many p
325−
326−-- |'skipMany' - skip zero or many instances of the parser
327−skipMany :: Parser a -> Parser ()
328−skipMany p = scan
329− where scan = (p >> scan) <|> return ()
330−
331−-- |'skipMany1' - skip one or many instances of the parser
332−skipMany1 :: Parser a -> Parser ()
333−skipMany1 p = p >> skipMany p
334−
335275-- | Test that a parser returned a non-null ByteString.
336276notEmpty :: Parser LB.ByteString -> Parser LB.ByteString
337277notEmpty p = Parser $ \s ->
@@ -391,3 +331,6 @@ parseTest p s =
391331 case parse p s of
392332 (st, Left msg) -> putStrLn $ msg ++ "\nGot:\n" ++ show st
393333 (_, Right r) -> print r
334+
335+#define PARSER Parser
336+#include "Word8Boilerplate.h"