git.iko.soy/typeable/attoparsec
on master help sign in
Added back the lookAhead combinator
commitdf136e5be4abcf6ada9b7e8250fac5dae6072d5d authorGergely Risko <errge@nilcons.com> dateMon 23 Jun 2014, 18:41 (12 years ago) parent6791bcd
2 files changed+20−1
Data/Attoparsec/Combinator.hs+8 tests/QC/Combinator.hs+12−1
Data/Attoparsec/Combinator.hs+8
@@ -33,6 +33,7 @@ module Data.Attoparsec.Combinator
3333 , satisfyElem
3434 , endOfInput
3535 , atEnd
36+ , lookAhead
3637 ) where
3738
3839import Control.Applicative (Alternative(..), Applicative(..), empty, liftA2,
@@ -240,3 +241,10 @@ feed f@(Fail _ _ _) _ = f
240241feed (Partial k) d = k d
241242feed (Done t r) d = Done (mappend t d) r
242243{-# INLINE feed #-}
244+
245+-- | Apply a parser without consuming any input.
246+lookAhead :: Parser i a -> Parser i a
247+lookAhead p = Parser $ \t pos more lose succ ->
248+ let succ' t' _pos' more' = succ t' pos more'
249+ in runParser p t pos more lose succ'
250+{-# INLINE lookAhead #-}
tests/QC/Combinator.hs+12−1
@@ -1,8 +1,11 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
13module QC.Combinator where
24
35import Control.Applicative
6+import Data.Maybe (fromJust, isJust)
47import Data.Word (Word8)
5−import QC.Common (Repack, parseBS, repackBS)
8+import QC.Common (Repack, parseBS, repackBS, toLazyBS)
69import Test.Framework (Test)
710import Test.Framework.Providers.QuickCheck2 (testProperty)
811import Test.QuickCheck
@@ -22,6 +25,13 @@ count (Positive (Small n)) rs s =
2225 (length <$> parseBS (C.count n (P.string s)) input) == Just n
2326 where input = repackBS rs (B.concat (replicate (n+1) s))
2427
28+lookAhead :: NonEmptyList Word8 -> Bool
29+lookAhead (NonEmpty xs) =
30+ let ys = B.pack xs
31+ withLookAheadThenConsume = (\x y -> (x, y)) <$> C.lookAhead (P.string ys) <*> P.string ys
32+ mr = parseBS withLookAheadThenConsume $ toLazyBS ys
33+ in isJust mr && fst (fromJust mr) == snd (fromJust mr)
34+
2535match :: Int -> NonNegative Int -> NonNegative Int -> Repack -> Bool
2636match n (NonNegative x) (NonNegative y) rs =
2737 parseBS (P.match parser) (repackBS rs input) == Just (input, n)
@@ -35,5 +45,6 @@ tests :: [Test]
3545tests = [
3646 testProperty "choice" choice
3747 , testProperty "count" count
48+ , testProperty "lookAhead" lookAhead
3849 , testProperty "match" match
3950 ]