{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}

module Database.PostgreSQL.Placeholder.Convert.QuestionToDollar
  ( convert
  , splitQueries
  ) where

import           Control.Monad                    (void)
import           Control.Monad.State.Strict       (StateT, evalStateT, lift, state)
import qualified Data.Attoparsec.ByteString       as AP
import qualified Data.Attoparsec.ByteString.Char8 as APC
import           Data.Attoparsec.Combinator       ((<?>))
import qualified Data.Attoparsec.Combinator       as AP
import qualified Data.Attoparsec.Internal.Types   as API
import           Data.ByteString                  (ByteString)
import qualified Data.ByteString                  as BS
import qualified Data.ByteString.UTF8             as BSU
import qualified Data.Either.Result               as R

#if !MIN_VERSION_base(4,13,0)
import           Control.Monad.Fail               (MonadFail)
#endif

convert :: MonadFail m => ByteString -> m ByteString
convert :: ByteString -> m ByteString
convert ByteString
q =
  Result ByteString -> m ByteString
forall (m :: * -> *) a. MonadFail m => Result a -> m a
R.toMonadFail (Result ByteString -> m ByteString)
-> Result ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ Either String ByteString -> Result ByteString
forall a. Either String a -> Result a
R.fromEither (Either String ByteString -> Result ByteString)
-> Either String ByteString -> Result ByteString
forall a b. (a -> b) -> a -> b
$
    (Parser ByteString -> ByteString -> Either String ByteString)
-> ByteString -> Parser ByteString -> Either String ByteString
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser ByteString -> ByteString -> Either String ByteString
forall a. Parser a -> ByteString -> Either String a
AP.parseOnly ByteString
q (Parser ByteString -> Either String ByteString)
-> Parser ByteString -> Either String ByteString
forall a b. (a -> b) -> a -> b
$
      (StateT Int Parser ByteString -> Int -> Parser ByteString)
-> Int -> StateT Int Parser ByteString -> Parser ByteString
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT Int Parser ByteString -> Int -> Parser ByteString
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT Int
1 (StateT Int Parser ByteString -> Parser ByteString)
-> StateT Int Parser ByteString -> Parser ByteString
forall a b. (a -> b) -> a -> b
$
        ([ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat ([ByteString] -> ByteString)
-> StateT Int Parser [ByteString] -> StateT Int Parser ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (StateT Int Parser [ByteString] -> StateT Int Parser ByteString)
-> StateT Int Parser [ByteString] -> StateT Int Parser ByteString
forall a b. (a -> b) -> a -> b
$ do
          [ByteString]
ss <-
            StateT Int Parser ByteString -> StateT Int Parser [ByteString]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
AP.many' (StateT Int Parser ByteString -> StateT Int Parser [ByteString])
-> StateT Int Parser ByteString -> StateT Int Parser [ByteString]
forall a b. (a -> b) -> a -> b
$
              [StateT Int Parser ByteString] -> StateT Int Parser ByteString
forall (f :: * -> *) a. Alternative f => [f a] -> f a
AP.choice
                [ StateT Int Parser ByteString
question
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString
questionLiteral
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString
singleQuoteLiteral
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString
doubleQuoteLiteral
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString
dollarQuoteLiteral
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString
lineComment
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString
blockComment
                , Parser ByteString -> StateT Int Parser ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Parser ByteString -> StateT Int Parser ByteString)
-> Parser ByteString -> StateT Int Parser ByteString
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack ([Word8] -> ByteString)
-> (Word8 -> [Word8]) -> Word8 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
:[]) (Word8 -> ByteString)
-> Parser ByteString Word8 -> Parser ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString Word8
AP.anyWord8
                ]
          Parser ByteString () -> StateT Int Parser ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Parser ByteString ()
forall t. Chunk t => Parser t ()
AP.endOfInput
          [ByteString] -> StateT Int Parser [ByteString]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [ByteString]
