{-|
Module:     Codec.Parser.Text
Copyright:  Jeremy List
License:    BSD-3
Maintainer: quick.dudley@gmail.com

'Phase's for processing 'TS.Text' objects and their lazy versions.
-}
module Codec.Phaser.Text (
  unpackText,
  unpackLazyText
 ) where

import qualified Data.Text as TS
import qualified Data.Text.Lazy as TL

import Control.Applicative

import Codec.Phaser.Core

-- | A 'Phase' which takes 'TS.Text's and yields their individual characters
unpackText :: (Monoid p) => Phase p TS.Text Char ()
unpackText :: forall p. Monoid p => Phase p Text Char ()
unpackText = (Phase p Text Char ()
forall {p}. Phase p Text Char ()
go Phase p Text Char ()
-> Phase p Text Char () -> Phase p Text Char ()
forall a b.
Phase p Text Char a -> Phase p Text Char b -> Phase p Text Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Phase p Text Char ()
forall p. Monoid p => Phase p Text Char ()
unpackText) Phase p Text Char ()
-> Phase p Text Char () -> Phase p Text Char ()
forall a.
Phase p Text Char a -> Phase p Text Char a -> Phase p Text Char a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> Phase p Text Char ()
forall a. a -> Phase p Text Char a
forall (m :: * -> *) a. Monad m => a -> m a
return () where
  go :: Phase p Text Char ()
go = Phase p Text Char Text
forall p i o. Phase p i o i
get Phase p Text Char Text
-> (Text -> Phase p Text Char ()) -> Phase p Text Char ()
forall a b.
Phase p Text Char a
-> (a -> Phase p Text Char b) -> Phase p Text Char b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Char -> Phase p Text Char () -> Phase p Text Char ())
-> Phase p Text Char () -> Text -> Phase p Text Char ()
forall a. (Char -> a -> a) -> a -> Text -> a
TS.foldr (\Char
w Phase p Text Char ()
r -> Char -> Phase p Text Char ()
forall o p i. o -> Phase p i o ()
yield Char
w Phase p Text Char ()
-> Phase p Text Char () -> Phase p Text Char ()
forall a b.
Phase p Text Char a -> Phase p Text Char b -> Phase p Text Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Phase p Text Char ()
r) (() -> Phase p Text Char ()
forall a. a -> Phase p Text Char a
forall (m :: * -> *) a. Monad m => a -> m a
return ())

-- | A 'Phase' which takes 'TL.Text's and yields their individual characters
unpackLazyText :: (Monoid p) => Phase p TL.Text Char ()
unpackLazyText :: forall p. Monoid p => Phase p Text Char ()
unpackLazyText = (Phase p Text Char ()
forall {p}. Phase p Text Char ()
go Phase p Text Char ()
-> Phase p Text Char () -> Phase p Text Char ()
forall a b.
Phase p Text Char a -> Phase p Text Char b -> Phase p Text Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Phase p Text Char ()
forall p. Monoid p => Phase p Text Char ()
unpackLazyText) Phase p Text Char ()
-> Phase p Text Char () -> Phase p Text Char ()
forall a.
Phase p Text Char a -> Phase p Text Char a -> Phase p Text Char a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> Phase p Text Char ()
forall a. a -> Phase p Text Char a
forall (m :: * -> *) a. Monad m => a -> m a
return () where
  go :: Phase p Text Char ()
go = Phase p Text Char Text
forall p i o. Phase p i o i
get Phase p Text Char Text
-> (Text -> Phase p Text Char ()) -> Phase p Text Char ()
forall a b.
Phase p Text Char a
-> (a -> Phase p Text Char b) -> Phase p Text Char b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Char -> Phase p Text Char () -> Phase p Text Char ())
-> Phase p Text Char () -> Text -> Phase p Text Char ()
forall a. (Char -> a -> a) -> a -> Text -> a
TL.foldr (\Char
w Phase p Text Char ()
r -> Char -> Phase p Text Char ()
forall o p i. o -> Phase p i o ()
yield Char
w Phase p Text Char ()
-> Phase p Text Char () -> Phase p Text Char ()
forall a b.
Phase p Text Char a -> Phase p Text Char b -> Phase p Text Char b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Phase p Text Char ()
r) (() -> Phase p Text Char ()
forall a. a -> Phase p Text Char a
forall (m :: * -> *) a. Monad m => a -> m a
return ())