{-# LANGUAGE PatternSynonyms #-}
module Parsley.Internal.Core.Primitives (
    Parser,
    Reg,
    module Parsley.Internal.Core.Primitives
  ) where

import Prelude hiding                      (pure, (<*>))
import Parsley.Internal.Core.CombinatorAST (Combinator(..), ScopeRegister(..), Reg(..), Parser(..), PosSelector(..))
import Parsley.Internal.Core.Defunc        (Defunc, charPred)

import Parsley.Internal.Common.Indexed     (Fix(In), (:+:)(..))

-- Core smart constructors
{-# INLINE pure #-}
pure :: Defunc a -> Parser a
pure :: forall a. Defunc a -> Parser a
pure = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (k :: Type -> Type). Defunc a -> Combinator k a
Pure

{-# INLINE satisfy #-}
satisfy :: Defunc (Char -> Bool) -> Parser Char
satisfy :: Defunc (Char -> Bool) -> Parser Char
satisfy = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (k :: Type -> Type). CharPred -> Combinator k Char
Satisfy forall b c a. (b -> c) -> (a -> b) -> a -> c
. Defunc (Char -> Bool) -> CharPred
charPred

{-# INLINE conditional #-}
conditional :: [(Defunc (a -> Bool), Parser b)] -> Parser a -> Parser b -> Parser b
conditional :: forall a b.
[(Defunc (a -> Bool), Parser b)]
-> Parser a -> Parser b -> Parser b
conditional [(Defunc (a -> Bool), Parser b)]
cs (Parser Fix (Combinator :+: ScopeRegister) a
p) (Parser Fix (Combinator :+: ScopeRegister) b
def) =
  let ([Defunc (a -> Bool)]
fs, [Parser b]
qs) = forall a b. [(a, b)] -> ([a], [b])
unzip [(Defunc (a -> Bool), Parser b)]
cs
  in forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall (k :: Type -> Type) a1 a.
k a1 -> [Defunc (a1 -> Bool)] -> [k a] -> k a -> Combinator k a
Match Fix (Combinator :+: ScopeRegister) a
p [Defunc (a -> Bool)]
fs (forall a b. (a -> b) -> [a] -> [b]
map forall a. Parser a -> Fix (Combinator :+: ScopeRegister) a
unParser [Parser b]
qs) Fix (Combinator :+: ScopeRegister) b
def)))

{-# INLINE (<*>) #-}
(<*>) :: Parser (a -> b) -> Parser a -> Parser b
Parser Fix (Combinator :+: ScopeRegister) (a -> b)
p <*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser Fix (Combinator :+: ScopeRegister) a
q = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (Fix (Combinator :+: ScopeRegister) (a -> b)
p forall (k :: Type -> Type) a1 a.
k (a1 -> a) -> k a1 -> Combinator k a
:<*>: Fix (Combinator :+: ScopeRegister) a
q)))

{-# INLINE (<*) #-}
(<*) :: Parser a -> Parser b -> Parser a
Parser Fix (Combinator :+: ScopeRegister) a
p <* :: forall a b. Parser a -> Parser b -> Parser a
<* Parser Fix (Combinator :+: ScopeRegister) b
q = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (Fix (Combinator :+: ScopeRegister) a
p forall (k :: Type -> Type) a b. k a -> k b -> Combinator k a
:<*: Fix (Combinator :+: ScopeRegister) b
q)))

{-# INLINE (*>) #-}
(*>) :: Parser a -> Parser b -> Parser b
Parser Fix (Combinator :+: ScopeRegister) a
p *> :: forall a b. Parser a -> Parser b -> Parser b
*> Parser Fix (Combinator :+: ScopeRegister) b
q = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (Fix (Combinator :+: ScopeRegister) a
p forall (k :: Type -> Type) a1 a. k a1 -> k a -> Combinator k a
:*>: Fix (Combinator :+: ScopeRegister) b
q)))

{-# INLINE empty #-}
empty :: Parser a
empty :: forall a. Parser a
empty = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L forall (k :: Type -> Type) a. Combinator k a
Empty))

{-# INLINE (<|>) #-}
(<|>) :: Parser a -> Parser a -> Parser a
Parser Fix (Combinator :+: ScopeRegister) a
p <|> :: forall a. Parser a -> Parser a -> Parser a
<|> Parser Fix (Combinator :+: ScopeRegister) a
q = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (Fix (Combinator :+: ScopeRegister) a
p forall (k :: Type -> Type) a. k a -> k a -> Combinator k a
:<|>: Fix (Combinator :+: ScopeRegister) a
q)))