ss

singleQuoteLiteral :: AP.Parser BS.ByteString
singleQuoteLiteral :: Parser ByteString
singleQuoteLiteral = Char -> Parser ByteString
oneCharQuoteLiteral Char
'\'' Parser ByteString -> String -> Parser ByteString
forall i a. Parser i a -> String -> Parser i a
<?> String
"singleQuoteLiteral"

doubleQuoteLiteral :: AP.Parser BS.ByteString
doubleQuoteLiteral :: Parser ByteString
doubleQuoteLiteral = Char -> Parser ByteString
oneCharQuoteLiteral Char
'"' Parser ByteString -> String -> Parser ByteString
forall i a. Parser i a -> String -> Parser i a
<?> String
"doubleQuoteLiteral"

oneCharQuoteLiteral :: Char -> AP.Parser BS.ByteString
oneCharQuoteLiteral :: Char -> Parser ByteString
oneCharQuoteLiteral Char
quote =
  Parser ByteString () -> Parser ByteString
forall a. Parser a -> Parser ByteString
takeConsumed (Parser ByteString () -> Parser ByteString)
-> Parser ByteString () -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ do
    Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Char -> Parser ByteString ())
-> Parser ByteString Char -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Char -> Parser ByteString Char
APC.char Char
quote
    Parser ByteString ()
body
    Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Char -> Parser ByteString ())
-> Parser ByteString Char -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Char -> Parser ByteString Char
APC.char Char
quote
  where
    body :: Parser ByteString ()
body = do
      Parser ByteString -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString -> Parser ByteString ())
-> Parser ByteString -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Parser ByteString
APC.takeTill (Bool -> Bool -> Bool
(||) (Bool -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\\') (Char -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
quote))
      () -> Parser ByteString () -> Parser ByteString ()
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
AP.option () (Parser ByteString () -> Parser ByteString ())
-> Parser ByteString () -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ do
        Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Char -> Parser ByteString ())
-> Parser ByteString Char -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Char -> Parser ByteString Char
APC.char Char
'\\'
        Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser ByteString Char
APC.anyChar -- It continues although it is something wrong if the character does not stand for escape sequence.
        Parser ByteString ()
body

dollarQuoteLiteral :: AP.Parser BS.ByteString
dollarQuoteLiteral :: Parser ByteString
dollarQuoteLiteral =
  (Parser ByteString -> String -> Parser ByteString
forall i a. Parser i a -> String -> Parser i a
<?> String
"dollarQuoteLiteral") (Parser ByteString -> Parser ByteString)
-> Parser ByteString -> Parser ByteString
forall a b. (a -> b) -> a -> b
$
    Parser ByteString () -> Parser ByteString
forall a. Parser a -> Parser ByteString
takeConsumed (Parser ByteString () -> Parser ByteString)
-> Parser ByteString () -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ do
      ByteString
quote <- Parser ByteString
dollarQuote
      ByteString -> Parser ByteString ()
go ByteString
quote
  where
    dollarQuote :: Parser ByteString
dollarQuote =
      (Parser ByteString -> String -> Parser ByteString
forall i a. Parser i a -> String -> Parser i a
<?> String
"dollarQuote") (Parser ByteString -> Parser ByteString)
-> Parser ByteString -> Parser ByteString
forall a b. (a -> b) -> a -> b
$
        Parser ByteString () -> Parser ByteString
forall a. Parser a -> Parser ByteString
takeConsumed (Parser ByteString () -> Parser ByteString)
-> Parser ByteString () -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ do
          Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Char -> Parser ByteString ())
-> Parser ByteString Char -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Char -> Parser ByteString Char
APC.char Char
'$'
          Parser ByteString String -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString String -> Parser ByteString ())
