{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Reifiable parser syntax.
--
-- The parser type in "Circuit.Parser" hides primitive operations inside
-- @K m@ closures over a @Body (,) f (K m)@ base. This module
-- exposes those primitives as constructors in a syntax tree, so the same
-- parser can be executed /and/ analyzed.
--
-- The plumbing — sequential composition and the structural combinators — is
-- the free category over the pure ambient-state arrow @Body (,) f (->)@
-- (@Body (,) f (->)@), extended with 'SigPrim' and 'SigComb' signatures.
-- The stream @f@ is ambient state throughout: primitives consume it, and
-- choice is a structural combinator, not a trace. There is no 'SigKnot' /
-- @Loop Either@ here — the knot-body category @Body@ is the fold target,
-- and the stream is never hidden in a feedback channel.
--
-- Executing a syntax tree is an algebra fold into @Body (,) f (K m)@;
-- static analysis is a fold into other targets ('FirstSet', 'Regex', the
-- Brzozowski derivative).
--
-- === doctests
--
-- >>> import Data.Functor.Identity (Identity)
-- >>> import Data.These (These(..))
-- >>> import Control.Applicative ((<|>))
-- >>> import Circuit.Parser.Syntax (charS, stringS, manyS, firstSet, toRegex)
--
-- >>> runParserSyntaxIdentity (charS 'a') "abc"
-- These 'a' "bc"
--
-- >>> runParserSyntaxIdentity (stringS "ab" <|> stringS "a") "ab"
-- These "ab" ""
--
-- >>> runParserSyntaxIdentity (stringS "ab" <|> stringS "a") "a"
-- These "a" ""
--
-- >>> runParserSyntaxIdentity (manyS (charS 'a')) "aaab"
-- These "aaa" "b"
--
-- >>> firstSet (charS 'a' <|> charS 'b' :: ParserSyntax String Char Char)
-- FirstSet {nullable = False, firstKind = FSPred <function>}
--
-- >>> toRegex (manyS (charS 'a') :: ParserSyntax String Char String)
-- Just REStar (REChar 'a')
module Circuit.Parser.Syntax
  ( -- * Signatures
    SigPrim (..),
    SigComb (..),

    -- * Syntax tree
    ParserSyntax (..),

    -- * Primitive constructors
    nextS,
    anyTokenS,
    satisfyS,
    charS,
    stringS,
    endOfInputS,
    takeRestS,

    -- * Combinator constructors
    tryS,
    optionalS,
    manyS,
    someS,
    skipManyS,
    countS,
    sepByS,
    sepBy1S,
    withOptionS,

    -- * Execution
    runParserSyntax,
    runParserSyntaxIdentity,

    -- * Static analysis
    FirstSet (..),
    firstSet,
    unreachableBranches,

    -- * Regex extraction
    Regex (..),
    toRegex,

    -- * Brzozowski derivatives
    derive,
    nullableValue,
  )
where

import Circuit.Body (Body (..))
import Circuit.Category (Category (..), K (..))
import Circuit.Parser (Parser (..), Uncons (..))
import Circuit.Parser qualified as PU
import Circuit.Syntax
  ( Algebra (..),
    SigCompose (..),
    Syntax (..),
    evalInto,
    (:+:) (..),
  )
import Control.Applicative (Alternative (empty, (<|>)), optional)
import Control.Monad (MonadPlus, void)
import Data.Kind (Type)
import Data.These (These (..))
import Prelude hiding (id, (.))

-- $setup
-- >>> import Data.Functor.Identity (Identity)
-- >>> import Data.These (These (..))
-- >>> import Control.Applicative ((<|>))
-- >>> import Circuit.Parser.Syntax (charS, stringS, manyS, firstSet, toRegex)

-- ---------------------------------------------------------------------------
-- Signatures
-- ---------------------------------------------------------------------------

-- | One-step parser primitives. Each constructor names a leaf operation on a
-- stream of elements @s@ with stream type @f@. The stream is ambient state,
-- so the source object is unit and the target carries the result plus
-- leftover stream.
data SigPrim (f :: Type) (s :: Type) (arr :: Type -> Type -> Type) (rec :: Type -> Type -> Type) (a :: Type) (b :: Type) where
  PrimNext :: SigPrim f s arr rec () (These s f)
  PrimSatisfy :: (s -> Bool) -> SigPrim f s arr rec () (These s f)
  PrimChar :: (Eq s) => s -> SigPrim f s arr rec () (These s f)
  PrimString :: (Eq s) => [s] -> SigPrim f s arr rec () (These [s] f)
  PrimEndOfInput :: SigPrim f s arr rec () (These () f)
  PrimTakeRest :: SigPrim f s arr rec () (These f f)

-- | Structural combinators that cannot be expressed as pure sequential
-- composition while preserving the syntax tree. These are eliminated by the
-- execution algebra into the corresponding @Parser@ combinators.
data SigComb (f :: Type) (s :: Type) (arr :: Type -> Type -> Type) (rec :: Type -> Type -> Type) (a :: Type) (b :: Type) where
  CombAp :: rec () (These (a -> b) f) -> rec () (These a f) -> SigComb f s arr rec () (These b f)
  CombBind :: rec () (These a f) -> (a -> rec () (These b f)) -> SigComb f s arr rec () (These b f)
  CombAlt :: rec () (These a f) -> rec () (These a f) -> SigComb f s arr rec () (These a f)
  CombMany :: rec () (These a f) -> SigComb f s arr rec () (These [a] f)
  CombTry :: rec () (These a f) -> SigComb f s arr rec () (These a f)
  CombFmap :: (a -> b) -> rec () (These a f) -> SigComb f s arr rec () (These b f)

-- | The full parser signature: composition, primitives, and structural
-- combinators. No 'SigKnot' — choice is a combinator, not a trace.
type ParserSyntaxSig f s = SigCompose :+: SigPrim f s :+: SigComb f s

-- | A parser syntax tree with stream type @f@, element type @s@, and result
-- type @a@. The stream is ambient state (@Body (,) f (->)@ base arrow), the source
-- object is unit, and the target carries the result plus leftover stream.
newtype ParserSyntax (f :: Type) (s :: Type) (a :: Type) = ParserSyntax
  { forall f s a.
ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
unParserSyntax ::
      Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
  }

-- | The underlying syntax type supports sequential composition via 'SigCompose'.
instance Category (Syntax (ParserSyntaxSig f s) (Body (,) f (->))) where
  id :: forall a. Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a a
id = Body (,) f (->) a a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a a
forall (arr :: * -> * -> *) a b (sig :: Sig).
arr a b -> Syntax sig arr a b
Lift Body (,) f (->) a a
forall a. Body (,) f (->) a a
forall k (arr :: k -> k -> *) (a :: k). Category arr => arr a a
id
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b c
f . :: forall b c a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b c
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a b
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a c
. Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a b
g = ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  a
  c
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a c
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (SigCompose
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  a
  c
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     a
     c
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b c
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a b
-> SigCompose
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     a
     c
forall {k1} {k} (rec :: k1 -> k1 -> *) (b1 :: k1) (b :: k1)
       (a :: k1) (arr :: k).
rec b1 b -> rec a b1 -> SigCompose arr rec a b
SigCompose Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b c
f Syntax (ParserSyntaxSig f s) (Body (,) f (->)) a b
g))

