git.iko.soy/typeable/attoparsec
on master help sign in
Add new lookahead functions
commitda754bb3130575b482c7536a4dc51f1f7f7322bb authorBryan O'Sullivan <bos@serpentine.com> dateTue 26 Nov 2013, 21:24 (13 years ago) parenta2351e7
5 files changed+45−8
Data/Attoparsec/ByteString.hs+4−1 Data/Attoparsec/ByteString/Char8.hs+12−3 Data/Attoparsec/ByteString/Internal.hs+13−2 Data/Attoparsec/Text.hs+3 Data/Attoparsec/Text/Internal.hs+13−2
Data/Attoparsec/ByteString.hs+4−1
@@ -47,11 +47,14 @@ module Data.Attoparsec.ByteString
4747 , I.word8
4848 , I.anyWord8
4949 , I.notWord8
50− , I.peekWord8
5150 , I.satisfy
5251 , I.satisfyWith
5352 , I.skip
5453
54+ -- ** Lookahead
55+ , I.peekWord8
56+ , I.peekWord8'
57+
5558 -- ** Byte classes
5659 , I.inClass
5760 , I.notInClass
Data/Attoparsec/ByteString/Char8.hs+12−3
@@ -46,9 +46,12 @@ module Data.Attoparsec.ByteString.Char8
4646 , char8
4747 , anyChar
4848 , notChar
49− , peekChar
5049 , satisfy
5150
51+ -- ** Lookahead
52+ , peekChar
53+ , peekChar'
54+
5255 -- ** Special character parsers
5356 , digit
5457 , letter_iso8859_15
@@ -223,8 +226,8 @@ anyChar :: Parser Char
223226anyChar = satisfy $ const True
224227{-# INLINE anyChar #-}
225228
226−-- | Match any character. Returns 'Nothing' if end of input has been
227−-- reached. Does not consume any input.
229+-- | Match any character, to perform lookahead. Returns 'Nothing' if
230+-- end of input has been reached. Does not consume any input.
228231--
229232-- /Note/: Because this parser does not fail, do not use it with
230233-- combinators such as 'many', because such parsers loop until a
@@ -233,6 +236,12 @@ peekChar :: Parser (Maybe Char)
233236peekChar = (fmap w2c) `fmap` I.peekWord8
234237{-# INLINE peekChar #-}
235238
239+-- | Match any character, to perform lookahead. Does not consume any
240+-- input, but will fail if end of input has been reached.
241+peekChar' :: Parser Char
242+peekChar' = w2c `fmap` I.peekWord8'
243+{-# INLINE peekChar' #-}
244+
236245-- | Fast predicate for matching ASCII space characters.
237246--
238247-- /Note/: This predicate only gives correct answers for the ASCII
Data/Attoparsec/ByteString/Internal.hs+13−2
@@ -34,7 +34,10 @@ module Data.Attoparsec.ByteString.Internal
3434 , skip
3535 , word8
3636 , notWord8
37+
38+ -- ** Lookahead
3739 , peekWord8
40+ , peekWord8'
3841
3942 -- ** Byte classes
4043 , inClass
@@ -426,8 +429,8 @@ notWord8 :: Word8 -> Parser Word8
426429notWord8 c = satisfy (/= c) <?> "not " ++ show c
427430{-# INLINE notWord8 #-}
428431
429−-- | Match any byte. Returns 'Nothing' if end of input has been
430−-- reached. Does not consume any input.
432+-- | Match any byte, to perform lookahead. Returns 'Nothing' if end of
433+-- input has been reached. Does not consume any input.
431434--
432435-- /Note/: Because this parser does not fail, do not use it with
433436-- combinators such as 'many', because such parsers loop until a
@@ -445,6 +448,14 @@ peekWord8 = T.Parser $ \i0 a0 m0 _kf ks ->
445448 in ks i0 a0 m0 (Just w)
446449{-# INLINE peekWord8 #-}
447450
451+-- | Match any byte, to perform lookahead. Does not consume any
452+-- input, but will fail if end of input has been reached.
453+peekWord8' :: Parser Word8
454+peekWord8' = do
455+ s <- ensure 1
456+ return $! B.unsafeHead s
457+{-# INLINE peekWord8' #-}
458+
448459-- | Match only if all input has been consumed.
449460endOfInput :: Parser ()
450461endOfInput = T.Parser $ \i0 a0 m0 kf ks ->
Data/Attoparsec/Text.hs+3
@@ -52,7 +52,10 @@ module Data.Attoparsec.Text
5252 , I.satisfy
5353 , I.satisfyWith
5454 , I.skip
55+
56+ -- ** Lookahead
5557 , I.peekChar
58+ , I.peekChar'
5659
5760 -- ** Special character parsers
5861 , digit
Data/Attoparsec/Text/Internal.hs+13−2
@@ -35,7 +35,10 @@ module Data.Attoparsec.Text.Internal
3535 , skip
3636 , char
3737 , notChar
38+
39+ -- ** Lookahead
3840 , peekChar
41+ , peekChar'
3942
4043 -- ** Character classes
4144 , inClass
@@ -440,8 +443,8 @@ notChar :: Char -> Parser Char
440443notChar c = satisfy (/= c) <?> "not " ++ show c
441444{-# INLINE notChar #-}
442445
443−-- | Match any character. Returns 'Nothing' if end of input has been
444−-- reached. Does not consume any input.
446+-- | Match any character, to perform lookahead. Returns 'Nothing' if
447+-- end of input has been reached. Does not consume any input.
445448--
446449-- /Note/: Because this parser does not fail, do not use it with
447450-- combinators such as 'many', because such parsers loop until a
@@ -459,6 +462,14 @@ peekChar = T.Parser $ \i0 a0 m0 _kf ks ->
459462 in ks i0 a0 m0 (Just c)
460463{-# INLINE peekChar #-}
461464
465+-- | Match any character, to perform lookahead. Does not consume any
466+-- input, but will fail if end of input has been reached.
467+peekChar' :: Parser Char
468+peekChar' = do
469+ s <- ensure 1
470+ return $! unsafeHead s
471+{-# INLINE peekChar' #-}
472+
462473-- | Match only if all input has been consumed.
463474endOfInput :: Parser ()
464475endOfInput = T.Parser $ \i0 a0 m0 kf ks ->