{-# INLINE lookAhead #-}
lookAhead :: Parser a -> Parser a
lookAhead :: forall a. Parser a -> Parser a
lookAhead = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (k :: Type -> Type) a. k a -> Combinator k a
LookAhead forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> Fix (Combinator :+: ScopeRegister) a
unParser

{-# INLINE notFollowedBy #-}
notFollowedBy :: Parser a -> Parser ()
notFollowedBy :: forall a. Parser a -> Parser ()
notFollowedBy = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (k :: Type -> Type) a1. k a1 -> Combinator k ()
NotFollowedBy forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> Fix (Combinator :+: ScopeRegister) a
unParser

{-# INLINE try #-}
try :: Parser a -> Parser a
try :: forall a. Parser a -> Parser a
try = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (k :: Type -> Type) a. k a -> Combinator k a
Try forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> Fix (Combinator :+: ScopeRegister) a
unParser

{-# INLINE branch #-}
branch :: Parser (Either a b) -> Parser (a -> c) -> Parser (b -> c) -> Parser c
branch :: forall a b c.
Parser (Either a b)
-> Parser (a -> c) -> Parser (b -> c) -> Parser c
branch (Parser Fix (Combinator :+: ScopeRegister) (Either a b)
c) (Parser Fix (Combinator :+: ScopeRegister) (a -> c)
p) (Parser Fix (Combinator :+: ScopeRegister) (b -> c)
q) = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall (k :: Type -> Type) a1 b a.
k (Either a1 b) -> k (a1 -> a) -> k (b -> a) -> Combinator k a
Branch Fix (Combinator :+: ScopeRegister) (Either a b)
c Fix (Combinator :+: ScopeRegister) (a -> c)
p Fix (Combinator :+: ScopeRegister) (b -> c)
q)))

{-# INLINE loop #-}
loop :: Parser () -> Parser a -> Parser a
loop :: forall a. Parser () -> Parser a -> Parser a
loop (Parser Fix (Combinator :+: ScopeRegister) ()
body) (Parser Fix (Combinator :+: ScopeRegister) a
exit) = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall (k :: Type -> Type) a. k () -> k a -> Combinator k a
Loop Fix (Combinator :+: ScopeRegister) ()
body Fix (Combinator :+: ScopeRegister) a
exit)))

{-# INLINE newRegister #-}
newRegister :: Parser a -> (forall r. Reg r a -> Parser b) -> Parser b
newRegister :: forall a b. Parser a -> (forall r. Reg r a -> Parser b) -> Parser b
newRegister (Parser Fix (Combinator :+: ScopeRegister) a
p) forall r. Reg r a -> Parser b
f = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (g :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (f :: k -> k1 -> Type).
g k2 a -> (:+:) f g k2 a
R (forall (k :: Type -> Type) a1 a.
k a1 -> (forall r. Reg r a1 -> k a) -> ScopeRegister k a
ScopeRegister Fix (Combinator :+: ScopeRegister) a
p (forall a. Parser a -> Fix (Combinator :+: ScopeRegister) a
unParser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. Reg r a -> Parser b
f))))

{-# INLINE get #-}
get :: Reg r a -> Parser a
get :: forall r a. Reg r a -> Parser a
get (Reg ΣVar a
reg) = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall a (k :: Type -> Type). ΣVar a -> Combinator k a
GetRegister ΣVar a
reg)))

{-# INLINE put #-}
put :: Reg r a -> Parser a -> Parser ()
put :: forall r a. Reg r a -> Parser a -> Parser ()
put (Reg ΣVar a
reg) (Parser Fix (Combinator :+: ScopeRegister) a
p) = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall a1 (k :: Type -> Type). ΣVar a1 -> k a1 -> Combinator k ()
PutRegister ΣVar a
reg Fix (Combinator :+: ScopeRegister) a
p)))

{-# INLINE line #-}
line :: Parser Int
line :: Parser Int
line = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall (k :: Type -> Type). PosSelector -> Combinator k Int
Position PosSelector
Line)))

{-# INLINE col #-}
col :: Parser Int
col :: Parser Int
col = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall (k :: Type -> Type). PosSelector -> Combinator k Int
Position PosSelector
Col)))

{-# INLINE debug #-}
debug :: String -> Parser a -> Parser a
debug :: forall a. String -> Parser a -> Parser a
debug String
name (Parser Fix (Combinator :+: ScopeRegister) a
p) = forall a. Fix (Combinator :+: ScopeRegister) a -> Parser a
Parser (forall {k} (f :: (k -> Type) -> k -> Type) (a :: k).
f (Fix f) a -> Fix f a
In (forall {k} {k1} (f :: k -> k1 -> Type) (k2 :: k) (a :: k1)
       (g :: k -> k1 -> Type).
f k2 a -> (:+:) f g k2 a
L (forall (k :: Type -> Type) a. String -> k a -> Combinator k a
Debug String
name Fix (Combinator :+: ScopeRegister) a
p)))