-- ---------------------------------------------------------------------------
-- Algebra for execution
-- ---------------------------------------------------------------------------

-- | Map primitive operations to their implementations in
-- @Body (,) f (K m)@.
instance
  (Monad m, Uncons f s) =>
  Algebra (SigPrim f s) (Body (,) f (->)) (Body (,) f (K m))
  where
  type
    Ctx (SigPrim f s) (Body (,) f (->)) (Body (,) f (K m)) =
      (Monad m, Uncons f s)
  alg :: forall (rec :: * -> * -> *) a b.
Ctx (SigPrim f s) (Body (,) f (->)) (Body (,) f (K m)) =>
(forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y)
-> (forall x y. rec x y -> Body (,) f (K m) x y)
-> SigPrim f s (Body (,) f (->)) rec a b
-> Body (,) f (K m) a b
alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
_ SigPrim f s (Body (,) f (->)) rec a b
PrimNext = Parser m f s s -> Body (,) f (K m) () (These s f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s. (Monad m, Uncons f s) => Parser m f s s
PU.next @m @f @s)
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
_ (PrimSatisfy s -> Bool
p) = Parser m f s s -> Body (,) f (K m) () (These s f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s.
(Monad m, Uncons f s) =>
(s -> Bool) -> Parser m f s s
PU.satisfy @m @f @s s -> Bool
p)
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
_ (PrimChar s
c) = Parser m f s s -> Body (,) f (K m) () (These s f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s.
(Monad m, Uncons f s, Eq s) =>
s -> Parser m f s s
PU.char @m @f @s s
c)
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
_ (PrimString [s]
cs) = Parser m f s [s] -> Body (,) f (K m) () (These [s] f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s.
(Monad m, Uncons f s, Eq s) =>
[s] -> Parser m f s [s]
PU.string @m @f @s [s]
cs)
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
_ SigPrim f s (Body (,) f (->)) rec a b
PrimEndOfInput = Parser m f s () -> Body (,) f (K m) () (These () f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s. (Monad m, Uncons f s) => Parser m f s ()
PU.endOfInput @m @f @s)
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
_ SigPrim f s (Body (,) f (->)) rec a b
PrimTakeRest = Parser m f s f -> Body (,) f (K m) () (These f f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s. (Monad m, Uncons f s) => Parser m f s f
PU.takeRest @m @f @s)

instance
  (Monad m, Uncons f s) =>
  Algebra (SigComb f s) (Body (,) f (->)) (Body (,) f (K m))
  where
  type
    Ctx (SigComb f s) (Body (,) f (->)) (Body (,) f (K m)) =
      (Monad m, Uncons f s)
  alg :: forall (rec :: * -> * -> *) a b.
Ctx (SigComb f s) (Body (,) f (->)) (Body (,) f (K m)) =>
(forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y)
-> (forall x y. rec x y -> Body (,) f (K m) x y)
-> SigComb f s (Body (,) f (->)) rec a b
-> Body (,) f (K m) a b
alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
rec (CombAp rec () (These (a -> b) f)
pf rec () (These a f)
pa) = Parser m f s b -> Body (,) f (K m) () (These b f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These (a -> b) f) -> Body (,) f (K m) () (These (a -> b) f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These (a -> b) f)
pf) Parser m f s (a -> b) -> Parser m f s a -> Parser m f s b
forall a b.
Parser m f s (a -> b) -> Parser m f s a -> Parser m f s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
pa))
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
rec (CombBind rec () (These a f)
p a -> rec () (These b f)
k) = Parser m f s b -> Body (,) f (K m) () (These b f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
p) Parser m f s a -> (a -> Parser m f s b) -> Parser m f s b
forall a b.
Parser m f s a -> (a -> Parser m f s b) -> Parser m f s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These b f) -> Body (,) f (K m) () (These b f)
forall x y. rec x y -> Body (,) f (K m) x y
rec (a -> rec () (These b f)
k a
a)))
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
rec (CombAlt rec () (These a f)
p1 rec () (These a f)
p2) = Parser m f s a -> Body (,) f (K m) () (These a f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
p1) Parser m f s a -> Parser m f s a -> Parser m f s a
forall a. Parser m f s a -> Parser m f s a -> Parser m f s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
p2))
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
rec (CombMany rec () (These a f)
p) = Parser m f s [a] -> Body (,) f (K m) () (These [a] f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s a.
(Monad m, Uncons f s) =>
Parser m f s a -> Parser m f s [a]
PU.many @m @f @s (forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
p)))
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
rec (CombTry rec () (These a f)
p) = Parser m f s a -> Body (,) f (K m) () (These a f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (forall (m :: * -> *) f s a.
(Monad m, Uncons f s) =>
Parser m f s a -> Parser m f s a
PU.try @m @f @s (forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
p)))
  alg forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
_ forall x y. rec x y -> Body (,) f (K m) x y
rec (CombFmap a -> b
g rec () (These a f)
p) = Parser m f s b -> Body (,) f (K m) () (These b f)
forall {k} (m :: * -> *) f (s :: k) a.
Parser m f s a -> Body (,) f (K m) () (These a f)
unParser (a -> b
g (a -> b) -> Parser m f s a -> Parser m f s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
forall (m :: * -> *) f s a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser @m @f @s (rec () (These a f) -> Body (,) f (K m) () (These a f)
forall x y. rec x y -> Body (,) f (K m) x y
rec rec () (These a f)
p))

-- | Interpret syntax into a concrete parser.
runParserSyntax ::
  forall m f s a.
  (Monad m, Uncons f s) =>
  ParserSyntax f s a ->
  Parser m f s a
