{-|
Module      : PostgREST.Request.Parsers
Description : PostgREST parser combinators

This module is in charge of parsing all the querystring values in an url, e.g. the select, id, order in `/projects?select=id,name&id=eq.1&order=id,name.desc`.
-}
module PostgREST.Request.Parsers
  ( pColumns
  , pLogicPath
  , pLogicSingleVal
  , pLogicTree
  , pOrder
  , pOrderTerm
  , pRequestColumns
  , pRequestFilter
  , pRequestLogicTree
  , pRequestOnConflict
  , pRequestOrder
  , pRequestRange
  , pRequestSelect
  , pSingleVal
  , pTreePath
  ) where

import qualified Data.HashMap.Strict as M
import qualified Data.Set            as S

import Data.Either.Combinators       (mapLeft)
import Data.Foldable                 (foldl1)
import Data.List                     (init, last)
import Data.Text                     (intercalate, replace, strip)
import Data.Tree                     (Tree (..))
import Text.Parsec.Error             (errorMessages,
                                      showErrorMessages)
import Text.ParserCombinators.Parsec (GenParser, ParseError, Parser,
                                      anyChar, between, char, digit,
                                      eof, errorPos, letter,
                                      lookAhead, many1, noneOf,
                                      notFollowedBy, oneOf, option,
                                      optionMaybe, parse, sepBy1,
                                      string, try, (<?>))

import PostgREST.DbStructure.Identifiers (FieldName)
import PostgREST.Error                   (ApiRequestError (ParseRequestError))
import PostgREST.Query.SqlFragment       (ftsOperators, operators)
import PostgREST.RangeQuery              (NonnegRange)

import PostgREST.Request.Types

import Protolude      hiding (intercalate, option, replace, toS, try)
import Protolude.Conv (toS)

pRequestSelect :: Text -> Either ApiRequestError [Tree SelectItem]
pRequestSelect :: Text -> Either ApiRequestError [Tree SelectItem]
pRequestSelect Text
selStr =
  Either ParseError [Tree SelectItem]
-> Either ApiRequestError [Tree SelectItem]
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError [Tree SelectItem]
 -> Either ApiRequestError [Tree SelectItem])
-> Either ParseError [Tree SelectItem]
-> Either ApiRequestError [Tree SelectItem]
forall a b. (a -> b) -> a -> b
$ Parsec String () [Tree SelectItem]
-> String -> String -> Either ParseError [Tree SelectItem]
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () [Tree SelectItem]
pFieldForest (String
"failed to parse select parameter (" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a b. StringConv a b => a -> b
toS Text
selStr String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")") (Text -> String
forall a b. StringConv a b => a -> b
toS Text
selStr)

pRequestOnConflict :: Text -> Either ApiRequestError  [FieldName]
pRequestOnConflict :: Text -> Either ApiRequestError [Text]
pRequestOnConflict Text
oncStr =
  Either ParseError [Text] -> Either ApiRequestError [Text]
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError [Text] -> Either ApiRequestError [Text])
-> Either ParseError [Text] -> Either ApiRequestError [Text]
forall a b. (a -> b) -> a -> b
$ Parsec String () [Text]
-> String -> String -> Either ParseError [Text]
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () [Text]
pColumns (String
"failed to parse on_conflict parameter (" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a b. StringConv a b => a -> b
toS Text
oncStr String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")") (Text -> String
forall a b. StringConv a b => a -> b
toS Text
oncStr)

pRequestFilter :: (Text, Text) -> Either ApiRequestError (EmbedPath, Filter)
pRequestFilter :: (Text, Text) -> Either ApiRequestError ([Text], Filter)
pRequestFilter (Text
k, Text
v) = Either ParseError ([Text], Filter)
-> Either ApiRequestError ([Text], Filter)
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError ([Text], Filter)
 -> Either ApiRequestError ([Text], Filter))
-> Either ParseError ([Text], Filter)
-> Either ApiRequestError ([Text], Filter)
forall a b. (a -> b) -> a -> b
$ (,) ([Text] -> Filter -> ([Text], Filter))
-> Either ParseError [Text]
-> Either ParseError (Filter -> ([Text], Filter))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError [Text]
path Either ParseError (Filter -> ([Text], Filter))
-> Either ParseError Filter -> Either ParseError ([Text], Filter)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Field -> OpExpr -> Filter
Filter (Field -> OpExpr -> Filter)
-> Either ParseError Field -> Either ParseError (OpExpr -> Filter)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError Field
fld Either ParseError (OpExpr -> Filter)
-> Either ParseError OpExpr -> Either ParseError Filter
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either ParseError OpExpr
oper)
  where
    treePath :: Either ParseError ([Text], Field)
treePath = Parsec String () ([Text], Field)
-> String -> String -> Either ParseError ([Text], Field)
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () ([Text], Field)
pTreePath (String
"failed to parser tree path (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. StringConv a b => a -> b
toS Text
k String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError ([Text], Field))
-> String -> Either ParseError ([Text], Field)
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a b. StringConv a b => a -> b
toS Text
k
    oper :: Either ParseError OpExpr
oper = Parsec String () OpExpr
-> String -> String -> Either ParseError OpExpr
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse (Parser Text -> Parsec String () OpExpr
pOpExpr Parser Text
pSingleVal) (String
"failed to parse filter (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. StringConv a b => a -> b
toS Text
v String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError OpExpr)
-> String -> Either ParseError OpExpr
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a b. StringConv a b => a -> b
toS Text
v
    path :: Either ParseError [Text]
