postgresql-syntax-kronor-0.5.0.3: library-internal/PostgresqlSyntax/Ast/IndirectionEl.hs
module PostgresqlSyntax.Ast.IndirectionEl where
import qualified HeadedMegaparsec as Parser
import PostgresqlSyntax.Algebra
import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)
import PostgresqlSyntax.Ast.Ident
import qualified PostgresqlSyntax.Helpers.Gens as Gens
import qualified PostgresqlSyntax.Helpers.Parsers as Parsers
import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders
import qualified PostgresqlSyntax.KeywordSet as KeywordSet
import PostgresqlSyntax.Prelude
import PostgresqlSyntax.Settings (Settings, resolveHaskellParamFields)
import qualified Test.QuickCheck as Qc
import qualified TextBuilder
-- |
-- 'PostgresqlSyntax.Ast.AttrName' is a bare alias to 'PostgresqlSyntax.Ast.ColLabel',
-- which is a bare alias to 'Ident', but the @ColLabel@ /parser/ (kept in
-- "PostgresqlSyntax.Parsing" since @ColLabel@ itself isn't extracted in this
-- batch) is more permissive than plain 'Ident'. Since this module sits below
-- "PostgresqlSyntax.Parsing" (no import cycle allowed), that ColLabel-flavored
-- element parser is duplicated here, same as 'PostgresqlSyntax.Ast.NameList'.
--
-- ==== References
-- @
-- indirection_el:
-- | '.' attr_name
-- | '.' '*'
-- | '[' a_expr ']'
-- | '[' opt_slice_bound ':' opt_slice_bound ']'
-- opt_slice_bound:
-- | a_expr
-- | EMPTY
-- @
data IndirectionEl
= AttrNameIndirectionEl Ident
| AllIndirectionEl
| ExprIndirectionEl AExpr
| SliceIndirectionEl (Maybe AExpr) (Maybe AExpr)
| -- |
-- Kronor extension: a Haskell field selector, @.$name@. Only ever produced
-- by 'hsParser' - i.e. only after a @$n@ parameter placeholder, and only
-- when 'resolveHaskellParamFields' is on. Never generated by 'Qc.arbitrary'.
HsAttrNameIndirectionEl Text
deriving (Show, Generic, Eq, Ord, Data)
instance IsAst IndirectionEl where
toTextBuilder settings = \case
AttrNameIndirectionEl a -> "." <> toTextBuilder settings a
AllIndirectionEl -> ".*"
ExprIndirectionEl a -> TextBuilders.renderInBrackets (toTextBuilder settings a)
SliceIndirectionEl a b -> TextBuilders.renderInBrackets (foldMap (toTextBuilder settings) a <> ":" <> foldMap (toTextBuilder settings) b)
HsAttrNameIndirectionEl a -> ".$" <> TextBuilder.text a
parser settings = customizedParser settings empty
-- |
-- Like 'parser', but additionally accepts the Kronor @.$name@ Haskell field
-- selector when 'resolveHaskellParamFields' is on.
--
-- This is a separate entry point rather than an alternative inside 'parser'
-- on purpose: the extension is confined to the @$n@ parameter position (see
-- 'PostgresqlSyntax.Ast.CExpr'), so @col.$field@ must keep failing to parse
-- even with the option enabled.
hsParser :: Settings -> Parser IndirectionEl
hsParser settings =
customizedParser settings
$ if resolveHaskellParamFields settings then hsAttrName else empty
where
-- Deliberately as permissive as the pre-0.5 fork's @hsColLabel@: any run
-- of characters that is neither whitespace nor a colon. Terminating on
-- @:@ is what makes @$1.$field::int8@ work, and admitting @.@ is what
-- makes a qualified name a single field.
hsAttrName =
Parser.label "Haskell field name" $ do
Parsers.char '$'
HsAttrNameIndirectionEl <$> Parsers.takeWhile1P (Just "Haskell field name") (\x -> not (isSpace x || x == ':'))
-- |
-- Parameterized over an extra alternative for the @'.' attr_name@ branch,
-- tried before the standard ones. 'parser' passes 'empty'; 'hsParser' passes
-- the Haskell field selector.
customizedParser :: Settings -> Parser IndirectionEl -> Parser IndirectionEl
customizedParser settings extraAttrEl =
asum
[ do
Parsers.char '.'
Parser.endHead
Parsers.space
extraAttrEl <|> AllIndirectionEl <$ Parsers.char '*' <|> AttrNameIndirectionEl <$> colLabelLikeName,
do
Parsers.char '['
Parser.endHead
Parsers.space
a <-
asum
[ do
Parsers.char ':'
Parser.endHead
Parsers.space
b <- optional (parser settings)
return (SliceIndirectionEl Nothing b),
do
a <- parser settings
asum
[ do
Parsers.space
Parsers.char ':'
Parsers.space
b <- optional (parser settings)
return (SliceIndirectionEl (Just a) b),
return (ExprIndirectionEl a)
]
]
Parsers.space
Parsers.char ']'
return a
]
where
colLabelLikeName =
Parser.label "column label" $
Parsers.keywordNameFromSet UnquotedIdent KeywordSet.keyword
<|> parser settings
instance Qc.Arbitrary IndirectionEl where
-- Hand-written rather than 'Qc.genericShrink' because 'Text' has no
-- 'Qc.Arbitrary' instance and 'HsAttrNameIndirectionEl' carries one. The
-- extension constructor shrinks to nothing - it is never generated anyway.
shrink = \case
AttrNameIndirectionEl a -> AttrNameIndirectionEl <$> Qc.shrink a
AllIndirectionEl -> []
ExprIndirectionEl a -> ExprIndirectionEl <$> Qc.shrink a
SliceIndirectionEl a b ->
(SliceIndirectionEl <$> Qc.shrink a <*> pure b)
<> (SliceIndirectionEl a <$> Qc.shrink b)
HsAttrNameIndirectionEl _ -> []
arbitrary =
Qc.sized $ \size ->
if size <= 1
then
Qc.oneof
[ AttrNameIndirectionEl <$> Qc.arbitrary,
pure AllIndirectionEl,
pure (SliceIndirectionEl Nothing Nothing)
]
else
Qc.oneof
[ AttrNameIndirectionEl <$> Qc.arbitrary,
pure AllIndirectionEl,
ExprIndirectionEl <$> Gens.downscale Qc.arbitrary,
SliceIndirectionEl <$> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary
]