runParserSyntax :: forall (m :: * -> *) f s a.
(Monad m, Uncons f s) =>
ParserSyntax f s a -> Parser m f s a
runParserSyntax (ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn) = Body (,) f (K m) () (These a f) -> Parser m f s a
forall {k} (m :: * -> *) f (s :: k) a.
Body (,) f (K m) () (These a f) -> Parser m f s a
Parser ((forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Body (,) f (K m) () (These a f)
forall (arr' :: * -> * -> *) (sig :: Sig) (arr :: * -> * -> *) a b.
(Category arr', Algebra sig arr arr', Ctx sig arr arr') =>
(forall x y. arr x y -> arr' x y) -> Syntax sig arr a b -> arr' a b
evalInto Body (,) f (->) x y -> Body (,) f (K m) x y
forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
emb Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn)
  where
    emb :: forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
    emb :: forall x y. Body (,) f (->) x y -> Body (,) f (K m) x y
emb (Body (f, x) -> (f, y)
g) = K m (f, x) (f, y) -> Body (,) f (K m) x y
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
       (arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (K m (f, x) (f, y) -> Body (,) f (K m) x y)
-> K m (f, x) (f, y) -> Body (,) f (K m) x y
forall a b. (a -> b) -> a -> b
$ ((f, x) -> m (f, y)) -> K m (f, x) (f, y)
forall {k} (m :: k -> *) a (b :: k). (a -> m b) -> K m a b
K ((f, y) -> m (f, y)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((f, y) -> m (f, y)) -> ((f, x) -> (f, y)) -> (f, x) -> m (f, y)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall k (arr :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category arr =>
arr b c -> arr a b -> arr a c
. (f, x) -> (f, y)
g)

-- | Interpret syntax into an identity parser.
runParserSyntaxIdentity ::
  (Uncons f s) =>
  ParserSyntax f s a ->
  f ->
  These a f
runParserSyntaxIdentity :: forall f s a. Uncons f s => ParserSyntax f s a -> f -> These a f
runParserSyntaxIdentity ParserSyntax f s a
p = Parser Identity f s a -> f -> These a f
forall {k} f (s :: k) a. Parser Identity f s a -> f -> These a f
PU.runParserIdentity (ParserSyntax f s a -> Parser Identity f s a
forall (m :: * -> *) f s a.
(Monad m, Uncons f s) =>
ParserSyntax f s a -> Parser m f s a
runParserSyntax ParserSyntax f s a
p)

-- ---------------------------------------------------------------------------
-- Instances
-- ---------------------------------------------------------------------------

instance (Uncons f s) => Functor (ParserSyntax f s) where
  fmap :: forall a b. (a -> b) -> ParserSyntax f s a -> ParserSyntax f s b
fmap a -> b
g (ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p) =
    Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
-> ParserSyntax f s b
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall a b (rec :: * -> * -> *) f s (arr :: * -> * -> *).
(a -> b)
-> rec () (These a f) -> SigComb f s arr rec () (These b f)
CombFmap a -> b
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p))))

instance (Uncons f s) => Applicative (ParserSyntax f s) where
  pure :: forall a. a -> ParserSyntax f s a
pure a
a = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> ParserSyntax f s a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall a b. (a -> b) -> a -> b
$ Body (,) f (->) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (arr :: * -> * -> *) a b (sig :: Sig).
arr a b -> Syntax sig arr a b
Lift (Body (,) f (->) () (These a f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> Body (,) f (->) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a b. (a -> b) -> a -> b
$ ((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f)
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
       (arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f))
-> ((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f)
forall a b. (a -> b) -> a -> b
$ \(f
st, ()) -> (f
st, a -> f -> These a f
forall a b. a -> b -> These a b
These a
a f
st)
  ParserSyntax Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf <*> :: forall a b.
ParserSyntax f s (a -> b)
-> ParserSyntax f s a -> ParserSyntax f s b
<*> ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa =
    Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
-> ParserSyntax f s b
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
 -> ParserSyntax f s b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
-> ParserSyntax f s b
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These b f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These b f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These b f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall a b. (a -> b) -> a -> b
$ SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These b f)
 -> (:+:)
      (SigPrim f s)
      (SigComb f s)
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These b f))
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall a b. (a -> b) -> a -> b
$ Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall (rec :: * -> * -> *) a b f s (arr :: * -> * -> *).
rec () (These (a -> b) f)
-> rec () (These a f) -> SigComb f s arr rec () (These b f)
CombAp Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa

instance (Uncons f s) => Monad (ParserSyntax f s) where
  ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
m >>= :: forall a b.
ParserSyntax f s a
-> (a -> ParserSyntax f s b) -> ParserSyntax f s b
>>= a -> ParserSyntax f s b
k =
    Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
-> ParserSyntax f s b
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
 -> ParserSyntax f s b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
-> ParserSyntax f s b
forall a b. (a -> b) -> a -> b
$
      ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These b f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall a b. (a -> b) -> a -> b
$
        (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These b f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These b f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall a b. (a -> b) -> a -> b
$
          SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These b f)
 -> (:+:)
      (SigPrim f s)
      (SigComb f s)
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These b f))
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall a b. (a -> b) -> a -> b
$
            Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> (a
    -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f))
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall (rec :: * -> * -> *) a f b s (arr :: * -> * -> *).
rec () (These a f)
-> (a -> rec () (These b f)) -> SigComb f s arr rec () (These b f)
CombBind Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
m (ParserSyntax f s b
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall f s a.
ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
unParserSyntax (ParserSyntax f s b
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f))
-> (a -> ParserSyntax f s b)
-> a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall k (arr :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category arr =>
arr b c -> arr a b -> arr a c
. a -> ParserSyntax f s b
k)

instance (Uncons f s) => Alternative (ParserSyntax f s) where
  empty :: forall a. ParserSyntax f s a
empty = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> ParserSyntax f s a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall a b. (a -> b) -> a -> b
$ Body (,) f (->) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (arr :: * -> * -> *) a b (sig :: Sig).
arr a b -> Syntax sig arr a b
Lift (Body (,) f (->) () (These a f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> Body (,) f (->) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a b. (a -> b) -> a -> b
$ ((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f)
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
       (arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f))
-> ((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f)
forall a b. (a -> b) -> a -> b
$ \(f
st, ()) -> (f
st, f -> These a f
forall a b. b -> These a b
That f
st)
  ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 <|> :: forall a.
ParserSyntax f s a -> ParserSyntax f s a -> ParserSyntax f s a
<|> ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2 =
    Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> ParserSyntax f s a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These a f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These a f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These a f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall a b. (a -> b) -> a -> b
$ SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These a f)
 -> (:+:)
      (SigPrim f s)
      (SigComb f s)
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These a f))
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall a b. (a -> b) -> a -> b
$ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall (rec :: * -> * -> *) a f s (arr :: * -> * -> *).
rec () (These a f)
-> rec () (These a f) -> SigComb f s arr rec () (These a f)
CombAlt Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2

instance (Uncons f s) => MonadPlus (ParserSyntax f s)

-- ---------------------------------------------------------------------------
-- Primitive constructors
-- ---------------------------------------------------------------------------

nextS :: ParserSyntax f s s
nextS :: forall f s. ParserSyntax f s s
nextS = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
-> ParserSyntax f s s
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
 -> ParserSyntax f s s)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
-> ParserSyntax f s s
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These s f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These s f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These s f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall a b. (a -> b) -> a -> b
$ SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
forall f s (arr :: * -> * -> *) (rec :: * -> * -> *).
SigPrim f s arr rec () (These s f)
PrimNext

anyTokenS :: ParserSyntax f s s
anyTokenS :: forall f s. ParserSyntax f s s
anyTokenS = ParserSyntax f s s
forall f s. ParserSyntax f s s
nextS

satisfyS :: (s -> Bool) -> ParserSyntax f s s
satisfyS :: forall s f. (s -> Bool) -> ParserSyntax f s s
satisfyS s -> Bool
p = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
-> ParserSyntax f s s
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
 -> ParserSyntax f s s)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
-> ParserSyntax f s s
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These s f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These s f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These s f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall a b. (a -> b) -> a -> b
$ SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L ((s -> Bool)
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall s f (arr :: * -> * -> *) (rec :: * -> * -> *).
(s -> Bool) -> SigPrim f s arr rec () (These s f)
PrimSatisfy s -> Bool
p)

