{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE UndecidableInstances #-}

{-|
Description:    Generic lazy parsers for transforming input.

Copyright:      (c) 2020 Sam May
License:        MPL-2.0
Maintainer:     ag.eitilt@gmail.com

Stability:      experimental
Portability:    portable

The existing parsing libraries are wonderful, but backtracking parsers have a
bad habit of being strict in their output; sure, you might be able to operate
over "Data.ByteString.Lazy", but they all expect to consume their entire input
before handing you their result.  "Data.Attoparsec"'s continuations fully lean
into that---even though you don't have to provide all the input in one block,
you can't get a value before closing it out.  [Text.Megaparsec]
(https://hackage.haskell.org/package/megaparsec/docs/Text-Megaparsec.html)
does provide a reentrant form in [runParser']
(https://hackage.haskell.org/package/megaparsec/docs/Text-Megaparsec.html#v:runParser-39-),
but it also comes with comparatively heavyweight error and pretty-printing
features.

For complicated formats, those all can indeed be desirable.  However, the HTML
algorithms have been optimized for minimal lookahead and certainly no output
revocation---once something is shipped out, it's not going to be called back.
Not taking advantage of that by using a lazy output type means that parsing
would always be subject to the whims of slow or unreliable network connections.
Moreover, the entire complexity of the parsing algorithm is built around never
reaching a fatal failure condition, so error handling and especially recovery
are unnecessary overhead.

And so, a custom parsing framework must be defined.
-}
module Web.Willow.Common.Parser
    ( -- * Concrete types
      Parser
    , runParser
    , ParserT ( .. )
    , StateParser
      -- * Parsing combinators
    , MonadParser ( .. )
    , end
    , satisfying
    , token
    , chunk
#if MIN_VERSION_base(4,11,0)
#else
    , (<>)
#endif
      -- * Supporting typeclasses
    , 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


-- | Unlike most monad transformers, a 'Parser' is built around the concept of
-- success and failure, so its "default" form is better structured over 'Maybe'
-- than over 'Data.Functor.Identity.Identity'.
type Parser stream = ParserT stream Maybe

-- | Set the constructed parser loose on a given input.  Returns both the
-- resulting value and the remaining contents of the 'Stream'.
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

-- | Purely a convenience of the package rather than the module, the state
-- machines described by the HTML standard all involve some degree of
-- persistence, and so are built over a deeper monad stack.  This could easily
-- one of the most common transformers to add, anyway, no matter what input is
-- being parsed.
type StateParser state stream = N.S.L.StateT state (Parser stream)

-- | Encapsulation of an operation for transforming the head of a 'Stream' into
-- some other value.  Standard usage, with similar behaviour to other
-- "Text.Parsec"-derived parsers, ("accept the first which matches") may be
-- obtained by instantiating @gather@ with 'Maybe', or non-deterministic
-- parsing ("accept any of these") through @[]@.
-- 
-- Notably, this implementation is designed to allow laziness in both input and
-- output.  For the best usage, therefore, consume as little input at a time as
-- possible, and so call 'runParser' often).
-- 
-- As part of this simplification, all "Text.Parsec"-style integrated state
-- (use 'Control.Monad.Trans.State.StateT') and "Text.Megaparsec"-style error
-- pretty-printing (build your position tracking into the @stream@, and/or wrap
-- the output in 'Either') has been stripped out.
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'

-- | 'pure' is a 'ParserT' which succeeds without consuming any input.  ('<*>')
-- and the other sequencing functions run the right 'ParserT' over the
-- remaining input after the left 'ParserT' returns.
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)

-- | 'A.empty' is a 'ParserT' which fails without consuming any input.  ('<|>')
-- applies both 'ParserT's to the same input ("automatically backtracks").
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

-- | ('<>') runs the right 'ParserT' over the remaining input after the left
-- 'ParserT' returns.
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)

-- | 'mempty' is a 'ParserT' which /succeeds/ without consuming any input; it
-- is therefore identical to @'pure' 'mempty'@ and /not/ 'A.empty' or 'N.mzero'.
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)

-- | ('>>=') runs the 'ParserT' resulting from the right (generation function)
-- argument over the remaining input after the left (value) 'ParserT' returns.
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

-- | 'N.mzero' is a 'ParserT' which fails without consuming input, while
-- 'N.mplus' applies both to the same input, modulo the semantics of the
-- @'A.Alternative' gather@ instance.
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

-- | 'N.X.throwError' is a 'ParserT' which fails without consuming any input.
-- 'N.X.catchError' runs the recovery parser over the /same/ input as was
-- passed to the original (failing) parser.
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)

-- | Performs an action on the current input without consuming it; i.e.
-- 'N.R.ask' is identical to 'N.S.get'.
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)

-- | Operates over the input that has not yet been processed.
-- 
-- Note that this therefore provides the means for forcing an early
-- end-of-stream:
-- 
-- @
-- 'N.S.put' 'mempty'
-- @
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)