path = ([Text], Field) -> [Text]
forall a b. (a, b) -> a
fst (([Text], Field) -> [Text])
-> Either ParseError ([Text], Field) -> Either ParseError [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError ([Text], Field)
treePath
    fld :: Either ParseError Field
fld = ([Text], Field) -> Field
forall a b. (a, b) -> b
snd (([Text], Field) -> Field)
-> Either ParseError ([Text], Field) -> Either ParseError Field
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError ([Text], Field)
treePath

pRequestOrder :: (Text, Text) -> Either ApiRequestError (EmbedPath, [OrderTerm])
pRequestOrder :: (Text, Text) -> Either ApiRequestError ([Text], [OrderTerm])
pRequestOrder (Text
k, Text
v) = Either ParseError ([Text], [OrderTerm])
-> Either ApiRequestError ([Text], [OrderTerm])
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError ([Text], [OrderTerm])
 -> Either ApiRequestError ([Text], [OrderTerm]))
-> Either ParseError ([Text], [OrderTerm])
-> Either ApiRequestError ([Text], [OrderTerm])
forall a b. (a -> b) -> a -> b
$ (,) ([Text] -> [OrderTerm] -> ([Text], [OrderTerm]))
-> Either ParseError [Text]
-> Either ParseError ([OrderTerm] -> ([Text], [OrderTerm]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError [Text]
path Either ParseError ([OrderTerm] -> ([Text], [OrderTerm]))
-> Either ParseError [OrderTerm]
-> Either ParseError ([Text], [OrderTerm])
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either ParseError [OrderTerm]
ord'
  where
    treePath :: Either ParseError ([Text], Field)
treePath = Parsec String () ([Text], Field)
-> String -> String -> Either ParseError ([Text], Field)
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () ([Text], Field)
pTreePath (String
"failed to parser tree path (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. StringConv a b => a -> b
toS Text
k String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError ([Text], Field))
-> String -> Either ParseError ([Text], Field)
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a b. StringConv a b => a -> b
toS Text
k
    path :: Either ParseError [Text]
path = ([Text], Field) -> [Text]
forall a b. (a, b) -> a
fst (([Text], Field) -> [Text])
-> Either ParseError ([Text], Field) -> Either ParseError [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError ([Text], Field)
treePath
    ord' :: Either ParseError [OrderTerm]
ord' = Parsec String () [OrderTerm]
-> String -> String -> Either ParseError [OrderTerm]
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () [OrderTerm]
pOrder (String
"failed to parse order (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. StringConv a b => a -> b
toS Text
v String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError [OrderTerm])
-> String -> Either ParseError [OrderTerm]
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a b. StringConv a b => a -> b
toS Text
v

pRequestRange :: (ByteString, NonnegRange) -> Either ApiRequestError (EmbedPath, NonnegRange)
pRequestRange :: (ByteString, NonnegRange)
-> Either ApiRequestError ([Text], NonnegRange)
pRequestRange (ByteString
k, NonnegRange
v) = Either ParseError ([Text], NonnegRange)
-> Either ApiRequestError ([Text], NonnegRange)
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError ([Text], NonnegRange)
 -> Either ApiRequestError ([Text], NonnegRange))
-> Either ParseError ([Text], NonnegRange)
-> Either ApiRequestError ([Text], NonnegRange)
forall a b. (a -> b) -> a -> b
$ (,) ([Text] -> NonnegRange -> ([Text], NonnegRange))
-> Either ParseError [Text]
-> Either ParseError (NonnegRange -> ([Text], NonnegRange))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError [Text]
path Either ParseError (NonnegRange -> ([Text], NonnegRange))
-> Either ParseError NonnegRange
-> Either ParseError ([Text], NonnegRange)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> NonnegRange -> Either ParseError NonnegRange
forall (f :: * -> *) a. Applicative f => a -> f a
pure NonnegRange
v
  where
    treePath :: Either ParseError ([Text], Field)
treePath = Parsec String () ([Text], Field)
-> String -> String -> Either ParseError ([Text], Field)
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () ([Text], Field)
pTreePath (String
"failed to parser tree path (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a b. StringConv a b => a -> b
toS ByteString
k String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError ([Text], Field))
-> String -> Either ParseError ([Text], Field)
forall a b. (a -> b) -> a -> b
$ ByteString -> String
forall a b. StringConv a b => a -> b
toS ByteString
k
    path :: Either ParseError [Text]
path = ([Text], Field) -> [Text]
forall a b. (a, b) -> a
fst (([Text], Field) -> [Text])
-> Either ParseError ([Text], Field) -> Either ParseError [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError ([Text], Field)
treePath

pRequestLogicTree :: (Text, Text) -> Either ApiRequestError (EmbedPath, LogicTree)
pRequestLogicTree :: (Text, Text) -> Either ApiRequestError ([Text], LogicTree)
pRequestLogicTree (Text
k, Text
v) = Either ParseError ([Text], LogicTree)
-> Either ApiRequestError ([Text], LogicTree)
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError ([Text], LogicTree)
 -> Either ApiRequestError ([Text], LogicTree))
-> Either ParseError ([Text], LogicTree)
-> Either ApiRequestError ([Text], LogicTree)
forall a b. (a -> b) -> a -> b
$ (,) ([Text] -> LogicTree -> ([Text], LogicTree))
-> Either ParseError [Text]
-> Either ParseError (LogicTree -> ([Text], LogicTree))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError [Text]
embedPath Either ParseError (LogicTree -> ([Text], LogicTree))
-> Either ParseError LogicTree
-> Either ParseError ([Text], LogicTree)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either ParseError LogicTree
logicTree
  where
    path :: Either ParseError ([Text], Text)
path = Parsec String () ([Text], Text)
-> String -> String -> Either ParseError ([Text], Text)
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () ([Text], Text)
pLogicPath (String
"failed to parser logic path (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. StringConv a b => a -> b
toS Text
k String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError ([Text], Text))
-> String -> Either ParseError ([Text], Text)
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a b. StringConv a b => a -> b
toS Text
k
    embedPath :: Either ParseError [Text]
embedPath = ([Text], Text) -> [Text]
forall a b. (a, b) -> a
fst (([Text], Text) -> [Text])
-> Either ParseError ([Text], Text) -> Either ParseError [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError ([Text], Text)
path
    logicTree :: Either ParseError LogicTree
logicTree = do
      Text
op <- ([Text], Text) -> Text
forall a b. (a, b) -> b
snd (([Text], Text) -> Text)
-> Either ParseError ([Text], Text) -> Either ParseError Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either ParseError ([Text], Text)
path
      -- Concat op and v to make pLogicTree argument regular,
      -- in the form of "?and=and(.. , ..)" instead of "?and=(.. , ..)"
      Parsec String () LogicTree
-> String -> String -> Either ParseError LogicTree
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () LogicTree
pLogicTree (String
"failed to parse logic tree (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. StringConv a b => a -> b
toS Text
v String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")") (String -> Either ParseError LogicTree)
-> String -> Either ParseError LogicTree
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a b. StringConv a b => a -> b
toS (Text
op Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
v)

pRequestColumns :: Maybe Text -> Either ApiRequestError (Maybe (S.Set FieldName))
pRequestColumns :: Maybe Text -> Either ApiRequestError (Maybe (Set Text))
pRequestColumns Maybe Text
colStr =
  case Maybe Text
colStr of
    Just Text
str ->
      Either ParseError (Maybe (Set Text))
-> Either ApiRequestError (Maybe (Set Text))
forall a. Either ParseError a -> Either ApiRequestError a
mapError (Either ParseError (Maybe (Set Text))
 -> Either ApiRequestError (Maybe (Set Text)))
-> Either ParseError (Maybe (Set Text))
-> Either ApiRequestError (Maybe (Set Text))
forall a b. (a -> b) -> a -> b
$ Set Text -> Maybe (Set Text)
forall a. a -> Maybe a
Just (Set Text -> Maybe (Set Text))
-> ([Text] -> Set Text) -> [Text] -> Maybe (Set Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Set Text
forall a. Ord a => [a] -> Set a
S.fromList ([Text] -> Maybe (Set Text))
-> Either ParseError [Text] -> Either ParseError (Maybe (Set Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsec String () [Text]
-> String -> String -> Either ParseError [Text]
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () [Text]
pColumns (String
"failed to parse columns parameter (" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a b. StringConv a b => a -> b
toS Text
str String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")") (Text -> String
forall a b. StringConv a b => a -> b
toS Text
str)
    Maybe Text
_ -> Maybe (Set Text) -> Either ApiRequestError (Maybe (Set Text))
forall a b. b -> Either a b
Right Maybe (Set Text)
forall a. Maybe a
Nothing

ws :: Parser Text
ws :: Parser Text
ws = String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text)
-> ParsecT String () Identity String -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
" \t")

lexeme :: Parser a -> Parser a
lexeme :: Parser a -> Parser a
lexeme Parser a
p = Parser Text
ws Parser Text -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser a
p Parser a -> Parser Text -> Parser a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Text
ws

pTreePath :: Parser (EmbedPath, Field)
pTreePath :: Parsec String () ([Text], Field)
pTreePath = do
  [Text]
p <- Parser Text
pFieldName Parser Text
-> ParsecT String () Identity Char -> Parsec String () [Text]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` ParsecT String () Identity Char
pDelimiter
  [JsonOperation]
jp <- [JsonOperation]
-> ParsecT String () Identity [JsonOperation]
-> ParsecT String () Identity [JsonOperation]
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [] ParsecT String () Identity [JsonOperation]
pJsonPath
  ([Text], Field) -> Parsec String () ([Text], Field)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Text] -> [Text]
forall a. [a] -> [a]
init [Text]
p, ([Text] -> Text
forall a. [a] -> a
last [Text]
p, [JsonOperation]
jp))

pFieldForest :: Parser [Tree SelectItem]
pFieldForest :: Parsec String () [Tree SelectItem]
pFieldForest = Parser (Tree SelectItem)
pFieldTree Parser (Tree SelectItem)
-> ParsecT String () Identity Char
-> Parsec String () [Tree SelectItem]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',')
  where
    pFieldTree :: Parser (Tree SelectItem)
    pFieldTree :: Parser (Tree SelectItem)
pFieldTree =  Parser (Tree SelectItem) -> Parser (Tree SelectItem)
forall tok st a. GenParser tok st a -> GenParser tok st a
try (SelectItem -> [Tree SelectItem] -> Tree SelectItem
forall a. a -> Forest a -> Tree a
Node (SelectItem -> [Tree SelectItem] -> Tree SelectItem)
-> ParsecT String () Identity SelectItem
-> ParsecT
     String () Identity ([Tree SelectItem] -> Tree SelectItem)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity SelectItem
pRelationSelect ParsecT String () Identity ([Tree SelectItem] -> Tree SelectItem)
-> Parsec String () [Tree SelectItem] -> Parser (Tree SelectItem)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> Parsec String () [Tree SelectItem]
-> Parsec String () [Tree SelectItem]
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')') Parsec String () [Tree SelectItem]
pFieldForest) Parser (Tree SelectItem)
-> Parser (Tree SelectItem) -> Parser (Tree SelectItem)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                  SelectItem -> [Tree SelectItem] -> Tree SelectItem
forall a. a -> Forest a -> Tree a
Node (SelectItem -> [Tree SelectItem] -> Tree SelectItem)
-> ParsecT String () Identity SelectItem
-> ParsecT
     String () Identity ([Tree SelectItem] -> Tree SelectItem)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity SelectItem
pFieldSelect ParsecT String () Identity ([Tree SelectItem] -> Tree SelectItem)
-> Parsec String () [Tree SelectItem] -> Parser (Tree SelectItem)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Tree SelectItem] -> Parsec String () [Tree SelectItem]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

pStar :: Parser Text
pStar :: Parser Text
pStar = ByteString -> Text
forall a b. StringConv a b => a -> b
toS (ByteString -> Text)
-> ParsecT String () Identity ByteString -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"*" ParsecT String () Identity String
-> ByteString -> ParsecT String () Identity ByteString
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> (ByteString
"*"::ByteString))

pFieldName :: Parser Text
pFieldName :: Parser Text
pFieldName =
  Parser Text
pQuotedValue Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
  Text -> [Text] -> Text
intercalate Text
"-" ([Text] -> Text) -> ([String] -> [Text]) -> [String] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Text) -> [String] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map String -> Text
forall a b. StringConv a b => a -> b
toS ([String] -> Text)
-> ParsecT String () Identity [String] -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT String () Identity Char
-> ParsecT String () Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
"_ ") ParsecT String () Identity String
-> ParsecT String () Identity Char
-> ParsecT String () Identity [String]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` ParsecT String () Identity Char
dash) Parser Text -> String -> Parser Text
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?>
  String
"field name (* or [a..z0..9_])"
  where
    isDash :: GenParser Char st ()
    isDash :: GenParser Char st ()
isDash = GenParser Char st () -> GenParser Char st ()
forall tok st a. GenParser tok st a -> GenParser tok st a
try ( Char -> ParsecT String st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'-' ParsecT String st Identity Char
-> GenParser Char st () -> GenParser Char st ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT String st Identity Char -> GenParser Char st ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Char -> ParsecT String st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'>') )
    dash :: Parser Char
    dash :: ParsecT String () Identity Char
dash = GenParser Char () ()
forall st. GenParser Char st ()
isDash GenParser Char () () -> Char -> ParsecT String () Identity Char
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'-'

pJsonPath :: Parser JsonPath
pJsonPath :: ParsecT String () Identity [JsonOperation]
pJsonPath = ParsecT String () Identity JsonOperation
-> ParsecT String () Identity [JsonOperation]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ParsecT String () Identity JsonOperation
pJsonOperation
  where
    pJsonOperation :: Parser JsonOperation
    pJsonOperation :: ParsecT String () Identity JsonOperation
pJsonOperation = ParsecT String () Identity (JsonOperand -> JsonOperation)
forall st.
ParsecT String st Identity (JsonOperand -> JsonOperation)
pJsonArrow ParsecT String () Identity (JsonOperand -> JsonOperation)
-> ParsecT String () Identity JsonOperand
-> ParsecT String () Identity JsonOperation
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT String () Identity JsonOperand
pJsonOperand

    pJsonArrow :: ParsecT String st Identity (JsonOperand -> JsonOperation)
pJsonArrow   =
      ParsecT String st Identity (JsonOperand -> JsonOperation)
-> ParsecT String st Identity (JsonOperand -> JsonOperation)
forall tok st a. GenParser tok st a -> GenParser tok st a
try (String -> ParsecT String st Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"->>" ParsecT String st Identity String
-> (JsonOperand -> JsonOperation)
-> ParsecT String st Identity (JsonOperand -> JsonOperation)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> JsonOperand -> JsonOperation
J2Arrow) ParsecT String st Identity (JsonOperand -> JsonOperation)
-> ParsecT String st Identity (JsonOperand -> JsonOperation)
-> ParsecT String st Identity (JsonOperand -> JsonOperation)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
      ParsecT String st Identity (JsonOperand -> JsonOperation)
-> ParsecT String st Identity (JsonOperand -> JsonOperation)
forall tok st a. GenParser tok st a -> GenParser tok st a
try (String -> ParsecT String st Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"->" ParsecT String st Identity String
-> (JsonOperand -> JsonOperation)
-> ParsecT String st Identity (JsonOperand -> JsonOperation)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> JsonOperand -> JsonOperation
JArrow)

    pJsonOperand :: ParsecT String () Identity JsonOperand
pJsonOperand =
      let pJKey :: ParsecT String () Identity JsonOperand
pJKey = Text -> JsonOperand
JKey (Text -> JsonOperand) -> (Text -> Text) -> Text -> JsonOperand
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
forall a b. StringConv a b => a -> b
toS (Text -> JsonOperand)
-> Parser Text -> ParsecT String () Identity JsonOperand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
pFieldName
          pJIdx :: ParsecT String u Identity JsonOperand
pJIdx = Text -> JsonOperand
JIdx (Text -> JsonOperand) -> (String -> Text) -> String -> JsonOperand
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a b. StringConv a b => a -> b
toS (String -> JsonOperand)
-> ParsecT String u Identity String
-> ParsecT String u Identity JsonOperand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((:) (Char -> String -> String)
-> ParsecT String u Identity Char
-> ParsecT String u Identity (String -> String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char
-> ParsecT String u Identity Char -> ParsecT String u Identity Char
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Char
'+' (Char -> ParsecT String u Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'-') ParsecT String u Identity (String -> String)
-> ParsecT String u Identity String
-> ParsecT String u Identity String
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT String u Identity Char -> ParsecT String u Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT String u Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit) ParsecT String u Identity JsonOperand
-> ParsecT String u Identity ()
-> ParsecT String u Identity JsonOperand
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String u Identity ()
forall st. GenParser Char st ()
pEnd
          pEnd :: ParsecT String st Identity ()
pEnd = ParsecT String st Identity () -> ParsecT String st Identity ()
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String st Identity String -> ParsecT String st Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT String st Identity String
 -> ParsecT String st Identity ())
-> ParsecT String st Identity String
-> ParsecT String st Identity ()
forall a b. (a -> b) -> a -> b
$ ParsecT String st Identity String
-> ParsecT String st Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (String -> ParsecT String st Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"->")) ParsecT String st Identity ()
-> ParsecT String st Identity () -> ParsecT String st Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                 ParsecT String st Identity () -> ParsecT String st Identity ()
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String st Identity String -> ParsecT String st Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT String st Identity String
 -> ParsecT String st Identity ())