charS :: (Eq s) => s -> ParserSyntax f s s
charS :: forall s f. Eq s => s -> ParserSyntax f s s
charS s
c = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
-> ParserSyntax f s s
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
 -> ParserSyntax f s s)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
-> ParserSyntax f s s
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These s f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These s f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These s f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These s f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall a b. (a -> b) -> a -> b
$ SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These s f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L (s
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These s f)
forall s f (arr :: * -> * -> *) (rec :: * -> * -> *).
Eq s =>
s -> SigPrim f s arr rec () (These s f)
PrimChar s
c)

stringS :: (Eq s) => [s] -> ParserSyntax f s [s]
stringS :: forall s f. Eq s => [s] -> ParserSyntax f s [s]
stringS [s]
cs = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
-> ParserSyntax f s [s]
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
 -> ParserSyntax f s [s])
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
-> ParserSyntax f s [s]
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [s] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These [s] f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [s] f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These [s] f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These [s] f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall a b. (a -> b) -> a -> b
$ SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [s] f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L ([s]
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall s f (arr :: * -> * -> *) (rec :: * -> * -> *).
Eq s =>
[s] -> SigPrim f s arr rec () (These [s] f)
PrimString [s]
cs)

endOfInputS :: ParserSyntax f s ()
endOfInputS :: forall f s. ParserSyntax f s ()
endOfInputS = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These () f)
-> ParserSyntax f s ()
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These () f)
 -> ParserSyntax f s ())
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These () f)
-> ParserSyntax f s ()
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These () f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These () f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These () f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These () f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These () f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These () f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These () f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These () f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These () f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These () f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These () f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These () f)
forall a b. (a -> b) -> a -> b
$ SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These () f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These () f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These () f)
forall f s (arr :: * -> * -> *) (rec :: * -> * -> *).
SigPrim f s arr rec () (These () f)
PrimEndOfInput

takeRestS :: ParserSyntax f s f
takeRestS :: forall f s. ParserSyntax f s f
takeRestS = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These f f)
-> ParserSyntax f s f
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These f f)
 -> ParserSyntax f s f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These f f)
-> ParserSyntax f s f
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These f f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These f f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These f f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These f f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These f f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These f f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These f f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These f f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These f f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These f f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These f f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These f f)
forall a b. (a -> b) -> a -> b
$ SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These f f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These f f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These f f)
forall f s (arr :: * -> * -> *) (rec :: * -> * -> *).
SigPrim f s arr rec () (These f f)
PrimTakeRest

-- ---------------------------------------------------------------------------
-- Combinators
-- ---------------------------------------------------------------------------

tryS :: ParserSyntax f s a -> ParserSyntax f s a
tryS :: forall f s a. ParserSyntax f s a -> ParserSyntax f s a
tryS (ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p) = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> ParserSyntax f s a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These a f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These a f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These a f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall a b. (a -> b) -> a -> b
$ SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These a f)
 -> (:+:)
      (SigPrim f s)
      (SigComb f s)
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These a f))
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall a b. (a -> b) -> a -> b
$ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall (rec :: * -> * -> *) a f s (arr :: * -> * -> *).
rec () (These a f) -> SigComb f s arr rec () (These a f)
CombTry Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p

optionalS :: (Uncons f s) => ParserSyntax f s a -> ParserSyntax f s (Maybe a)
optionalS :: forall f s a.
Uncons f s =>
ParserSyntax f s a -> ParserSyntax f s (Maybe a)
optionalS = ParserSyntax f s a -> ParserSyntax f s (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional

manyS :: ParserSyntax f s a -> ParserSyntax f s [a]
manyS :: forall f s a. ParserSyntax f s a -> ParserSyntax f s [a]
manyS (ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p) = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
-> ParserSyntax f s [a]
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
 -> ParserSyntax f s [a])
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
-> ParserSyntax f s [a]
forall a b. (a -> b) -> a -> b
$ ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [a] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op (ParserSyntaxSig
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These [a] f)
 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f))
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
forall a b. (a -> b) -> a -> b
$ (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [a] f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((:+:)
   (SigPrim f s)
   (SigComb f s)
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These [a] f)
 -> ParserSyntaxSig
      f
      s
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These [a] f))
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall a b. (a -> b) -> a -> b
$ SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [a] f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
   f
   s
   (Body (,) f (->))
   (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
   ()
   (These [a] f)
 -> (:+:)
      (SigPrim f s)
      (SigComb f s)
      (Body (,) f (->))
      (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
      ()
      (These [a] f))
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall a b. (a -> b) -> a -> b
$ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall (rec :: * -> * -> *) a f s (arr :: * -> * -> *).
rec () (These a f) -> SigComb f s arr rec () (These [a] f)
CombMany Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p

someS :: (Uncons f s) => ParserSyntax f s a -> ParserSyntax f s [a]
someS :: forall f s a.
Uncons f s =>
ParserSyntax f s a -> ParserSyntax f s [a]
someS ParserSyntax f s a
p = (:) (a -> [a] -> [a])
-> ParserSyntax f s a -> ParserSyntax f s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserSyntax f s a
p ParserSyntax f s ([a] -> [a])
-> ParserSyntax f s [a] -> ParserSyntax f s [a]
forall a b.
ParserSyntax f s (a -> b)
-> ParserSyntax f s a -> ParserSyntax f s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParserSyntax f s a -> ParserSyntax f s [a]
forall f s a. ParserSyntax f s a -> ParserSyntax f s [a]
manyS ParserSyntax f s a
p

skipManyS :: (Uncons f s) => ParserSyntax f s a -> ParserSyntax f s ()
skipManyS :: forall f s a.
Uncons f s =>
ParserSyntax f s a -> ParserSyntax f s ()
skipManyS ParserSyntax f s a
p = ParserSyntax f s [a] -> ParserSyntax f s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParserSyntax f s a -> ParserSyntax f s [a]
forall f s a. ParserSyntax f s a -> ParserSyntax f s [a]
manyS ParserSyntax f s a
p)

countS :: (Uncons f s) => Int -> ParserSyntax f s a -> ParserSyntax f s [a]
countS :: forall f s a.
Uncons f s =>
Int -> ParserSyntax f s a -> ParserSyntax f s [a]
countS Int
n ParserSyntax f s a
p
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = [a] -> ParserSyntax f s [a]
forall a. a -> ParserSyntax f s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
  | Bool
