git.iko.soy/typeable/attoparsec
on master help sign in
Turn Pos into a newtype

Preparation for trying to speed Text ops back up.

commit77c5894a9e9d2861e2812a0c6cc11816d277b2ec authorBryan O'Sullivan <bos@serpentine.com> dateThu 8 May 2014, 16:13 (12 years ago) parent2570e3f
3 files changed+35−30
Data/Attoparsec/ByteString/Internal.hs+20−18 Data/Attoparsec/Internal/Types.hs+3−2 Data/Attoparsec/Text/Internal.hs+12−10
Data/Attoparsec/ByteString/Internal.hs+20−18
@@ -371,15 +371,15 @@ notWord8 c = satisfy (/= c) <?> "not " ++ show c
371371-- combinators such as 'many', because such parsers loop until a
372372-- failure occurs. Careless use will thus result in an infinite loop.
373373peekWord8 :: Parser (Maybe Word8)
374−peekWord8 = T.Parser $ \t pos more _lose succ ->
374+peekWord8 = T.Parser $ \t pos@(Pos pos_) more _lose succ ->
375375 case () of
376− _| pos < B.length t ->
377− let !w = B.unsafeIndex t pos
376+ _| pos_ < B.length t ->
377+ let !w = B.unsafeIndex t pos_
378378 in succ t pos more (Just w)
379379 | more == Complete ->
380380 succ t pos more Nothing
381381 | otherwise ->
382− let succ' t' pos' more' = let !w = B.unsafeIndex t pos
382+ let succ' t' pos' more' = let !w = B.unsafeIndex t pos_
383383 in succ t' pos' more' (Just w)
384384 lose' t' pos' more' = succ t' pos' more' Nothing
385385 in prompt t pos more lose' succ'
@@ -400,22 +400,22 @@ endOfLine = (word8 10 >> return ()) <|> (string "\r\n" >> return ())
400400
401401-- | Terminal failure continuation.
402402failK :: Failure a
403−failK t pos _more stack msg = Fail (B.unsafeDrop pos t) stack msg
403+failK t (Pos pos) _more stack msg = Fail (B.unsafeDrop pos t) stack msg
404404{-# INLINE failK #-}
405405
406406-- | Terminal success continuation.
407407successK :: Success a a
408−successK t pos _more a = Done (B.unsafeDrop pos t) a
408+successK t (Pos pos) _more a = Done (B.unsafeDrop pos t) a
409409{-# INLINE successK #-}
410410
411411-- | Run a parser.
412412parse :: Parser a -> B.ByteString -> Result a
413−parse m s = T.runParser m s 0 Incomplete failK successK
413+parse m s = T.runParser m s (Pos 0) Incomplete failK successK
414414{-# INLINE parse #-}
415415
416416-- | Run a parser that cannot be resupplied via a 'Partial' result.
417417parseOnly :: Parser a -> B.ByteString -> Either String a
418−parseOnly m s = case T.runParser m s 0 Complete failK successK of
418+parseOnly m s = case T.runParser m s (Pos 0) Complete failK successK of
419419 Fail _ _ err -> Left err
420420 Done _ a -> Right a
421421 _ -> error "parseOnly: impossible error!"
@@ -430,16 +430,18 @@ inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r
430430{-# INLINE inlinePerformIO #-}
431431
432432get :: Parser ByteString
433−get = T.Parser $ \t pos more _lose succ -> succ t pos more (B.unsafeDrop pos t)
433+get = T.Parser $ \t pos more _lose succ ->
434+ succ t pos more (B.unsafeDrop (fromPos pos) t)
434435{-# INLINE get #-}
435436
436437endOfChunk :: Parser Bool
437438endOfChunk = T.Parser $ \t pos more _lose succ ->
438− succ t pos more (pos == B.length t)
439+ succ t pos more (fromPos pos == B.length t)
439440{-# INLINE endOfChunk #-}
440441
441442advance :: Int -> Parser ()
442−advance n = T.Parser $ \t pos more _lose succ -> succ t (pos+n) more ()
443+advance n = T.Parser $ \t pos more _lose succ ->
444+ succ t (pos + Pos n) more ()
443445{-# INLINE advance #-}
444446
445447ensureSuspended :: Int -> ByteString -> Pos -> More
@@ -450,7 +452,7 @@ ensureSuspended n t pos more lose succ =
450452 runParser (demandInput >> go) t pos more lose succ
451453 where go = T.Parser $ \t' pos' more' lose' succ' ->
452454 if lengthAtLeast pos' n t'
453− then succ' t' pos' more' (substring pos n t')
455+ then succ' t' pos' more' (substring pos (Pos n) t')
454456 else runParser (demandInput >> go) t' pos' more' lose' succ'
455457
456458-- | If at least @n@ elements of input are available, return the
@@ -458,7 +460,7 @@ ensureSuspended n t pos more lose succ =
458460ensure :: Int -> Parser ByteString
459461ensure n = T.Parser $ \t pos more lose succ ->
460462 if lengthAtLeast pos n t
461− then succ t pos more (substring pos n t)
463+ then succ t pos more (substring pos (Pos n) t)
462464 -- The uncommon case is kept out-of-line to reduce code size:
463465 else ensureSuspended n t pos more lose succ
464466-- Non-recursive so the bounds check can be inlined:
@@ -511,8 +513,8 @@ endOfInput = T.Parser $ \t pos more lose succ ->
511513-- portion of the input that was consumed while it was being parsed.
512514match :: Parser a -> Parser (ByteString, a)
513515match p = T.Parser $ \t pos more lose succ ->
514− let succ' t' pos' more' a = succ t' pos' more'
515− (substring pos (pos'-pos) t', a)
516+ let succ' t' pos' more' a =
517+ succ t' pos' more' (substring pos (pos'-pos) t', a)
516518 in runParser p t pos more lose succ'
517519
518520-- | Return an indication of whether the end of input has been
@@ -522,9 +524,9 @@ atEnd = not <$> wantInput
522524{-# INLINE atEnd #-}
523525
524526lengthAtLeast :: Pos -> Int -> ByteString -> Bool
525−lengthAtLeast pos n bs = B.length bs >= pos + n
527+lengthAtLeast (Pos pos) n bs = B.length bs >= pos + n
526528{-# INLINE lengthAtLeast #-}
527529
528−substring :: Pos -> Int -> ByteString -> ByteString
529−substring pos n bs = B.unsafeTake n (B.unsafeDrop pos bs)
530+substring :: Pos -> Pos -> ByteString -> ByteString
531+substring (Pos pos) (Pos n) bs = B.unsafeTake n (B.unsafeDrop pos bs)
530532{-# INLINE substring #-}
Data/Attoparsec/Internal/Types.hs+3−2
@@ -17,7 +17,7 @@ module Data.Attoparsec.Internal.Types
1717 Parser(..)
1818 , Failure
1919 , Success
20− , Pos
20+ , Pos(..)
2121 , IResult(..)
2222 , More(..)
2323 , (<>)
@@ -29,7 +29,8 @@ import Control.Monad (MonadPlus(..))
2929import Data.Monoid (Monoid(..))
3030import Prelude hiding (getChar, succ)
3131
32−type Pos = Int
32+newtype Pos = Pos { fromPos :: Int }
33+ deriving (Eq, Ord, Show, Num)
3334
3435-- | The result of a parse. This is parameterised over the type @t@
3536-- of string that was processed.
Data/Attoparsec/Text/Internal.hs+12−10
@@ -382,12 +382,12 @@ peekChar :: Parser (Maybe Char)
382382peekChar = T.Parser $ \t pos more _lose succ ->
383383 case () of
384384 _| lengthAtLeast pos 1 t ->
385− let !c = T.head (T.drop pos t)
385+ let !c = T.head (T.drop (fromPos pos) t)
386386 in succ t pos more (Just c)
387387 | more == Complete ->
388388 succ t pos more Nothing
389389 | otherwise ->
390− let succ' t' pos' more' = let !c = T.head (T.drop pos t')
390+ let succ' t' pos' more' = let !c = T.head (T.drop (fromPos pos) t')
391391 in succ t' pos' more' (Just c)
392392 lose' t' pos' more' = succ t' pos' more' Nothing
393393 in prompt t pos more lose' succ'
@@ -408,12 +408,12 @@ endOfLine = (char '\n' >> return ()) <|> (string "\r\n" >> return ())
408408
409409-- | Terminal failure continuation.
410410failK :: Failure a
411−failK t pos _more stack msg = Fail (T.drop pos t) stack msg
411+failK t (Pos pos) _more stack msg = Fail (T.drop pos t) stack msg
412412{-# INLINE failK #-}
413413
414414-- | Terminal success continuation.
415415successK :: Success a a
416−successK t pos _more a = Done (T.drop pos t) a
416+successK t (Pos pos) _more a = Done (T.drop pos t) a
417417{-# INLINE successK #-}
418418
419419-- | Run a parser.
@@ -430,16 +430,18 @@ parseOnly m s = case runParser m s 0 Complete failK successK of
430430{-# INLINE parseOnly #-}
431431
432432get :: Parser Text
433−get = T.Parser $ \t pos more _lose succ -> succ t pos more (T.drop pos t)
433+get = T.Parser $ \t pos more _lose succ ->
434+ succ t pos more (T.drop (fromPos pos) t)
434435{-# INLINE get #-}
435436
436437endOfChunk :: Parser Bool
437438endOfChunk = T.Parser $ \t pos more _lose succ ->
438− succ t pos more (pos == T.length t)
439+ succ t pos more (fromPos pos == T.length t)
439440{-# INLINE endOfChunk #-}
440441
441442advance :: Int -> Parser ()
442−advance n = T.Parser $ \t pos more _lose succ -> succ t (pos+n) more ()
443+advance n = T.Parser $ \t pos more _lose succ ->
444+ succ t (Pos ((fromPos pos) + n)) more ()
443445{-# INLINE advance #-}
444446
445447ensureSuspended :: Int -> Text -> Pos -> More
@@ -512,7 +514,7 @@ endOfInput = T.Parser $ \t pos more lose succ ->
512514match :: Parser a -> Parser (Text, a)
513515match p = T.Parser $ \t pos more lose succ ->
514516 let succ' t' pos' more' a = succ t' pos' more'
515− (substring pos (pos'-pos) t', a)
517+ (substring pos (fromPos pos'-fromPos pos) t', a)
516518 in runParser p t pos more lose succ'
517519
518520-- | Return an indication of whether the end of input has been
@@ -523,9 +525,9 @@ atEnd = not <$> wantInput
523525
524526lengthAtLeast :: Pos -> Int -> Text -> Bool
525527lengthAtLeast pos n t = T.lengthWord16 t `quot` 2 >= o || T.length t >= o
526− where o = pos + n
528+ where o = fromPos pos + n
527529{-# INLINE lengthAtLeast #-}
528530
529531substring :: Pos -> Int -> Text -> Text
530−substring pos n t = T.take n (T.drop pos t)
532+substring (Pos pos) n t = T.take n (T.drop pos t)
531533{-# INLINE substring #-}