git.iko.soy/typeable/html-parse
on master help sign in
Add parseTokensLazy
3fe194cBen Gamari
Text/HTML/Parser.hs+16−1
@@ -8,6 +8,7 @@
8module Text.HTML.Parser
8module Text.HTML.Parser
9 ( -- * Parsing
9 ( -- * Parsing
10 parseTokens
10 parseTokens
11+ , parseTokensLazy
11 , token
12 , token
12 -- * Types
13 -- * Types
13 , Token(..)
14 , Token(..)
@@ -24,8 +25,10 @@ import Control.Monad (guard)
24import Control.DeepSeq
25import Control.DeepSeq
25
26
26import Data.Attoparsec.Text
27import Data.Attoparsec.Text
28+import qualified Data.Attoparsec.Text.Lazy as AL
27import Data.Text (Text)
29import Data.Text (Text)
28import qualified Data.Text as T
30import qualified Data.Text as T
31+import qualified Data.Text.Lazy as TL
29import Data.Text.Lazy.Builder (Builder)
32import Data.Text.Lazy.Builder (Builder)
30import qualified Data.Text.Lazy.Builder as B
33import qualified Data.Text.Lazy.Builder as B
31import Prelude hiding (take, takeWhile)
34import Prelude hiding (take, takeWhile)
@@ -246,7 +249,7 @@ doctype = do
246bogusComment :: Parser Token
249bogusComment :: Parser Token
247bogusComment = fail "Bogus comment"
250bogusComment = fail "Bogus comment"
248
251
249−-- | Parse a lazy list of tokens.
252+-- | Parse a lazy list of tokens from strict 'Text'.
250parseTokens :: Text -> [Token]
253parseTokens :: Text -> [Token]
251parseTokens = unfoldr f
254parseTokens = unfoldr f
252 where
255 where
@@ -257,3 +260,15 @@ parseTokens = unfoldr f
257 case parse token t of
260 case parse token t of
258 Done rest tok -> Just (tok, rest)
261 Done rest tok -> Just (tok, rest)
259 _ -> Nothing
262 _ -> Nothing
263+
264+-- | Parse a lazy list of tokens from lazy 'TL.Text'.
265+parseTokensLazy :: TL.Text -> [Token]
266+parseTokensLazy = unfoldr f
267+ where
268+ f :: TL.Text -> Maybe (Token, TL.Text)
269+ f t
270+ | TL.null t = Nothing
271+ | otherwise =
272+ case AL.parse token t of
273+ AL.Done rest tok -> Just (tok, rest)
274+ _ -> Nothing