Commits

Bryan O'Sullivan  committed 6e4cfda

Add new lookahead functions

  • Participants
  • Parent commits dcb0517

Comments (0)

Files changed (5)

File Data/Attoparsec/ByteString.hs

     , I.word8
     , I.anyWord8
     , I.notWord8
-    , I.peekWord8
     , I.satisfy
     , I.satisfyWith
     , I.skip
 
+    -- ** Lookahead
+    , I.peekWord8
+    , I.peekWord8'
+
     -- ** Byte classes
     , I.inClass
     , I.notInClass

File Data/Attoparsec/ByteString/Char8.hs

     , char8
     , anyChar
     , notChar
+    , satisfy
+
+    -- ** Lookahead
     , peekChar
-    , satisfy
+    , peekChar'
 
     -- ** Special character parsers
     , digit
 anyChar = satisfy $ const True
 {-# INLINE anyChar #-}
 
--- | Match any character. Returns 'Nothing' if end of input has been
--- reached. Does not consume any input.
+-- | Match any character, to perform lookahead. Returns 'Nothing' if
+-- end of input has been reached. Does not consume any input.
 --
 -- /Note/: Because this parser does not fail, do not use it with
 -- combinators such as 'many', because such parsers loop until a
 peekChar = (fmap w2c) `fmap` I.peekWord8
 {-# INLINE peekChar #-}
 
+-- | Match any character, to perform lookahead.  Does not consume any
+-- input, but will fail if end of input has been reached.
+peekChar' :: Parser Char
+peekChar' = w2c `fmap` I.peekWord8'
+{-# INLINE peekChar' #-}
+
 -- | Fast predicate for matching ASCII space characters.
 --
 -- /Note/: This predicate only gives correct answers for the ASCII

File Data/Attoparsec/ByteString/Internal.hs

     , skip
     , word8
     , notWord8
+
+    -- ** Lookahead
     , peekWord8
+    , peekWord8'
 
     -- ** Byte classes
     , inClass
 notWord8 c = satisfy (/= c) <?> "not " ++ show c
 {-# INLINE notWord8 #-}
 
--- | Match any byte. Returns 'Nothing' if end of input has been
--- reached. Does not consume any input.
+-- | Match any byte, to perform lookahead. Returns 'Nothing' if end of
+-- input has been reached. Does not consume any input.
 --
 -- /Note/: Because this parser does not fail, do not use it with
 -- combinators such as 'many', because such parsers loop until a
                  in ks i0 a0 m0 (Just w)
 {-# INLINE peekWord8 #-}
 
+-- | Match any byte, to perform lookahead.  Does not consume any
+-- input, but will fail if end of input has been reached.
+peekWord8' :: Parser Word8
+peekWord8' = do
+  s <- ensure 1
+  return $! B.unsafeHead s
+{-# INLINE peekWord8' #-}
+
 -- | Match only if all input has been consumed.
 endOfInput :: Parser ()
 endOfInput = T.Parser $ \i0 a0 m0 kf ks ->

File Data/Attoparsec/Text.hs

     , I.satisfy
     , I.satisfyWith
     , I.skip
+
+    -- ** Lookahead
     , I.peekChar
+    , I.peekChar'
 
     -- ** Special character parsers
     , digit

File Data/Attoparsec/Text/Internal.hs

     , skip
     , char
     , notChar
+
+    -- ** Lookahead
     , peekChar
+    , peekChar'
 
     -- ** Character classes
     , inClass
 notChar c = satisfy (/= c) <?> "not " ++ show c
 {-# INLINE notChar #-}
 
--- | Match any character. Returns 'Nothing' if end of input has been
--- reached. Does not consume any input.
+-- | Match any character, to perform lookahead. Returns 'Nothing' if
+-- end of input has been reached. Does not consume any input.
 --
 -- /Note/: Because this parser does not fail, do not use it with
 -- combinators such as 'many', because such parsers loop until a
                 in ks i0 a0 m0 (Just c)
 {-# INLINE peekChar #-}
 
+-- | Match any character, to perform lookahead.  Does not consume any
+-- input, but will fail if end of input has been reached.
+peekChar' :: Parser Char
+peekChar' = do
+  s <- ensure 1
+  return $! unsafeHead s
+{-# INLINE peekChar' #-}
+
 -- | Match only if all input has been consumed.
 endOfInput :: Parser ()
 endOfInput = T.Parser $ \i0 a0 m0 kf ks ->