-> ParsecT String st Identity String
-> ParsecT String st Identity ()
forall a b. (a -> b) -> a -> b
$ ParsecT String st Identity String
-> ParsecT String st Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (String -> ParsecT String st Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"::")) ParsecT String st Identity ()
-> ParsecT String st Identity () -> ParsecT String st Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                 ParsecT String st Identity () -> ParsecT String st Identity ()
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT String st Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof in
      ParsecT String () Identity JsonOperand
-> ParsecT String () Identity JsonOperand
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT String () Identity JsonOperand
forall u. ParsecT String u Identity JsonOperand
pJIdx ParsecT String () Identity JsonOperand
-> ParsecT String () Identity JsonOperand
-> ParsecT String () Identity JsonOperand
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT String () Identity JsonOperand
-> ParsecT String () Identity JsonOperand
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT String () Identity JsonOperand
pJKey

pField :: Parser Field
pField :: Parser Field
pField = Parser Field -> Parser Field
forall a. Parser a -> Parser a
lexeme (Parser Field -> Parser Field) -> Parser Field -> Parser Field
forall a b. (a -> b) -> a -> b
$ (,) (Text -> [JsonOperation] -> Field)
-> Parser Text
-> ParsecT String () Identity ([JsonOperation] -> Field)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
pFieldName ParsecT String () Identity ([JsonOperation] -> Field)
-> ParsecT String () Identity [JsonOperation] -> Parser Field
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [JsonOperation]
-> ParsecT String () Identity [JsonOperation]
-> ParsecT String () Identity [JsonOperation]
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [] ParsecT String () Identity [JsonOperation]
pJsonPath

