{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module      :  Text.Megaparsec.Internal
-- Copyright   :  © 2015–present Megaparsec contributors
--                © 2007 Paolo Martini
--                © 1999–2001 Daan Leijen
-- License     :  FreeBSD
--
-- Maintainer  :  Mark Karpov <markkarpov92@gmail.com>
-- Stability   :  experimental
-- Portability :  portable
--
-- Internal definitions. Versioning rules do not apply here. Please do not
-- rely on these unless you really know what you're doing.
--
-- @since 6.5.0
module Text.Megaparsec.Internal
  ( -- * Data types
    Hints (..),
    Reply (..),
    Consumption (..),
    Result (..),
    ParsecT (..),

    -- * Helper functions
    toHints,
    withHints,
    accHints,
    refreshHints,
    runParsecT,
    withParsecT,
  )
where

import Control.Applicative
import Control.Monad
import Control.Monad.Cont.Class
import Control.Monad.Error.Class
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Reader.Class
import Control.Monad.State.Class
import Control.Monad.Trans
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as NE
import Data.Proxy
import Data.Semigroup
import Data.Set (Set)
import qualified Data.Set as E
import Data.String (IsString (..))
import Text.Megaparsec.Class
import Text.Megaparsec.Error
import Text.Megaparsec.State
import Text.Megaparsec.Stream

----------------------------------------------------------------------------
-- Data types

-- | 'Hints' represent a collection of 'ErrorItem's to be included into
-- 'ParseError' (when it's a 'TrivialError') as “expected” message items
-- when a parser fails without consuming input right after successful parser
-- that produced the hints.
--
-- For example, without hints you could get:
--
-- >>> parseTest (many (char 'r') <* eof) "ra"
-- 1:2:
-- unexpected 'a'
-- expecting end of input
--
-- We're getting better error messages with the help of hints:
--
-- >>> parseTest (many (char 'r') <* eof) "ra"
-- 1:2:
-- unexpected 'a'
-- expecting 'r' or end of input
newtype Hints t = Hints (Set (ErrorItem t))

instance (Ord t) => Semigroup (Hints t) where
  Hints Set (ErrorItem t)
xs <> :: Hints t -> Hints t -> Hints t
<> Hints Set (ErrorItem t)
ys = forall t. Set (ErrorItem t) -> Hints t
Hints forall a b. (a -> b) -> a -> b
$ Set (ErrorItem t)
xs forall a. Semigroup a => a -> a -> a
<> Set (ErrorItem t)
ys

instance (Ord t) => Monoid (Hints t) where
  mempty :: Hints t
mempty = forall t. Set (ErrorItem t) -> Hints t
Hints forall a. Monoid a => a
mempty

-- | All information available after parsing. This includes consumption of
-- input, success (with the returned value) or failure (with the parse
-- error), and parser state at the end of parsing.
--
-- See also: 'Consumption', 'Result'.
data Reply e s a = Reply (State s e) Consumption (Result s e a)

-- | Whether the input has been consumed or not.
--
-- See also: 'Result', 'Reply'.
data Consumption
  = -- | Some part of input stream was consumed
    Consumed
  | -- | No input was consumed
    Virgin

-- | Whether the parser has failed or not. On success we include the
-- resulting value, on failure we include a 'ParseError'.
--
-- See also: 'Consumption', 'Reply'.
data Result s e a
  = -- | Parser succeeded
    OK a
  | -- | Parser failed
    Error (ParseError s e)

-- | @'ParsecT' e s m a@ is a parser with custom data component of error
-- @e@, stream type @s@, underlying monad @m@ and return type @a@.
newtype ParsecT e s m a = ParsecT
  { forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ::
      forall b.
      State s e ->
      (a -> State s e -> Hints (Token s) -> m b) -> -- consumed-OK
      (ParseError s e -> State s e -> m b) -> -- consumed-error
      (a -> State s e -> Hints (Token s) -> m b) -> -- empty-OK
      (ParseError s e -> State s e -> m b) -> -- empty-error
      m b
  }

-- | @since 5.3.0
instance (Stream s, Semigroup a) => Semigroup (ParsecT e s m a) where
  <> :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)
  {-# INLINE (<>) #-}
  sconcat :: NonEmpty (ParsecT e s m a) -> ParsecT e s m a
sconcat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Semigroup a => NonEmpty a -> a
sconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
  {-# INLINE sconcat #-}

-- | @since 5.3.0
instance (Stream s, Monoid a) => Monoid (ParsecT e s m a) where
  mempty :: ParsecT e s m a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
  {-# INLINE mempty #-}
  mappend :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
  {-# INLINE mappend #-}
  mconcat :: [ParsecT e s m a] -> ParsecT e s m a
mconcat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
  {-# INLINE mconcat #-}

-- | @since 6.3.0
instance
  (a ~ Tokens s, IsString a, Eq a, Stream s, Ord e) =>
  IsString (ParsecT e s m a)
  where
  fromString :: String -> ParsecT e s m a
fromString String
s = forall e s (m :: * -> *).
MonadParsec e s m =>
(Tokens s -> Tokens s -> Bool) -> Tokens s -> m (Tokens s)
tokens forall a. Eq a => a -> a -> Bool
(==) (forall a. IsString a => String -> a
fromString String
s)

instance Functor (ParsecT e s m) where
  fmap :: forall a b. (a -> b) -> ParsecT e s m a -> ParsecT e s m b
fmap = forall a b e s (m :: * -> *).
(a -> b) -> ParsecT e s m a -> ParsecT e s m b
pMap

pMap :: (a -> b) -> ParsecT e s m a -> ParsecT e s m b
pMap :: forall a b e s (m :: * -> *).
(a -> b) -> ParsecT e s m a -> ParsecT e s m b
pMap a -> b
f ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s b -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr b -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s (b -> State s e -> Hints (Token s) -> m b
cok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) ParseError s e -> State s e -> m b
cerr (b -> State s e -> Hints (Token s) -> m b
eok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) ParseError s e -> State s e -> m b
eerr
{-# INLINE pMap #-}

-- | 'pure' returns a parser that __succeeds__ without consuming input.
instance (Stream s) => Applicative (ParsecT e s m) where
  pure :: forall a. a -> ParsecT e s m a
pure = forall s a e (m :: * -> *). Stream s => a -> ParsecT e s m a
pPure
  <*> :: forall a b.
ParsecT e s m (a -> b) -> ParsecT e s m a -> ParsecT e s m b
(<*>) = forall s e (m :: * -> *) a b.
Stream s =>
ParsecT e s m (a -> b) -> ParsecT e s m a -> ParsecT e s m b
pAp
  ParsecT e s m a
p1 *> :: forall a b. ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m b
*> ParsecT e s m b
p2 = ParsecT e s m a
p1 forall s e (m :: * -> *) a b.
Stream s =>
ParsecT e s m a -> (a -> ParsecT e s m b) -> ParsecT e s m b
`pBind` forall a b. a -> b -> a
const ParsecT e s m b
p2
  ParsecT e s m a
p1 <* :: forall a b. ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m a
<* ParsecT e s m b
p2 = do a
x1 <- ParsecT e s m a
p1; forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT e s m b
p2; forall (m :: * -> *) a. Monad m => a -> m a
return a
x1

pPure :: (Stream s) => a -> ParsecT e s m a
pPure :: forall s a e (m :: * -> *). Stream s => a -> ParsecT e s m a
pPure a
x = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
_ -> a -> State s e -> Hints (Token s) -> m b
eok a
x State s e
s forall a. Monoid a => a
mempty
{-# INLINE pPure #-}

pAp ::
  (Stream s) =>
  ParsecT e s m (a -> b) ->
  ParsecT e s m a ->
  ParsecT e s m b
pAp :: forall s e (m :: * -> *) a b.
Stream s =>
ParsecT e s m (a -> b) -> ParsecT e s m a -> ParsecT e s m b
pAp ParsecT e s m (a -> b)
m ParsecT e s m a
k = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s b -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr b -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let mcok :: (a -> b) -> State s e -> Hints (Token s) -> m b
mcok a -> b
x State s e
s' Hints (Token s)
hs =
        forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser
          ParsecT e s m a
k
          State s e
s'
          (b -> State s e -> Hints (Token s) -> m b
cok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x)
          ParseError s e -> State s e -> m b
cerr
          (forall s a e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
accHints Hints (Token s)
hs (b -> State s e -> Hints (Token s) -> m b
cok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x))
          (forall s e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
withHints Hints (Token s)
hs ParseError s e -> State s e -> m b
cerr)
      meok :: (a -> b) -> State s e -> Hints (Token s) -> m b
meok a -> b
x State s e
s' Hints (Token s)
hs =
        forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser
          ParsecT e s m a
k
          State s e
s'
          (b -> State s e -> Hints (Token s) -> m b
cok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x)
          ParseError s e -> State s e -> m b
cerr
          (forall s a e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
accHints Hints (Token s)
hs (b -> State s e -> Hints (Token s) -> m b
eok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x))
          (forall s e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
withHints Hints (Token s)
hs ParseError s e -> State s e -> m b
eerr)
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m (a -> b)
m State s e
s (a -> b) -> State s e -> Hints (Token s) -> m b
mcok ParseError s e -> State s e -> m b
cerr (a -> b) -> State s e -> Hints (Token s) -> m b
meok ParseError s e -> State s e -> m b
eerr
{-# INLINE pAp #-}

-- | 'empty' is a parser that __fails__ without consuming input.
instance (Ord e, Stream s) => Alternative (ParsecT e s m) where
  empty :: forall a. ParsecT e s m a
empty = forall (m :: * -> *) a. MonadPlus m => m a
mzero
  <|> :: forall a. ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
(<|>) = forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus

-- | 'return' returns a parser that __succeeds__ without consuming input.
instance (Stream s) => Monad (ParsecT e s m) where
  return :: forall a. a -> ParsecT e s m a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
  >>= :: forall a b.
ParsecT e s m a -> (a -> ParsecT e s m b) -> ParsecT e s m b
(>>=) = forall s e (m :: * -> *) a b.
Stream s =>
ParsecT e s m a -> (a -> ParsecT e s m b) -> ParsecT e s m b
pBind

pBind ::
  (Stream s) =>
  ParsecT e s m a ->
  (a -> ParsecT e s m b) ->
  ParsecT e s m b
pBind :: forall s e (m :: * -> *) a b.
Stream s =>
ParsecT e s m a -> (a -> ParsecT e s m b) -> ParsecT e s m b
pBind ParsecT e s m a
m a -> ParsecT e s m b
k = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s b -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr b -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let mcok :: a -> State s e -> Hints (Token s) -> m b
mcok a
x State s e
s' Hints (Token s)
hs =
        forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser
          (a -> ParsecT e s m b
k a
x)
          State s e
s'
          b -> State s e -> Hints (Token s) -> m b
cok
          ParseError s e -> State s e -> m b
cerr
          (forall s a e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
accHints Hints (Token s)
hs b -> State s e -> Hints (Token s) -> m b
cok)
          (forall s e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
withHints Hints (Token s)
hs ParseError s e -> State s e -> m b
cerr)
      meok :: a -> State s e -> Hints (Token s) -> m b
meok a
x State s e
s' Hints (Token s)
hs =
        forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser
          (a -> ParsecT e s m b
k a
x)
          State s e
s'
          b -> State s e -> Hints (Token s) -> m b
cok
          ParseError s e -> State s e -> m b
cerr
          (forall s a e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
accHints Hints (Token s)
hs b -> State s e -> Hints (Token s) -> m b
eok)
          (forall s e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
withHints Hints (Token s)
hs ParseError s e -> State s e -> m b
eerr)
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
m State s e
s a -> State s e -> Hints (Token s) -> m b
mcok ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
meok ParseError s e -> State s e -> m b
eerr
{-# INLINE pBind #-}

instance (Stream s) => Fail.MonadFail (ParsecT e s m) where
  fail :: forall a. String -> ParsecT e s m a
fail = forall e s (m :: * -> *) a. String -> ParsecT e s m a
pFail

pFail :: String -> ParsecT e s m a
pFail :: forall e s (m :: * -> *) a. String -> ParsecT e s m a
pFail String
msg = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
_ Int
o PosState s
_ [ParseError s e]
_) a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
eerr ->
  let d :: Set (ErrorFancy e)
d = forall a. a -> Set a
E.singleton (forall e. String -> ErrorFancy e
ErrorFail String
msg)
   in ParseError s e -> State s e -> m b
eerr (forall s e. Int -> Set (ErrorFancy e) -> ParseError s e
FancyError Int
o Set (ErrorFancy e)
d) State s e
s
{-# INLINE pFail #-}

instance (Stream s, MonadIO m) => MonadIO (ParsecT e s m) where
  liftIO :: forall a. IO a -> ParsecT e s m a
liftIO = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance (Stream s, MonadReader r m) => MonadReader r (ParsecT e s m) where
  ask :: ParsecT e s m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
  local :: forall a. (r -> r) -> ParsecT e s m a -> ParsecT e s m a
local r -> r
f ParsecT e s m a
p = forall s (m :: * -> *) e a.
(Stream s, Monad m) =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s e
s -> forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f (forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT ParsecT e s m a
p State s e
s)

instance (Stream s, MonadState st m) => MonadState st (ParsecT e s m) where
  get :: ParsecT e s m st
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
  put :: st -> ParsecT e s m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put

instance (Stream s, MonadCont m) => MonadCont (ParsecT e s m) where
  callCC :: forall a b.
((a -> ParsecT e s m b) -> ParsecT e s m a) -> ParsecT e s m a
callCC (a -> ParsecT e s m b) -> ParsecT e s m a
f = forall s (m :: * -> *) e a.
(Stream s, Monad m) =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s e
s ->
    forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
callCC forall a b. (a -> b) -> a -> b
$ \Reply e s a -> m (Reply e s b)
c ->
      forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT ((a -> ParsecT e s m b) -> ParsecT e s m a
f (\a
a -> forall s (m :: * -> *) e a.
(Stream s, Monad m) =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s e
s' -> Reply e s a -> m (Reply e s b)
c (forall {s} {e} {a}. State s e -> a -> Reply e s a
pack State s e
s' a
a))) State s e
s
    where
      pack :: State s e -> a -> Reply e s a
pack State s e
s a
a = forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s Consumption
Virgin (forall s e a. a -> Result s e a
OK a
a)

instance (Stream s, MonadError e' m) => MonadError e' (ParsecT e s m) where
  throwError :: forall a. e' -> ParsecT e s m a
throwError = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
  ParsecT e s m a
p catchError :: forall a.
ParsecT e s m a -> (e' -> ParsecT e s m a) -> ParsecT e s m a
`catchError` e' -> ParsecT e s m a
h = forall s (m :: * -> *) e a.
(Stream s, Monad m) =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s e
s ->
    forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT ParsecT e s m a
p State s e
s forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` \e'
e ->
      forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT (e' -> ParsecT e s m a
h e'
e) State s e
s

mkPT :: (Stream s, Monad m) => (State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT :: forall s (m :: * -> *) e a.
(Stream s, Monad m) =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT State s e -> m (Reply e s a)
k = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr -> do
  (Reply State s e
s' Consumption
consumption Result s e a
result) <- State s e -> m (Reply e s a)
k State s e
s
  case Consumption
consumption of
    Consumption
Consumed ->
      case Result s e a
result of
        OK a
x -> a -> State s e -> Hints (Token s) -> m b
cok a
x State s e
s' forall a. Monoid a => a
mempty
        Error ParseError s e
e -> ParseError s e -> State s e -> m b
cerr ParseError s e
e State s e
s'
    Consumption
Virgin ->
      case Result s e a
result of
        OK a
x -> a -> State s e -> Hints (Token s) -> m b
eok a
x State s e
s' forall a. Monoid a => a
mempty
        Error ParseError s e
e -> ParseError s e -> State s e -> m b
eerr ParseError s e
e State s e
s'

-- | 'mzero' is a parser that __fails__ without consuming input.
--
-- __Note__: strictly speaking, this instance is unlawful. The right
-- identity law does not hold, e.g. in general this is not true:
--
-- > v >> mzero = mero
--
-- However the following holds:
--
-- > try v >> mzero = mzero
instance (Ord e, Stream s) => MonadPlus (ParsecT e s m) where
  mzero :: forall a. ParsecT e s m a
mzero = forall e s (m :: * -> *) a. ParsecT e s m a
pZero
  mplus :: forall a. ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
mplus = forall e s (m :: * -> *) a.
(Ord e, Stream s) =>
ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
pPlus

pZero :: ParsecT e s m a
pZero :: forall e s (m :: * -> *) a. ParsecT e s m a
pZero = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
_ Int
o PosState s
_ [ParseError s e]
_) a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
eerr ->
  ParseError s e -> State s e -> m b
eerr (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o forall a. Maybe a
Nothing forall a. Set a
E.empty) State s e
s
{-# INLINE pZero #-}

pPlus ::
  (Ord e, Stream s) =>
  ParsecT e s m a ->
  ParsecT e s m a ->
  ParsecT e s m a
pPlus :: forall e s (m :: * -> *) a.
(Ord e, Stream s) =>
ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
pPlus ParsecT e s m a
m ParsecT e s m a
n = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let meerr :: ParseError s e -> State s e -> m b
meerr ParseError s e
err State s e
ms =
        let ncerr :: ParseError s e -> State s e -> m b
ncerr ParseError s e
err' State s e
s' = ParseError s e -> State s e -> m b
cerr (ParseError s e
err' forall a. Semigroup a => a -> a -> a
<> ParseError s e
err) (forall s e. State s e -> State s e -> State s e
longestMatch State s e
ms State s e
s')
            neok :: a -> State s e -> Hints (Token s) -> m b
neok a
x State s e
s' Hints (Token s)
hs = a -> State s e -> Hints (Token s) -> m b
eok a
x State s e
s' (forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err forall a. Semigroup a => a -> a -> a
<> Hints (Token s)
hs)
            neerr :: ParseError s e -> State s e -> m b
neerr ParseError s e
err' State s e
s' = ParseError s e -> State s e -> m b
eerr (ParseError s e
err' forall a. Semigroup a => a -> a -> a
<> ParseError s e
err) (forall s e. State s e -> State s e -> State s e
longestMatch State s e
ms State s e
s')
         in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
n State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
ncerr a -> State s e -> Hints (Token s) -> m b
neok ParseError s e -> State s e -> m b
neerr
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
m State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
meerr
{-# INLINE pPlus #-}

-- | From two states, return the one with the greater number of processed
-- tokens. If the numbers of processed tokens are equal, prefer the second
-- state.
longestMatch :: State s e -> State s e -> State s e
longestMatch :: forall s e. State s e -> State s e -> State s e
longestMatch s1 :: State s e
s1@(State s
_ Int
o1 PosState s
_ [ParseError s e]
_) s2 :: State s e
s2@(State s
_ Int
o2 PosState s
_ [ParseError s e]
_) =
  case Int
o1 forall a. Ord a => a -> a -> Ordering
`compare` Int
o2 of
    Ordering
LT -> State s e
s2
    Ordering
EQ -> State s e
s2
    Ordering
GT -> State s e
s1
{-# INLINE longestMatch #-}

-- | @since 6.0.0
instance (Stream s, MonadFix m) => MonadFix (ParsecT e s m) where
  mfix :: forall a. (a -> ParsecT e s m a) -> ParsecT e s m a
mfix a -> ParsecT e s m a
f = forall s (m :: * -> *) e a.
(Stream s, Monad m) =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s e
s -> forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix forall a b. (a -> b) -> a -> b
$ \(~(Reply State s e
_ Consumption
_ Result s e a
result)) -> do
    let a :: a
a = case Result s e a
result of
          OK a
a' -> a
a'
          Error ParseError s e
_ -> forall a. HasCallStack => String -> a
error String
"mfix ParsecT"
    forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT (a -> ParsecT e s m a
f a
a) State s e
s

instance (Stream s) => MonadTrans (ParsecT e s) where
  lift :: forall (m :: * -> *) a. Monad m => m a -> ParsecT e s m a
lift m a
amb = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
_ ->
    m a
amb forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> a -> State s e -> Hints (Token s) -> m b
eok a
a State s e
s forall a. Monoid a => a
mempty

instance (Ord e, Stream s) => MonadParsec e s (ParsecT e s m) where
  parseError :: forall a. ParseError s e -> ParsecT e s m a
parseError = forall s e (m :: * -> *) a. ParseError s e -> ParsecT e s m a
pParseError
  label :: forall a. String -> ParsecT e s m a -> ParsecT e s m a
label = forall e s (m :: * -> *) a.
String -> ParsecT e s m a -> ParsecT e s m a
pLabel
  try :: forall a. ParsecT e s m a -> ParsecT e s m a
try = forall e s (m :: * -> *) a. ParsecT e s m a -> ParsecT e s m a
pTry
  lookAhead :: forall a. ParsecT e s m a -> ParsecT e s m a
lookAhead = forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m a
pLookAhead
  notFollowedBy :: forall a. ParsecT e s m a -> ParsecT e s m ()
notFollowedBy = forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m ()
pNotFollowedBy
  withRecovery :: forall a.
(ParseError s e -> ParsecT e s m a)
-> ParsecT e s m a -> ParsecT e s m a
withRecovery = forall s e (m :: * -> *) a.
Stream s =>
(ParseError s e -> ParsecT e s m a)
-> ParsecT e s m a -> ParsecT e s m a
pWithRecovery
  observing :: forall a.
ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a)
observing = forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a)
pObserving
  eof :: ParsecT e s m ()
eof = forall e s (m :: * -> *). Stream s => ParsecT e s m ()
pEof
  token :: forall a.
(Token s -> Maybe a)
-> Set (ErrorItem (Token s)) -> ParsecT e s m a
token = forall e s (m :: * -> *) a.
Stream s =>
(Token s -> Maybe a)
-> Set (ErrorItem (Token s)) -> ParsecT e s m a
pToken
  tokens :: (Tokens s -> Tokens s -> Bool)
-> Tokens s -> ParsecT e s m (Tokens s)
tokens = forall e s (m :: * -> *).
Stream s =>
(Tokens s -> Tokens s -> Bool)
-> Tokens s -> ParsecT e s m (Tokens s)
pTokens
  takeWhileP :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
takeWhileP = forall e s (m :: * -> *).
Stream s =>
Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
pTakeWhileP
  takeWhile1P :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
takeWhile1P = forall e s (m :: * -> *).
Stream s =>
Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
pTakeWhile1P
  takeP :: Maybe String -> Int -> ParsecT e s m (Tokens s)
takeP = forall e s (m :: * -> *).
Stream s =>
Maybe String -> Int -> ParsecT e s m (Tokens s)
pTakeP
  getParserState :: ParsecT e s m (State s e)
getParserState = forall s e (m :: * -> *). Stream s => ParsecT e s m (State s e)
pGetParserState
  updateParserState :: (State s e -> State s e) -> ParsecT e s m ()
updateParserState = forall s e (m :: * -> *).
Stream s =>
(State s e -> State s e) -> ParsecT e s m ()
pUpdateParserState

pParseError ::
  ParseError s e ->
  ParsecT e s m a
pParseError :: forall s e (m :: * -> *) a. ParseError s e -> ParsecT e s m a
pParseError ParseError s e
e = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
eerr -> ParseError s e -> State s e -> m b
eerr ParseError s e
e State s e
s
{-# INLINE pParseError #-}

pLabel :: String -> ParsecT e s m a -> ParsecT e s m a
pLabel :: forall e s (m :: * -> *) a.
String -> ParsecT e s m a -> ParsecT e s m a
pLabel String
l ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let el :: Maybe (ErrorItem (Token s))
el = forall t. NonEmpty Char -> ErrorItem t
Label forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty String
l
      cok' :: a -> State s e -> Hints (Token s) -> m b
cok' a
x State s e
s' Hints (Token s)
hs =
        case Maybe (ErrorItem (Token s))
el of
          Maybe (ErrorItem (Token s))
Nothing -> a -> State s e -> Hints (Token s) -> m b
cok a
x State s e
s' (forall t. Hints t -> Maybe (ErrorItem t) -> Hints t
refreshHints Hints (Token s)
hs forall a. Maybe a
Nothing)
          Just ErrorItem (Token s)
_ -> a -> State s e -> Hints (Token s) -> m b
cok a
x State s e
s' Hints (Token s)
hs
      eok' :: a -> State s e -> Hints (Token s) -> m b
eok' a
x State s e
s' Hints (Token s)
hs = a -> State s e -> Hints (Token s) -> m b
eok a
x State s e
s' (forall t. Hints t -> Maybe (ErrorItem t) -> Hints t
refreshHints Hints (Token s)
hs Maybe (ErrorItem (Token s))
el)
      eerr' :: ParseError s e -> State s e -> m b
eerr' ParseError s e
err = ParseError s e -> State s e -> m b
eerr forall a b. (a -> b) -> a -> b
$
        case ParseError s e
err of
          (TrivialError Int
pos Maybe (ErrorItem (Token s))
us Set (ErrorItem (Token s))
_) ->
            forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
pos Maybe (ErrorItem (Token s))
us (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Set a
E.empty forall a. a -> Set a
E.singleton Maybe (ErrorItem (Token s))
el)
          ParseError s e
_ -> ParseError s e
err
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s a -> State s e -> Hints (Token s) -> m b
cok' ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok' ParseError s e -> State s e -> m b
eerr'
{-# INLINE pLabel #-}

pTry :: ParsecT e s m a -> ParsecT e s m a
pTry :: forall e s (m :: * -> *) a. ParsecT e s m a -> ParsecT e s m a
pTry ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let eerr' :: ParseError s e -> State s e -> m b
eerr' ParseError s e
err State s e
_ = ParseError s e -> State s e -> m b
eerr ParseError s e
err State s e
s
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
eerr' a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr'
{-# INLINE pTry #-}

pLookAhead :: (Stream s) => ParsecT e s m a -> ParsecT e s m a
pLookAhead :: forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m a
pLookAhead ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let eok' :: a -> State s e -> Hints (Token s) -> m b
eok' a
a State s e
_ Hints (Token s)
_ = a -> State s e -> Hints (Token s) -> m b
eok a
a State s e
s forall a. Monoid a => a
mempty
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s a -> State s e -> Hints (Token s) -> m b
eok' ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok' ParseError s e -> State s e -> m b
eerr
{-# INLINE pLookAhead #-}

pNotFollowedBy :: (Stream s) => ParsecT e s m a -> ParsecT e s m ()
pNotFollowedBy :: forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m ()
pNotFollowedBy ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
input Int
o PosState s
_ [ParseError s e]
_) () -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ () -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let what :: ErrorItem (Token s)
what = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall t. ErrorItem t
EndOfInput (forall t. NonEmpty t -> ErrorItem t
Tokens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> NonEmpty a
nes forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) (forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input)
      unexpect :: ErrorItem (Token s) -> ParseError s e
unexpect ErrorItem (Token s)
u = forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o (forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem (Token s)
u) forall a. Set a
E.empty
      cok' :: a -> State s e -> Hints (Token s) -> m b
cok' a
_ State s e
_ Hints (Token s)
_ = ParseError s e -> State s e -> m b
eerr (ErrorItem (Token s) -> ParseError s e
unexpect ErrorItem (Token s)
what) State s e
s
      cerr' :: ParseError s e -> State s e -> m b
cerr' ParseError s e
_ State s e
_ = () -> State s e -> Hints (Token s) -> m b
eok () State s e
s forall a. Monoid a => a
mempty
      eok' :: a -> State s e -> Hints (Token s) -> m b
eok' a
_ State s e
_ Hints (Token s)
_ = ParseError s e -> State s e -> m b
eerr (ErrorItem (Token s) -> ParseError s e
unexpect ErrorItem (Token s)
what) State s e
s
      eerr' :: ParseError s e -> State s e -> m b
eerr' ParseError s e
_ State s e
_ = () -> State s e -> Hints (Token s) -> m b
eok () State s e
s forall a. Monoid a => a
mempty
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s a -> State s e -> Hints (Token s) -> m b
cok' ParseError s e -> State s e -> m b
cerr' a -> State s e -> Hints (Token s) -> m b
eok' ParseError s e -> State s e -> m b
eerr'
{-# INLINE pNotFollowedBy #-}

pWithRecovery ::
  (Stream s) =>
  (ParseError s e -> ParsecT e s m a) ->
  ParsecT e s m a ->
  ParsecT e s m a
pWithRecovery :: forall s e (m :: * -> *) a.
Stream s =>
(ParseError s e -> ParsecT e s m a)
-> ParsecT e s m a -> ParsecT e s m a
pWithRecovery ParseError s e -> ParsecT e s m a
r ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
cerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let mcerr :: ParseError s e -> State s e -> m b
mcerr ParseError s e
err State s e
ms =
        let rcok :: a -> State s e -> Hints (Token s) -> m b
rcok a
x State s e
s' Hints (Token s)
_ = a -> State s e -> Hints (Token s) -> m b
cok a
x State s e
s' forall a. Monoid a => a
mempty
            rcerr :: ParseError s e -> State s e -> m b
rcerr ParseError s e
_ State s e
_ = ParseError s e -> State s e -> m b
cerr ParseError s e
err State s e
ms
            reok :: a -> State s e -> Hints (Token s) -> m b
reok a
x State s e
s' Hints (Token s)
_ = a -> State s e -> Hints (Token s) -> m b
eok a
x State s e
s' (forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err)
            reerr :: ParseError s e -> State s e -> m b
reerr ParseError s e
_ State s e
_ = ParseError s e -> State s e -> m b
cerr ParseError s e
err State s e
ms
         in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser (ParseError s e -> ParsecT e s m a
r ParseError s e
err) State s e
ms a -> State s e -> Hints (Token s) -> m b
rcok ParseError s e -> State s e -> m b
rcerr a -> State s e -> Hints (Token s) -> m b
reok ParseError s e -> State s e -> m b
reerr
      meerr :: ParseError s e -> State s e -> m b
meerr ParseError s e
err State s e
ms =
        let rcok :: a -> State s e -> Hints (Token s) -> m b
rcok a
x State s e
s' Hints (Token s)
_ = a -> State s e -> Hints (Token s) -> m b
cok a
x State s e
s' (forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err)
            rcerr :: ParseError s e -> State s e -> m b
rcerr ParseError s e
_ State s e
_ = ParseError s e -> State s e -> m b
eerr ParseError s e
err State s e
ms
            reok :: a -> State s e -> Hints (Token s) -> m b
reok a
x State s e
s' Hints (Token s)
_ = a -> State s e -> Hints (Token s) -> m b
eok a
x State s e
s' (forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err)
            reerr :: ParseError s e -> State s e -> m b
reerr ParseError s e
_ State s e
_ = ParseError s e -> State s e -> m b
eerr ParseError s e
err State s e
ms
         in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser (ParseError s e -> ParsecT e s m a
r ParseError s e
err) State s e
ms a -> State s e -> Hints (Token s) -> m b
rcok ParseError s e -> State s e -> m b
rcerr a -> State s e -> Hints (Token s) -> m b
reok ParseError s e -> State s e -> m b
reerr
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
mcerr a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
meerr
{-# INLINE pWithRecovery #-}

pObserving ::
  (Stream s) =>
  ParsecT e s m a ->
  ParsecT e s m (Either (ParseError s e) a)
pObserving :: forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a)
pObserving ParsecT e s m a
p = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
_ ->
  let cerr' :: ParseError s e -> State s e -> m b
cerr' ParseError s e
err State s e
s' = Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b
cok (forall a b. a -> Either a b
Left ParseError s e
err) State s e
s' forall a. Monoid a => a
mempty
      eerr' :: ParseError s e -> State s e -> m b
eerr' ParseError s e
err State s e
s' = Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b
eok (forall a b. a -> Either a b
Left ParseError s e
err) State s e
s' (forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err)
   in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s (Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b
cok forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) ParseError s e -> State s e -> m b
cerr' (Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b
eok forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) ParseError s e -> State s e -> m b
eerr'
{-# INLINE pObserving #-}

pEof :: forall e s m. (Stream s) => ParsecT e s m ()
pEof :: forall e s (m :: * -> *). Stream s => ParsecT e s m ()
pEof = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
input Int
o PosState s
pst [ParseError s e]
de) () -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ () -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  case forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input of
    Maybe (Token s, s)
Nothing -> () -> State s e -> Hints (Token s) -> m b
eok () State s e
s forall a. Monoid a => a
mempty
    Just (Token s
x, s
_) ->
      let us :: Maybe (ErrorItem (Token s))
us = (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. NonEmpty t -> ErrorItem t
Tokens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> NonEmpty a
nes) Token s
x
          ps :: Set (ErrorItem t)
ps = forall a. a -> Set a
E.singleton forall t. ErrorItem t
EndOfInput
       in ParseError s e -> State s e -> m b
eerr
            (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o Maybe (ErrorItem (Token s))
us forall {t}. Set (ErrorItem t)
ps)
            (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input Int
o PosState s
pst [ParseError s e]
de)
{-# INLINE pEof #-}

pToken ::
  forall e s m a.
  (Stream s) =>
  (Token s -> Maybe a) ->
  Set (ErrorItem (Token s)) ->
  ParsecT e s m a
pToken :: forall e s (m :: * -> *) a.
Stream s =>
(Token s -> Maybe a)
-> Set (ErrorItem (Token s)) -> ParsecT e s m a
pToken Token s -> Maybe a
test Set (ErrorItem (Token s))
ps = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
input Int
o PosState s
pst [ParseError s e]
de) a -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ a -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
eerr ->
  case forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input of
    Maybe (Token s, s)
Nothing ->
      let us :: Maybe (ErrorItem t)
us = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall t. ErrorItem t
EndOfInput
       in ParseError s e -> State s e -> m b
eerr (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o forall {t}. Maybe (ErrorItem t)
us Set (ErrorItem (Token s))
ps) State s e
s
    Just (Token s
c, s
cs) ->
      case Token s -> Maybe a
test Token s
c of
        Maybe a
Nothing ->
          let us :: Maybe (ErrorItem (Token s))
us = (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. NonEmpty t -> ErrorItem t
Tokens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> NonEmpty a
nes) Token s
c
           in ParseError s e -> State s e -> m b
eerr
                (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o Maybe (ErrorItem (Token s))
us Set (ErrorItem (Token s))
ps)
                (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input Int
o PosState s
pst [ParseError s e]
de)
        Just a
x ->
          a -> State s e -> Hints (Token s) -> m b
cok a
x (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
cs (Int
o forall a. Num a => a -> a -> a
+ Int
1) PosState s
pst [ParseError s e]
de) forall a. Monoid a => a
mempty
{-# INLINE pToken #-}

pTokens ::
  forall e s m.
  (Stream s) =>
  (Tokens s -> Tokens s -> Bool) ->
  Tokens s ->
  ParsecT e s m (Tokens s)
pTokens :: forall e s (m :: * -> *).
Stream s =>
(Tokens s -> Tokens s -> Bool)
-> Tokens s -> ParsecT e s m (Tokens s)
pTokens Tokens s -> Tokens s -> Bool
f Tokens s
tts = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
input Int
o PosState s
pst [ParseError s e]
de) Tokens s -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ Tokens s -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
eerr ->
  let pxy :: Proxy s
pxy = forall {k} (t :: k). Proxy t
Proxy :: Proxy s
      unexpect :: Int -> ErrorItem (Token s) -> ParseError s e
unexpect Int
pos' ErrorItem (Token s)
u =
        let us :: Maybe (ErrorItem (Token s))
us = forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem (Token s)
u
            ps :: Set (ErrorItem (Token s))
ps = (forall a. a -> Set a
E.singleton forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. NonEmpty t -> ErrorItem t
Tokens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> NonEmpty a
NE.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens Proxy s
pxy) Tokens s
tts
         in forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
pos' Maybe (ErrorItem (Token s))
us Set (ErrorItem (Token s))
ps
      len :: Int
len = forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
tts
   in case forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
takeN_ Int
len s
input of
        Maybe (Tokens s, s)
Nothing ->
          ParseError s e -> State s e -> m b
eerr (Int -> ErrorItem (Token s) -> ParseError s e
unexpect Int
o forall t. ErrorItem t
EndOfInput) State s e
s
        Just (Tokens s
tts', s
input') ->
          if Tokens s -> Tokens s -> Bool
f Tokens s
tts Tokens s
tts'
            then
              let st :: State s e
st = forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de
               in if forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty Proxy s
pxy Tokens s
tts
                    then Tokens s -> State s e -> Hints (Token s) -> m b
eok Tokens s
tts' State s e
st forall a. Monoid a => a
mempty
                    else Tokens s -> State s e -> Hints (Token s) -> m b
cok Tokens s
tts' State s e
st forall a. Monoid a => a
mempty
            else
              let ps :: ErrorItem (Token s)
ps = (forall t. NonEmpty t -> ErrorItem t
Tokens forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> NonEmpty a
NE.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens Proxy s
pxy) Tokens s
tts'
               in ParseError s e -> State s e -> m b
eerr (Int -> ErrorItem (Token s) -> ParseError s e
unexpect Int
o ErrorItem (Token s)
ps) (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input Int
o PosState s
pst [ParseError s e]
de)
{-# INLINE pTokens #-}

pTakeWhileP ::
  forall e s m.
  (Stream s) =>
  Maybe String ->
  (Token s -> Bool) ->
  ParsecT e s m (Tokens s)
pTakeWhileP :: forall e s (m :: * -> *).
Stream s =>
Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
pTakeWhileP Maybe String
ml Token s -> Bool
f = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \(State s
input Int
o PosState s
pst [ParseError s e]
de) Tokens s -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ Tokens s -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
_ ->
  let pxy :: Proxy s
pxy = forall {k} (t :: k). Proxy t
Proxy :: Proxy s
      (Tokens s
ts, s
input') = forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token s -> Bool
f s
input
      len :: Int
len = forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
ts
      hs :: Hints (Token s)
hs =
        case Maybe String
ml forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty of
          Maybe (NonEmpty Char)
Nothing -> forall a. Monoid a => a
mempty
          Just NonEmpty Char
l -> (forall t. Set (ErrorItem t) -> Hints t
Hints forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Set a
E.singleton forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. NonEmpty Char -> ErrorItem t
Label) NonEmpty Char
l
   in if forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty Proxy s
pxy Tokens s
ts
        then Tokens s -> State s e -> Hints (Token s) -> m b
eok Tokens s
ts (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de) Hints (Token s)
hs
        else Tokens s -> State s e -> Hints (Token s) -> m b
cok Tokens s
ts (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de) Hints (Token s)
hs
{-# INLINE pTakeWhileP #-}

pTakeWhile1P ::
  forall e s m.
  (Stream s) =>
  Maybe String ->
  (Token s -> Bool) ->
  ParsecT e s m (Tokens s)
pTakeWhile1P :: forall e s (m :: * -> *).
Stream s =>
Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
pTakeWhile1P Maybe String
ml Token s -> Bool
f = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \(State s
input Int
o PosState s
pst [ParseError s e]
de) Tokens s -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ Tokens s -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
eerr ->
  let pxy :: Proxy s
pxy = forall {k} (t :: k). Proxy t
Proxy :: Proxy s
      (Tokens s
ts, s
input') = forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token s -> Bool
f s
input
      len :: Int
len = forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
ts
      el :: Maybe (ErrorItem (Token s))
el = forall t. NonEmpty Char -> ErrorItem t
Label forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe String
ml forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty)
      hs :: Hints (Token s)
hs =
        case Maybe (ErrorItem (Token s))
el of
          Maybe (ErrorItem (Token s))
Nothing -> forall a. Monoid a => a
mempty
          Just ErrorItem (Token s)
l -> (forall t. Set (ErrorItem t) -> Hints t
Hints forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Set a
E.singleton) ErrorItem (Token s)
l
   in if forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty Proxy s
pxy Tokens s
ts
        then
          let us :: Maybe (ErrorItem (Token s))
us = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$
                case forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input of
                  Maybe (Token s, s)
Nothing -> forall t. ErrorItem t
EndOfInput
                  Just (Token s
t, s
_) -> forall t. NonEmpty t -> ErrorItem t
Tokens (forall a. a -> NonEmpty a
nes Token s
t)
              ps :: Set (ErrorItem (Token s))
ps = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Set a
E.empty forall a. a -> Set a
E.singleton Maybe (ErrorItem (Token s))
el
           in ParseError s e -> State s e -> m b
eerr
                (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o Maybe (ErrorItem (Token s))
us Set (ErrorItem (Token s))
ps)
                (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input Int
o PosState s
pst [ParseError s e]
de)
        else Tokens s -> State s e -> Hints (Token s) -> m b
cok Tokens s
ts (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de) Hints (Token s)
hs
{-# INLINE pTakeWhile1P #-}

pTakeP ::
  forall e s m.
  (Stream s) =>
  Maybe String ->
  Int ->
  ParsecT e s m (Tokens s)
pTakeP :: forall e s (m :: * -> *).
Stream s =>
Maybe String -> Int -> ParsecT e s m (Tokens s)
pTakeP Maybe String
ml Int
n' = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \s :: State s e
s@(State s
input Int
o PosState s
pst [ParseError s e]
de) Tokens s -> State s e -> Hints (Token s) -> m b
cok ParseError s e -> State s e -> m b
_ Tokens s -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
eerr ->
  let n :: Int
n = forall a. Ord a => a -> a -> a
max Int
0 Int
n'
      pxy :: Proxy s
pxy = forall {k} (t :: k). Proxy t
Proxy :: Proxy s
      el :: Maybe (ErrorItem (Token s))
el = forall t. NonEmpty Char -> ErrorItem t
Label forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe String
ml forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty)
      ps :: Set (ErrorItem (Token s))
ps = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Set a
E.empty forall a. a -> Set a
E.singleton Maybe (ErrorItem (Token s))
el
   in case forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
takeN_ Int
n s
input of
        Maybe (Tokens s, s)
Nothing ->
          ParseError s e -> State s e -> m b
eerr (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall t. ErrorItem t
EndOfInput) Set (ErrorItem (Token s))
ps) State s e
s
        Just (Tokens s
ts, s
input') ->
          let len :: Int
len = forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
ts
           in if Int
len forall a. Eq a => a -> a -> Bool
/= Int
n
                then
                  ParseError s e -> State s e -> m b
eerr
                    (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError (Int
o forall a. Num a => a -> a -> a
+ Int
len) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall t. ErrorItem t
EndOfInput) Set (ErrorItem (Token s))
ps)
                    (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input Int
o PosState s
pst [ParseError s e]
de)
                else Tokens s -> State s e -> Hints (Token s) -> m b
cok Tokens s
ts (forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de) forall a. Monoid a => a
mempty
{-# INLINE pTakeP #-}

pGetParserState :: (Stream s) => ParsecT e s m (State s e)
pGetParserState :: forall s e (m :: * -> *). Stream s => ParsecT e s m (State s e)
pGetParserState = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s State s e -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ State s e -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
_ -> State s e -> State s e -> Hints (Token s) -> m b
eok State s e
s State s e
s forall a. Monoid a => a
mempty
{-# INLINE pGetParserState #-}

pUpdateParserState :: (Stream s) => (State s e -> State s e) -> ParsecT e s m ()
pUpdateParserState :: forall s e (m :: * -> *).
Stream s =>
(State s e -> State s e) -> ParsecT e s m ()
pUpdateParserState State s e -> State s e
f = forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e
s () -> State s e -> Hints (Token s) -> m b
_ ParseError s e -> State s e -> m b
_ () -> State s e -> Hints (Token s) -> m b
eok ParseError s e -> State s e -> m b
_ -> () -> State s e -> Hints (Token s) -> m b
eok () (State s e -> State s e
f State s e
s) forall a. Monoid a => a
mempty
{-# INLINE pUpdateParserState #-}

nes :: a -> NonEmpty a
nes :: forall a. a -> NonEmpty a
nes a
x = a
x forall a. a -> [a] -> NonEmpty a
:| []
{-# INLINE nes #-}

----------------------------------------------------------------------------
-- Helper functions

-- | Convert a 'ParseError' record into 'Hints'.
toHints ::
  (Stream s) =>
  -- | Current offset in input stream
  Int ->
  -- | Parse error to convert
  ParseError s e ->
  Hints (Token s)
toHints :: forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints Int
streamPos = \case
  TrivialError Int
errOffset Maybe (ErrorItem (Token s))
_ Set (ErrorItem (Token s))
ps ->
    -- NOTE This is important to check here that the error indeed has
    -- happened at the same position as current position of stream because
    -- there might have been backtracking with 'try' and in that case we
    -- must not convert such a parse error to hints.
    if Int
streamPos forall a. Eq a => a -> a -> Bool
== Int
errOffset
      then forall t. Set (ErrorItem t) -> Hints t
Hints (if forall a. Set a -> Bool
E.null Set (ErrorItem (Token s))
ps then forall a. Set a
E.empty else Set (ErrorItem (Token s))
ps)
      else forall a. Monoid a => a
mempty
  FancyError Int
_ Set (ErrorFancy e)
_ -> forall a. Monoid a => a
mempty
{-# INLINE toHints #-}

-- | @'withHints' hs c@ makes “error” continuation @c@ use given hints @hs@.
--
-- __Note__ that if resulting continuation gets 'ParseError' that has custom
-- data in it, hints are ignored.
withHints ::
  (Stream s) =>
  -- | Hints to use
  Hints (Token s) ->
  -- | Continuation to influence
  (ParseError s e -> State s e -> m b) ->
  -- | First argument of resulting continuation
  ParseError s e ->
  -- | Second argument of resulting continuation
  State s e ->
  m b
withHints :: forall s e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
withHints (Hints Set (ErrorItem (Token s))
ps') ParseError s e -> State s e -> m b
c ParseError s e
e =
  case ParseError s e
e of
    TrivialError Int
pos Maybe (ErrorItem (Token s))
us Set (ErrorItem (Token s))
ps -> ParseError s e -> State s e -> m b
c (forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
pos Maybe (ErrorItem (Token s))
us (forall a. Ord a => Set a -> Set a -> Set a
E.union Set (ErrorItem (Token s))
ps Set (ErrorItem (Token s))
ps'))
    ParseError s e
_ -> ParseError s e -> State s e -> m b
c ParseError s e
e
{-# INLINE withHints #-}

-- | @'accHints' hs c@ results in “OK” continuation that will add given
-- hints @hs@ to third argument of original continuation @c@.
accHints ::
  (Stream s) =>
  -- | 'Hints' to add
  Hints (Token s) ->
  -- | An “OK” continuation to alter
  (a -> State s e -> Hints (Token s) -> m b) ->
  -- | Altered “OK” continuation
  (a -> State s e -> Hints (Token s) -> m b)
accHints :: forall s a e (m :: * -> *) b.
Stream s =>
Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
accHints Hints (Token s)
hs1 a -> State s e -> Hints (Token s) -> m b
c a
x State s e
s Hints (Token s)
hs2 = a -> State s e -> Hints (Token s) -> m b
c a
x State s e
s (Hints (Token s)
hs1 forall a. Semigroup a => a -> a -> a
<> Hints (Token s)
hs2)
{-# INLINE accHints #-}

-- | Replace the hints with the given 'ErrorItem' (or delete it if 'Nothing'
-- is given). This is used in the 'label' primitive.
refreshHints :: Hints t -> Maybe (ErrorItem t) -> Hints t
refreshHints :: forall t. Hints t -> Maybe (ErrorItem t) -> Hints t
refreshHints (Hints Set (ErrorItem t)
_) Maybe (ErrorItem t)
Nothing = forall t. Set (ErrorItem t) -> Hints t
Hints forall a. Set a
E.empty
refreshHints (Hints Set (ErrorItem t)
hs) (Just ErrorItem t
m) =
  if forall a. Set a -> Bool
E.null Set (ErrorItem t)
hs
    then forall t. Set (ErrorItem t) -> Hints t
Hints Set (ErrorItem t)
hs
    else forall t. Set (ErrorItem t) -> Hints t
Hints (forall a. a -> Set a
E.singleton ErrorItem t
m)
{-# INLINE refreshHints #-}

-- | Low-level unpacking of the 'ParsecT' type.
runParsecT ::
  (Monad m) =>
  -- | Parser to run
  ParsecT e s m a ->
  -- | Initial state
  State s e ->
  m (Reply e s a)
runParsecT :: forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT ParsecT e s m a
p State s e
s = forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s forall {m :: * -> *} {a} {s} {e} {p}.
Monad m =>
a -> State s e -> p -> m (Reply e s a)
cok forall {m :: * -> *} {s} {e} {a}.
Monad m =>
ParseError s e -> State s e -> m (Reply e s a)
cerr forall {m :: * -> *} {a} {s} {e} {p}.
Monad m =>
a -> State s e -> p -> m (Reply e s a)
eok forall {m :: * -> *} {s} {e} {a}.
Monad m =>
ParseError s e -> State s e -> m (Reply e s a)
eerr
  where
    cok :: a -> State s e -> p -> m (Reply e s a)
cok a
a State s e
s' p
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Consumed (forall s e a. a -> Result s e a
OK a
a)
    cerr :: ParseError s e -> State s e -> m (Reply e s a)
cerr ParseError s e
err State s e
s' = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Consumed (forall s e a. ParseError s e -> Result s e a
Error ParseError s e
err)
    eok :: a -> State s e -> p -> m (Reply e s a)
eok a
a State s e
s' p
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Virgin (forall s e a. a -> Result s e a
OK a
a)
    eerr :: ParseError s e -> State s e -> m (Reply e s a)
eerr ParseError s e
err State s e
s' = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Virgin (forall s e a. ParseError s e -> Result s e a
Error ParseError s e
err)

-- | Transform any custom errors thrown by the parser using the given
-- function. Similar in function and purpose to @withExceptT@.
--
-- __Note__ that the inner parser will start with an empty collection of
-- “delayed” 'ParseError's. Any delayed 'ParseError's produced in the inner
-- parser will be lifted by applying the provided function and added to the
-- collection of delayed parse errors of the outer parser.
--
-- @since 7.0.0
withParsecT ::
  forall e e' s m a.
  (Monad m, Ord e') =>
  (e -> e') ->
  -- | Inner parser
  ParsecT e s m a ->
  -- | Outer parser
  ParsecT e' s m a
withParsecT :: forall e e' s (m :: * -> *) a.
(Monad m, Ord e') =>
(e -> e') -> ParsecT e s m a -> ParsecT e' s m a
withParsecT e -> e'
f ParsecT e s m a
p =
  forall e s (m :: * -> *) a.
(forall b.
 State s e
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (a -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s e'
s a -> State s e' -> Hints (Token s) -> m b
cok ParseError s e' -> State s e' -> m b
cerr a -> State s e' -> Hints (Token s) -> m b
eok ParseError s e' -> State s e' -> m b
eerr ->
    let s' :: State s e
s' =
          State s e'
s
            { stateParseErrors :: [ParseError s e]
stateParseErrors = []
            }
        adjustState :: State s e -> State s e'
        adjustState :: State s e -> State s e'
adjustState State s e
st =
          State s e
st
            { stateParseErrors :: [ParseError s e']
stateParseErrors =
                (forall e' e s.
Ord e' =>
(e -> e') -> ParseError s e -> ParseError s e'
mapParseError e -> e'
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s e. State s e -> [ParseError s e]
stateParseErrors State s e
st)
                  forall a. [a] -> [a] -> [a]
++ forall s e. State s e -> [ParseError s e]
stateParseErrors State s e'
s
            }
        cok' :: a -> State s e -> Hints (Token s) -> m b
cok' a
x State s e
st Hints (Token s)
hs = a -> State s e' -> Hints (Token s) -> m b
cok a
x (State s e -> State s e'
adjustState State s e
st) Hints (Token s)
hs
        cerr' :: ParseError s e -> State s e -> m b
cerr' ParseError s e
e State s e
st = ParseError s e' -> State s e' -> m b
cerr (forall e' e s.
Ord e' =>
(e -> e') -> ParseError s e -> ParseError s e'
mapParseError e -> e'
f ParseError s e
e) (State s e -> State s e'
adjustState State s e
st)
        eok' :: a -> State s e -> Hints (Token s) -> m b
eok' a
x State s e
st Hints (Token s)
hs = a -> State s e' -> Hints (Token s) -> m b
eok a
x (State s e -> State s e'
adjustState State s e
st) Hints (Token s)
hs
        eerr' :: ParseError s e -> State s e -> m b
eerr' ParseError s e
e State s e
st = ParseError s e' -> State s e' -> m b
eerr (forall e' e s.
Ord e' =>
(e -> e') -> ParseError s e -> ParseError s e'
mapParseError e -> e'
f ParseError s e
e) (State s e -> State s e'
adjustState State s e
st)
     in forall e s (m :: * -> *) a.
ParsecT e s m a
-> forall b.
   State s e
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> (a -> State s e -> Hints (Token s) -> m b)
   -> (ParseError s e -> State s e -> m b)
   -> m b
unParser ParsecT e s m a
p State s e
s' a -> State s e -> Hints (Token s) -> m b
cok' ParseError s e -> State s e -> m b
cerr' a -> State s e -> Hints (Token s) -> m b
eok' ParseError s e -> State s e -> m b
eerr'
{-# INLINE withParsecT #-}