git.iko.soy/typeable/attoparsec
on master help sign in
Unified 'prompt' function from .Internal modules.
commit518628725ae21e3ded851c62c7fab6d76444836f authorMikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com> dateTue 11 Feb 2014, 23:25 (13 years ago) parentaf57e81
5 files changed+55−26
Data/Attoparsec/ByteString/Internal.hs−11 Data/Attoparsec/Internal.hs+30 Data/Attoparsec/Internal/Types.hs+23−1 Data/Attoparsec/Text/Internal.hs−13 Data/Attoparsec/Types.hs+2−1
Data/Attoparsec/ByteString/Internal.hs−11
@@ -121,17 +121,6 @@ ensure !n = T.Parser $ \i0 a0 m0 kf ks ->
121121-- Non-recursive so the bounds check can be inlined:
122122{-# INLINE ensure #-}
123123
124−-- | Ask for input. If we receive any, pass it to a success
125−-- continuation, otherwise to a failure continuation.
126−prompt :: Input -> Added -> More
127− -> (Input -> Added -> More -> Result r)
128− -> (Input -> Added -> More -> Result r)
129− -> Result r
130−prompt i0 a0 _m0 kf ks = Partial $ \s ->
131− if B.null s
132− then kf i0 a0 Complete
133− else ks (i0 <> I s) (a0 <> A s) Incomplete
134−
135124-- | Immediately demand more input via a 'Partial' continuation
136125-- result.
137126demandInput :: Parser ()
Data/Attoparsec/Internal.hs+30
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12-- |
23-- Module : Data.Attoparsec.Internal
34-- Copyright : Bryan O'Sullivan 2012
@@ -15,8 +16,13 @@ module Data.Attoparsec.Internal
1516 compareResults
1617 , get
1718 , put
19+ , prompt
1820 ) where
1921
22+#if __GLASGOW_HASKELL__ >= 700
23+import Data.ByteString (ByteString)
24+import Data.Text (Text)
25+#endif
2026import Data.Attoparsec.Internal.Types
2127
2228-- | Compare two 'IResult' values for equality.
@@ -39,3 +45,27 @@ get = Parser $ \i0 a0 m0 _kf ks -> ks i0 a0 m0 (unI i0)
3945put :: t -> Parser t ()
4046put c = Parser $ \_i0 a0 m0 _kf ks -> ks (I c) a0 m0 ()
4147{-# INLINE put #-}
48+
49+-- | Ask for input. If we receive any, pass it to a success
50+-- continuation, otherwise to a failure continuation.
51+prompt :: Chunk t
52+ => Input t -> Added t -> More
53+ -> (Input t -> Added t -> More -> IResult t r)
54+ -> (Input t -> Added t -> More -> IResult t r)
55+ -> IResult t r
56+prompt i0 a0 _m0 kf ks = Partial $ \s ->
57+ if nullChunk s
58+ then kf i0 a0 Complete
59+ else ks (i0 <> I s) (a0 <> A s) Incomplete
60+#if __GLASGOW_HASKELL__ >= 700
61+{-# SPECIALIZE prompt :: Input ByteString -> Added ByteString -> More
62+ -> (Input ByteString -> Added ByteString -> More
63+ -> IResult ByteString r)
64+ -> (Input ByteString -> Added ByteString -> More
65+ -> IResult ByteString r)
66+ -> IResult ByteString r #-}
67+{-# SPECIALIZE prompt :: Input Text -> Added Text -> More
68+ -> (Input Text -> Added Text -> More -> IResult Text r)
69+ -> (Input Text -> Added Text-> More -> IResult Text r)
70+ -> IResult Text r #-}
71+#endif
Data/Attoparsec/Internal/Types.hs+23−1
@@ -1,5 +1,5 @@
11{-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving, OverloadedStrings,
2− Rank2Types, RecordWildCards #-}
2+ Rank2Types, RecordWildCards, TypeFamilies #-}
33-- |
44-- Module : Data.Attoparsec.Internal.Types
55-- Copyright : Bryan O'Sullivan 2007-2011
@@ -23,11 +23,17 @@ module Data.Attoparsec.Internal.Types
2323 , More(..)
2424 , addS
2525 , (<>)
26+ , Chunk(..)
2627 ) where
2728
2829import Control.Applicative (Alternative(..), Applicative(..), (<$>))
2930import Control.DeepSeq (NFData(rnf))
3031import Control.Monad (MonadPlus(..))
32+import Data.Word (Word8)
33+import Data.ByteString (ByteString)
34+import qualified Data.ByteString as BS
35+import Data.Text (Text)
36+import qualified Data.Text as T
3137import Data.Monoid (Monoid(..))
3238import Prelude hiding (getChar, take, takeWhile)
3339
@@ -225,3 +231,19 @@ failDesc err = Parser (\i0 a0 m0 kf _ks -> kf i0 a0 m0 [] msg)
225231(<>) :: (Monoid m) => m -> m -> m
226232(<>) = mappend
227233{-# INLINE (<>) #-}
234+
235+-- | A common interface for input chunks.
236+class Monoid c => Chunk c where
237+ type ChunkElem c
238+ -- | Test if the chunk is empty.
239+ nullChunk :: c -> Bool
240+
241+instance Chunk ByteString where
242+ type ChunkElem ByteString = Word8
243+ nullChunk = BS.null
244+ {-# INLINE nullChunk #-}
245+
246+instance Chunk Text where
247+ type ChunkElem Text = Char
248+ nullChunk = T.null
249+ {-# INLINE nullChunk #-}
Data/Attoparsec/Text/Internal.hs−13
@@ -83,8 +83,6 @@ import qualified Data.Text.Lazy as L
8383
8484type Parser = T.Parser Text
8585type Result = IResult Text
86−type Input = T.Input Text
87−type Added = T.Added Text
8886type Failure r = T.Failure Text r
8987type Success a r = T.Success Text a r
9088
@@ -109,17 +107,6 @@ ensure !n = T.Parser $ \i0 a0 m0 kf ks ->
109107 else runParser (demandInput >> go n') i0 a0 m0 kf ks
110108{-# INLINE ensure #-}
111109
112−-- | Ask for input. If we receive any, pass it to a success
113−-- continuation, otherwise to a failure continuation.
114−prompt :: Input -> Added -> More
115− -> (Input -> Added -> More -> Result r)
116− -> (Input -> Added -> More -> Result r)
117− -> Result r
118−prompt i0 a0 _m0 kf ks = Partial $ \s ->
119− if T.null s
120− then kf i0 a0 Complete
121− else ks (i0 <> I s) (a0 <> A s) Incomplete
122−
123110-- | Immediately demand more input via a 'Partial' continuation
124111-- result.
125112demandInput :: Parser ()
Data/Attoparsec/Types.hs+2−1
@@ -14,6 +14,7 @@ module Data.Attoparsec.Types
1414 (
1515 Parser
1616 , IResult(..)
17+ , Chunk(..)
1718 ) where
1819
19−import Data.Attoparsec.Internal.Types (Parser(..), IResult(..))
20+import Data.Attoparsec.Internal.Types (Parser(..), IResult(..), Chunk(..))