-- | The parser the inner function generates is run over the remaining input
-- after the argument function runs (thus generating the inner function).
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


-- | A sequence of values which may be processed via a 'MonadParser'.  This
-- class is essentially just a unification of the various list-like interfaces
-- (@'uncons' == 'head'@, etc.) as Haskell's abstractions are slightly lacking
-- in that area.
-- 
-- >>> Just (tok, str) == uncons (cons tok str)
-- True
class Monoid stream => Stream stream token | stream -> token where
    {-# MINIMAL cons, uncons #-}
    -- | Prepend a token to the stream for proximate processing, before
    -- everything already in it.
    cons :: token -> stream -> stream
    -- | As 'cons', but append multiple tokens at once.
    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
    -- | Retrieve the next token from the stream.
    -- 
    -- This should only return 'Nothing' if the stream is actually empty---if
    -- the next value is not available yet due to slow IO or other computation,
    -- 'uncons' waits until it is.
    uncons :: stream -> Maybe (token, stream)
    -- | Retrieve the next several tokens from the stream.
    -- 
    -- If fewer tokens are in the input stream than asked for, the left side of
    -- the return value is the (shorter than requested) entire input stream and
    -- the right is 'mempty'.
    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'')
    -- | The number of tokens remaining in the stream.
    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


-- | Generalize the transformation of an input 'Stream' into a more meaningful
-- value.  This class provides the basic building blocks from which more
-- expressive such parsers may be constructed.
-- 
-- See also the description of 'ParserT' for some of the design decisions.
class
  ( A.Alternative m, Monad m
  , Stream stream token, Monoid stream
  ) => MonadParser m stream token | m -> stream where

    -- | Runs the argument parser on the current input, without consuming any
    -- of it; these are identical semantics to saving and restoring the input
    -- after running the computation, assuming the 'N.S.MonadState' instance
    -- runs over the input stream (see 'ParserT'):
    -- 
    -- @
    -- input <- 'N.S.get'
    -- a <- parser
    -- 'N.S.put' input
    -- @
    -- 
    -- @
    -- a <- 'lookAhead' parser
    -- @
    lookAhead :: m out -> m out

    -- | Succeeds if and only if the argument parser fails (the input is not
    -- consumed).
    avoiding :: m out -> m ()

    -- | Retrieve the next token in the stream, whatever it may be.  Identical
    -- to @'uncons'@ in all but type.
    next :: m token

    -- | Retrieve the next several tokens in the stream.  Identical to
    -- 'Control.Monad.Combinators.count' (with a safer index type) in the case
    -- that @gather@ is a list @[token]@.
    -- 
    -- If fewer tokens are in the input stream than asked for, returns what
    -- does remain in the input stream.
    nextChunk :: Word -> m stream

    -- | Prepend a token to the input stream to be processed next.  Identical
    -- to operating on the stream directly through 'N.S.MonadState', if that
    -- instance also exists.
    -- 
    -- @
    -- stream <- 'N.S.get'
    -- 'N.S.put' $ 'cons' tok stream
    -- @
    -- 
    -- @
    -- 'push' tok
    -- @
    push :: token -> m ()

    -- | Concatenate the given sequence with the existing input, processing the
    -- argument before the older @stream@.
    pushChunk :: stream -> m ()

    -- | Drop the remainder of the input, simulating an early end-of-stream.
    -- Can be emulated through appropriate 'N.S.MonadState' and 'Monoid'
    -- instances:
    -- 
    -- @
    -- stream <- 'N.S.get'
    -- 'N.S.put' 'mempty'
    -- 'return' stream
    -- @
    -- 
    -- @
    -- 'abridge'
    -- @
    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


-- | Succeeds if and only if the input is empty.
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

-- | Expect a specific token from the 'Stream', and fail if a different
-- token is found instead.  Identical to running 'satisfying' with equality
-- in the (by far most likely) case that @gather@ is a 'Monad' in addition
-- to an 'A.Alternative':
-- 
-- @
-- tok \<- 'next' '>>=' 'satisfying' ('==' desired)
-- @
-- 
-- @
-- tok <- 'token' desired
-- @
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)

-- | Expect a specific sequence of tokens from the 'Stream', and fail if
-- anything else is found instead, or if the 'Stream' doesn't have enough
-- characters before its end.  Identical to running 'satisfying' with equality
-- over 'nextChunk' in the case that @stream@ is an 'Eq' (which all provided
-- instances are) and can easily provide a 'length' (which they do, unless the
-- sequence to test against also needs to be lazy).
-- 
-- @
-- stream \<- 'nextChunk' ('length' desired) '>>=' 'satisfying' ('==' desired)
-- @
-- 
-- @
-- stream <- 'chunk' 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)

-- | Succeeds if and only if the value parsed by the argument parser satisfies
-- the predicate.  No further input is consumed.
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