aliasSeparator :: Parser ()
aliasSeparator :: GenParser Char () ()
aliasSeparator = Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
':' ParsecT String () Identity Char
-> GenParser Char () () -> GenParser Char () ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT String () Identity Char -> GenParser Char () ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
':')

pRelationSelect :: Parser SelectItem
pRelationSelect :: ParsecT String () Identity SelectItem
pRelationSelect = ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall a. Parser a -> Parser a
lexeme (ParsecT String () Identity SelectItem
 -> ParsecT String () Identity SelectItem)
-> ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall a b. (a -> b) -> a -> b
$ ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall tok st a. GenParser tok st a -> GenParser tok st a
try ( do
    Maybe Text
alias <- Parser Text -> ParsecT String () Identity (Maybe Text)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe ( Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try(Parser Text
pFieldName Parser Text -> GenParser Char () () -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* GenParser Char () ()
aliasSeparator) )
    Field
fld <- Parser Field
pField
    Maybe Text
hint <- Parser Text -> ParsecT String () Identity (Maybe Text)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (
        Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try ( Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'!' ParsecT String () Identity Char -> Parser Text -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text
pFieldName) Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        -- deprecated, remove in next major version
        Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try ( Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'.' ParsecT String () Identity Char -> Parser Text -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text
pFieldName)
      )
    SelectItem -> ParsecT String () Identity SelectItem
forall (m :: * -> *) a. Monad m => a -> m a
return (Field
fld, Maybe Text
forall a. Maybe a
Nothing, Maybe Text
alias, Maybe Text
hint)
  )

