@@ -8,6 +8,7 @@
88module Text.HTML.Parser
99 ( -- * Parsing
1010 parseTokens
11+ , parseTokensLazy
1112 , token
1213 -- * Types
1314 , Token(..)
@@ -24,8 +25,10 @@ import Control.Monad (guard)
2425import Control.DeepSeq
2526
2627import Data.Attoparsec.Text
28+import qualified Data.Attoparsec.Text.Lazy as AL
2729import Data.Text (Text)
2830import qualified Data.Text as T
31+import qualified Data.Text.Lazy as TL
2932import Data.Text.Lazy.Builder (Builder)
3033import qualified Data.Text.Lazy.Builder as B
3134import Prelude hiding (take, takeWhile)
@@ -246,7 +249,7 @@ doctype = do
246249bogusComment :: Parser Token
247250bogusComment = fail "Bogus comment"
248251
249−-- | Parse a lazy list of tokens.
252+-- | Parse a lazy list of tokens from strict 'Text'.
250253parseTokens :: Text -> [Token]
251254parseTokens = unfoldr f
252255 where
@@ -257,3 +260,15 @@ parseTokens = unfoldr f
257260 case parse token t of
258261 Done rest tok -> Just (tok, rest)
259262 _ -> 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