-- The default type signature of type class methods are changed
-- to introduce a Liftable constraint and the same type class but on the 'Output' repr,
-- this setup avoids to define the method with boilerplate code when its default
-- definition with lift* and 'trans' does what is expected by an instance
-- of the type class. This is almost as explained in:
-- https://ro-che.info/articles/2016-02-03-finally-tagless-boilerplate
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveLift #-} -- For TH.Lift (ErrorItem tok)
{-# LANGUAGE StandaloneDeriving #-} -- For Show (ErrorItem (InputToken inp))
{-# LANGUAGE TemplateHaskell #-}
-- | Semantic of the grammar combinators used to express parsers,
-- in the convenient tagless-final encoding.
module Symantic.Parser.Grammar.Combinators where

import Data.Bool (Bool(..), not, (||))
import Data.Char (Char)
import Data.Either (Either(..))
import Data.Eq (Eq(..))
import Data.Function ((.), flip, const)
import Data.Int (Int)
import Data.Maybe (Maybe(..))
import Data.Ord (Ord)
import Data.String (String)
import Text.Show (Show(..))
import qualified Data.Functor as Functor
import qualified Data.List as List
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH

import qualified Symantic.Univariant.Trans as Sym
import qualified Symantic.Parser.Haskell as H

-- * Type 'TermGrammar'
type TermGrammar = H.Term H.ValueCode

-- * Class 'Applicable'
-- | This is like the usual 'Functor' and 'Applicative' type classes
-- from the @base@ package, but using @('TermGrammar' a)@ instead of just @(a)@
-- to be able to use and pattern match on some usual terms of type @(a)@ (like 'H.id')
-- and thus apply some optimizations.
-- @(repr)@, for "representation", is the usual tagless-final abstraction
-- over the many semantics that this syntax (formed by the methods
-- of type class like this one) will be interpreted.
class Applicable repr where
  -- | @(a2b '<$>' ra)@ parses like @(ra)@ but maps its returned value with @(a2b)@.
  (<$>) :: TermGrammar (a -> b) -> repr a -> repr b
  (<$>) TermGrammar (a -> b)
f = (TermGrammar (a -> b) -> repr (a -> b)
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar (a -> b)
f repr (a -> b) -> repr a -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*>)

  -- | Like '<$>' but with its arguments 'flip'-ped.
  (<&>) :: repr a -> TermGrammar (a -> b) -> repr b
  (<&>) = (TermGrammar (a -> b) -> repr a -> repr b)
-> repr a -> TermGrammar (a -> b) -> repr b
forall a b c. (a -> b -> c) -> b -> a -> c
flip TermGrammar (a -> b) -> repr a -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
(<$>)

  -- | @(a '<$' rb)@ parses like @(rb)@ but discards its returned value by replacing it with @(a)@.
  (<$) :: TermGrammar a -> repr b -> repr a
  (<$) TermGrammar a
x = (TermGrammar a -> repr a
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar a
x repr a -> repr b -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr a
<*)

  -- | @(ra '$>' b)@ parses like @(ra)@ but discards its returned value by replacing it with @(b)@.
  ($>) :: repr a -> TermGrammar b -> repr b
  ($>) = (TermGrammar b -> repr a -> repr b)
-> repr a -> TermGrammar b -> repr b
forall a b c. (a -> b -> c) -> b -> a -> c
flip TermGrammar b -> repr a -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar a -> repr b -> repr a
(<$)

  -- | @('pure' a)@ parses the empty string, always succeeding in returning @(a)@.
  pure :: TermGrammar a -> repr a
  default pure ::
    Sym.Liftable repr => Applicable (Sym.Output repr) =>
    TermGrammar a -> repr a
  pure = Output repr a -> repr a
forall (repr :: * -> *) a. Liftable repr => Output repr a -> repr a
Sym.lift (Output repr a -> repr a)
-> (TermGrammar a -> Output repr a) -> TermGrammar a -> repr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermGrammar a -> Output repr a
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure

  -- | @(ra2b '<*>' ra)@ parses sequentially @(ra2b)@ and then @(ra)@,
  -- and returns the application of the function returned by @(ra2b)@
  -- to the value returned by @(ra)@.
  (<*>) :: repr (a -> b) -> repr a -> repr b
  default (<*>) ::
    Sym.Liftable2 repr => Applicable (Sym.Output repr) =>
    repr (a -> b) -> repr a -> repr b
  (<*>) = (Output repr (a -> b) -> Output repr a -> Output repr b)
-> repr (a -> b) -> repr a -> repr b
forall (repr :: * -> *) a b c.
Liftable2 repr =>
(Output repr a -> Output repr b -> Output repr c)
-> repr a -> repr b -> repr c
Sym.lift2 Output repr (a -> b) -> Output repr a -> Output repr b
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
(<*>)

  -- | @('liftA2' a2b2c ra rb)@ parses sequentially @(ra)@ and then @(rb)@,
  -- and returns the application of @(a2b2c)@ to the values returned by those parsers.
  liftA2 :: TermGrammar (a -> b -> c) -> repr a -> repr b -> repr c
  liftA2 TermGrammar (a -> b -> c)
f repr a
x = repr (b -> c) -> repr b -> repr c
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
(<*>) (TermGrammar (a -> b -> c)
f TermGrammar (a -> b -> c) -> repr a -> repr (b -> c)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr a
x)

  -- | @(ra '<*' rb)@ parses sequentially @(ra)@ and then @(rb)@,
  -- and returns like @(ra)@, discarding the return value of @(rb)@.
  (<*) :: repr a -> repr b -> repr a
  (<*) = TermGrammar (a -> b -> a) -> repr a -> repr b -> repr a
forall (repr :: * -> *) a b c.
Applicable repr =>
TermGrammar (a -> b -> c) -> repr a -> repr b -> repr c
liftA2 TermGrammar (a -> b -> a)
forall (repr :: * -> *) a b. Termable repr => repr (a -> b -> a)
H.const

  -- | @(ra '*>' rb)@ parses sequentially @(ra)@ and then @(rb)@,
  -- and returns like @(rb)@, discarding the return value of @(ra)@.
  (*>) :: repr a -> repr b -> repr b
  repr a
x *> repr b
y = (Term ValueCode (b -> b)
forall (repr :: * -> *) a. Termable repr => repr (a -> a)
H.id Term ValueCode (b -> b) -> repr a -> repr (b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar a -> repr b -> repr a
<$ repr a
x) repr (b -> b) -> repr b -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr b
y

  -- | Like '<*>' but with its arguments 'flip'-ped.
  (<**>) :: repr a -> repr (a -> b) -> repr b
  (<**>) = TermGrammar (a -> (a -> b) -> b)
-> repr a -> repr (a -> b) -> repr b
forall (repr :: * -> *) a b c.
Applicable repr =>
TermGrammar (a -> b -> c) -> repr a -> repr b -> repr c
liftA2 (Term ValueCode (((a -> b) -> a -> b) -> a -> (a -> b) -> b)
forall (repr :: * -> *) a b c.
Termable repr =>
repr ((a -> b -> c) -> b -> a -> c)
H.flip Term ValueCode (((a -> b) -> a -> b) -> a -> (a -> b) -> b)
-> Term ValueCode ((a -> b) -> a -> b)
-> TermGrammar (a -> (a -> b) -> b)
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ Term ValueCode ((a -> b) -> a -> b)
forall (repr :: * -> *) a b.
Termable repr =>
repr ((a -> b) -> a -> b)
(H.$))
  {-
  (<**>) :: repr a -> repr (a -> b) -> repr b
  (<**>) = liftA2 (\a f -> f a)
  -}
infixl 4 <$>, <&>, <$, $>, <*>, <*, *>, <**>

-- * Class 'Alternable'
class Alternable repr where
  -- | @(rl '<|>' rr)@ parses @(rl)@ and return its return value or,
  -- if it fails, parses @(rr)@ from where @(rl)@ has left the input stream,
  -- and returns its return value.
  (<|>) :: repr a -> repr a -> repr a
  -- | @(empty)@ parses nothing, always failing to return a value.
  empty :: repr a
  -- | @('try' ra)@ records the input stream position,
  -- then parses like @(ra)@ and either returns its value it it succeeds or fails
  -- if it fails but with a reset of the input stream to the recorded position.
  -- Generally used on the first alternative: @('try' rl '<|>' rr)@.
  try :: repr a -> repr a
  default (<|>) ::
    Sym.Liftable2 repr => Alternable (Sym.Output repr) =>
    repr a -> repr a -> repr a
  default empty ::
    Sym.Liftable repr => Alternable (Sym.Output repr) =>
    repr a
  default try ::
    Sym.Liftable1 repr => Alternable (Sym.Output repr) =>
    repr a -> repr a
  (<|>) = (Output repr a -> Output repr a -> Output repr a)
-> repr a -> repr a -> repr a
forall (repr :: * -> *) a b c.
Liftable2 repr =>
(Output repr a -> Output repr b -> Output repr c)
-> repr a -> repr b -> repr c
Sym.lift2 Output repr a -> Output repr a -> Output repr a
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
(<|>)
  empty = Output repr a -> repr a
forall (repr :: * -> *) a. Liftable repr => Output repr a -> repr a
Sym.lift Output repr a
forall (repr :: * -> *) a. Alternable repr => repr a
empty
  try = (Output repr a -> Output repr a) -> repr a -> repr a
forall (repr :: * -> *) a b.
Liftable1 repr =>
(Output repr a -> Output repr b) -> repr a -> repr b
Sym.lift1 Output repr a -> Output repr a
forall (repr :: * -> *) a. Alternable repr => repr a -> repr a
try
  -- | Like @('<|>')@ but with different returning types for the alternatives,
  -- and a return value wrapped in an 'Either' accordingly.
  (<+>) :: Applicable repr => Alternable repr => repr a -> repr b -> repr (Either a b)
  repr a
p <+> repr b
q = Term ValueCode (a -> Either a b)
forall (repr :: * -> *) l r.
Termable repr =>
repr (l -> Either l r)
H.left Term ValueCode (a -> Either a b) -> repr a -> repr (Either a b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr a
p repr (Either a b) -> repr (Either a b) -> repr (Either a b)
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> Term ValueCode (b -> Either a b)
forall (repr :: * -> *) r l.
Termable repr =>
repr (r -> Either l r)
H.right Term ValueCode (b -> Either a b) -> repr b -> repr (Either a b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr b
q
infixl 3 <|>, <+>

optionally :: Applicable repr => Alternable repr => repr a -> TermGrammar b -> repr b
optionally :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr) =>
repr a -> TermGrammar b -> repr b
optionally repr a
p TermGrammar b
x = repr a
p repr a -> TermGrammar b -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> TermGrammar b -> repr b
$> TermGrammar b
x repr b -> repr b -> repr b
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> TermGrammar b -> repr b
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar b
x

optional :: Applicable repr => Alternable repr => repr a -> repr ()
optional :: forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
repr a -> repr ()
optional = (repr a -> TermGrammar () -> repr ())
-> TermGrammar () -> repr a -> repr ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip repr a -> TermGrammar () -> repr ()
forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr) =>
repr a -> TermGrammar b -> repr b
optionally TermGrammar ()
forall (repr :: * -> *). Termable repr => repr ()
H.unit

option :: Applicable repr => Alternable repr => TermGrammar a -> repr a -> repr a
option :: forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
TermGrammar a -> repr a -> repr a
option TermGrammar a
x repr a
p = repr a
p repr a -> repr a -> repr a
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> TermGrammar a -> repr a
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar a
x

choice :: Alternable repr => [repr a] -> repr a
choice :: forall (repr :: * -> *) a. Alternable repr => [repr a] -> repr a
choice = (repr a -> repr a -> repr a) -> repr a -> [repr a] -> repr a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr repr a -> repr a -> repr a
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
(<|>) repr a
forall (repr :: * -> *) a. Alternable repr => repr a
empty
 -- FIXME: Here hlint suggests to use Data.Foldable.asum,
 -- but at this point there is no asum for our own (<|>)

maybeP :: Applicable repr => Alternable repr => repr a -> repr (Maybe a)
maybeP :: forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
repr a -> repr (Maybe a)
maybeP repr a
p = TermGrammar (Maybe a) -> repr (Maybe a) -> repr (Maybe a)
forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
TermGrammar a -> repr a -> repr a
option TermGrammar (Maybe a)
forall (repr :: * -> *) a. Termable repr => repr (Maybe a)
H.nothing (Term ValueCode (a -> Maybe a)
forall (repr :: * -> *) a. Termable repr => repr (a -> Maybe a)
H.just Term ValueCode (a -> Maybe a) -> repr a -> repr (Maybe a)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr a
p)

manyTill :: Applicable repr => Alternable repr => repr a -> repr b -> repr [a]
manyTill :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr) =>
repr a -> repr b -> repr [a]
manyTill repr a
p repr b
end = let go :: repr [a]
go = repr b
end repr b -> TermGrammar [a] -> repr [a]
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> TermGrammar b -> repr b
$> TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil repr [a] -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> repr a
p repr a -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
Applicable repr =>
repr a -> repr [a] -> repr [a]
<:> repr [a]
go in repr [a]
go

-- * Class 'Selectable'
class Selectable repr where
  branch :: repr (Either a b) -> repr (a -> c) -> repr (b -> c) -> repr c
  default branch ::
    Sym.Liftable3 repr => Selectable (Sym.Output repr) =>
    repr (Either a b) -> repr (a -> c) -> repr (b -> c) -> repr c
  branch = (Output repr (Either a b)
 -> Output repr (a -> c) -> Output repr (b -> c) -> Output repr c)
-> repr (Either a b) -> repr (a -> c) -> repr (b -> c) -> repr c
forall (repr :: * -> *) a b c d.
Liftable3 repr =>
(Output repr a -> Output repr b -> Output repr c -> Output repr d)
-> repr a -> repr b -> repr c -> repr d
Sym.lift3 Output repr (Either a b)
-> Output repr (a -> c) -> Output repr (b -> c) -> Output repr c
forall (repr :: * -> *) a b c.
Selectable repr =>
repr (Either a b) -> repr (a -> c) -> repr (b -> c) -> repr c
branch

-- * Class 'Matchable'
class Matchable repr where
  conditional ::
    Eq a => repr a -> [TermGrammar (a -> Bool)] -> [repr b] -> repr b -> repr b
  default conditional ::
    Sym.Unliftable repr => Sym.Liftable1 repr => Matchable (Sym.Output repr) =>
    Eq a => repr a -> [TermGrammar (a -> Bool)] -> [repr b] -> repr b -> repr b
  conditional repr a
a [TermGrammar (a -> Bool)]
ps [repr b]
bs = (Output repr b -> Output repr b) -> repr b -> repr b
forall (repr :: * -> *) a b.
Liftable1 repr =>
(Output repr a -> Output repr b) -> repr a -> repr b
Sym.lift1 (Output repr a
-> [TermGrammar (a -> Bool)]
-> [Output repr b]
-> Output repr b
-> Output repr b
forall (repr :: * -> *) a b.
(Matchable repr, Eq a) =>
repr a -> [TermGrammar (a -> Bool)] -> [repr b] -> repr b -> repr b
conditional (repr a -> Output repr a
forall (from :: * -> *) (to :: * -> *) a.
Trans from to =>
from a -> to a
Sym.trans repr a
a) [TermGrammar (a -> Bool)]
ps (repr b -> Output repr b
forall (from :: * -> *) (to :: * -> *) a.
Trans from to =>
from a -> to a
Sym.trans (repr b -> Output repr b) -> [repr b] -> [Output repr b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Functor.<$> [repr b]
bs))

  match :: Eq a => repr a -> [TermGrammar a] -> (TermGrammar a -> repr b) -> repr b -> repr b
  match repr a
a [TermGrammar a]
as TermGrammar a -> repr b
a2b = repr a -> [TermGrammar (a -> Bool)] -> [repr b] -> repr b -> repr b
forall (repr :: * -> *) a b.
(Matchable repr, Eq a) =>
repr a -> [TermGrammar (a -> Bool)] -> [repr b] -> repr b -> repr b
conditional repr a
a ((Term ValueCode (a -> a -> Bool)
forall (repr :: * -> *) a.
(Termable repr, Eq a) =>
repr (a -> a -> Bool)
H.eq Term ValueCode (a -> a -> Bool)
-> TermGrammar a -> TermGrammar (a -> Bool)
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@) (TermGrammar a -> TermGrammar (a -> Bool))
-> [TermGrammar a] -> [TermGrammar (a -> Bool)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Functor.<$> [TermGrammar a]
as) (TermGrammar a -> repr b
a2b (TermGrammar a -> repr b) -> [TermGrammar a] -> [repr b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Functor.<$> [TermGrammar a]
as)
  -- match a as a2b = conditional a (((H.eq H..@ H.qual) H..@) Functor.<$> as) (a2b Functor.<$> as)

-- * Class 'Foldable'
class Foldable repr where
  chainPre :: repr (a -> a) -> repr a -> repr a
  chainPost :: repr a -> repr (a -> a) -> repr a
  {-
  default chainPre ::
    Sym.Liftable2 repr => Foldable (Sym.Output repr) =>
    repr (a -> a) -> repr a -> repr a
  default chainPost ::
    Sym.Liftable2 repr => Foldable (Sym.Output repr) =>
    repr a -> repr (a -> a) -> repr a
  chainPre = Sym.lift2 chainPre
  chainPost = Sym.lift2 chainPost
  -}
  default chainPre ::
    Applicable repr =>
    Alternable repr =>
    repr (a -> a) -> repr a -> repr a
  default chainPost ::
    Applicable repr =>
    Alternable repr =>
    repr a -> repr (a -> a) -> repr a
  chainPre repr (a -> a)
op repr a
p = repr (a -> a)
go repr (a -> a) -> repr a -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr a
p
    where go :: repr (a -> a)
go = Term ValueCode ((a -> a) -> (a -> a) -> a -> a)
forall (repr :: * -> *) b c a.
Termable repr =>
repr ((b -> c) -> (a -> b) -> a -> c)
(H..) Term ValueCode ((a -> a) -> (a -> a) -> a -> a)
-> repr (a -> a) -> repr ((a -> a) -> a -> a)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr (a -> a)
op repr ((a -> a) -> a -> a) -> repr (a -> a) -> repr (a -> a)
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr (a -> a)
go repr (a -> a) -> repr (a -> a) -> repr (a -> a)
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> TermGrammar (a -> a) -> repr (a -> a)
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar (a -> a)
forall (repr :: * -> *) a. Termable repr => repr (a -> a)
H.id
  chainPost repr a
p repr (a -> a)
op = repr a
p repr a -> repr (a -> a) -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr (a -> b) -> repr b
<**> repr (a -> a)
go
    where go :: repr (a -> a)
go = Term ValueCode ((a -> a) -> (a -> a) -> a -> a)
forall (repr :: * -> *) b c a.
Termable repr =>
repr ((b -> c) -> (a -> b) -> a -> c)
(H..) Term ValueCode ((a -> a) -> (a -> a) -> a -> a)
-> repr (a -> a) -> repr ((a -> a) -> a -> a)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr (a -> a)
op repr ((a -> a) -> a -> a) -> repr (a -> a) -> repr (a -> a)
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr (a -> a)
go repr (a -> a) -> repr (a -> a) -> repr (a -> a)
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> TermGrammar (a -> a) -> repr (a -> a)
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar (a -> a)
forall (repr :: * -> *) a. Termable repr => repr (a -> a)
H.id

{-
conditional :: Selectable repr => [(TermGrammar (a -> Bool), repr b)] -> repr a -> repr b -> repr b
conditional cs p def = match p fs qs def
  where (fs, qs) = List.unzip cs
-}

-- * Class 'Satisfiable'
class Satisfiable tok repr where
  satisfy :: [ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
  default satisfy ::
    Sym.Liftable repr => Satisfiable tok (Sym.Output repr) =>
    [ErrorItem tok] ->
    TermGrammar (tok -> Bool) -> repr tok
  satisfy [ErrorItem tok]
es = Output repr tok -> repr tok
forall (repr :: * -> *) a. Liftable repr => Output repr a -> repr a
Sym.lift (Output repr tok -> repr tok)
-> (TermGrammar (tok -> Bool) -> Output repr tok)
-> TermGrammar (tok -> Bool)
-> repr tok
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ErrorItem tok] -> TermGrammar (tok -> Bool) -> Output repr tok
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy [ErrorItem tok]
es

  item :: repr tok
  item = [ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy [] (Term ValueCode (Bool -> tok -> Bool)
forall (repr :: * -> *) a b. Termable repr => repr (a -> b -> a)
H.const Term ValueCode (Bool -> tok -> Bool)
-> Term ValueCode Bool -> TermGrammar (tok -> Bool)
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ Bool -> Term ValueCode Bool
forall (repr :: * -> *). Termable repr => Bool -> repr Bool
H.bool Bool
True)

-- ** Type 'ErrorItem'
data ErrorItem tok
  =  ErrorItemToken tok
  |  ErrorItemLabel String
  |  ErrorItemHorizon Int
  |  ErrorItemEnd
deriving instance Eq tok => Eq (ErrorItem tok)
deriving instance Ord tok => Ord (ErrorItem tok)
deriving instance Show tok => Show (ErrorItem tok)
deriving instance TH.Lift tok => TH.Lift (ErrorItem tok)

-- * Class 'Lookable'
class Lookable repr where
  look :: repr a -> repr a
  negLook :: repr a -> repr ()
  default look :: Sym.Liftable1 repr => Lookable (Sym.Output repr) => repr a -> repr a
  default negLook :: Sym.Liftable1 repr => Lookable (Sym.Output repr) => repr a -> repr ()
  look = (Output repr a -> Output repr a) -> repr a -> repr a
forall (repr :: * -> *) a b.
Liftable1 repr =>
(Output repr a -> Output repr b) -> repr a -> repr b
Sym.lift1 Output repr a -> Output repr a
forall (repr :: * -> *) a. Lookable repr => repr a -> repr a
look
  negLook = (Output repr a -> Output repr ()) -> repr a -> repr ()
forall (repr :: * -> *) a b.
Liftable1 repr =>
(Output repr a -> Output repr b) -> repr a -> repr b
Sym.lift1 Output repr a -> Output repr ()
forall (repr :: * -> *) a. Lookable repr => repr a -> repr ()
negLook

  eof :: repr ()
  eof = Output repr () -> repr ()
forall (repr :: * -> *) a. Liftable repr => Output repr a -> repr a
Sym.lift Output repr ()
forall (repr :: * -> *). Lookable repr => repr ()
eof
  default eof :: Sym.Liftable repr => Lookable (Sym.Output repr) => repr ()
  -- eof = negLook (satisfy @Char [ErrorItemAny] (H.const H..@ H.bool True))
             -- (item @Char)

{-# INLINE (<:>) #-}
infixl 4 <:>
(<:>) :: Applicable repr => repr a -> repr [a] -> repr [a]
<:> :: forall (repr :: * -> *) a.
Applicable repr =>
repr a -> repr [a] -> repr [a]
(<:>) = TermGrammar (a -> [a] -> [a]) -> repr a -> repr [a] -> repr [a]
forall (repr :: * -> *) a b c.
Applicable repr =>
TermGrammar (a -> b -> c) -> repr a -> repr b -> repr c
liftA2 TermGrammar (a -> [a] -> [a])
forall (repr :: * -> *) a. Termable repr => repr (a -> [a] -> [a])
H.cons

sequence :: Applicable repr => [repr a] -> repr [a]
sequence :: forall (repr :: * -> *) a. Applicable repr => [repr a] -> repr [a]
sequence = (repr a -> repr [a] -> repr [a])
-> repr [a] -> [repr a] -> repr [a]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr repr a -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
Applicable repr =>
repr a -> repr [a] -> repr [a]
(<:>) (TermGrammar [a] -> repr [a]
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil)

traverse :: Applicable repr => (a -> repr b) -> [a] -> repr [b]
traverse :: forall (repr :: * -> *) a b.
Applicable repr =>
(a -> repr b) -> [a] -> repr [b]
traverse a -> repr b
f = [repr b] -> repr [b]
forall (repr :: * -> *) a. Applicable repr => [repr a] -> repr [a]
sequence ([repr b] -> repr [b]) -> ([a] -> [repr b]) -> [a] -> repr [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> repr b) -> [a] -> [repr b]
forall a b. (a -> b) -> [a] -> [b]
List.map a -> repr b
f
 -- FIXME: Here hlint suggests to use Control.Monad.mapM,
 -- but at this point there is no mapM for our own sequence

repeat :: Applicable repr => Int -> repr a -> repr [a]
repeat :: forall (repr :: * -> *) a.
Applicable repr =>
Int -> repr a -> repr [a]
repeat Int
n repr a
p = (Int -> repr a) -> [Int] -> repr [a]
forall (repr :: * -> *) a b.
Applicable repr =>
(a -> repr b) -> [a] -> repr [b]
traverse (repr a -> Int -> repr a
forall a b. a -> b -> a
const repr a
p) [Int
1..Int
n]

between :: Applicable repr => repr o -> repr c -> repr a -> repr a
between :: forall (repr :: * -> *) o c a.
Applicable repr =>
repr o -> repr c -> repr a -> repr a
between repr o
open repr c
close repr a
p = repr o
open repr o -> repr a -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr b
*> repr a
p repr a -> repr c -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr a
<* repr c
close

string ::
  Applicable repr => Alternable repr =>
  Satisfiable Char repr =>
  [Char] -> repr [Char]
string :: forall (repr :: * -> *).
(Applicable repr, Alternable repr, Satisfiable Char repr) =>
String -> repr String
string = repr String -> repr String
forall (repr :: * -> *) a. Alternable repr => repr a -> repr a
try (repr String -> repr String)
-> (String -> repr String) -> String -> repr String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> repr Char) -> String -> repr String
forall (repr :: * -> *) a b.
Applicable repr =>
(a -> repr b) -> [a] -> repr [b]
traverse Char -> repr Char
forall (repr :: * -> *).
(Applicable repr, Satisfiable Char repr) =>
Char -> repr Char
char

oneOf ::
  TH.Lift tok => Eq tok =>
  Satisfiable tok repr =>
  [tok] -> repr tok
oneOf :: forall tok (repr :: * -> *).
(Lift tok, Eq tok, Satisfiable tok repr) =>
[tok] -> repr tok
oneOf [tok]
ts = [ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy [String -> ErrorItem tok
forall tok. String -> ErrorItem tok
ErrorItemLabel String
"oneOf"]
  (ValueCode (tok -> Bool) -> TermGrammar (tok -> Bool)
forall (from :: * -> *) (to :: * -> *) a.
Trans from to =>
from a -> to a
Sym.trans ValueCode :: forall a. a -> CodeQ a -> ValueCode a
H.ValueCode
    { value :: tok -> Bool
value = (tok -> [tok] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`List.elem` [tok]
ts)
    , code :: CodeQ (tok -> Bool)
code = [||\t -> $$(ofChars ts [||t||])||] })

noneOf ::
  TH.Lift tok => Eq tok =>
  Satisfiable tok repr =>
  [tok] -> repr tok
noneOf :: forall tok (repr :: * -> *).
(Lift tok, Eq tok, Satisfiable tok repr) =>
[tok] -> repr tok
noneOf [tok]
cs = [ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy (tok -> ErrorItem tok
forall tok. tok -> ErrorItem tok
ErrorItemToken (tok -> ErrorItem tok) -> [tok] -> [ErrorItem tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Functor.<$> [tok]
cs) (ValueCode (tok -> Bool) -> TermGrammar (tok -> Bool)
forall (from :: * -> *) (to :: * -> *) a.
Trans from to =>
from a -> to a
Sym.trans ValueCode :: forall a. a -> CodeQ a -> ValueCode a
H.ValueCode
  { value :: tok -> Bool
value = Bool -> Bool
not (Bool -> Bool) -> (tok -> Bool) -> tok -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (tok -> [tok] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`List.elem` [tok]
cs)
  , code :: CodeQ (tok -> Bool)
code = [||\c -> not $$(ofChars cs [||c||])||]
  })

ofChars ::
  TH.Lift tok => Eq tok =>
  {-alternatives-}[tok] ->
  {-input-}TH.CodeQ tok ->
  TH.CodeQ Bool
ofChars :: forall tok. (Lift tok, Eq tok) => [tok] -> CodeQ tok -> CodeQ Bool
ofChars = (tok -> (Code Q tok -> CodeQ Bool) -> Code Q tok -> CodeQ Bool)
-> (Code Q tok -> CodeQ Bool) -> [tok] -> Code Q tok -> CodeQ Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr (\tok
alt Code Q tok -> CodeQ Bool
acc ->
  \Code Q tok
inp -> [|| alt == $$inp || $$(acc inp) ||])
  (CodeQ Bool -> Code Q tok -> CodeQ Bool
forall a b. a -> b -> a
const [||False||])

more :: Applicable repr => Satisfiable Char repr => Lookable repr => repr ()
more :: forall (repr :: * -> *).
(Applicable repr, Satisfiable Char repr, Lookable repr) =>
repr ()
more = repr () -> repr ()
forall (repr :: * -> *) a. Lookable repr => repr a -> repr a
look (repr Char -> repr ()
forall (repr :: * -> *) a. Applicable repr => repr a -> repr ()
void (forall tok (repr :: * -> *). Satisfiable tok repr => repr tok
item @Char))

char ::
  Applicable repr => Satisfiable Char repr =>
  Char -> repr Char
char :: forall (repr :: * -> *).
(Applicable repr, Satisfiable Char repr) =>
Char -> repr Char
char Char
c = [ErrorItem Char] -> TermGrammar (Char -> Bool) -> repr Char
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy [Char -> ErrorItem Char
forall tok. tok -> ErrorItem tok
ErrorItemToken Char
c] (Term ValueCode (Char -> Char -> Bool)
forall (repr :: * -> *) a.
(Termable repr, Eq a) =>
repr (a -> a -> Bool)
H.eq Term ValueCode (Char -> Char -> Bool)
-> Term ValueCode Char -> TermGrammar (Char -> Bool)
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ Char -> Term ValueCode Char
forall (repr :: * -> *) tok.
(Termable repr, Lift tok, Show tok) =>
tok -> repr tok
H.char Char
c) repr Char -> Term ValueCode Char -> repr Char
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> TermGrammar b -> repr b
$> Char -> Term ValueCode Char
forall (repr :: * -> *) tok.
(Termable repr, Lift tok, Show tok) =>
tok -> repr tok
H.char Char
c
-- char c = satisfy [ErrorItemToken c] (H.eq H..@ H.qual H..@ H.char c) $> H.char c

anyChar :: Satisfiable Char repr => repr Char
anyChar :: forall (repr :: * -> *). Satisfiable Char repr => repr Char
anyChar = [ErrorItem Char] -> TermGrammar (Char -> Bool) -> repr Char
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy [] (Term ValueCode (Bool -> Char -> Bool)
forall (repr :: * -> *) a b. Termable repr => repr (a -> b -> a)
H.const Term ValueCode (Bool -> Char -> Bool)
-> Term ValueCode Bool -> TermGrammar (Char -> Bool)
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ Bool -> Term ValueCode Bool
forall (repr :: * -> *). Termable repr => Bool -> repr Bool
H.bool Bool
True)

token ::
  TH.Lift tok => Show tok => Eq tok =>
  Applicable repr => Satisfiable tok repr =>
  tok -> repr tok
token :: forall tok (repr :: * -> *).
(Lift tok, Show tok, Eq tok, Applicable repr,
 Satisfiable tok repr) =>
tok -> repr tok
token tok
tok = [ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
forall tok (repr :: * -> *).
Satisfiable tok repr =>
[ErrorItem tok] -> TermGrammar (tok -> Bool) -> repr tok
satisfy [tok -> ErrorItem tok
forall tok. tok -> ErrorItem tok
ErrorItemToken tok
tok] (Term ValueCode (tok -> tok -> Bool)
forall (repr :: * -> *) a.
(Termable repr, Eq a) =>
repr (a -> a -> Bool)
H.eq Term ValueCode (tok -> tok -> Bool)
-> Term ValueCode tok -> TermGrammar (tok -> Bool)
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ tok -> Term ValueCode tok
forall (repr :: * -> *) tok.
(Termable repr, Lift tok, Show tok) =>
tok -> repr tok
H.char tok
tok) repr tok -> Term ValueCode tok -> repr tok
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> TermGrammar b -> repr b
$> tok -> Term ValueCode tok
forall (repr :: * -> *) tok.
(Termable repr, Lift tok, Show tok) =>
tok -> repr tok
H.char tok
tok
-- token tok = satisfy [ErrorItemToken tok] (H.eq H..@ H.qual H..@ H.char tok) $> H.char tok

tokens ::
  TH.Lift tok => Eq tok => Show tok =>
  Applicable repr => Alternable repr =>
  Satisfiable tok repr => [tok] -> repr [tok]
tokens :: forall tok (repr :: * -> *).
(Lift tok, Eq tok, Show tok, Applicable repr, Alternable repr,
 Satisfiable tok repr) =>
[tok] -> repr [tok]
tokens = repr [tok] -> repr [tok]
forall (repr :: * -> *) a. Alternable repr => repr a -> repr a
try (repr [tok] -> repr [tok])
-> ([tok] -> repr [tok]) -> [tok] -> repr [tok]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (tok -> repr tok) -> [tok] -> repr [tok]
forall (repr :: * -> *) a b.
Applicable repr =>
(a -> repr b) -> [a] -> repr [b]
traverse tok -> repr tok
forall tok (repr :: * -> *).
(Lift tok, Show tok, Eq tok, Applicable repr,
 Satisfiable tok repr) =>
tok -> repr tok
token

-- Composite Combinators
-- someTill :: repr a -> repr b -> repr [a]
-- someTill p end = negLook end *> (p <:> manyTill p end)

void :: Applicable repr => repr a -> repr ()
void :: forall (repr :: * -> *) a. Applicable repr => repr a -> repr ()
void repr a
p = repr a
p repr a -> repr () -> repr ()
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr b
*> repr ()
forall (repr :: * -> *). Applicable repr => repr ()
unit

unit :: Applicable repr => repr ()
unit :: forall (repr :: * -> *). Applicable repr => repr ()
unit = TermGrammar () -> repr ()
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar ()
forall (repr :: * -> *). Termable repr => repr ()
H.unit

{-
constp :: Applicable repr => repr a -> repr (b -> a)
constp = (H.const <$>)


-- Alias Operations
infixl 1 >>
(>>) :: Applicable repr => repr a -> repr b -> repr b
(>>) = (*>)

-- Monoidal Operations

infixl 4 <~>
(<~>) :: Applicable repr => repr a -> repr b -> repr (a, b)
(<~>) = liftA2 (H.runtime (,))

infixl 4 <~
(<~) :: Applicable repr => repr a -> repr b -> repr a
(<~) = (<*)

infixl 4 ~>
(~>) :: Applicable repr => repr a -> repr b -> repr b
(~>) = (*>)

-- Lift Operations
liftA2 ::
 Applicable repr =>
 TermGrammar (a -> b -> c) -> repr a -> repr b -> repr c
liftA2 f x = (<*>) (fmap f x)

liftA3 ::
 Applicable repr =>
 TermGrammar (a -> b -> c -> d) -> repr a -> repr b -> repr c -> repr d
liftA3 f a b c = liftA2 f a b <*> c

-}

-- Parser Folds
pfoldr ::
 Applicable repr => Foldable repr =>
 TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
pfoldr :: forall (repr :: * -> *) a b.
(Applicable repr, Foldable repr) =>
TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
pfoldr TermGrammar (a -> b -> b)
f TermGrammar b
k repr a
p = repr (b -> b) -> repr b -> repr b
forall (repr :: * -> *) a.
Foldable repr =>
repr (a -> a) -> repr a -> repr a
chainPre (TermGrammar (a -> b -> b)
f TermGrammar (a -> b -> b) -> repr a -> repr (b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr a
p) (TermGrammar b -> repr b
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar b
k)

pfoldr1 ::
 Applicable repr => Foldable repr =>
 TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
pfoldr1 :: forall (repr :: * -> *) a b.
(Applicable repr, Foldable repr) =>
TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
pfoldr1 TermGrammar (a -> b -> b)
f TermGrammar b
k repr a
p = TermGrammar (a -> b -> b)
f TermGrammar (a -> b -> b) -> repr a -> repr (b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr a
p repr (b -> b) -> repr b -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
forall (repr :: * -> *) a b.
(Applicable repr, Foldable repr) =>
TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
pfoldr TermGrammar (a -> b -> b)
f TermGrammar b
k repr a
p

pfoldl ::
 Applicable repr => Foldable repr =>
 TermGrammar (b -> a -> b) -> TermGrammar b -> repr a -> repr b
pfoldl :: forall (repr :: * -> *) b a.
(Applicable repr, Foldable repr) =>
TermGrammar (b -> a -> b) -> TermGrammar b -> repr a -> repr b
pfoldl TermGrammar (b -> a -> b)
f TermGrammar b
k repr a
p = repr b -> repr (b -> b) -> repr b
forall (repr :: * -> *) a.
Foldable repr =>
repr a -> repr (a -> a) -> repr a
chainPost (TermGrammar b -> repr b
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar b
k) ((Term ValueCode ((b -> a -> b) -> a -> b -> b)
forall (repr :: * -> *) a b c.
Termable repr =>
repr ((a -> b -> c) -> b -> a -> c)
H.flip Term ValueCode ((b -> a -> b) -> a -> b -> b)
-> repr (b -> a -> b) -> repr (a -> b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> TermGrammar (b -> a -> b) -> repr (b -> a -> b)
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar (b -> a -> b)
f) repr (a -> b -> b) -> repr a -> repr (b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr a
p)

pfoldl1 ::
 Applicable repr => Foldable repr =>
 TermGrammar (b -> a -> b) -> TermGrammar b -> repr a -> repr b
pfoldl1 :: forall (repr :: * -> *) b a.
(Applicable repr, Foldable repr) =>
TermGrammar (b -> a -> b) -> TermGrammar b -> repr a -> repr b
pfoldl1 TermGrammar (b -> a -> b)
f TermGrammar b
k repr a
p = repr b -> repr (b -> b) -> repr b
forall (repr :: * -> *) a.
Foldable repr =>
repr a -> repr (a -> a) -> repr a
chainPost (TermGrammar (b -> a -> b)
f TermGrammar (b -> a -> b) -> repr b -> repr (a -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> TermGrammar b -> repr b
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar b
k repr (a -> b) -> repr a -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr a
p) ((Term ValueCode ((b -> a -> b) -> a -> b -> b)
forall (repr :: * -> *) a b c.
Termable repr =>
repr ((a -> b -> c) -> b -> a -> c)
H.flip Term ValueCode ((b -> a -> b) -> a -> b -> b)
-> repr (b -> a -> b) -> repr (a -> b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> TermGrammar (b -> a -> b) -> repr (b -> a -> b)
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure TermGrammar (b -> a -> b)
f) repr (a -> b -> b) -> repr a -> repr (b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr a
p)

-- Chain Combinators
chainl1' ::
 Applicable repr => Foldable repr =>
 TermGrammar (a -> b) -> repr a -> repr (b -> a -> b) -> repr b
chainl1' :: forall (repr :: * -> *) a b.
(Applicable repr, Foldable repr) =>
TermGrammar (a -> b) -> repr a -> repr (b -> a -> b) -> repr b
chainl1' TermGrammar (a -> b)
f repr a
p repr (b -> a -> b)
op = repr b -> repr (b -> b) -> repr b
forall (repr :: * -> *) a.
Foldable repr =>
repr a -> repr (a -> a) -> repr a
chainPost (TermGrammar (a -> b)
f TermGrammar (a -> b) -> repr a -> repr b
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr a
p) (Term ValueCode ((b -> a -> b) -> a -> b -> b)
forall (repr :: * -> *) a b c.
Termable repr =>
repr ((a -> b -> c) -> b -> a -> c)
H.flip Term ValueCode ((b -> a -> b) -> a -> b -> b)
-> repr (b -> a -> b) -> repr (a -> b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> repr (b -> a -> b)
op repr (a -> b -> b) -> repr a -> repr (b -> b)
forall (repr :: * -> *) a b.
Applicable repr =>
repr (a -> b) -> repr a -> repr b
<*> repr a
p)

chainl1 ::
 Applicable repr => Foldable repr =>
 repr a -> repr (a -> a -> a) -> repr a
chainl1 :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr (a -> a -> a) -> repr a
chainl1 = TermGrammar (a -> a) -> repr a -> repr (a -> a -> a) -> repr a
forall (repr :: * -> *) a b.
(Applicable repr, Foldable repr) =>
TermGrammar (a -> b) -> repr a -> repr (b -> a -> b) -> repr b
chainl1' TermGrammar (a -> a)
forall (repr :: * -> *) a. Termable repr => repr (a -> a)
H.id

{-
chainr1' :: ParserOps rep => rep (a -> b) -> repr a -> repr (a -> b -> b) -> repr b
chainr1' f p op = newRegister_ H.id $ \acc ->
  let go = bind p $ \x ->
           modify acc (H.flip (H..@) <$> (op <*> x)) *> go
       <|> f <$> x
  in go <**> get acc

chainr1 :: repr a -> repr (a -> a -> a) -> repr a
chainr1 = chainr1' H.id

chainr :: repr a -> repr (a -> a -> a) -> TermGrammar a -> repr a
chainr p op x = option x (chainr1 p op)
-}

chainl ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr (a -> a -> a) -> TermGrammar a -> repr a
chainl :: forall (repr :: * -> *) a.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr (a -> a -> a) -> TermGrammar a -> repr a
chainl repr a
p repr (a -> a -> a)
op TermGrammar a
x = TermGrammar a -> repr a -> repr a
forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
TermGrammar a -> repr a -> repr a
option TermGrammar a
x (repr a -> repr (a -> a -> a) -> repr a
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr (a -> a -> a) -> repr a
chainl1 repr a
p repr (a -> a -> a)
op)

-- Derived Combinators
many ::
 Applicable repr => Foldable repr =>
 repr a -> repr [a]
many :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr [a]
many = TermGrammar (a -> [a] -> [a])
-> TermGrammar [a] -> repr a -> repr [a]
forall (repr :: * -> *) a b.
(Applicable repr, Foldable repr) =>
TermGrammar (a -> b -> b) -> TermGrammar b -> repr a -> repr b
pfoldr TermGrammar (a -> [a] -> [a])
forall (repr :: * -> *) a. Termable repr => repr (a -> [a] -> [a])
H.cons TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil

manyN ::
 Applicable repr => Foldable repr =>
 Int -> repr a -> repr [a]
manyN :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
Int -> repr a -> repr [a]
manyN Int
n repr a
p = (Int -> repr [a] -> repr [a]) -> repr [a] -> [Int] -> repr [a]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr ((repr [a] -> repr [a]) -> Int -> repr [a] -> repr [a]
forall a b. a -> b -> a
const (repr a
p repr a -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
Applicable repr =>
repr a -> repr [a] -> repr [a]
<:>)) (repr a -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr [a]
many repr a
p) [Int
1..Int
n]

some ::
 Applicable repr => Foldable repr =>
 repr a -> repr [a]
some :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr [a]
some = Int -> repr a -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
Int -> repr a -> repr [a]
manyN Int
1

skipMany ::
 Applicable repr => Foldable repr =>
 repr a -> repr ()
--skipMany p = let skipManyp = p *> skipManyp <|> unit in skipManyp
skipMany :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr ()
skipMany = repr () -> repr ()
forall (repr :: * -> *) a. Applicable repr => repr a -> repr ()
void (repr () -> repr ()) -> (repr a -> repr ()) -> repr a -> repr ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermGrammar (() -> a -> ()) -> TermGrammar () -> repr a -> repr ()
forall (repr :: * -> *) b a.
(Applicable repr, Foldable repr) =>
TermGrammar (b -> a -> b) -> TermGrammar b -> repr a -> repr b
pfoldl TermGrammar (() -> a -> ())
forall (repr :: * -> *) a b. Termable repr => repr (a -> b -> a)
H.const TermGrammar ()
forall (repr :: * -> *). Termable repr => repr ()
H.unit -- the void here will encourage the optimiser to recognise that the register is unused

skipManyN ::
 Applicable repr => Foldable repr =>
 Int -> repr a -> repr ()
skipManyN :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
Int -> repr a -> repr ()
skipManyN Int
n repr a
p = (Int -> repr () -> repr ()) -> repr () -> [Int] -> repr ()
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr ((repr () -> repr ()) -> Int -> repr () -> repr ()
forall a b. a -> b -> a
const (repr a
p repr a -> repr () -> repr ()
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr b
*>)) (repr a -> repr ()
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr ()
skipMany repr a
p) [Int
1..Int
n]

skipSome ::
 Applicable repr => Foldable repr =>
 repr a -> repr ()
skipSome :: forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr ()
skipSome = Int -> repr a -> repr ()
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
Int -> repr a -> repr ()
skipManyN Int
1

sepBy ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr b -> repr [a]
sepBy :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
sepBy repr a
p repr b
sep = TermGrammar [a] -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
TermGrammar a -> repr a -> repr a
option TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil (repr a -> repr b -> repr [a]
forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
sepBy1 repr a
p repr b
sep)

sepBy1 ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr b -> repr [a]
sepBy1 :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
sepBy1 repr a
p repr b
sep = repr a
p repr a -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
Applicable repr =>
repr a -> repr [a] -> repr [a]
<:> repr a -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr [a]
many (repr b
sep repr b -> repr a -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr b
*> repr a
p)

endBy ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr b -> repr [a]
endBy :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
endBy repr a
p repr b
sep = repr a -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr [a]
many (repr a
p repr a -> repr b -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr a
<* repr b
sep)

endBy1 ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr b -> repr [a]
endBy1 :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
endBy1 repr a
p repr b
sep = repr a -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Foldable repr) =>
repr a -> repr [a]
some (repr a
p repr a -> repr b -> repr a
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr a
<* repr b
sep)

sepEndBy ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr b -> repr [a]
sepEndBy :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
sepEndBy repr a
p repr b
sep = TermGrammar [a] -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
TermGrammar a -> repr a -> repr a
option TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil (repr a -> repr b -> repr [a]
forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
sepEndBy1 repr a
p repr b
sep)

sepEndBy1 ::
 Applicable repr => Alternable repr => Foldable repr =>
 repr a -> repr b -> repr [a]
sepEndBy1 :: forall (repr :: * -> *) a b.
(Applicable repr, Alternable repr, Foldable repr) =>
repr a -> repr b -> repr [a]
sepEndBy1 repr a
p repr b
sep =
  let seb1 :: repr [a]
seb1 = repr a
p repr a -> repr (a -> [a]) -> repr [a]
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr (a -> b) -> repr b
<**> (repr b
sep repr b -> repr (a -> [a]) -> repr (a -> [a])
forall (repr :: * -> *) a b.
Applicable repr =>
repr a -> repr b -> repr b
*> (Term ValueCode ((a -> [a] -> [a]) -> [a] -> a -> [a])
forall (repr :: * -> *) a b c.
Termable repr =>
repr ((a -> b -> c) -> b -> a -> c)
H.flip Term ValueCode ((a -> [a] -> [a]) -> [a] -> a -> [a])
-> Term ValueCode (a -> [a] -> [a])
-> Term ValueCode ([a] -> a -> [a])
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ Term ValueCode (a -> [a] -> [a])
forall (repr :: * -> *) a. Termable repr => repr (a -> [a] -> [a])
H.cons Term ValueCode ([a] -> a -> [a]) -> repr [a] -> repr (a -> [a])
forall (repr :: * -> *) a b.
Applicable repr =>
TermGrammar (a -> b) -> repr a -> repr b
<$> TermGrammar [a] -> repr [a] -> repr [a]
forall (repr :: * -> *) a.
(Applicable repr, Alternable repr) =>
TermGrammar a -> repr a -> repr a
option TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil repr [a]
seb1)
                 repr (a -> [a]) -> repr (a -> [a]) -> repr (a -> [a])
forall (repr :: * -> *) a.
Alternable repr =>
repr a -> repr a -> repr a
<|> TermGrammar (a -> [a]) -> repr (a -> [a])
forall (repr :: * -> *) a.
Applicable repr =>
TermGrammar a -> repr a
pure (Term ValueCode ((a -> [a] -> [a]) -> [a] -> a -> [a])
forall (repr :: * -> *) a b c.
Termable repr =>
repr ((a -> b -> c) -> b -> a -> c)
H.flip Term ValueCode ((a -> [a] -> [a]) -> [a] -> a -> [a])
-> Term ValueCode (a -> [a] -> [a])
-> Term ValueCode ([a] -> a -> [a])
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ Term ValueCode (a -> [a] -> [a])
forall (repr :: * -> *) a. Termable repr => repr (a -> [a] -> [a])
H.cons Term ValueCode ([a] -> a -> [a])
-> TermGrammar [a] -> TermGrammar (a -> [a])
forall (repr :: * -> *) a b.
Termable repr =>
repr (a -> b) -> repr a -> repr b
H..@ TermGrammar [a]
forall (repr :: * -> *) a. Termable repr => repr [a]
H.nil))
  in repr [a]
seb1

{-
sepEndBy1 :: repr a -> repr b -> repr [a]
sepEndBy1 p sep = newRegister_ H.id $ \acc ->
  let go = modify acc ((H.flip (H..)) H..@ H.cons <$> p)
         *> (sep *> (go <|> get acc) <|> get acc)
  in go <*> pure H.nil
-}

{-
-- Combinators interpreters for 'Sym.Any'.
instance Applicable repr => Applicable (Sym.Any repr)
instance Satisfiable repr => Satisfiable (Sym.Any repr)
instance Alternable repr => Alternable (Sym.Any repr)
instance Selectable repr => Selectable (Sym.Any repr)
instance Matchable repr => Matchable (Sym.Any repr)
instance Lookable repr => Lookable (Sym.Any repr)
instance Foldable repr => Foldable (Sym.Any repr)
-}