{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# 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,
    refreshLastHint,
    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 Semigroup (Hints t) where
  Hints [Set (ErrorItem t)]
xs <> :: Hints t -> Hints t -> Hints t
<> Hints [Set (ErrorItem t)]
ys = [Set (ErrorItem t)] -> Hints t
forall t. [Set (ErrorItem t)] -> Hints t
Hints ([Set (ErrorItem t)] -> Hints t) -> [Set (ErrorItem t)] -> Hints t
forall a b. (a -> b) -> a -> b
$ [Set (ErrorItem t)]
xs [Set (ErrorItem t)] -> [Set (ErrorItem t)] -> [Set (ErrorItem t)]
forall a. Semigroup a => a -> a -> a
<> [Set (ErrorItem t)]
ys

instance Monoid (Hints t) where
  mempty :: Hints t
mempty = [Set (ErrorItem t)] -> Hints t
forall t. [Set (ErrorItem t)] -> Hints t
Hints [Set (ErrorItem t)]
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
  { 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
(<>) = (a -> a -> a)
-> 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 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
  {-# INLINE (<>) #-}
  sconcat :: NonEmpty (ParsecT e s m a) -> ParsecT e s m a
sconcat = (NonEmpty a -> a) -> ParsecT e s m (NonEmpty a) -> ParsecT e s m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty a -> a
forall a. Semigroup a => NonEmpty a -> a
sconcat (ParsecT e s m (NonEmpty a) -> ParsecT e s m a)
-> (NonEmpty (ParsecT e s m a) -> ParsecT e s m (NonEmpty a))
-> NonEmpty (ParsecT e s m a)
-> ParsecT e s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (ParsecT e s m a) -> ParsecT e s m (NonEmpty a)
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 = a -> ParsecT e s m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
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 = ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
forall a. Semigroup a => a -> a -> a
(<>)
  {-# INLINE mappend #-}
  mconcat :: [ParsecT e s m a] -> ParsecT e s m a
mconcat = ([a] -> a) -> ParsecT e s m [a] -> ParsecT e s m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> a
forall a. Monoid a => [a] -> a
mconcat (ParsecT e s m [a] -> ParsecT e s m a)
-> ([ParsecT e s m a] -> ParsecT e s m [a])
-> [ParsecT e s m a]
-> ParsecT e s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ParsecT e s m a] -> ParsecT e s m [a]
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 = (Tokens s -> Tokens s -> Bool)
-> Tokens s -> ParsecT e s m (Tokens s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Tokens s -> Tokens s -> Bool) -> Tokens s -> m (Tokens s)
tokens Tokens s -> Tokens s -> Bool
forall a. Eq a => a -> a -> Bool
(==) (String -> a
forall a. IsString a => String -> a
fromString String
s)

instance Functor (ParsecT e s m) where
  fmap :: (a -> b) -> ParsecT e s m a -> ParsecT e s m b
fmap = (a -> b) -> ParsecT e s m a -> ParsecT e s m b
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 :: (a -> b) -> ParsecT e s m a -> ParsecT e s m b
pMap a -> b
f ParsecT e s m a
p = (forall b.
 State s e
 -> (b -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (b -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m b
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 b.
  State s e
  -> (b -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> (b -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m b)
-> (forall b.
    State s e
    -> (b -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> (b -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m b
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 ->
  ParsecT e s m a
-> 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
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 (b -> State s e -> Hints (Token s) -> m b)
-> (a -> b) -> a -> State s e -> Hints (Token s) -> m b
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 (b -> State s e -> Hints (Token s) -> m b)
-> (a -> b) -> a -> State s e -> Hints (Token s) -> m b
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 :: a -> ParsecT e s m a
pure = a -> ParsecT e s m a
forall a e s (m :: * -> *). a -> ParsecT e s m a
pPure
  <*> :: ParsecT e s m (a -> b) -> ParsecT e s m a -> ParsecT e s m 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 *> :: 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 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` ParsecT e s m b -> a -> ParsecT e s m b
forall a b. a -> b -> a
const ParsecT e s m b
p2
  ParsecT e s m a
p1 <* :: 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; ParsecT e s m b -> ParsecT e s m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT e s m b
p2; a -> ParsecT e s m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x1

pPure :: a -> ParsecT e s m a
pPure :: a -> ParsecT e s m a
pPure a
x = (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
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 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)
-> (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
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 Hints (Token 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 :: 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 b.
 State s e
 -> (b -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (b -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m b
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 b.
  State s e
  -> (b -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> (b -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m b)
-> (forall b.
    State s e
    -> (b -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> (b -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m b
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 =
        ParsecT e s m a
-> 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
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 (b -> State s e -> Hints (Token s) -> m b)
-> (a -> b) -> a -> State s e -> Hints (Token s) -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x)
          ParseError s e -> State s e -> m b
cerr
          (Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
forall t a s e (m :: * -> *) b.
Hints t
-> (a -> State s e -> Hints t -> m b)
-> a
-> State s e
-> Hints t
-> m b
accHints Hints (Token s)
hs (b -> State s e -> Hints (Token s) -> m b
cok (b -> State s e -> Hints (Token s) -> m b)
-> (a -> b) -> a -> State s e -> Hints (Token s) -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x))
          (Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
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 =
        ParsecT e s m a
-> 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
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 (b -> State s e -> Hints (Token s) -> m b)
-> (a -> b) -> a -> State s e -> Hints (Token s) -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x)
          ParseError s e -> State s e -> m b
cerr
          (Hints (Token s)
-> (a -> State s e -> Hints (Token s) -> m b)
-> a
-> State s e
-> Hints (Token s)
-> m b
forall t a s e (m :: * -> *) b.
Hints t
-> (a -> State s e -> Hints t -> m b)
-> a
-> State s e
-> Hints t
-> m b
accHints Hints (Token s)
hs (b -> State s e -> Hints (Token s) -> m b
eok (b -> State s e -> Hints (Token s) -> m b)
-> (a -> b) -> a -> State s e -> Hints (Token s) -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
x))
          (Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
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 ParsecT e s m (a -> b)
-> State s e
-> ((a -> b) -> State s e -> Hints (Token s) -> m b)
-> (ParseError s e -> State s e -> m b)
-> ((a -> b) -> State s e -> Hints (Token s) -> m b)
-> (ParseError s e -> State s e -> m b)
-> m b
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 :: ParsecT e s m a
empty = ParsecT e s m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
  <|> :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m 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 :: a -> ParsecT e s m a
return = a -> ParsecT e s m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  >>= :: ParsecT e s m a -> (a -> ParsecT e s m b) -> ParsecT e s m 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 :: 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 b.
 State s e
 -> (b -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (b -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m b
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 b.
  State s e
  -> (b -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> (b -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m b)
-> (forall b.
    State s e
    -> (b -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> (b -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m b
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 =
        ParsecT e s m b
-> State s e
-> (b -> State s e -> Hints (Token s) -> m b)
-> (ParseError s e -> State s e -> m b)
-> (b -> State s e -> Hints (Token s) -> m b)
-> (ParseError s e -> State s e -> m b)
-> m b
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
          (Hints (Token s)
-> (b -> State s e -> Hints (Token s) -> m b)
-> b
-> State s e
-> Hints (Token s)
-> m b
forall t a s e (m :: * -> *) b.
Hints t
-> (a -> State s e -> Hints t -> m b)
-> a
-> State s e
-> Hints t
-> m b
accHints Hints (Token s)
hs b -> State s e -> Hints (Token s) -> m b
cok)
          (Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
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 =
        ParsecT e s m b
-> State s e
-> (b -> State s e -> Hints (Token s) -> m b)
-> (ParseError s e -> State s e -> m b)
-> (b -> State s e -> Hints (Token s) -> m b)
-> (ParseError s e -> State s e -> m b)
-> m b
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
          (Hints (Token s)
-> (b -> State s e -> Hints (Token s) -> m b)
-> b
-> State s e
-> Hints (Token s)
-> m b
forall t a s e (m :: * -> *) b.
Hints t
-> (a -> State s e -> Hints t -> m b)
-> a
-> State s e
-> Hints t
-> m b
accHints Hints (Token s)
hs b -> State s e -> Hints (Token s) -> m b
eok)
          (Hints (Token s)
-> (ParseError s e -> State s e -> m b)
-> ParseError s e
-> State s e
-> m b
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 ParsecT e s m a
-> 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
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 :: String -> ParsecT e s m a
fail = String -> ParsecT e s m a
forall e s (m :: * -> *) a. String -> ParsecT e s m a
pFail

pFail :: String -> ParsecT e s m a
pFail :: String -> ParsecT e s m a
pFail String
msg = (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
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 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)
-> (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
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 = ErrorFancy e -> Set (ErrorFancy e)
forall a. a -> Set a
E.singleton (String -> ErrorFancy e
forall e. String -> ErrorFancy e
ErrorFail String
msg)
   in ParseError s e -> State s e -> m b
eerr (Int -> Set (ErrorFancy e) -> ParseError s e
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 :: IO a -> ParsecT e s m a
liftIO = m a -> ParsecT e s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ParsecT e s m a)
-> (IO a -> m a) -> IO a -> ParsecT e s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
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 = m r -> ParsecT e s m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m r
forall r (m :: * -> *). MonadReader r m => m r
ask
  local :: (r -> r) -> ParsecT e s m a -> ParsecT e s m a
local r -> r
f ParsecT e s m a
p = (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall (m :: * -> *) s e a.
Monad m =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT ((State s e -> m (Reply e s a)) -> ParsecT e s m a)
-> (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall a b. (a -> b) -> a -> b
$ \State s e
s -> (r -> r) -> m (Reply e s a) -> m (Reply e s a)
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f (ParsecT e s m a -> State s e -> m (Reply e s a)
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 = m st -> ParsecT e s m st
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m st
forall s (m :: * -> *). MonadState s m => m s
get
  put :: st -> ParsecT e s m ()
put = m () -> ParsecT e s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> ParsecT e s m ())
-> (st -> m ()) -> st -> ParsecT e s m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. st -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put

instance (Stream s, MonadCont m) => MonadCont (ParsecT e s m) where
  callCC :: ((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 = (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall (m :: * -> *) s e a.
Monad m =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT ((State s e -> m (Reply e s a)) -> ParsecT e s m a)
-> (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall a b. (a -> b) -> a -> b
$ \State s e
s ->
    ((Reply e s a -> m (Reply e s b)) -> m (Reply e s a))
-> m (Reply e s a)
forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
callCC (((Reply e s a -> m (Reply e s b)) -> m (Reply e s a))
 -> m (Reply e s a))
-> ((Reply e s a -> m (Reply e s b)) -> m (Reply e s a))
-> m (Reply e s a)
forall a b. (a -> b) -> a -> b
$ \Reply e s a -> m (Reply e s b)
c ->
      ParsecT e s m a -> State s e -> m (Reply e s a)
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 -> (State s e -> m (Reply e s b)) -> ParsecT e s m b
forall (m :: * -> *) s e a.
Monad m =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT ((State s e -> m (Reply e s b)) -> ParsecT e s m b)
-> (State s e -> m (Reply e s b)) -> ParsecT e s m b
forall a b. (a -> b) -> a -> b
$ \State s e
s' -> Reply e s a -> m (Reply e s b)
c (State s e -> a -> Reply e s a
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 = State s e -> Consumption -> Result s e a -> Reply e s a
forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s Consumption
Virgin (a -> Result s e a
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 :: e' -> ParsecT e s m a
throwError = m a -> ParsecT e s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ParsecT e s m a) -> (e' -> m a) -> e' -> ParsecT e s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e' -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
  ParsecT e s m a
p catchError :: ParsecT e s m a -> (e' -> ParsecT e s m a) -> ParsecT e s m a
`catchError` e' -> ParsecT e s m a
h = (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall (m :: * -> *) s e a.
Monad m =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT ((State s e -> m (Reply e s a)) -> ParsecT e s m a)
-> (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall a b. (a -> b) -> a -> b
$ \State s e
s ->
    ParsecT e s m a -> State s e -> m (Reply e s a)
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 m (Reply e s a) -> (e' -> m (Reply e s a)) -> m (Reply e s a)
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` \e'
e ->
      ParsecT e s m a -> State s e -> m (Reply e s a)
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 :: Monad m => (State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT :: (State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT State s e -> m (Reply e s a)
k = (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
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 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)
-> (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
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' Hints (Token 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' Hints (Token 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 :: ParsecT e s m a
mzero = ParsecT e s m a
forall e s (m :: * -> *) a. ParsecT e s m a
pZero
  mplus :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
mplus = ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a
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 :: ParsecT e s m a
pZero = (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
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 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)
-> (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
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 (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o Maybe (ErrorItem (Token s))
forall a. Maybe a
Nothing Set (ErrorItem (Token s))
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 :: 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 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
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 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)
-> (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
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' ParseError s e -> ParseError s e -> ParseError s e
forall a. Semigroup a => a -> a -> a
<> ParseError s e
err) (State s e -> State s e -> State s e
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' (Int -> ParseError s e -> Hints (Token s)
forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (State s e -> Int
forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err Hints (Token s) -> Hints (Token s) -> Hints (Token s)
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' ParseError s e -> ParseError s e -> ParseError s e
forall a. Semigroup a => a -> a -> a
<> ParseError s e
err) (State s e -> State s e -> State s e
forall s e. State s e -> State s e -> State s e
longestMatch State s e
ms State s e
s')
         in ParsecT e s m a
-> 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
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 ParsecT e s m a
-> 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
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 :: 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 Int -> Int -> Ordering
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 :: (a -> ParsecT e s m a) -> ParsecT e s m a
mfix a -> ParsecT e s m a
f = (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall (m :: * -> *) s e a.
Monad m =>
(State s e -> m (Reply e s a)) -> ParsecT e s m a
mkPT ((State s e -> m (Reply e s a)) -> ParsecT e s m a)
-> (State s e -> m (Reply e s a)) -> ParsecT e s m a
forall a b. (a -> b) -> a -> b
$ \State s e
s -> (Reply e s a -> m (Reply e s a)) -> m (Reply e s a)
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix ((Reply e s a -> m (Reply e s a)) -> m (Reply e s a))
-> (Reply e s a -> m (Reply e s a)) -> m (Reply e s a)
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
_ -> String -> a
forall a. HasCallStack => String -> a
error String
"mfix ParsecT"
    ParsecT e s m a -> State s e -> m (Reply e s a)
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 MonadTrans (ParsecT e s) where
  lift :: m a -> ParsecT e s m a
lift m a
amb = (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
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 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)
-> (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
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 m a -> (a -> m b) -> m b
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 Hints (Token s)
forall a. Monoid a => a
mempty

instance (Ord e, Stream s) => MonadParsec e s (ParsecT e s m) where
  parseError :: ParseError s e -> ParsecT e s m a
parseError = ParseError s e -> ParsecT e s m a
forall s e (m :: * -> *) a. ParseError s e -> ParsecT e s m a
pParseError
  label :: String -> ParsecT e s m a -> ParsecT e s m a
label = String -> ParsecT e s m a -> ParsecT e s m a
forall e s (m :: * -> *) a.
String -> ParsecT e s m a -> ParsecT e s m a
pLabel
  try :: ParsecT e s m a -> ParsecT e s m a
try = ParsecT e s m a -> ParsecT e s m a
forall e s (m :: * -> *) a. ParsecT e s m a -> ParsecT e s m a
pTry
  lookAhead :: ParsecT e s m a -> ParsecT e s m a
lookAhead = ParsecT e s m a -> ParsecT e s m a
forall e s (m :: * -> *) a. ParsecT e s m a -> ParsecT e s m a
pLookAhead
  notFollowedBy :: ParsecT e s m a -> ParsecT e s m ()
notFollowedBy = ParsecT e s m a -> ParsecT e s m ()
forall s e (m :: * -> *) a.
Stream s =>
ParsecT e s m a -> ParsecT e s m ()
pNotFollowedBy
  withRecovery :: (ParseError s e -> ParsecT e s m a)
-> ParsecT e s m a -> ParsecT e s m a
withRecovery = (ParseError s e -> ParsecT e s m a)
-> ParsecT e s m a -> ParsecT e s m a
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 :: ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a)
observing = ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a)
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 = ParsecT e s m ()
forall e s (m :: * -> *). Stream s => ParsecT e s m ()
pEof
  token :: (Token s -> Maybe a)
-> Set (ErrorItem (Token s)) -> ParsecT e s m a
token = (Token s -> Maybe a)
-> Set (ErrorItem (Token s)) -> ParsecT e s m a
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 = (Tokens s -> Tokens s -> Bool)
-> Tokens s -> ParsecT e s m (Tokens s)
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 = Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
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 = Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
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 = Maybe String -> Int -> ParsecT e s m (Tokens s)
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 = ParsecT e s m (State s e)
forall e s (m :: * -> *). ParsecT e s m (State s e)
pGetParserState
  updateParserState :: (State s e -> State s e) -> ParsecT e s m ()
updateParserState = (State s e -> State s e) -> ParsecT e s m ()
forall s e (m :: * -> *).
(State s e -> State s e) -> ParsecT e s m ()
pUpdateParserState

pParseError ::
  ParseError s e ->
  ParsecT e s m a
pParseError :: ParseError s e -> ParsecT e s m a
pParseError ParseError s e
e = (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
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 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)
-> (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
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 :: String -> ParsecT e s m a -> ParsecT e s m a
pLabel String
l ParsecT e s m a
p = (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
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 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)
-> (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
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 = NonEmpty Char -> ErrorItem (Token s)
forall t. NonEmpty Char -> ErrorItem t
Label (NonEmpty Char -> ErrorItem (Token s))
-> Maybe (NonEmpty Char) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Maybe (NonEmpty Char)
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' (Hints (Token s) -> Maybe (ErrorItem (Token s)) -> Hints (Token s)
forall t. Hints t -> Maybe (ErrorItem t) -> Hints t
refreshLastHint Hints (Token s)
hs Maybe (ErrorItem (Token s))
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' (Hints (Token s) -> Maybe (ErrorItem (Token s)) -> Hints (Token s)
forall t. Hints t -> Maybe (ErrorItem t) -> Hints t
refreshLastHint 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 (ParseError s e -> State s e -> m b)
-> ParseError s e -> State s e -> m b
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))
_) ->
            Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
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))
-> (ErrorItem (Token s) -> Set (ErrorItem (Token s)))
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set (ErrorItem (Token s))
forall a. Set a
E.empty ErrorItem (Token s) -> Set (ErrorItem (Token s))
forall a. a -> Set a
E.singleton Maybe (ErrorItem (Token s))
el)
          ParseError s e
_ -> ParseError s e
err
   in ParsecT e s m a
-> 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
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 :: ParsecT e s m a -> ParsecT e s m a
pTry ParsecT e s m a
p = (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
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 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)
-> (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
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 ParsecT e s m a
-> 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
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 :: ParsecT e s m a -> ParsecT e s m a
pLookAhead :: ParsecT e s m a -> ParsecT e s m a
pLookAhead ParsecT e s m a
p = (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
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 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)
-> (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
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 Hints (Token s)
forall a. Monoid a => a
mempty
   in ParsecT e s m a
-> 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
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 :: ParsecT e s m a -> ParsecT e s m ()
pNotFollowedBy ParsecT e s m a
p = (forall b.
 State 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)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m ()
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 b.
  State 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)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m ())
-> (forall b.
    State 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)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m ()
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 = ErrorItem (Token s)
-> ((Token s, s) -> ErrorItem (Token s))
-> Maybe (Token s, s)
-> ErrorItem (Token s)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ErrorItem (Token s)
forall t. ErrorItem t
EndOfInput (NonEmpty (Token s) -> ErrorItem (Token s)
forall t. NonEmpty t -> ErrorItem t
Tokens (NonEmpty (Token s) -> ErrorItem (Token s))
-> ((Token s, s) -> NonEmpty (Token s))
-> (Token s, s)
-> ErrorItem (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Token s -> NonEmpty (Token s)
forall a. a -> NonEmpty a
nes (Token s -> NonEmpty (Token s))
-> ((Token s, s) -> Token s) -> (Token s, s) -> NonEmpty (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Token s, s) -> Token s
forall a b. (a, b) -> a
fst) (s -> Maybe (Token s, s)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input)
      unexpect :: ErrorItem (Token s) -> ParseError s e
unexpect ErrorItem (Token s)
u = Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o (ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem (Token s)
u) Set (ErrorItem (Token s))
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 Hints (Token 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 Hints (Token s)
forall a. Monoid a => a
mempty
   in ParsecT e s m a
-> 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
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 :: (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 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
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 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)
-> (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
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' Hints (Token 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' (Int -> ParseError s e -> Hints (Token s)
forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (State s e -> Int
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 ParsecT e s m a
-> 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
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' (Int -> ParseError s e -> Hints (Token s)
forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (State s e -> Int
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' (Int -> ParseError s e -> Hints (Token s)
forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (State s e -> Int
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 ParsecT e s m a
-> 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
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 ParsecT e s m a
-> 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
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 :: ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a)
pObserving ParsecT e s m a
p = (forall b.
 State s e
 -> (Either (ParseError s e) a
     -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> (Either (ParseError s e) a
     -> State s e -> Hints (Token s) -> m b)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m (Either (ParseError s e) a)
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 b.
  State s e
  -> (Either (ParseError s e) a
      -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> (Either (ParseError s e) a
      -> State s e -> Hints (Token s) -> m b)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m (Either (ParseError s e) a))
-> (forall b.
    State s e
    -> (Either (ParseError s e) a
        -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> (Either (ParseError s e) a
        -> State s e -> Hints (Token s) -> m b)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m (Either (ParseError s e) a)
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 (ParseError s e -> Either (ParseError s e) a
forall a b. a -> Either a b
Left ParseError s e
err) State s e
s' Hints (Token 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 (ParseError s e -> Either (ParseError s e) a
forall a b. a -> Either a b
Left ParseError s e
err) State s e
s' (Int -> ParseError s e -> Hints (Token s)
forall s e. Stream s => Int -> ParseError s e -> Hints (Token s)
toHints (State s e -> Int
forall s e. State s e -> Int
stateOffset State s e
s') ParseError s e
err)
   in ParsecT e s m a
-> 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
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 (Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b)
-> (a -> Either (ParseError s e) a)
-> a
-> State s e
-> Hints (Token s)
-> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either (ParseError s e) a
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 (Either (ParseError s e) a -> State s e -> Hints (Token s) -> m b)
-> (a -> Either (ParseError s e) a)
-> a
-> State s e
-> Hints (Token s)
-> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either (ParseError s e) a
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 :: ParsecT e s m ()
pEof = (forall b.
 State 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)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m ()
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 b.
  State 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)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m ())
-> (forall b.
    State 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)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m ()
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 s -> Maybe (Token s, s)
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 Hints (Token s)
forall a. Monoid a => a
mempty
    Just (Token s
x, s
_) ->
      let us :: Maybe (ErrorItem (Token s))
us = (ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ErrorItem (Token s) -> Maybe (ErrorItem (Token s)))
-> (Token s -> ErrorItem (Token s))
-> Token s
-> Maybe (ErrorItem (Token s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Token s) -> ErrorItem (Token s)
forall t. NonEmpty t -> ErrorItem t
Tokens (NonEmpty (Token s) -> ErrorItem (Token s))
-> (Token s -> NonEmpty (Token s))
-> Token s
-> ErrorItem (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Token s -> NonEmpty (Token s)
forall a. a -> NonEmpty a
nes) Token s
x
          ps :: Set (ErrorItem t)
ps = ErrorItem t -> Set (ErrorItem t)
forall a. a -> Set a
E.singleton ErrorItem t
forall t. ErrorItem t
EndOfInput
       in ParseError s e -> State s e -> m b
eerr
            (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
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))
forall t. Set (ErrorItem t)
ps)
            (s -> Int -> PosState s -> [ParseError s e] -> State s e
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 :: (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 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
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 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)
-> (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
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 s -> Maybe (Token s, s)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input of
    Maybe (Token s, s)
Nothing ->
      let us :: Maybe (ErrorItem t)
us = ErrorItem t -> Maybe (ErrorItem t)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem t
forall t. ErrorItem t
EndOfInput
       in ParseError s e -> State s e -> m b
eerr (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o Maybe (ErrorItem (Token s))
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 = (ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall a. a -> Maybe a
Just (ErrorItem (Token s) -> Maybe (ErrorItem (Token s)))
-> (Token s -> ErrorItem (Token s))
-> Token s
-> Maybe (ErrorItem (Token s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Token s) -> ErrorItem (Token s)
forall t. NonEmpty t -> ErrorItem t
Tokens (NonEmpty (Token s) -> ErrorItem (Token s))
-> (Token s -> NonEmpty (Token s))
-> Token s
-> ErrorItem (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Token s -> NonEmpty (Token s)
forall a. a -> NonEmpty a
nes) Token s
c
           in ParseError s e -> State s e -> m b
eerr
                (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
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)
                (s -> Int -> PosState s -> [ParseError s e] -> State s e
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 (s -> Int -> PosState s -> [ParseError s e] -> State s e
forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
cs (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) PosState s
pst [ParseError s e]
de) Hints (Token s)
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 :: (Tokens s -> Tokens s -> Bool)
-> Tokens s -> ParsecT e s m (Tokens s)
pTokens Tokens s -> Tokens s -> Bool
f Tokens s
tts = (forall b.
 State s e
 -> (Tokens s -> State s e -> Hints (Token s) -> m b)
 -> (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)
 -> m b)
-> ParsecT e s m (Tokens s)
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 b.
  State s e
  -> (Tokens s -> State s e -> Hints (Token s) -> m b)
  -> (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)
  -> m b)
 -> ParsecT e s m (Tokens s))
-> (forall b.
    State s e
    -> (Tokens s -> State s e -> Hints (Token s) -> m b)
    -> (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)
    -> m b)
-> ParsecT e s m (Tokens s)
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 = Proxy s
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 = ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem (Token s)
u
            ps :: Set (ErrorItem (Token s))
ps = (ErrorItem (Token s) -> Set (ErrorItem (Token s))
forall a. a -> Set a
E.singleton (ErrorItem (Token s) -> Set (ErrorItem (Token s)))
-> (Tokens s -> ErrorItem (Token s))
-> Tokens s
-> Set (ErrorItem (Token s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Token s) -> ErrorItem (Token s)
forall t. NonEmpty t -> ErrorItem t
Tokens (NonEmpty (Token s) -> ErrorItem (Token s))
-> (Tokens s -> NonEmpty (Token s))
-> Tokens s
-> ErrorItem (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Token s] -> NonEmpty (Token s)
forall a. [a] -> NonEmpty a
NE.fromList ([Token s] -> NonEmpty (Token s))
-> (Tokens s -> [Token s]) -> Tokens s -> NonEmpty (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy s -> Tokens s -> [Token s]
forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens Proxy s
pxy) Tokens s
tts
         in Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
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 = Proxy s -> Tokens s -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
tts
   in case Int -> s -> Maybe (Tokens s, s)
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 ErrorItem (Token s)
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 = s -> Int -> PosState s -> [ParseError s e] -> State s e
forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de
               in if Proxy s -> Tokens s -> Bool
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 Hints (Token s)
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 Hints (Token s)
forall a. Monoid a => a
mempty
            else
              let ps :: ErrorItem (Token s)
ps = (NonEmpty (Token s) -> ErrorItem (Token s)
forall t. NonEmpty t -> ErrorItem t
Tokens (NonEmpty (Token s) -> ErrorItem (Token s))
-> (Tokens s -> NonEmpty (Token s))
-> Tokens s
-> ErrorItem (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Token s] -> NonEmpty (Token s)
forall a. [a] -> NonEmpty a
NE.fromList ([Token s] -> NonEmpty (Token s))
-> (Tokens s -> [Token s]) -> Tokens s -> NonEmpty (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy s -> Tokens s -> [Token s]
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) (s -> Int -> PosState s -> [ParseError s e] -> State s e
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 :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
pTakeWhileP Maybe String
ml Token s -> Bool
f = (forall b.
 State s e
 -> (Tokens s -> State s e -> Hints (Token s) -> m b)
 -> (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)
 -> m b)
-> ParsecT e s m (Tokens s)
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 b.
  State s e
  -> (Tokens s -> State s e -> Hints (Token s) -> m b)
  -> (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)
  -> m b)
 -> ParsecT e s m (Tokens s))
-> (forall b.
    State s e
    -> (Tokens s -> State s e -> Hints (Token s) -> m b)
    -> (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)
    -> m b)
-> ParsecT e s m (Tokens s)
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 = Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s
      (Tokens s
ts, s
input') = (Token s -> Bool) -> s -> (Tokens s, s)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token s -> Bool
f s
input
      len :: Int
len = Proxy s -> Tokens s -> Int
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 Maybe String
-> (String -> Maybe (NonEmpty Char)) -> Maybe (NonEmpty Char)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe (NonEmpty Char)
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty of
          Maybe (NonEmpty Char)
Nothing -> Hints (Token s)
forall a. Monoid a => a
mempty
          Just NonEmpty Char
l -> ([Set (ErrorItem (Token s))] -> Hints (Token s)
forall t. [Set (ErrorItem t)] -> Hints t
Hints ([Set (ErrorItem (Token s))] -> Hints (Token s))
-> (NonEmpty Char -> [Set (ErrorItem (Token s))])
-> NonEmpty Char
-> Hints (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set (ErrorItem (Token s)) -> [Set (ErrorItem (Token s))]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set (ErrorItem (Token s)) -> [Set (ErrorItem (Token s))])
-> (NonEmpty Char -> Set (ErrorItem (Token s)))
-> NonEmpty Char
-> [Set (ErrorItem (Token s))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorItem (Token s) -> Set (ErrorItem (Token s))
forall a. a -> Set a
E.singleton (ErrorItem (Token s) -> Set (ErrorItem (Token s)))
-> (NonEmpty Char -> ErrorItem (Token s))
-> NonEmpty Char
-> Set (ErrorItem (Token s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty Char -> ErrorItem (Token s)
forall t. NonEmpty Char -> ErrorItem t
Label) NonEmpty Char
l
   in if Proxy s -> Tokens s -> Bool
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 (s -> Int -> PosState s -> [ParseError s e] -> State s e
forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o Int -> Int -> Int
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 (s -> Int -> PosState s -> [ParseError s e] -> State s e
forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o Int -> Int -> Int
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 :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s)
pTakeWhile1P Maybe String
ml Token s -> Bool
f = (forall b.
 State s e
 -> (Tokens s -> State s e -> Hints (Token s) -> m b)
 -> (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)
 -> m b)
-> ParsecT e s m (Tokens s)
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 b.
  State s e
  -> (Tokens s -> State s e -> Hints (Token s) -> m b)
  -> (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)
  -> m b)
 -> ParsecT e s m (Tokens s))
-> (forall b.
    State s e
    -> (Tokens s -> State s e -> Hints (Token s) -> m b)
    -> (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)
    -> m b)
-> ParsecT e s m (Tokens s)
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 = Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s
      (Tokens s
ts, s
input') = (Token s -> Bool) -> s -> (Tokens s, s)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token s -> Bool
f s
input
      len :: Int
len = Proxy s -> Tokens s -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
ts
      el :: Maybe (ErrorItem (Token s))
el = NonEmpty Char -> ErrorItem (Token s)
forall t. NonEmpty Char -> ErrorItem t
Label (NonEmpty Char -> ErrorItem (Token s))
-> Maybe (NonEmpty Char) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe String
ml Maybe String
-> (String -> Maybe (NonEmpty Char)) -> Maybe (NonEmpty Char)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe (NonEmpty Char)
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 -> Hints (Token s)
forall a. Monoid a => a
mempty
          Just ErrorItem (Token s)
l -> ([Set (ErrorItem (Token s))] -> Hints (Token s)
forall t. [Set (ErrorItem t)] -> Hints t
Hints ([Set (ErrorItem (Token s))] -> Hints (Token s))
-> (ErrorItem (Token s) -> [Set (ErrorItem (Token s))])
-> ErrorItem (Token s)
-> Hints (Token s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set (ErrorItem (Token s)) -> [Set (ErrorItem (Token s))]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set (ErrorItem (Token s)) -> [Set (ErrorItem (Token s))])
-> (ErrorItem (Token s) -> Set (ErrorItem (Token s)))
-> ErrorItem (Token s)
-> [Set (ErrorItem (Token s))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorItem (Token s) -> Set (ErrorItem (Token s))
forall a. a -> Set a
E.singleton) ErrorItem (Token s)
l
   in if Proxy s -> Tokens s -> Bool
forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty Proxy s
pxy Tokens s
ts
        then
          let us :: Maybe (ErrorItem (Token s))
us = ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ErrorItem (Token s) -> Maybe (ErrorItem (Token s)))
-> ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall a b. (a -> b) -> a -> b
$
                case s -> Maybe (Token s, s)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ s
input of
                  Maybe (Token s, s)
Nothing -> ErrorItem (Token s)
forall t. ErrorItem t
EndOfInput
                  Just (Token s
t, s
_) -> NonEmpty (Token s) -> ErrorItem (Token s)
forall t. NonEmpty t -> ErrorItem t
Tokens (Token s -> NonEmpty (Token s)
forall a. a -> NonEmpty a
nes Token s
t)
              ps :: Set (ErrorItem (Token s))
ps = Set (ErrorItem (Token s))
-> (ErrorItem (Token s) -> Set (ErrorItem (Token s)))
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set (ErrorItem (Token s))
forall a. Set a
E.empty ErrorItem (Token s) -> Set (ErrorItem (Token s))
forall a. a -> Set a
E.singleton Maybe (ErrorItem (Token s))
el
           in ParseError s e -> State s e -> m b
eerr
                (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
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)
                (s -> Int -> PosState s -> [ParseError s e] -> State s e
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 (s -> Int -> PosState s -> [ParseError s e] -> State s e
forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o Int -> Int -> Int
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 :: Maybe String -> Int -> ParsecT e s m (Tokens s)
pTakeP Maybe String
ml Int
n = (forall b.
 State s e
 -> (Tokens s -> State s e -> Hints (Token s) -> m b)
 -> (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)
 -> m b)
-> ParsecT e s m (Tokens s)
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 b.
  State s e
  -> (Tokens s -> State s e -> Hints (Token s) -> m b)
  -> (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)
  -> m b)
 -> ParsecT e s m (Tokens s))
-> (forall b.
    State s e
    -> (Tokens s -> State s e -> Hints (Token s) -> m b)
    -> (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)
    -> m b)
-> ParsecT e s m (Tokens s)
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 pxy :: Proxy s
pxy = Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s
      el :: Maybe (ErrorItem (Token s))
el = NonEmpty Char -> ErrorItem (Token s)
forall t. NonEmpty Char -> ErrorItem t
Label (NonEmpty Char -> ErrorItem (Token s))
-> Maybe (NonEmpty Char) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe String
ml Maybe String
-> (String -> Maybe (NonEmpty Char)) -> Maybe (NonEmpty Char)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe (NonEmpty Char)
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty)
      ps :: Set (ErrorItem (Token s))
ps = Set (ErrorItem (Token s))
-> (ErrorItem (Token s) -> Set (ErrorItem (Token s)))
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set (ErrorItem (Token s))
forall a. Set a
E.empty ErrorItem (Token s) -> Set (ErrorItem (Token s))
forall a. a -> Set a
E.singleton Maybe (ErrorItem (Token s))
el
   in case Int -> s -> Maybe (Tokens s, s)
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 (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError Int
o (ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem (Token s)
forall t. ErrorItem t
EndOfInput) Set (ErrorItem (Token s))
ps) State s e
s
        Just (Tokens s
ts, s
input') ->
          let len :: Int
len = Proxy s -> Tokens s -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
ts
           in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
n
                then
                  ParseError s e -> State s e -> m b
eerr
                    (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
forall s e.
Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
TrivialError (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len) (ErrorItem (Token s) -> Maybe (ErrorItem (Token s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ErrorItem (Token s)
forall t. ErrorItem t
EndOfInput) Set (ErrorItem (Token s))
ps)
                    (s -> Int -> PosState s -> [ParseError s e] -> State s e
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 (s -> Int -> PosState s -> [ParseError s e] -> State s e
forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State s
input' (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len) PosState s
pst [ParseError s e]
de) Hints (Token s)
forall a. Monoid a => a
mempty
{-# INLINE pTakeP #-}

pGetParserState :: ParsecT e s m (State s e)
pGetParserState :: ParsecT e s m (State s e)
pGetParserState = (forall b.
 State s e
 -> (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)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m (State s 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 b.
  State s e
  -> (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)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m (State s e))
-> (forall b.
    State s e
    -> (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)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m (State s e)
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 Hints (Token s)
forall a. Monoid a => a
mempty
{-# INLINE pGetParserState #-}

pUpdateParserState :: (State s e -> State s e) -> ParsecT e s m ()
pUpdateParserState :: (State s e -> State s e) -> ParsecT e s m ()
pUpdateParserState State s e -> State s e
f = (forall b.
 State 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)
 -> (ParseError s e -> State s e -> m b)
 -> m b)
-> ParsecT e s m ()
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 b.
  State 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)
  -> (ParseError s e -> State s e -> m b)
  -> m b)
 -> ParsecT e s m ())
-> (forall b.
    State 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)
    -> (ParseError s e -> State s e -> m b)
    -> m b)
-> ParsecT e s m ()
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) Hints (Token s)
forall a. Monoid a => a
mempty
{-# INLINE pUpdateParserState #-}

nes :: a -> NonEmpty a
nes :: a -> NonEmpty a
nes a
x = a
x a -> [a] -> NonEmpty a
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 :: 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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
errOffset
      then [Set (ErrorItem (Token s))] -> Hints (Token s)
forall t. [Set (ErrorItem t)] -> Hints t
Hints (if Set (ErrorItem (Token s)) -> Bool
forall a. Set a -> Bool
E.null Set (ErrorItem (Token s))
ps then [] else [Set (ErrorItem (Token s))
ps])
      else Hints (Token s)
forall a. Monoid a => a
mempty
  FancyError Int
_ Set (ErrorFancy e)
_ -> Hints (Token s)
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 :: 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 (Int
-> Maybe (ErrorItem (Token s))
-> Set (ErrorItem (Token s))
-> ParseError s e
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))] -> Set (ErrorItem (Token s))
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
E.unions (Set (ErrorItem (Token s))
ps Set (ErrorItem (Token s))
-> [Set (ErrorItem (Token s))] -> [Set (ErrorItem (Token s))]
forall a. a -> [a] -> [a]
: [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 ::
  -- | 'Hints' to add
  Hints t ->
  -- | An “OK” continuation to alter
  (a -> State s e -> Hints t -> m b) ->
  -- | Altered “OK” continuation
  (a -> State s e -> Hints t -> m b)
accHints :: Hints t
-> (a -> State s e -> Hints t -> m b)
-> a
-> State s e
-> Hints t
-> m b
accHints Hints t
hs1 a -> State s e -> Hints t -> m b
c a
x State s e
s Hints t
hs2 = a -> State s e -> Hints t -> m b
c a
x State s e
s (Hints t
hs1 Hints t -> Hints t -> Hints t
forall a. Semigroup a => a -> a -> a
<> Hints t
hs2)
{-# INLINE accHints #-}

-- | Replace the most recent group of hints (if any) with the given
-- 'ErrorItem' (or delete it if 'Nothing' is given). This is used in the
-- 'label' primitive.
refreshLastHint :: Hints t -> Maybe (ErrorItem t) -> Hints t
refreshLastHint :: Hints t -> Maybe (ErrorItem t) -> Hints t
refreshLastHint (Hints []) Maybe (ErrorItem t)
_ = [Set (ErrorItem t)] -> Hints t
forall t. [Set (ErrorItem t)] -> Hints t
Hints []
refreshLastHint (Hints (Set (ErrorItem t)
_ : [Set (ErrorItem t)]
xs)) Maybe (ErrorItem t)
Nothing = [Set (ErrorItem t)] -> Hints t
forall t. [Set (ErrorItem t)] -> Hints t
Hints [Set (ErrorItem t)]
xs
refreshLastHint (Hints (Set (ErrorItem t)
_ : [Set (ErrorItem t)]
xs)) (Just ErrorItem t
m) = [Set (ErrorItem t)] -> Hints t
forall t. [Set (ErrorItem t)] -> Hints t
Hints (ErrorItem t -> Set (ErrorItem t)
forall a. a -> Set a
E.singleton ErrorItem t
m Set (ErrorItem t) -> [Set (ErrorItem t)] -> [Set (ErrorItem t)]
forall a. a -> [a] -> [a]
: [Set (ErrorItem t)]
xs)
{-# INLINE refreshLastHint #-}

-- | 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 :: ParsecT e s m a -> State s e -> m (Reply e s a)
runParsecT ParsecT e s m a
p State s e
s = ParsecT e s m a
-> State s e
-> (a -> State s e -> Hints (Token s) -> m (Reply e s a))
-> (ParseError s e -> State s e -> m (Reply e s a))
-> (a -> State s e -> Hints (Token s) -> m (Reply e s a))
-> (ParseError s e -> State s e -> m (Reply e s a))
-> m (Reply e s a)
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 (Reply e s a)
forall (m :: * -> *) a s e p.
Monad m =>
a -> State s e -> p -> m (Reply e s a)
cok ParseError s e -> State s e -> m (Reply e s a)
forall (m :: * -> *) s e a.
Monad m =>
ParseError s e -> State s e -> m (Reply e s a)
cerr a -> State s e -> Hints (Token s) -> m (Reply e s a)
forall (m :: * -> *) a s e p.
Monad m =>
a -> State s e -> p -> m (Reply e s a)
eok ParseError s e -> State s e -> m (Reply e s a)
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
_ = Reply e s a -> m (Reply e s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Reply e s a -> m (Reply e s a)) -> Reply e s a -> m (Reply e s a)
forall a b. (a -> b) -> a -> b
$ State s e -> Consumption -> Result s e a -> Reply e s a
forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Consumed (a -> Result s e a
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' = Reply e s a -> m (Reply e s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Reply e s a -> m (Reply e s a)) -> Reply e s a -> m (Reply e s a)
forall a b. (a -> b) -> a -> b
$ State s e -> Consumption -> Result s e a -> Reply e s a
forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Consumed (ParseError s e -> Result s e a
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
_ = Reply e s a -> m (Reply e s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Reply e s a -> m (Reply e s a)) -> Reply e s a -> m (Reply e s a)
forall a b. (a -> b) -> a -> b
$ State s e -> Consumption -> Result s e a -> Reply e s a
forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Virgin (a -> Result s e a
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' = Reply e s a -> m (Reply e s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Reply e s a -> m (Reply e s a)) -> Reply e s a -> m (Reply e s a)
forall a b. (a -> b) -> a -> b
$ State s e -> Consumption -> Result s e a -> Reply e s a
forall e s a.
State s e -> Consumption -> Result s e a -> Reply e s a
Reply State s e
s' Consumption
Virgin (ParseError s e -> Result s e a
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 :: (e -> e') -> ParsecT e s m a -> ParsecT e' s m a
withParsecT e -> e'
f ParsecT e s m a
p =
  (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
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 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)
-> (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
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 =
                ((e -> e') -> ParseError s e -> ParseError s e'
forall e' e s.
Ord e' =>
(e -> e') -> ParseError s e -> ParseError s e'
mapParseError e -> e'
f (ParseError s e -> ParseError s e')
-> [ParseError s e] -> [ParseError s e']
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State s e -> [ParseError s e]
forall s e. State s e -> [ParseError s e]
stateParseErrors State s e
st)
                  [ParseError s e'] -> [ParseError s e'] -> [ParseError s e']
forall a. [a] -> [a] -> [a]
++ State s e' -> [ParseError s e']
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 ((e -> e') -> ParseError s e -> ParseError s e'
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 ((e -> e') -> ParseError s e -> ParseError s e'
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 ParsecT e s m a
-> 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
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 #-}