{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE UndecidableInstances #-}
module Web.Willow.Common.Parser
(
Parser
, runParser
, ParserT ( .. )
, StateParser
, MonadParser ( .. )
, end
, satisfying
, token
, chunk
#if MIN_VERSION_base(4,11,0)
#else
, (<>)
#endif
, Stream ( .. )
) where
import qualified Control.Applicative as A
import qualified Control.Monad as N
import qualified Control.Monad.Cont.Class as N.C
import qualified Control.Monad.Except as N.X
import qualified Control.Monad.Fail as N.F
import qualified Control.Monad.Fix as N.FX
import qualified Control.Monad.IO.Class as N.IO
import qualified Control.Monad.Reader as N.R
import qualified Control.Monad.RWS.Lazy as N.T.L
import qualified Control.Monad.RWS.Strict as N.T.S
import qualified Control.Monad.State.Class as N.S
import qualified Control.Monad.State.Lazy as N.S.L
import qualified Control.Monad.State.Strict as N.S.S
#if MIN_VERSION_transformers(0,5,3)
import qualified Control.Monad.Trans.Accum as N.A
#endif
import qualified Control.Monad.Trans.Class as N.T
import qualified Control.Monad.Trans.Identity as N.I
import qualified Control.Monad.Trans.Maybe as N.M
import qualified Control.Monad.Writer.Lazy as N.W.L
import qualified Control.Monad.Writer.Strict as N.W.S
import qualified Data.Bifunctor as F.B
import qualified Data.ByteString as BS.S
import qualified Data.ByteString.Lazy as BS.L
import qualified Data.Maybe as Y
import qualified Data.Text as T.S
import qualified Data.Text.Lazy as T.L
import qualified Data.Word as W
import Control.Applicative ( (<|>) )
#if MIN_VERSION_base(4,11,0)
#else
import Data.Semigroup
#endif
type Parser stream = ParserT stream Maybe
runParser :: Parser stream out -> stream -> Maybe (out, stream)
runParser :: Parser stream out -> stream -> Maybe (out, stream)
runParser = Parser stream out -> stream -> Maybe (out, stream)
forall stream (gather :: * -> *) out.
ParserT stream gather out -> stream -> gather (out, stream)
runParserT
type StateParser state stream = N.S.L.StateT state (Parser stream)
newtype ParserT stream gather out = ParserT
{ ParserT stream gather out -> stream -> gather (out, stream)
runParserT :: stream -> gather (out, stream)
}
instance
( Functor gather
) => Functor (ParserT stream gather) where
fmap :: (a -> b) -> ParserT stream gather a -> ParserT stream gather b
fmap a -> b
f (ParserT stream -> gather (a, stream)
a') = (stream -> gather (b, stream)) -> ParserT stream gather b
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (b, stream)) -> ParserT stream gather b)
-> (stream -> gather (b, stream)) -> ParserT stream gather b
forall a b. (a -> b) -> a -> b
$ ((a, stream) -> (b, stream))
-> gather (a, stream) -> gather (b, stream)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> (a, stream) -> (b, stream)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
F.B.first a -> b
f) (gather (a, stream) -> gather (b, stream))
-> (stream -> gather (a, stream)) -> stream -> gather (b, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> gather (a, stream)
a'
instance
( Monad gather
) => Applicative (ParserT stream gather) where
pure :: a -> ParserT stream gather a
pure a
out = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input -> (a, stream) -> gather (a, stream)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
out, stream
input)
ParserT stream -> gather (a -> b, stream)
f' <*> :: ParserT stream gather (a -> b)
-> ParserT stream gather a -> ParserT stream gather b
<*> ParserT stream -> gather (a, stream)
a' = (stream -> gather (b, stream)) -> ParserT stream gather b
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (b, stream)) -> ParserT stream gather b)
-> (stream -> gather (b, stream)) -> ParserT stream gather b
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
(a -> b
f, stream
interm) <- stream -> gather (a -> b, stream)
f' stream
input
(a
a, stream
output) <- stream -> gather (a, stream)
a' stream
interm
(b, stream) -> gather (b, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
a, stream
output)
instance
( A.Alternative gather, Monad gather
) => A.Alternative (ParserT stream gather) where
empty :: ParserT stream gather a
empty = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ gather (a, stream) -> stream -> gather (a, stream)
forall a b. a -> b -> a
const gather (a, stream)
forall (f :: * -> *) a. Alternative f => f a
A.empty
ParserT stream -> gather (a, stream)
a' <|> :: ParserT stream gather a
-> ParserT stream gather a -> ParserT stream gather a
<|> ParserT stream -> gather (a, stream)
b' = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input ->
stream -> gather (a, stream)
a' stream
input gather (a, stream) -> gather (a, stream) -> gather (a, stream)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> stream -> gather (a, stream)
b' stream
input
instance
( Monad gather
, Semigroup out
) => Semigroup (ParserT stream gather out) where
ParserT stream -> gather (out, stream)
a' <> :: ParserT stream gather out
-> ParserT stream gather out -> ParserT stream gather out
<> ParserT stream -> gather (out, stream)
b' = (stream -> gather (out, stream)) -> ParserT stream gather out
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (out, stream)) -> ParserT stream gather out)
-> (stream -> gather (out, stream)) -> ParserT stream gather out
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
(out
a, stream
interm) <- stream -> gather (out, stream)
a' stream
input
(out
b, stream
output) <- stream -> gather (out, stream)
b' stream
interm
(out, stream) -> gather (out, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (out
a out -> out -> out
forall a. Semigroup a => a -> a -> a
<> out
b, stream
output)
instance
( Monad gather
, Monoid out
#if MIN_VERSION_base(4,11,0)
) => Monoid (ParserT stream gather out) where
#else
, Semigroup out
) => Monoid (ParserT stream gather out) where
mappend = (<>)
#endif
mempty :: ParserT stream gather out
mempty = (stream -> gather (out, stream)) -> ParserT stream gather out
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (out, stream)) -> ParserT stream gather out)
-> (stream -> gather (out, stream)) -> ParserT stream gather out
forall a b. (a -> b) -> a -> b
$ \stream
input -> (out, stream) -> gather (out, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (out
forall a. Monoid a => a
mempty, stream
input)
instance
( Monad gather
) => Monad (ParserT stream gather) where
ParserT stream -> gather (a, stream)
a' >>= :: ParserT stream gather a
-> (a -> ParserT stream gather b) -> ParserT stream gather b
>>= a -> ParserT stream gather b
f = (stream -> gather (b, stream)) -> ParserT stream gather b
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (b, stream)) -> ParserT stream gather b)
-> (stream -> gather (b, stream)) -> ParserT stream gather b
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
(a
a, stream
output) <- stream -> gather (a, stream)
a' stream
input
let ParserT stream -> gather (b, stream)
b' = a -> ParserT stream gather b
f a
a
stream -> gather (b, stream)
b' stream
output
instance
( A.Alternative gather, Monad gather
) => N.MonadPlus (ParserT stream gather)
instance
( N.F.MonadFail gather
) => N.F.MonadFail (ParserT stream gather) where
fail :: String -> ParserT stream gather a
fail = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (String -> stream -> gather (a, stream))
-> String
-> ParserT stream gather a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. gather (a, stream) -> stream -> gather (a, stream)
forall a b. a -> b -> a
const (gather (a, stream) -> stream -> gather (a, stream))
-> (String -> gather (a, stream))
-> String
-> stream
-> gather (a, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> gather (a, stream)
forall (m :: * -> *) a. MonadFail m => String -> m a
N.F.fail
instance
( N.X.MonadError err gather
) => N.X.MonadError err (ParserT stream gather) where
throwError :: err -> ParserT stream gather a
throwError = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (err -> stream -> gather (a, stream))
-> err
-> ParserT stream gather a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. gather (a, stream) -> stream -> gather (a, stream)
forall a b. a -> b -> a
const (gather (a, stream) -> stream -> gather (a, stream))
-> (err -> gather (a, stream))
-> err
-> stream
-> gather (a, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. err -> gather (a, stream)
forall e (m :: * -> *) a. MonadError e m => e -> m a
N.X.throwError
catchError :: ParserT stream gather a
-> (err -> ParserT stream gather a) -> ParserT stream gather a
catchError (ParserT stream -> gather (a, stream)
a') err -> ParserT stream gather a
f = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input -> gather (a, stream)
-> (err -> gather (a, stream)) -> gather (a, stream)
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
N.X.catchError (stream -> gather (a, stream)
a' stream
input) ((err -> gather (a, stream)) -> gather (a, stream))
-> (err -> gather (a, stream)) -> gather (a, stream)
forall a b. (a -> b) -> a -> b
$ \err
err ->
let ParserT stream -> gather (a, stream)
b' = err -> ParserT stream gather a
f err
err
in stream -> gather (a, stream)
b' stream
input
instance
( Monad gather
) => N.FX.MonadFix (ParserT stream gather) where
mfix :: (a -> ParserT stream gather a) -> ParserT stream gather a
mfix a -> ParserT stream gather a
f = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
let ParserT stream -> gather (a, stream)
f' = (a -> ParserT stream gather a) -> ParserT stream gather a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
N.FX.mfix a -> ParserT stream gather a
f
stream -> gather (a, stream)
f' stream
input
instance N.T.MonadTrans (ParserT stream) where
lift :: m a -> ParserT stream m a
lift m a
a' = (stream -> m (a, stream)) -> ParserT stream m a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> m (a, stream)) -> ParserT stream m a)
-> (stream -> m (a, stream)) -> ParserT stream m a
forall a b. (a -> b) -> a -> b
$ \stream
input ->
m a
a' m a -> (a -> m (a, stream)) -> m (a, stream)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> (a, stream) -> m (a, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, stream
input)
instance
( Monad gather
) => N.R.MonadReader stream (ParserT stream gather) where
ask :: ParserT stream gather stream
ask = ParserT stream gather stream
forall s (m :: * -> *). MonadState s m => m s
N.S.get
local :: (stream -> stream)
-> ParserT stream gather a -> ParserT stream gather a
local stream -> stream
f (ParserT stream -> gather (a, stream)
a') = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ stream -> gather (a, stream)
a' (stream -> gather (a, stream))
-> (stream -> stream) -> stream -> gather (a, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> stream
f
reader :: (stream -> a) -> ParserT stream gather a
reader stream -> a
f = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input -> (a, stream) -> gather (a, stream)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (stream -> a
f stream
input, stream
input)
instance
( Monad gather
) => N.S.MonadState stream (ParserT stream gather) where
state :: (stream -> (a, stream)) -> ParserT stream gather a
state stream -> (a, stream)
f = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ (a, stream) -> gather (a, stream)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, stream) -> gather (a, stream))
-> (stream -> (a, stream)) -> stream -> gather (a, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> (a, stream)
f
instance
( N.IO.MonadIO gather
) => N.IO.MonadIO (ParserT stream gather) where
liftIO :: IO a -> ParserT stream gather a
liftIO IO a
a' = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
a
a <- IO a -> gather a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
N.IO.liftIO IO a
a'
(a, stream) -> gather (a, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, stream
input)
instance N.C.MonadCont gather => N.C.MonadCont (ParserT stream gather) where
callCC :: ((a -> ParserT stream gather b) -> ParserT stream gather a)
-> ParserT stream gather a
callCC (a -> ParserT stream gather b) -> ParserT stream gather a
f = (stream -> gather (a, stream)) -> ParserT stream gather a
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (a, stream)) -> ParserT stream gather a)
-> (stream -> gather (a, stream)) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \stream
input -> (((a, stream) -> gather (b, stream)) -> gather (a, stream))
-> gather (a, stream)
forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
N.C.callCC ((((a, stream) -> gather (b, stream)) -> gather (a, stream))
-> gather (a, stream))
-> (((a, stream) -> gather (b, stream)) -> gather (a, stream))
-> gather (a, stream)
forall a b. (a -> b) -> a -> b
$ \(a, stream) -> gather (b, stream)
g ->
ParserT stream gather a -> stream -> gather (a, stream)
forall stream (gather :: * -> *) out.
ParserT stream gather out -> stream -> gather (out, stream)
runParserT ((a -> ParserT stream gather b) -> ParserT stream gather a
f ((a -> ParserT stream gather b) -> ParserT stream gather a)
-> (a -> ParserT stream gather b) -> ParserT stream gather a
forall a b. (a -> b) -> a -> b
$ \a
a -> (stream -> gather (b, stream)) -> ParserT stream gather b
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (b, stream)) -> ParserT stream gather b)
-> (stream -> gather (b, stream)) -> ParserT stream gather b
forall a b. (a -> b) -> a -> b
$ \stream
input' -> (a, stream) -> gather (b, stream)
g (a
a, stream
input')) stream
input
class Monoid stream => Stream stream token | stream -> token where
{-# MINIMAL cons, uncons #-}
cons :: token -> stream -> stream
consChunk :: stream -> stream -> stream
consChunk stream
a stream
b = case stream -> Maybe (token, stream)
forall stream token.
Stream stream token =>
stream -> Maybe (token, stream)
uncons stream
a of
Maybe (token, stream)
Nothing -> stream
b
Just (token
t, stream
ts) -> token -> stream -> stream
forall stream token.
Stream stream token =>
token -> stream -> stream
cons token
t (stream -> stream) -> stream -> stream
forall a b. (a -> b) -> a -> b
$ stream -> stream -> stream
forall stream token.
Stream stream token =>
stream -> stream -> stream
consChunk stream
ts stream
b
uncons :: stream -> Maybe (token, stream)
unconsChunk :: Word -> stream -> (stream, stream)
unconsChunk Word
0 stream
str = (stream
forall a. Monoid a => a
mempty, stream
str)
unconsChunk Word
n stream
str = case stream -> Maybe (token, stream)
forall stream token.
Stream stream token =>
stream -> Maybe (token, stream)
uncons stream
str of
Maybe (token, stream)
Nothing -> (stream
forall a. Monoid a => a
mempty, stream
str)
Just (token
t, stream
str') ->
let (stream
ts, stream
str'') = Word -> stream -> (stream, stream)
forall stream token.
Stream stream token =>
Word -> stream -> (stream, stream)
unconsChunk (Word -> Word
forall a. Enum a => a -> a
pred Word
n) stream
str'
in (token -> stream -> stream
forall stream token.
Stream stream token =>
token -> stream -> stream
cons token
t stream
ts, stream
str'')
chunkLen :: stream -> Word
chunkLen stream
str = case stream -> Maybe (token, stream)
forall stream token.
Stream stream token =>
stream -> Maybe (token, stream)
uncons stream
str of
Just (token
_, stream
str') -> Word
1 Word -> Word -> Word
forall a. Num a => a -> a -> a
+ stream -> Word
forall stream token. Stream stream token => stream -> Word
chunkLen stream
str'
Maybe (token, stream)
Nothing -> Word
0
instance Stream BS.L.ByteString W.Word8 where
cons :: Word8 -> ByteString -> ByteString
cons = Word8 -> ByteString -> ByteString
BS.L.cons
consChunk :: ByteString -> ByteString -> ByteString
consChunk = ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
(<>)
uncons :: ByteString -> Maybe (Word8, ByteString)
uncons = ByteString -> Maybe (Word8, ByteString)
BS.L.uncons
unconsChunk :: Word -> ByteString -> (ByteString, ByteString)
unconsChunk = Int64 -> ByteString -> (ByteString, ByteString)
BS.L.splitAt (Int64 -> ByteString -> (ByteString, ByteString))
-> (Word -> Int64)
-> Word
-> ByteString
-> (ByteString, ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral
chunkLen :: ByteString -> Word
chunkLen = Int64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Word) -> (ByteString -> Int64) -> ByteString -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
BS.L.length
instance Stream BS.S.ByteString W.Word8 where
cons :: Word8 -> ByteString -> ByteString
cons = Word8 -> ByteString -> ByteString
BS.S.cons
consChunk :: ByteString -> ByteString -> ByteString
consChunk = ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
(<>)
uncons :: ByteString -> Maybe (Word8, ByteString)
uncons = ByteString -> Maybe (Word8, ByteString)
BS.S.uncons
unconsChunk :: Word -> ByteString -> (ByteString, ByteString)
unconsChunk = Int -> ByteString -> (ByteString, ByteString)
BS.S.splitAt (Int -> ByteString -> (ByteString, ByteString))
-> (Word -> Int) -> Word -> ByteString -> (ByteString, ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
chunkLen :: ByteString -> Word
chunkLen = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word) -> (ByteString -> Int) -> ByteString -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.S.length
instance Stream T.L.Text Char where
cons :: Char -> Text -> Text
cons = Char -> Text -> Text
T.L.cons
consChunk :: Text -> Text -> Text
consChunk = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>)
uncons :: Text -> Maybe (Char, Text)
uncons = Text -> Maybe (Char, Text)
T.L.uncons
unconsChunk :: Word -> Text -> (Text, Text)
unconsChunk = Int64 -> Text -> (Text, Text)
T.L.splitAt (Int64 -> Text -> (Text, Text))
-> (Word -> Int64) -> Word -> Text -> (Text, Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral
chunkLen :: Text -> Word
chunkLen = Int64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Word) -> (Text -> Int64) -> Text -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int64
T.L.length
instance Stream T.S.Text Char where
cons :: Char -> Text -> Text
cons = Char -> Text -> Text
T.S.cons
consChunk :: Text -> Text -> Text
consChunk = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>)
uncons :: Text -> Maybe (Char, Text)
uncons = Text -> Maybe (Char, Text)
T.S.uncons
unconsChunk :: Word -> Text -> (Text, Text)
unconsChunk = Int -> Text -> (Text, Text)
T.S.splitAt (Int -> Text -> (Text, Text))
-> (Word -> Int) -> Word -> Text -> (Text, Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
chunkLen :: Text -> Word
chunkLen = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word) -> (Text -> Int) -> Text -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
T.S.length
instance Stream [token] token where
cons :: token -> [token] -> [token]
cons = (:)
consChunk :: [token] -> [token] -> [token]
consChunk = [token] -> [token] -> [token]
forall a. Semigroup a => a -> a -> a
(<>)
uncons :: [token] -> Maybe (token, [token])
uncons [] = Maybe (token, [token])
forall a. Maybe a
Nothing
uncons (token
t:[token]
ts) = (token, [token]) -> Maybe (token, [token])
forall a. a -> Maybe a
Just (token
t, [token]
ts)
unconsChunk :: Word -> [token] -> ([token], [token])
unconsChunk = Int -> [token] -> ([token], [token])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int -> [token] -> ([token], [token]))
-> (Word -> Int) -> Word -> [token] -> ([token], [token])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
chunkLen :: [token] -> Word
chunkLen = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word) -> ([token] -> Int) -> [token] -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [token] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
class
( A.Alternative m, Monad m
, Stream stream token, Monoid stream
) => MonadParser m stream token | m -> stream where
lookAhead :: m out -> m out
avoiding :: m out -> m ()
next :: m token
nextChunk :: Word -> m stream
push :: token -> m ()
pushChunk :: stream -> m ()
abridge :: m stream
instance
( A.Alternative gather, Monad gather
, Stream stream token, Monoid stream
) => MonadParser (ParserT stream gather) stream token where
lookAhead :: ParserT stream gather out -> ParserT stream gather out
lookAhead (ParserT stream -> gather (out, stream)
a') = (stream -> gather (out, stream)) -> ParserT stream gather out
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (out, stream)) -> ParserT stream gather out)
-> (stream -> gather (out, stream)) -> ParserT stream gather out
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
(out
a, stream
_) <- stream -> gather (out, stream)
a' stream
input
(out, stream) -> gather (out, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (out
a, stream
input)
avoiding :: ParserT stream gather out -> ParserT stream gather ()
avoiding (ParserT stream -> gather (out, stream)
bad') = (stream -> gather ((), stream)) -> ParserT stream gather ()
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather ((), stream)) -> ParserT stream gather ())
-> (stream -> gather ((), stream)) -> ParserT stream gather ()
forall a b. (a -> b) -> a -> b
$ \stream
input -> do
Maybe (out, stream)
bad <- gather (out, stream) -> gather (Maybe (out, stream))
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
A.optional (gather (out, stream) -> gather (Maybe (out, stream)))
-> gather (out, stream) -> gather (Maybe (out, stream))
forall a b. (a -> b) -> a -> b
$ stream -> gather (out, stream)
bad' stream
input
if Maybe (out, stream) -> Bool
forall a. Maybe a -> Bool
Y.isJust Maybe (out, stream)
bad
then gather ((), stream)
forall (f :: * -> *) a. Alternative f => f a
A.empty
else ((), stream) -> gather ((), stream)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((), stream
input)
next :: ParserT stream gather token
next = (stream -> gather (token, stream)) -> ParserT stream gather token
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (token, stream)) -> ParserT stream gather token)
-> (stream -> gather (token, stream))
-> ParserT stream gather token
forall a b. (a -> b) -> a -> b
$ gather (token, stream)
-> ((token, stream) -> gather (token, stream))
-> Maybe (token, stream)
-> gather (token, stream)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe gather (token, stream)
forall (f :: * -> *) a. Alternative f => f a
A.empty (token, stream) -> gather (token, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (token, stream) -> gather (token, stream))
-> (stream -> Maybe (token, stream))
-> stream
-> gather (token, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> Maybe (token, stream)
forall stream token.
Stream stream token =>
stream -> Maybe (token, stream)
uncons
nextChunk :: Word -> ParserT stream gather stream
nextChunk Word
l = (stream -> gather (stream, stream)) -> ParserT stream gather stream
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (stream, stream))
-> ParserT stream gather stream)
-> (stream -> gather (stream, stream))
-> ParserT stream gather stream
forall a b. (a -> b) -> a -> b
$ (stream, stream) -> gather (stream, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return ((stream, stream) -> gather (stream, stream))
-> (stream -> (stream, stream))
-> stream
-> gather (stream, stream)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> stream -> (stream, stream)
forall stream token.
Stream stream token =>
Word -> stream -> (stream, stream)
unconsChunk Word
l
push :: token -> ParserT stream gather ()
push token
tok = (stream -> gather ((), stream)) -> ParserT stream gather ()
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather ((), stream)) -> ParserT stream gather ())
-> (stream -> gather ((), stream)) -> ParserT stream gather ()
forall a b. (a -> b) -> a -> b
$ \stream
input -> ((), stream) -> gather ((), stream)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), token -> stream -> stream
forall stream token.
Stream stream token =>
token -> stream -> stream
cons token
tok stream
input)
pushChunk :: stream -> ParserT stream gather ()
pushChunk stream
str = (stream -> gather ((), stream)) -> ParserT stream gather ()
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather ((), stream)) -> ParserT stream gather ())
-> (stream -> gather ((), stream)) -> ParserT stream gather ()
forall a b. (a -> b) -> a -> b
$ \stream
input -> ((), stream) -> gather ((), stream)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), stream -> stream -> stream
forall stream token.
Stream stream token =>
stream -> stream -> stream
consChunk stream
str stream
input)
abridge :: ParserT stream gather stream
abridge = (stream -> gather (stream, stream)) -> ParserT stream gather stream
forall stream (gather :: * -> *) out.
(stream -> gather (out, stream)) -> ParserT stream gather out
ParserT ((stream -> gather (stream, stream))
-> ParserT stream gather stream)
-> (stream -> gather (stream, stream))
-> ParserT stream gather stream
forall a b. (a -> b) -> a -> b
$ \stream
input -> (stream, stream) -> gather (stream, stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (stream
input, stream
forall a. Monoid a => a
mempty)
#if MIN_VERSION_transformers(0,5,3)
instance
( MonadParser trans stream token
, Monoid accum
, N.MonadPlus trans
) => MonadParser (N.A.AccumT accum trans) stream token where
lookAhead :: AccumT accum trans out -> AccumT accum trans out
lookAhead (N.A.AccumT accum -> trans (out, accum)
trans) = (accum -> trans (out, accum)) -> AccumT accum trans out
forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
N.A.AccumT ((accum -> trans (out, accum)) -> AccumT accum trans out)
-> (accum -> trans (out, accum)) -> AccumT accum trans out
forall a b. (a -> b) -> a -> b
$ trans (out, accum) -> trans (out, accum)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead (trans (out, accum) -> trans (out, accum))
-> (accum -> trans (out, accum)) -> accum -> trans (out, accum)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. accum -> trans (out, accum)
trans
avoiding :: AccumT accum trans out -> AccumT accum trans ()
avoiding (N.A.AccumT accum -> trans (out, accum)
trans) = (accum -> trans ((), accum)) -> AccumT accum trans ()
forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
N.A.AccumT ((accum -> trans ((), accum)) -> AccumT accum trans ())
-> (accum -> trans ((), accum)) -> AccumT accum trans ()
forall a b. (a -> b) -> a -> b
$ \accum
accum ->
trans (out, accum) -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding (accum -> trans (out, accum)
trans accum
accum) trans () -> trans ((), accum) -> trans ((), accum)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), accum) -> trans ((), accum)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), accum
accum)
next :: AccumT accum trans token
next = trans token -> AccumT accum trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> AccumT accum trans stream
nextChunk = trans stream -> AccumT accum trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> AccumT accum trans stream)
-> (Word -> trans stream) -> Word -> AccumT accum trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> AccumT accum trans ()
push = trans () -> AccumT accum trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> AccumT accum trans ())
-> (token -> trans ()) -> token -> AccumT accum trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> AccumT accum trans ()
pushChunk = trans () -> AccumT accum trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> AccumT accum trans ())
-> (stream -> trans ()) -> stream -> AccumT accum trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: AccumT accum trans stream
abridge = trans stream -> AccumT accum trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
#endif
instance
( MonadParser trans stream token
, Monoid except
) => MonadParser (N.X.ExceptT except trans) stream token where
lookAhead :: ExceptT except trans out -> ExceptT except trans out
lookAhead (N.X.ExceptT trans (Either except out)
trans) = trans (Either except out) -> ExceptT except trans out
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
N.X.ExceptT (trans (Either except out) -> ExceptT except trans out)
-> trans (Either except out) -> ExceptT except trans out
forall a b. (a -> b) -> a -> b
$ trans (Either except out) -> trans (Either except out)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead trans (Either except out)
trans
avoiding :: ExceptT except trans out -> ExceptT except trans ()
avoiding (N.X.ExceptT trans (Either except out)
trans) = trans (Either except ()) -> ExceptT except trans ()
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
N.X.ExceptT (trans (Either except ()) -> ExceptT except trans ())
-> trans (Either except ()) -> ExceptT except trans ()
forall a b. (a -> b) -> a -> b
$
trans (Either except out) -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding trans (Either except out)
trans trans () -> trans (Either except ()) -> trans (Either except ())
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Either except () -> trans (Either except ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Either except ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
next :: ExceptT except trans token
next = trans token -> ExceptT except trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> ExceptT except trans stream
nextChunk = trans stream -> ExceptT except trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> ExceptT except trans stream)
-> (Word -> trans stream) -> Word -> ExceptT except trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> ExceptT except trans ()
push = trans () -> ExceptT except trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> ExceptT except trans ())
-> (token -> trans ()) -> token -> ExceptT except trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> ExceptT except trans ()
pushChunk = trans () -> ExceptT except trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> ExceptT except trans ())
-> (stream -> trans ()) -> stream -> ExceptT except trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: ExceptT except trans stream
abridge = trans stream -> ExceptT except trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
) => MonadParser (N.I.IdentityT trans) stream token where
lookAhead :: IdentityT trans out -> IdentityT trans out
lookAhead (N.I.IdentityT trans out
trans) = trans out -> IdentityT trans out
forall k (f :: k -> *) (a :: k). f a -> IdentityT f a
N.I.IdentityT (trans out -> IdentityT trans out)
-> trans out -> IdentityT trans out
forall a b. (a -> b) -> a -> b
$ trans out -> trans out
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead trans out
trans
avoiding :: IdentityT trans out -> IdentityT trans ()
avoiding (N.I.IdentityT trans out
trans) = trans () -> IdentityT trans ()
forall k (f :: k -> *) (a :: k). f a -> IdentityT f a
N.I.IdentityT (trans () -> IdentityT trans ()) -> trans () -> IdentityT trans ()
forall a b. (a -> b) -> a -> b
$ trans out -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding trans out
trans
next :: IdentityT trans token
next = trans token -> IdentityT trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> IdentityT trans stream
nextChunk = trans stream -> IdentityT trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> IdentityT trans stream)
-> (Word -> trans stream) -> Word -> IdentityT trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> IdentityT trans ()
push = trans () -> IdentityT trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> IdentityT trans ())
-> (token -> trans ()) -> token -> IdentityT trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> IdentityT trans ()
pushChunk = trans () -> IdentityT trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> IdentityT trans ())
-> (stream -> trans ()) -> stream -> IdentityT trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: IdentityT trans stream
abridge = trans stream -> IdentityT trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
) => MonadParser (N.M.MaybeT trans) stream token where
lookAhead :: MaybeT trans out -> MaybeT trans out
lookAhead (N.M.MaybeT trans (Maybe out)
trans) = trans (Maybe out) -> MaybeT trans out
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
N.M.MaybeT (trans (Maybe out) -> MaybeT trans out)
-> trans (Maybe out) -> MaybeT trans out
forall a b. (a -> b) -> a -> b
$ trans (Maybe out) -> trans (Maybe out)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead trans (Maybe out)
trans
avoiding :: MaybeT trans out -> MaybeT trans ()
avoiding (N.M.MaybeT trans (Maybe out)
trans) = trans (Maybe ()) -> MaybeT trans ()
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
N.M.MaybeT (trans (Maybe ()) -> MaybeT trans ())
-> trans (Maybe ()) -> MaybeT trans ()
forall a b. (a -> b) -> a -> b
$
trans (Maybe out) -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding trans (Maybe out)
trans trans () -> trans (Maybe ()) -> trans (Maybe ())
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe () -> trans (Maybe ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Maybe ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
next :: MaybeT trans token
next = trans token -> MaybeT trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> MaybeT trans stream
nextChunk = trans stream -> MaybeT trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> MaybeT trans stream)
-> (Word -> trans stream) -> Word -> MaybeT trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> MaybeT trans ()
push = trans () -> MaybeT trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> MaybeT trans ())
-> (token -> trans ()) -> token -> MaybeT trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> MaybeT trans ()
pushChunk = trans () -> MaybeT trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> MaybeT trans ())
-> (stream -> trans ()) -> stream -> MaybeT trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: MaybeT trans stream
abridge = trans stream -> MaybeT trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
) => MonadParser (N.R.ReaderT reader trans) stream token where
lookAhead :: ReaderT reader trans out -> ReaderT reader trans out
lookAhead (N.R.ReaderT reader -> trans out
trans) = (reader -> trans out) -> ReaderT reader trans out
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
N.R.ReaderT ((reader -> trans out) -> ReaderT reader trans out)
-> (reader -> trans out) -> ReaderT reader trans out
forall a b. (a -> b) -> a -> b
$ trans out -> trans out
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead (trans out -> trans out)
-> (reader -> trans out) -> reader -> trans out
forall b c a. (b -> c) -> (a -> b) -> a -> c
. reader -> trans out
trans
avoiding :: ReaderT reader trans out -> ReaderT reader trans ()
avoiding (N.R.ReaderT reader -> trans out
trans) = (reader -> trans ()) -> ReaderT reader trans ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
N.R.ReaderT ((reader -> trans ()) -> ReaderT reader trans ())
-> (reader -> trans ()) -> ReaderT reader trans ()
forall a b. (a -> b) -> a -> b
$ trans out -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding (trans out -> trans ())
-> (reader -> trans out) -> reader -> trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. reader -> trans out
trans
next :: ReaderT reader trans token
next = trans token -> ReaderT reader trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> ReaderT reader trans stream
nextChunk = trans stream -> ReaderT reader trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> ReaderT reader trans stream)
-> (Word -> trans stream) -> Word -> ReaderT reader trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> ReaderT reader trans ()
push = trans () -> ReaderT reader trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> ReaderT reader trans ())
-> (token -> trans ()) -> token -> ReaderT reader trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> ReaderT reader trans ()
pushChunk = trans () -> ReaderT reader trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> ReaderT reader trans ())
-> (stream -> trans ()) -> stream -> ReaderT reader trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: ReaderT reader trans stream
abridge = trans stream -> ReaderT reader trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
, N.MonadPlus trans
) => MonadParser (N.S.L.StateT state trans) stream token where
lookAhead :: StateT state trans out -> StateT state trans out
lookAhead (N.S.L.StateT state -> trans (out, state)
trans) = (state -> trans (out, state)) -> StateT state trans out
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
N.S.L.StateT ((state -> trans (out, state)) -> StateT state trans out)
-> (state -> trans (out, state)) -> StateT state trans out
forall a b. (a -> b) -> a -> b
$ trans (out, state) -> trans (out, state)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead (trans (out, state) -> trans (out, state))
-> (state -> trans (out, state)) -> state -> trans (out, state)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. state -> trans (out, state)
trans
avoiding :: StateT state trans out -> StateT state trans ()
avoiding (N.S.L.StateT state -> trans (out, state)
trans) = (state -> trans ((), state)) -> StateT state trans ()
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
N.S.L.StateT ((state -> trans ((), state)) -> StateT state trans ())
-> (state -> trans ((), state)) -> StateT state trans ()
forall a b. (a -> b) -> a -> b
$ \state
state ->
trans out -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding ((out, state) -> out
forall a b. (a, b) -> a
fst ((out, state) -> out) -> trans (out, state) -> trans out
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> state -> trans (out, state)
trans state
state) trans () -> trans ((), state) -> trans ((), state)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), state) -> trans ((), state)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), state
state)
next :: StateT state trans token
next = trans token -> StateT state trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> StateT state trans stream
nextChunk = trans stream -> StateT state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> StateT state trans stream)
-> (Word -> trans stream) -> Word -> StateT state trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> StateT state trans ()
push = trans () -> StateT state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> StateT state trans ())
-> (token -> trans ()) -> token -> StateT state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> StateT state trans ()
pushChunk = trans () -> StateT state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> StateT state trans ())
-> (stream -> trans ()) -> stream -> StateT state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: StateT state trans stream
abridge = trans stream -> StateT state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
, N.MonadPlus trans
) => MonadParser (N.S.S.StateT state trans) stream token where
lookAhead :: StateT state trans out -> StateT state trans out
lookAhead (N.S.S.StateT state -> trans (out, state)
trans) = (state -> trans (out, state)) -> StateT state trans out
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
N.S.S.StateT ((state -> trans (out, state)) -> StateT state trans out)
-> (state -> trans (out, state)) -> StateT state trans out
forall a b. (a -> b) -> a -> b
$ trans (out, state) -> trans (out, state)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead (trans (out, state) -> trans (out, state))
-> (state -> trans (out, state)) -> state -> trans (out, state)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. state -> trans (out, state)
trans
avoiding :: StateT state trans out -> StateT state trans ()
avoiding (N.S.S.StateT state -> trans (out, state)
trans) = (state -> trans ((), state)) -> StateT state trans ()
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
N.S.S.StateT ((state -> trans ((), state)) -> StateT state trans ())
-> (state -> trans ((), state)) -> StateT state trans ()
forall a b. (a -> b) -> a -> b
$ \state
state ->
trans out -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding ((out, state) -> out
forall a b. (a, b) -> a
fst ((out, state) -> out) -> trans (out, state) -> trans out
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> state -> trans (out, state)
trans state
state) trans () -> trans ((), state) -> trans ((), state)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), state) -> trans ((), state)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), state
state)
next :: StateT state trans token
next = trans token -> StateT state trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> StateT state trans stream
nextChunk = trans stream -> StateT state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> StateT state trans stream)
-> (Word -> trans stream) -> Word -> StateT state trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> StateT state trans ()
push = trans () -> StateT state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> StateT state trans ())
-> (token -> trans ()) -> token -> StateT state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> StateT state trans ()
pushChunk = trans () -> StateT state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> StateT state trans ())
-> (stream -> trans ()) -> stream -> StateT state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: StateT state trans stream
abridge = trans stream -> StateT state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
, Monoid writer
) => MonadParser (N.W.L.WriterT writer trans) stream token where
lookAhead :: WriterT writer trans out -> WriterT writer trans out
lookAhead (N.W.L.WriterT trans (out, writer)
trans) = trans (out, writer) -> WriterT writer trans out
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
N.W.L.WriterT (trans (out, writer) -> WriterT writer trans out)
-> trans (out, writer) -> WriterT writer trans out
forall a b. (a -> b) -> a -> b
$ trans (out, writer) -> trans (out, writer)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead trans (out, writer)
trans
avoiding :: WriterT writer trans out -> WriterT writer trans ()
avoiding (N.W.L.WriterT trans (out, writer)
trans) = trans ((), writer) -> WriterT writer trans ()
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
N.W.L.WriterT (trans ((), writer) -> WriterT writer trans ())
-> trans ((), writer) -> WriterT writer trans ()
forall a b. (a -> b) -> a -> b
$
trans out -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding (((out, writer) -> out) -> trans (out, writer) -> trans out
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (out, writer) -> out
forall a b. (a, b) -> a
fst trans (out, writer)
trans) trans () -> trans ((), writer) -> trans ((), writer)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), writer) -> trans ((), writer)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), writer
forall a. Monoid a => a
mempty)
next :: WriterT writer trans token
next = trans token -> WriterT writer trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> WriterT writer trans stream
nextChunk = trans stream -> WriterT writer trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> WriterT writer trans stream)
-> (Word -> trans stream) -> Word -> WriterT writer trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> WriterT writer trans ()
push = trans () -> WriterT writer trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> WriterT writer trans ())
-> (token -> trans ()) -> token -> WriterT writer trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> WriterT writer trans ()
pushChunk = trans () -> WriterT writer trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> WriterT writer trans ())
-> (stream -> trans ()) -> stream -> WriterT writer trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: WriterT writer trans stream
abridge = trans stream -> WriterT writer trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
, Monoid writer
) => MonadParser (N.W.S.WriterT writer trans) stream token where
lookAhead :: WriterT writer trans out -> WriterT writer trans out
lookAhead (N.W.S.WriterT trans (out, writer)
trans) = trans (out, writer) -> WriterT writer trans out
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
N.W.S.WriterT (trans (out, writer) -> WriterT writer trans out)
-> trans (out, writer) -> WriterT writer trans out
forall a b. (a -> b) -> a -> b
$ trans (out, writer) -> trans (out, writer)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead trans (out, writer)
trans
avoiding :: WriterT writer trans out -> WriterT writer trans ()
avoiding (N.W.S.WriterT trans (out, writer)
trans) = trans ((), writer) -> WriterT writer trans ()
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
N.W.S.WriterT (trans ((), writer) -> WriterT writer trans ())
-> trans ((), writer) -> WriterT writer trans ()
forall a b. (a -> b) -> a -> b
$
trans out -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding (((out, writer) -> out) -> trans (out, writer) -> trans out
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (out, writer) -> out
forall a b. (a, b) -> a
fst trans (out, writer)
trans) trans () -> trans ((), writer) -> trans ((), writer)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), writer) -> trans ((), writer)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), writer
forall a. Monoid a => a
mempty)
next :: WriterT writer trans token
next = trans token -> WriterT writer trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> WriterT writer trans stream
nextChunk = trans stream -> WriterT writer trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> WriterT writer trans stream)
-> (Word -> trans stream) -> Word -> WriterT writer trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> WriterT writer trans ()
push = trans () -> WriterT writer trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> WriterT writer trans ())
-> (token -> trans ()) -> token -> WriterT writer trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> WriterT writer trans ()
pushChunk = trans () -> WriterT writer trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> WriterT writer trans ())
-> (stream -> trans ()) -> stream -> WriterT writer trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: WriterT writer trans stream
abridge = trans stream -> WriterT writer trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
, Monoid writer
, N.MonadPlus trans
) => MonadParser (N.T.S.RWST reader writer state trans) stream token where
lookAhead :: RWST reader writer state trans out
-> RWST reader writer state trans out
lookAhead (N.T.S.RWST reader -> state -> trans (out, state, writer)
trans) = (reader -> state -> trans (out, state, writer))
-> RWST reader writer state trans out
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
N.T.S.RWST ((reader -> state -> trans (out, state, writer))
-> RWST reader writer state trans out)
-> (reader -> state -> trans (out, state, writer))
-> RWST reader writer state trans out
forall a b. (a -> b) -> a -> b
$ \reader
reader -> trans (out, state, writer) -> trans (out, state, writer)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead (trans (out, state, writer) -> trans (out, state, writer))
-> (state -> trans (out, state, writer))
-> state
-> trans (out, state, writer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. reader -> state -> trans (out, state, writer)
trans reader
reader
avoiding :: RWST reader writer state trans out
-> RWST reader writer state trans ()
avoiding (N.T.S.RWST reader -> state -> trans (out, state, writer)
trans) = (reader -> state -> trans ((), state, writer))
-> RWST reader writer state trans ()
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
N.T.S.RWST ((reader -> state -> trans ((), state, writer))
-> RWST reader writer state trans ())
-> (reader -> state -> trans ((), state, writer))
-> RWST reader writer state trans ()
forall a b. (a -> b) -> a -> b
$ \reader
reader state
state ->
trans (out, state, writer) -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding (reader -> state -> trans (out, state, writer)
trans reader
reader state
state) trans () -> trans ((), state, writer) -> trans ((), state, writer)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ((), state, writer) -> trans ((), state, writer)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), state
state, writer
forall a. Monoid a => a
mempty)
next :: RWST reader writer state trans token
next = trans token -> RWST reader writer state trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> RWST reader writer state trans stream
nextChunk = trans stream -> RWST reader writer state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> RWST reader writer state trans stream)
-> (Word -> trans stream)
-> Word
-> RWST reader writer state trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> RWST reader writer state trans ()
push = trans () -> RWST reader writer state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> RWST reader writer state trans ())
-> (token -> trans ())
-> token
-> RWST reader writer state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> RWST reader writer state trans ()
pushChunk = trans () -> RWST reader writer state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> RWST reader writer state trans ())
-> (stream -> trans ())
-> stream
-> RWST reader writer state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: RWST reader writer state trans stream
abridge = trans stream -> RWST reader writer state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
instance
( MonadParser trans stream token
, Monoid writer
, N.MonadPlus trans
) => MonadParser (N.T.L.RWST reader writer state trans) stream token where
lookAhead :: RWST reader writer state trans out
-> RWST reader writer state trans out
lookAhead (N.T.L.RWST reader -> state -> trans (out, state, writer)
trans) = (reader -> state -> trans (out, state, writer))
-> RWST reader writer state trans out
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
N.T.L.RWST ((reader -> state -> trans (out, state, writer))
-> RWST reader writer state trans out)
-> (reader -> state -> trans (out, state, writer))
-> RWST reader writer state trans out
forall a b. (a -> b) -> a -> b
$ \reader
reader -> trans (out, state, writer) -> trans (out, state, writer)
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m out
lookAhead (trans (out, state, writer) -> trans (out, state, writer))
-> (state -> trans (out, state, writer))
-> state
-> trans (out, state, writer)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. reader -> state -> trans (out, state, writer)
trans reader
reader
avoiding :: RWST reader writer state trans out
-> RWST reader writer state trans ()
avoiding (N.T.L.RWST reader -> state -> trans (out, state, writer)
trans) = (reader -> state -> trans ((), state, writer))
-> RWST reader writer state trans ()
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
N.T.L.RWST ((reader -> state -> trans ((), state, writer))
-> RWST reader writer state trans ())
-> (reader -> state -> trans ((), state, writer))
-> RWST reader writer state trans ()
forall a b. (a -> b) -> a -> b
$ \reader
reader state
state -> do
trans (out, state, writer) -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding (trans (out, state, writer) -> trans ())
-> trans (out, state, writer) -> trans ()
forall a b. (a -> b) -> a -> b
$ reader -> state -> trans (out, state, writer)
trans reader
reader state
state
((), state, writer) -> trans ((), state, writer)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), state
state, writer
forall a. Monoid a => a
mempty)
next :: RWST reader writer state trans token
next = trans token -> RWST reader writer state trans token
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
nextChunk :: Word -> RWST reader writer state trans stream
nextChunk = trans stream -> RWST reader writer state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans stream -> RWST reader writer state trans stream)
-> (Word -> trans stream)
-> Word
-> RWST reader writer state trans stream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk
push :: token -> RWST reader writer state trans ()
push = trans () -> RWST reader writer state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> RWST reader writer state trans ())
-> (token -> trans ())
-> token
-> RWST reader writer state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. token -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
token -> m ()
push
pushChunk :: stream -> RWST reader writer state trans ()
pushChunk = trans () -> RWST reader writer state trans ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift (trans () -> RWST reader writer state trans ())
-> (stream -> trans ())
-> stream
-> RWST reader writer state trans ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. stream -> trans ()
forall (m :: * -> *) stream token.
MonadParser m stream token =>
stream -> m ()
pushChunk
abridge :: RWST reader writer state trans stream
abridge = trans stream -> RWST reader writer state trans stream
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
N.T.lift trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m stream
abridge
end :: MonadParser trans stream token => trans ()
end :: trans ()
end = trans token -> trans ()
forall (m :: * -> *) stream token out.
MonadParser m stream token =>
m out -> m ()
avoiding trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next
token ::
( MonadParser trans stream token
, Eq token
) => token -> trans token
token :: token -> trans token
token token
desired = trans token
forall (m :: * -> *) stream token.
MonadParser m stream token =>
m token
next trans token -> (token -> trans token) -> trans token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (token -> Bool) -> token -> trans token
forall (trans :: * -> *) stream token out.
MonadParser trans stream token =>
(out -> Bool) -> out -> trans out
satisfying (token -> token -> Bool
forall a. Eq a => a -> a -> Bool
== token
desired)
chunk ::
( MonadParser trans stream token
, Eq stream
) => stream -> trans stream
chunk :: stream -> trans stream
chunk stream
desired = Word -> trans stream
forall (m :: * -> *) stream token.
MonadParser m stream token =>
Word -> m stream
nextChunk (stream -> Word
forall stream token. Stream stream token => stream -> Word
chunkLen stream
desired) trans stream -> (stream -> trans stream) -> trans stream
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (stream -> Bool) -> stream -> trans stream
forall (trans :: * -> *) stream token out.
MonadParser trans stream token =>
(out -> Bool) -> out -> trans out
satisfying (stream -> stream -> Bool
forall a. Eq a => a -> a -> Bool
== stream
desired)
satisfying :: MonadParser trans stream token => (out -> Bool) -> out -> trans out
satisfying :: (out -> Bool) -> out -> trans out
satisfying out -> Bool
test out
out
| out -> Bool
test out
out = out -> trans out
forall (m :: * -> *) a. Monad m => a -> m a
return out
out
| Bool
otherwise = trans out
forall (f :: * -> *) a. Alternative f => f a
A.empty