-> Parser ByteString String -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Parser ByteString Char -> Parser ByteString String
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
AP.many' (Parser ByteString Char -> Parser ByteString String)
-> Parser ByteString Char -> Parser ByteString String
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Parser ByteString Char
APC.satisfy (Bool -> Bool -> Bool
(&&) (Bool -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
' ') (Char -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'$'))
          Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Char -> Parser ByteString ())
-> Parser ByteString Char -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Char -> Parser ByteString Char
APC.char Char
'$'
    go :: ByteString -> Parser ByteString ()
go ByteString
quote = Parser ByteString ()
go'
      where
        go' :: Parser ByteString ()
go' = do
          Parser ByteString String -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString String -> Parser ByteString ())
-> Parser ByteString String -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Parser ByteString Char -> Parser ByteString String
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
AP.many' (Parser ByteString Char -> Parser ByteString String)
-> Parser ByteString Char -> Parser ByteString String
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Parser ByteString Char
APC.satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'$')
          [Parser ByteString ()] -> Parser ByteString ()
forall (f :: * -> *) a. Alternative f => [f a] -> f a
AP.choice
            [ Parser ByteString -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString -> Parser ByteString ())
-> Parser ByteString -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Parser ByteString
AP.string ByteString
quote
            , Char -> Parser ByteString Char
APC.char Char
'$' Parser ByteString Char
-> Parser ByteString () -> Parser ByteString ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ByteString ()
go'
            ]

lineComment :: AP.Parser BS.ByteString
lineComment :: Parser ByteString
lineComment =
  (Parser ByteString -> String -> Parser ByteString
forall i a. Parser i a -> String -> Parser i a
<?> String
"lineComment") (Parser ByteString -> Parser ByteString)
-> Parser ByteString -> Parser ByteString
forall a b. (a -> b) -> a -> b
$
    Parser ByteString () -> Parser ByteString
forall a. Parser a -> Parser ByteString
takeConsumed (Parser ByteString () -> Parser ByteString)
-> Parser ByteString () -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ do
      Parser ByteString -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser ByteString
"--"
      Parser ByteString String -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString String -> Parser ByteString ())
-> Parser ByteString String -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Parser ByteString Char -> Parser ByteString String
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
AP.many' (Parser ByteString Char -> Parser ByteString String)
-> Parser ByteString Char -> Parser ByteString String
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Parser ByteString Char
APC.satisfy (Bool -> Bool -> Bool
(&&) (Bool -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r') (Char -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n'))
      () -> Parser ByteString () -> Parser ByteString ()
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
AP.option () (Parser ByteString () -> Parser ByteString ())
-> Parser ByteString () -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ Parser ByteString () -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser ByteString ()
APC.endOfLine

blockComment :: AP.Parser BS.ByteString
blockComment :: Parser ByteString
blockComment =
  (Parser ByteString -> String -> Parser ByteString
forall i a. Parser i a -> String -> Parser i a
<?> String
"blockComment") (Parser ByteString -> Parser ByteString)
-> Parser ByteString -> Parser ByteString
forall a b. (a -> b) -> a -> b
$
    Parser ByteString () -> Parser ByteString
forall a. Parser a -> Parser ByteString
takeConsumed (Parser ByteString () -> Parser ByteString)
-> Parser ByteString () -> Parser ByteString
forall a b. (a -> b) -> a -> b
$ do
      Parser ByteString -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser ByteString
"/*"
      Parser ByteString ()
go
  where
    go :: Parser ByteString ()
go = do
      Parser ByteString [()] -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString [()] -> Parser ByteString ())
-> Parser ByteString [()] -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$
        Parser ByteString () -> Parser ByteString [()]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
AP.many' (Parser ByteString () -> Parser ByteString [()])
-> Parser ByteString () -> Parser ByteString [()]
forall a b. (a -> b) -> a -> b
$
          [Parser ByteString ()] -> Parser ByteString ()