pFieldSelect :: Parser SelectItem
pFieldSelect :: ParsecT String () Identity SelectItem
pFieldSelect = ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall a. Parser a -> Parser a
lexeme (ParsecT String () Identity SelectItem
 -> ParsecT String () Identity SelectItem)
-> ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall a b. (a -> b) -> a -> b
$
  ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall tok st a. GenParser tok st a -> GenParser tok st a
try (
    do
      Maybe Text
alias <- Parser Text -> ParsecT String () Identity (Maybe Text)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe ( Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try(Parser Text
pFieldName Parser Text -> GenParser Char () () -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* GenParser Char () ()
aliasSeparator) )
      Field
fld <- Parser Field
pField
      Maybe String
cast' <- ParsecT String () Identity String
-> ParsecT String () Identity (Maybe String)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"::" ParsecT String () Identity String
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter)
      SelectItem -> ParsecT String () Identity SelectItem
forall (m :: * -> *) a. Monad m => a -> m a
return (Field
fld, String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text) -> Maybe String -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe String
cast', Maybe Text
alias, Maybe Text
forall a. Maybe a
Nothing)
  )
  ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
-> ParsecT String () Identity SelectItem
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> do
    Text
s <- Parser Text
pStar
    SelectItem -> ParsecT String () Identity SelectItem
forall (m :: * -> *) a. Monad m => a -> m a
return ((Text
s, []), Maybe Text
forall a. Maybe a
Nothing, Maybe Text
forall a. Maybe a
Nothing, Maybe Text
forall a. Maybe a
Nothing)

pOpExpr :: Parser SingleVal -> Parser OpExpr
pOpExpr :: Parser Text -> Parsec String () OpExpr
pOpExpr Parser Text
pSVal = Parsec String () OpExpr -> Parsec String () OpExpr
forall tok st a. GenParser tok st a -> GenParser tok st a
try ( String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"not" ParsecT String () Identity String
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> Parsec String () OpExpr -> Parsec String () OpExpr
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Bool -> Operation -> OpExpr
OpExpr Bool
True (Operation -> OpExpr)
-> ParsecT String () Identity Operation -> Parsec String () OpExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Operation
pOperation)) Parsec String () OpExpr
-> Parsec String () OpExpr -> Parsec String () OpExpr
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool -> Operation -> OpExpr
OpExpr Bool
False (Operation -> OpExpr)
-> ParsecT String () Identity Operation -> Parsec String () OpExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Operation
pOperation
  where
    pOperation :: Parser Operation
    pOperation :: ParsecT String () Identity Operation
