{-|
Module      : Data.Profunctor.Grammar.Parsector
Description : grammar distributor with failures
Copyright   : (C) 2026 - Eitan Chatav
License     : BSD-style (see the file LICENSE)
Maintainer  : Eitan Chatav <eitan.chatav@gmail.com>
Stability   : provisional
Portability : non-portable

See Leijen,
[Parsec: Direct Style Monadic Parser Combinators For The Real World]
(https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/parsec-paper-letter.pdf)
-}

module Data.Profunctor.Grammar.Parsector
  ( -- * Parsector
    Parsector (..)
  , parsecP
  , unparsecP
  , ParsecState (..)
  , ParsecFailure (..)
  ) where

import Control.Applicative
import Control.Arrow
import Control.Category
import Control.Lens
import Control.Lens.Grammar.BackusNaur
import Control.Lens.Grammar.Symbol
import Control.Lens.Grammar.Token
import Control.Lens.PartialIso
import Control.Monad
import Data.Monoid.Disjunctive
import Data.Profunctor
import Data.Profunctor.Unsafe ((#.), (.#))
import Data.Profunctor.Distributor
import Data.Profunctor.Monoidal.Cocartesian
import Data.Profunctor.Filtrator
import Data.Profunctor.Monoidal
import Data.Tree
import GHC.Exts
import Prelude hiding (id, (.))
import Witherable (Filterable (..))

{- | t'Parsector' is an invertible @LL(1)@ parser which is intended
to provide detailed failure information, based on [Parsec]
(https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/parsec-paper-letter.pdf).
-}
newtype Parsector s a b = Parsector
  {forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector :: forall x. (ParsecState s b -> x) -> ParsecState s a -> x}

{- | Run t'Parsector' as a parser: consume tokens from @s@,
left to right, returning a t'ParsecState' whose `parsecResult`
is `Nothing` on failure and `Just` the output syntax value on success. -}
parsecP
  :: Categorized (Item s)
  => Parsector s a b
  -> s -- ^ input stream
  -> ParsecState s b
parsecP :: forall s a b.
Categorized (Item s) =>
Parsector s a b -> s -> ParsecState s b
parsecP Parsector s a b
p s
s = Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p ParsecState s b -> ParsecState s b
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (Bool -> Word -> s -> ParsecFailure s -> Maybe a -> ParsecState s a
forall s a.
Bool -> Word -> s -> ParsecFailure s -> Maybe a -> ParsecState s a
ParsecState Bool
False Word
0 s
s ParsecFailure s
forall a. Monoid a => a
mempty Maybe a
forall a. Maybe a
Nothing)

{- | Run t'Parsector' as a printer: given a syntax value @a@ and
an input stream, append tokens to @s@ left to right,
returning a t'ParsecState' whose `parsecResult` is
`Nothing` on failure or `Just` a successful output syntax value,
in which case, `parsecStream` is the output stream. -}
unparsecP
  :: Categorized (Item s)
  => Parsector s a b
  -> a -- ^ input syntax
  -> s -- ^ input stream
  -> ParsecState s b
unparsecP :: forall s a b.
Categorized (Item s) =>
Parsector s a b -> a -> s -> ParsecState s b
unparsecP Parsector s a b
p a
a s
s = Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p ParsecState s b -> ParsecState s b
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (Bool -> Word -> s -> ParsecFailure s -> Maybe a -> ParsecState s a
forall s a.
Bool -> Word -> s -> ParsecFailure s -> Maybe a -> ParsecState s a
ParsecState Bool
False Word
0 s
s ParsecFailure s
forall a. Monoid a => a
mempty (a -> Maybe a
forall a. a -> Maybe a
Just a
a))

{- | t'ParsecState' is both the input and output type of the
underlying function inside t'Parsector'.
@Parsector s a b@ is equivalent to

@ParsecState s a -> ParsecState s b@

So t'ParsecState' has a dual interpretation as input and output. -}
data ParsecState s a = ParsecState
  { forall s a. ParsecState s a -> Bool
parsecLooked :: !Bool
    {- ^ `True` once the parser has consumed/produced at least one token
    since the last `<|>` / `try` decision point.
    Controls @LL(1)@ commitment: a failure with `parsecLooked` `True`
    is propagated immediately without trying alternatives.
    Reset to `False` by `try` on failure.
    -}
  , forall s a. ParsecState s a -> Word
parsecOffset :: !Word
    -- ^ Number of tokens consumed from the start of the stream.
  , forall s a. ParsecState s a -> s
parsecStream :: s -- ^ stream
  , forall s a. ParsecState s a -> ParsecFailure s
parsecFailure  :: ParsecFailure s
    {- ^ t'ParsecFailure' channel.

    * If `parsecResult` is `Nothing`, this is the hard failure.
    * If `parsecResult` is `Just`, this is deferred failure/hint info
      from empty-failing alternatives at the current position.

    `<|>` and `>>=` propagate and merge this field to preserve
    expected-token reporting on downstream failures.
    -}
  , forall s a. ParsecState s a -> Maybe a
parsecResult :: Maybe a
    {- ^
    As input, `Nothing` means parse mode and
    `Just` means print mode with an input syntax value.

    As output `Nothing` means failure (inspect `parsecFailure`) and
    `Just` means success with an output syntax value.
    -}
  }

{- | t'ParsecFailure' is the failure payload produced by t'Parsector',
stored in `parsecFailure`.
t'ParsecFailure' is a `Monoid` and t'Parsector' merges failures/hints
when control flow reaches the same offset without commitment.
-}
data ParsecFailure s = ParsecFailure
  { forall s. ParsecFailure s -> TokenClass (Item s)
parsecExpect :: TokenClass (Item s)
    {- ^ Class of expected token `Item`s at the `parsecOffset`.
    `tokenClass`es and `Tokenized` combinators specify expectations.
    Under `<>`, expectations are combined with disjunction `>|<`.
    In case of a parse failure, contrast with the actual `parsecStream`,
    which is either unexpectedly empty or begins with an unexpected token.
    -}
  , forall s. ParsecFailure s -> [Tree String]
parsecLabels :: [Tree String]
    {- ^ Forest of `rule` labels active at the `parsecOffset`.
    Each `rule` wraps its inner labels in a new `Node`.
    `ruleRec` & `fail` also create label nodes.
    Under `<>`, forests are concatenated as siblings.
    Use `drawForest` to display.
    -}
  }

-- ParsecFailure instances
deriving stock instance
  ( Categorized (Item s)
  , Show (Item s), Show (Categorize (Item s))
  ) => Show (ParsecFailure s)
deriving stock instance
  ( Categorized (Item s)
  , Read (Item s), Read (Categorize (Item s))
  ) => Read (ParsecFailure s)
deriving stock instance Categorized (Item s) => Eq (ParsecFailure s)
deriving stock instance Categorized (Item s) => Ord (ParsecFailure s)
instance Categorized (Item s) => Semigroup (ParsecFailure s) where
  ParsecFailure TokenClass (Item s)
e1 [Tree String]
l1 <> :: ParsecFailure s -> ParsecFailure s -> ParsecFailure s
<> ParsecFailure TokenClass (Item s)
e2 [Tree String]
l2 = TokenClass (Item s) -> [Tree String] -> ParsecFailure s
forall s. TokenClass (Item s) -> [Tree String] -> ParsecFailure s
ParsecFailure (TokenClass (Item s)
e1 TokenClass (Item s) -> TokenClass (Item s) -> TokenClass (Item s)
forall a. DisjunctiveMonoid a => a -> a -> a
>|< TokenClass (Item s)
e2) ([Tree String]
l1 [Tree String] -> [Tree String] -> [Tree String]
forall a. [a] -> [a] -> [a]
++ [Tree String]
l2)
instance Categorized (Item s) => Monoid (ParsecFailure s) where
  mempty :: ParsecFailure s
mempty = TokenClass (Item s) -> [Tree String] -> ParsecFailure s
forall s. TokenClass (Item s) -> [Tree String] -> ParsecFailure s
ParsecFailure TokenClass (Item s)
forall a. DisjunctiveMonoid a => a
falseP []

-- ParsecState instances
deriving stock instance Functor (ParsecState s)
deriving stock instance Foldable (ParsecState s)
deriving stock instance Traversable (ParsecState s)
deriving stock instance
  ( Categorized (Item s)
  , Show (Item s), Show (Categorize (Item s))
  , Show a, Show s
  ) => Show (ParsecState s a)
deriving stock instance
  ( Categorized (Item s)
  , Read (Item s), Read (Categorize (Item s))
  , Read a, Read s
  ) => Read (ParsecState s a)
deriving stock instance
  ( Categorized (Item s)
  , Eq a, Eq s
  ) => Eq (ParsecState s a)
deriving stock instance
  ( Categorized (Item s)
  , Ord a, Ord s
  ) => Ord (ParsecState s a)

-- Parsector instances
instance
  ( Categorized token, Item s ~ token
  , Cons s s token token, Snoc s s token token
  ) => Tokenized token (Parsector s token token) where
  anyToken :: Parsector s token token
anyToken = TokenClass token -> Parsector s token token
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass token
forall token p. Tokenized token p => p
anyToken
  token :: token -> Parsector s token token
token token
t = TokenClass token -> Parsector s token token
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (token -> TokenClass token
forall token p. Tokenized token p => token -> p
token token
t)
  oneOf :: forall (f :: * -> *).
Foldable f =>
f token -> Parsector s token token
oneOf f token
ts = TokenClass token -> Parsector s token token
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (f token -> TokenClass token
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f token -> TokenClass token
oneOf f token
ts)
  notOneOf :: forall (f :: * -> *).
Foldable f =>
f token -> Parsector s token token
notOneOf f token
ts = TokenClass token -> Parsector s token token
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (f token -> TokenClass token
forall token p (f :: * -> *).
(Tokenized token p, Foldable f) =>
f token -> p
forall (f :: * -> *). Foldable f => f token -> TokenClass token
notOneOf f token
ts)
  asIn :: Categorize token -> Parsector s token token
asIn Categorize token
cat = TokenClass token -> Parsector s token token
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Categorize token -> TokenClass token
forall token p. Tokenized token p => Categorize token -> p
asIn Categorize token
cat)
  notAsIn :: Categorize token -> Parsector s token token
notAsIn Categorize token
cat = TokenClass token -> Parsector s token token
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass (Categorize token -> TokenClass token
forall token p. Tokenized token p => Categorize token -> p
notAsIn Categorize token
cat)
instance
  ( Categorized token, Item s ~ token
  , Cons s s token token, Snoc s s token token
  ) => TokenAlgebra token (Parsector s token token) where
    {-# SPECIALISE instance TokenAlgebra Char (Parsector String Char Char) #-}
    -- The test & failures are made once, rather than for each token.
    tokenClass :: TokenClass token -> Parsector s token token
tokenClass TokenClass token
test = token -> Bool
matches (token -> Bool)
-> Parsector s token token -> Parsector s token token
forall a b. a -> b -> b
`seq` (forall x. (ParsecState s token -> x) -> ParsecState s token -> x)
-> Parsector s token token
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector (\ParsecState s token -> x
callback ParsecState s token
query ->
      let
        stream :: s
stream = ParsecState s token -> s
forall s a. ParsecState s a -> s
parsecStream ParsecState s token
query
        mode :: Maybe token
mode = ParsecState s token -> Maybe token
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s token
query
        offset :: Word
offset = ParsecState s token -> Word
forall s a. ParsecState s a -> Word
parsecOffset ParsecState s token
query
        replyOk :: token -> s -> ParsecState s token
replyOk token
tok s
str = ParsecState s token
query
          { parsecLooked = True
          , parsecFailure  = noFailure
          , parsecStream = str
          , parsecOffset = offset + 1
          , parsecResult = Just tok
          }
        replyErr :: ParsecState s token
replyErr = ParsecState s token
query
          { parsecFailure  = failure
          , parsecResult = Nothing }
      in
        ParsecState s token -> x
callback (ParsecState s token -> x) -> ParsecState s token -> x
forall a b. (a -> b) -> a -> b
$ case Maybe token
mode of
          -- print mode
          Just token
tok
            | token -> Bool
matches token
tok -> token -> s -> ParsecState s token
replyOk token
tok (s -> token -> s
forall s a. Snoc s s a a => s -> a -> s
snoc s
stream token
tok)
            | Bool
otherwise -> ParsecState s token
replyErr
          -- parse mode
          Maybe token
Nothing -> case s -> Maybe (token, s)
forall s a. Cons s s a a => s -> Maybe (a, s)
uncons s
stream of
            Just (token
tok, s
rest)
              | token -> Bool
matches token
tok -> token -> s -> ParsecState s token
replyOk token
tok s
rest
              | Bool
otherwise -> ParsecState s token
replyErr
            Maybe (token, s)
Nothing -> ParsecState s token
replyErr)
      where
        matches :: token -> Bool
        matches :: token -> Bool
matches = TokenClass token -> token -> Bool
forall token p. TokenAlgebra token p => TokenClass token -> p
tokenClass TokenClass token
test
        noFailure :: ParsecFailure s
noFailure = ParsecFailure s
forall a. Monoid a => a
mempty
        failure :: ParsecFailure s
failure = TokenClass (Item s) -> [Tree String] -> ParsecFailure s
forall s. TokenClass (Item s) -> [Tree String] -> ParsecFailure s
ParsecFailure TokenClass token
TokenClass (Item s)
test []
instance BackusNaurForm (Parsector s a b)
instance Ruled (Parsector s a b) where
  -- | Wraps inner `parsecLabels` in a new `Node name` on failure.
  -- Has no effect on success.
  rule :: String -> Parsector s a b -> Parsector s a b
rule String
name Parsector s a b
p = (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
 -> Parsector s a b)
-> (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall a b. (a -> b) -> a -> b
$ \ParsecState s b -> x
callback ParsecState s a
query ->
    ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s b -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p) ParsecState s a
query ((ParsecState s b -> x) -> x) -> (ParsecState s b -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
reply -> ParsecState s b -> x
callback (ParsecState s b -> x) -> ParsecState s b -> x
forall a b. (a -> b) -> a -> b
$
      case ParsecState s b -> Maybe b
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s b
reply of
        Maybe b
Nothing -> ParsecState s b
reply
          { parsecFailure =
              let ParsecFailure expect labels = parsecFailure reply
              in ParsecFailure expect [Node name labels]
          }
        Just b
_ -> ParsecState s b
reply
instance
  ( Categorized token, Item s ~ token
  , Cons s s token token, Snoc s s token token
  ) => TerminalSymbol token (Parsector s () ()) where
    {-# SPECIALISE instance TerminalSymbol Char (Parsector String () ()) #-}
    {- | Like @only str ?< tokens str@, checking its tokens in one loop,
    failing without consuming at the first token, and committed after.
    -}
    terminal :: [token] -> Parsector s () ()
terminal [token]
str = (forall x. (ParsecState s () -> x) -> ParsecState s () -> x)
-> Parsector s () ()
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s () -> x) -> ParsecState s () -> x)
 -> Parsector s () ())
-> (forall x. (ParsecState s () -> x) -> ParsecState s () -> x)
-> Parsector s () ()
forall a b. (a -> b) -> a -> b
$ \ParsecState s () -> x
callback ParsecState s ()
query -> ParsecState s () -> x
callback (ParsecState s () -> x) -> ParsecState s () -> x
forall a b. (a -> b) -> a -> b
$
      case [token]
str of
        [] -> ParsecState s ()
query {parsecResult = Just ()}
        [token]
_ -> [Item s] -> ParsecState s () -> ParsecState s ()
forall {s}.
(Categorized (Item s), Snoc s s (Item s) (Item s),
 Cons s s (Item s) (Item s)) =>
[Item s] -> ParsecState s () -> ParsecState s ()
go [token]
[Item s]
str ParsecState s ()
query
      where
        go :: [Item s] -> ParsecState s () -> ParsecState s ()
go [] ParsecState s ()
state = ParsecState s ()
state {parsecResult = Just ()}
        go (Item s
tok : [Item s]
rest) ParsecState s ()
state =
          let
            next :: s -> ParsecState s ()
next s
str' = ParsecState s ()
state
              { parsecLooked = True
              , parsecFailure = mempty
              , parsecStream = str'
              , parsecOffset = parsecOffset state + 1
              }
            failure :: ParsecState s ()
failure = ParsecState s ()
state
              { parsecFailure = ParsecFailure (token tok) []
              , parsecResult = Nothing
              }
          in
            case ParsecState s () -> Maybe ()
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s ()
state of
              Just () -> [Item s] -> ParsecState s () -> ParsecState s ()
go [Item s]
rest (s -> ParsecState s ()
next (s -> Item s -> s
forall s a. Snoc s s a a => s -> a -> s
snoc (ParsecState s () -> s
forall s a. ParsecState s a -> s
parsecStream ParsecState s ()
state) Item s
tok))
              Maybe ()
Nothing -> case s -> Maybe (Item s, s)
forall s a. Cons s s a a => s -> Maybe (a, s)
uncons (ParsecState s () -> s
forall s a. ParsecState s a -> s
parsecStream ParsecState s ()
state) of
                Just (Item s
tok', s
str') | Item s
tok' Item s -> Item s -> Bool
forall a. Eq a => a -> a -> Bool
== Item s
tok -> [Item s] -> ParsecState s () -> ParsecState s ()
go [Item s]
rest (s -> ParsecState s ()
next s
str')
                Maybe (Item s, s)
_ -> ParsecState s ()
failure
instance
  ( Item s ~ Char
  , Cons s s Char Char, Snoc s s Char Char
  , x ~ (), y ~ ()
  ) => IsString (Parsector s x y) where
  fromString :: String -> Parsector s x y
fromString = String -> Parsector s x y
forall token s. TerminalSymbol token s => [token] -> s
terminal
instance Functor (Parsector s a) where
  fmap :: forall a b. (a -> b) -> Parsector s a a -> Parsector s a b
fmap = (a -> b) -> Parsector s a a -> Parsector s a b
forall b c a. (b -> c) -> Parsector s a b -> Parsector s a c
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap
instance Categorized (Item s) => Applicative (Parsector s a) where
  pure :: forall a. a -> Parsector s a a
pure a
b = (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
 -> Parsector s a a)
-> (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall a b. (a -> b) -> a -> b
$ \ParsecState s a -> x
callback ParsecState s a
query ->
    ParsecState s a -> x
callback ParsecState s a
query { parsecResult = Just b }
  <*> :: forall a b.
Parsector s a (a -> b) -> Parsector s a a -> Parsector s a b
(<*>) = ((a -> b) -> a -> b)
-> Parsector s a (a -> b) -> Parsector s a a -> Parsector s a b
forall a b c.
(a -> b -> c)
-> Parsector s a a -> Parsector s a b -> Parsector s a c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (a -> b) -> a -> b
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  -- | Like `>>=`, without a continuation of the first result.
  liftA2 :: forall a b c.
(a -> b -> c)
-> Parsector s a a -> Parsector s a b -> Parsector s a c
liftA2 a -> b -> c
f Parsector s a a
p Parsector s a b
q = (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
 -> Parsector s a c)
-> (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall a b. (a -> b) -> a -> b
$ \ParsecState s c -> x
callback ParsecState s a
query ->
    ((ParsecState s a -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s a -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a a
-> forall x. (ParsecState s a -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a a
p) ParsecState s a
query ((ParsecState s a -> x) -> x) -> (ParsecState s a -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s a
reply ->
      case ParsecState s a -> Maybe a
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s a
reply of
        Maybe a
Nothing -> ParsecState s c -> x
callback ParsecState s a
reply { parsecResult = Nothing }
        Just a
b ->
          ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s b -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
q) (ParsecState s a -> ParsecState s a -> ParsecState s a
forall s a b.
Categorized (Item s) =>
ParsecState s a -> ParsecState s b -> ParsecState s a
bound ParsecState s a
query ParsecState s a
reply) ((ParsecState s b -> x) -> x) -> (ParsecState s b -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
qReply ->
            ParsecState s c -> x
callback (ParsecState s a -> ParsecState s c -> ParsecState s c
forall s b c.
Categorized (Item s) =>
ParsecState s b -> ParsecState s c -> ParsecState s c
rebound ParsecState s a
reply (a -> b -> c
f a
b (b -> c) -> ParsecState s b -> ParsecState s c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecState s b
qReply))

instance Categorized (Item s) => Monad (Parsector s a) where
  return :: forall a. a -> Parsector s a a
return = a -> Parsector s a a
forall a. a -> Parsector s a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Parsector s a a
p >>= :: forall a b.
Parsector s a a -> (a -> Parsector s a b) -> Parsector s a b
>>= a -> Parsector s a b
f = (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
 -> Parsector s a b)
-> (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall a b. (a -> b) -> a -> b
$ \ParsecState s b -> x
callback ParsecState s a
query ->
    ((ParsecState s a -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s a -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a a
-> forall x. (ParsecState s a -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a a
p) ParsecState s a
query ((ParsecState s a -> x) -> x) -> (ParsecState s a -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s a
reply ->
      case ParsecState s a -> Maybe a
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s a
reply of
        Maybe a
Nothing -> ParsecState s b -> x
callback ParsecState s a
reply { parsecResult = Nothing }
        Just a
b ->
          ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s b -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector (a -> Parsector s a b
f a
b)) (ParsecState s a -> ParsecState s a -> ParsecState s a
forall s a b.
Categorized (Item s) =>
ParsecState s a -> ParsecState s b -> ParsecState s a
bound ParsecState s a
query ParsecState s a
reply) ((ParsecState s b -> x) -> x) -> (ParsecState s b -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
fReply ->
            ParsecState s b -> x
callback (ParsecState s a -> ParsecState s b -> ParsecState s b
forall s b c.
Categorized (Item s) =>
ParsecState s b -> ParsecState s c -> ParsecState s c
rebound ParsecState s a
reply ParsecState s b
fReply)

-- | The query of the second of a sequence, after the first's reply.
bound
  :: Categorized (Item s)
  => ParsecState s a -> ParsecState s b -> ParsecState s a
bound :: forall s a b.
Categorized (Item s) =>
ParsecState s a -> ParsecState s b -> ParsecState s a
bound ParsecState s a
query ParsecState s b
reply = ParsecState s b
reply
  { parsecLooked = False
  , parsecFailure = mempty
  , parsecResult = parsecResult query
  }

{- | The reply of a sequence, from the first's & the second's replies,
which keeps the first's hints when the second hasn't consumed.
-}
rebound
  :: Categorized (Item s)
  => ParsecState s b -> ParsecState s c -> ParsecState s c
rebound :: forall s b c.
Categorized (Item s) =>
ParsecState s b -> ParsecState s c -> ParsecState s c
rebound ParsecState s b
reply ParsecState s c
next
  | ParsecState s c -> Bool
forall s a. ParsecState s a -> Bool
parsecLooked ParsecState s c
next = ParsecState s c
next
  | Bool
otherwise = ParsecState s c
next
      { parsecLooked = parsecLooked reply
      , parsecFailure = parsecFailure reply <> parsecFailure next
      }

instance Categorized (Item s) => Alternative (Parsector s a) where
  -- | Always fails without consuming input; expects nothing.
  empty :: forall a. Parsector s a a
empty = (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
 -> Parsector s a a)
-> (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall a b. (a -> b) -> a -> b
$ \ParsecState s a -> x
callback ParsecState s a
query ->
    ParsecState s a -> x
callback ParsecState s a
query { parsecFailure = mempty, parsecResult = Nothing }
  Parsector s a a
p <|> :: forall a. Parsector s a a -> Parsector s a a -> Parsector s a a
<|> Parsector s a a
q = (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
 -> Parsector s a a)
-> (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall a b. (a -> b) -> a -> b
$ \ParsecState s a -> x
callback ParsecState s a
query ->
    ((ParsecState s a -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s a -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a a
-> forall x. (ParsecState s a -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a a
p) ParsecState s a
query ((ParsecState s a -> x) -> x) -> (ParsecState s a -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s a
replyP -> ParsecState s a -> x
callback (ParsecState s a -> x) -> ParsecState s a -> x
forall a b. (a -> b) -> a -> b
$
      case ParsecState s a -> Maybe a
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s a
replyP of
        -- if p succeeds, take p's branch
        Just a
_ -> ParsecState s a
replyP
        -- if p failed after consuming (committed), propagate immediately
        Maybe a
Nothing | ParsecState s a -> Bool
forall s a. ParsecState s a -> Bool
parsecLooked ParsecState s a
replyP -> ParsecState s a
replyP
        -- if p failed without consuming, try q
        Maybe a
Nothing ->
          let errP :: ParsecFailure s
errP = ParsecState s a -> ParsecFailure s
forall s a. ParsecState s a -> ParsecFailure s
parsecFailure ParsecState s a
replyP
          in ((ParsecState s a -> ParsecState s a)
 -> ParsecState s a -> ParsecState s a)
-> ParsecState s a
-> (ParsecState s a -> ParsecState s a)
-> ParsecState s a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a a
-> forall x. (ParsecState s a -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a a
q) ParsecState s a
query ((ParsecState s a -> ParsecState s a) -> ParsecState s a)
-> (ParsecState s a -> ParsecState s a) -> ParsecState s a
forall a b. (a -> b) -> a -> b
$ \ParsecState s a
replyQ ->
          case (ParsecState s a -> Bool
forall s a. ParsecState s a -> Bool
parsecLooked ParsecState s a
replyQ, ParsecState s a -> Maybe a
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s a
replyQ) of
            -- q consumed (ok or err): propagate as-is, drop errP
            (Bool
True, Maybe a
_)         -> ParsecState s a
replyQ
            -- q empty ok: carry errP forward as hint for downstream
            (Bool
False, Just a
_)   -> ParsecState s a
replyQ { parsecFailure = errP <> parsecFailure replyQ }
            -- both empty fail: merge failures
            (Bool
False, Maybe a
Nothing)  -> ParsecState s a
replyP { parsecFailure = errP <> parsecFailure replyQ }
instance Categorized (Item s) => MonadPlus (Parsector s a)
instance Categorized (Item s) => MonadFail (Parsector s a) where
  fail :: forall a. String -> Parsector s a a
fail String
msg = String -> Parsector s a a -> Parsector s a a
forall p. Ruled p => String -> p -> p
rule String
msg Parsector s a a
forall a. Parsector s a a
forall (f :: * -> *) a. Alternative f => f a
empty
instance Tried (Parsector s a b) where
  -- | On failure, resets `parsecLooked` to @False@, allowing
  -- the enclosing `<|>` to try the next alternative even if @p@
  -- consumed input. Also restores the stream/offset decision state.
  -- Has no effect on success.
  try :: Parsector s a b -> Parsector s a b
try Parsector s a b
p = (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
 -> Parsector s a b)
-> (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall a b. (a -> b) -> a -> b
$ \ParsecState s b -> x
callback ParsecState s a
query ->
    ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s b -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p) ParsecState s a
query ((ParsecState s b -> x) -> x) -> (ParsecState s b -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
reply -> ParsecState s b -> x
callback (ParsecState s b -> x) -> ParsecState s b -> x
forall a b. (a -> b) -> a -> b
$
      case ParsecState s b -> Maybe b
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s b
reply of
        Maybe b
Nothing -> ParsecState s a
query
          { parsecLooked = False
          , parsecFailure  = parsecFailure reply
          , parsecResult = Nothing
          }
        Just b
_ -> ParsecState s b
reply
instance Categorized (Item s) => Filterable (Parsector s a) where
  mapMaybe :: forall a b. (a -> Maybe b) -> Parsector s a a -> Parsector s a b
mapMaybe = (a -> Maybe a)
-> (a -> Maybe b) -> Parsector s a a -> Parsector s a b
forall (p :: * -> * -> *) s a b t.
(Choice p, Cochoice p) =>
(s -> Maybe a) -> (b -> Maybe t) -> p a b -> p s t
dimapMaybe a -> Maybe a
forall a. a -> Maybe a
Just
instance Category (Parsector s) where
  id :: forall a. Parsector s a a
id = (forall x. (ParsecState s a -> x) -> ParsecState s a -> x)
-> Parsector s a a
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector (ParsecState s a -> x) -> ParsecState s a -> x
forall a. a -> a
forall x. (ParsecState s a -> x) -> ParsecState s a -> x
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  Parsector forall x. (ParsecState s c -> x) -> ParsecState s b -> x
q . :: forall b c a. Parsector s b c -> Parsector s a b -> Parsector s a c
. Parsector forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p = (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ((ParsecState s c -> x) -> ParsecState s b -> x)
-> (ParsecState s c -> x)
-> ParsecState s a
-> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ParsecState s c -> x) -> ParsecState s b -> x
forall x. (ParsecState s c -> x) -> ParsecState s b -> x
q)
instance Categorized (Item s) => Arrow (Parsector s) where
  arr :: forall b c. (b -> c) -> Parsector s b c
arr b -> c
f = (forall x. (ParsecState s c -> x) -> ParsecState s b -> x)
-> Parsector s b c
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s c -> x) -> ParsecState s b -> x)
 -> Parsector s b c)
-> (forall x. (ParsecState s c -> x) -> ParsecState s b -> x)
-> Parsector s b c
forall a b. (a -> b) -> a -> b
$ \ParsecState s c -> x
callback ParsecState s b
reply -> ParsecState s c -> x
callback (b -> c
f (b -> c) -> ParsecState s b -> ParsecState s c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecState s b
reply)
  *** :: forall b c b' c'.
Parsector s b c -> Parsector s b' c' -> Parsector s (b, b') (c, c')
(***) = Parsector s b c -> Parsector s b' c' -> Parsector s (b, b') (c, c')
forall (p :: * -> * -> *) a b c d.
Monoidal p =>
p a b -> p c d -> p (a, c) (b, d)
(>*<)
  first :: forall b c d. Parsector s b c -> Parsector s (b, d) (c, d)
first = Parsector s b c -> Parsector s (b, d) (c, d)
forall b c d. Parsector s b c -> Parsector s (b, d) (c, d)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (a, c) (b, c)
first'
  second :: forall b c d. Parsector s b c -> Parsector s (d, b) (d, c)
second = Parsector s b c -> Parsector s (d, b) (d, c)
forall b c d. Parsector s b c -> Parsector s (d, b) (d, c)
forall (p :: * -> * -> *) a b c.
Strong p =>
p a b -> p (c, a) (c, b)
second'
instance Categorized (Item s) => ArrowZero (Parsector s) where
  zeroArrow :: forall b c. Parsector s b c
zeroArrow = Parsector s b c
forall a. Parsector s b a
forall (f :: * -> *) a. Alternative f => f a
empty
instance Categorized (Item s) => ArrowPlus (Parsector s) where
  <+> :: forall b c. Parsector s b c -> Parsector s b c -> Parsector s b c
(<+>) = Parsector s b c -> Parsector s b c -> Parsector s b c
forall a. Parsector s b a -> Parsector s b a -> Parsector s b a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
instance Categorized (Item s) => ArrowChoice (Parsector s) where
  +++ :: forall b c b' c'.
Parsector s b c
-> Parsector s b' c' -> Parsector s (Either b b') (Either c c')
(+++) = Parsector s b c
-> Parsector s b' c' -> Parsector s (Either b b') (Either c c')
forall b c b' c'.
Parsector s b c
-> Parsector s b' c' -> Parsector s (Either b b') (Either c c')
forall (p :: * -> * -> *) a b c d.
CocartesianMonoidal p =>
p a b -> p c d -> p (Either a c) (Either b d)
(>+<)
  left :: forall b c d.
Parsector s b c -> Parsector s (Either b d) (Either c d)
left = Parsector s b c -> Parsector s (Either b d) (Either c d)
forall b c d.
Parsector s b c -> Parsector s (Either b d) (Either c d)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either a c) (Either b c)
left'
  right :: forall b c d.
Parsector s b c -> Parsector s (Either d b) (Either d c)
right = Parsector s b c -> Parsector s (Either d b) (Either d c)
forall b c d.
Parsector s b c -> Parsector s (Either d b) (Either d c)
forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right'
instance Profunctor (Parsector s) where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Parsector s b c -> Parsector s a d
dimap a -> b
f c -> d
g (Parsector forall x. (ParsecState s c -> x) -> ParsecState s b -> x
p) = (forall x. (ParsecState s d -> x) -> ParsecState s a -> x)
-> Parsector s a d
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s d -> x) -> ParsecState s a -> x)
 -> Parsector s a d)
-> (forall x. (ParsecState s d -> x) -> ParsecState s a -> x)
-> Parsector s a d
forall a b. (a -> b) -> a -> b
$ \ParsecState s d -> x
callback ParsecState s a
query ->
    (ParsecState s c -> x) -> ParsecState s b -> x
forall x. (ParsecState s c -> x) -> ParsecState s b -> x
p (ParsecState s d -> x
callback (ParsecState s d -> x)
-> (ParsecState s c -> ParsecState s d) -> ParsecState s c -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (c -> d) -> ParsecState s c -> ParsecState s d
forall a b. (a -> b) -> ParsecState s a -> ParsecState s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> d
g) ((a -> b) -> ParsecState s a -> ParsecState s b
forall a b. (a -> b) -> ParsecState s a -> ParsecState s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ParsecState s a
query)
  lmap :: forall a b c. (a -> b) -> Parsector s b c -> Parsector s a c
lmap a -> b
f (Parsector forall x. (ParsecState s c -> x) -> ParsecState s b -> x
p) = (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
 -> Parsector s a c)
-> (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall a b. (a -> b) -> a -> b
$ \ParsecState s c -> x
callback ParsecState s a
query ->
    (ParsecState s c -> x) -> ParsecState s b -> x
forall x. (ParsecState s c -> x) -> ParsecState s b -> x
p ParsecState s c -> x
callback ((a -> b) -> ParsecState s a -> ParsecState s b
forall a b. (a -> b) -> ParsecState s a -> ParsecState s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ParsecState s a
query)
  rmap :: forall b c a. (b -> c) -> Parsector s a b -> Parsector s a c
rmap b -> c
g (Parsector forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p) = (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
 -> Parsector s a c)
-> (forall x. (ParsecState s c -> x) -> ParsecState s a -> x)
-> Parsector s a c
forall a b. (a -> b) -> a -> b
$ \ParsecState s c -> x
callback -> (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p (ParsecState s c -> x
callback (ParsecState s c -> x)
-> (ParsecState s b -> ParsecState s c) -> ParsecState s b -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (b -> c) -> ParsecState s b -> ParsecState s c
forall a b. (a -> b) -> ParsecState s a -> ParsecState s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
g)
  #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> Parsector s a b -> Parsector s a c
(#.) q b c
_ = Parsector s a b -> Parsector s a c
forall a b. Coercible a b => a -> b
coerce
  Parsector s b c
p .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
Parsector s b c -> q a b -> Parsector s a c
.# q a b
_ = Parsector s b c -> Parsector s a c
forall a b. Coercible a b => a -> b
coerce Parsector s b c
p
instance Strong (Parsector s) where
  first' :: forall a b c. Parsector s a b -> Parsector s (a, c) (b, c)
first' Parsector s a b
p = (forall x.
 (ParsecState s (b, c) -> x) -> ParsecState s (a, c) -> x)
-> Parsector s (a, c) (b, c)
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x.
  (ParsecState s (b, c) -> x) -> ParsecState s (a, c) -> x)
 -> Parsector s (a, c) (b, c))
-> (forall x.
    (ParsecState s (b, c) -> x) -> ParsecState s (a, c) -> x)
-> Parsector s (a, c) (b, c)
forall a b. (a -> b) -> a -> b
$ \ParsecState s (b, c) -> x
callback ParsecState s (a, c)
reply0 ->
    ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s b -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p) ((a, c) -> a
forall a b. (a, b) -> a
fst ((a, c) -> a) -> ParsecState s (a, c) -> ParsecState s a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecState s (a, c)
reply0) ((ParsecState s b -> x) -> x) -> (ParsecState s b -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
reply1 ->
      ParsecState s (b, c) -> x
callback ParsecState s b
reply1
        { parsecResult = (,)
            <$> parsecResult reply1
            <*> (snd <$> parsecResult reply0)
        }
  second' :: forall a b c. Parsector s a b -> Parsector s (c, a) (c, b)
second' Parsector s a b
p = (forall x.
 (ParsecState s (c, b) -> x) -> ParsecState s (c, a) -> x)
-> Parsector s (c, a) (c, b)
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x.
  (ParsecState s (c, b) -> x) -> ParsecState s (c, a) -> x)
 -> Parsector s (c, a) (c, b))
-> (forall x.
    (ParsecState s (c, b) -> x) -> ParsecState s (c, a) -> x)
-> Parsector s (c, a) (c, b)
forall a b. (a -> b) -> a -> b
$ \ParsecState s (c, b) -> x
callback ParsecState s (c, a)
reply0 ->
    ((ParsecState s b -> x) -> ParsecState s a -> x)
-> ParsecState s a -> (ParsecState s b -> x) -> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p) ((c, a) -> a
forall a b. (a, b) -> b
snd ((c, a) -> a) -> ParsecState s (c, a) -> ParsecState s a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecState s (c, a)
reply0) ((ParsecState s b -> x) -> x) -> (ParsecState s b -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
reply1 ->
      ParsecState s (c, b) -> x
callback ParsecState s b
reply1
        { parsecResult = (,)
            <$> (fst <$> parsecResult reply0)
            <*> parsecResult reply1
        }
-- | Like `alternate`, without a sum of branches.
instance Categorized (Item s) => Choice (Parsector s) where
  left' :: forall a b c.
Parsector s a b -> Parsector s (Either a c) (Either b c)
left' (Parsector forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p) = (forall x.
 (ParsecState s (Either b c) -> x)
 -> ParsecState s (Either a c) -> x)
-> Parsector s (Either a c) (Either b c)
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x.
  (ParsecState s (Either b c) -> x)
  -> ParsecState s (Either a c) -> x)
 -> Parsector s (Either a c) (Either b c))
-> (forall x.
    (ParsecState s (Either b c) -> x)
    -> ParsecState s (Either a c) -> x)
-> Parsector s (Either a c) (Either b c)
forall a b. (a -> b) -> a -> b
$ \ParsecState s (Either b c) -> x
callback ParsecState s (Either a c)
query ->
    case ParsecState s (Either a c) -> Maybe (Either a c)
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s (Either a c)
query of
      Just (Right c
_) ->
        ParsecState s (Either b c) -> x
callback ParsecState s (Either a c)
query { parsecFailure = mempty, parsecResult = Nothing }
      Maybe (Either a c)
result -> (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p (ParsecState s (Either b c) -> x
callback (ParsecState s (Either b c) -> x)
-> (ParsecState s b -> ParsecState s (Either b c))
-> ParsecState s b
-> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (b -> Either b c) -> ParsecState s b -> ParsecState s (Either b c)
forall a b. (a -> b) -> ParsecState s a -> ParsecState s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> Either b c
forall a b. a -> Either a b
Left)
        ParsecState s (Either a c)
query { parsecResult = either Just (const Nothing) =<< result }
  right' :: forall a b c.
Parsector s a b -> Parsector s (Either c a) (Either c b)
right' (Parsector forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p) = (forall x.
 (ParsecState s (Either c b) -> x)
 -> ParsecState s (Either c a) -> x)
-> Parsector s (Either c a) (Either c b)
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x.
  (ParsecState s (Either c b) -> x)
  -> ParsecState s (Either c a) -> x)
 -> Parsector s (Either c a) (Either c b))
-> (forall x.
    (ParsecState s (Either c b) -> x)
    -> ParsecState s (Either c a) -> x)
-> Parsector s (Either c a) (Either c b)
forall a b. (a -> b) -> a -> b
$ \ParsecState s (Either c b) -> x
callback ParsecState s (Either c a)
query ->
    case ParsecState s (Either c a) -> Maybe (Either c a)
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s (Either c a)
query of
      Just (Left c
_) ->
        ParsecState s (Either c b) -> x
callback ParsecState s (Either c a)
query { parsecFailure = mempty, parsecResult = Nothing }
      Maybe (Either c a)
result -> (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p (ParsecState s (Either c b) -> x
callback (ParsecState s (Either c b) -> x)
-> (ParsecState s b -> ParsecState s (Either c b))
-> ParsecState s b
-> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (b -> Either c b) -> ParsecState s b -> ParsecState s (Either c b)
forall a b. (a -> b) -> ParsecState s a -> ParsecState s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> Either c b
forall a b. b -> Either a b
Right)
        ParsecState s (Either c a)
query { parsecResult = either (const Nothing) Just =<< result }
instance Categorized (Item s) => CocartesianMonoidal (Parsector s)
instance Categorized (Item s) => Distributor (Parsector s) where
  {- | Like @eotList . swapped >~ p >*< manyP p >+< oneP@, in one loop,
  with its hints: a failure of @p@ without consuming ends the list,
  merging its expectations with those of the last element.
  -}
  manyP :: forall a b. Parsector s a b -> Parsector s [a] [b]
manyP (Parsector forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p) = (forall x. (ParsecState s [b] -> x) -> ParsecState s [a] -> x)
-> Parsector s [a] [b]
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s [b] -> x) -> ParsecState s [a] -> x)
 -> Parsector s [a] [b])
-> (forall x. (ParsecState s [b] -> x) -> ParsecState s [a] -> x)
-> Parsector s [a] [b]
forall a b. (a -> b) -> a -> b
$ \ParsecState s [b] -> x
callback ParsecState s [a]
query ->
    let
      -- the first element is tried on the query itself
      parsedFirst :: ParsecState s b -> x
parsedFirst ParsecState s b
reply = case ParsecState s b -> Maybe b
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s b
reply of
        Just b
b -> [b] -> ParsecState s b -> x
parsedRest [b
b] ParsecState s b
reply
        Maybe b
Nothing
          | ParsecState s b -> Bool
forall s a. ParsecState s a -> Bool
parsecLooked ParsecState s b
reply -> ParsecState s [b] -> x
callback ParsecState s b
reply {parsecResult = Nothing}
          | Bool
otherwise -> ParsecState s [b] -> x
callback ParsecState s [a]
query
              { parsecResult = Just []
              , parsecFailure = parsecFailure reply <> parsecFailure query
              }
      -- the rest are tried after the reply to the last
      parsedRest :: [b] -> ParsecState s b -> x
parsedRest [b]
bs ParsecState s b
reply = (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p
        (\ParsecState s b
next -> case ParsecState s b -> Maybe b
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s b
next of
          Just b
b -> [b] -> ParsecState s b -> x
parsedRest (b
b b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [b]
bs) ParsecState s b
next
          Maybe b
Nothing -> Bool -> [b] -> ParsecState s b -> ParsecState s b -> x
ended Bool
False [b]
bs ParsecState s b
reply ParsecState s b
next)
        (ParsecState s b -> ParsecState s b
forall {s} {a}.
Categorized (Item s) =>
ParsecState s a -> ParsecState s a
afresh ParsecState s b
reply) {parsecResult = Nothing}
      printedFirst :: [a] -> ParsecState s b -> x
printedFirst [a]
as ParsecState s b
reply = case ParsecState s b -> Maybe b
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s b
reply of
        Just b
b -> [b] -> [a] -> ParsecState s b -> x
printedRest [b
b] [a]
as ParsecState s b
reply
        Maybe b
Nothing -> ParsecState s [b] -> x
callback ParsecState s b
reply {parsecResult = Nothing}
      printedRest :: [b] -> [a] -> ParsecState s b -> x
printedRest [b]
bs [a]
as ParsecState s b
reply = case [a]
as of
        [] -> ParsecState s [b] -> x
callback ParsecState s b
reply {parsecResult = Just (reverse bs)}
        a
a : [a]
as' -> (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p
          (\ParsecState s b
next -> case ParsecState s b -> Maybe b
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s b
next of
            Just b
b -> [b] -> [a] -> ParsecState s b -> x
printedRest (b
b b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [b]
bs) [a]
as' ParsecState s b
next
            Maybe b
Nothing -> Bool -> [b] -> ParsecState s b -> ParsecState s b -> x
ended Bool
True [b]
bs ParsecState s b
reply ParsecState s b
next)
          (ParsecState s b -> ParsecState s b
forall {s} {a}.
Categorized (Item s) =>
ParsecState s a -> ParsecState s a
afresh ParsecState s b
reply) {parsecResult = Just a}
      {- an element failed after the last one's reply, ending a parsed list,
      or failing a printed one, unless it was committed -}
      ended :: Bool -> [b] -> ParsecState s b -> ParsecState s b -> x
ended Bool
printing [b]
bs ParsecState s b
reply ParsecState s b
next
        | ParsecState s b -> Bool
forall s a. ParsecState s a -> Bool
parsecLooked ParsecState s b
next = ParsecState s [b] -> x
callback ParsecState s b
next {parsecResult = Nothing}
        | Bool
printing = ParsecState s [b] -> x
callback ParsecState s b
next
            { parsecLooked = parsecLooked reply
            , parsecFailure = parsecFailure reply <> parsecFailure next
            , parsecResult = Nothing
            }
        | Bool
otherwise = ParsecState s [b] -> x
callback ParsecState s b
reply
            { parsecFailure = parsecFailure reply <> parsecFailure next
            , parsecResult = Just (reverse bs)
            }
      afresh :: ParsecState s a -> ParsecState s a
afresh ParsecState s a
reply = ParsecState s a
reply {parsecLooked = False, parsecFailure = mempty}
    in
      case ParsecState s [a] -> Maybe [a]
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s [a]
query of
        Maybe [a]
Nothing -> (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p ParsecState s b -> x
parsedFirst ParsecState s [a]
query {parsecResult = Nothing}
        Just [] -> ParsecState s [b] -> x
callback ParsecState s [a]
query {parsecResult = Just []}
        Just (a
a : [a]
as) -> (ParsecState s b -> x) -> ParsecState s a -> x
forall x. (ParsecState s b -> x) -> ParsecState s a -> x
p ([a] -> ParsecState s b -> x
printedFirst [a]
as) ParsecState s [a]
query {parsecResult = Just a}
instance Categorized (Item s) => DisjunctiveMonoid (Parsector s a b)
instance Categorized (Item s) => CocartesianChoice (Parsector s) where
  alternate :: forall a b c d.
Either (Parsector s a b) (Parsector s c d)
-> Parsector s (Either a c) (Either b d)
alternate (Left Parsector s a b
p) = (forall x.
 (ParsecState s (Either b d) -> x)
 -> ParsecState s (Either a c) -> x)
-> Parsector s (Either a c) (Either b d)
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x.
  (ParsecState s (Either b d) -> x)
  -> ParsecState s (Either a c) -> x)
 -> Parsector s (Either a c) (Either b d))
-> (forall x.
    (ParsecState s (Either b d) -> x)
    -> ParsecState s (Either a c) -> x)
-> Parsector s (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ \ParsecState s (Either b d) -> x
callback ParsecState s (Either a c)
query -> ParsecState s (Either b d) -> x
callback (ParsecState s (Either b d) -> x)
-> ParsecState s (Either b d) -> x
forall a b. (a -> b) -> a -> b
$
    let
      replyOk :: ParsecState s a
replyOk = ParsecState s (Either a c)
query
        { parsecResult = case parsecResult query of
            Maybe (Either a c)
Nothing         -> Maybe a
forall a. Maybe a
Nothing
            Just (Left a
a)   -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
            Just (Right c
_)  -> Maybe a
forall a. Maybe a
Nothing
        }
      replyErr :: ParsecState s (Either b d)
replyErr = ParsecState s (Either a c)
query { parsecFailure = mempty, parsecResult = Nothing }
    in
      case (ParsecState s (Either a c) -> Maybe (Either a c)
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s (Either a c)
query, ParsecState s a -> Maybe a
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s a
replyOk) of
        (Just Either a c
_, Maybe a
Nothing) -> ParsecState s (Either b d)
replyErr
        (Maybe (Either a c), Maybe a)
_________________ ->
          ((ParsecState s b -> ParsecState s (Either b d))
 -> ParsecState s a -> ParsecState s (Either b d))
-> ParsecState s a
-> (ParsecState s b -> ParsecState s (Either b d))
-> ParsecState s (Either b d)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s a b
p) ParsecState s a
replyOk ((ParsecState s b -> ParsecState s (Either b d))
 -> ParsecState s (Either b d))
-> (ParsecState s b -> ParsecState s (Either b d))
-> ParsecState s (Either b d)
forall a b. (a -> b) -> a -> b
$ \ParsecState s b
reply -> ParsecState s b
reply
            { parsecResult = fmap Left (parsecResult reply) }
  alternate (Right Parsector s c d
p) = (forall x.
 (ParsecState s (Either b d) -> x)
 -> ParsecState s (Either a c) -> x)
-> Parsector s (Either a c) (Either b d)
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x.
  (ParsecState s (Either b d) -> x)
  -> ParsecState s (Either a c) -> x)
 -> Parsector s (Either a c) (Either b d))
