{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Org.Parser.Definitions
  ( module Org.Parser.Definitions
  , module Org.Types
  , module Org.Builder
  , module Org.Parser.State
  , module Text.Megaparsec
  , module Text.Megaparsec.Char
  , module Text.Megaparsec.Debug
  , module Data.Char
  )
where

import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isPunctuation, isSpace)
import Org.Builder (OrgElements, OrgObjects)
import Org.Parser.State
import Org.Types
import Text.Megaparsec
import Text.Megaparsec.Char
import Text.Megaparsec.Debug
import Prelude hiding (State)

type Parser = Parsec Void Text

type MonadParser m = MonadParsec Void Text m

newtype OrgParser a = OrgParser (ReaderT OrgParserEnv (StateT OrgParserState Parser) a)
  deriving newtype
    ( forall a b. a -> OrgParser b -> OrgParser a
forall a b. (a -> b) -> OrgParser a -> OrgParser b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> OrgParser b -> OrgParser a
$c<$ :: forall a b. a -> OrgParser b -> OrgParser a
fmap :: forall a b. (a -> b) -> OrgParser a -> OrgParser b
$cfmap :: forall a b. (a -> b) -> OrgParser a -> OrgParser b
Functor
    , Functor OrgParser
forall a. a -> OrgParser a
forall a b. OrgParser a -> OrgParser b -> OrgParser a
forall a b. OrgParser a -> OrgParser b -> OrgParser b
forall a b. OrgParser (a -> b) -> OrgParser a -> OrgParser b
forall a b c.
(a -> b -> c) -> OrgParser a -> OrgParser b -> OrgParser c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. OrgParser a -> OrgParser b -> OrgParser a
$c<* :: forall a b. OrgParser a -> OrgParser b -> OrgParser a
*> :: forall a b. OrgParser a -> OrgParser b -> OrgParser b
$c*> :: forall a b. OrgParser a -> OrgParser b -> OrgParser b
liftA2 :: forall a b c.
(a -> b -> c) -> OrgParser a -> OrgParser b -> OrgParser c
$cliftA2 :: forall a b c.
(a -> b -> c) -> OrgParser a -> OrgParser b -> OrgParser c
<*> :: forall a b. OrgParser (a -> b) -> OrgParser a -> OrgParser b
$c<*> :: forall a b. OrgParser (a -> b) -> OrgParser a -> OrgParser b
pure :: forall a. a -> OrgParser a
$cpure :: forall a. a -> OrgParser a
Applicative
    , Applicative OrgParser
forall a. a -> OrgParser a
forall a b. OrgParser a -> OrgParser b -> OrgParser b
forall a b. OrgParser a -> (a -> OrgParser b) -> OrgParser b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> OrgParser a
$creturn :: forall a. a -> OrgParser a
>> :: forall a b. OrgParser a -> OrgParser b -> OrgParser b
$c>> :: forall a b. OrgParser a -> OrgParser b -> OrgParser b
>>= :: forall a b. OrgParser a -> (a -> OrgParser b) -> OrgParser b
$c>>= :: forall a b. OrgParser a -> (a -> OrgParser b) -> OrgParser b
Monad
    , Applicative OrgParser
forall a. OrgParser a
forall a. OrgParser a -> OrgParser [a]
forall a. OrgParser a -> OrgParser a -> OrgParser a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. OrgParser a -> OrgParser [a]
$cmany :: forall a. OrgParser a -> OrgParser [a]
some :: forall a. OrgParser a -> OrgParser [a]
$csome :: forall a. OrgParser a -> OrgParser [a]
<|> :: forall a. OrgParser a -> OrgParser a -> OrgParser a
$c<|> :: forall a. OrgParser a -> OrgParser a -> OrgParser a
empty :: forall a. OrgParser a
$cempty :: forall a. OrgParser a
Alternative
    , MonadState OrgParserState
    , MonadReader OrgParserEnv
    , Monad OrgParser
Alternative OrgParser
forall a. OrgParser a
forall a. OrgParser a -> OrgParser a -> OrgParser a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: forall a. OrgParser a -> OrgParser a -> OrgParser a
$cmplus :: forall a. OrgParser a -> OrgParser a -> OrgParser a
mzero :: forall a. OrgParser a
$cmzero :: forall a. OrgParser a
MonadPlus
    , Monad OrgParser
forall a. String -> OrgParser a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: forall a. String -> OrgParser a
$cfail :: forall a. String -> OrgParser a
MonadFail
    , MonadParsec Void Text
    )