forall (f :: * -> *) a. Alternative f => [f a] -> f a
AP.choice
            [ Parser ByteString -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser ByteString
blockComment
            , Parser ByteString Char -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Parser ByteString Char -> Parser ByteString ())
-> Parser ByteString Char -> Parser ByteString ()
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Parser ByteString Char
APC.satisfy (Bool -> Bool -> Bool
(&&) (Bool -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'/') (Char -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'*'))
            ]
      [Parser ByteString ()] -> Parser ByteString ()
forall (f :: * -> *) a. Alternative f => [f a] -> f a
AP.choice
        [ Parser ByteString -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser ByteString
"*/"
        , Parser ByteString -> Parser ByteString
forall i a. Parser i a -> Parser i a
AP.lookAhead Parser ByteString
"/*" Parser ByteString -> Parser ByteString () -> Parser ByteString ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ByteString ()
go
        , (Char -> Bool) -> Parser ByteString Char
APC.satisfy (Bool -> Bool -> Bool
(||) (Bool -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool -> Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') (Char -> Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'*')) Parser ByteString Char
-> Parser ByteString () -> Parser ByteString ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser ByteString ()
go
        ]

question :: StateT Int AP.Parser BS.ByteString
question :: StateT Int Parser ByteString
question = do
  StateT Int Parser Char -> StateT Int Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (StateT Int Parser Char -> StateT Int Parser ())
-> StateT Int Parser Char -> StateT Int Parser ()
forall a b. (a -> b) -> a -> b
$ Parser ByteString Char -> StateT Int Parser Char
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Parser ByteString Char -> StateT Int Parser Char)
-> Parser ByteString Char -> StateT Int Parser Char
forall a b. (a -> b) -> a -> b
$ Char -> Parser ByteString Char
APC.char Char
'?'
  Int
i <- StateT Int Parser Int
getAndIncrement
  ByteString -> StateT Int Parser ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> StateT Int Parser ByteString)
-> ByteString -> StateT Int Parser ByteString
forall a b. (a -> b) -> a -> b
$ ByteString
"$" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> String -> ByteString
BSU.fromString (Int -> String
forall a. Show a => a -> String
show Int
i)
  where
    getAndIncrement :: StateT Int AP.Parser Int
    getAndIncrement :: StateT Int Parser Int
getAndIncrement = (Int -> (Int, Int)) -> StateT Int Parser Int
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (\Int
i -> (Int
i, Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))

questionLiteral :: AP.Parser BS.ByteString
questionLiteral :: Parser ByteString
questionLiteral = Parser ByteString -> Parser ByteString
forall a. Parser a -> Parser ByteString
takeConsumed Parser ByteString
"\\?"

-- | Split a query including multiple queries separated by semicolon.
splitQueries :: ByteString -> [ByteString]
splitQueries :: ByteString -> [ByteString]
splitQueries ByteString
q =
  case Parser [ByteString] -> ByteString -> Either String [ByteString]
forall a. Parser a -> ByteString -> Either String a
AP.parseOnly Parser [ByteString]
parser ByteString
q of
    Right [ByteString]
qs -> [ByteString]
qs
    Left String
_   -> [ByteString
q]
  where
    parser :: Parser [ByteString]
parser =
      ([Maybe ByteString] -> [ByteString]
conv ([Maybe ByteString] -> [ByteString])
-> Parser ByteString [Maybe ByteString] -> Parser [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Parser ByteString [Maybe ByteString] -> Parser [ByteString])
-> Parser ByteString [Maybe ByteString] -> Parser [ByteString]
forall a b. (a -> b) -> a -> b
$
        Parser ByteString (Maybe ByteString)
-> Parser ByteString [Maybe ByteString]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
AP.many' (Parser ByteString (Maybe ByteString)
 -> Parser ByteString [Maybe ByteString])
-> Parser ByteString (Maybe ByteString)
-> Parser ByteString [Maybe ByteString]
forall a b. (a -> b) -> a -> b
$
          [Parser ByteString (Maybe ByteString)]
-> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a. Alternative f => [f a] -> f a
AP.choice
            [ Char -> Parser ByteString Char
APC.char Char
';' Parser ByteString Char
-> Parser ByteString (Maybe ByteString)
-> Parser ByteString (Maybe ByteString)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe ByteString -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ByteString
forall a. Maybe a
Nothing
            , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> Parser ByteString -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString
singleQuoteLiteral
            , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> Parser ByteString -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString
doubleQuoteLiteral
            , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> Parser ByteString -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString
dollarQuoteLiteral
            , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> Parser ByteString -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString
lineComment
            , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> Parser ByteString -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString
blockComment
            , ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> (Word8 -> ByteString) -> Word8 -> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
BS.pack ([Word8] -> ByteString)
-> (Word8 -> [Word8]) -> Word8 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
:[]) (Word8 -> Maybe ByteString)
-> Parser ByteString Word8 -> Parser ByteString (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString Word8
AP.anyWord8
            ]
    conv :: [Maybe BS.ByteString] -> [BS.ByteString]
    conv :: [Maybe ByteString] -> [ByteString]
conv =
      (Maybe ByteString -> [ByteString] -> [ByteString])
-> [ByteString] -> [Maybe ByteString] -> [ByteString]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Maybe ByteString -> [ByteString] -> [ByteString]
forall a. (Semigroup a, IsString a) => Maybe a -> [a] -> [a]
go []
      where
        go :: Maybe a -> [a] -> [a]
go (Just a
s) (a
a:[a]
as) = (a
s a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a)a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
as
        go (Just a
s) []     = [a
s]
        go Maybe a
Nothing [a]
acc     = a
""a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
acc

takeConsumed :: AP.Parser a -> AP.Parser BS.ByteString
takeConsumed :: Parser a -> Parser ByteString
takeConsumed Parser a
p = do
  Int
n <-
    Parser ByteString Int -> Parser ByteString Int
forall i a. Parser i a -> Parser i a
AP.lookAhead (Parser ByteString Int -> Parser ByteString Int)
-> Parser ByteString Int -> Parser ByteString Int
forall a b. (a -> b) -> a -> b
$ do
      API.Pos Int
startPos <- Parser Pos
currentPos
      Parser a -> Parser ByteString ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Parser a
p
      API.Pos Int
endPos <- Parser Pos
currentPos
      Int -> Parser ByteString Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Parser ByteString Int) -> Int -> Parser ByteString Int
forall a b. (a -> b) -> a -> b
$ Int
endPos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
startPos
  Int -> Parser ByteString
AP.take Int
n

currentPos :: AP.Parser API.Pos
currentPos :: Parser Pos
currentPos = (forall r.
 State ByteString
 -> Pos
 -> More
 -> Failure ByteString (State ByteString) r
 -> Success ByteString (State ByteString) Pos r
 -> IResult ByteString r)
-> Parser Pos
forall i a.
(forall r.
 State i
 -> Pos
 -> More
 -> Failure i (State i) r
 -> Success i (State i) a r
 -> IResult i r)
-> Parser i a
API.Parser ((forall r.
  State ByteString
  -> Pos
  -> More
  -> Failure ByteString (State ByteString) r
  -> Success ByteString (State ByteString) Pos r
  -> IResult ByteString r)
 -> Parser Pos)
-> (forall r.
    State ByteString
    -> Pos
    -> More
    -> Failure ByteString (State ByteString) r
    -> Success ByteString (State ByteString) Pos r
    -> IResult ByteString r)
-> Parser Pos
forall a b. (a -> b) -> a -> b
$ \State ByteString
t Pos
pos More
more Failure ByteString (State ByteString) r
_lose Success ByteString (State ByteString) Pos r
suc -> Success ByteString (State ByteString) Pos r
suc State ByteString
t Pos
pos More
more Pos
pos