Commits

Bryan O'Sullivan committed 6e4cfda

Add new lookahead functions

Comments (0)

Files changed (5)

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

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

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 ->

Data/Attoparsec/Text.hs

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

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 ->
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.