pOperation =
          Text -> Text -> Operation
Op (Text -> Text -> Operation)
-> (String -> Text) -> String -> Text -> Operation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text -> Operation)
-> ParsecT String () Identity String
-> ParsecT String () Identity (Text -> Operation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT String () Identity String
 -> ParsecT String () Identity String
 -> ParsecT String () Identity String)
-> [ParsecT String () Identity String]
-> ParsecT String () Identity String
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 ParsecT String () Identity String
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (ParsecT String () Identity String
-> ParsecT String () Identity String
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity String
 -> ParsecT String () Identity String)
-> (Text -> ParsecT String () Identity String)
-> Text
-> ParsecT String () Identity String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ParsecT String () Identity String
-> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String () Identity Char
pDelimiter) (ParsecT String () Identity String
 -> ParsecT String () Identity String)
-> (String -> ParsecT String () Identity String)
-> String
-> ParsecT String () Identity String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string) (String -> ParsecT String () Identity String)
-> (Text -> String) -> Text -> ParsecT String () Identity String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a b. StringConv a b => a -> b
toS (Text -> ParsecT String () Identity String)
-> [Text] -> [ParsecT String () Identity String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Text ByteString -> [Text]
forall k v. HashMap k v -> [k]
M.keys HashMap Text ByteString
ops) ParsecT String () Identity (Text -> Operation)
-> Parser Text -> ParsecT String () Identity Operation
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text
pSVal
      ParsecT String () Identity Operation
-> ParsecT String () Identity Operation
-> ParsecT String () Identity Operation
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Text] -> Operation
In ([Text] -> Operation)
-> Parsec String () [Text] -> ParsecT String () Identity Operation
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT String () Identity Char -> ParsecT String () Identity Char
forall tok st a. GenParser tok st a -> GenParser tok st a
try (String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"in" ParsecT String () Identity String
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT String () Identity Char
pDelimiter) ParsecT String () Identity Char
-> Parsec String () [Text] -> Parsec String () [Text]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parsec String () [Text]
pListVal)
      ParsecT String () Identity Operation
-> ParsecT String () Identity Operation
-> ParsecT String () Identity Operation
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT String () Identity Operation
pFts
      ParsecT String () Identity Operation
-> String -> ParsecT String () Identity Operation
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"operator (eq, gt, ...)"

    pFts :: ParsecT String () Identity Operation
pFts = do
      String
op   <- (ParsecT String () Identity String
 -> ParsecT String () Identity String
 -> ParsecT String () Identity String)
-> [ParsecT String () Identity String]
-> ParsecT String () Identity String
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 ParsecT String () Identity String
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) (ParsecT String () Identity String
-> ParsecT String () Identity String
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity String
 -> ParsecT String () Identity String)
-> (Text -> ParsecT String () Identity String)
-> Text
-> ParsecT String () Identity String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string (String -> ParsecT String () Identity String)
-> (Text -> String) -> Text -> ParsecT String () Identity String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a b. StringConv a b => a -> b
toS (Text -> ParsecT String () Identity String)
-> [Text] -> [ParsecT String () Identity String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
ftsOps)
      Maybe String
lang <- ParsecT String () Identity String
-> ParsecT String () Identity (Maybe String)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (ParsecT String () Identity String
 -> ParsecT String () Identity (Maybe String))
-> ParsecT String () Identity String
-> ParsecT String () Identity (Maybe String)
forall a b. (a -> b) -> a -> b
$ ParsecT String () Identity String
-> ParsecT String () Identity String
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall s (m :: * -> *) t u open close a.
Stream s m t =>
ParsecT s u m open
-> ParsecT s u m close -> ParsecT s u m a -> ParsecT s u m a
between (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')') (ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
"_")))
      ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> ParsecT String () Identity Operation
-> ParsecT String () Identity Operation
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Maybe Text -> Text -> Operation
Fts (String -> Text
forall a b. StringConv a b => a -> b
toS String
op) (String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text) -> Maybe String -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe String
lang) (Text -> Operation)
-> Parser Text -> ParsecT String () Identity Operation
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
pSVal

    ops :: HashMap Text ByteString
ops = (Text -> ByteString -> Bool)
-> HashMap Text ByteString -> HashMap Text ByteString
forall k v. (k -> v -> Bool) -> HashMap k v -> HashMap k v
M.filterWithKey (Bool -> ByteString -> Bool
forall a b. a -> b -> a
const (Bool -> ByteString -> Bool)
-> (Text -> Bool) -> Text -> ByteString -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> [Text] -> Bool) -> [Text] -> Text -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
notElem (Text
"in"Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:[Text]
ftsOps)) HashMap Text ByteString
operators
    ftsOps :: [Text]
ftsOps = HashMap Text ByteString -> [Text]
forall k v. HashMap k v -> [k]
M.keys HashMap Text ByteString
ftsOperators

pSingleVal :: Parser SingleVal
pSingleVal :: Parser Text
pSingleVal = String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text)
-> ParsecT String () Identity String -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar

pListVal :: Parser ListVal
pListVal :: Parsec String () [Text]
pListVal = ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') ParsecT String () Identity Char
-> Parsec String () [Text] -> Parsec String () [Text]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text
pListElement Parser Text
-> ParsecT String () Identity Char -> Parsec String () [Text]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',' Parsec String () [Text]
-> ParsecT String () Identity Char -> Parsec String () [Text]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')')

pListElement :: Parser Text
pListElement :: Parser Text
pListElement = Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try (Parser Text
pQuotedValue Parser Text -> GenParser Char () () -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String () Identity Char -> GenParser Char () ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
",)")) Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text)
-> ParsecT String () Identity String -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
",)"))