otherwise = (:) (a -> [a] -> [a])
-> ParserSyntax f s a -> ParserSyntax f s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserSyntax f s a
p ParserSyntax f s ([a] -> [a])
-> ParserSyntax f s [a] -> ParserSyntax f s [a]
forall a b.
ParserSyntax f s (a -> b)
-> ParserSyntax f s a -> ParserSyntax f s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> ParserSyntax f s a -> ParserSyntax f s [a]
forall f s a.
Uncons f s =>
Int -> ParserSyntax f s a -> ParserSyntax f s [a]
countS (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ParserSyntax f s a
p

sepByS :: (Uncons f s) => ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s [a]
sepByS :: forall f s a b.
Uncons f s =>
ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s [a]
sepByS ParserSyntax f s a
p ParserSyntax f s b
sep = ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s [a]
forall f s a b.
Uncons f s =>
ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s [a]
sepBy1S ParserSyntax f s a
p ParserSyntax f s b
sep ParserSyntax f s [a]
-> ParserSyntax f s [a] -> ParserSyntax f s [a]
forall a.
ParserSyntax f s a -> ParserSyntax f s a -> ParserSyntax f s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> ParserSyntax f s [a]
forall a. a -> ParserSyntax f s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

sepBy1S :: (Uncons f s) => ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s [a]
sepBy1S :: forall f s a b.
Uncons f s =>
ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s [a]
sepBy1S ParserSyntax f s a
p ParserSyntax f s b
sep = ParserSyntax f s a
p ParserSyntax f s a
-> (a -> ParserSyntax f s [a]) -> ParserSyntax f s [a]
forall a b.
ParserSyntax f s a
-> (a -> ParserSyntax f s b) -> ParserSyntax f s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x -> ParserSyntax f s a -> ParserSyntax f s [a]
forall f s a. ParserSyntax f s a -> ParserSyntax f s [a]
manyS (ParserSyntax f s a -> ParserSyntax f s a
forall f s a. ParserSyntax f s a -> ParserSyntax f s a
tryS (ParserSyntax f s b
sep ParserSyntax f s b -> ParserSyntax f s a -> ParserSyntax f s a
forall a b.
ParserSyntax f s a -> ParserSyntax f s b -> ParserSyntax f s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParserSyntax f s a
p)) ParserSyntax f s [a]
-> ([a] -> ParserSyntax f s [a]) -> ParserSyntax f s [a]
forall a b.
ParserSyntax f s a
-> (a -> ParserSyntax f s b) -> ParserSyntax f s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[a]
xs -> [a] -> ParserSyntax f s [a]
forall a. a -> ParserSyntax f s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs)

withOptionS ::
  (Uncons f s) =>
  ParserSyntax f s a ->
  (a -> ParserSyntax f s b) ->
  ParserSyntax f s b ->
  ParserSyntax f s b
withOptionS :: forall f s a b.
Uncons f s =>
ParserSyntax f s a
-> (a -> ParserSyntax f s b)
-> ParserSyntax f s b
-> ParserSyntax f s b
withOptionS ParserSyntax f s a
p a -> ParserSyntax f s b
f ParserSyntax f s b
def = (ParserSyntax f s a
p ParserSyntax f s a
-> (a -> ParserSyntax f s b) -> ParserSyntax f s b
forall a b.
ParserSyntax f s a
-> (a -> ParserSyntax f s b) -> ParserSyntax f s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ParserSyntax f s b
f) ParserSyntax f s b -> ParserSyntax f s b -> ParserSyntax f s b
forall a.
ParserSyntax f s a -> ParserSyntax f s a -> ParserSyntax f s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParserSyntax f s b
def

-- ---------------------------------------------------------------------------
-- Static analysis
-- ---------------------------------------------------------------------------

-- | Possible first tokens of a parser, plus whether it can succeed without
-- consuming input.
data FirstSet s = FirstSet
  { forall s. FirstSet s -> Bool
nullable :: Bool,
    forall s. FirstSet s -> FirstKind s
firstKind :: FirstKind s
  }

-- | Classification of a first-set.
data FirstKind s
  = FSNone
  | FSPred (s -> Bool)
  | FSUniversal

instance Show (FirstSet s) where
  show :: FirstSet s -> String
show FirstSet s
fs =
    String
"FirstSet {nullable = "
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ Bool -> String
forall a. Show a => a -> String
show (FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable FirstSet s
fs)
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", firstKind = "
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ FirstKind s -> String
forall {s}. FirstKind s -> String
showKind (FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
fs)
      String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"}"
    where
      showKind :: FirstKind s -> String
showKind FirstKind s
FSNone = String
"FSNone"
      showKind (FSPred s -> Bool
_) = String
"FSPred <function>"
      showKind FirstKind s
FSUniversal = String
"FSUniversal"

-- | Nullable first-set.
nullFS :: FirstSet s
nullFS :: forall s. FirstSet s
nullFS = Bool -> FirstKind s -> FirstSet s
forall s. Bool -> FirstKind s -> FirstSet s
FirstSet Bool
True FirstKind s
forall s. FirstKind s
FSNone

-- | First-set for a single token predicate.
predFS :: (s -> Bool) -> FirstSet s
predFS :: forall s. (s -> Bool) -> FirstSet s
predFS s -> Bool
p = Bool -> FirstKind s -> FirstSet s
forall s. Bool -> FirstKind s -> FirstSet s
FirstSet Bool
False ((s -> Bool) -> FirstKind s
forall s. (s -> Bool) -> FirstKind s
FSPred s -> Bool
p)

-- | Universal first-set.
universalFS :: FirstSet s
universalFS :: forall s. FirstSet s
universalFS = Bool -> FirstKind s -> FirstSet s
forall s. Bool -> FirstKind s -> FirstSet s
FirstSet Bool
False FirstKind s
forall s. FirstKind s
FSUniversal

-- | Union of two first-sets.
unionFS :: FirstSet s -> FirstSet s -> FirstSet s
unionFS :: forall s. FirstSet s -> FirstSet s -> FirstSet s
unionFS FirstSet s
x FirstSet s
y =
  FirstSet
    { nullable :: Bool
nullable = FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable FirstSet s
x Bool -> Bool -> Bool
|| FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable FirstSet s
y,
      firstKind :: FirstKind s
firstKind = case (FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
x, FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
y) of
        (FirstKind s
FSUniversal, FirstKind s
_) -> FirstKind s
forall s. FirstKind s
FSUniversal
        (FirstKind s
_, FirstKind s
FSUniversal) -> FirstKind s
forall s. FirstKind s
FSUniversal
        (FirstKind s
FSNone, FirstKind s
k) -> FirstKind s
k
        (FirstKind s
k, FirstKind s
FSNone) -> FirstKind s
k
        (FSPred s -> Bool
px, FSPred s -> Bool
py) -> (s -> Bool) -> FirstKind s
forall s. (s -> Bool) -> FirstKind s
FSPred (\s
c -> s -> Bool
px s
c Bool -> Bool -> Bool
|| s -> Bool
py s
c)
    }