-> (forall x.
    (ParsecState s (Either b d) -> x)
    -> ParsecState s (Either a c) -> x)
-> Parsector s (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ \ParsecState s (Either b d) -> x
callback ParsecState s (Either a c)
query -> ParsecState s (Either b d) -> x
callback (ParsecState s (Either b d) -> x)
-> ParsecState s (Either b d) -> x
forall a b. (a -> b) -> a -> b
$
    let
      replyOk :: ParsecState s c
replyOk = ParsecState s (Either a c)
query
        { parsecResult = case parsecResult query of
            Maybe (Either a c)
Nothing         -> Maybe c
forall a. Maybe a
Nothing
            Just (Left a
_)   -> Maybe c
forall a. Maybe a
Nothing
            Just (Right c
b)  -> c -> Maybe c
forall a. a -> Maybe a
Just c
b
        }
      replyErr :: ParsecState s (Either b d)
replyErr = ParsecState s (Either a c)
query { parsecFailure = mempty, parsecResult = Nothing }
    in
      case (ParsecState s (Either a c) -> Maybe (Either a c)
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s (Either a c)
query, ParsecState s c -> Maybe c
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s c
replyOk) of
        (Just Either a c
_, Maybe c
Nothing) -> ParsecState s (Either b d)
replyErr
        (Maybe (Either a c), Maybe c)
_________________ ->
          ((ParsecState s d -> ParsecState s (Either b d))
 -> ParsecState s c -> ParsecState s (Either b d))
-> ParsecState s c
-> (ParsecState s d -> ParsecState s (Either b d))
-> ParsecState s (Either b d)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s c d
-> forall x. (ParsecState s d -> x) -> ParsecState s c -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s c d
p) ParsecState s c
replyOk ((ParsecState s d -> ParsecState s (Either b d))
 -> ParsecState s (Either b d))
-> (ParsecState s d -> ParsecState s (Either b d))
-> ParsecState s (Either b d)
forall a b. (a -> b) -> a -> b
$ \ParsecState s d
reply -> ParsecState s d
reply
            { parsecResult = fmap Right (parsecResult reply) }
instance Categorized (Item s) => Alternator (Parsector s) where
  optionP :: forall a b. APrism a b () () -> Parsector s a b -> Parsector s a b
optionP APrism a b () ()
def Parsector s a b
p = (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
 -> Parsector s a b)
-> (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall a b. (a -> b) -> a -> b
$ \ParsecState s b -> x
callback ParsecState s a
query ->
    case ParsecState s a -> Maybe a
forall s a. ParsecState s a -> Maybe a
parsecResult ParsecState s a
query of
      Maybe a
Nothing -> Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector (Parsector s a b
p Parsector s a b -> Parsector s a b -> Parsector s a b
forall a. Parsector s a a -> Parsector s a a -> Parsector s a a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> APrism a b () () -> Parsector s a b
forall (p :: * -> * -> *) a b.
(Monoidal p, Choice p) =>
APrism a b () () -> p a b
pureP APrism a b () ()
def) ParsecState s b -> x
callback ParsecState s a
query
      Just a
_ -> Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector (APrism a b () () -> Parsector s a b
forall (p :: * -> * -> *) a b.
(Monoidal p, Choice p) =>
APrism a b () () -> p a b
pureP APrism a b () ()
def Parsector s a b -> Parsector s a b -> Parsector s a b
forall a. Parsector s a a -> Parsector s a a -> Parsector s a a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parsector s a b
p) ParsecState s b -> x
callback ParsecState s a
query
instance Categorized (Item s) => Cochoice (Parsector s) where
  unleft :: forall a d b.
Parsector s (Either a d) (Either b d) -> Parsector s a b
unleft = (Parsector s a b, Parsector s d d) -> Parsector s a b
forall a b. (a, b) -> a
fst ((Parsector s a b, Parsector s d d) -> Parsector s a b)
-> (Parsector s (Either a d) (Either b d)
    -> (Parsector s a b, Parsector s d d))
-> Parsector s (Either a d) (Either b d)
-> Parsector s a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsector s (Either a d) (Either b d)
-> (Parsector s a b, Parsector s d d)
forall a c b d.
Parsector s (Either a c) (Either b d)
-> (Parsector s a b, Parsector s c d)
forall (p :: * -> * -> *) a c b d.
Filtrator p =>
p (Either a c) (Either b d) -> (p a b, p c d)
filtrate
  unright :: forall d a b.
Parsector s (Either d a) (Either d b) -> Parsector s a b
unright = (Parsector s d d, Parsector s a b) -> Parsector s a b
forall a b. (a, b) -> b
snd ((Parsector s d d, Parsector s a b) -> Parsector s a b)
-> (Parsector s (Either d a) (Either d b)
    -> (Parsector s d d, Parsector s a b))
-> Parsector s (Either d a) (Either d b)
-> Parsector s a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Parsector s (Either d a) (Either d b)
-> (Parsector s d d, Parsector s a b)
forall a c b d.
Parsector s (Either a c) (Either b d)
-> (Parsector s a b, Parsector s c d)
forall (p :: * -> * -> *) a c b d.
Filtrator p =>
p (Either a c) (Either b d) -> (p a b, p c d)
filtrate
instance Categorized (Item s) => Filtrator (Parsector s) where
  filtrate :: forall a c b d.
Parsector s (Either a c) (Either b d)
-> (Parsector s a b, Parsector s c d)
filtrate Parsector s (Either a c) (Either b d)
p =
    ( (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
 -> Parsector s a b)
-> (forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
forall a b. (a -> b) -> a -> b
$ \ParsecState s b -> x
callback ParsecState s a
query ->
        ((ParsecState s (Either b d) -> x)
 -> ParsecState s (Either a c) -> x)
-> ParsecState s (Either a c)
-> (ParsecState s (Either b d) -> x)
-> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s (Either a c) (Either b d)
-> forall x.
   (ParsecState s (Either b d) -> x)
   -> ParsecState s (Either a c) -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s (Either a c) (Either b d)
p) (a -> Either a c
forall a b. a -> Either a b
Left (a -> Either a c) -> ParsecState s a -> ParsecState s (Either a c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecState s a
query) ((ParsecState s (Either b d) -> x) -> x)
-> (ParsecState s (Either b d) -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s (Either b d)
reply ->
          ParsecState s b -> x
callback ParsecState s (Either b d)
reply
          { parsecFailure = case parsecResult reply of
            Just (Right d
_) -> ParsecFailure s
forall a. Monoid a => a
mempty
            Maybe (Either b d)
_ -> ParsecState s (Either b d) -> ParsecFailure s
forall s a. ParsecState s a -> ParsecFailure s
parsecFailure ParsecState s (Either b d)
reply
          , parsecResult =
            parsecResult reply >>= either Just (const Nothing)
          }
    , (forall x. (ParsecState s d -> x) -> ParsecState s c -> x)
-> Parsector s c d
forall s a b.
(forall x. (ParsecState s b -> x) -> ParsecState s a -> x)
-> Parsector s a b
Parsector ((forall x. (ParsecState s d -> x) -> ParsecState s c -> x)
 -> Parsector s c d)
-> (forall x. (ParsecState s d -> x) -> ParsecState s c -> x)
-> Parsector s c d
forall a b. (a -> b) -> a -> b
$ \ParsecState s d -> x
callback ParsecState s c
query ->
        ((ParsecState s (Either b d) -> x)
 -> ParsecState s (Either a c) -> x)
-> ParsecState s (Either a c)
-> (ParsecState s (Either b d) -> x)
-> x
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Parsector s (Either a c) (Either b d)
-> forall x.
   (ParsecState s (Either b d) -> x)
   -> ParsecState s (Either a c) -> x
forall s a b.
Parsector s a b
-> forall x. (ParsecState s b -> x) -> ParsecState s a -> x
runParsector Parsector s (Either a c) (Either b d)
p) (c -> Either a c
forall a b. b -> Either a b
Right (c -> Either a c) -> ParsecState s c -> ParsecState s (Either a c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecState s c
query) ((ParsecState s (Either b d) -> x) -> x)
-> (ParsecState s (Either b d) -> x) -> x
forall a b. (a -> b) -> a -> b
$ \ParsecState s (Either b d)
reply ->
          ParsecState s d -> x
callback ParsecState s (Either b d)
reply
          { parsecFailure = case parsecResult reply of
            Just (Left b
_) -> ParsecFailure s
forall a. Monoid a => a
mempty
            Maybe (Either b d)
_ -> ParsecState s (Either b d) -> ParsecFailure s
forall s a. ParsecState s a -> ParsecFailure s
parsecFailure ParsecState s (Either b d)
reply
          , parsecResult =
            parsecResult reply >>= either (const Nothing) Just
          }
    )