pQuotedValue :: Parser Text
pQuotedValue :: Parser Text
pQuotedValue = String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text)
-> ParsecT String () Identity String -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'"' ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"\"") ParsecT String () Identity String
-> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'"')

pDelimiter :: Parser Char
pDelimiter :: ParsecT String () Identity Char
pDelimiter = Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'.' ParsecT String () Identity Char
-> String -> ParsecT String () Identity Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"delimiter (.)"

pOrder :: Parser [OrderTerm]
pOrder :: Parsec String () [OrderTerm]
pOrder = Parser OrderTerm -> Parser OrderTerm
forall a. Parser a -> Parser a
lexeme Parser OrderTerm
pOrderTerm Parser OrderTerm
-> ParsecT String () Identity Char -> Parsec String () [OrderTerm]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
','

pOrderTerm :: Parser OrderTerm
pOrderTerm :: Parser OrderTerm
pOrderTerm = do
  Field
fld <- Parser Field
pField
  Maybe OrderDirection
dir <- ParsecT String () Identity OrderDirection
-> ParsecT String () Identity (Maybe OrderDirection)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (ParsecT String () Identity OrderDirection
 -> ParsecT String () Identity (Maybe OrderDirection))
-> ParsecT String () Identity OrderDirection
-> ParsecT String () Identity (Maybe OrderDirection)
forall a b. (a -> b) -> a -> b
$
         ParsecT String () Identity OrderDirection
-> ParsecT String () Identity OrderDirection
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"asc" ParsecT String () Identity String
-> OrderDirection -> ParsecT String () Identity OrderDirection
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> OrderDirection
OrderAsc) ParsecT String () Identity OrderDirection
-> ParsecT String () Identity OrderDirection
-> ParsecT String () Identity OrderDirection
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         ParsecT String () Identity OrderDirection
-> ParsecT String () Identity OrderDirection
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"desc" ParsecT String () Identity String
-> OrderDirection -> ParsecT String () Identity OrderDirection
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> OrderDirection
OrderDesc)
  Maybe OrderNulls
nls <- ParsecT String () Identity OrderNulls
-> ParsecT String () Identity (Maybe OrderNulls)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe ParsecT String () Identity OrderNulls
pNulls ParsecT String () Identity (Maybe OrderNulls)
-> GenParser Char () ()
-> ParsecT String () Identity (Maybe OrderNulls)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* GenParser Char () ()
forall st. GenParser Char st ()
pEnd ParsecT String () Identity (Maybe OrderNulls)
-> ParsecT String () Identity (Maybe OrderNulls)
-> ParsecT String () Identity (Maybe OrderNulls)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
         GenParser Char () ()
forall st. GenParser Char st ()
pEnd GenParser Char () ()
-> Maybe OrderNulls
-> ParsecT String () Identity (Maybe OrderNulls)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Maybe OrderNulls
forall a. Maybe a
Nothing
  OrderTerm -> Parser OrderTerm
forall (m :: * -> *) a. Monad m => a -> m a
return (OrderTerm -> Parser OrderTerm) -> OrderTerm -> Parser OrderTerm
forall a b. (a -> b) -> a -> b
$ Field -> Maybe OrderDirection -> Maybe OrderNulls -> OrderTerm
OrderTerm Field
fld Maybe OrderDirection
dir Maybe OrderNulls
nls
  where
    pNulls :: ParsecT String () Identity OrderNulls
pNulls = ParsecT String () Identity OrderNulls
-> ParsecT String () Identity OrderNulls
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"nullsfirst" ParsecT String () Identity String
-> OrderNulls -> ParsecT String () Identity OrderNulls
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> OrderNulls
OrderNullsFirst) ParsecT String () Identity OrderNulls
-> ParsecT String () Identity OrderNulls
-> ParsecT String () Identity OrderNulls
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
             ParsecT String () Identity OrderNulls
-> ParsecT String () Identity OrderNulls
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"nullslast"  ParsecT String () Identity String
-> OrderNulls -> ParsecT String () Identity OrderNulls
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> OrderNulls
OrderNullsLast)
    pEnd :: ParsecT String st Identity ()
pEnd = ParsecT String st Identity () -> ParsecT String st Identity ()
forall tok st a. GenParser tok st a -> GenParser tok st a
try (ParsecT String st Identity Char -> ParsecT String st Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT String st Identity Char -> ParsecT String st Identity ())
-> ParsecT String st Identity Char -> ParsecT String st Identity ()
forall a b. (a -> b) -> a -> b
$ ParsecT String st Identity Char -> ParsecT String st Identity Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (Char -> ParsecT String st Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',')) ParsecT String st Identity ()
-> ParsecT String st Identity () -> ParsecT String st Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
           ParsecT String st Identity () -> ParsecT String st Identity ()
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT String st Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof

pLogicTree :: Parser LogicTree
pLogicTree :: Parsec String () LogicTree
pLogicTree = Filter -> LogicTree
Stmnt (Filter -> LogicTree)
-> ParsecT String () Identity Filter -> Parsec String () LogicTree
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Filter
-> ParsecT String () Identity Filter
forall tok st a. GenParser tok st a -> GenParser tok st a
try ParsecT String () Identity Filter
pLogicFilter
             Parsec String () LogicTree
-> Parsec String () LogicTree -> Parsec String () LogicTree
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool -> LogicOperator -> [LogicTree] -> LogicTree
Expr (Bool -> LogicOperator -> [LogicTree] -> LogicTree)
-> ParsecT String () Identity Bool
-> ParsecT
     String () Identity (LogicOperator -> [LogicTree] -> LogicTree)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Bool
pNot ParsecT
  String () Identity (LogicOperator -> [LogicTree] -> LogicTree)
-> ParsecT String () Identity LogicOperator
-> ParsecT String () Identity ([LogicTree] -> LogicTree)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT String () Identity LogicOperator
pLogicOp ParsecT String () Identity ([LogicTree] -> LogicTree)
-> ParsecT String () Identity [LogicTree]
-> Parsec String () LogicTree
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') ParsecT String () Identity Char
-> ParsecT String () Identity [LogicTree]
-> ParsecT String () Identity [LogicTree]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parsec String () LogicTree
pLogicTree Parsec String () LogicTree
-> ParsecT String () Identity Char
-> ParsecT String () Identity [LogicTree]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',') ParsecT String () Identity [LogicTree]
-> ParsecT String () Identity Char
-> ParsecT String () Identity [LogicTree]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')'))
  where
    pLogicFilter :: Parser Filter
    pLogicFilter :: ParsecT String () Identity Filter
pLogicFilter = Field -> OpExpr -> Filter
Filter (Field -> OpExpr -> Filter)
-> Parser Field -> ParsecT String () Identity (OpExpr -> Filter)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Field
pField ParsecT String () Identity (OpExpr -> Filter)
-> ParsecT String () Identity Char
-> ParsecT String () Identity (OpExpr -> Filter)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String () Identity Char
pDelimiter ParsecT String () Identity (OpExpr -> Filter)
-> Parsec String () OpExpr -> ParsecT String () Identity Filter
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text -> Parsec String () OpExpr
pOpExpr Parser Text
pLogicSingleVal
    pNot :: Parser Bool
    pNot :: ParsecT String () Identity Bool
pNot = ParsecT String () Identity Bool -> ParsecT String () Identity Bool
forall tok st a. GenParser tok st a -> GenParser tok st a
try (String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"not" ParsecT String () Identity String
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT String () Identity Char
pDelimiter ParsecT String () Identity Char
-> Bool -> ParsecT String () Identity Bool
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Bool
True)
           ParsecT String () Identity Bool
-> ParsecT String () Identity Bool
-> ParsecT String () Identity Bool
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool -> ParsecT String () Identity Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
           ParsecT String () Identity Bool
-> String -> ParsecT String () Identity Bool
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"negation operator (not)"
    pLogicOp :: Parser LogicOperator
    pLogicOp :: ParsecT String () Identity LogicOperator
pLogicOp = ParsecT String () Identity LogicOperator
-> ParsecT String () Identity LogicOperator
forall tok st a. GenParser tok st a -> GenParser tok st a
try (String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"and" ParsecT String () Identity String
-> LogicOperator -> ParsecT String () Identity LogicOperator
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> LogicOperator
And)
               ParsecT String () Identity LogicOperator
-> ParsecT String () Identity LogicOperator
-> ParsecT String () Identity LogicOperator
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"or" ParsecT String () Identity String
-> LogicOperator -> ParsecT String () Identity LogicOperator
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> LogicOperator
Or
               ParsecT String () Identity LogicOperator
-> String -> ParsecT String () Identity LogicOperator
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"logic operator (and, or)"

pLogicSingleVal :: Parser SingleVal
pLogicSingleVal :: Parser Text
pLogicSingleVal = Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try (Parser Text
pQuotedValue Parser Text -> GenParser Char () () -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT String () Identity Char -> GenParser Char () ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
",)")) Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Text -> Parser Text
forall tok st a. GenParser tok st a -> GenParser tok st a
try Parser Text
pPgArray Parser Text -> Parser Text -> Parser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text)
-> ParsecT String () Identity String -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
",)"))
  where
    pPgArray :: Parser Text
    pPgArray :: Parser Text
pPgArray =  do
      String
a <- String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"{"
      String
b <- ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"{}")
      String
c <- String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"}"
      Text -> Parser Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Text
forall a b. StringConv a b => a -> b
toS (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
b String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
c)

pLogicPath :: Parser (EmbedPath, Text)
pLogicPath :: Parsec String () ([Text], Text)
pLogicPath = do
  [Text]
path <- Parser Text
pFieldName Parser Text
-> ParsecT String () Identity Char -> Parsec String () [Text]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` ParsecT String () Identity Char
pDelimiter
  let op :: Text
op = [Text] -> Text
forall a. [a] -> a
last [Text]
path
      notOp :: Text
notOp = Text
"not." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
op
  ([Text], Text) -> Parsec String () ([Text], Text)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"not") ([Text] -> [Text]
forall a. [a] -> [a]
init [Text]
path), if Text
"not" Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
path then Text
notOp else Text
op)

pColumns :: Parser [FieldName]
pColumns :: Parsec String () [Text]
pColumns = Parser Text
pFieldName Parser Text
-> ParsecT String () Identity Char -> Parsec String () [Text]
forall s (m :: * -> *) t u a sep.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
`sepBy1` ParsecT String () Identity Char -> ParsecT String () Identity Char
forall a. Parser a -> Parser a
lexeme (Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',')

mapError :: Either ParseError a -> Either ApiRequestError a
mapError :: Either ParseError a -> Either ApiRequestError a
mapError = (ParseError -> ApiRequestError)
-> Either ParseError a -> Either ApiRequestError a
forall a c b. (a -> c) -> Either a b -> Either c b
mapLeft ParseError -> ApiRequestError
translateError
  where
    translateError :: ParseError -> ApiRequestError
translateError ParseError
e =
      Text -> Text -> ApiRequestError
ParseRequestError Text
message Text
details
      where
        message :: Text
message = SourcePos -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show (SourcePos -> Text) -> SourcePos -> Text
forall a b. (a -> b) -> a -> b
$ ParseError -> SourcePos
errorPos ParseError
e
        details :: Text
details = Text -> Text
strip (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text -> Text
replace Text
"\n" Text
" " (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a b. StringConv a b => a -> b
toS
           (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String
-> String -> String -> String -> String -> [Message] -> String
showErrorMessages String
"or" String
"unknown parse error" String
"expecting" String
"unexpected" String
"end of input" (ParseError -> [Message]
errorMessages ParseError
e)