type OrgParseError = ParseErrorBundle Text Void

-- * Last char

setLastChar :: Maybe Char -> OrgParser ()
setLastChar :: Maybe Char -> OrgParser ()
setLastChar Maybe Char
lchar =
  forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\OrgParserState
c -> OrgParserState
c {orgStateLastChar :: Maybe Char
orgStateLastChar = Maybe Char
lchar forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> OrgParserState -> Maybe Char
orgStateLastChar OrgParserState
c})

clearLastChar :: OrgParser ()
clearLastChar :: OrgParser ()
clearLastChar = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\OrgParserState
c -> OrgParserState
c {orgStateLastChar :: Maybe Char
orgStateLastChar = forall a. Maybe a
Nothing})

putLastChar :: Char -> OrgParser ()
putLastChar :: Char -> OrgParser ()
putLastChar Char
lchar = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\OrgParserState
c -> OrgParserState
c {orgStateLastChar :: Maybe Char
orgStateLastChar = forall a. a -> Maybe a
Just Char
lchar})

withIndentLevel :: Int -> OrgParser a -> OrgParser a
withIndentLevel :: forall a. Int -> OrgParser a -> OrgParser a
withIndentLevel Int
i = forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local \OrgParserEnv
s -> OrgParserEnv
s {orgEnvIndentLevel :: Int
orgEnvIndentLevel = Int
i}

-- * State and Environment convenience functions

type FullState = (State Text Void, OrgParserState)

getFullState :: OrgParser FullState
getFullState :: OrgParser FullState
getFullState = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) forall e s (m :: * -> *). MonadParsec e s m => m (State s e)
getParserState forall s (m :: * -> *). MonadState s m => m s
get

setFullState :: FullState -> OrgParser ()
setFullState :: FullState -> OrgParser ()
setFullState (State Text Void
pS, OrgParserState
oS) = forall e s (m :: * -> *). MonadParsec e s m => State s e -> m ()
setParserState State Text Void
pS forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s (m :: * -> *). MonadState s m => s -> m ()
put OrgParserState
oS

getsO :: (OrgOptions -> a) -> OrgParser a
getsO :: forall a. (OrgOptions -> a) -> OrgParser a
getsO OrgOptions -> a
f = forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (OrgOptions -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. OrgParserEnv -> OrgOptions
orgEnvOptions)

-- * Marked parsers

data Marked m a = Marked
  { forall (m :: * -> *) a. Marked m a -> String
getMarks :: String
  , forall (m :: * -> *) a. Marked m a -> m a
getParser :: m a
  }

instance Functor m => Functor (Marked m) where
  fmap :: forall a b. (a -> b) -> Marked m a -> Marked m b
fmap a -> b
f x :: Marked m a
x@(Marked String
_ m a
p) = Marked m a
x {getParser :: m b
getParser = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f m a
p}

instance Alternative m => Semigroup (Marked m a) where
  Marked String
s1 m a
p1 <> :: Marked m a -> Marked m a -> Marked m a
<> Marked String
s2 m a
p2 =
    forall (m :: * -> *) a. String -> m a -> Marked m a
Marked (String
s1 forall a. [a] -> [a] -> [a]
++ String
s2) (m a
p1 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m a
p2)

instance Alternative m => Monoid (Marked m a) where
  mempty :: Marked m a
mempty = forall (m :: * -> *) a. String -> m a -> Marked m a
Marked [] forall (f :: * -> *) a. Alternative f => f a
empty
  mconcat :: [Marked m a] -> Marked m a
mconcat [Marked m a]
ms =
    forall (m :: * -> *) a. String -> m a -> Marked m a
Marked
      (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap forall (m :: * -> *) a. Marked m a -> String
getMarks [Marked m a]
ms)
      (forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall (m :: * -> *) a. Marked m a -> m a
getParser [Marked m a]
ms)
  {-# INLINE mconcat #-}