-- | Sequential composition of first-sets.
seqFS :: FirstSet s -> FirstSet s -> FirstSet s
seqFS :: forall s. FirstSet s -> FirstSet s -> FirstSet s
seqFS FirstSet s
x FirstSet s
y =
  FirstSet
    { nullable :: Bool
nullable = FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable FirstSet s
x Bool -> Bool -> Bool
&& FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable FirstSet s
y,
      firstKind :: FirstKind s
firstKind = case (FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable FirstSet s
x, FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
x, FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
y) of
        (Bool
True, FirstKind s
FSNone, FirstKind s
k) -> FirstKind s
k
        (Bool
True, FirstKind s
k, FirstKind s
FSNone) -> FirstKind s
k
        (Bool
True, FSPred s -> Bool
px, FSPred s -> Bool
py) -> (s -> Bool) -> FirstKind s
forall s. (s -> Bool) -> FirstKind s
FSPred (\s
c -> s -> Bool
px s
c Bool -> Bool -> Bool
|| s -> Bool
py s
c)
        (Bool
True, FirstKind s
FSUniversal, FirstKind s
_) -> FirstKind s
forall s. FirstKind s
FSUniversal
        (Bool
True, FirstKind s
_, FirstKind s
FSUniversal) -> FirstKind s
forall s. FirstKind s
FSUniversal
        (Bool
False, FirstKind s
k, FirstKind s
_) -> FirstKind s
k
    }

-- | Compute the first-set of an arbitrary syntax subtree.
firstSetSyntax :: Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax :: forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax (Lift Body (,) f (->) x y
_) = FirstSet s
forall s. FirstSet s
nullFS
firstSetSyntax (Op ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
op) = case ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
op of
  L (SigCompose Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f) -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1 -> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f FirstSet s -> FirstSet s -> FirstSet s
forall s. FirstSet s -> FirstSet s -> FirstSet s
`seqFS` Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y -> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
g
  R (L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
prim) -> case SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
prim of
    SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
PrimNext -> FirstSet s
forall s. FirstSet s
universalFS
    PrimSatisfy s -> Bool
p -> (s -> Bool) -> FirstSet s
forall s. (s -> Bool) -> FirstSet s
predFS s -> Bool
p
    PrimChar s
c -> (s -> Bool) -> FirstSet s
forall s. (s -> Bool) -> FirstSet s
predFS (s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
c)
    PrimString [] -> FirstSet s
forall s. FirstSet s
nullFS
    PrimString (s
c : [s]
_) -> (s -> Bool) -> FirstSet s
forall s. (s -> Bool) -> FirstSet s
predFS (s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
c)
    SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
PrimEndOfInput -> FirstSet s
forall s. FirstSet s
nullFS
    SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
PrimTakeRest -> FirstSet s
forall s. FirstSet s
universalFS
  R (R SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
comb) -> case SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
comb of
    CombAp Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa -> Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf FirstSet s -> FirstSet s -> FirstSet s
forall s. FirstSet s -> FirstSet s -> FirstSet s
`seqFS` Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa
    CombBind Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
_ a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
_ -> FirstSet s
forall s. FirstSet s
nullFS
    CombAlt Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2 -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 FirstSet s -> FirstSet s -> FirstSet s
forall s. FirstSet s -> FirstSet s -> FirstSet s
`unionFS` Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2
    CombMany Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> Bool -> FirstKind s -> FirstSet s
forall s. Bool -> FirstKind s -> FirstSet s
FirstSet Bool
True (FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p))
    CombTry Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p
    CombFmap a -> b
_ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p

