@@ -1,3 +1,4 @@
1-----------------------------------------------------------------------------
2-----------------------------------------------------------------------------
3-- Module : Data.ParserCombinators.Attoparsec.Internal
4-- Module : Data.ParserCombinators.Attoparsec.Internal
@@ -28,7 +29,6 @@ module Data.ParserCombinators.Attoparsec.Internal
29 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
30 -- * Things vaguely like those in @Parsec.Combinator@ (and @Parsec.Prim@)
33 , manyTill
33 , manyTill
@@ -69,8 +69,8 @@ module Data.ParserCombinators.Attoparsec.Internal
72−import Control.Applicative (Alternative(..), Applicative(..), (<$>))
72+import Control.Applicative
73−import Control.Monad (MonadPlus(..), ap, liftM2)
73+import Control.Monad (MonadPlus(..), ap)
74import Control.Monad.Fix (MonadFix(..))
74import Control.Monad.Fix (MonadFix(..))
75import qualified Data.ByteString as SB
75import qualified Data.ByteString as SB
76import qualified Data.ByteString.Lazy as LB
76import qualified Data.ByteString.Lazy as LB
@@ -132,18 +132,6 @@ plus p1 p2 =
133{-# INLINE plus #-}
133{-# INLINE plus #-}
135−instance MonadPlus Parser where
139−instance Applicative Parser where
143−instance Alternative Parser where
147mkState :: LB.ByteString -> Int64 -> S
135mkState :: LB.ByteString -> Int64 -> S
148mkState s = case s of
136mkState s = case s of
149 LB.Empty -> S SB.empty s
137 LB.Empty -> S SB.empty s
@@ -194,26 +182,6 @@ satisfy p =
194 Nothing -> unParser (nextChunk >> satisfy p) s
182 Nothing -> unParser (nextChunk >> satisfy p) s
195{-# INLINE satisfy #-}
183{-# INLINE satisfy #-}
197−anyWord8 :: Parser Word8
198−anyWord8 = satisfy $ const True
199−{-# INLINE anyWord8 #-}
201−-- | Satisfy a specific character.
202−word8 :: Word8 -> Parser Word8
203−word8 c = satisfy (== c) <?> show c
206−-- | Satisfy a specific character.
207−notWord8 :: Word8 -> Parser Word8
208−notWord8 c = satisfy (/= c) <?> "not " ++ show c
209−{-# INLINE notWord8 #-}
211−sepBy :: Parser a -> Parser s -> Parser [a]
212−sepBy p s = liftM2 (:) p ((s >> sepBy1 p s) <|> return []) <|> return []
214−sepBy1 :: Parser a -> Parser s -> Parser [a]
215−sepBy1 p s = liftM2 (:) p ((s >> sepBy1 p s) <|> return [])
217-- | Satisfy a literal string.
185-- | Satisfy a literal string.
218string :: LB.ByteString -> Parser LB.ByteString
186string :: LB.ByteString -> Parser LB.ByteString
219string s = Parser $ \(S sb lb n) ->
187string s = Parser $ \(S sb lb n) ->
@@ -253,11 +221,6 @@ stringTransform f s = Parser $ \(S sb lb n) ->
253 where fs = f s
221 where fs = f s
254{-# INLINE stringTransform #-}
222{-# INLINE stringTransform #-}
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)
261try :: Parser a -> Parser a
224try :: Parser a -> Parser a
262try p = Parser $ \s@(S sb lb _) ->
225try p = Parser $ \s@(S sb lb _) ->
263 case unParser p s of
226 case unParser p s of
@@ -275,13 +238,14 @@ takeAll = Parser $ \(S sb lb n) ->
275 let bs = sb +: lb
238 let bs = sb +: lb
276 in Right (bs, mkState LB.empty (n + LB.length bs))
239 in Right (bs, mkState LB.empty (n + LB.length bs))
278−takeCount :: Int64 -> Parser LB.ByteString
241+takeCount :: Int -> Parser LB.ByteString
279takeCount k =
242takeCount k =
280 Parser $ \(S sb lb n) ->
243 Parser $ \(S sb lb n) ->
281 let bs = sb +: lb
244 let bs = sb +: lb
282− (h,t) = LB.splitAt k bs
246+ (h,t) = LB.splitAt k' bs
283− in if LB.length h == k
247+ in if LB.length h == k'
284− then Right (h, mkState t (n + k))
248+ then Right (h, mkState t (n + k'))
285 else Left (bs, [show k ++ " bytes"])
249 else Left (bs, [show k ++ " bytes"])
287-- | Consume characters while the predicate is true.
251-- | Consume characters while the predicate is true.
@@ -308,30 +272,6 @@ takeWhile1 p =
308 | otherwise -> Right (h, mkState t (n + LB.length h))
272 | otherwise -> Right (h, mkState t (n + LB.length h))
309{-# INLINE takeWhile1 #-}
273{-# INLINE takeWhile1 #-}
311−-- | Skip over characters while the predicate is true.
312−skipWhile :: (Word8 -> Bool) -> Parser ()
313−skipWhile p = takeWhile p >> return ()
314−{-# INLINE skipWhile #-}
316−manyTill :: Parser a -> Parser b -> Parser [a]
317−manyTill p end = scan
318− where scan = (end >> return []) <|> liftM2 (:) p scan
320−many :: Parser a -> Parser [a]
321−many p = ((:) <$> p <*> many p) <|> return []
323−many1 :: Parser a -> Parser [a]
324−many1 p = (:) <$> p <*> many p
326−-- |'skipMany' - skip zero or many instances of the parser
327−skipMany :: Parser a -> Parser ()
329− where scan = (p >> scan) <|> return ()
331−-- |'skipMany1' - skip one or many instances of the parser
332−skipMany1 :: Parser a -> Parser ()
333−skipMany1 p = p >> skipMany p
335-- | Test that a parser returned a non-null ByteString.
275-- | Test that a parser returned a non-null ByteString.
336notEmpty :: Parser LB.ByteString -> Parser LB.ByteString
276notEmpty :: Parser LB.ByteString -> Parser LB.ByteString
337notEmpty p = Parser $ \s ->
277notEmpty p = Parser $ \s ->
@@ -391,3 +331,6 @@ parseTest p s =
391 case parse p s of
331 case parse p s of
392 (st, Left msg) -> putStrLn $ msg ++ "\nGot:\n" ++ show st
332 (st, Left msg) -> putStrLn $ msg ++ "\nGot:\n" ++ show st
393 (_, Right r) -> print r
333 (_, Right r) -> print r
335+#define PARSER Parser
336+#include "Word8Boilerplate.h"