-- | Compute the first-set of a parser syntax tree.
firstSet :: ParserSyntax f s a -> FirstSet s
firstSet :: forall f s a. ParserSyntax f s a -> FirstSet s
firstSet = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> FirstSet s)
-> (ParserSyntax f s a
    -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> ParserSyntax f s a
-> FirstSet s
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall k (arr :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category arr =>
arr b c -> arr a b -> arr a c
. ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
unParserSyntax

-- | Detect unreachable branches in choice nodes. A branch is unreachable when
-- the left side can consume any token that the right side can consume.
unreachableBranches :: ParserSyntax f s a -> [String]
unreachableBranches :: forall f s a. ParserSyntax f s a -> [String]
unreachableBranches = [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [] (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> [String])
-> (ParserSyntax f s a
    -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> ParserSyntax f s a
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall k (arr :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category arr =>
arr b c -> arr a b -> arr a c
. ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
unParserSyntax
  where
    go :: [FirstSet s] -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
    go :: forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
_ (Lift Body (,) f (->) x y
_) = []
    go [FirstSet s]
acc (Op ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
op) = case ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
op of
      L (SigCompose Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f) ->
        [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1 -> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
acc Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y -> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go ([FirstSet s]
acc [FirstSet s] -> [FirstSet s] -> [FirstSet s]
forall a. [a] -> [a] -> [a]
++ [Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1 -> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f]) Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
g
      R (L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
_) -> []
      R (R SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
comb) -> case SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
comb of
        CombAlt Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2 ->
          let left :: FirstSet s
left = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1
              rights :: [FirstSet s]
rights = [FirstSet s]
acc [FirstSet s] -> [FirstSet s] -> [FirstSet s]
forall a. [a] -> [a] -> [a]
++ [FirstSet s
left]
           in FirstSet s -> FirstSet s -> [String]
forall s. FirstSet s -> FirstSet s -> [String]
check FirstSet s
left (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> FirstSet s
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> FirstSet s
firstSetSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2)
                [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
rights Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1
                [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
rights Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2
        CombAp Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa -> [FirstSet s]
-> Syntax
     (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
acc Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
acc Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa
        CombBind Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
_ a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
_ -> []
        CombMany Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
acc Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p
        CombTry Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
acc Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p
        CombFmap a -> b
_ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> [FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> [String]
forall s f x y.
[FirstSet s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> [String]
go [FirstSet s]
acc Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p

    check :: FirstSet s -> FirstSet s -> [String]
    check :: forall s. FirstSet s -> FirstSet s -> [String]
check FirstSet s
left FirstSet s
right =
      case (FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
left, FirstSet s -> FirstKind s
forall s. FirstSet s -> FirstKind s
firstKind FirstSet s
right) of
        (FirstKind s
FSUniversal, FSPred s -> Bool
_) ->
          [String
"right branch can never fire (left consumes any token)"]
        (FirstKind s
FSUniversal, FirstKind s
FSUniversal) ->
          [String
"right branch can never fire (left consumes any token)"]
        (FirstKind s, FirstKind s)
_ -> []

-- ---------------------------------------------------------------------------
-- Regex extraction
-- ---------------------------------------------------------------------------

-- | A simple regular-expression AST.
data Regex s
  = REEmpty
  | REAny
  | REChar s
  | REString [s]
  | REClass (s -> Bool)
  | REAlt (Regex s) (Regex s)
  | RESeq (Regex s) (Regex s)
  | REStar (Regex s)

instance (Show s) => Show (Regex s) where
  show :: Regex s -> String
show Regex s
REEmpty = String
"REEmpty"
  show Regex s
REAny = String
"REAny"
  show (REChar s
c) = String
"REChar " String -> ShowS
forall a. [a] -> [a] -> [a]
++ s -> String
forall a. Show a => a -> String
show s
c
  show (REString [s]
cs) = String
"REString " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [s] -> String
forall a. Show a => a -> String
show [s]
cs
  show (REClass s -> Bool
_) = String
"REClass <function>"
  show (REAlt Regex s
r1 Regex s
r2) = String
"REAlt (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Regex s -> String
forall a. Show a => a -> String
show Regex s
r1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
") (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Regex s -> String
forall a. Show a => a -> String
show Regex s
r2 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
  show (RESeq Regex s
r1 Regex s
r2) = String
"RESeq (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Regex s -> String
forall a. Show a => a -> String
show Regex s
r1 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
") (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Regex s -> String
forall a. Show a => a -> String
show Regex s
r2 String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
  show (REStar Regex s
r) = String
"REStar (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Regex s -> String
forall a. Show a => a -> String
show Regex s
r String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"

-- | Extract a regex from a syntax tree, if it is regular. Returns 'Nothing' for
-- primitives or combinators that cannot be expressed as regular expressions.
toRegex :: ParserSyntax f s a -> Maybe (Regex s)
toRegex :: forall f s a. ParserSyntax f s a -> Maybe (Regex s)
toRegex = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
 -> Maybe (Regex s))
-> (ParserSyntax f s a
    -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f))
-> ParserSyntax f s a
-> Maybe (Regex s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall k (arr :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category arr =>
arr b c -> arr a b -> arr a c
. ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
ParserSyntax f s a
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
unParserSyntax
  where
    go :: Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y -> Maybe (Regex s)
    go :: forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go (Lift Body (,) f (->) x y
_) = Regex s -> Maybe (Regex s)
forall a. a -> Maybe a
Just Regex s
forall s. Regex s
REEmpty
    go (Op ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
op) = case ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
op of
      L (SigCompose Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f) -> Regex s -> Regex s -> Regex s
forall s. Regex s -> Regex s -> Regex s
RESeq (Regex s -> Regex s -> Regex s)
-> Maybe (Regex s) -> Maybe (Regex s -> Regex s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x b1
f Maybe (Regex s -> Regex s) -> Maybe (Regex s) -> Maybe (Regex s)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 y
g
      R (L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
prim) -> case SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
prim of
        SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
PrimNext -> Regex s -> Maybe (Regex s)
forall a. a -> Maybe a
Just Regex s
forall s. Regex s
REAny
        PrimSatisfy s -> Bool
p -> Regex s -> Maybe (Regex s)
forall a. a -> Maybe a
Just ((s -> Bool) -> Regex s
forall s. (s -> Bool) -> Regex s
REClass s -> Bool
p)
        PrimChar s
c -> Regex s -> Maybe (Regex s)
forall a. a -> Maybe a
Just (s -> Regex s
forall s. s -> Regex s
REChar s
c)
        PrimString [s]
cs -> Regex s -> Maybe (Regex s)
forall a. a -> Maybe a
Just ([s] -> Regex s
forall s. [s] -> Regex s
REString [s]
cs)
        SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
PrimEndOfInput -> Regex s -> Maybe (Regex s)
forall a. a -> Maybe a
Just Regex s
forall s. Regex s
REEmpty
        SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
PrimTakeRest -> Maybe (Regex s)
forall a. Maybe a
Nothing
      R (R SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
comb) -> case SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  x
  y
comb of
        CombAp Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa -> Regex s -> Regex s -> Regex s
forall s. Regex s -> Regex s -> Regex s
RESeq (Regex s -> Regex s -> Regex s)
-> Maybe (Regex s) -> Maybe (Regex s -> Regex s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf Maybe (Regex s -> Regex s) -> Maybe (Regex s) -> Maybe (Regex s)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa
        CombBind Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
_ a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
_ -> Maybe (Regex s)
forall a. Maybe a
Nothing
        CombAlt Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2 -> Regex s -> Regex s -> Regex s
forall s. Regex s -> Regex s -> Regex s
REAlt (Regex s -> Regex s -> Regex s)
-> Maybe (Regex s) -> Maybe (Regex s -> Regex s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 Maybe (Regex s -> Regex s) -> Maybe (Regex s) -> Maybe (Regex s)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2
        CombMany Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> Regex s -> Regex s
forall s. Regex s -> Regex s
REStar (Regex s -> Regex s) -> Maybe (Regex s) -> Maybe (Regex s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p
        CombTry Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p
        CombFmap a -> b
_ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Maybe (Regex s)
forall f s x y.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) x y
-> Maybe (Regex s)
go Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p

-- ---------------------------------------------------------------------------
-- Brzozowski derivatives
-- ---------------------------------------------------------------------------

-- | The parser that remains after consuming one token.
--
-- This is the core of the coalgebraic compiler: a 'Process' machine state is a
-- parser syntax tree, and consuming a token transitions to its derivative.
-- The implementation covers the applicative + alternative + 'many' fragment;
-- 'CombBind' is rejected because it is dependent composition.
derive :: (Eq s, Uncons f s) => s -> ParserSyntax f s a -> ParserSyntax f s a
derive :: forall s f a.
(Eq s, Uncons f s) =>
s -> ParserSyntax f s a -> ParserSyntax f s a
derive s
c (ParserSyntax Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn) = Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax (s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn)

-- | Syntax-level derivative.
deriveSyntax ::
  forall s f a.
  (Eq s, Uncons f s) =>
  s ->
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f) ->
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax :: forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn = case Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn of
  Lift Body (,) f (->) () (These a f)
_ -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
  Op ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
op -> case ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
op of
    L (SigCompose Syntax (ParserSyntaxSig f s) (Body (,) f (->)) b1 (These a f)
_ Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () b1
_) ->
      String
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a. HasCallStack => String -> a
error String
"deriveSyntax: explicit SigCompose not supported; use fmap/Applicative/Alternative constructors"
    R (L SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
prim) -> s
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
Eq s =>
s
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
derivePrim s
c SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
prim
    R (R SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
comb) -> s
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveComb s
c SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
comb

-- | Derivative of a primitive.
derivePrim ::
  forall s f a.
  (Eq s) =>
  s ->
  SigPrim f s (Body (,) f (->)) (Syntax (ParserSyntaxSig f s) (Body (,) f (->))) () (These a f) ->
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
derivePrim :: forall s f a.
Eq s =>
s
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
derivePrim s
c SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
prim = case SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
prim of
  SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
PrimNext -> a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a f s.
a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pureSyntax s
a
c
  PrimSatisfy s -> Bool
p -> if s -> Bool
p s
c then a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a f s.
a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pureSyntax s
a
c else Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
  PrimChar s
d -> if s
c s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
d then a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a f s.
a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pureSyntax s
a
c else Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
  PrimString [] -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
  PrimString (s
d : [s]
ds) ->
    if s
c s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
d
      then ([s] -> a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a b f s.
(a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
fmapSyntax (s
d s -> [s] -> [s]
forall a. a -> [a] -> [a]
:) ([s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
forall s f.
Eq s =>
[s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
stringSyntax [s]
ds)
      else Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
  SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
PrimEndOfInput -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
  SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
PrimTakeRest -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax

-- | Derivative of a combinator.
deriveComb ::
  forall s f a.
  (Eq s, Uncons f s) =>
  s ->
  SigComb f s (Body (,) f (->)) (Syntax (ParserSyntaxSig f s) (Body (,) f (->))) () (These a f) ->
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveComb :: forall s f a.
(Eq s, Uncons f s) =>
s
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveComb s
c SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
comb = case SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
comb of
  CombAp Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa ->
    let left :: Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
left = (:+:)
  SigCompose
  (SigPrim f s :+: SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> (:+:)
     SigCompose
     (SigPrim f s :+: SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall (rec :: * -> * -> *) a b f s (arr :: * -> * -> *).
rec () (These (a -> b) f)
-> rec () (These a f) -> SigComb f s arr rec () (These b f)
CombAp (s
-> Syntax
     (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> Syntax
     (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf) Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa)))
        right :: Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
right = case ParserSyntax f s (a -> b) -> Maybe (a -> b)
forall f s a. Uncons f s => ParserSyntax f s a -> Maybe a
nullableValue (Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
-> ParserSyntax f s (a -> b)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> ParserSyntax f s a
ParserSyntax Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These (a -> b) f)
pf) of
          Just a -> b
g -> (a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall a b f s.
(a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
fmapSyntax a -> b
g (s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pa)
          Maybe (a -> b)
Nothing -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax
     in ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall (rec :: * -> * -> *) a f s (arr :: * -> * -> *).
rec () (These a f)
-> rec () (These a f) -> SigComb f s arr rec () (These a f)
CombAlt Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
left Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
right)))
  CombBind Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
_ a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
_ -> String
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a. HasCallStack => String -> a
error String
"deriveComb: CombBind not supported"
  CombAlt Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1 Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2 ->
    ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall (rec :: * -> * -> *) a f s (arr :: * -> * -> *).
rec () (These a f)
-> rec () (These a f) -> SigComb f s arr rec () (These a f)
CombAlt (s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p1) (s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p2))))
  CombMany Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p ->
    ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op
      ( (:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R
          ( SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These a f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R
              ( Syntax
  (ParserSyntaxSig f s) (Body (,) f (->)) () (These ([a] -> a) f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These a f)
forall (rec :: * -> * -> *) a b f s (arr :: * -> * -> *).
rec () (These (a -> b) f)
-> rec () (These a f) -> SigComb f s arr rec () (These b f)
CombAp
                  ((a -> [a] -> a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax
     (ParserSyntaxSig f s) (Body (,) f (->)) () (These ([a] -> a) f)
forall a b f s.
(a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
fmapSyntax (:) (s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p))
                  (ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [a] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [a] f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [a] f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [a] f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [a] f)
forall (rec :: * -> * -> *) a f s (arr :: * -> * -> *).
rec () (These a f) -> SigComb f s arr rec () (These [a] f)
CombMany Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p))))
              )
          )
      )
  CombTry Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p
  CombFmap a -> b
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p -> (a -> a)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall a b f s.
(a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
fmapSyntax a -> a
a -> b
g (s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall s f a.
(Eq s, Uncons f s) =>
s
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
deriveSyntax s
c Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
p)

-- | Syntax tree that always fails, returning the input stream unchanged.
emptyResultSyntax :: Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax :: forall f s a.
Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
emptyResultSyntax = Body (,) f (->) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (arr :: * -> * -> *) a b (sig :: Sig).
arr a b -> Syntax sig arr a b
Lift (((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f)
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
       (arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (\(f
st, ()) -> (f
st, f -> These a f
forall a b. b -> These a b
That f
st)))

-- | Syntax tree that returns a constant value without consuming input.
pureSyntax :: a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pureSyntax :: forall a f s.
a -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
pureSyntax a
a = Body (,) f (->) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
forall (arr :: * -> * -> *) a b (sig :: Sig).
arr a b -> Syntax sig arr a b
Lift (((f, ()) -> (f, These a f)) -> Body (,) f (->) () (These a f)
forall {k} {k1} {k2} (t :: k -> k1 -> k2) (ch :: k)
       (arr :: k2 -> k2 -> *) (a :: k1) (b :: k1).
arr (t ch a) (t ch b) -> Body t ch arr a b
Body (\(f
st, ()) -> (f
st, a -> f -> These a f
forall a b. a -> b -> These a b
These a
a f
st)))

-- | Lift a pure function over the result of a syntax tree using the structural
-- 'CombFmap' node.
fmapSyntax ::
  (a -> b) ->
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f) ->
  Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
fmapSyntax :: forall a b f s.
(a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
fmapSyntax a -> b
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn = ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These b f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigComb
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These b f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R ((a -> b)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
-> SigComb
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These b f)
forall a b (rec :: * -> * -> *) f s (arr :: * -> * -> *).
(a -> b)
-> rec () (These a f) -> SigComb f s arr rec () (These b f)
CombFmap a -> b
g Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These a f)
syn)))

-- | Syntax tree for matching a fixed string.
stringSyntax ::
  (Eq s) => [s] -> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
stringSyntax :: forall s f.
Eq s =>
[s]
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
stringSyntax [s]
cs = ParserSyntaxSig
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [s] f)
-> Syntax (ParserSyntaxSig f s) (Body (,) f (->)) () (These [s] f)
forall (sig :: Sig) (arr :: * -> * -> *) a b.
sig arr (Syntax sig arr) a b -> Syntax sig arr a b
Op ((:+:)
  (SigPrim f s)
  (SigComb f s)
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [s] f)
-> ParserSyntaxSig
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall {k} {k1} {k2} {k3} (sig2 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig1 :: k -> k1 -> k2 -> k3 -> *).
sig2 arr rec a b -> (:+:) sig1 sig2 arr rec a b
R (SigPrim
  f
  s
  (Body (,) f (->))
  (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
  ()
  (These [s] f)
-> (:+:)
     (SigPrim f s)
     (SigComb f s)
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall {k} {k1} {k2} {k3} (sig1 :: k -> k1 -> k2 -> k3 -> *)
       (arr :: k) (rec :: k1) (a :: k2) (b :: k3)
       (sig2 :: k -> k1 -> k2 -> k3 -> *).
sig1 arr rec a b -> (:+:) sig1 sig2 arr rec a b
L ([s]
-> SigPrim
     f
     s
     (Body (,) f (->))
     (Syntax (ParserSyntaxSig f s) (Body (,) f (->)))
     ()
     (These [s] f)
forall s f (arr :: * -> * -> *) (rec :: * -> * -> *).
Eq s =>
[s] -> SigPrim f s arr rec () (These [s] f)
PrimString [s]
cs)))

-- | Check whether a parser can succeed without consuming any input, and if
-- so extract the value it would return.
nullableValue :: forall f s a. (Uncons f s) => ParserSyntax f s a -> Maybe a
nullableValue :: forall f s a. Uncons f s => ParserSyntax f s a -> Maybe a
nullableValue ParserSyntax f s a
p
  | FirstSet s -> Bool
forall s. FirstSet s -> Bool
nullable (ParserSyntax f s a -> FirstSet s
forall f s a. ParserSyntax f s a -> FirstSet s
firstSet ParserSyntax f s a
p) =
      case ParserSyntax f s a -> f -> These a f
forall f s a. Uncons f s => ParserSyntax f s a -> f -> These a f
runParserSyntaxIdentity ParserSyntax f s a
p (forall f s. Uncons f s => f
nil @f @s) of
        These a
a f
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
        These a f
_ -> Maybe a
forall a. Maybe a
Nothing
  | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing