postgresql-syntax-kronor (empty) → 0.5.0.3
raw patch · 344 files changed
+13792/−0 lines, 344 filesdep +QuickCheckdep +basedep +bytestring
Dependencies added: QuickCheck, base, bytestring, case-insensitive, clock, containers, criterion, hashable, headed-megaparsec, hspec, megaparsec, parser-combinators, postgresql-syntax-kronor, rerebase, text, text-builder, unordered-containers
Files
- CHANGELOG.md +122/−0
- LICENSE +22/−0
- README.md +6/−0
- bench/Main.hs +124/−0
- hspec-test/Ast/AExprReversableOpSpec.hs +10/−0
- hspec-test/Ast/AExprSpec.hs +81/−0
- hspec-test/Ast/AexprConstSpec.hs +10/−0
- hspec-test/Ast/AliasClauseSpec.hs +10/−0
- hspec-test/Ast/AllOpSpec.hs +10/−0
- hspec-test/Ast/AnyNameSpec.hs +10/−0
- hspec-test/Ast/AnyOperatorSpec.hs +10/−0
- hspec-test/Ast/ArrayBoundsSpec.hs +10/−0
- hspec-test/Ast/ArrayExprListSpec.hs +10/−0
- hspec-test/Ast/ArrayExprSpec.hs +10/−0
- hspec-test/Ast/AscDescSpec.hs +10/−0
- hspec-test/Ast/AttrsSpec.hs +10/−0
- hspec-test/Ast/BExprIsOpSpec.hs +10/−0
- hspec-test/Ast/BExprSpec.hs +10/−0
- hspec-test/Ast/BconstSpec.hs +10/−0
- hspec-test/Ast/BitSpec.hs +10/−0
- hspec-test/Ast/CExprSpec.hs +11/−0
- hspec-test/Ast/CallStmtSpec.hs +10/−0
- hspec-test/Ast/CaseExprSpec.hs +10/−0
- hspec-test/Ast/CharacterSpec.hs +10/−0
- hspec-test/Ast/ColumnrefSpec.hs +10/−0
- hspec-test/Ast/CommonTableExprSpec.hs +10/−0
- hspec-test/Ast/ConfExprSpec.hs +10/−0
- hspec-test/Ast/ConstCharacterSpec.hs +10/−0
- hspec-test/Ast/ConstDatetimeSpec.hs +10/−0
- hspec-test/Ast/ConstTypenameSpec.hs +10/−0
- hspec-test/Ast/DeleteStmtSpec.hs +13/−0
- hspec-test/Ast/ExplicitRowSpec.hs +10/−0
- hspec-test/Ast/ExprListSpec.hs +10/−0
- hspec-test/Ast/ExtractArgSpec.hs +10/−0
- hspec-test/Ast/ExtractListSpec.hs +10/−0
- hspec-test/Ast/FconstSpec.hs +10/−0
- hspec-test/Ast/ForLockingClauseSpec.hs +10/−0
- hspec-test/Ast/ForLockingItemSpec.hs +10/−0
- hspec-test/Ast/ForLockingStrengthSpec.hs +10/−0
- hspec-test/Ast/FrameBoundSpec.hs +27/−0
- hspec-test/Ast/FrameClauseModeSpec.hs +10/−0
- hspec-test/Ast/FrameClauseSpec.hs +10/−0
- hspec-test/Ast/FrameExtentSpec.hs +10/−0
- hspec-test/Ast/FromClauseSpec.hs +10/−0
- hspec-test/Ast/FromListSpec.hs +10/−0
- hspec-test/Ast/FuncAliasClauseSpec.hs +10/−0
- hspec-test/Ast/FuncApplicationParamsSpec.hs +10/−0
- hspec-test/Ast/FuncApplicationSpec.hs +10/−0
- hspec-test/Ast/FuncArgExprSpec.hs +10/−0
- hspec-test/Ast/FuncConstArgsSpec.hs +10/−0
- hspec-test/Ast/FuncExprCommonSubexprSpec.hs +10/−0
- hspec-test/Ast/FuncExprSpec.hs +10/−0
- hspec-test/Ast/FuncExprWindowlessSpec.hs +10/−0
- hspec-test/Ast/FuncNameSpec.hs +10/−0
- hspec-test/Ast/FuncTableSpec.hs +10/−0
- hspec-test/Ast/GenericTypeSpec.hs +10/−0
- hspec-test/Ast/GroupByItemSpec.hs +10/−0
- hspec-test/Ast/GroupClauseSpec.hs +10/−0
- hspec-test/Ast/HavingClauseSpec.hs +10/−0
- hspec-test/Ast/HsTargetElSpec.hs +45/−0
- hspec-test/Ast/HsTargetListSpec.hs +18/−0
- hspec-test/Ast/IconstSpec.hs +10/−0
- hspec-test/Ast/IdentSpec.hs +10/−0
- hspec-test/Ast/ImplicitRowSpec.hs +10/−0
- hspec-test/Ast/InExprSpec.hs +11/−0
- hspec-test/Ast/IndexElemDefSpec.hs +10/−0
- hspec-test/Ast/IndexElemSpec.hs +24/−0
- hspec-test/Ast/IndexParamsSpec.hs +10/−0
- hspec-test/Ast/IndirectionElSpec.hs +10/−0
- hspec-test/Ast/IndirectionSpec.hs +10/−0
- hspec-test/Ast/InsertColumnItemSpec.hs +10/−0
- hspec-test/Ast/InsertColumnListSpec.hs +10/−0
- hspec-test/Ast/InsertRestSpec.hs +14/−0
- hspec-test/Ast/InsertStmtSpec.hs +12/−0
- hspec-test/Ast/InsertTargetSpec.hs +10/−0
- hspec-test/Ast/IntervalSecondSpec.hs +10/−0
- hspec-test/Ast/IntervalSpec.hs +10/−0
- hspec-test/Ast/IntoClauseSpec.hs +10/−0
- hspec-test/Ast/JoinQualSpec.hs +10/−0
- hspec-test/Ast/JoinTypeSpec.hs +10/−0
- hspec-test/Ast/JoinedTableSpec.hs +10/−0
- hspec-test/Ast/LimitClauseSpec.hs +10/−0
- hspec-test/Ast/MathOpSpec.hs +10/−0
- hspec-test/Ast/NameListSpec.hs +10/−0
- hspec-test/Ast/NullsOrderSpec.hs +10/−0
- hspec-test/Ast/NumericSpec.hs +10/−0
- hspec-test/Ast/OffsetClauseSpec.hs +10/−0
- hspec-test/Ast/OnConflictDoSpec.hs +10/−0
- hspec-test/Ast/OnConflictSpec.hs +10/−0
- hspec-test/Ast/OpSpec.hs +11/−0
- hspec-test/Ast/OptOrdinalitySpec.hs +10/−0
- hspec-test/Ast/OptTempTableNameSpec.hs +10/−0
- hspec-test/Ast/OptVaryingSpec.hs +8/−0
- hspec-test/Ast/OverClauseSpec.hs +10/−0
- hspec-test/Ast/OverlayListSpec.hs +10/−0
- hspec-test/Ast/OverrideKindSpec.hs +10/−0
- hspec-test/Ast/PositionListSpec.hs +10/−0
- hspec-test/Ast/PreparableStmtSpec.hs +103/−0
- hspec-test/Ast/QualAllOpSpec.hs +10/−0
- hspec-test/Ast/QualOpSpec.hs +10/−0
- hspec-test/Ast/QualifiedNameSpec.hs +10/−0
- hspec-test/Ast/RelationExprOptAliasSpec.hs +10/−0
- hspec-test/Ast/RelationExprSpec.hs +10/−0
- hspec-test/Ast/ReturningClauseSpec.hs +10/−0
- hspec-test/Ast/RowSpec.hs +10/−0
- hspec-test/Ast/RowsfromItemSpec.hs +10/−0
- hspec-test/Ast/RowsfromListSpec.hs +10/−0
- hspec-test/Ast/SconstSpec.hs +15/−0
- hspec-test/Ast/SelectBinOpSpec.hs +10/−0
- hspec-test/Ast/SelectClauseSpec.hs +12/−0
- hspec-test/Ast/SelectFetchFirstValueSpec.hs +10/−0
- hspec-test/Ast/SelectLimitSpec.hs +10/−0
- hspec-test/Ast/SelectLimitValueSpec.hs +10/−0
- hspec-test/Ast/SelectNoParensSpec.hs +12/−0
- hspec-test/Ast/SelectStmtSpec.hs +10/−0
- hspec-test/Ast/SelectWithParensSpec.hs +19/−0
- hspec-test/Ast/SetClauseListSpec.hs +10/−0
- hspec-test/Ast/SetClauseSpec.hs +10/−0
- hspec-test/Ast/SetTargetListSpec.hs +10/−0
- hspec-test/Ast/SetTargetSpec.hs +10/−0
- hspec-test/Ast/SimpleSelectSpec.hs +46/−0
- hspec-test/Ast/SimpleTypenameSpec.hs +10/−0
- hspec-test/Ast/SortBySpec.hs +22/−0
- hspec-test/Ast/SortClauseSpec.hs +10/−0
- hspec-test/Ast/SubTypeSpec.hs +10/−0
- hspec-test/Ast/SubqueryOpSpec.hs +10/−0
- hspec-test/Ast/SubstrListFromForSpec.hs +10/−0
- hspec-test/Ast/SubstrListSpec.hs +10/−0
- hspec-test/Ast/SymbolicExprBinOpSpec.hs +10/−0
- hspec-test/Ast/TableFuncElementListSpec.hs +10/−0
- hspec-test/Ast/TableFuncElementSpec.hs +10/−0
- hspec-test/Ast/TableRefSpec.hs +12/−0
- hspec-test/Ast/TablesampleClauseSpec.hs +10/−0
- hspec-test/Ast/TargetElSpec.hs +10/−0
- hspec-test/Ast/TargetListSpec.hs +10/−0
- hspec-test/Ast/TargetingSpec.hs +10/−0
- hspec-test/Ast/TimezoneSpec.hs +10/−0
- hspec-test/Ast/TrimListSpec.hs +10/−0
- hspec-test/Ast/TrimModifierSpec.hs +10/−0
- hspec-test/Ast/TypeListSpec.hs +10/−0
- hspec-test/Ast/TypenameArrayDimensionsSpec.hs +8/−0
- hspec-test/Ast/TypenameSpec.hs +46/−0
- hspec-test/Ast/UpdateStmtSpec.hs +12/−0
- hspec-test/Ast/UsingClauseSpec.hs +10/−0
- hspec-test/Ast/ValuesClauseSpec.hs +10/−0
- hspec-test/Ast/VerbalExprBinOpSpec.hs +10/−0
- hspec-test/Ast/WhenClauseListSpec.hs +10/−0
- hspec-test/Ast/WhenClauseSpec.hs +10/−0
- hspec-test/Ast/WhereClauseSpec.hs +10/−0
- hspec-test/Ast/WhereOrCurrentClauseSpec.hs +10/−0
- hspec-test/Ast/WindowClauseSpec.hs +10/−0
- hspec-test/Ast/WindowDefinitionSpec.hs +10/−0
- hspec-test/Ast/WindowExclusionClauseSpec.hs +10/−0
- hspec-test/Ast/WindowSpecificationSpec.hs +20/−0
- hspec-test/Ast/WithClauseSpec.hs +10/−0
- hspec-test/Ast/XconstSpec.hs +10/−0
- hspec-test/Helpers/Expectations.hs +77/−0
- hspec-test/Helpers/Specs.hs +110/−0
- hspec-test/LoweringSpec.hs +210/−0
- hspec-test/Main.hs +8/−0
- hspec-test/Spec.hs +1/−0
- library-internal/PostgresqlSyntax/Algebra.hs +270/−0
- library-internal/PostgresqlSyntax/Ast.hs +385/−0
- library-internal/PostgresqlSyntax/Ast/AExpr.hs +433/−0
- library-internal/PostgresqlSyntax/Ast/AExpr.hs-boot +22/−0
- library-internal/PostgresqlSyntax/Ast/AExprReversableOp.hs +113/−0
- library-internal/PostgresqlSyntax/Ast/AexprConst.hs +126/−0
- library-internal/PostgresqlSyntax/Ast/AliasClause.hs +51/−0
- library-internal/PostgresqlSyntax/Ast/AllOp.hs +37/−0
- library-internal/PostgresqlSyntax/Ast/AnyName.hs +43/−0
- library-internal/PostgresqlSyntax/Ast/AnyOperator.hs +56/−0
- library-internal/PostgresqlSyntax/Ast/ArrayBounds.hs +28/−0
- library-internal/PostgresqlSyntax/Ast/ArrayExpr.hs +50/−0
- library-internal/PostgresqlSyntax/Ast/ArrayExpr.hs-boot +19/−0
- library-internal/PostgresqlSyntax/Ast/ArrayExprList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/AscDesc.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Attrs.hs +45/−0
- library-internal/PostgresqlSyntax/Ast/BExpr.hs +161/−0
- library-internal/PostgresqlSyntax/Ast/BExpr.hs-boot +19/−0
- library-internal/PostgresqlSyntax/Ast/BExprIsOp.hs +55/−0
- library-internal/PostgresqlSyntax/Ast/Bconst.hs +33/−0
- library-internal/PostgresqlSyntax/Ast/Bit.hs +43/−0
- library-internal/PostgresqlSyntax/Ast/CExpr.hs +198/−0
- library-internal/PostgresqlSyntax/Ast/CallStmt.hs +22/−0
- library-internal/PostgresqlSyntax/Ast/CaseExpr.hs +54/−0
- library-internal/PostgresqlSyntax/Ast/Character.hs +57/−0
- library-internal/PostgresqlSyntax/Ast/Columnref.hs +32/−0
- library-internal/PostgresqlSyntax/Ast/CommonTableExpr.hs +59/−0
- library-internal/PostgresqlSyntax/Ast/ConfExpr.hs +51/−0
- library-internal/PostgresqlSyntax/Ast/ConstCharacter.hs +41/−0
- library-internal/PostgresqlSyntax/Ast/ConstDatetime.hs +69/−0
- library-internal/PostgresqlSyntax/Ast/ConstTypename.hs +49/−0
- library-internal/PostgresqlSyntax/Ast/DeleteStmt.hs +55/−0
- library-internal/PostgresqlSyntax/Ast/ExplicitRow.hs +41/−0
- library-internal/PostgresqlSyntax/Ast/ExprList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/ExtractArg.hs +68/−0
- library-internal/PostgresqlSyntax/Ast/ExtractList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Fconst.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/ForLockingClause.hs +41/−0
- library-internal/PostgresqlSyntax/Ast/ForLockingItem.hs +49/−0
- library-internal/PostgresqlSyntax/Ast/ForLockingStrength.hs +48/−0
- library-internal/PostgresqlSyntax/Ast/FrameBound.hs +56/−0
- library-internal/PostgresqlSyntax/Ast/FrameClause.hs +40/−0
- library-internal/PostgresqlSyntax/Ast/FrameClauseMode.hs +34/−0
- library-internal/PostgresqlSyntax/Ast/FrameExtent.hs +37/−0
- library-internal/PostgresqlSyntax/Ast/FromClause.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/FromList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/FuncAliasClause.hs +83/−0
- library-internal/PostgresqlSyntax/Ast/FuncApplication.hs +39/−0
- library-internal/PostgresqlSyntax/Ast/FuncApplicationParams.hs +114/−0
- library-internal/PostgresqlSyntax/Ast/FuncArgExpr.hs +70/−0
- library-internal/PostgresqlSyntax/Ast/FuncConstArgs.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/FuncExpr.hs +78/−0
- library-internal/PostgresqlSyntax/Ast/FuncExprCommonSubexpr.hs +199/−0
- library-internal/PostgresqlSyntax/Ast/FuncExprCommonSubexpr.hs-boot +19/−0
- library-internal/PostgresqlSyntax/Ast/FuncExprWindowless.hs +37/−0
- library-internal/PostgresqlSyntax/Ast/FuncName.hs +60/−0
- library-internal/PostgresqlSyntax/Ast/FuncTable.hs +50/−0
- library-internal/PostgresqlSyntax/Ast/GenericType.hs +45/−0
- library-internal/PostgresqlSyntax/Ast/GroupByItem.hs +68/−0
- library-internal/PostgresqlSyntax/Ast/GroupClause.hs +35/−0
- library-internal/PostgresqlSyntax/Ast/HavingClause.hs +31/−0
- library-internal/PostgresqlSyntax/Ast/HsTargetEl.hs +138/−0
- library-internal/PostgresqlSyntax/Ast/HsTargetList.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/Iconst.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Ident.hs +103/−0
- library-internal/PostgresqlSyntax/Ast/ImplicitRow.hs +38/−0
- library-internal/PostgresqlSyntax/Ast/InExpr.hs +69/−0
- library-internal/PostgresqlSyntax/Ast/IndexElem.hs +58/−0
- library-internal/PostgresqlSyntax/Ast/IndexElemDef.hs +46/−0
- library-internal/PostgresqlSyntax/Ast/IndexParams.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Indirection.hs +36/−0
- library-internal/PostgresqlSyntax/Ast/IndirectionEl.hs +151/−0
- library-internal/PostgresqlSyntax/Ast/InsertColumnItem.hs +32/−0
- library-internal/PostgresqlSyntax/Ast/InsertColumnList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/InsertRest.hs +72/−0
- library-internal/PostgresqlSyntax/Ast/InsertStmt.hs +57/−0
- library-internal/PostgresqlSyntax/Ast/InsertTarget.hs +31/−0
- library-internal/PostgresqlSyntax/Ast/Interval.hs +94/−0
- library-internal/PostgresqlSyntax/Ast/IntervalSecond.hs +36/−0
- library-internal/PostgresqlSyntax/Ast/IntoClause.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/JoinQual.hs +40/−0
- library-internal/PostgresqlSyntax/Ast/JoinType.hs +60/−0
- library-internal/PostgresqlSyntax/Ast/JoinedTable.hs +141/−0
- library-internal/PostgresqlSyntax/Ast/JoinedTable.hs-boot +3/−0
- library-internal/PostgresqlSyntax/Ast/LimitClause.hs +100/−0
- library-internal/PostgresqlSyntax/Ast/MathOp.hs +76/−0
- library-internal/PostgresqlSyntax/Ast/NameList.hs +43/−0
- library-internal/PostgresqlSyntax/Ast/NullsOrder.hs +28/−0
- library-internal/PostgresqlSyntax/Ast/Numeric.hs +99/−0
- library-internal/PostgresqlSyntax/Ast/OffsetClause.hs +56/−0
- library-internal/PostgresqlSyntax/Ast/OnConflict.hs +40/−0
- library-internal/PostgresqlSyntax/Ast/OnConflictDo.hs +57/−0
- library-internal/PostgresqlSyntax/Ast/Op.hs +36/−0
- library-internal/PostgresqlSyntax/Ast/OptOrdinality.hs +24/−0
- library-internal/PostgresqlSyntax/Ast/OptTempTableName.hs +85/−0
- library-internal/PostgresqlSyntax/Ast/OptVarying.hs +24/−0
- library-internal/PostgresqlSyntax/Ast/OverClause.hs +43/−0
- library-internal/PostgresqlSyntax/Ast/OverlayList.hs +58/−0
- library-internal/PostgresqlSyntax/Ast/OverrideKind.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/PositionList.hs +26/−0
- library-internal/PostgresqlSyntax/Ast/PreparableStmt.hs +55/−0
- library-internal/PostgresqlSyntax/Ast/QualAllOp.hs +39/−0
- library-internal/PostgresqlSyntax/Ast/QualOp.hs +38/−0
- library-internal/PostgresqlSyntax/Ast/QualifiedName.hs +43/−0
- library-internal/PostgresqlSyntax/Ast/RelationExpr.hs +55/−0
- library-internal/PostgresqlSyntax/Ast/RelationExprOptAlias.hs +43/−0
- library-internal/PostgresqlSyntax/Ast/ReturningClause.hs +33/−0
- library-internal/PostgresqlSyntax/Ast/Row.hs +34/−0
- library-internal/PostgresqlSyntax/Ast/RowsfromItem.hs +39/−0
- library-internal/PostgresqlSyntax/Ast/RowsfromList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Sconst.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/SelectBinOp.hs +32/−0
- library-internal/PostgresqlSyntax/Ast/SelectClause.hs +74/−0
- library-internal/PostgresqlSyntax/Ast/SelectClause.hs-boot +19/−0
- library-internal/PostgresqlSyntax/Ast/SelectFetchFirstValue.hs +61/−0
- library-internal/PostgresqlSyntax/Ast/SelectLimit.hs +51/−0
- library-internal/PostgresqlSyntax/Ast/SelectLimitValue.hs +38/−0
- library-internal/PostgresqlSyntax/Ast/SelectNoParens.hs +106/−0
- library-internal/PostgresqlSyntax/Ast/SelectNoParens.hs-boot +22/−0
- library-internal/PostgresqlSyntax/Ast/SelectStmt.hs +39/−0
- library-internal/PostgresqlSyntax/Ast/SelectWithParens.hs +70/−0
- library-internal/PostgresqlSyntax/Ast/SelectWithParens.hs-boot +21/−0
- library-internal/PostgresqlSyntax/Ast/SetClause.hs +53/−0
- library-internal/PostgresqlSyntax/Ast/SetClauseList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/SetTarget.hs +32/−0
- library-internal/PostgresqlSyntax/Ast/SetTargetList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/SimpleSelect.hs +232/−0
- library-internal/PostgresqlSyntax/Ast/SimpleSelect.hs-boot +26/−0
- library-internal/PostgresqlSyntax/Ast/SimpleTypename.hs +74/−0
- library-internal/PostgresqlSyntax/Ast/SortBy.hs +54/−0
- library-internal/PostgresqlSyntax/Ast/SortClause.hs +35/−0
- library-internal/PostgresqlSyntax/Ast/SubType.hs +33/−0
- library-internal/PostgresqlSyntax/Ast/SubqueryOp.hs +53/−0
- library-internal/PostgresqlSyntax/Ast/SubstrList.hs +45/−0
- library-internal/PostgresqlSyntax/Ast/SubstrListFromFor.hs +63/−0
- library-internal/PostgresqlSyntax/Ast/SymbolicExprBinOp.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/TableFuncElement.hs +40/−0
- library-internal/PostgresqlSyntax/Ast/TableFuncElementList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/TableRef.hs +165/−0
- library-internal/PostgresqlSyntax/Ast/TableRef.hs-boot +24/−0
- library-internal/PostgresqlSyntax/Ast/TablesampleClause.hs +47/−0
- library-internal/PostgresqlSyntax/Ast/TargetEl.hs +70/−0
- library-internal/PostgresqlSyntax/Ast/TargetList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Targeting.hs +66/−0
- library-internal/PostgresqlSyntax/Ast/Timezone.hs +30/−0
- library-internal/PostgresqlSyntax/Ast/TrimList.hs +45/−0
- library-internal/PostgresqlSyntax/Ast/TrimModifier.hs +33/−0
- library-internal/PostgresqlSyntax/Ast/TypeList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/Typename.hs +46/−0
- library-internal/PostgresqlSyntax/Ast/TypenameArrayDimensions.hs +45/−0
- library-internal/PostgresqlSyntax/Ast/UpdateStmt.hs +65/−0
- library-internal/PostgresqlSyntax/Ast/UsingClause.hs +28/−0
- library-internal/PostgresqlSyntax/Ast/ValuesClause.hs +40/−0
- library-internal/PostgresqlSyntax/Ast/VerbalExprBinOp.hs +38/−0
- library-internal/PostgresqlSyntax/Ast/WhenClause.hs +35/−0
- library-internal/PostgresqlSyntax/Ast/WhenClauseList.hs +27/−0
- library-internal/PostgresqlSyntax/Ast/WhereClause.hs +31/−0
- library-internal/PostgresqlSyntax/Ast/WhereOrCurrentClause.hs +52/−0
- library-internal/PostgresqlSyntax/Ast/WindowClause.hs +32/−0
- library-internal/PostgresqlSyntax/Ast/WindowDefinition.hs +26/−0
- library-internal/PostgresqlSyntax/Ast/WindowExclusionClause.hs +49/−0
- library-internal/PostgresqlSyntax/Ast/WindowSpecification.hs +79/−0
- library-internal/PostgresqlSyntax/Ast/WithClause.hs +36/−0
- library-internal/PostgresqlSyntax/Ast/WithClause.hs-boot +19/−0
- library-internal/PostgresqlSyntax/Ast/Xconst.hs +34/−0
- library-internal/PostgresqlSyntax/CharSet.hs +21/−0
- library-internal/PostgresqlSyntax/Extras/Error.hs +40/−0
- library-internal/PostgresqlSyntax/Extras/HeadedMegaparsec.hs +133/−0
- library-internal/PostgresqlSyntax/Extras/NonEmpty.hs +33/−0
- library-internal/PostgresqlSyntax/Extras/TextBuilder.hs +16/−0
- library-internal/PostgresqlSyntax/Helpers/Gens.hs +51/−0
- library-internal/PostgresqlSyntax/Helpers/Parsers.hs +229/−0
- library-internal/PostgresqlSyntax/Helpers/Shrinks.hs +30/−0
- library-internal/PostgresqlSyntax/Helpers/TextBuilders.hs +35/−0
- library-internal/PostgresqlSyntax/KeywordSet.hs +66/−0
- library-internal/PostgresqlSyntax/Lowering.hs +235/−0
- library-internal/PostgresqlSyntax/Predicate.hs +71/−0
- library-internal/PostgresqlSyntax/Prelude.hs +95/−0
- library-internal/PostgresqlSyntax/Settings.hs +81/−0
- library-internal/PostgresqlSyntax/Validation.hs +54/−0
- library/PostgresqlSyntax.hs +41/−0
- nesting-bench/Main.hs +79/−0
- postgresql-syntax-kronor.cabal +473/−0
+ CHANGELOG.md view
@@ -0,0 +1,122 @@+# v0.5.0.3++## Fixes++- Fixed subquery operators requiring whitespace around them: unspaced forms like `a=ANY(b)` used to be a parse error and now parse, matching Postgres (#36).++# v0.5.0.2++## Non-breaking++- `PostgresqlSyntax.Algebra`'s `LeftRecursion base ext item` class (internal-library-only, not part of the public `PostgresqlSyntax` API) is now a single-method `Extends base ext` - drops the unread `item` type parameter: `extension`/`applyExtension`/`foldExtensions` are gone in favor of one `parseExtensions` method; `nonRecursiveParser` → `parseBase`; `parseLeftRecursive` → `parseMaybeExtended`; `leftRecursionProperties` → `extendedByProperties`. `JoinedTableExtension` is deleted (#33).+- `TableRef.hs` and `FuncApplicationParams.hs` now have explicit export lists, exporting only their types and instances (#30).++## Fixes++- Fixed the parsed tree shape of `UNION`/`INTERSECT`/`EXCEPT` chains to match Postgres's actual associativity and precedence (`gram.y`'s `%left UNION EXCEPT` / `%left INTERSECT`, INTERSECT binding tighter): `a EXCEPT b EXCEPT c` now nests left instead of right, and `a INTERSECT b UNION c` now roots at `UNION` instead of `INTERSECT`. Rendered text is unaffected; only `SimpleSelect`'s parsed/canonical tree shape for such chains changes (#30).+- Fixed a further associativity bug in the same fold: a run of two or more consecutive `INTERSECT`s (e.g. `a INTERSECT b INTERSECT c`) nested right instead of left, contradicting `gram.y`'s `%left INTERSECT`. Parsed/canonical tree shape changes for such chains; rendered text is unaffected (#34).+- Fixed `InsertRest` misparsing parenthesized `VALUES`/`SELECT` as a column list (#35).++# v0.5.0.1++## Fixes++- Added doc-files to the .cabal-file. README.md, LICENSE and CHANGELOG.md.++# v0.5.0.0++## Breaking++- `IsAst` class methods `parser`/`toTextBuilder` now take a `Settings` parameter+ to control parse/render options (e.g. nullability `?` markers). Migration:+ `parse x` -> `parse mempty x`, `toText x` -> `toText mempty x`,+ `parseWithPosError x` -> `parseWithPosError mempty x`. New public module+ `PostgresqlSyntax.Settings` provides the `Settings` type and the+ `nullabilityMarkers` constructor.+- Nullability `?` markers in `Typename` are now opt-in via+ `nullabilityMarkers True`. In standard mode (`mempty`) they are not+ recognized - both flags parse as `False` and a literal `?` in their+ position is a parse error. Markers no longer accept a preceding space:+ `int ?` -> `int?`. Extended mode gives up one spelling of real Postgres:+ `x::jsonb? 'b'` (unspaced jsonb key-existence operator); use+ `x::jsonb ? 'b'` or `jsonb_exists(x, 'b')`.+- Removed `PostgresqlSyntax.Parsing` and `PostgresqlSyntax.Rendering`. Every+ type's parser and renderer are now the `parser`/`toTextBuilder` methods of+ its `IsAst` instance (exported from `PostgresqlSyntax`), and the top-level+ entry points moved to the `PostgresqlSyntax` module: `Parsing.run`/`.runWithPosError`+ became `PostgresqlSyntax.parse`/`.parseWithPosError`, generalized to work over+ any `IsAst` type rather than taking an explicit parser argument.+- `PostgresqlSyntax.KeywordSet` and `PostgresqlSyntax.Validation` are no longer+ part of the public API surface.+- Several former type aliases are now distinct ADTs/newtypes instead of bare+ `Either`/`Maybe`/primitive aliases: `SelectStmt`, `SelectClause`,+ `ExplicitRow`, and the primitive-wrapper newtypes `Sconst`, `Bconst`,+ `Xconst`, `Iconst`, `Fconst`, `Op`, `OptVarying`, `Timezone`,+ `IntervalSecond`, `OptOrdinality`. Code that pattern-matched these as+ `Either`/`Maybe`/`Text`/`Bool` directly needs to match on the new+ constructors instead.+- Removed the `SuffixQualOpAExpr` constructor of `AExpr`. It modelled the+ postfix operator production (`a_expr qual_Op`), which Postgres removed in+ version 14 - `x OPERATOR(pg_catalog.+#)` is a syntax error in every+ supported server version, so the parser no longer accepts it and the+ renderer can no longer emit it. Code pattern-matching or constructing+ `SuffixQualOpAExpr` needs to drop those cases. This also removes a family+ of round-trip failures: the rendering `<operand> <operator>` left the+ operator without a right-hand side, so reparsing swallowed whatever+ keyword followed (`PRECEDING`, `FOLLOWING`, `ROWS`, an implicit column+ alias, or - in the postfix case specifically - the `?` of the `Typename`+ nullability extension) as its operand. Only the postfix-triggered `?`+ swallow is fixed by this; `Op "?"` colliding with the nullability+ extension as a *binary* operator is a separate, still-open issue.++## Fixes++- Fix the `AExpr`/`BExpr` renderers (and related expression renderers) being+ precedence-naive: operators were concatenated without parenthesization, so a+ non-canonical but valid AST (e.g. `(NOT x) - y`) could render to SQL that+ re-parses to a different tree (`NOT (x - y)`). Renderers now parenthesize+ based on operator precedence, making render -> parse round-trip safe for the+ full value space of each type, not just parser-canonical trees.++# v0.4.5.0++## Non-breaking++- Derive `Data` for every AST type in `PostgresqlSyntax.Ast` (#6). Enables+ generic (SYB-style) traversals and transformations over the syntax tree,+ e.g. for expression normalization.+- Add `parseWithSourcePosError`, like `parseWithPosError` but pairing each+ error with a `Text.Megaparsec.SourcePos` instead of a raw `Int` byte+ offset, so callers (e.g. `hasql-th`, see+ [nikita-volkov/hasql-th#35](https://github.com/nikita-volkov/hasql-th/issues/35))+ can report line/column positions without recomputing them from the input.++# v0.4.4.0++## Fixes++- Fix parsing time growing exponentially with the nesting depth of an expression (#8).+ Inputs as small as `((((((((((((a + b))))))))))))` previously did not finish parsing;+ they now parse in well under a millisecond. Three grammar alternatives each parsed+ the content of a parenthesised group before discovering they did not apply, tripling+ the work per level of nesting; they are now left-factored so each group is parsed once.+ Parsing time is linear in input size, with a quadratic term in nesting depth alone+ (1000 characters of pure nesting take about a second; realistic input is unaffected).++## Non-breaking++- Redundant parentheses around a sub-select now parse to a canonical shape.+ `((select 1))` produces `WithParensSelectWithParens`; the equivalent+ `NoParensSelectWithParens` of a `SelectNoParens` carrying nothing but that same+ parenthesised select is no longer produced. The two rendered identically, so this+ only affects which of two equivalent trees you get back. Trees that carry a+ set operation, sort clause, limit or locking clause around the parenthesised+ select are unaffected.++# v0.4.3.2++## Fixes++- Fix keyword error reporting under megaparsec >=9.8 (#20)+- Fix OFFSET/aExpr round-trip failure for `OPERATOR(...)` prefix (#11, #22)+- Fix hedgehog generator for `type_function_name` to use its own keyword set
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2020, Nikita Volkov++Permission is hereby granted, free of charge, to any person+obtaining a copy of this software and associated documentation+files (the "Software"), to deal in the Software without+restriction, including without limitation the rights to use,+copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the+Software is furnished to do so, subject to the following+conditions:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR+OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,6 @@+# postgresql-syntax++[](https://hackage.haskell.org/package/postgresql-syntax)+[](https://nikita-volkov.github.io/postgresql-syntax/)++Postgres syntax tree and related utils extracted from [the "hasql-th" package](https://github.com/nikita-volkov/hasql-th).
+ bench/Main.hs view
@@ -0,0 +1,124 @@+{-# OPTIONS_GHC -Wno-missing-signatures #-}++-- |+-- Benchmark for the "keyword allocation cost in wide alternations" concern+-- raised in <https://github.com/nikita-volkov/postgresql-syntax/issues/21>.+--+-- Compares the shipped, cheap 'PostgresqlSyntax.Helpers.Parsers.keyword'+-- (a bare 'empty' on mismatch, no allocation beyond the consumed text)+-- against a richer alternative that reports a proper \"expected one of ...\"+-- error via 'Text.Megaparsec.failure' (as PR #20 proposed), the way it would+-- behave sitting inside a wide 'asum'\/'choice' of keyword alternatives.+--+-- Both implementations here pin the reported error offset via+-- 'Megaparsec.setOffset', matching the fix applied to the shipped+-- 'PostgresqlSyntax.Helpers.Parsers.keyword' to stay correct under megaparsec+-- >=9.8's stricter '(<|>)' error-merging. That fix is what makes the rich+-- variant produce complete "expecting ..." lists at all under current+-- megaparsec; see 'PostgresqlSyntax.Helpers.Parsers.keyword''s haddock.+module Main where++import Criterion.Main+import qualified Data.Char as Char+import qualified Data.Set as Set+import qualified Data.Text as Text+import HeadedMegaparsec (HeadedParsec, parse, toParsec)+import qualified PostgresqlSyntax+import Prelude+import qualified Test.QuickCheck as Qc+import qualified Text.Megaparsec as Megaparsec++-- * Shared token scanner (mirrors 'PostgresqlSyntax.Helpers.Parsers.anyKeyword')++firstIdentifierChar :: Char -> Bool+firstIdentifierChar x = Char.isAlpha x || x == '_' || x >= '\200' && x <= '\377'++notFirstIdentifierChar :: Char -> Bool+notFirstIdentifierChar x = Char.isAlphaNum x || x == '_' || x == '$' || x >= '\200' && x <= '\377'++anyKeywordRaw :: Megaparsec.Parsec Void Text.Text Text.Text+anyKeywordRaw =+ Megaparsec.label "keyword" $ do+ firstChar <- Megaparsec.satisfy firstIdentifierChar+ remainder <- Megaparsec.takeWhileP Nothing notFirstIdentifierChar+ return (Text.toLower (Text.cons firstChar remainder))++-- * The two candidate implementations++-- | Shipped implementation: fails with a bare, unlabeled 'empty' on mismatch.+cheapKeyword :: Text.Text -> HeadedParsec Void Text.Text Text.Text+cheapKeyword expected = parse $ do+ off <- Megaparsec.getOffset+ parsed <- anyKeywordRaw+ if expected == parsed then return parsed else Megaparsec.setOffset off *> empty++-- | PR #20-style implementation: fails with an explicit chunk expectation,+-- so failed alternatives can be unioned into a readable "expecting one of+-- ..." message. Allocates a 'NonEmpty' 'Char' and a 'Set' per failed branch.+richKeyword :: Text.Text -> HeadedParsec Void Text.Text Text.Text+richKeyword expected = parse $ do+ off <- Megaparsec.getOffset+ parsed <- anyKeywordRaw+ if expected == parsed then return parsed else Megaparsec.setOffset off *> chunkFailure expected parsed+ where+ chunkFailure expectedTxt givenTxt =+ Megaparsec.failure+ (Just (errorItemConverter givenTxt))+ (Set.fromList (pure (errorItemConverter expectedTxt)))+ errorItemConverter t = Megaparsec.Tokens $ case Text.uncons t of+ Just (h, rest) -> h :| Text.unpack rest+ Nothing -> ' ' :| []++runHeaded :: HeadedParsec Void Text.Text a -> Text.Text -> Either (Megaparsec.ParseErrorBundle Text.Text Void) a+runHeaded p = Megaparsec.runParser (toParsec p <* Megaparsec.eof) ""++-- * Keyword word lists (sized to mirror the shipped keyword sets)++-- | ~90 words: the size of a typical mid-size dispatch, mirroring+-- @PostgresqlSyntax.KeywordSet.reservedKeyword@.+reservedWords :: [Text.Text]+reservedWords = ["all", "analyse", "analyze", "and", "any", "array", "as", "asc", "asymmetric", "both", "case", "cast", "check", "collate", "column", "constraint", "create", "current_catalog", "current_date", "current_role", "current_time", "current_timestamp", "current_user", "default", "deferrable", "desc", "distinct", "do", "else", "end", "except", "false", "fetch", "for", "foreign", "from", "grant", "group", "having", "in", "initially", "intersect", "into", "lateral", "leading", "limit", "localtime", "localtimestamp", "not", "null", "offset", "on", "only", "or", "order", "placing", "primary", "references", "returning", "select", "session_user", "some", "symmetric", "table", "then", "to", "trailing", "true", "union", "unique", "user", "using", "variadic", "when", "where", "window", "with"]++-- | ~440 words: the full reserved-word set, worst case, mirroring+-- @PostgresqlSyntax.KeywordSet.keyword@.+allWords :: [Text.Text]+allWords = ["abort", "absolute", "access", "action", "add", "admin", "after", "aggregate", "all", "also", "alter", "always", "analyse", "analyze", "and", "any", "array", "as", "asc", "assertion", "assignment", "asymmetric", "at", "attach", "attribute", "authorization", "backward", "before", "begin", "between", "bigint", "binary", "bit", "boolean", "both", "by", "cache", "call", "called", "cascade", "cascaded", "case", "cast", "catalog", "chain", "char", "character", "characteristics", "check", "checkpoint", "class", "close", "cluster", "coalesce", "collate", "collation", "column", "columns", "comment", "comments", "commit", "committed", "concurrently", "configuration", "conflict", "connection", "constraint", "constraints", "content", "continue", "conversion", "copy", "cost", "create", "cross", "csv", "cube", "current", "current_catalog", "current_date", "current_role", "current_schema", "current_time", "current_timestamp", "current_user", "cursor", "cycle", "data", "database", "day", "deallocate", "dec", "decimal", "declare", "default", "defaults", "deferrable", "deferred", "definer", "delete", "delimiter", "delimiters", "depends", "desc", "detach", "dictionary", "disable", "discard", "distinct", "do", "document", "domain", "double", "drop", "each", "else", "enable", "encoding", "encrypted", "end", "enum", "escape", "event", "except", "exclude", "excluding", "exclusive", "execute", "exists", "explain", "expression", "extension", "external", "extract", "false", "family", "fetch", "filter", "first", "float", "following", "for", "force", "foreign", "forward", "freeze", "from", "full", "function", "functions", "generated", "global", "grant", "granted", "greatest", "group", "grouping", "groups", "handler", "having", "header", "hold", "hour", "identity", "if", "ilike", "immediate", "immutable", "implicit", "import", "in", "include", "including", "increment", "index", "indexes", "inherit", "inherits", "initially", "inline", "inner", "inout", "input", "insensitive", "insert", "instead", "int", "integer", "intersect", "interval", "into", "invoker", "is", "isnull", "isolation", "join", "key", "label", "language", "large", "last", "lateral", "leading", "leakproof", "least", "left", "level", "like", "limit", "listen", "load", "local", "localtime", "localtimestamp", "location", "lock", "locked", "logged", "mapping", "match", "materialized", "maxvalue", "method", "minute", "minvalue", "mode", "month", "move", "name", "names", "national", "natural", "nchar", "new", "next", "nfc", "nfd", "nfkc", "nfkd", "no", "none", "normalize", "normalized", "not", "nothing", "notify", "notnull", "nowait", "null", "nullif", "nulls", "numeric", "object", "of", "off", "offset", "oids", "old", "on", "only", "operator", "option", "options", "or", "order", "ordinality", "others", "out", "outer", "over", "overlaps", "overlay", "overriding", "owned", "owner", "parallel", "parser", "partial", "partition", "passing", "password", "placing", "plans", "policy", "position", "preceding", "precision", "prepare", "prepared", "preserve", "primary", "prior", "privileges", "procedural", "procedure", "procedures", "program", "publication", "quote", "range", "read", "real", "reassign", "recheck", "recursive", "ref", "references", "referencing", "refresh", "reindex", "relative", "release", "rename", "repeatable", "replace", "replica", "reset", "restart", "restrict", "returning", "returns", "revoke", "right", "role", "rollback", "rollup", "routine", "routines", "row", "rows", "rule", "savepoint", "schema", "schemas", "scroll", "search", "second", "security", "select", "sequence", "sequences", "serializable", "server", "session", "session_user", "set", "setof", "sets", "share", "show", "similar", "simple", "skip", "smallint", "snapshot", "some", "sql", "stable", "standalone", "start", "statement", "statistics", "stdin", "stdout", "storage", "stored", "strict", "strip", "subscription", "substring", "support", "symmetric", "sysid", "system", "table", "tables", "tablesample", "tablespace", "temp", "template", "temporary", "text", "then", "ties", "time", "timestamp", "to", "trailing", "transaction", "transform", "treat", "trigger", "trim", "true", "truncate", "trusted", "type", "types", "uescape", "unbounded", "uncommitted", "unencrypted", "union", "unique", "unknown", "unlisten", "unlogged", "until", "update", "user", "using", "vacuum", "valid", "validate", "validator", "value", "values", "varchar", "variadic", "varying", "verbose", "version", "view", "views", "volatile", "when", "where", "whitespace", "window", "with", "within", "without", "work", "wrapper", "write", "xml", "xmlattributes", "xmlconcat", "xmlelement", "xmlexists", "xmlforest", "xmlnamespaces", "xmlparse", "xmlpi", "xmlroot", "xmlserialize", "xmltable", "year", "yes", "zone"]++-- * Wide-alternation scenarios++-- | An alternation over every element of the given keyword list, dispatched+-- via the given per-keyword implementation. This is the shape 'keyword'+-- actually sits in throughout the grammar: many 'HeadedParsec' alternatives+-- tried in sequence via '(<|>)'\/'asum'.+wideAlternation :: (Text.Text -> HeadedParsec Void Text.Text Text.Text) -> [Text.Text] -> HeadedParsec Void Text.Text Text.Text+wideAlternation impl kws = asum (map impl kws)++main :: IO ()+main = do+ let lastOf xs = last xs+ firstOf (x : _) = x+ firstOf [] = error "empty keyword list"+ noMatchToken = "zzzznotakeyword" :: Text.Text++ corpus <- replicateM 500 (Qc.generate (Qc.resize 15 (Qc.arbitrary @PostgresqlSyntax.PreparableStmt)))+ let corpusTexts = map (PostgresqlSyntax.toText mempty) corpus++ defaultMain+ [ bgroup+ "keyword in wide alternation"+ [ bgroup+ (label <> ", " <> scenario)+ [ bench "cheap" $ whnf (runHeaded (wideAlternation cheapKeyword kws)) token,+ bench "rich" $ whnf (runHeaded (wideAlternation richKeyword kws)) token+ ]+ | (label, kws) <- [("~90 words", reservedWords), ("~440 words", allWords)],+ (scenario, token) <-+ [ ("first match", firstOf kws),+ ("last match", lastOf kws),+ ("no match", noMatchToken)+ ]+ ],+ bgroup+ "full grammar on generated corpus (reference, shipped keyword only)"+ [bench "500 generated preparableStmt" $ nf (map (either (const False) (const True) . PostgresqlSyntax.parse @PostgresqlSyntax.PreparableStmt mempty)) corpusTexts]+ ]
+ hspec-test/Ast/AExprReversableOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AExprReversableOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AExprReversableOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AExprReversableOp+ itSatisfiesArbitrary @AExprReversableOp
+ hspec-test/Ast/AExprSpec.hs view
@@ -0,0 +1,81 @@+module Ast.AExprSpec (spec) where++import qualified Data.Text as Text+import Helpers.Specs+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AExpr+import PostgresqlSyntax.Ast.CExpr+import PostgresqlSyntax.Ast.Columnref+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import PostgresqlSyntax.Ast.VerbalExprBinOp+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AExpr+ itSatisfiesCanonicalizes @AExpr+ itSatisfiesRefines @SelectWithParens @AExpr+ itSatisfiesArbitrary @AExpr+ describe "Postgres grammar conformance" $ do+ describe "accepts subquery operators without surrounding whitespace" $ do+ itParses @AExpr "a=ANY(b)"++ -- gram.y:15985,15987 have only @a_expr qual_Op a_expr@ and+ -- @qual_Op a_expr@ - the postfix @a_expr qual_Op@ form was removed+ -- from Postgres in v14.+ describe "rejects postfix operators" $ do+ itRejects @AExpr "1 +#"+ itRejects @AExpr "1 OPERATOR(pg_catalog.+#)"+ itRejects @AExpr "a +#"+ describe "Nesting depth" $ do+ itParsesWithin @AExpr 5 (Text.replicate 50 "(" <> "a + b" <> Text.replicate 50 ")")+ let terms off = Text.intercalate " + " ["coalesce(c" <> Text.pack (show (off + i)) <> ", 0)" | i <- [1 .. 24 :: Int]]+ coalesceSumInput = Text.replicate 6 "(" <> "(" <> terms 0 <> ") - (" <> terms 24 <> ")" <> Text.replicate 6 ")"+ itParsesWithin @AExpr 5 coalesceSumInput+ it "OVERLAPS still parses" $ do+ let render :: AExpr -> Text+ render = toText mempty+ fmap render (parse @AExpr mempty "(1, 2) overlaps (3, 4)") `shouldBe` Right "(1, 2) OVERLAPS (3, 4)"+ fmap render (parse @AExpr mempty "row(1, 2) overlaps row(3, 4)") `shouldBe` Right "ROW (1, 2) OVERLAPS ROW (3, 4)"+ describe "LIKE/ILIKE/SIMILAR TO ESCAPE rhs" $ do+ -- Regression for a bug introduced in 92a4d96: the ESCAPE-clause guard on+ -- the LIKE/ILIKE/SIMILAR TO pattern operand (`d`) was applied whenever an+ -- ESCAPE clause was present at all, instead of only when `d` is itself an+ -- unbounded shape. That spuriously parenthesized bounded operands (a+ -- plain column reference here), producing an extra 'InParensCExpr' layer+ -- on reparse that the original AST never had.+ let render :: AExpr -> Text+ render = toText mempty+ roundtrip sql = fmap render (parse @AExpr mempty sql)+ it "does not add parens around a bounded column-reference rhs" $ do+ roundtrip "a LIKE b ESCAPE 'x'" `shouldBe` Right "a LIKE b ESCAPE 'x'"+ it "does not add parens around a bounded ILIKE rhs" $ do+ roundtrip "a ILIKE b ESCAPE 'x'" `shouldBe` Right "a ILIKE b ESCAPE 'x'"+ it "does not add parens around a bounded SIMILAR TO rhs" $ do+ roundtrip "a SIMILAR TO b ESCAPE 'x'" `shouldBe` Right "a SIMILAR TO b ESCAPE 'x'"+ it "still parenthesizes an unbounded (nested, escape-less LIKE) rhs" $ do+ -- Constructed directly rather than by parsing a string: parsing+ -- "a LIKE b LIKE c ESCAPE 'x'" greedily binds the ESCAPE to the+ -- innermost LIKE (its own @d@ operand is unrestricted @a_expr@), so it+ -- can't reach the shape below by parsing alone. The shape is real+ -- though: an unbounded, escape-less nested 'VerbalExprBinOpAExpr' as+ -- the rhs of an outer production that itself has an ESCAPE clause. Left+ -- unparenthesized, rendering it bare would let a reparse of the outer+ -- ESCAPE bind to the inner LIKE instead, changing the AST.+ -- Reparsing always wraps a parenthesized operand in an explicit+ -- 'InParensCExpr' (parens are themselves a production), so the+ -- fixpoint of render/parse is the parenthesized form, not the bare+ -- @outer@ below - that's expected and matches how+ -- 'PostgresqlSyntax.Ast.AExpr.Qc.Arbitrary'\'s generator pre-wraps+ -- this same shape. What matters here is that the parens are present at+ -- all, and that the fixpoint is stable.+ let colref name = CExprAExpr (ColumnrefCExpr (Columnref (UnquotedIdent name) Nothing))+ innerD = VerbalExprBinOpAExpr (colref "b") False LikeVerbalExprBinOp (colref "c") Nothing+ outer = VerbalExprBinOpAExpr (colref "a") False LikeVerbalExprBinOp innerD (Just (colref "e"))+ wrappedOuter = VerbalExprBinOpAExpr (colref "a") False LikeVerbalExprBinOp (CExprAExpr (InParensCExpr innerD Nothing)) (Just (colref "e"))+ sql = render outer+ sql `shouldBe` "a LIKE (b LIKE c) ESCAPE e"+ parse @AExpr mempty sql `shouldBe` Right wrappedOuter+ fmap render (parse @AExpr mempty sql) `shouldBe` Right sql
+ hspec-test/Ast/AexprConstSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AexprConstSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AexprConst+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AexprConst+ itSatisfiesArbitrary @AexprConst
+ hspec-test/Ast/AliasClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AliasClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AliasClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AliasClause+ itSatisfiesArbitrary @AliasClause
+ hspec-test/Ast/AllOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AllOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AllOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AllOp+ itSatisfiesArbitrary @AllOp
+ hspec-test/Ast/AnyNameSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AnyNameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AnyName+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AnyName+ itSatisfiesArbitrary @AnyName
+ hspec-test/Ast/AnyOperatorSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AnyOperatorSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AnyOperator+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AnyOperator+ itSatisfiesArbitrary @AnyOperator
+ hspec-test/Ast/ArrayBoundsSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ArrayBoundsSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ArrayBounds+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ArrayBounds+ itSatisfiesArbitrary @ArrayBounds
+ hspec-test/Ast/ArrayExprListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ArrayExprListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ArrayExprList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ArrayExprList+ itSatisfiesArbitrary @ArrayExprList
+ hspec-test/Ast/ArrayExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ArrayExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ArrayExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ArrayExpr+ itSatisfiesArbitrary @ArrayExpr
+ hspec-test/Ast/AscDescSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AscDescSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.AscDesc+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @AscDesc+ itSatisfiesArbitrary @AscDesc
+ hspec-test/Ast/AttrsSpec.hs view
@@ -0,0 +1,10 @@+module Ast.AttrsSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Attrs+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Attrs+ itSatisfiesArbitrary @Attrs
+ hspec-test/Ast/BExprIsOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.BExprIsOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.BExprIsOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @BExprIsOp+ itSatisfiesArbitrary @BExprIsOp
+ hspec-test/Ast/BExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.BExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.BExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @BExpr+ itSatisfiesArbitrary @BExpr
+ hspec-test/Ast/BconstSpec.hs view
@@ -0,0 +1,10 @@+module Ast.BconstSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Bconst+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Bconst+ itSatisfiesArbitrary @Bconst
+ hspec-test/Ast/BitSpec.hs view
@@ -0,0 +1,10 @@+module Ast.BitSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Bit+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Bit+ itSatisfiesArbitrary @Bit
+ hspec-test/Ast/CExprSpec.hs view
@@ -0,0 +1,11 @@+module Ast.CExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.CExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @CExpr+ itSatisfiesCanonicalizes @CExpr+ itSatisfiesArbitrary @CExpr
+ hspec-test/Ast/CallStmtSpec.hs view
@@ -0,0 +1,10 @@+module Ast.CallStmtSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.CallStmt+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @CallStmt+ itSatisfiesArbitrary @CallStmt
+ hspec-test/Ast/CaseExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.CaseExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.CaseExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @CaseExpr+ itSatisfiesArbitrary @CaseExpr
+ hspec-test/Ast/CharacterSpec.hs view
@@ -0,0 +1,10 @@+module Ast.CharacterSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Character+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Character+ itSatisfiesArbitrary @Character
+ hspec-test/Ast/ColumnrefSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ColumnrefSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Columnref+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Columnref+ itSatisfiesArbitrary @Columnref
+ hspec-test/Ast/CommonTableExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.CommonTableExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.CommonTableExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @CommonTableExpr+ itSatisfiesArbitrary @CommonTableExpr
+ hspec-test/Ast/ConfExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ConfExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ConfExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ConfExpr+ itSatisfiesArbitrary @ConfExpr
+ hspec-test/Ast/ConstCharacterSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ConstCharacterSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ConstCharacter+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ConstCharacter+ itSatisfiesArbitrary @ConstCharacter
+ hspec-test/Ast/ConstDatetimeSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ConstDatetimeSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ConstDatetime+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ConstDatetime+ itSatisfiesArbitrary @ConstDatetime
+ hspec-test/Ast/ConstTypenameSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ConstTypenameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ConstTypename+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ConstTypename+ itSatisfiesArbitrary @ConstTypename
+ hspec-test/Ast/DeleteStmtSpec.hs view
@@ -0,0 +1,13 @@+module Ast.DeleteStmtSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.DeleteStmt+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @DeleteStmt+ itSatisfiesArbitrary @DeleteStmt+ itParses @DeleteStmt "delete from foo as set"+ itParses @DeleteStmt "delete from foo alias where 1"+ itRejects @DeleteStmt "delete from foo set"
+ hspec-test/Ast/ExplicitRowSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ExplicitRowSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ExplicitRow+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ExplicitRow+ itSatisfiesArbitrary @ExplicitRow
+ hspec-test/Ast/ExprListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ExprListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ExprList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ExprList+ itSatisfiesArbitrary @ExprList
+ hspec-test/Ast/ExtractArgSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ExtractArgSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ExtractArg+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ExtractArg+ itSatisfiesArbitrary @ExtractArg
+ hspec-test/Ast/ExtractListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ExtractListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ExtractList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ExtractList+ itSatisfiesArbitrary @ExtractList
+ hspec-test/Ast/FconstSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FconstSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Fconst+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Fconst+ itSatisfiesArbitrary @Fconst
+ hspec-test/Ast/ForLockingClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ForLockingClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ForLockingClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ForLockingClause+ itSatisfiesArbitrary @ForLockingClause
+ hspec-test/Ast/ForLockingItemSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ForLockingItemSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ForLockingItem+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ForLockingItem+ itSatisfiesArbitrary @ForLockingItem
+ hspec-test/Ast/ForLockingStrengthSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ForLockingStrengthSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ForLockingStrength+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ForLockingStrength+ itSatisfiesArbitrary @ForLockingStrength
+ hspec-test/Ast/FrameBoundSpec.hs view
@@ -0,0 +1,27 @@+module Ast.FrameBoundSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FrameBound+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FrameBound+ itSatisfiesArbitrary @FrameBound+ describe "Postgres grammar conformance" $ do+ -- gram.y:17567 frame_bound. UNBOUNDED is an unreserved keyword, so+ -- @UNBOUNDED PRECEDING@ is ambiguous with @a_expr PRECEDING@ where+ -- the a_expr is a column named "unbounded"; gram.y:915 resolves it by+ -- giving UNBOUNDED lower precedence than PRECEDING, i.e. the keyword+ -- reading wins and the column reading needs quoting.+ it "frame_bound" $ do+ let render :: FrameBound -> Text+ render = toText mempty+ fmap render (parse @FrameBound mempty "unbounded preceding") `shouldBe` Right "UNBOUNDED PRECEDING"+ fmap render (parse @FrameBound mempty "unbounded following") `shouldBe` Right "UNBOUNDED FOLLOWING"+ fmap render (parse @FrameBound mempty "current row") `shouldBe` Right "CURRENT ROW"+ fmap render (parse @FrameBound mempty "1 preceding") `shouldBe` Right "1 PRECEDING"+ fmap render (parse @FrameBound mempty "a following") `shouldBe` Right "a FOLLOWING"+ fmap render (parse @FrameBound mempty "\"unbounded\" preceding") `shouldBe` Right "\"unbounded\" PRECEDING"
+ hspec-test/Ast/FrameClauseModeSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FrameClauseModeSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FrameClauseMode+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FrameClauseMode+ itSatisfiesArbitrary @FrameClauseMode
+ hspec-test/Ast/FrameClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FrameClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FrameClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FrameClause+ itSatisfiesArbitrary @FrameClause
+ hspec-test/Ast/FrameExtentSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FrameExtentSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FrameExtent+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FrameExtent+ itSatisfiesArbitrary @FrameExtent
+ hspec-test/Ast/FromClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FromClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FromClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FromClause+ itSatisfiesArbitrary @FromClause
+ hspec-test/Ast/FromListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FromListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FromList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FromList+ itSatisfiesArbitrary @FromList
+ hspec-test/Ast/FuncAliasClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncAliasClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncAliasClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncAliasClause+ itSatisfiesArbitrary @FuncAliasClause
+ hspec-test/Ast/FuncApplicationParamsSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncApplicationParamsSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncApplicationParams+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncApplicationParams+ itSatisfiesArbitrary @FuncApplicationParams
+ hspec-test/Ast/FuncApplicationSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncApplicationSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncApplication+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncApplication+ itSatisfiesArbitrary @FuncApplication
+ hspec-test/Ast/FuncArgExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncArgExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncArgExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncArgExpr+ itSatisfiesArbitrary @FuncArgExpr
+ hspec-test/Ast/FuncConstArgsSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncConstArgsSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncConstArgs+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncConstArgs+ itSatisfiesArbitrary @FuncConstArgs
+ hspec-test/Ast/FuncExprCommonSubexprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncExprCommonSubexprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncExprCommonSubexpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncExprCommonSubexpr+ itSatisfiesArbitrary @FuncExprCommonSubexpr
+ hspec-test/Ast/FuncExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncExpr+ itSatisfiesArbitrary @FuncExpr
+ hspec-test/Ast/FuncExprWindowlessSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncExprWindowlessSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncExprWindowless+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncExprWindowless+ itSatisfiesArbitrary @FuncExprWindowless
+ hspec-test/Ast/FuncNameSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncNameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncName+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncName+ itSatisfiesArbitrary @FuncName
+ hspec-test/Ast/FuncTableSpec.hs view
@@ -0,0 +1,10 @@+module Ast.FuncTableSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.FuncTable+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @FuncTable+ itSatisfiesArbitrary @FuncTable
+ hspec-test/Ast/GenericTypeSpec.hs view
@@ -0,0 +1,10 @@+module Ast.GenericTypeSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.GenericType+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @GenericType+ itSatisfiesArbitrary @GenericType
+ hspec-test/Ast/GroupByItemSpec.hs view
@@ -0,0 +1,10 @@+module Ast.GroupByItemSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.GroupByItem+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @GroupByItem+ itSatisfiesArbitrary @GroupByItem
+ hspec-test/Ast/GroupClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.GroupClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.GroupClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @GroupClause+ itSatisfiesArbitrary @GroupClause
+ hspec-test/Ast/HavingClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.HavingClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.HavingClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @HavingClause+ itSatisfiesArbitrary @HavingClause
+ hspec-test/Ast/HsTargetElSpec.hs view
@@ -0,0 +1,45 @@+module Ast.HsTargetElSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.HsTargetEl+import PostgresqlSyntax.Settings (haskellTargets, nullabilityMarkers)+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @HsTargetEl+ itSatisfiesArbitrary @HsTargetEl+ describe "HsFieldTargetEl" $ do+ itSatisfiesIsAst @HsFieldTargetEl+ itSatisfiesArbitrary @HsFieldTargetEl+ describe "Parsers" $ do+ itParses @HsTargetEl "a"+ itParses @HsTargetEl "a.b AS c"+ itParses @HsTargetEl "*"+ describe "extended" $ do+ describe "function targets" $ do+ itParsesWith @HsTargetEl hs "$f (a, b)"+ itParsesWith @HsTargetEl hs "$f (a)"+ itParsesWith @HsTargetEl hs "$f ()"+ itParsesWith @HsTargetEl hs "$f ($g (a), b)"+ itParsesWith @HsTargetEl hs "$Mk (a, b)"+ itParsesWith @HsTargetEl hs "$Mod.f (a)"+ itParsesWith @HsTargetEl hs "$Mod.Mk (a)"+ itRejectsWith @HsTargetEl mempty "$f (a, b)"+ itRejectsWith @HsTargetEl mempty "$f ()"+ itRejectsWith @HsTargetEl mempty "$Mod.f (a)"+ describe "record targets" $ do+ itParsesWith @HsTargetEl hs "$R {a = x::int8}"+ itParsesWith @HsTargetEl hs "$R {a = x, b = y}"+ itParsesWith @HsTargetEl hs "$r {a = x}"+ itParsesWith @HsTargetEl hs "$R {a = $f (x, y)}"+ itParsesWith @HsTargetEl hs "$R {a = $S {b = x}}"+ itRejectsWith @HsTargetEl mempty "$R {a = x::int8}"+ itRejectsWith @HsTargetEl mempty "$r {a = x}"+ describe "with nullability markers" $ do+ itParsesWith @HsTargetEl (hs <> nullabilityMarkers True) "$mk (a::int8, b::text?)"+ itParsesWith @HsTargetEl (hs <> nullabilityMarkers True) "$R {a = b::text?}"+ itRejectsWith @HsTargetEl hs "$mk (a::int8, b::text?)"+ where+ hs = haskellTargets True
+ hspec-test/Ast/HsTargetListSpec.hs view
@@ -0,0 +1,18 @@+module Ast.HsTargetListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.HsTargetList+import PostgresqlSyntax.Settings (haskellTargets)+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @HsTargetList+ itSatisfiesArbitrary @HsTargetList+ describe "extended" $ do+ itParsesWith @HsTargetList hs "$f (a), b, $R {c = d}"+ itParsesWith @HsTargetList hs "a, $f (b, c)"+ itRejectsWith @HsTargetList mempty "$f (a), b"+ where+ hs = haskellTargets True
+ hspec-test/Ast/IconstSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IconstSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Iconst+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Iconst+ itSatisfiesArbitrary @Iconst
+ hspec-test/Ast/IdentSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IdentSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Ident+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Ident+ itSatisfiesArbitrary @Ident
+ hspec-test/Ast/ImplicitRowSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ImplicitRowSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ImplicitRow+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ImplicitRow+ itSatisfiesArbitrary @ImplicitRow
+ hspec-test/Ast/InExprSpec.hs view
@@ -0,0 +1,11 @@+module Ast.InExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.InExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @InExpr+ itSatisfiesCanonicalizes @InExpr+ itSatisfiesArbitrary @InExpr
+ hspec-test/Ast/IndexElemDefSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IndexElemDefSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.IndexElemDef+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @IndexElemDef+ itSatisfiesArbitrary @IndexElemDef
+ hspec-test/Ast/IndexElemSpec.hs view
@@ -0,0 +1,24 @@+module Ast.IndexElemSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.IndexElem+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @IndexElem+ itSatisfiesArbitrary @IndexElem+ describe "Postgres grammar conformance" $ do+ -- gram.y:8558 index_elem: ColId index_elem_options, and gram.y:8596+ -- opt_nulls_order. index_elem_options' operator-class name+ -- (opt_qualified_name, gram.y:8525) is a bare ColId, so it is+ -- directly ambiguous with the unreserved NULLS that follows it.+ it "index_elem" $ do+ let render :: IndexElem -> Text+ render = toText mempty+ fmap render (parse @IndexElem mempty "a") `shouldBe` Right "a"+ fmap render (parse @IndexElem mempty "a nulls first") `shouldBe` Right "a NULLS FIRST"+ fmap render (parse @IndexElem mempty "a text_ops nulls first") `shouldBe` Right "a text_ops NULLS FIRST"+ fmap render (parse @IndexElem mempty "a collate \"C\" text_ops desc") `shouldBe` Right "a COLLATE \"C\" text_ops DESC"
+ hspec-test/Ast/IndexParamsSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IndexParamsSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.IndexParams+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @IndexParams+ itSatisfiesArbitrary @IndexParams
+ hspec-test/Ast/IndirectionElSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IndirectionElSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.IndirectionEl+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @IndirectionEl+ itSatisfiesArbitrary @IndirectionEl
+ hspec-test/Ast/IndirectionSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IndirectionSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Indirection+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Indirection+ itSatisfiesArbitrary @Indirection
+ hspec-test/Ast/InsertColumnItemSpec.hs view
@@ -0,0 +1,10 @@+module Ast.InsertColumnItemSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.InsertColumnItem+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @InsertColumnItem+ itSatisfiesArbitrary @InsertColumnItem
+ hspec-test/Ast/InsertColumnListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.InsertColumnListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.InsertColumnList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @InsertColumnList+ itSatisfiesArbitrary @InsertColumnList
+ hspec-test/Ast/InsertRestSpec.hs view
@@ -0,0 +1,14 @@+module Ast.InsertRestSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.InsertRest+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @InsertRest+ itSatisfiesArbitrary @InsertRest+ -- https://github.com/nikita-volkov/postgresql-syntax/issues/35+ itParses @InsertRest "(values (default))"+ itParses @InsertRest "(select 1)"+ itParses @InsertRest "(values) values (1)"
+ hspec-test/Ast/InsertStmtSpec.hs view
@@ -0,0 +1,12 @@+module Ast.InsertStmtSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.InsertStmt+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @InsertStmt+ itSatisfiesArbitrary @InsertStmt+ -- https://github.com/nikita-volkov/postgresql-syntax/issues/35+ itParses @InsertStmt "insert into ta.xa (values (default))"
+ hspec-test/Ast/InsertTargetSpec.hs view
@@ -0,0 +1,10 @@+module Ast.InsertTargetSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.InsertTarget+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @InsertTarget+ itSatisfiesArbitrary @InsertTarget
+ hspec-test/Ast/IntervalSecondSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IntervalSecondSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.IntervalSecond+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @IntervalSecond+ itSatisfiesArbitrary @IntervalSecond
+ hspec-test/Ast/IntervalSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IntervalSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Interval+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Interval+ itSatisfiesArbitrary @Interval
+ hspec-test/Ast/IntoClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.IntoClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.IntoClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @IntoClause+ itSatisfiesArbitrary @IntoClause
+ hspec-test/Ast/JoinQualSpec.hs view
@@ -0,0 +1,10 @@+module Ast.JoinQualSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.JoinQual+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @JoinQual+ itSatisfiesArbitrary @JoinQual
+ hspec-test/Ast/JoinTypeSpec.hs view
@@ -0,0 +1,10 @@+module Ast.JoinTypeSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.JoinType+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @JoinType+ itSatisfiesArbitrary @JoinType
+ hspec-test/Ast/JoinedTableSpec.hs view
@@ -0,0 +1,10 @@+module Ast.JoinedTableSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.JoinedTable+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @JoinedTable+ itSatisfiesArbitrary @JoinedTable
+ hspec-test/Ast/LimitClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.LimitClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.LimitClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @LimitClause+ itSatisfiesArbitrary @LimitClause
+ hspec-test/Ast/MathOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.MathOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.MathOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @MathOp+ itSatisfiesArbitrary @MathOp
+ hspec-test/Ast/NameListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.NameListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.NameList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @NameList+ itSatisfiesArbitrary @NameList
+ hspec-test/Ast/NullsOrderSpec.hs view
@@ -0,0 +1,10 @@+module Ast.NullsOrderSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.NullsOrder+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @NullsOrder+ itSatisfiesArbitrary @NullsOrder
+ hspec-test/Ast/NumericSpec.hs view
@@ -0,0 +1,10 @@+module Ast.NumericSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Numeric+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Numeric+ itSatisfiesArbitrary @Numeric
+ hspec-test/Ast/OffsetClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OffsetClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OffsetClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OffsetClause+ itSatisfiesArbitrary @OffsetClause
+ hspec-test/Ast/OnConflictDoSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OnConflictDoSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OnConflictDo+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OnConflictDo+ itSatisfiesArbitrary @OnConflictDo
+ hspec-test/Ast/OnConflictSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OnConflictSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OnConflict+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OnConflict+ itSatisfiesArbitrary @OnConflict
+ hspec-test/Ast/OpSpec.hs view
@@ -0,0 +1,11 @@+module Ast.OpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Op+import Prelude hiding (Op)+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Op+ itSatisfiesArbitrary @Op
+ hspec-test/Ast/OptOrdinalitySpec.hs view
@@ -0,0 +1,10 @@+module Ast.OptOrdinalitySpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OptOrdinality+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OptOrdinality+ itSatisfiesArbitrary @OptOrdinality
+ hspec-test/Ast/OptTempTableNameSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OptTempTableNameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OptTempTableName+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OptTempTableName+ itSatisfiesArbitrary @OptTempTableName
+ hspec-test/Ast/OptVaryingSpec.hs view
@@ -0,0 +1,8 @@+module Ast.OptVaryingSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OptVarying+import Test.Hspec++spec :: Spec+spec = itSatisfiesArbitrary @OptVarying
+ hspec-test/Ast/OverClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OverClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OverClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OverClause+ itSatisfiesArbitrary @OverClause
+ hspec-test/Ast/OverlayListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OverlayListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OverlayList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OverlayList+ itSatisfiesArbitrary @OverlayList
+ hspec-test/Ast/OverrideKindSpec.hs view
@@ -0,0 +1,10 @@+module Ast.OverrideKindSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.OverrideKind+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @OverrideKind+ itSatisfiesArbitrary @OverrideKind
+ hspec-test/Ast/PositionListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.PositionListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.PositionList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @PositionList+ itSatisfiesArbitrary @PositionList
+ hspec-test/Ast/PreparableStmtSpec.hs view
@@ -0,0 +1,103 @@+module Ast.PreparableStmtSpec (spec) where++import qualified Data.Text as Text+import Helpers.Specs+import PostgresqlSyntax.Algebra (parse, toText)+import PostgresqlSyntax.Ast.PreparableStmt+import PostgresqlSyntax.Settings (haskellParamFields, haskellTargets, nullabilityMarkers)+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @PreparableStmt+ itSatisfiesArbitrary @PreparableStmt+ describe "Parsers" $ do+ itParses @PreparableStmt+ "select i :: int8 from auth.user as u\n\+ \inner join edgenode.usere_provider as p\n\+ \on u.id = p.user_id\n\+ \inner join edgenode.provider_branch as b\n\+ \on b.provider_fk = p.provider_id"+ itParses @PreparableStmt "select * from items for update limit 1"+ itParses @PreparableStmt "select * from items limit 1 for update"+ itParses @PreparableStmt "select * from items for share limit 10"+ itParses @PreparableStmt "select * from items for no key update limit 1"+ itParses @PreparableStmt "select * from items for key share limit 1"+ itParses @PreparableStmt "select * from items for update of items nowait limit 1"+ itParses @PreparableStmt "select * from items for update skip locked limit 1"+ itParses @PreparableStmt "select * from items order by id for update limit 1"+ itParses @PreparableStmt "select * from items for update offset 5 limit 10"+ describe "Haskell targets" $ do+ itParsesWith @PreparableStmt hs "select $mkUser (id::int8, name::text) from users"+ itParsesWith @PreparableStmt hs "select $User {uid = id::int8, uname = name::text} from users"+ itParsesWith @PreparableStmt hs "select id::int8, $f (name::text) from users"+ itParsesWith @PreparableStmt hs "select $f () from users"+ itParsesWith @PreparableStmt (hs <> nullabilityMarkers True) "select $mk (a::int8, b::text?) from t"+ itRejectsWith @PreparableStmt mempty "select $mkUser (id::int8, name::text) from users"+ itRejectsWith @PreparableStmt mempty "select $User {uid = id::int8} from users"+ for_ roundtripCases $ \sql ->+ it ("Renders back faithfully: " <> Text.unpack sql) $+ (toText mempty <$> parse @PreparableStmt hs sql) `shouldBe` Right sql+ describe "Nesting depth" $ do+ itParsesWithin @PreparableStmt 5 ("select " <> Text.replicate 50 "(" <> "a + b" <> Text.replicate 50 ")")+ describe "Error reporting" $ do+ itReportsError @PreparableStmt+ "select i :: int8 fom auth.user as u\n\+ \inner join edgenode.usere_provider as p\n\+ \on u.id = p.user_id\n\+ \inner join edgenode.provider_branch as b\n\+ \on b.provider_fk = p.provider_id"+ "(21,\"offset=21:\\nunexpected 'a'\\nexpecting end of input or white space\\n\")"+ itReportsError @PreparableStmt+ "select i :: int8 from auth.user as u\n\+ \WHERE u.id IS NO NULL && TRUE"+ "(51,\"offset=51:\\nexpecting white space\\n\")"+ describe "SourcePos error reporting" $ do+ itReportsSourcePosError @PreparableStmt+ "select i :: int8 fom auth.user as u\n\+ \inner join edgenode.usere_provider as p\n\+ \on u.id = p.user_id\n\+ \inner join edgenode.provider_branch as b\n\+ \on b.provider_fk = p.provider_id"+ "1:22 unexpected 'a'\nexpecting end of input or white space\n"+ itReportsSourcePosError @PreparableStmt+ "select i :: int8 from auth.user as u\n\+ \WHERE u.id IS NO NULL && TRUE"+ "2:15 expecting white space\n"+ itReportsSourcePosError @PreparableStmt+ "SLECT id FROM qsdqsd"+ "1:1 unexpected 'S'\nexpecting '(' or white space\n"+ itReportsSourcePosError @PreparableStmt+ "SELECT id FROM as"+ "1:18 Reserved keyword \"as\" used as an identifier. If that's what you intend, you have to wrap it in double quotes.\n"+ describe "Haskell param fields" $ do+ itParsesWith @PreparableStmt hsParams "select $1.$field::int8"+ itParsesWith @PreparableStmt hsParams "select $1.$a::int8, $1.$b::text"+ itParsesWith @PreparableStmt hsParams "select $1.$a::int8, $1.$a::int8"+ itParsesWith @PreparableStmt hsParams "select $2.$a::int8, $1::text"+ itParsesWith @PreparableStmt hsParams "insert into a (b, c) values ($1.$b::int8, $1.$c::text)"+ itParsesWith @PreparableStmt hsParams "select * from a where id = $1.$id::uuid"+ -- Combined with the nullability markers, which are lexically independent:+ -- @?@ suffixes a typename, @$@ prefixes a field name.+ itParsesWith @PreparableStmt (nullabilityMarkers True <> hsParams) "select $1.$a::int8?"+ itParsesWith @PreparableStmt (nullabilityMarkers True <> hsParams) "select $1.$a::text?, $1.$b::int8[]?"+ -- Off by default: standard Postgres parsing is unchanged.+ itRejectsWith @PreparableStmt mempty "select $1.$field::int8"+ itRejectsWith @PreparableStmt mempty "select $1.$a::int8, $1.$b::text"+ -- The extension is confined to the @$n@ position: a Haskell field after+ -- an ordinary column reference stays a parse error even with it on.+ itRejectsWith @PreparableStmt hsParams "select col.$field"+ itRejectsWith @PreparableStmt hsParams "select col.$field::int8"+ where+ hs = haskellTargets True+ -- Written in the renderer's canonical form (uppercase keywords, spaced+ -- casts) so that parse-then-render is the identity on the string.+ roundtripCases =+ [ "SELECT $mkUser (id :: int8, name :: text) FROM users",+ "SELECT $f () FROM users",+ "SELECT $Mod.Mk (a) FROM t",+ "SELECT $User {uid = id :: int8, uname = $f (name :: text)} FROM users",+ "SELECT a, $f (b), $R {c = d} FROM t"+ ]+ hsParams = haskellParamFields True
+ hspec-test/Ast/QualAllOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.QualAllOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.QualAllOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @QualAllOp+ itSatisfiesArbitrary @QualAllOp
+ hspec-test/Ast/QualOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.QualOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.QualOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @QualOp+ itSatisfiesArbitrary @QualOp
+ hspec-test/Ast/QualifiedNameSpec.hs view
@@ -0,0 +1,10 @@+module Ast.QualifiedNameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.QualifiedName+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @QualifiedName+ itSatisfiesArbitrary @QualifiedName
+ hspec-test/Ast/RelationExprOptAliasSpec.hs view
@@ -0,0 +1,10 @@+module Ast.RelationExprOptAliasSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.RelationExprOptAlias+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @RelationExprOptAlias+ itSatisfiesArbitrary @RelationExprOptAlias
+ hspec-test/Ast/RelationExprSpec.hs view
@@ -0,0 +1,10 @@+module Ast.RelationExprSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.RelationExpr+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @RelationExpr+ itSatisfiesArbitrary @RelationExpr
+ hspec-test/Ast/ReturningClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ReturningClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ReturningClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ReturningClause+ itSatisfiesArbitrary @ReturningClause
+ hspec-test/Ast/RowSpec.hs view
@@ -0,0 +1,10 @@+module Ast.RowSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Row+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Row+ itSatisfiesArbitrary @Row
+ hspec-test/Ast/RowsfromItemSpec.hs view
@@ -0,0 +1,10 @@+module Ast.RowsfromItemSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.RowsfromItem+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @RowsfromItem+ itSatisfiesArbitrary @RowsfromItem
+ hspec-test/Ast/RowsfromListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.RowsfromListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.RowsfromList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @RowsfromList+ itSatisfiesArbitrary @RowsfromList
+ hspec-test/Ast/SconstSpec.hs view
@@ -0,0 +1,15 @@+module Ast.SconstSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Sconst+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Sconst+ itSatisfiesArbitrary @Sconst+ describe "Parsers" $ do+ itParses @Sconst "'it''s good'"+ itParses @Sconst "$$it's good$$"+ itParses @Sconst "$x$it's good$x$"
+ hspec-test/Ast/SelectBinOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SelectBinOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectBinOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectBinOp+ itSatisfiesArbitrary @SelectBinOp
+ hspec-test/Ast/SelectClauseSpec.hs view
@@ -0,0 +1,12 @@+module Ast.SelectClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectClause+import PostgresqlSyntax.Ast.SimpleSelect+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectClause+ itSatisfiesExtends @SelectClause @SimpleSelect+ itSatisfiesArbitrary @SelectClause
+ hspec-test/Ast/SelectFetchFirstValueSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SelectFetchFirstValueSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectFetchFirstValue+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectFetchFirstValue+ itSatisfiesArbitrary @SelectFetchFirstValue
+ hspec-test/Ast/SelectLimitSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SelectLimitSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectLimit+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectLimit+ itSatisfiesArbitrary @SelectLimit
+ hspec-test/Ast/SelectLimitValueSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SelectLimitValueSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectLimitValue+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectLimitValue+ itSatisfiesArbitrary @SelectLimitValue
+ hspec-test/Ast/SelectNoParensSpec.hs view
@@ -0,0 +1,12 @@+module Ast.SelectNoParensSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectNoParens+import PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectNoParens+ itSatisfiesRefines @SelectWithParens @SelectNoParens+ itSatisfiesArbitrary @SelectNoParens
+ hspec-test/Ast/SelectStmtSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SelectStmtSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SelectStmt+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectStmt+ itSatisfiesArbitrary @SelectStmt
+ hspec-test/Ast/SelectWithParensSpec.hs view
@@ -0,0 +1,19 @@+module Ast.SelectWithParensSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.SelectNoParens+import PostgresqlSyntax.Ast.SelectWithParens+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SelectWithParens+ itSatisfiesCanonicalizes @SelectWithParens+ itSatisfiesArbitrary @SelectWithParens+ describe "Nesting depth" $ do+ -- The parenthesised sub-select has two possible representations.+ it "redundant parens around a sub-select are canonicalised"+ $ parse @SelectWithParens mempty "((select 1))"+ `shouldBe` (WithParensSelectWithParens . NoParensSelectWithParens <$> parse @SelectNoParens mempty "select 1")
+ hspec-test/Ast/SetClauseListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SetClauseListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SetClauseList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SetClauseList+ itSatisfiesArbitrary @SetClauseList
+ hspec-test/Ast/SetClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SetClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SetClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SetClause+ itSatisfiesArbitrary @SetClause
+ hspec-test/Ast/SetTargetListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SetTargetListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SetTargetList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SetTargetList+ itSatisfiesArbitrary @SetTargetList
+ hspec-test/Ast/SetTargetSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SetTargetSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SetTarget+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SetTarget+ itSatisfiesArbitrary @SetTarget
+ hspec-test/Ast/SimpleSelectSpec.hs view
@@ -0,0 +1,46 @@+module Ast.SimpleSelectSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.QualifiedName+import PostgresqlSyntax.Ast.RelationExpr+import PostgresqlSyntax.Ast.SelectBinOp+import PostgresqlSyntax.Ast.SelectClause+import PostgresqlSyntax.Ast.SimpleSelect+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SimpleSelect+ itSatisfiesCanonicalizes @SimpleSelect+ itSatisfiesArbitrary @SimpleSelect+ describe "Precedence" precedenceSpec++-- |+-- Reproduces the three parse-shape checks from+-- <https://github.com/nikita-volkov/postgresql-syntax/issues/30> against+-- @gram.y@'s @%left UNION EXCEPT@ \/ @%left INTERSECT@ declarations+-- (gram.y:813-814): @INTERSECT@ binds tighter than @UNION@\/@EXCEPT@, and+-- all three are left-associative among themselves.+precedenceSpec :: Spec+precedenceSpec = do+ it "a EXCEPT b EXCEPT c nests left"+ $ parse mempty "TABLE a EXCEPT TABLE b EXCEPT TABLE c"+ `shouldBe` Right (bin ExceptSelectBinOp (bin ExceptSelectBinOp (table "a") (table "b")) (table "c"))+ it "a INTERSECT b UNION c roots at UNION"+ $ parse mempty "TABLE a INTERSECT TABLE b UNION TABLE c"+ `shouldBe` Right (bin UnionSelectBinOp (bin IntersectSelectBinOp (table "a") (table "b")) (table "c"))+ it "a UNION b INTERSECT c is a UNION (b INTERSECT c)"+ $ parse mempty "TABLE a UNION TABLE b INTERSECT TABLE c"+ `shouldBe` Right (bin UnionSelectBinOp (table "a") (bin IntersectSelectBinOp (table "b") (table "c")))+ it "a INTERSECT b INTERSECT c nests left, not right (issue #34)"+ $ parse mempty "TABLE a INTERSECT TABLE b INTERSECT TABLE c"+ `shouldBe` Right (bin IntersectSelectBinOp (bin IntersectSelectBinOp (table "a") (table "b")) (table "c"))+ it "a INTERSECT b INTERSECT c INTERSECT d nests fully left (issue #34)"+ $ parse mempty "TABLE a INTERSECT TABLE b INTERSECT TABLE c INTERSECT TABLE d"+ `shouldBe` Right (bin IntersectSelectBinOp (bin IntersectSelectBinOp (bin IntersectSelectBinOp (table "a") (table "b")) (table "c")) (table "d"))+ where+ table name = TableSimpleSelect (SimpleRelationExpr (SimpleQualifiedName (UnquotedIdent name)) False)+ bin op lhs rhs = BinSimpleSelect op (SimpleSelectSelectClause lhs) Nothing (SimpleSelectSelectClause rhs)
+ hspec-test/Ast/SimpleTypenameSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SimpleTypenameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SimpleTypename+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SimpleTypename+ itSatisfiesArbitrary @SimpleTypename
+ hspec-test/Ast/SortBySpec.hs view
@@ -0,0 +1,22 @@+module Ast.SortBySpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.SortBy+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SortBy+ itSatisfiesArbitrary @SortBy+ describe "Postgres grammar conformance" $ do+ it "sortby" $ do+ let render :: SortBy -> Text+ render = toText mempty+ fmap render (parse @SortBy mempty "a") `shouldBe` Right "a"+ fmap render (parse @SortBy mempty "a asc") `shouldBe` Right "a ASC"+ fmap render (parse @SortBy mempty "a desc nulls last") `shouldBe` Right "a DESC NULLS LAST"+ fmap render (parse @SortBy mempty "a nulls first") `shouldBe` Right "a NULLS FIRST"+ fmap render (parse @SortBy mempty "a using > nulls last") `shouldBe` Right "a USING > NULLS LAST"+ fmap render (parse @SortBy mempty "nulls") `shouldBe` Right "nulls"
+ hspec-test/Ast/SortClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SortClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SortClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SortClause+ itSatisfiesArbitrary @SortClause
+ hspec-test/Ast/SubTypeSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SubTypeSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SubType+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SubType+ itSatisfiesArbitrary @SubType
+ hspec-test/Ast/SubqueryOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SubqueryOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SubqueryOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SubqueryOp+ itSatisfiesArbitrary @SubqueryOp
+ hspec-test/Ast/SubstrListFromForSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SubstrListFromForSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SubstrListFromFor+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SubstrListFromFor+ itSatisfiesArbitrary @SubstrListFromFor
+ hspec-test/Ast/SubstrListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SubstrListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SubstrList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SubstrList+ itSatisfiesArbitrary @SubstrList
+ hspec-test/Ast/SymbolicExprBinOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.SymbolicExprBinOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.SymbolicExprBinOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @SymbolicExprBinOp+ itSatisfiesArbitrary @SymbolicExprBinOp
+ hspec-test/Ast/TableFuncElementListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TableFuncElementListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TableFuncElementList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TableFuncElementList+ itSatisfiesArbitrary @TableFuncElementList
+ hspec-test/Ast/TableFuncElementSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TableFuncElementSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TableFuncElement+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TableFuncElement+ itSatisfiesArbitrary @TableFuncElement
+ hspec-test/Ast/TableRefSpec.hs view
@@ -0,0 +1,12 @@+module Ast.TableRefSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.JoinedTable+import PostgresqlSyntax.Ast.TableRef+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TableRef+ itSatisfiesExtends @TableRef @JoinedTable+ itSatisfiesArbitrary @TableRef
+ hspec-test/Ast/TablesampleClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TablesampleClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TablesampleClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TablesampleClause+ itSatisfiesArbitrary @TablesampleClause
+ hspec-test/Ast/TargetElSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TargetElSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TargetEl+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TargetEl+ itSatisfiesArbitrary @TargetEl
+ hspec-test/Ast/TargetListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TargetListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TargetList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TargetList+ itSatisfiesArbitrary @TargetList
+ hspec-test/Ast/TargetingSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TargetingSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Targeting+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Targeting+ itSatisfiesArbitrary @Targeting
+ hspec-test/Ast/TimezoneSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TimezoneSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Timezone+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Timezone+ itSatisfiesArbitrary @Timezone
+ hspec-test/Ast/TrimListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TrimListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TrimList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TrimList+ itSatisfiesArbitrary @TrimList
+ hspec-test/Ast/TrimModifierSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TrimModifierSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TrimModifier+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TrimModifier+ itSatisfiesArbitrary @TrimModifier
+ hspec-test/Ast/TypeListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.TypeListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TypeList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @TypeList+ itSatisfiesArbitrary @TypeList
+ hspec-test/Ast/TypenameArrayDimensionsSpec.hs view
@@ -0,0 +1,8 @@+module Ast.TypenameArrayDimensionsSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.TypenameArrayDimensions+import Test.Hspec++spec :: Spec+spec = itSatisfiesArbitrary @TypenameArrayDimensions
+ hspec-test/Ast/TypenameSpec.hs view
@@ -0,0 +1,46 @@+module Ast.TypenameSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Typename+import PostgresqlSyntax.Settings (nullabilityMarkers)+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Typename+ itSatisfiesArbitrary @Typename+ describe "Parsers" $ do+ itParses @Typename "int4"+ itParses @Typename "int4[][]"+ itParses @Typename "aa array"+ itParses @Typename "DOUBLE PRECISION"+ itParses @Typename "bool"+ itParses @Typename "int2"+ itParses @Typename "int4"+ itParses @Typename "int8"+ itParses @Typename "float4"+ itParses @Typename "float8"+ itParses @Typename "numeric"+ itParses @Typename "char"+ itParses @Typename "text"+ itParses @Typename "bytea"+ itParses @Typename "date"+ itParses @Typename "timestamp"+ itParses @Typename "timestamptz"+ itParses @Typename "time"+ itParses @Typename "timetz"+ itParses @Typename "interval"+ itParses @Typename "uuid"+ itParses @Typename "inet"+ itParses @Typename "json"+ itParses @Typename "jsonb"+ describe "extended" $ do+ itParsesWith @Typename (nullabilityMarkers True) "text?"+ itParsesWith @Typename (nullabilityMarkers True) "text[]?"+ itParsesWith @Typename (nullabilityMarkers True) "text?[]?"+ itParsesWith @Typename (nullabilityMarkers True) "text?[]"+ itRejectsWith @Typename mempty "text?"+ itRejectsWith @Typename mempty "text[]?"+ itRejectsWith @Typename mempty "text?[]?"+ itRejectsWith @Typename mempty "text?[]"
+ hspec-test/Ast/UpdateStmtSpec.hs view
@@ -0,0 +1,12 @@+module Ast.UpdateStmtSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.UpdateStmt+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @UpdateStmt+ itSatisfiesArbitrary @UpdateStmt+ itParses @UpdateStmt "update foo as set set x = 1"+ itRejects @UpdateStmt "update foo set set x = 1"
+ hspec-test/Ast/UsingClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.UsingClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.UsingClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @UsingClause+ itSatisfiesArbitrary @UsingClause
+ hspec-test/Ast/ValuesClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.ValuesClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.ValuesClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @ValuesClause+ itSatisfiesArbitrary @ValuesClause
+ hspec-test/Ast/VerbalExprBinOpSpec.hs view
@@ -0,0 +1,10 @@+module Ast.VerbalExprBinOpSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.VerbalExprBinOp+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @VerbalExprBinOp+ itSatisfiesArbitrary @VerbalExprBinOp
+ hspec-test/Ast/WhenClauseListSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WhenClauseListSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WhenClauseList+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WhenClauseList+ itSatisfiesArbitrary @WhenClauseList
+ hspec-test/Ast/WhenClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WhenClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WhenClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WhenClause+ itSatisfiesArbitrary @WhenClause
+ hspec-test/Ast/WhereClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WhereClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WhereClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WhereClause+ itSatisfiesArbitrary @WhereClause
+ hspec-test/Ast/WhereOrCurrentClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WhereOrCurrentClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WhereOrCurrentClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WhereOrCurrentClause+ itSatisfiesArbitrary @WhereOrCurrentClause
+ hspec-test/Ast/WindowClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WindowClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WindowClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WindowClause+ itSatisfiesArbitrary @WindowClause
+ hspec-test/Ast/WindowDefinitionSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WindowDefinitionSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WindowDefinition+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WindowDefinition+ itSatisfiesArbitrary @WindowDefinition
+ hspec-test/Ast/WindowExclusionClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WindowExclusionClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WindowExclusionClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WindowExclusionClause+ itSatisfiesArbitrary @WindowExclusionClause
+ hspec-test/Ast/WindowSpecificationSpec.hs view
@@ -0,0 +1,20 @@+module Ast.WindowSpecificationSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WindowSpecification+import Prelude+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WindowSpecification+ itSatisfiesArbitrary @WindowSpecification+ describe "Postgres grammar conformance" $ do+ -- gram.y:17428 window_specification: the sort clause and the+ -- partition clause are both followed by opt_frame_clause, whose+ -- leading keywords (RANGE/ROWS/GROUPS, kwlist.h:375,408,201) are+ -- unreserved and therefore also legal ColIds.+ itParses @WindowSpecification "(order by a rows unbounded preceding)"+ itParses @WindowSpecification "(order by a range unbounded preceding)"+ itParses @WindowSpecification "(partition by a groups unbounded preceding)"+ itParses @WindowSpecification "(partition by a order by b rows 1 preceding)"
+ hspec-test/Ast/WithClauseSpec.hs view
@@ -0,0 +1,10 @@+module Ast.WithClauseSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.WithClause+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @WithClause+ itSatisfiesArbitrary @WithClause
+ hspec-test/Ast/XconstSpec.hs view
@@ -0,0 +1,10 @@+module Ast.XconstSpec (spec) where++import Helpers.Specs+import PostgresqlSyntax.Ast.Xconst+import Test.Hspec++spec :: Spec+spec = do+ itSatisfiesIsAst @Xconst+ itSatisfiesArbitrary @Xconst
+ hspec-test/Helpers/Expectations.hs view
@@ -0,0 +1,77 @@+{-# LANGUAGE AllowAmbiguousTypes #-}++module Helpers.Expectations+ ( parsesTo,+ rejects,+ reportsError,+ reportsSourcePosError,+ parsesWithin,+ parsesToWith,+ rejectsWith,+ )+where++import qualified Data.List.NonEmpty as NonEmpty+import qualified Data.Text as Text+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Settings (Settings)+import Prelude+import Test.Hspec+import Text.Megaparsec.Pos (sourcePosPretty)++parsesTo :: forall a. (HasCallStack, IsAst a) => Text -> Expectation+parsesTo input =+ case parse @a mempty input of+ Left err -> expectationFailure (Text.unpack (err <> "\ninput: " <> input))+ Right _ -> pure ()++rejects :: forall a. (HasCallStack, IsAst a, Show a) => Text -> Expectation+rejects input =+ case parse @a mempty input of+ Left _ -> pure ()+ Right a ->+ expectationFailure+ ("expected a parse failure\ninput: " <> Text.unpack input <> "\nparsed: " <> show a)++reportsError :: forall a. (HasCallStack, IsAst a) => Text -> String -> Expectation+reportsError input expected =+ case parseWithPosError @a mempty input of+ Left err -> show (NonEmpty.head err) `shouldBe` expected+ Right _ -> expectationFailure "expected a parse error, but it succeeded"++-- | Like 'reportsError' but checks 'parseWithSourcePosError''s+-- 'Text.Megaparsec.SourcePos'-based errors instead.+reportsSourcePosError :: forall a. (HasCallStack, IsAst a) => Text -> String -> Expectation+reportsSourcePosError input expected =+ case parseWithSourcePosError @a mempty input of+ Left err ->+ let (pos, msg) = NonEmpty.head err+ in (Text.unpack (Text.pack (sourcePosPretty pos) <> " " <> msg)) `shouldBe` expected+ Right _ -> expectationFailure "expected a parse error, but it succeeded"++parsesWithin :: forall a. (HasCallStack, IsAst a) => Int -> Text -> Expectation+parsesWithin seconds input = do+ result <-+ timeout (seconds * 1000000)+ $ case parse @a mempty input of+ Left err -> expectationFailure (Text.unpack (err <> "\ninput: " <> input))+ Right _ -> pure ()+ case result of+ Nothing -> expectationFailure ("Did not finish parsing within " <> show seconds <> "s")+ Just () -> pure ()++-- | Like 'parsesTo' but with a non-default 'Settings'.+parsesToWith :: forall a. (HasCallStack, IsAst a) => Settings -> Text -> Expectation+parsesToWith settings input =+ case parse @a settings input of+ Left err -> expectationFailure (Text.unpack (err <> "\ninput: " <> input))+ Right _ -> pure ()++-- | Like 'rejects' but with a non-default 'Settings'.+rejectsWith :: forall a. (HasCallStack, IsAst a, Show a) => Settings -> Text -> Expectation+rejectsWith settings input =+ case parse @a settings input of+ Left _ -> pure ()+ Right a ->+ expectationFailure+ ("expected a parse failure\ninput: " <> Text.unpack input <> "\nparsed: " <> show a)
+ hspec-test/Helpers/Specs.hs view
@@ -0,0 +1,110 @@+{-# LANGUAGE AllowAmbiguousTypes #-}++module Helpers.Specs+ ( itSatisfiesIsAst,+ itSatisfiesCanonicalizes,+ itSatisfiesRefines,+ itSatisfiesExtends,+ itSatisfiesArbitrary,+ itParses,+ itRejects,+ itReportsError,+ itReportsSourcePosError,+ itParsesWithin,++ -- * Settings-aware variants+ itParsesWith,+ itRejectsWith,+ )+where++import qualified Data.Text as Text+import qualified Helpers.Expectations as Expectations+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Settings (Settings)+import Prelude+import Test.Hspec+import Test.Hspec.QuickCheck (prop)+import qualified Test.QuickCheck as Qc++itSatisfiesIsAst :: forall a. (IsAst a, Eq a, Show a, Qc.Arbitrary a) => Spec+itSatisfiesIsAst =+ describe "IsAst" $ for_ (isAstProperties @a) (uncurry prop)++itSatisfiesCanonicalizes :: forall a. (Canonicalizes a, Eq a, Show a, Qc.Arbitrary a) => Spec+itSatisfiesCanonicalizes =+ describe "Canonicalizes" $ for_ (canonicalizesProperties @a) (uncurry prop)++itSatisfiesRefines :: forall sub sup. (Refines sub sup, IsAst sub, Eq sub, Show sub, Qc.Arbitrary sub) => Spec+itSatisfiesRefines =+ describe "Refines" $ for_ (refinesProperties @sub @sup) (uncurry prop)++itSatisfiesExtends :: forall base ext. (Extends base ext, IsAst base, Eq base, Show base, Qc.Arbitrary base) => Spec+itSatisfiesExtends =+ describe "Extends" $ for_ (extendedByProperties @base @ext) (uncurry prop)++itSatisfiesArbitrary :: forall a. (IsAst a, Show a, Qc.Arbitrary a) => Spec+itSatisfiesArbitrary =+ describe "Arbitrary" $ do+ prop "Terminates at size 0" terminatesAtZero+ prop "Grows boundedly" growsBounded+ where+ terminatesAtZero =+ Qc.forAll (Qc.resize 0 (Qc.arbitrary @a)) $ \x ->+ let sql = toText mempty x+ len = Text.length sql+ in Qc.counterexample+ ("rendered " <> show len <> " chars at size 0 (max " <> show zeroSizeMaxLen <> ")" <> "\n" <> Text.unpack sql)+ (len <= zeroSizeMaxLen)+ where+ zeroSizeMaxLen = 500 :: Int++ growsBounded =+ Qc.forAll (Qc.resize maxGenSize (Qc.arbitrary @a)) $ \x ->+ let sql = toText mempty x+ len = Text.length sql+ in Qc.counterexample+ ("rendered " <> show len <> " chars at size " <> show maxGenSize <> " (max " <> show maxGenSizeMaxLen <> ")")+ (len <= maxGenSizeMaxLen)+ where+ maxGenSize = 100 :: Int+ maxGenSizeMaxLen = 1000000 :: Int++itParses :: forall a. (HasCallStack, IsAst a) => Text -> Spec+itParses sql =+ describe (Text.unpack sql) $ do+ it "Parses" (Expectations.parsesTo @a sql)++itRejects :: forall a. (HasCallStack, IsAst a, Show a) => Text -> Spec+itRejects sql =+ describe (Text.unpack sql) $ do+ it "Rejects" (Expectations.rejects @a sql)++itReportsError :: forall a. (HasCallStack, IsAst a) => Text -> String -> Spec+itReportsError sql expected =+ describe (Text.unpack sql) $ do+ it ("Reports error: " <> expected) (Expectations.reportsError @a sql expected)++-- | Like 'itReportsError' but checks 'PostgresqlSyntax.Algebra.parseWithSourcePosError''s+-- 'Text.Megaparsec.SourcePos'-based errors instead.+itReportsSourcePosError :: forall a. (HasCallStack, IsAst a) => Text -> String -> Spec+itReportsSourcePosError sql expected =+ describe (Text.unpack sql) $ do+ it ("Reports error: " <> expected) (Expectations.reportsSourcePosError @a sql expected)++itParsesWithin :: forall a. (HasCallStack, IsAst a) => Int -> Text -> Spec+itParsesWithin seconds sql =+ describe (Text.unpack sql) $ do+ it ("Parses within " <> show seconds <> " seconds") (Expectations.parsesWithin @a seconds sql)++-- | Wraps 'Expectations.parsesToWith' as a standalone 'Spec'.+itParsesWith :: forall a. (HasCallStack, IsAst a) => Settings -> Text -> Spec+itParsesWith settings sql =+ describe (Text.unpack sql) $ do+ it "Parses" (Expectations.parsesToWith @a settings sql)++-- | Wraps 'Expectations.rejectsWith' as a standalone 'Spec'.+itRejectsWith :: forall a. (HasCallStack, IsAst a, Show a) => Settings -> Text -> Spec+itRejectsWith settings sql =+ describe (Text.unpack sql) $ do+ it "Rejects" (Expectations.rejectsWith @a settings sql)
+ hspec-test/LoweringSpec.hs view
@@ -0,0 +1,210 @@+module LoweringSpec (spec) where++import qualified Data.List.NonEmpty as NonEmpty+import qualified Data.Map.Strict as Map+import qualified Data.Text as Text+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.CExpr+import PostgresqlSyntax.Ast.HsTargetEl+import PostgresqlSyntax.Ast.HsTargetList+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Indirection+import PostgresqlSyntax.Ast.IndirectionEl+import PostgresqlSyntax.Ast.JoinedTable+import PostgresqlSyntax.Ast.PreparableStmt+import PostgresqlSyntax.Ast.TargetEl+import PostgresqlSyntax.Lowering+import PostgresqlSyntax.Settings (Settings, haskellParamFields, haskellTargets)+import Prelude+import Test.Hspec+import Test.Hspec.QuickCheck (prop)+import qualified Test.QuickCheck as Qc++spec :: Spec+spec = do+ describe "renameParams" $ do+ describe "Slot allocation" $ do+ it "Gives two fields of the same parameter two slots" $+ slotsOf "select $1.$a::int8, $1.$b::text"+ `shouldBe` [((1, Just "a"), 1), ((1, Just "b"), 2)]+ it "Reuses the slot of a repeated field" $+ slotsOf "select $1.$a::int8, $1.$a::int8"+ `shouldBe` [((1, Just "a"), 1)]+ it "Reuses the slot of a bare parameter used twice" $+ slotsOf "select $1::int8, $1::int8"+ `shouldBe` [((1, Nothing), 1)]+ it "Keys a bare parameter separately from a field of the same parameter" $+ slotsOf "select $1.$a::int8, $1::text"+ `shouldBe` [((1, Nothing), 2), ((1, Just "a"), 1)]+ it "Numbers a later parameter after an earlier interpolated one" $+ slotsOf "select $1.$a::int8, $2::text"+ `shouldBe` [((1, Just "a"), 1), ((2, Nothing), 2)]+ it "Renumbers densely from a sparse, out-of-order original numbering" $+ slotsOf "select $7::int8, $3::text, $7::int8"+ `shouldBe` [((3, Nothing), 2), ((7, Nothing), 1)]++ describe "Traversal order" $ do+ it "Numbers an enclosing parameter before one nested in its indirection" $ do+ slotsOf "select $2[$1]" `shouldBe` [((1, Nothing), 2), ((2, Nothing), 1)]+ sqlOf "select $2[$1]" `shouldBe` "SELECT $1[$2]"+ it "Numbers the operands of an expression left to right" $+ sqlOf "select $3::int8 + $2::int8 + $1::int8"+ `shouldBe` "SELECT $1 :: int8 + $2 :: int8 + $3 :: int8"+ -- The one node whose children are not visited in render order. The+ -- pre-0.5 fork numbered a join qualifier ahead of the tables it joins,+ -- because its constructor held the join method in the first field, and+ -- eight of the 1513 quasiquotes in the Kronor repo depend on that to+ -- keep the numbering they already have. Reduced from+ -- kronor-worker/src/Worker/Acquirer/Clearhaus/FetchDisputes.hs:138.+ it "Numbers a join qualifier before the tables it joins" $+ sqlOf "select a.id from a join unnest($2::uuid[]) as b (id) on a.org = $1::bigint"+ `shouldBe` "SELECT a.id FROM a JOIN unnest($2 :: uuid[]) AS b (id) ON a.org = $1 :: BIGINT"++ describe "Rendering after lowering" $ do+ it "Leaves plain positional placeholders and no Haskell field names" $+ sqlOf "select $1.$b::text, $1.$a::int8, $2::uuid"+ `shouldBe` "SELECT $1 :: text, $2 :: int8, $3 :: uuid"+ it "Keeps an ordinary indirection chain on a non-interpolated parameter" $+ sqlOf "select $1.a.b" `shouldBe` "SELECT $1.a.b"++ describe "A Haskell head discards the rest of its indirection chain" $ do+ -- Not reachable through the parser: the field-name predicate stops only+ -- at whitespace and @:@, so @$1.$foo.bar@ lexes as the single field+ -- @foo.bar@ (which is what makes qualified names work) rather than as a+ -- two-element chain. Asserted on a hand-built AST so the semantics is+ -- pinned anyway - the whole chain is Haskell-level addressing that the+ -- caller resolves, so none of it should reach the server.+ let ast =+ ParamCExpr+ 1+ ( Just+ ( Indirection+ ( HsAttrNameIndirectionEl "foo"+ :| [AttrNameIndirectionEl (UnquotedIdent "bar")]+ )+ )+ )+ it "Drops the chain" $+ fst (renameParams ast) `shouldBe` ParamCExpr 1 Nothing+ it "Keys the slot by the Haskell field" $+ Map.toList (snd (renameParams ast)) `shouldBe` [((1, Just "foo"), 1)]+ it "Lexes a dotted field name as one field instead" $+ slotsOf "select $1.$foo.bar::int8" `shouldBe` [((1, Just "foo.bar"), 1)]++ -- The traversal rests on constructor field order equalling render order+ -- across some two hundred node types, which is checked here rather than+ -- assumed. Statements containing a qualified join are excluded because+ -- that is the one node where the two deliberately disagree; see the+ -- literal case above.+ prop "Allocates in the order the placeholders appear in the rendered SQL" $+ \(stmt :: PreparableStmt) ->+ not (containsQualJoin stmt) Qc.==>+ let rendered = toText mempty stmt+ allocated = fmap (fst . fst) (sortOn snd (Map.toList (snd (renameParams stmt))))+ appeared = renderedParamOrder rendered+ in Qc.counterexample+ (Text.unpack rendered <> "\nallocated: " <> show allocated <> "\nappeared: " <> show appeared)+ (allocated == appeared)++ describe "lower" $ do+ -- The consumer builds its row decoder by walking the Haskell targets, so+ -- they have to outlive the pass; only rendering erases them. Getting this+ -- backwards silently drops the decoder mapping for every statement that+ -- uses a function or record target.+ it "Keeps the Haskell targets in the tree it returns" $+ renderWith (haskellTargets True) (fst (lower (parseStmtWith (haskellTargets True) "select $mk (a::int8, b::text)")))+ `shouldBe` "SELECT $mk (a :: int8, b :: text)"+ it "Renders as plain SQL once erased" $+ sqlOf' (haskellTargets True) "select $mk (a::int8, b::text)"+ `shouldBe` "SELECT a :: int8, b :: text"+ it "Numbers placeholders inside Haskell targets in leaf order" $+ Map.toList (snd (lower (parseStmtWith (haskellTargets True <> hsParams) "select $mk ($2::int8, $1::text)")))+ `shouldBe` [((1, Nothing), 2), ((2, Nothing), 1)]+ it "Numbers as if the targets had been erased first" $+ let ast = parseStmtWith (haskellTargets True <> hsParams) "select $mk ($2::int8, $1::text) from t where x = $3::uuid"+ in snd (lower ast) `shouldBe` snd (renameParams (eraseHaskellTargets ast))++ describe "eraseHaskellTargets" $ do+ it "Flattens nested Haskell targets to their SQL leaves, depth-first" $+ eraseHaskellTargets+ ( HsTargetList+ ( HsFuncTargetEl+ "mk"+ [ SqlTargetEl (targetEl "a"),+ HsRecTargetEl "R" (HsFieldEl "x" (SqlTargetEl (targetEl "b")) :| [])+ ]+ :| [SqlTargetEl (targetEl "c")]+ )+ )+ `shouldBe` HsTargetList (NonEmpty.fromList (fmap (SqlTargetEl . targetEl) ["a", "b", "c"]))+ it "Reaches target lists nested anywhere in the value" $+ eraseHaskellTargets [Just (HsTargetList (HsFuncTargetEl "f" [SqlTargetEl (targetEl "a")] :| []))]+ `shouldBe` [Just (HsTargetList (SqlTargetEl (targetEl "a") :| []))]+ it "Leaves a list with no SQL leaves alone" $+ let noLeaves = HsTargetList (HsFuncTargetEl "f" [] :| [])+ in eraseHaskellTargets noLeaves `shouldBe` noLeaves++-- * Helpers++hsParams :: Settings+hsParams = haskellParamFields True++parseStmt :: Text -> PreparableStmt+parseStmt sql = either (error . Text.unpack) id (parse @PreparableStmt hsParams sql)++-- | The 'InputParams' of a lowered statement, as a key-ordered list.+slotsOf :: Text -> [((Int, Maybe Text), Int)]+slotsOf = Map.toList . snd . lower . parseStmt++-- |+-- A lowered statement rendered back to plain Postgres, the way a consumer+-- has to do it: 'lower' keeps the Haskell targets, so erase before+-- rendering.+sqlOf :: Text -> Text+sqlOf = toText mempty . eraseHaskellTargets . fst . lower . parseStmt++containsQualJoin :: (Data a) => a -> Bool+containsQualJoin x = case cast x :: Maybe JoinedTable of+ Just QualJoinedTable {} -> True+ _ -> or (gmapQ containsQualJoin x)++parseStmtWith :: Settings -> Text -> PreparableStmt+parseStmtWith settings sql = either (error . Text.unpack) id (parse @PreparableStmt settings sql)++renderWith :: Settings -> PreparableStmt -> Text+renderWith = toText++sqlOf' :: Settings -> Text -> Text+sqlOf' settings = toText mempty . eraseHaskellTargets . fst . lower . parseStmtWith settings++targetEl :: Text -> TargetEl+targetEl = either (error . Text.unpack) id . parse @TargetEl mempty++-- |+-- The original indices of the @$n@ placeholders of a rendered statement, in+-- order of first appearance.+--+-- A placeholder is the only thing rendering emits a bare @$@ for, but a @$@+-- can also occur inside a string constant or a quoted identifier - skipped+-- here rather than scanned - and inside an unquoted identifier, where it is a+-- legal continuation character. Hence the preceding-character check: a+-- placeholder never follows an identifier character.+renderedParamOrder :: Text -> [Int]+renderedParamOrder = nub . go ' ' . Text.unpack+ where+ go previous = \case+ [] -> []+ '\'' : rest -> go '\'' (skipQuoted '\'' rest)+ '"' : rest -> go '"' (skipQuoted '"' rest)+ '$' : rest+ | not (isIdentChar previous),+ (digits@(_ : _), rest') <- span isDigit rest ->+ read digits : go '0' rest'+ a : rest -> go a rest+ isIdentChar a = isAlphaNum a || a == '_' || a == '$'+ -- Both quotings escape the quote character by doubling it.+ skipQuoted q = \case+ [] -> []+ a : b : rest | a == q, b == q -> skipQuoted q rest+ a : rest | a == q -> rest+ _ : rest -> skipQuoted q rest
+ hspec-test/Main.hs view
@@ -0,0 +1,8 @@+module Main (main) where++import Prelude+import qualified Spec+import Test.Hspec++main :: IO ()+main = hspec $ parallel Spec.spec
+ hspec-test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-}
+ library-internal/PostgresqlSyntax/Algebra.hs view
@@ -0,0 +1,270 @@+{-# LANGUAGE AllowAmbiguousTypes #-}++module PostgresqlSyntax.Algebra+ ( IsAst (..),+ toText,+ parse,+ parseWithPosError,+ parseWithSourcePosError,+ isAstProperties,+ Canonicalizes (..),+ canonicalizesProperties,+ Refines (..),+ refinesProperties,+ LeftRecursive (..),+ Extends (..),+ extendedByProperties,+ parseMaybeExtended,+ parseExtended,+ parseExtensionChain,+ Parser,+ )+where++import qualified Data.Text as Text+import qualified HeadedMegaparsec as Parser+import qualified PostgresqlSyntax.Extras.HeadedMegaparsec as Extras+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (Settings)+import qualified Test.QuickCheck as Qc+import qualified Text.Megaparsec as Megaparsec+import qualified TextBuilder++-- |+-- Class of AST types that can be rendered to SQL text and parsed back again.+--+-- Laws:+--+-- * __Roundtrips__: @parse settings (toText settings a) = Right a@ for every+-- 'Settings' - rendering and parsing are inverses.+-- * __Congruent rendering__: @a == b => toTextBuilder settings a == toTextBuilder settings b@+-- for every 'Settings' - rendering only depends on the value, not on how it+-- was constructed. This is what makes it meaningful to say two structurally+-- different shapes can still render to identical text - the ambiguity that+-- 'Canonicalizes' exists to resolve.+class IsAst a where+ -- |+ -- Render an AST value to a 'TextBuilder' using the given 'Settings'.+ -- This is the low-level rendering primitive; 'toText' wraps it.+ toTextBuilder :: Settings -> a -> TextBuilder++ -- |+ -- A parser for this AST type, parameterized by 'Settings'.+ -- The parser must satisfy the roundtrip law documented on 'IsAst'.+ parser :: Settings -> Parser a++-- |+-- 'Qc.Property'-checkers for 'IsAst'\'s documented laws, keyed by name.+isAstProperties :: forall a. (IsAst a, Eq a, Show a, Qc.Arbitrary a) => [(String, Qc.Property)]+isAstProperties =+ [ ( "Roundtrips",+ Qc.property $ \(a :: a) ->+ let sql = toText mempty a+ in case parse mempty sql of+ Left err ->+ Qc.counterexample ("rendered: " <> Text.unpack sql <> "\nparse failed: " <> Text.unpack err) False+ Right a' ->+ Qc.counterexample+ ("rendered: " <> Text.unpack sql <> "\nrestored: " <> Text.unpack (toText mempty a'))+ (a' Qc.=== a)+ ),+ ( "Renders equal values equally",+ Qc.property $ \(a :: a) (b :: a) ->+ a /= b || toText mempty a == toText mempty b+ )+ ]++-- |+-- Render a value to 'Text' via its 'toTextBuilder' method.+toText :: (IsAst a) => Settings -> a -> Text+toText settings = TextBuilder.toText . toTextBuilder settings++-- |+-- Parse a 'Text' input with the type's 'parser', returning either a+-- pretty-printed error or the parsed value. The parser is chosen by the+-- caller's type inference (via the 'IsAst' constraint), so callers no longer+-- pass an explicit parser argument.+parse :: (IsAst a) => Settings -> Text -> Either Text a+parse settings = first Text.pack . Extras.run (Extras.totally (parser settings))++-- |+-- Like 'parse' but returns the structured error list (each error paired with+-- its byte offset) instead of a single pretty-printed message.+parseWithPosError :: (IsAst a) => Settings -> Text -> Either (NonEmpty (Int, Text)) a+parseWithPosError settings = first (fmap (second Text.pack)) . Extras.runParserWithErrorPos (Extras.totally (parser settings))++-- |+-- Like 'parseWithPosError' but pairs each error with its+-- 'Text.Megaparsec.SourcePos' instead of a raw byte offset.+parseWithSourcePosError :: (IsAst a) => Settings -> Text -> Either (NonEmpty (Megaparsec.SourcePos, Text)) a+parseWithSourcePosError settings = first (fmap (second Text.pack)) . Extras.runParserWithSourcePosError (Extras.totally (parser settings))++-- |+-- Laws:+--+-- * __Idempotent__: @canonicalize . canonicalize = canonicalize@+-- * __Parse-agreement__ (the property this class exists to provide):+-- @parse settings . toText settings = Right . canonicalize@ for every+-- 'PostgresqlSyntax.Settings.Settings'+class (IsAst a) => Canonicalizes a where+ canonicalize :: a -> a+ canonicalize = id++-- |+-- 'Qc.Property'-checkers for 'Canonicalizes'\'s documented laws, keyed by+-- name. \"Parse-agreement\" is tested at 'mempty' 'Settings', matching how+-- 'isAstProperties'\'s \"Renders equal values equally\" property handles \"for+-- every 'Settings'\" - no 'Qc.Arbitrary' 'Settings' instance exists or is+-- being added.+canonicalizesProperties :: forall a. (Canonicalizes a, Eq a, Show a, Qc.Arbitrary a) => [(String, Qc.Property)]+canonicalizesProperties =+ [ ( "Idempotent",+ Qc.property $ \(a :: a) ->+ canonicalize (canonicalize a) Qc.=== canonicalize a+ ),+ ( "Parse-agreement",+ Qc.property $ \(a :: a) ->+ parse mempty (toText mempty a) Qc.=== Right (canonicalize a)+ )+ ]++-- |+-- Laws:+--+-- * __Refinement law__: @project . embed = Just@+--+-- Expresses embedding relationships between AST node types across module+-- boundaries where cross-module pattern matching is unavailable. Instances+-- hold between two types when the 'sub' type can be trivially embedded into+-- 'sup', and trivial 'sup' values can be recognized as such a 'sub' and+-- extracted back out.+class Refines sub sup where+ embed :: sub -> sup+ project :: sup -> Maybe sub++-- |+-- 'Qc.Property'-checkers for 'Refines'\'s documented laws, keyed by name.+refinesProperties :: forall sub sup. (Refines sub sup, IsAst sub, Eq sub, Show sub, Qc.Arbitrary sub) => [(String, Qc.Property)]+refinesProperties =+ [ ( "Refinement law",+ Qc.property $ \(a :: sub) ->+ project (embed a :: sup) Qc.=== Just a+ )+ ]++-- |+-- A type some of whose grammar productions are left-recursive - i.e. there+-- is a larger recursive form built by extending a value of this type on its+-- left. 'parseBase' is everything that is /not/ one of those productions:+-- the @β@ of @A -> Aα | β@.+--+-- This is a strictly weaker claim than 'Extends', which additionally+-- names the specific @ext@ of one such hub. A type can be 'LeftRecursive'+-- without being any hub's @base@ - 'PostgresqlSyntax.Ast.JoinedTable' and+-- 'PostgresqlSyntax.Ast.SimpleSelect' both are, since each is reached by+-- extending a /different/ type (@table_ref@ and @select_clause@+-- respectively) yet still has non-left-recursive productions of its own.+--+-- Separating this from 'Extends' is what lets each instance live with+-- the type it constructs: 'parseBase' mentions only its own type, so it+-- belongs to that type's module, while a hub's 'parseExtensions' belongs to+-- the module defining @ext@. Because both are class methods, either module+-- can reach the other's parser through an @hs-boot@ instance declaration+-- without exporting a bare helper.+--+-- Note that \"non-recursive\" here means non-/left/-recursive only: a+-- production may still recurse, so long as it doesn't begin with the+-- recursive position (e.g. @'(' joined_table ')'@).+class (IsAst a) => LeftRecursive a where+ -- | Parse only the productions that don't left-recurse (@β@).+ parseBase :: Settings -> Parser a++-- |+-- The two halves of a left-recursive grammar production, split apart by+-- left-recursion elimination (@A -> Aα | β@ becomes @A -> β α*@): 'base' is+-- @A@ (and supplies @β@ via its 'LeftRecursive' instance), and 'ext' is+-- what one or more @α@\'s, applied to a @base@, produce.+--+-- Laws:+--+-- * __Base-parser agreement__: @parser \@base = parseMaybeExtended \@base@+-- * __Maximal munch__: 'parseExtensions' must not return while a further+-- extension is available - instances build this on 'parseExtensionChain'+-- where possible, which already guarantees it.+class (LeftRecursive base, Refines ext base) => Extends base ext | ext -> base where+ -- | Parse one or more extensions onto an already-parsed left operand,+ -- folding as it goes, and return the fully-extended result.+ parseExtensions :: Settings -> base -> Parser ext++-- |+-- 'Qc.Property'-checker for 'Extends'\'s \"Base-parser agreement\" law,+-- keyed by name. \"Maximal munch\" isn't checked here: it's a per-instance+-- parsing obligation, not something a generated 'base' value can exercise+-- through 'parser' alone. The agreement check is itself up to whether+-- parsing succeeds, ignoring error message text, since a base's 'parser'+-- may wrap 'parseMaybeExtended' in a 'HeadedMegaparsec.label' or similar+-- that changes failure messages without changing what's accepted.+extendedByProperties :: forall base ext. (Extends base ext, IsAst base, Eq base, Show base, Qc.Arbitrary base) => [(String, Qc.Property)]+extendedByProperties =+ [ ( "Base-parser agreement",+ Qc.property $ \(a :: base) ->+ let sql = toText mempty a+ run p = first (const ()) (Extras.run (Extras.totally p) sql)+ in run (parser @base mempty) Qc.=== run (parseMaybeExtended @ext mempty)+ )+ ]++-- |+-- Parses zero or more extensions onto a 'parseBase', via 'parseExtensions'.+-- This is what @A -> β α*@ (the whole of @A@) means as a parser.+parseMaybeExtended :: forall ext base. (Extends base ext) => Settings -> Parser base+parseMaybeExtended settings = do+ b <- parseBase @base settings+ optional (parseExtensions @base settings b) >>= maybe (pure b) (pure . embed @ext @base)++-- |+-- Like 'parseMaybeExtended', but requires at least one extension to follow+-- the base, and so returns the fully-applied 'ext' type directly rather+-- than 'base'. This is what a bare @α*@ (one or more) means as a parser,+-- for hubs where a chain of at least one extension is itself the+-- interesting type (e.g. a @joined_table@, which is never a bare+-- @table_ref@ with zero joins).+--+-- Unlike 'parseMaybeExtended', 'parseBase' here is wrapped in+-- 'Parser.wrapToHead'. Without it, if 'parseBase' itself commits past an+-- internal 'Parser.endHead' (e.g. by matching a nested, fully-parenthesized+-- instance of the very thing this function's caller is one alternative+-- for), that commitment silently swallows the immediately-following "is+-- there at least one extension?" check: a missing extension would fail as+-- a hard, uncatchable error instead of a clean one this function's own+-- caller can backtrack from. 'wrapToHead' resets that, forcing the check+-- to fail cleanly. 'parseMaybeExtended' doesn't need this: its own+-- extension check already goes through 'optional', which independently+-- wraps in 'Megaparsec.try' regardless of what 'parseBase' committed to.+parseExtended :: forall base ext. (Extends base ext) => Settings -> Parser ext+parseExtended settings = do+ b <- Parser.wrapToHead (parseBase @base settings)+ parseExtensions @base settings b++-- |+-- Parses one or more items back-to-back, wrapping each in+-- 'Parser.wrapToHead'\/'Parser.endHead' so that, once an item's own head has+-- matched, backtracking out of the whole chain (back to "there are no more+-- items") is no longer attempted - matching the hand-written+-- recursive-descent loops this replaces. This is the shared backtracking+-- protocol underlying 'Extends'\'s \"Maximal munch\" law: an instance+-- building 'parseExtensions' on top of this combinator gets the law for+-- free, since 'go' only stops once a further item genuinely isn't+-- available.+parseExtensionChain :: Parser item -> Parser (NonEmpty item)+parseExtensionChain item = go+ where+ go = do+ i <- Parser.wrapToHead item+ Parser.endHead+ rest <- optional go+ pure $ case rest of+ Nothing -> i :| []+ Just (j :| js) -> i :| j : js++type Parser = Parser.HeadedParsec Void Text
+ library-internal/PostgresqlSyntax/Ast.hs view
@@ -0,0 +1,385 @@+-- |+-- Names for nodes mostly resemble the according definitions in the @gram.y@+-- original Postgres parser file, except for the cases where we can optimize on that.+--+-- For reasoning see the docs of the parsing module of this project.+module PostgresqlSyntax.Ast+ ( -- * Nodes+ AExpr (..),+ AExprReversableOp (..),+ AexprConst (..),+ AliasClause (..),+ AllOp (..),+ AnyName (..),+ AnyOperator (..),+ ArrayBounds (..),+ ArrayExpr (..),+ ArrayExprList (..),+ AscDesc (..),+ Attrs (..),+ BExpr (..),+ BExprIsOp (..),+ Bconst (..),+ Bit (..),+ CExpr (..),+ CallStmt (..),+ CaseExpr (..),+ Character (..),+ Columnref (..),+ CommonTableExpr (..),+ ConfExpr (..),+ ConstCharacter (..),+ ConstDatetime (..),+ ConstTypename (..),+ DeleteStmt (..),+ ExplicitRow (..),+ ExprList (..),+ ExtractArg (..),+ ExtractList (..),+ Fconst (..),+ ForLockingClause (..),+ ForLockingItem (..),+ ForLockingStrength (..),+ FrameBound (..),+ FrameClause (..),+ FrameClauseMode (..),+ FrameExtent (..),+ FromClause (..),+ FromList (..),+ FuncAliasClause (..),+ FuncApplication (..),+ FuncApplicationParams (..),+ FuncArgExpr (..),+ FuncConstArgs (..),+ FuncExpr (..),+ FuncExprCommonSubexpr (..),+ FuncExprWindowless (..),+ FuncName (..),+ FuncTable (..),+ GenericType (..),+ GroupByItem (..),+ GroupClause (..),+ HavingClause (..),+ HsFieldTargetEl (..),+ HsTargetEl (..),+ HsTargetList (..),+ Iconst (..),+ Ident (..),+ ImplicitRow (..),+ InExpr (..),+ IndexElem (..),+ IndexElemDef (..),+ IndexParams (..),+ Indirection (..),+ IndirectionEl (..),+ InsertColumnItem (..),+ InsertColumnList (..),+ InsertRest (..),+ InsertStmt (..),+ InsertTarget (..),+ Interval (..),+ IntervalSecond (..),+ IntoClause (..),+ JoinQual (..),+ JoinType (..),+ JoinedTable (..),+ LimitClause (..),+ MathOp (..),+ NameList (..),+ NullsOrder (..),+ Numeric (..),+ OffsetClause (..),+ OnConflict (..),+ OnConflictDo (..),+ Op (..),+ OptOrdinality (..),+ OptTempTableName (..),+ OptVarying (..),+ OverClause (..),+ OverlayList (..),+ OverrideKind (..),+ PositionList (..),+ PreparableStmt (..),+ QualAllOp (..),+ QualOp (..),+ QualifiedName (..),+ RelationExpr (..),+ RelationExprOptAlias (..),+ ReturningClause (..),+ Row (..),+ RowsfromItem (..),+ RowsfromList (..),+ Sconst (..),+ SelectBinOp (..),+ SelectClause (..),+ SelectFetchFirstValue (..),+ SelectLimit (..),+ SelectLimitValue (..),+ SelectNoParens (..),+ SelectStmt (..),+ SelectWithParens (..),+ SetClause (..),+ SetClauseList (..),+ SetTarget (..),+ SetTargetList (..),+ SimpleSelect (..),+ SimpleTypename (..),+ SortBy (..),+ SortClause (..),+ SubType (..),+ SubqueryOp (..),+ SubstrList (..),+ SubstrListFromFor (..),+ SymbolicExprBinOp (..),+ TableFuncElement (..),+ TableFuncElementList (..),+ TableRef (..),+ TablesampleClause (..),+ TargetEl (..),+ TargetList (..),+ Targeting (..),+ Timezone (..),+ TrimList (..),+ TrimModifier (..),+ TypeList (..),+ Typename (..),+ TypenameArrayDimensions (..),+ UpdateStmt (..),+ UsingClause (..),+ ValuesClause (..),+ VerbalExprBinOp (..),+ WhenClause (..),+ WhenClauseList (..),+ WhereClause (..),+ WhereOrCurrentClause (..),+ WindowClause (..),+ WindowDefinition (..),+ WindowExclusionClause (..),+ WindowSpecification (..),+ WithClause (..),+ Xconst (..),++ -- * Bare aliases+ ExistingWindowName,+ PartitionClause,+ RepeatableClause,+ ColDefList,+ CollateClause,+ WithinGroupClause,+ FilterClause,+ OverlayPlacing,+ SubstrFrom,+ SubstrFor,+ CaseArg,+ CaseDefault,+ ConstBit,+ ColId,+ ColLabel,+ Name,+ CursorName,+ AttrName,+ TypeModifiers,+ Collate,+ Class,+ TypeFunctionName,+ )+where++import PostgresqlSyntax.Ast.AExpr+import PostgresqlSyntax.Ast.AExprReversableOp+import PostgresqlSyntax.Ast.AexprConst+import PostgresqlSyntax.Ast.AliasClause+import PostgresqlSyntax.Ast.AllOp+import PostgresqlSyntax.Ast.AnyName+import PostgresqlSyntax.Ast.AnyOperator+import PostgresqlSyntax.Ast.ArrayBounds+import PostgresqlSyntax.Ast.ArrayExpr+import PostgresqlSyntax.Ast.ArrayExprList+import PostgresqlSyntax.Ast.AscDesc+import PostgresqlSyntax.Ast.Attrs+import PostgresqlSyntax.Ast.BExpr+import PostgresqlSyntax.Ast.BExprIsOp+import PostgresqlSyntax.Ast.Bconst+import PostgresqlSyntax.Ast.Bit+import PostgresqlSyntax.Ast.CExpr+import PostgresqlSyntax.Ast.CallStmt+import PostgresqlSyntax.Ast.CaseExpr+import PostgresqlSyntax.Ast.Character+import PostgresqlSyntax.Ast.Columnref+import PostgresqlSyntax.Ast.CommonTableExpr+import PostgresqlSyntax.Ast.ConfExpr+import PostgresqlSyntax.Ast.ConstCharacter+import PostgresqlSyntax.Ast.ConstDatetime+import PostgresqlSyntax.Ast.ConstTypename+import PostgresqlSyntax.Ast.DeleteStmt+import PostgresqlSyntax.Ast.ExplicitRow+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.ExtractArg+import PostgresqlSyntax.Ast.ExtractList+import PostgresqlSyntax.Ast.Fconst+import PostgresqlSyntax.Ast.ForLockingClause+import PostgresqlSyntax.Ast.ForLockingItem+import PostgresqlSyntax.Ast.ForLockingStrength+import PostgresqlSyntax.Ast.FrameBound+import PostgresqlSyntax.Ast.FrameClause+import PostgresqlSyntax.Ast.FrameClauseMode+import PostgresqlSyntax.Ast.FrameExtent+import PostgresqlSyntax.Ast.FromClause+import PostgresqlSyntax.Ast.FromList+import PostgresqlSyntax.Ast.FuncAliasClause+import PostgresqlSyntax.Ast.FuncApplication+import PostgresqlSyntax.Ast.FuncApplicationParams+import PostgresqlSyntax.Ast.FuncArgExpr+import PostgresqlSyntax.Ast.FuncConstArgs+import PostgresqlSyntax.Ast.FuncExpr+import PostgresqlSyntax.Ast.FuncExprCommonSubexpr+import PostgresqlSyntax.Ast.FuncExprWindowless+import PostgresqlSyntax.Ast.FuncName+import PostgresqlSyntax.Ast.FuncTable+import PostgresqlSyntax.Ast.GenericType+import PostgresqlSyntax.Ast.GroupByItem+import PostgresqlSyntax.Ast.GroupClause+import PostgresqlSyntax.Ast.HavingClause+import PostgresqlSyntax.Ast.HsTargetEl+import PostgresqlSyntax.Ast.HsTargetList+import PostgresqlSyntax.Ast.Iconst+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.ImplicitRow+import PostgresqlSyntax.Ast.InExpr+import PostgresqlSyntax.Ast.IndexElem+import PostgresqlSyntax.Ast.IndexElemDef+import PostgresqlSyntax.Ast.IndexParams+import PostgresqlSyntax.Ast.Indirection+import PostgresqlSyntax.Ast.IndirectionEl+import PostgresqlSyntax.Ast.InsertColumnItem+import PostgresqlSyntax.Ast.InsertColumnList+import PostgresqlSyntax.Ast.InsertRest+import PostgresqlSyntax.Ast.InsertStmt+import PostgresqlSyntax.Ast.InsertTarget+import PostgresqlSyntax.Ast.Interval+import PostgresqlSyntax.Ast.IntervalSecond+import PostgresqlSyntax.Ast.IntoClause+import PostgresqlSyntax.Ast.JoinQual+import PostgresqlSyntax.Ast.JoinType+import PostgresqlSyntax.Ast.JoinedTable+import PostgresqlSyntax.Ast.LimitClause+import PostgresqlSyntax.Ast.MathOp+import PostgresqlSyntax.Ast.NameList+import PostgresqlSyntax.Ast.NullsOrder+import PostgresqlSyntax.Ast.Numeric+import PostgresqlSyntax.Ast.OffsetClause+import PostgresqlSyntax.Ast.OnConflict+import PostgresqlSyntax.Ast.OnConflictDo+import PostgresqlSyntax.Ast.Op+import PostgresqlSyntax.Ast.OptOrdinality+import PostgresqlSyntax.Ast.OptTempTableName+import PostgresqlSyntax.Ast.OptVarying+import PostgresqlSyntax.Ast.OverClause+import PostgresqlSyntax.Ast.OverlayList+import PostgresqlSyntax.Ast.OverrideKind+import PostgresqlSyntax.Ast.PositionList+import PostgresqlSyntax.Ast.PreparableStmt+import PostgresqlSyntax.Ast.QualAllOp+import PostgresqlSyntax.Ast.QualOp+import PostgresqlSyntax.Ast.QualifiedName+import PostgresqlSyntax.Ast.RelationExpr+import PostgresqlSyntax.Ast.RelationExprOptAlias+import PostgresqlSyntax.Ast.ReturningClause+import PostgresqlSyntax.Ast.Row+import PostgresqlSyntax.Ast.RowsfromItem+import PostgresqlSyntax.Ast.RowsfromList+import PostgresqlSyntax.Ast.Sconst+import PostgresqlSyntax.Ast.SelectBinOp+import PostgresqlSyntax.Ast.SelectClause+import PostgresqlSyntax.Ast.SelectFetchFirstValue+import PostgresqlSyntax.Ast.SelectLimit+import PostgresqlSyntax.Ast.SelectLimitValue+import PostgresqlSyntax.Ast.SelectNoParens+import PostgresqlSyntax.Ast.SelectStmt+import PostgresqlSyntax.Ast.SelectWithParens+import PostgresqlSyntax.Ast.SetClause+import PostgresqlSyntax.Ast.SetClauseList+import PostgresqlSyntax.Ast.SetTarget+import PostgresqlSyntax.Ast.SetTargetList+import PostgresqlSyntax.Ast.SimpleSelect+import PostgresqlSyntax.Ast.SimpleTypename+import PostgresqlSyntax.Ast.SortBy+import PostgresqlSyntax.Ast.SortClause+import PostgresqlSyntax.Ast.SubType+import PostgresqlSyntax.Ast.SubqueryOp+import PostgresqlSyntax.Ast.SubstrList+import PostgresqlSyntax.Ast.SubstrListFromFor+import PostgresqlSyntax.Ast.SymbolicExprBinOp+import PostgresqlSyntax.Ast.TableFuncElement+import PostgresqlSyntax.Ast.TableFuncElementList+import PostgresqlSyntax.Ast.TableRef+import PostgresqlSyntax.Ast.TablesampleClause+import PostgresqlSyntax.Ast.TargetEl+import PostgresqlSyntax.Ast.TargetList+import PostgresqlSyntax.Ast.Targeting+import PostgresqlSyntax.Ast.Timezone+import PostgresqlSyntax.Ast.TrimList+import PostgresqlSyntax.Ast.TrimModifier+import PostgresqlSyntax.Ast.TypeList+import PostgresqlSyntax.Ast.Typename+import PostgresqlSyntax.Ast.TypenameArrayDimensions+import PostgresqlSyntax.Ast.UpdateStmt+import PostgresqlSyntax.Ast.UsingClause+import PostgresqlSyntax.Ast.ValuesClause+import PostgresqlSyntax.Ast.VerbalExprBinOp+import PostgresqlSyntax.Ast.WhenClause+import PostgresqlSyntax.Ast.WhenClauseList+import PostgresqlSyntax.Ast.WhereClause+import PostgresqlSyntax.Ast.WhereOrCurrentClause+import PostgresqlSyntax.Ast.WindowClause+import PostgresqlSyntax.Ast.WindowDefinition+import PostgresqlSyntax.Ast.WindowExclusionClause+import PostgresqlSyntax.Ast.WindowSpecification+import PostgresqlSyntax.Ast.WithClause+import PostgresqlSyntax.Ast.Xconst++-- * Bare aliases++type ExistingWindowName = ColId++type PartitionClause = ExprList++type RepeatableClause = AExpr++type ColDefList = TableFuncElementList++type CollateClause = AnyName++type WithinGroupClause = SortClause++type FilterClause = AExpr++type OverlayPlacing = AExpr++type SubstrFrom = AExpr++type SubstrFor = AExpr++type CaseArg = AExpr++type CaseDefault = AExpr++type ConstBit = Bit++type ColId = Ident++type ColLabel = Ident++type Name = ColId++type CursorName = Name++type AttrName = ColLabel++type TypeModifiers = ExprList++type Collate = AnyName++type Class = AnyName++type TypeFunctionName = Ident
+ library-internal/PostgresqlSyntax/Ast/AExpr.hs view
@@ -0,0 +1,433 @@+module PostgresqlSyntax.Ast.AExpr+ ( AExpr (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AExprReversableOp+import PostgresqlSyntax.Ast.AnyName+import PostgresqlSyntax.Ast.CExpr (CExpr)+import qualified PostgresqlSyntax.Ast.CExpr as CExpr+import PostgresqlSyntax.Ast.QualOp+import PostgresqlSyntax.Ast.Row+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import PostgresqlSyntax.Ast.SubType+import PostgresqlSyntax.Ast.SubqueryOp+import PostgresqlSyntax.Ast.SymbolicExprBinOp+import PostgresqlSyntax.Ast.Typename+import PostgresqlSyntax.Ast.VerbalExprBinOp+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (Settings)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- a_expr:+-- | c_expr+-- | a_expr TYPECAST Typename+-- | a_expr COLLATE any_name+-- | a_expr AT TIME ZONE a_expr+-- | '+' a_expr+-- | '-' a_expr+-- | a_expr '+' a_expr+-- | a_expr '-' a_expr+-- | a_expr '*' a_expr+-- | a_expr '/' a_expr+-- | a_expr '%' a_expr+-- | a_expr '^' a_expr+-- | a_expr '<' a_expr+-- | a_expr '>' a_expr+-- | a_expr '=' a_expr+-- | a_expr LESS_EQUALS a_expr+-- | a_expr GREATER_EQUALS a_expr+-- | a_expr NOT_EQUALS a_expr+-- | a_expr qual_Op a_expr+-- | qual_Op a_expr+-- | a_expr AND a_expr+-- | a_expr OR a_expr+-- | NOT a_expr+-- | NOT_LA a_expr+-- | a_expr LIKE a_expr+-- | a_expr LIKE a_expr ESCAPE a_expr+-- | a_expr NOT_LA LIKE a_expr+-- | a_expr NOT_LA LIKE a_expr ESCAPE a_expr+-- | a_expr ILIKE a_expr+-- | a_expr ILIKE a_expr ESCAPE a_expr+-- | a_expr NOT_LA ILIKE a_expr+-- | a_expr NOT_LA ILIKE a_expr ESCAPE a_expr+-- | a_expr SIMILAR TO a_expr+-- | a_expr SIMILAR TO a_expr ESCAPE a_expr+-- | a_expr NOT_LA SIMILAR TO a_expr+-- | a_expr NOT_LA SIMILAR TO a_expr ESCAPE a_expr+-- | a_expr IS NULL_P+-- | a_expr ISNULL+-- | a_expr IS NOT NULL_P+-- | a_expr NOTNULL+-- | row OVERLAPS row+-- | a_expr IS TRUE_P+-- | a_expr IS NOT TRUE_P+-- | a_expr IS FALSE_P+-- | a_expr IS NOT FALSE_P+-- | a_expr IS UNKNOWN+-- | a_expr IS NOT UNKNOWN+-- | a_expr IS DISTINCT FROM a_expr+-- | a_expr IS NOT DISTINCT FROM a_expr+-- | a_expr IS OF '(' type_list ')'+-- | a_expr IS NOT OF '(' type_list ')'+-- | a_expr BETWEEN opt_asymmetric b_expr AND a_expr+-- | a_expr NOT_LA BETWEEN opt_asymmetric b_expr AND a_expr+-- | a_expr BETWEEN SYMMETRIC b_expr AND a_expr+-- | a_expr NOT_LA BETWEEN SYMMETRIC b_expr AND a_expr+-- | a_expr IN_P in_expr+-- | a_expr NOT_LA IN_P in_expr+-- | a_expr subquery_Op sub_type select_with_parens+-- | a_expr subquery_Op sub_type '(' a_expr ')'+-- | UNIQUE select_with_parens+-- | a_expr IS DOCUMENT_P+-- | a_expr IS NOT DOCUMENT_P+-- | DEFAULT+-- @+data AExpr+ = CExprAExpr CExpr+ | TypecastAExpr AExpr Typename+ | CollateAExpr AExpr AnyName+ | AtTimeZoneAExpr AExpr AExpr+ | PlusAExpr AExpr+ | MinusAExpr AExpr+ | SymbolicBinOpAExpr AExpr SymbolicExprBinOp AExpr+ | PrefixQualOpAExpr QualOp AExpr+ | AndAExpr AExpr AExpr+ | OrAExpr AExpr AExpr+ | NotAExpr AExpr+ | VerbalExprBinOpAExpr AExpr Bool VerbalExprBinOp AExpr (Maybe AExpr)+ | ReversableOpAExpr AExpr Bool AExprReversableOp+ | IsnullAExpr AExpr+ | NotnullAExpr AExpr+ | OverlapsAExpr Row Row+ | SubqueryAExpr AExpr SubqueryOp SubType (Either SelectWithParens AExpr)+ | UniqueAExpr SelectWithParens+ | DefaultAExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AExpr where+ toTextBuilder settings = \case+ CExprAExpr a -> toTextBuilder settings a+ TypecastAExpr a b -> renderOperand a <> " :: " <> toTextBuilder settings b+ CollateAExpr a b -> renderOperand a <> " COLLATE " <> toTextBuilder settings b+ AtTimeZoneAExpr a b -> renderOperand a <> " AT TIME ZONE " <> toTextBuilder settings b+ PlusAExpr a -> "+ " <> toTextBuilder settings a+ MinusAExpr a -> "- " <> toTextBuilder settings a+ SymbolicBinOpAExpr a b c -> renderOperand a <> " " <> toTextBuilder settings b <> " " <> toTextBuilder settings c+ PrefixQualOpAExpr a b -> toTextBuilder settings a <> " " <> toTextBuilder settings b+ AndAExpr a b -> renderOperand a <> " AND " <> toTextBuilder settings b+ OrAExpr a b -> renderOperand a <> " OR " <> toTextBuilder settings b+ NotAExpr a -> "NOT " <> toTextBuilder settings a+ VerbalExprBinOpAExpr a b c d e -> renderOperand a <> " " <> bool "" "NOT " b <> toTextBuilder settings c <> " " <> renderVerbalRhs e d <> foldMap (mappend " ESCAPE " . toTextBuilder settings) e+ ReversableOpAExpr a b c -> renderOperand a <> " " <> renderAExprReversableOp b c+ IsnullAExpr a -> renderOperand a <> " ISNULL"+ NotnullAExpr a -> renderOperand a <> " NOTNULL"+ OverlapsAExpr a b -> toTextBuilder settings a <> " OVERLAPS " <> toTextBuilder settings b+ SubqueryAExpr a b c d -> renderOperand a <> " " <> toTextBuilder settings b <> " " <> toTextBuilder settings c <> " " <> either (toTextBuilder settings) (TextBuilders.renderInParens . toTextBuilder settings) d+ UniqueAExpr a -> "UNIQUE " <> toTextBuilder settings a+ DefaultAExpr -> "DEFAULT"+ where+ -- Renders an operand sitting in the left\/accumulator position of a+ -- suffix production (the @a@ that 'customizedParser'\'s @suffixRec@+ -- threads through). Every alternative in @suffix@ parses its+ -- right-hand operand via an unrestricted recursive @a_expr@ call, so+ -- rendering it plainly always reconstructs the same shape on reparse.+ -- The *left* position is different: control only returns to+ -- @suffixRec@'s loop after a __bounded__ production - one that+ -- doesn't itself end in an unrestricted recursive @a_expr@. A value+ -- shaped like 'PlusAExpr'\/'MinusAExpr'\/'NotAExpr'\/+ -- 'PrefixQualOpAExpr'\/'AtTimeZoneAExpr'\/'SymbolicBinOpAExpr'\/+ -- 'AndAExpr'\/'OrAExpr'\/'VerbalExprBinOpAExpr', or a 'ReversableOpAExpr'+ -- whose 'PostgresqlSyntax.Ast.AExprReversableOp' ends in one too, is+ -- unbounded - placed there bare, it would greedily re-absorb whatever+ -- follows on reparse (e.g. rendering @SymbolicBinOpAExpr (NotAExpr x)+ -- op y@ plainly as @NOT x op y@ reparses as @NotAExpr (SymbolicBinOpAExpr+ -- x op y)@). Parenthesizing it, via the existing @'(' a_expr ')'@+ -- 'PostgresqlSyntax.Ast.CExpr' production, keeps rendering correct for+ -- any 'AExpr' value, whether or not the parser itself could have+ -- produced it in that position.+ renderOperand a+ | isBoundedAExprOperand a = toTextBuilder settings a+ | otherwise = TextBuilders.renderInParens (toTextBuilder settings a)+ -- The @d@ operand of a @LIKE@\/@ILIKE@\/@SIMILAR TO@ production, when+ -- it has a trailing @ESCAPE@ clause of its own (@e@). Since @d@ is+ -- parsed via an unrestricted recursive @a_expr@, rendering it bare+ -- when it's unbounded (see 'isBoundedAExprOperand') - e.g. a dangling+ -- operator, or an escape-less 'VerbalExprBinOpAExpr' whose own greedy+ -- @optional escape@ check would otherwise swallow the text meant for+ -- \*this* production's @ESCAPE@ - reparses differently. Bounded shapes+ -- (a column reference, a constant, an already-parenthesized+ -- expression, DEFAULT, …) always terminate cleanly and never need the+ -- extra parens, exactly like @renderOperand@'s left position.+ renderVerbalRhs e d = case e of+ Nothing -> toTextBuilder settings d+ Just _+ | isBoundedAExprOperand d -> toTextBuilder settings d+ | otherwise -> TextBuilders.renderInParens (toTextBuilder settings d)+ -- Distinct from 'PostgresqlSyntax.Ast.AExprReversableOp'\'s own+ -- @toTextBuilder@ (which bakes in the "positive" @IS@\/@BETWEEN@\/@IN@+ -- Parsers.keyword but not the negation) - this one threads the external+ -- @Bool@ (@NOT@) in, mirroring the pre-extraction top-level+ -- @aExprReversableOp@ renderer.+ renderAExprReversableOp a = \case+ NullAExprReversableOp -> bool "IS " "IS NOT " a <> "NULL"+ TrueAExprReversableOp -> bool "IS " "IS NOT " a <> "TRUE"+ FalseAExprReversableOp -> bool "IS " "IS NOT " a <> "FALSE"+ UnknownAExprReversableOp -> bool "IS " "IS NOT " a <> "UNKNOWN"+ DistinctFromAExprReversableOp b -> bool "IS " "IS NOT " a <> "DISTINCT FROM " <> toTextBuilder settings b+ OfAExprReversableOp b -> bool "IS " "IS NOT " a <> "OF " <> TextBuilders.renderInParens (toTextBuilder settings b)+ BetweenAExprReversableOp b c d -> bool "" "NOT " a <> bool "BETWEEN " "BETWEEN ASYMMETRIC " b <> toTextBuilder settings c <> " AND " <> toTextBuilder settings d+ BetweenSymmetricAExprReversableOp b c -> bool "" "NOT " a <> "BETWEEN SYMMETRIC " <> toTextBuilder settings b <> " AND " <> toTextBuilder settings c+ InAExprReversableOp b -> bool "" "NOT " a <> "IN " <> toTextBuilder settings b+ DocumentAExprReversableOp -> bool "IS " "IS NOT " a <> "DOCUMENT"+ parser settings = customizedParser settings (parser settings)++-- |+-- Parameterized over the 'PostgresqlSyntax.Ast.CExpr' parser embedded at the+-- base case - the only axis 'filteredParser' needs to customize (via+-- 'PostgresqlSyntax.Ast.CExpr.customizedParser'). Every other occurrence of+-- @a_expr@\/@b_expr@\/@select_with_parens@ in the grammar below uses the+-- ordinary, unfiltered parsers, exactly as the pre-extraction+-- @customizedAExpr@ did (its @bExpr@\/@selectWithParens@ references were+-- never threaded through the @cExpr@ parameter either).+customizedParser :: Settings -> Parser CExpr -> Parser AExpr+customizedParser settings cExpr = suffixRec base suffix+ where+ aExpr = customizedParser settings cExpr+ base =+ asum+ [ DefaultAExpr <$ Parsers.keyword "default",+ UniqueAExpr <$> (Parsers.keyword "unique" *> Parsers.space1 *> parser settings),+ Parsers.qualOpExpr settings aExpr PrefixQualOpAExpr,+ PlusAExpr <$> Parsers.plusedExpr aExpr,+ MinusAExpr <$> Parsers.minusedExpr aExpr,+ NotAExpr <$> (Parsers.keyword "not" *> Parsers.space1 *> aExpr),+ CExprAExpr <$> cExpr+ ]+ suffix a =+ asum+ [ overlapsSuffix settings a,+ do+ Parsers.space+ b <- Parser.wrapToHead (parser settings)+ Parsers.space+ c <- Parser.wrapToHead (parser settings)+ Parsers.space+ d <- Left <$> Parser.wrapToHead (parser settings) <|> Right <$> Parsers.inParens aExpr+ return (SubqueryAExpr a b c d),+ Parsers.typecastExpr settings a TypecastAExpr,+ CollateAExpr a <$> (Parsers.space1 *> Parsers.keyword "collate" *> Parsers.space1 *> Parser.endHead *> parser settings),+ AtTimeZoneAExpr a <$> (Parsers.space1 *> Parsers.keyphrase "at time zone" *> Parsers.space1 *> Parser.endHead *> aExpr),+ Parsers.symbolicBinOpExpr settings a aExpr SymbolicBinOpAExpr,+ AndAExpr a <$> (Parsers.space1 *> Parsers.keyword "and" *> Parsers.space1 *> Parser.endHead *> aExpr),+ OrAExpr a <$> (Parsers.space1 *> Parsers.keyword "or" *> Parsers.space1 *> Parser.endHead *> aExpr),+ do+ Parsers.space1+ b <- Parsers.trueIfPresent (Parsers.keyword "not" *> Parsers.space1)+ c <- parser settings+ Parsers.space1+ Parser.endHead+ d <- aExpr+ e <- optional (Parsers.space1 *> Parsers.keyword "escape" *> Parsers.space1 *> Parser.endHead *> aExpr)+ return (VerbalExprBinOpAExpr a b c d e),+ do+ Parsers.space1+ Parsers.keyword "is"+ Parsers.space1+ Parser.endHead+ b <- Parsers.trueIfPresent (Parsers.keyword "not" *> Parsers.space1)+ c <-+ asum+ [ NullAExprReversableOp <$ Parsers.keyword "null",+ TrueAExprReversableOp <$ Parsers.keyword "true",+ FalseAExprReversableOp <$ Parsers.keyword "false",+ UnknownAExprReversableOp <$ Parsers.keyword "unknown",+ DistinctFromAExprReversableOp <$> (Parsers.keyword "distinct" *> Parsers.space1 *> Parsers.keyword "from" *> Parsers.space1 *> Parser.endHead *> aExpr),+ OfAExprReversableOp <$> (Parsers.keyword "of" *> Parsers.space1 *> Parser.endHead *> Parsers.inParens (parser settings)),+ DocumentAExprReversableOp <$ Parsers.keyword "document"+ ]+ return (ReversableOpAExpr a b c),+ do+ Parsers.space1+ b <- Parsers.trueIfPresent (Parsers.keyword "not" *> Parsers.space1)+ Parsers.keyword "between"+ Parsers.space1+ Parser.endHead+ c <-+ asum+ [ BetweenSymmetricAExprReversableOp <$ (Parsers.keyword "symmetric" *> Parsers.space1),+ BetweenAExprReversableOp True <$ (Parsers.keyword "asymmetric" *> Parsers.space1),+ pure (BetweenAExprReversableOp False)+ ]+ d <- parser settings+ Parsers.space1+ Parsers.keyword "and"+ Parsers.space1+ e <- aExpr+ return (ReversableOpAExpr a b (c d e)),+ do+ Parsers.space1+ b <- Parsers.trueIfPresent (Parsers.keyword "not" *> Parsers.space1)+ Parsers.keyword "in"+ Parsers.space+ c <- InAExprReversableOp <$> parser settings+ return (ReversableOpAExpr a b c),+ IsnullAExpr a <$ (Parsers.space1 *> Parsers.keyword "isnull"),+ NotnullAExpr a <$ (Parsers.space1 *> Parsers.keyword "notnull")+ ]++-- |+-- The @OVERLAPS@ operator, as a suffix of an already parsed left operand.+-- Reinterprets the already-parsed base 'AExpr' as a 'Row' rather than+-- speculatively parsing a @row@ on top of it - see the original+-- @overlapsSuffix@ in the pre-extraction @PostgresqlSyntax.Parsing@ for the+-- exponential-blowup rationale this avoids.+overlapsSuffix :: Settings -> AExpr -> Parser AExpr+overlapsSuffix settings a = do+ b <- maybe empty pure (aExprRow a)+ Parsers.space1+ Parsers.keyword "overlaps"+ Parser.endHead+ Parsers.space1+ c <- parser settings+ return (OverlapsAExpr b c)+ where+ aExprRow = \case+ CExprAExpr (CExpr.ExplicitRowCExpr x) -> Just (ExplicitRowRow x)+ CExprAExpr (CExpr.ImplicitRowCExpr x) -> Just (ImplicitRowRow x)+ _ -> Nothing++-- |+-- Whether the given 'AExpr' is safe to place in the left\/accumulator+-- position of a suffix production without parenthesizing it - see+-- @renderOperand@ in the 'IsAst' instance above for why that position is+-- special. A shape is bounded when parsing it can never end in an+-- unrestricted recursive @a_expr@ call, i.e. control is guaranteed to+-- return to @suffixRec@'s loop once it's done.+--+-- Note this predicate is purely about operator precedence and+-- associativity: an unbounded shape is one whose own rendering ends in an+-- unrestricted recursive @a_expr@, so placed bare in the left position it+-- would re-absorb the suffix that follows (e.g. rendering+-- @SymbolicBinOpAExpr (NotAExpr x) op y@ plainly as @NOT x op y@ reparses+-- as @NotAExpr (SymbolicBinOpAExpr x op y)@). It is /not/ a general+-- terminator-keyword guard - it isn't a general-purpose mechanism against+-- an expression swallowing a keyword that terminates an enclosing+-- production; the only shape that ever created that hazard by construction+-- was the postfix @a_expr qual_Op@ production, which no longer exists here+-- or in @references/gram.y@ (see gram.y:15985,15987; Postgres removed+-- postfix operators in v14). It does incidentally get reused for that+-- purpose in "PostgresqlSyntax.Ast.TargetEl" (to stop an @OrAExpr@'s right+-- operand from absorbing a following implicit alias) - that's just one+-- call site's use of it, not evidence of general applicability.+isBoundedAExprOperand :: AExpr -> Bool+isBoundedAExprOperand = \case+ PlusAExpr {} -> False+ MinusAExpr {} -> False+ NotAExpr {} -> False+ PrefixQualOpAExpr {} -> False+ AtTimeZoneAExpr {} -> False+ SymbolicBinOpAExpr {} -> False+ AndAExpr {} -> False+ OrAExpr {} -> False+ VerbalExprBinOpAExpr {} -> False+ ReversableOpAExpr _ _ c -> case c of+ DistinctFromAExprReversableOp {} -> False+ BetweenAExprReversableOp {} -> False+ BetweenSymmetricAExprReversableOp {} -> False+ _ -> True+ _ -> True++-- |+-- A generator for the left\/accumulator position of a suffix production+-- (see 'isBoundedAExprOperand'): whatever the given generator produces gets+-- wrapped in an explicit @'(' a_expr ')'@ (via 'CExpr.InParensCExpr') when+-- it wouldn't otherwise be reachable there, so the result always round-trips+-- through 'parser'.+safeAExprOperand :: Qc.Gen AExpr -> Qc.Gen AExpr+safeAExprOperand gen = do+ a <- gen+ pure $+ if isBoundedAExprOperand a+ then a+ else CExprAExpr (CExpr.InParensCExpr a Nothing)++instance Refines SelectWithParens AExpr where+ embed a = CExprAExpr (CExpr.SelectWithParensCExpr a Nothing)+ project = \case+ CExprAExpr (CExpr.SelectWithParensCExpr a Nothing) -> Just a+ _ -> Nothing++-- |+-- Collapses the non-canonical @Right@-wrapping-a-bare-@select_with_parens@+-- shape of 'SubqueryAExpr'\'s final field to the @Left@ shape the parser+-- actually produces for it.+--+-- @a_expr subquery_Op sub_type select_with_parens@ (i.e. @Left@) and+-- @a_expr subquery_Op sub_type '(' a_expr ')'@ (i.e. @Right@) overlap+-- whenever the parenthesized @a_expr@ is itself nothing but a bare,+-- indirection-less @select_with_parens@: both parse @x = ANY ((select 1))@.+-- @suffix@ tries the @select_with_parens@ alternative before the+-- parenthesized @a_expr@ one (see the @d <- Left <$> ... <|> Right <$> ...@+-- line above), so that's always what the parser returns - never @Right@ -+-- making the latter non-canonical for this shape. Both 'arbitrary' and+-- 'shrink' can otherwise construct it, which renders fine but parses back to+-- a different, canonical value and so breaks the roundtrip property.+instance Canonicalizes AExpr where+ canonicalize = \case+ SubqueryAExpr a b c (Right d)+ | Just inner <- project d -> SubqueryAExpr a b c (Left inner)+ other -> other++instance Qc.Arbitrary AExpr where+ shrink = fmap canonicalize . Qc.genericShrink+ arbitrary =+ fmap canonicalize $ Qc.sized $ \n ->+ if n <= 1+ then pure DefaultAExpr+ else+ Qc.oneof+ [ CExprAExpr <$> Qc.arbitrary,+ pure DefaultAExpr,+ TypecastAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary,+ CollateAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary,+ AtTimeZoneAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Gens.downscale Qc.arbitrary,+ PlusAExpr <$> Gens.downscale Qc.arbitrary,+ MinusAExpr <$> Gens.downscale Qc.arbitrary,+ SymbolicBinOpAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ PrefixQualOpAExpr <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ AndAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Gens.downscale Qc.arbitrary,+ OrAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Gens.downscale Qc.arbitrary,+ NotAExpr <$> Gens.downscale Qc.arbitrary,+ ( do+ a <- safeAExprOperand (Gens.downscale Qc.arbitrary)+ b <- Qc.arbitrary+ c <- Qc.arbitrary+ e <- Gens.terminatingMaybe (Gens.downscale Qc.arbitrary)+ -- See @renderVerbalRhs@ in the 'IsAst' instance above:+ -- whenever there's an escape clause, an unbounded @d@ must+ -- be pre-wrapped in parens so the generated value already+ -- matches what parsing the (necessarily parenthesized)+ -- rendering reconstructs - the same rule 'safeAExprOperand'+ -- applies for the left/accumulator position.+ d <- case e of+ Nothing -> Gens.downscale Qc.arbitrary+ Just _ -> safeAExprOperand (Gens.downscale Qc.arbitrary)+ pure (VerbalExprBinOpAExpr a b c d e)+ ),+ ReversableOpAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary <*> Qc.arbitrary,+ IsnullAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary),+ NotnullAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary),+ OverlapsAExpr <$> Qc.arbitrary <*> Qc.arbitrary,+ SubqueryAExpr <$> safeAExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary <*> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ UniqueAExpr <$> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/AExpr.hs-boot view
@@ -0,0 +1,22 @@+module PostgresqlSyntax.Ast.AExpr where++import PostgresqlSyntax.Algebra (IsAst, Refines)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import Test.QuickCheck (Arbitrary)++data AExpr++instance Show AExpr++instance Eq AExpr++instance Ord AExpr++instance Data AExpr++instance IsAst AExpr++instance Arbitrary AExpr++instance Refines SelectWithParens AExpr
+ library-internal/PostgresqlSyntax/Ast/AExprReversableOp.hs view
@@ -0,0 +1,113 @@+module PostgresqlSyntax.Ast.AExprReversableOp where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import {-# SOURCE #-} PostgresqlSyntax.Ast.BExpr (BExpr)+import PostgresqlSyntax.Ast.InExpr+import PostgresqlSyntax.Ast.TypeList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- The part of the following productions that follows @a_expr [NOT]@ /+-- @b_expr [NOT]@ - the leading @IS@\/@NOT@ toggle itself is external to this+-- type (it lives alongside it, e.g. in @ReversableOpAExpr AExpr Bool+-- AExprReversableOp@), mirroring how 'PostgresqlSyntax.Ast.VerbalExprBinOp'+-- keeps @NOT_LA@ external. Only the @IS@\/@BETWEEN@\/@IN@ Parsers.keyword that's+-- intrinsic to each specific alternative (as opposed to the shared negation)+-- is captured here.+--+-- ==== References+-- @+-- | a_expr IS NULL_P+-- | a_expr IS TRUE_P+-- | a_expr IS FALSE_P+-- | a_expr IS UNKNOWN+-- | a_expr IS DISTINCT FROM a_expr+-- | a_expr IS OF '(' type_list ')'+-- | a_expr BETWEEN opt_asymmetric b_expr AND a_expr+-- | a_expr BETWEEN SYMMETRIC b_expr AND a_expr+-- | a_expr IN_P in_expr+-- | a_expr IS DOCUMENT_P+-- @+data AExprReversableOp+ = NullAExprReversableOp+ | TrueAExprReversableOp+ | FalseAExprReversableOp+ | UnknownAExprReversableOp+ | DistinctFromAExprReversableOp AExpr+ | OfAExprReversableOp TypeList+ | BetweenAExprReversableOp Bool BExpr AExpr+ | BetweenSymmetricAExprReversableOp BExpr AExpr+ | InAExprReversableOp InExpr+ | DocumentAExprReversableOp+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AExprReversableOp where+ toTextBuilder settings = \case+ NullAExprReversableOp -> "IS NULL"+ TrueAExprReversableOp -> "IS TRUE"+ FalseAExprReversableOp -> "IS FALSE"+ UnknownAExprReversableOp -> "IS UNKNOWN"+ DistinctFromAExprReversableOp b -> "IS DISTINCT FROM " <> toTextBuilder settings b+ OfAExprReversableOp b -> "IS OF " <> TextBuilders.renderInParens (toTextBuilder settings b)+ BetweenAExprReversableOp b c d -> bool "BETWEEN " "BETWEEN ASYMMETRIC " b <> toTextBuilder settings c <> " AND " <> toTextBuilder settings d+ BetweenSymmetricAExprReversableOp b c -> "BETWEEN SYMMETRIC " <> toTextBuilder settings b <> " AND " <> toTextBuilder settings c+ InAExprReversableOp b -> "IN " <> toTextBuilder settings b+ DocumentAExprReversableOp -> "IS DOCUMENT"+ parser settings =+ asum+ [ Parsers.keyword "is"+ *> Parsers.space1+ *> Parser.endHead+ *> asum+ [ NullAExprReversableOp <$ Parsers.keyword "null",+ TrueAExprReversableOp <$ Parsers.keyword "true",+ FalseAExprReversableOp <$ Parsers.keyword "false",+ UnknownAExprReversableOp <$ Parsers.keyword "unknown",+ DistinctFromAExprReversableOp <$> (Parsers.keyword "distinct" *> Parsers.space1 *> Parsers.keyword "from" *> Parsers.space1 *> Parser.endHead *> parser settings),+ OfAExprReversableOp <$> (Parsers.keyword "of" *> Parsers.space1 *> Parser.endHead *> Parsers.inParens (parser settings)),+ DocumentAExprReversableOp <$ Parsers.keyword "document"+ ],+ do+ Parsers.keyword "between"+ Parsers.space1+ Parser.endHead+ c <-+ asum+ [ BetweenSymmetricAExprReversableOp <$ (Parsers.keyword "symmetric" *> Parsers.space1),+ BetweenAExprReversableOp True <$ (Parsers.keyword "asymmetric" *> Parsers.space1),+ pure (BetweenAExprReversableOp False)+ ]+ d <- parser settings+ Parsers.space1+ Parsers.keyword "and"+ Parsers.space1+ e <- parser settings+ return (c d e),+ InAExprReversableOp <$> (Parsers.keyword "in" *> Parsers.space *> parser settings)+ ]++instance Qc.Arbitrary AExprReversableOp where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then Qc.oneof [pure NullAExprReversableOp, pure TrueAExprReversableOp, pure FalseAExprReversableOp, pure UnknownAExprReversableOp, pure DocumentAExprReversableOp]+ else+ Qc.oneof+ [ pure NullAExprReversableOp,+ pure TrueAExprReversableOp,+ pure FalseAExprReversableOp,+ pure UnknownAExprReversableOp,+ DistinctFromAExprReversableOp <$> Gens.downscale Qc.arbitrary,+ OfAExprReversableOp <$> Gens.downscale Qc.arbitrary,+ BetweenAExprReversableOp <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ BetweenSymmetricAExprReversableOp <$> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ InAExprReversableOp <$> Gens.downscale Qc.arbitrary,+ pure DocumentAExprReversableOp+ ]
+ library-internal/PostgresqlSyntax/Ast/AexprConst.hs view
@@ -0,0 +1,126 @@+module PostgresqlSyntax.Ast.AexprConst where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Bconst+import PostgresqlSyntax.Ast.ConstTypename+import PostgresqlSyntax.Ast.Fconst+import PostgresqlSyntax.Ast.FuncConstArgs+import PostgresqlSyntax.Ast.FuncName+import PostgresqlSyntax.Ast.Iconst+import PostgresqlSyntax.Ast.Interval+import PostgresqlSyntax.Ast.Sconst+import PostgresqlSyntax.Ast.Xconst+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc+import qualified Text.Megaparsec as Megaparsec+import qualified Text.Megaparsec.Char as MegaparsecChar++-- |+-- ==== References+-- @+-- AexprConst:+-- | Iconst+-- | FCONST+-- | Sconst+-- | BCONST+-- | XCONST+-- | func_name Sconst+-- | func_name '(' func_arg_list opt_sort_clause ')' Sconst+-- | ConstTypename Sconst+-- | ConstInterval Sconst opt_interval+-- | ConstInterval '(' Iconst ')' Sconst+-- | TRUE_P+-- | FALSE_P+-- | NULL_P+-- @+data AexprConst+ = IAexprConst Iconst+ | FAexprConst Fconst+ | SAexprConst Sconst+ | BAexprConst Bconst+ | XAexprConst Xconst+ | FuncAexprConst FuncName (Maybe FuncConstArgs) Sconst+ | ConstTypenameAexprConst ConstTypename Sconst+ | StringIntervalAexprConst Sconst (Maybe Interval)+ | IntIntervalAexprConst Iconst Sconst+ | BoolAexprConst Bool+ | NullAexprConst+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AexprConst where+ toTextBuilder settings = \case+ IAexprConst a -> toTextBuilder settings a+ FAexprConst a -> toTextBuilder settings a+ SAexprConst a -> toTextBuilder settings a+ BAexprConst a -> toTextBuilder settings a+ XAexprConst a -> toTextBuilder settings a+ FuncAexprConst a b c -> toTextBuilder settings a <> foldMap (TextBuilders.renderInParens . toTextBuilder settings) b <> " " <> toTextBuilder settings c+ ConstTypenameAexprConst a b -> toTextBuilder settings a <> " " <> toTextBuilder settings b+ StringIntervalAexprConst a b -> "INTERVAL " <> toTextBuilder settings a <> TextBuilders.suffixMaybe (toTextBuilder settings) b+ IntIntervalAexprConst a b -> "INTERVAL " <> TextBuilders.renderInParens (toTextBuilder settings a) <> " " <> toTextBuilder settings b+ BoolAexprConst a -> if a then "TRUE" else "FALSE"+ NullAexprConst -> "NULL"+ parser settings =+ asum+ [ do+ Parsers.keyword "interval"+ Parsers.space1+ Parser.endHead+ a <-+ asum+ [ do+ a <- parser settings+ Parser.endHead+ b <- optional (Parsers.space1 *> parser settings)+ return (StringIntervalAexprConst a b),+ do+ a <- Parsers.inParens (parser settings)+ Parsers.space1+ Parser.endHead+ b <- parser settings+ return (IntIntervalAexprConst a b)+ ]+ return a,+ do+ a <- parser settings+ Parsers.space1+ Parser.endHead+ b <- parser settings+ return (ConstTypenameAexprConst a b),+ BoolAexprConst True <$ Parsers.keyword "true",+ BoolAexprConst False <$ Parsers.keyword "false",+ NullAexprConst <$ Parsers.keyword "null" <* Parser.parse (Megaparsec.notFollowedBy MegaparsecChar.alphaNumChar),+ either IAexprConst FAexprConst <$> (Right <$> parser settings <|> Left <$> parser settings),+ SAexprConst <$> parser settings,+ BAexprConst <$> parser settings,+ XAexprConst <$> parser settings,+ Parser.wrapToHead $ do+ a <- parser settings+ Parsers.space+ b <- Parsers.inParens (parser settings)+ Parsers.space1+ d <- parser settings+ return (FuncAexprConst a (Just b) d),+ FuncAexprConst <$> (Parser.wrapToHead (parser settings) <* Parsers.space1) <*> pure Nothing <*> parser settings+ ]++instance Qc.Arbitrary AexprConst where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ IAexprConst <$> Qc.arbitrary,+ FAexprConst <$> Qc.arbitrary,+ SAexprConst <$> Qc.arbitrary,+ BAexprConst <$> Qc.arbitrary,+ XAexprConst <$> Qc.arbitrary,+ FuncAexprConst <$> Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary <*> Qc.arbitrary,+ ConstTypenameAexprConst <$> Qc.arbitrary <*> Qc.arbitrary,+ StringIntervalAexprConst <$> Qc.arbitrary <*> Qc.arbitrary,+ IntIntervalAexprConst <$> Qc.arbitrary <*> Qc.arbitrary,+ BoolAexprConst <$> Qc.arbitrary,+ pure NullAexprConst+ ]
+ library-internal/PostgresqlSyntax/Ast/AliasClause.hs view
@@ -0,0 +1,51 @@+module PostgresqlSyntax.Ast.AliasClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.NameList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- alias_clause:+-- | AS ColId '(' name_list ')'+-- | AS ColId+-- | ColId '(' name_list ')'+-- | ColId+-- @+--+-- 'PostgresqlSyntax.Ast.ColId' is a bare alias to 'Ident', but its /parser/+-- (kept in "PostgresqlSyntax.Parsing" since @ColId@ 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+-- ColId-flavored element parser is duplicated here (mirroring @colId@'s+-- definition), same as 'PostgresqlSyntax.Ast.NameList'.+data AliasClause = AliasClause Bool Ident (Maybe NameList)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AliasClause where+ toTextBuilder settings (AliasClause a b c) =+ TextBuilders.optLexemes+ [ if a then Just "AS" else Nothing,+ Just (toTextBuilder settings b),+ fmap (TextBuilders.renderInParens . toTextBuilder settings) c+ ]+ parser settings = do+ (as, alias) <- (True,) <$> (Parsers.keyword "as" *> Parsers.space1 *> Parser.endHead *> colIdLikeName) <|> (False,) <$> colIdLikeName+ columnAliases <- optional (Parsers.space1 *> Parsers.inParens (parser settings))+ return (AliasClause as alias columnAliases)+ where+ colIdLikeName =+ Parser.label "identifier" $+ parser settings+ <|> Parsers.keywordNameFromSet UnquotedIdent (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword)++instance Qc.Arbitrary AliasClause where+ shrink = Qc.genericShrink+ arbitrary = AliasClause <$> arbitrary <*> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/AllOp.hs view
@@ -0,0 +1,37 @@+module PostgresqlSyntax.Ast.AllOp where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.MathOp+import PostgresqlSyntax.Ast.Op+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- all_Op:+-- | Op+-- | MathOp+-- @+data AllOp+ = OpAllOp Op+ | MathAllOp MathOp+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AllOp where+ toTextBuilder settings = \case+ OpAllOp a -> toTextBuilder settings a+ MathAllOp a -> toTextBuilder settings a+ parser settings =+ asum+ [ OpAllOp <$> parser settings,+ MathAllOp <$> parser settings+ ]++instance Qc.Arbitrary AllOp where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ OpAllOp <$> Qc.arbitrary,+ MathAllOp <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/AnyName.hs view
@@ -0,0 +1,43 @@+module PostgresqlSyntax.Ast.AnyName where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Attrs+import PostgresqlSyntax.Ast.Ident+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- any_name:+-- | ColId+-- | ColId attrs+-- @+--+-- 'PostgresqlSyntax.Ast.ColId' is a bare alias to 'Ident', but its /parser/+-- (kept in "PostgresqlSyntax.Parsing" since @ColId@ 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+-- ColId-flavored element parser is duplicated here (mirroring @colId@'s+-- definition), same as 'PostgresqlSyntax.Ast.NameList'. (This is also+-- exactly what "PostgresqlSyntax.Parsing"'s @customizedAnyName@ does with+-- its @colId@ argument; that generic helper stays in "PostgresqlSyntax.Parsing"+-- since its other caller, @filteredAnyName@, passes a filtered variant.)+data AnyName = AnyName Ident (Maybe Attrs)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AnyName where+ toTextBuilder settings (AnyName a b) = toTextBuilder settings a <> foldMap (toTextBuilder settings) b+ parser settings = AnyName <$> (Parser.wrapToHead colIdLikeName <* Parser.endHead) <*> optional (Parsers.space *> parser settings)+ where+ colIdLikeName =+ Parser.label "identifier" $+ parser settings+ <|> Parsers.keywordNameFromSet UnquotedIdent (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword)++instance Qc.Arbitrary AnyName where+ shrink = Qc.genericShrink+ arbitrary = AnyName <$> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/AnyOperator.hs view
@@ -0,0 +1,56 @@+module PostgresqlSyntax.Ast.AnyOperator where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AllOp+import PostgresqlSyntax.Ast.Ident+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- any_operator:+-- | all_Op+-- | ColId '.' any_operator+-- @+--+-- 'PostgresqlSyntax.Ast.ColId' is a bare alias to 'Ident', but its /parser/+-- (kept in "PostgresqlSyntax.Parsing" since @ColId@ 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+-- ColId-flavored element parser is duplicated here (mirroring @colId@'s+-- definition), same as 'PostgresqlSyntax.Ast.NameList'.+data AnyOperator+ = AllOpAnyOperator AllOp+ | QualifiedAnyOperator Ident AnyOperator+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst AnyOperator where+ toTextBuilder settings = \case+ AllOpAnyOperator a -> toTextBuilder settings a+ QualifiedAnyOperator a b -> toTextBuilder settings a <> "." <> toTextBuilder settings b+ parser settings =+ asum+ [ AllOpAnyOperator <$> parser settings,+ QualifiedAnyOperator <$> colIdLikeName <*> (Parsers.space *> Parsers.char '.' *> Parsers.space *> parser settings)+ ]+ where+ colIdLikeName =+ Parser.label "identifier" $+ parser settings+ <|> Parsers.keywordNameFromSet UnquotedIdent (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword)++instance Qc.Arbitrary AnyOperator where+ shrink = Qc.genericShrink+ arbitrary = Qc.sized $ \n ->+ if n <= 1+ then AllOpAnyOperator <$> Qc.arbitrary+ else+ Qc.oneof+ [ AllOpAnyOperator <$> Qc.arbitrary,+ QualifiedAnyOperator <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/ArrayBounds.hs view
@@ -0,0 +1,28 @@+module PostgresqlSyntax.Ast.ArrayBounds where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Iconst+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_array_bounds:+-- | opt_array_bounds '[' Iconst ']'+-- | opt_array_bounds '[' ']'+-- | EMPTY+-- @+newtype ArrayBounds = ArrayBounds (NonEmpty (Maybe Iconst))+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ArrayBounds where+ toTextBuilder settings (ArrayBounds a) = TextBuilders.spaceNonEmpty (TextBuilders.renderInBrackets . foldMap (toTextBuilder settings)) a+ parser settings = ArrayBounds <$> Parsers.sep1 Parsers.space (Parsers.inBrackets (optional (parser settings)))++instance Qc.Arbitrary ArrayBounds where+ shrink = Qc.genericShrink+ arbitrary = ArrayBounds <$> Gens.nonEmptyUpTo 3 (Qc.oneof [pure Nothing, Just <$> Qc.arbitrary])
+ library-internal/PostgresqlSyntax/Ast/ArrayExpr.hs view
@@ -0,0 +1,50 @@+module PostgresqlSyntax.Ast.ArrayExpr where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ArrayExprList+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- array_expr:+-- | '[' expr_list ']'+-- | '[' array_expr_list ']'+-- | '[' ']'+-- @+data ArrayExpr+ = ExprListArrayExpr ExprList+ | ArrayExprListArrayExpr ArrayExprList+ | EmptyArrayExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ArrayExpr where+ toTextBuilder settings =+ TextBuilders.renderInBrackets . \case+ ExprListArrayExpr a -> toTextBuilder settings a+ ArrayExprListArrayExpr a -> toTextBuilder settings a+ EmptyArrayExpr -> mempty+ parser settings =+ Parsers.inBrackets $+ asum+ [ ArrayExprListArrayExpr <$> parser settings,+ ExprListArrayExpr <$> parser settings,+ pure EmptyArrayExpr+ ]++instance Qc.Arbitrary ArrayExpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then pure EmptyArrayExpr+ else+ Qc.oneof+ [ ExprListArrayExpr <$> Qc.arbitrary,+ ArrayExprListArrayExpr <$> Qc.arbitrary,+ pure EmptyArrayExpr+ ]
+ library-internal/PostgresqlSyntax/Ast/ArrayExpr.hs-boot view
@@ -0,0 +1,19 @@+module PostgresqlSyntax.Ast.ArrayExpr where++import PostgresqlSyntax.Algebra (IsAst)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data ArrayExpr++instance Show ArrayExpr++instance Eq ArrayExpr++instance Ord ArrayExpr++instance Data ArrayExpr++instance IsAst ArrayExpr++instance Arbitrary ArrayExpr
+ library-internal/PostgresqlSyntax/Ast/ArrayExprList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.ArrayExprList where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.ArrayExpr (ArrayExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- array_expr_list:+-- | array_expr+-- | array_expr_list ',' array_expr+-- @+newtype ArrayExprList = ArrayExprList (NonEmpty ArrayExpr)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ArrayExprList where+ toTextBuilder settings (ArrayExprList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = ArrayExprList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary ArrayExprList where+ shrink = Qc.genericShrink+ arbitrary = ArrayExprList <$> Gens.nonEmptyUpTo 100 (Gens.downscale Qc.arbitrary)
+ library-internal/PostgresqlSyntax/Ast/AscDesc.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.AscDesc where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_asc_desc:+-- | ASC+-- | DESC+-- | EMPTY+-- @+data AscDesc = AscAscDesc | DescAscDesc+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst AscDesc where+ toTextBuilder _settings = \case+ AscAscDesc -> "ASC"+ DescAscDesc -> "DESC"+ parser _settings = Parsers.keyword "asc" $> AscAscDesc <|> Parsers.keyword "desc" $> DescAscDesc++instance Qc.Arbitrary AscDesc where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/Attrs.hs view
@@ -0,0 +1,45 @@+module PostgresqlSyntax.Ast.Attrs where++import qualified Control.Applicative.Combinators.NonEmpty as NonEmpty+import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- attrs:+-- | '.' attr_name+-- | attrs '.' attr_name+-- @+--+-- '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': it additionally accepts the full @keyword@ lexical class as an+-- identifier. Since this module sits below "PostgresqlSyntax.Parsing" (no+-- import cycle allowed), that ColLabel-flavored element parser is+-- duplicated here (mirroring @colLabel@'s definition) rather than reused,+-- to preserve exact parsing behavior. (See 'PostgresqlSyntax.Ast.NameList'+-- for the same pattern applied to @ColId@.)+newtype Attrs = Attrs (NonEmpty Ident)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Attrs where+ toTextBuilder settings (Attrs a) = foldMap (mappend "." . toTextBuilder settings) a+ parser settings = Attrs <$> NonEmpty.some (Parsers.char '.' *> Parser.endHead *> Parsers.space *> colLabelLikeName)+ where+ colLabelLikeName =+ Parser.label "column label" $+ Parsers.keywordNameFromSet UnquotedIdent KeywordSet.keyword+ <|> parser settings++instance Qc.Arbitrary Attrs where+ shrink = Qc.genericShrink+ arbitrary = Attrs <$> Gens.nonEmptyUpTo 9 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/BExpr.hs view
@@ -0,0 +1,161 @@+module PostgresqlSyntax.Ast.BExpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.BExprIsOp+import PostgresqlSyntax.Ast.CExpr (CExpr)+import PostgresqlSyntax.Ast.QualOp+import PostgresqlSyntax.Ast.SymbolicExprBinOp+import PostgresqlSyntax.Ast.Typename+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- b_expr:+-- | c_expr+-- | b_expr TYPECAST Typename+-- | '+' b_expr+-- | '-' b_expr+-- | b_expr '+' b_expr+-- | b_expr '-' b_expr+-- | b_expr '*' b_expr+-- | b_expr '/' b_expr+-- | b_expr '%' b_expr+-- | b_expr '^' b_expr+-- | b_expr '<' b_expr+-- | b_expr '>' b_expr+-- | b_expr '=' b_expr+-- | b_expr LESS_EQUALS b_expr+-- | b_expr GREATER_EQUALS b_expr+-- | b_expr NOT_EQUALS b_expr+-- | b_expr qual_Op b_expr+-- | qual_Op b_expr+-- | b_expr IS DISTINCT FROM b_expr+-- | b_expr IS NOT DISTINCT FROM b_expr+-- | b_expr IS OF '(' type_list ')'+-- | b_expr IS NOT OF '(' type_list ')'+-- | b_expr IS DOCUMENT_P+-- | b_expr IS NOT DOCUMENT_P+-- @+--+-- Unlike 'PostgresqlSyntax.Ast.AExpr', nothing customizes this parser's+-- @c_expr@\/identifier axis externally, so - despite also being a+-- recursion hub - it needs no @customizedParser@\/@filteredParser@ export.+data BExpr+ = CExprBExpr CExpr+ | TypecastBExpr BExpr Typename+ | PlusBExpr BExpr+ | MinusBExpr BExpr+ | SymbolicBinOpBExpr BExpr SymbolicExprBinOp BExpr+ | QualOpBExpr QualOp BExpr+ | IsOpBExpr BExpr Bool BExprIsOp+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst BExpr where+ toTextBuilder settings = \case+ CExprBExpr a -> toTextBuilder settings a+ TypecastBExpr a b -> renderOperand a <> " :: " <> toTextBuilder settings b+ PlusBExpr a -> "+ " <> toTextBuilder settings a+ MinusBExpr a -> "- " <> toTextBuilder settings a+ SymbolicBinOpBExpr a b c -> renderOperand a <> " " <> toTextBuilder settings b <> " " <> toTextBuilder settings c+ QualOpBExpr a b -> toTextBuilder settings a <> " " <> toTextBuilder settings b+ IsOpBExpr a b c -> renderOperand a <> " " <> renderBExprIsOp b c+ where+ -- See 'PostgresqlSyntax.Ast.AExpr'\'s @renderOperand@ for the+ -- rationale - same left\/accumulator-position hazard, mirrored here+ -- for 'BExpr'\'s own (smaller) suffix grammar. Unlike 'AExpr', there's+ -- no @'(' b_expr ')'@ production to fall back on, so parenthesizing+ -- reinterprets the operand as an @a_expr@ via+ -- 'PostgresqlSyntax.Ast.CExpr'\'s @'(' a_expr ')'@ instead - still+ -- valid, semantically-equivalent SQL, just not the same 'BExpr' shape+ -- on reparse (only relevant to hand-constructed values; the+ -- 'Qc.Arbitrary' instance below never generates an operand needing+ -- this fallback).+ renderOperand a+ | isBoundedBExprOperand a = toTextBuilder settings a+ | otherwise = TextBuilders.renderInParens (toTextBuilder settings a)+ renderBExprIsOp a =+ mappend (bool "IS " "IS NOT " a) . \case+ DistinctFromBExprIsOp b -> "DISTINCT FROM " <> toTextBuilder settings b+ OfBExprIsOp b -> "OF " <> TextBuilders.renderInParens (toTextBuilder settings b)+ DocumentBExprIsOp -> "DOCUMENT"+ parser settings = suffixRec base suffix+ where+ bExpr = suffixRec base suffix+ base =+ asum+ [ Parsers.qualOpExpr settings bExpr QualOpBExpr,+ PlusBExpr <$> Parsers.plusedExpr bExpr,+ MinusBExpr <$> Parsers.minusedExpr bExpr,+ CExprBExpr <$> parser settings+ ]+ suffix a =+ asum+ [ Parsers.typecastExpr settings a TypecastBExpr,+ Parsers.symbolicBinOpExpr settings a bExpr SymbolicBinOpBExpr,+ do+ Parsers.space1+ Parsers.keyword "is"+ Parsers.space1+ Parser.endHead+ b <- Parsers.trueIfPresent (Parsers.keyword "not" *> Parsers.space1)+ c <-+ asum+ [ DistinctFromBExprIsOp <$> (Parsers.keyphrase "distinct from" *> Parsers.space1 *> Parser.endHead *> bExpr),+ OfBExprIsOp <$> (Parsers.keyword "of" *> Parsers.space1 *> Parser.endHead *> Parsers.inParens (parser settings)),+ DocumentBExprIsOp <$ Parsers.keyword "document"+ ]+ return (IsOpBExpr a b c)+ ]++-- |+-- Whether the given 'BExpr' is safe to place in the left\/accumulator+-- position of a suffix production without parenthesizing it - see+-- 'IsAst' 'BExpr'\'s @renderOperand@. Mirrors+-- 'PostgresqlSyntax.Ast.AExpr.isBoundedAExprOperand'.+isBoundedBExprOperand :: BExpr -> Bool+isBoundedBExprOperand = \case+ PlusBExpr {} -> False+ MinusBExpr {} -> False+ QualOpBExpr {} -> False+ SymbolicBinOpBExpr {} -> False+ IsOpBExpr _ _ c -> case c of+ DistinctFromBExprIsOp {} -> False+ _ -> True+ _ -> True++-- |+-- A generator for the left\/accumulator position of a suffix production+-- (see 'isBoundedBExprOperand'). Unlike+-- 'PostgresqlSyntax.Ast.AExpr.safeAExprOperand', 'BExpr' has no+-- parenthesizing escape hatch of its own (see @renderOperand@ above), so an+-- unbounded draw is simply replaced by an always-bounded 'CExprBExpr'+-- instead of wrapped.+safeBExprOperand :: Qc.Gen BExpr -> Qc.Gen BExpr+safeBExprOperand gen = do+ a <- gen+ if isBoundedBExprOperand a+ then pure a+ else CExprBExpr <$> Qc.arbitrary++instance Qc.Arbitrary BExpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then CExprBExpr <$> Qc.arbitrary+ else+ Qc.oneof+ [ CExprBExpr <$> Qc.arbitrary,+ TypecastBExpr <$> safeBExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary,+ PlusBExpr <$> Gens.downscale Qc.arbitrary,+ MinusBExpr <$> Gens.downscale Qc.arbitrary,+ SymbolicBinOpBExpr <$> safeBExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ QualOpBExpr <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ IsOpBExpr <$> safeBExprOperand (Gens.downscale Qc.arbitrary) <*> Qc.arbitrary <*> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/BExpr.hs-boot view
@@ -0,0 +1,19 @@+module PostgresqlSyntax.Ast.BExpr where++import PostgresqlSyntax.Algebra (IsAst)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data BExpr++instance Show BExpr++instance Eq BExpr++instance Ord BExpr++instance Data BExpr++instance IsAst BExpr++instance Arbitrary BExpr
+ library-internal/PostgresqlSyntax/Ast/BExprIsOp.hs view
@@ -0,0 +1,55 @@+module PostgresqlSyntax.Ast.BExprIsOp where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.BExpr (BExpr)+import PostgresqlSyntax.Ast.TypeList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- Renders\/parses only the \"positive\" form (@DISTINCT FROM ...@\/@OF+-- (...)@\/@DOCUMENT@), mirroring 'PostgresqlSyntax.Ast.AExprReversableOp':+-- the external @IS@\/@IS NOT@ toggle lives alongside this type in+-- 'PostgresqlSyntax.Ast.BExpr'\'s own @IsOpBExpr BExpr Bool BExprIsOp@+-- constructor and is rendered\/parsed there, not here.+--+-- ==== References+-- @+-- | b_expr IS DISTINCT FROM b_expr+-- | b_expr IS NOT DISTINCT FROM b_expr+-- | b_expr IS OF '(' type_list ')'+-- | b_expr IS NOT OF '(' type_list ')'+-- | b_expr IS DOCUMENT_P+-- | b_expr IS NOT DOCUMENT_P+-- @+data BExprIsOp+ = DistinctFromBExprIsOp BExpr+ | OfBExprIsOp TypeList+ | DocumentBExprIsOp+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst BExprIsOp where+ toTextBuilder settings = \case+ DistinctFromBExprIsOp b -> "DISTINCT FROM " <> toTextBuilder settings b+ OfBExprIsOp b -> "OF " <> TextBuilders.renderInParens (toTextBuilder settings b)+ DocumentBExprIsOp -> "DOCUMENT"+ parser settings =+ asum+ [ DistinctFromBExprIsOp <$> (Parsers.keyphrase "distinct from" *> Parsers.space1 *> Parser.endHead *> parser settings),+ OfBExprIsOp <$> (Parsers.keyword "of" *> Parsers.space1 *> Parser.endHead *> Parsers.inParens (parser settings)),+ DocumentBExprIsOp <$ Parsers.keyword "document"+ ]++instance Qc.Arbitrary BExprIsOp where+ shrink = Qc.genericShrink+ arbitrary =+ Gens.oneofRec+ [ pure DocumentBExprIsOp+ ]+ [ DistinctFromBExprIsOp <$> Qc.arbitrary,+ OfBExprIsOp <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/Bconst.hs view
@@ -0,0 +1,33 @@+module PostgresqlSyntax.Ast.Bconst where++import qualified Data.Text as Text+import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.Shrinks as Shrinks+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc+import qualified TextBuilder++-- |+-- ==== References+-- @+-- BCONST+-- @+newtype Bconst = Bconst Text+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Bconst where+ toTextBuilder _settings (Bconst a) = "B'" <> TextBuilder.text a <> "'"+ parser _settings = Parser.label "bit literal" $ do+ Parsers.string' "b'"+ Parser.endHead+ a <- Parsers.takeWhile1P (Just "0 or 1") (\b -> b == '0' || b == '1')+ Parsers.char '\''+ return (Bconst a)++instance Qc.Arbitrary Bconst where+ shrink (Bconst a) = Bconst <$> Shrinks.nonEmptyText a+ arbitrary = do+ len <- Qc.choose (1, 100)+ Bconst . Text.pack <$> Qc.vectorOf len (Qc.elements "01")
+ library-internal/PostgresqlSyntax/Ast/Bit.hs view
@@ -0,0 +1,43 @@+module PostgresqlSyntax.Ast.Bit where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.OptVarying+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- Bit:+-- | BitWithLength+-- | BitWithoutLength+-- ConstBit:+-- | BitWithLength+-- | BitWithoutLength+-- BitWithLength:+-- | BIT opt_varying '(' expr_list ')'+-- BitWithoutLength:+-- | BIT opt_varying+-- @+data Bit = Bit OptVarying (Maybe ExprList)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Bit where+ toTextBuilder settings (Bit a b) =+ TextBuilders.optLexemes+ [ Just "BIT",+ bool Nothing (Just "VARYING") (coerce a :: Bool),+ fmap (TextBuilders.renderInParens . toTextBuilder settings) b+ ]+ parser settings = do+ Parsers.keyword "bit"+ a <- parser settings+ b <- optional (Parsers.space1 *> Parsers.inParens (parser settings))+ return (Bit a b)++instance Qc.Arbitrary Bit where+ shrink = Qc.genericShrink+ arbitrary = Bit <$> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/CExpr.hs view
@@ -0,0 +1,198 @@+module PostgresqlSyntax.Ast.CExpr+ ( CExpr (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.AexprConst+import PostgresqlSyntax.Ast.ArrayExpr+import PostgresqlSyntax.Ast.CaseExpr+import PostgresqlSyntax.Ast.Columnref+import PostgresqlSyntax.Ast.ExplicitRow+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.FuncExpr+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.ImplicitRow+import PostgresqlSyntax.Ast.Indirection+import qualified PostgresqlSyntax.Ast.Indirection as Indirection+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import qualified PostgresqlSyntax.Extras.NonEmpty as NonEmpty+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (Settings)+import qualified PostgresqlSyntax.Settings as Settings+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- c_expr:+-- | columnref+-- | AexprConst+-- | PARAM opt_indirection+-- | '(' a_expr ')' opt_indirection+-- | case_expr+-- | func_expr+-- | select_with_parens+-- | select_with_parens indirection+-- | EXISTS select_with_parens+-- | ARRAY select_with_parens+-- | ARRAY array_expr+-- | explicit_row+-- | implicit_row+-- | GROUPING '(' expr_list ')'+-- @+data CExpr+ = ColumnrefCExpr Columnref+ | AexprConstCExpr AexprConst+ | ParamCExpr Int (Maybe Indirection)+ | InParensCExpr AExpr (Maybe Indirection)+ | CaseCExpr CaseExpr+ | FuncCExpr FuncExpr+ | SelectWithParensCExpr SelectWithParens (Maybe Indirection)+ | ExistsCExpr SelectWithParens+ | ArrayCExpr (Either SelectWithParens ArrayExpr)+ | ExplicitRowCExpr ExplicitRow+ | ImplicitRowCExpr ImplicitRow+ | GroupingCExpr ExprList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst CExpr where+ toTextBuilder settings = \case+ ColumnrefCExpr a -> toTextBuilder settings a+ AexprConstCExpr a -> toTextBuilder settings a+ ParamCExpr a b -> "$" <> TextBuilder.intDec a <> foldMap (toTextBuilder settings) b+ InParensCExpr a b -> TextBuilders.renderInParens (toTextBuilder settings a) <> foldMap (toTextBuilder settings) b+ CaseCExpr a -> toTextBuilder settings a+ FuncCExpr a -> toTextBuilder settings a+ SelectWithParensCExpr a b -> toTextBuilder settings a <> foldMap (toTextBuilder settings) b+ ExistsCExpr a -> "EXISTS " <> toTextBuilder settings a+ ArrayCExpr a -> "ARRAY " <> either (toTextBuilder settings) (toTextBuilder settings) a+ ExplicitRowCExpr a -> toTextBuilder settings a+ ImplicitRowCExpr a -> toTextBuilder settings a+ GroupingCExpr a -> "GROUPING " <> TextBuilders.renderInParens (toTextBuilder settings a)+ parser settings = customizedParser settings (colId settings)++-- |+-- Parameterized over the @ColId@-like identifier parser used by the plain+-- 'ColumnrefCExpr' alternative - the one place 'PostgresqlSyntax.Ast.AExpr'+-- \'s @filteredParser@ needs to customize. Every other alternative here+-- (parenthesized expressions, @ARRAY@, @EXISTS@, function calls, ...) always+-- uses the ordinary, unfiltered parsers for its nested @a_expr@\/+-- @select_with_parens@\/etc, exactly as the pre-extraction+-- @customizedCExpr@\/@parenthesizedExprCExpr@ did - the filtering doesn't+-- propagate past this one level.+customizedParser :: Settings -> Parser Ident -> Parser CExpr+customizedParser settings colIdParser =+ asum+ [ ParamCExpr <$> (Parsers.char '$' *> Parsers.decimal <* Parser.endHead) <*> optional (Parsers.space *> paramIndirectionParser),+ CaseCExpr <$> parser settings,+ ExplicitRowCExpr <$> parser settings,+ Parsers.inParensWithClause (Parsers.keyword "grouping") (GroupingCExpr . ExprList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)),+ Parsers.keyword "exists" *> Parsers.space *> (ExistsCExpr <$> parser settings),+ do+ Parsers.keyword "array"+ Parsers.space+ asum+ [ ArrayCExpr . Right <$> parser settings,+ ArrayCExpr . Left <$> parser settings+ ],+ do+ a <- Parser.wrapToHead (parser settings)+ Parser.endHead+ b <- optional (Parsers.space *> parser settings)+ return (SelectWithParensCExpr a b),+ parenthesizedExprCExpr,+ AexprConstCExpr <$> Parser.wrapToHead (parser settings),+ FuncCExpr <$> parser settings,+ ColumnrefCExpr <$> customizedColumnref+ ]+ where+ -- The Kronor @.$field@ Haskell selector is confined to this one position:+ -- only the indirection of a @$n@ parameter placeholder may carry it, never+ -- an ordinary column reference's.+ paramIndirectionParser =+ if Settings.resolveHaskellParamFields settings+ then Indirection.hsParser settings+ else parser settings++ customizedColumnref = do+ a <- Parser.wrapToHead colIdParser+ Parser.endHead+ b <- optional (Parsers.space *> parser settings)+ return (Columnref a b)++ -- See 'PostgresqlSyntax.Ast.AExpr'\'s doc on the sibling parser this+ -- replaces (@parenthesizedExprCExpr@\/implicit-row sharing trick) for+ -- why the single @a_expr@ parse is shared between the two endings.+ parenthesizedExprCExpr :: Parser CExpr+ parenthesizedExprCExpr = do+ Parsers.char '('+ Parsers.space+ a <- parser settings+ Parsers.space+ asum+ [ do+ Parsers.char ','+ Parser.endHead+ Parsers.space+ b <- Parsers.sep1 Parsers.commaSeparator (parser settings)+ Parsers.space+ Parsers.char ')'+ return $ ImplicitRowCExpr $ case NonEmpty.consAndUnsnoc a b of+ (c, d) -> ImplicitRow (ExprList c) d,+ do+ Parsers.char ')'+ Parser.endHead+ b <- optional (Parsers.space *> parser settings)+ return (InParensCExpr a b)+ ]++instance Qc.Arbitrary CExpr where+ shrink = fmap canonicalize . Qc.genericShrink+ arbitrary =+ fmap canonicalize $ Qc.sized $ \n ->+ if n <= 1+ then ColumnrefCExpr <$> Qc.arbitrary+ else+ Qc.oneof+ [ ColumnrefCExpr <$> Qc.arbitrary,+ AexprConstCExpr <$> Qc.arbitrary,+ ParamCExpr <$> Qc.choose (1, 19) <*> Qc.arbitrary,+ InParensCExpr <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ CaseCExpr <$> Qc.arbitrary,+ FuncCExpr <$> Qc.arbitrary,+ SelectWithParensCExpr <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ ExistsCExpr <$> Gens.downscale Qc.arbitrary,+ ArrayCExpr <$> Gens.downscale Qc.arbitrary,+ ExplicitRowCExpr <$> Qc.arbitrary,+ ImplicitRowCExpr <$> Qc.arbitrary,+ GroupingCExpr <$> Qc.arbitrary+ ]++-- |+-- Collapses the non-canonical @InParensCExpr@-wrapping-a-@SelectWithParensCExpr@+-- shape to the @SelectWithParensCExpr@\/@WithParensSelectWithParens@ shape the+-- parser actually produces for it.+--+-- @'(' a_expr ')' opt_indirection@ (i.e. 'InParensCExpr') and+-- @select_with_parens@ (i.e. 'SelectWithParensCExpr') overlap whenever the+-- inner @a_expr@ is itself nothing but a bare, indirection-less+-- @select_with_parens@: both parse @((select 1))@. 'customizedParser' tries+-- the @select_with_parens@ alternative before @parenthesizedExprCExpr@, so+-- that's always what the parser returns - never 'InParensCExpr' - making+-- the latter non-canonical for this shape. Both 'arbitrary' and 'shrink' can+-- otherwise construct it (shrinking the outer indirection to @Nothing@ is+-- exactly how it arises), which renders fine but parses back to a+-- different, canonical value and so breaks the roundtrip property.+instance Canonicalizes CExpr where+ canonicalize = \case+ InParensCExpr a outerIndirection+ | Just inner <- project @SelectWithParens a ->+ SelectWithParensCExpr (embed @SelectWithParens inner) outerIndirection+ other -> other
+ library-internal/PostgresqlSyntax/Ast/CallStmt.hs view
@@ -0,0 +1,22 @@+module PostgresqlSyntax.Ast.CallStmt where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncApplication+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++newtype CallStmt+ = CallStmt FuncApplication+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst CallStmt where+ toTextBuilder settings (CallStmt a) = "CALL " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "call"+ Parsers.space1+ CallStmt <$> parser settings++instance Qc.Arbitrary CallStmt where+ shrink = Qc.genericShrink+ arbitrary = CallStmt <$> arbitrary
+ library-internal/PostgresqlSyntax/Ast/CaseExpr.hs view
@@ -0,0 +1,54 @@+module PostgresqlSyntax.Ast.CaseExpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.WhenClauseList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- case_expr:+-- | CASE case_arg when_clause_list case_default END_P+-- @+data CaseExpr = CaseExpr (Maybe AExpr) WhenClauseList (Maybe AExpr)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst CaseExpr where+ toTextBuilder settings (CaseExpr a b c) =+ TextBuilders.optLexemes+ [ Just "CASE",+ fmap (toTextBuilder settings) a,+ Just (toTextBuilder settings b),+ fmap caseDefault c,+ Just "END"+ ]+ where+ caseDefault d = "ELSE " <> toTextBuilder settings d+ parser settings = Parser.label "case expression" $ do+ Parsers.keyword "case"+ Parsers.space1+ Parser.endHead+ arg <- optional (parser settings <* Parsers.space1)+ whenClauses <- parser settings+ Parsers.space1+ default' <- optional elseClause+ Parsers.keyword "end"+ pure (CaseExpr arg whenClauses default')+ where+ elseClause = do+ Parsers.keyword "else"+ Parsers.space1+ Parser.endHead+ a <- parser settings+ Parsers.space1+ return a++instance Qc.Arbitrary CaseExpr where+ shrink = Qc.genericShrink+ arbitrary = CaseExpr <$> Gens.terminatingMaybe (Gens.downscale arbitrary) <*> arbitrary <*> Gens.terminatingMaybe (Gens.downscale arbitrary)
+ library-internal/PostgresqlSyntax/Ast/Character.hs view
@@ -0,0 +1,57 @@+module PostgresqlSyntax.Ast.Character where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.OptVarying+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- character:+-- | CHARACTER opt_varying+-- | CHAR_P opt_varying+-- | VARCHAR+-- | NATIONAL CHARACTER opt_varying+-- | NATIONAL CHAR_P opt_varying+-- | NCHAR opt_varying+-- @+data Character+ = CharacterCharacter OptVarying+ | CharCharacter OptVarying+ | VarcharCharacter+ | NationalCharacterCharacter OptVarying+ | NationalCharCharacter OptVarying+ | NcharCharacter OptVarying+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Character where+ toTextBuilder _settings = \case+ CharacterCharacter a -> "CHARACTER" <> bool "" " VARYING" (coerce a :: Bool)+ CharCharacter a -> "CHAR" <> bool "" " VARYING" (coerce a :: Bool)+ VarcharCharacter -> "VARCHAR"+ NationalCharacterCharacter a -> "NATIONAL CHARACTER" <> bool "" " VARYING" (coerce a :: Bool)+ NationalCharCharacter a -> "NATIONAL CHAR" <> bool "" " VARYING" (coerce a :: Bool)+ NcharCharacter a -> "NCHAR" <> bool "" " VARYING" (coerce a :: Bool)+ parser settings =+ asum+ [ CharacterCharacter <$> (Parsers.keyword "character" *> parser settings),+ CharCharacter <$> (Parsers.keyword "char" *> parser settings),+ VarcharCharacter <$ Parsers.keyword "varchar",+ NationalCharacterCharacter <$> (Parsers.keyphrase "national character" *> parser settings),+ NationalCharCharacter <$> (Parsers.keyphrase "national char" *> parser settings),+ NcharCharacter <$> (Parsers.keyword "nchar" *> parser settings)+ ]++instance Qc.Arbitrary Character where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ CharacterCharacter <$> Qc.arbitrary,+ CharCharacter <$> Qc.arbitrary,+ pure VarcharCharacter,+ NationalCharacterCharacter <$> Qc.arbitrary,+ NationalCharCharacter <$> Qc.arbitrary,+ NcharCharacter <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/Columnref.hs view
@@ -0,0 +1,32 @@+module PostgresqlSyntax.Ast.Columnref where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident (Ident, colId)+import PostgresqlSyntax.Ast.Indirection+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- columnref:+-- | ColId+-- | ColId indirection+-- @+data Columnref = Columnref Ident (Maybe Indirection)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Columnref where+ toTextBuilder settings (Columnref a b) = toTextBuilder settings a <> foldMap (toTextBuilder settings) b+ parser settings = do+ a <- Parser.wrapToHead (colId settings)+ Parser.endHead+ b <- optional (Parsers.space *> parser settings)+ return (Columnref a b)++instance Qc.Arbitrary Columnref where+ shrink = Qc.genericShrink+ arbitrary = Columnref <$> arbitrary <*> Gens.terminatingMaybe arbitrary
+ library-internal/PostgresqlSyntax/Ast/CommonTableExpr.hs view
@@ -0,0 +1,59 @@+module PostgresqlSyntax.Ast.CommonTableExpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.PreparableStmt+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- common_table_expr:+-- | name opt_name_list AS opt_materialized '(' PreparableStmt ')'+-- opt_materialized:+-- | MATERIALIZED+-- | NOT MATERIALIZED+-- | EMPTY+-- @+data CommonTableExpr = CommonTableExpr Ident (Maybe (NonEmpty Ident)) (Maybe Bool) PreparableStmt+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst CommonTableExpr where+ toTextBuilder settings (CommonTableExpr a b c d) =+ TextBuilders.optLexemes+ [ Just (toTextBuilder settings a),+ fmap (TextBuilders.renderInParens . TextBuilders.commaNonEmpty (toTextBuilder settings)) b,+ Just "AS",+ fmap materialization c,+ Just (TextBuilders.renderInParens (toTextBuilder settings d))+ ]+ where+ materialization = bool "NOT MATERIALIZED" "MATERIALIZED"+ parser settings = Parser.label "common table expression" $ do+ name <- colId settings <* Parsers.space <* Parser.endHead+ nameList <- optional (Parsers.inParens (Parsers.sep1 Parsers.commaSeparator (colId settings)) <* Parsers.space1)+ Parsers.keyword "as"+ Parsers.space1+ materialized <- optional (materialized <* Parsers.space1)+ stmt <- Parsers.inParens (parser settings)+ return (CommonTableExpr name nameList materialized stmt)+ where+ materialized =+ True+ <$ Parsers.keyword "materialized"+ <|> False+ <$ Parsers.keyphrase "not materialized"++instance Qc.Arbitrary CommonTableExpr where+ shrink = Qc.genericShrink+ arbitrary =+ CommonTableExpr+ <$> Qc.arbitrary+ <*> Gens.terminatingMaybe (Gens.nonEmptyUpTo 6 Qc.arbitrary)+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/ConfExpr.hs view
@@ -0,0 +1,51 @@+module PostgresqlSyntax.Ast.ConfExpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.IndexParams+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_conf_expr:+-- | '(' index_params ')' where_clause+-- | ON CONSTRAINT name+-- | EMPTY+-- @+--+-- @where_clause@ is inlined here as a bare 'PostgresqlSyntax.Ast.AExpr'+-- rather than going through 'PostgresqlSyntax.Ast.WhereClause'.+-- @name@ is a bare alias to 'PostgresqlSyntax.Ast.Ident'.+data ConfExpr+ = WhereConfExpr IndexParams (Maybe AExpr)+ | ConstraintConfExpr Ident+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ConfExpr where+ toTextBuilder settings = \case+ WhereConfExpr a b -> TextBuilders.renderInParens (toTextBuilder settings a) <> TextBuilders.suffixMaybe whereClause b+ ConstraintConfExpr a -> "ON CONSTRAINT " <> toTextBuilder settings a+ where+ whereClause a = "WHERE " <> toTextBuilder settings a+ parser settings =+ asum+ [ WhereConfExpr <$> Parsers.inParens (parser settings) <*> optional (Parsers.space *> whereClause),+ ConstraintConfExpr <$> (Parsers.keyword "on" *> Parsers.space1 *> Parsers.keyword "constraint" *> Parsers.space1 *> Parser.endHead *> colId settings)+ ]+ where+ whereClause = Parsers.keyword "where" *> Parsers.space1 *> Parser.endHead *> parser settings++instance Qc.Arbitrary ConfExpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ WhereConfExpr <$> Qc.arbitrary <*> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary),+ ConstraintConfExpr <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/ConstCharacter.hs view
@@ -0,0 +1,41 @@+module PostgresqlSyntax.Ast.ConstCharacter where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Character+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- ConstCharacter:+-- | CharacterWithLength+-- | CharacterWithoutLength+-- CharacterWithLength:+-- | character '(' Iconst ')'+-- CharacterWithoutLength:+-- | character+-- @+data ConstCharacter = ConstCharacter Character (Maybe Int64)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ConstCharacter where+ toTextBuilder settings (ConstCharacter a b) = toTextBuilder settings a <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . TextBuilder.int64Dec) b+ parser settings = ConstCharacter <$> (parser settings <* Parser.endHead) <*> optional (Parsers.space *> Parsers.inParens Parsers.decimal)++instance Qc.Arbitrary ConstCharacter where+ shrink = Qc.genericShrink++ -- The length here is parsed via 'Parsers.decimal' (unsigned), so it must+ -- never be negative - mirroring 'PostgresqlSyntax.Ast.IntervalSecond'\'s+ -- own @nonNegative@.+ arbitrary = ConstCharacter <$> arbitrary <*> Qc.oneof [pure Nothing, Just <$> nonNegativeInt64]+ where+ nonNegativeInt64 = Qc.sized (\n -> Qc.choose (0, cap n))+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/ConstDatetime.hs view
@@ -0,0 +1,69 @@+module PostgresqlSyntax.Ast.ConstDatetime where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Timezone+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- ConstDatetime:+-- | TIMESTAMP '(' Iconst ')' opt_timezone+-- | TIMESTAMP opt_timezone+-- | TIME '(' Iconst ')' opt_timezone+-- | TIME opt_timezone+-- @+data ConstDatetime+ = TimestampConstDatetime (Maybe Int64) (Maybe Timezone)+ | TimeConstDatetime (Maybe Int64) (Maybe Timezone)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ConstDatetime where+ toTextBuilder settings = \case+ TimestampConstDatetime a b ->+ TextBuilders.optLexemes+ [ Just "TIMESTAMP",+ fmap (TextBuilders.renderInParens . TextBuilder.int64Dec) a,+ fmap (toTextBuilder settings) b+ ]+ TimeConstDatetime a b ->+ TextBuilders.optLexemes+ [ Just "TIME",+ fmap (TextBuilders.renderInParens . TextBuilder.int64Dec) a,+ fmap (toTextBuilder settings) b+ ]+ parser settings =+ asum+ [ do+ Parsers.keyword "timestamp"+ a <- optional (Parsers.space1 *> Parsers.inParens Parsers.decimal)+ b <- optional (Parsers.space1 *> parser settings)+ return (TimestampConstDatetime a b),+ do+ Parsers.keyword "time"+ a <- optional (Parsers.space1 *> Parsers.inParens Parsers.decimal)+ b <- optional (Parsers.space1 *> parser settings)+ return (TimeConstDatetime a b)+ ]++instance Qc.Arbitrary ConstDatetime where+ shrink = Qc.genericShrink++ -- The precision here is parsed via 'Parsers.decimal' (unsigned), so it+ -- must never be negative - mirroring+ -- 'PostgresqlSyntax.Ast.IntervalSecond'\'s own @nonNegative@.+ arbitrary =+ Qc.oneof+ [ TimestampConstDatetime <$> nonNegativeMaybeInt64 <*> Qc.arbitrary,+ TimeConstDatetime <$> nonNegativeMaybeInt64 <*> Qc.arbitrary+ ]+ where+ nonNegativeMaybeInt64 = Qc.oneof [pure Nothing, Just <$> nonNegativeInt64]+ nonNegativeInt64 = Qc.sized (\n -> Qc.choose (0, cap n))+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/ConstTypename.hs view
@@ -0,0 +1,49 @@+module PostgresqlSyntax.Ast.ConstTypename where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Bit (Bit)+import PostgresqlSyntax.Ast.ConstCharacter+import PostgresqlSyntax.Ast.ConstDatetime+import PostgresqlSyntax.Ast.Numeric+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- ConstTypename:+-- | Numeric+-- | ConstBit+-- | ConstCharacter+-- | ConstDatetime+-- @+data ConstTypename+ = NumericConstTypename Numeric+ | ConstBitConstTypename Bit+ | ConstCharacterConstTypename ConstCharacter+ | ConstDatetimeConstTypename ConstDatetime+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ConstTypename where+ toTextBuilder settings = \case+ NumericConstTypename a -> toTextBuilder settings a+ ConstBitConstTypename a -> toTextBuilder settings a+ ConstCharacterConstTypename a -> toTextBuilder settings a+ ConstDatetimeConstTypename a -> toTextBuilder settings a+ parser settings =+ asum+ [ NumericConstTypename <$> parser settings,+ ConstBitConstTypename <$> parser settings,+ ConstCharacterConstTypename <$> parser settings,+ ConstDatetimeConstTypename <$> parser settings+ ]++instance Qc.Arbitrary ConstTypename where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ NumericConstTypename <$> Qc.arbitrary,+ ConstBitConstTypename <$> Qc.arbitrary,+ ConstCharacterConstTypename <$> Qc.arbitrary,+ ConstDatetimeConstTypename <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/DeleteStmt.hs view
@@ -0,0 +1,55 @@+module PostgresqlSyntax.Ast.DeleteStmt where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.RelationExprOptAlias (RelationExprOptAlias)+import PostgresqlSyntax.Ast.ReturningClause+import PostgresqlSyntax.Ast.UsingClause+import PostgresqlSyntax.Ast.WhereOrCurrentClause+import {-# SOURCE #-} PostgresqlSyntax.Ast.WithClause (WithClause)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- DeleteStmt:+-- | opt_with_clause DELETE_P FROM relation_expr_opt_alias+-- using_clause where_or_current_clause returning_clause+-- @+data DeleteStmt = DeleteStmt (Maybe WithClause) RelationExprOptAlias (Maybe UsingClause) (Maybe WhereOrCurrentClause) (Maybe ReturningClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst DeleteStmt where+ toTextBuilder settings (DeleteStmt a b c d e) =+ TextBuilders.prefixMaybe (toTextBuilder settings) a+ <> "DELETE FROM "+ <> toTextBuilder settings b+ <> TextBuilders.suffixMaybe (toTextBuilder settings) c+ <> TextBuilders.suffixMaybe (toTextBuilder settings) d+ <> TextBuilders.suffixMaybe (toTextBuilder settings) e+ parser settings = do+ a <- optional (Parser.wrapToHead (parser settings) <* Parsers.space1)+ Parsers.keyword "delete"+ Parsers.space1+ Parser.endHead+ Parsers.keyword "from"+ Parsers.space1+ b <- parser settings+ c <- optional (Parsers.space1 *> parser settings)+ d <- optional (Parsers.space1 *> parser settings)+ e <- optional (Parsers.space1 *> parser settings)+ return (DeleteStmt a b c d e)++instance Qc.Arbitrary DeleteStmt where+ shrink = Qc.genericShrink+ arbitrary =+ DeleteStmt+ <$> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary)+ <*> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/ExplicitRow.hs view
@@ -0,0 +1,41 @@+module PostgresqlSyntax.Ast.ExplicitRow where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- explicit_row:+-- | ROW '(' expr_list ')'+-- | ROW '(' ')'+-- @+data ExplicitRow+ = EmptyExplicitRow+ | ExprListExplicitRow ExprList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ExplicitRow where+ toTextBuilder settings a =+ "ROW "+ <> TextBuilders.renderInParens+ ( case a of+ EmptyExplicitRow -> mempty+ ExprListExplicitRow b -> toTextBuilder settings b+ )+ parser settings =+ Parsers.keyword "row"+ *> Parsers.space+ *> Parsers.inParens (maybe EmptyExplicitRow ExprListExplicitRow <$> optional (parser settings))++instance Qc.Arbitrary ExplicitRow where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ pure EmptyExplicitRow,+ ExprListExplicitRow <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/ExprList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.ExprList where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- expr_list:+-- | a_expr+-- | expr_list ',' a_expr+-- @+newtype ExprList = ExprList (NonEmpty AExpr)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ExprList where+ toTextBuilder settings (ExprList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = ExprList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary ExprList where+ shrink = Qc.genericShrink+ arbitrary = ExprList <$> Gens.nonEmptyUpTo 6 (Gens.downscale Qc.arbitrary)
+ library-internal/PostgresqlSyntax/Ast/ExtractArg.hs view
@@ -0,0 +1,68 @@+module PostgresqlSyntax.Ast.ExtractArg where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Sconst+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- extract_arg:+-- | IDENT+-- | YEAR_P+-- | MONTH_P+-- | DAY_P+-- | HOUR_P+-- | MINUTE_P+-- | SECOND_P+-- | Sconst+-- @+data ExtractArg+ = IdentExtractArg Ident+ | YearExtractArg+ | MonthExtractArg+ | DayExtractArg+ | HourExtractArg+ | MinuteExtractArg+ | SecondExtractArg+ | SconstExtractArg Sconst+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ExtractArg where+ toTextBuilder settings = \case+ IdentExtractArg a -> toTextBuilder settings a+ YearExtractArg -> "YEAR"+ MonthExtractArg -> "MONTH"+ DayExtractArg -> "DAY"+ HourExtractArg -> "HOUR"+ MinuteExtractArg -> "MINUTE"+ SecondExtractArg -> "SECOND"+ SconstExtractArg a -> toTextBuilder settings a+ parser settings =+ asum+ [ YearExtractArg <$ Parsers.keyword "year",+ MonthExtractArg <$ Parsers.keyword "month",+ DayExtractArg <$ Parsers.keyword "day",+ HourExtractArg <$ Parsers.keyword "hour",+ MinuteExtractArg <$ Parsers.keyword "minute",+ SecondExtractArg <$ Parsers.keyword "second",+ SconstExtractArg <$> parser settings,+ IdentExtractArg <$> parser settings+ ]++instance Qc.Arbitrary ExtractArg where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ IdentExtractArg <$> Qc.arbitrary,+ pure YearExtractArg,+ pure MonthExtractArg,+ pure DayExtractArg,+ pure HourExtractArg,+ pure MinuteExtractArg,+ pure SecondExtractArg,+ SconstExtractArg <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/ExtractList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.ExtractList where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExtractArg+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- extract_list:+-- | extract_arg FROM a_expr+-- | EMPTY+-- @+data ExtractList = ExtractList ExtractArg AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ExtractList where+ toTextBuilder settings (ExtractList a b) = toTextBuilder settings a <> " FROM " <> toTextBuilder settings b+ parser settings = ExtractList <$> parser settings <*> (Parsers.space1 *> Parsers.keyword "from" *> Parsers.space1 *> parser settings)++instance Qc.Arbitrary ExtractList where+ shrink = Qc.genericShrink+ arbitrary = ExtractList <$> arbitrary <*> Gens.downscale arbitrary
+ library-internal/PostgresqlSyntax/Ast/Fconst.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.Fconst where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- FCONST+-- @+newtype Fconst = Fconst Double+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Fconst where+ toTextBuilder _settings (Fconst a) = TextBuilder.doubleDec a+ parser _settings = Fconst <$> Parsers.float++instance Qc.Arbitrary Fconst where+ shrink = Qc.genericShrink++ -- Parsed via 'Parsers.float' (unsigned - the sign, when present, is a+ -- separate unary @AExpr@\/@BExpr@ operator applied outside this type), so+ -- it must never be negative, mirroring+ -- 'PostgresqlSyntax.Ast.IntervalSecond'\'s own @nonNegative@.+ arbitrary =+ Fconst . abs+ <$> (Qc.arbitrary `Qc.suchThat` (\a -> fromIntegral (round a :: Int) /= a))
+ library-internal/PostgresqlSyntax/Ast/ForLockingClause.hs view
@@ -0,0 +1,41 @@+module PostgresqlSyntax.Ast.ForLockingClause where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ForLockingItem+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- for_locking_clause:+-- | for_locking_items+-- | FOR READ ONLY+-- for_locking_items:+-- | for_locking_item+-- | for_locking_items for_locking_item+-- @+data ForLockingClause+ = ItemsForLockingClause (NonEmpty ForLockingItem)+ | ReadOnlyForLockingClause+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ForLockingClause where+ toTextBuilder settings = \case+ ItemsForLockingClause a -> TextBuilders.spaceNonEmpty (toTextBuilder settings) a+ ReadOnlyForLockingClause -> "FOR READ ONLY"+ parser settings = readOnly <|> items+ where+ readOnly = ReadOnlyForLockingClause <$ Parsers.keyphrase "for read only"+ items = ItemsForLockingClause <$> Parsers.sep1 Parsers.space1 (parser settings)++instance Qc.Arbitrary ForLockingClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ItemsForLockingClause <$> Gens.nonEmptyUpTo 7 Qc.arbitrary,+ pure ReadOnlyForLockingClause+ ]
+ library-internal/PostgresqlSyntax/Ast/ForLockingItem.hs view
@@ -0,0 +1,49 @@+module PostgresqlSyntax.Ast.ForLockingItem where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ForLockingStrength+import PostgresqlSyntax.Ast.QualifiedName+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- for_locking_item:+-- | for_locking_strength locked_rels_list opt_nowait_or_skip+-- locked_rels_list:+-- | OF qualified_name_list+-- | EMPTY+-- opt_nowait_or_skip:+-- | NOWAIT+-- | SKIP LOCKED+-- | EMPTY+-- @+data ForLockingItem = ForLockingItem ForLockingStrength (Maybe (NonEmpty QualifiedName)) (Maybe Bool)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ForLockingItem where+ toTextBuilder settings (ForLockingItem a b c) =+ TextBuilders.optLexemes+ [ Just (toTextBuilder settings a),+ fmap lockedRelsList b,+ fmap nowaitOrSkip c+ ]+ where+ lockedRelsList a' = "OF " <> TextBuilders.commaNonEmpty (toTextBuilder settings) a'+ nowaitOrSkip = bool "NOWAIT" "SKIP LOCKED"+ parser settings = do+ strength <- parser settings+ rels <- optional $ Parsers.space1 *> Parsers.keyword "of" *> Parsers.space1 *> Parser.endHead *> Parsers.sep1 Parsers.commaSeparator (parser settings)+ nowaitOrSkip <- optional (Parsers.space1 *> nowaitOrSkip)+ return (ForLockingItem strength rels nowaitOrSkip)+ where+ nowaitOrSkip = False <$ Parsers.keyword "nowait" <|> True <$ Parsers.keyphrase "skip locked"++instance Qc.Arbitrary ForLockingItem where+ shrink = Qc.genericShrink+ arbitrary = ForLockingItem <$> arbitrary <*> Gens.terminatingMaybe arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/ForLockingStrength.hs view
@@ -0,0 +1,48 @@+module PostgresqlSyntax.Ast.ForLockingStrength where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- for_locking_strength:+-- | FOR UPDATE+-- | FOR NO KEY UPDATE+-- | FOR SHARE+-- | FOR KEY SHARE+-- @+data ForLockingStrength+ = UpdateForLockingStrength+ | NoKeyUpdateForLockingStrength+ | ShareForLockingStrength+ | KeyForLockingStrength+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ForLockingStrength where+ toTextBuilder _settings = \case+ UpdateForLockingStrength -> "FOR UPDATE"+ NoKeyUpdateForLockingStrength -> "FOR NO KEY UPDATE"+ ShareForLockingStrength -> "FOR SHARE"+ KeyForLockingStrength -> "FOR KEY SHARE"+ parser _settings =+ UpdateForLockingStrength+ <$ Parsers.keyphrase "for update"+ <|> NoKeyUpdateForLockingStrength+ <$ Parsers.keyphrase "for no key update"+ <|> ShareForLockingStrength+ <$ Parsers.keyphrase "for share"+ <|> KeyForLockingStrength+ <$ Parsers.keyphrase "for key share"++instance Qc.Arbitrary ForLockingStrength where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.elements+ [ UpdateForLockingStrength,+ NoKeyUpdateForLockingStrength,+ ShareForLockingStrength,+ KeyForLockingStrength+ ]
+ library-internal/PostgresqlSyntax/Ast/FrameBound.hs view
@@ -0,0 +1,56 @@+module PostgresqlSyntax.Ast.FrameBound where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- frame_bound:+-- | UNBOUNDED PRECEDING+-- | UNBOUNDED FOLLOWING+-- | CURRENT_P ROW+-- | a_expr PRECEDING+-- | a_expr FOLLOWING+-- @+data FrameBound+ = UnboundedPrecedingFrameBound+ | UnboundedFollowingFrameBound+ | CurrentRowFrameBound+ | PrecedingFrameBound AExpr+ | FollowingFrameBound AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FrameBound where+ toTextBuilder settings = \case+ UnboundedPrecedingFrameBound -> "UNBOUNDED PRECEDING"+ UnboundedFollowingFrameBound -> "UNBOUNDED FOLLOWING"+ CurrentRowFrameBound -> "CURRENT ROW"+ PrecedingFrameBound a -> toTextBuilder settings a <> " PRECEDING"+ FollowingFrameBound a -> toTextBuilder settings a <> " FOLLOWING"+ parser settings =+ UnboundedPrecedingFrameBound+ <$ Parsers.keyphrase "unbounded preceding"+ <|> UnboundedFollowingFrameBound+ <$ Parsers.keyphrase "unbounded following"+ <|> CurrentRowFrameBound+ <$ Parsers.keyphrase "current row"+ <|> do+ a <- parser settings :: Parser AExpr+ Parsers.space1+ PrecedingFrameBound a <$ Parsers.keyword "preceding" <|> FollowingFrameBound a <$ Parsers.keyword "following"++instance Qc.Arbitrary FrameBound where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ pure UnboundedPrecedingFrameBound,+ pure UnboundedFollowingFrameBound,+ pure CurrentRowFrameBound,+ PrecedingFrameBound <$> Gens.downscale Qc.arbitrary,+ FollowingFrameBound <$> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FrameClause.hs view
@@ -0,0 +1,40 @@+module PostgresqlSyntax.Ast.FrameClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FrameClauseMode+import PostgresqlSyntax.Ast.FrameExtent+import PostgresqlSyntax.Ast.WindowExclusionClause+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_frame_clause:+-- | RANGE frame_extent opt_window_exclusion_clause+-- | ROWS frame_extent opt_window_exclusion_clause+-- | GROUPS frame_extent opt_window_exclusion_clause+-- | EMPTY+-- @+data FrameClause = FrameClause FrameClauseMode FrameExtent (Maybe WindowExclusionClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FrameClause where+ toTextBuilder settings (FrameClause a b c) =+ TextBuilders.optLexemes+ [ Just (toTextBuilder settings a),+ Just (toTextBuilder settings b),+ fmap (toTextBuilder settings) c+ ]+ parser settings = do+ a <- parser settings <* Parsers.space1 <* Parser.endHead+ b <- parser settings+ c <- optional (Parsers.space1 *> parser settings)+ return (FrameClause a b c)++instance Qc.Arbitrary FrameClause where+ shrink = Qc.genericShrink+ arbitrary = FrameClause <$> arbitrary <*> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/FrameClauseMode.hs view
@@ -0,0 +1,34 @@+module PostgresqlSyntax.Ast.FrameClauseMode where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_frame_clause:+-- | RANGE frame_extent opt_window_exclusion_clause+-- | ROWS frame_extent opt_window_exclusion_clause+-- | GROUPS frame_extent opt_window_exclusion_clause+-- | EMPTY+-- @+data FrameClauseMode = RangeFrameClauseMode | RowsFrameClauseMode | GroupsFrameClauseMode+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FrameClauseMode where+ toTextBuilder _settings = \case+ RangeFrameClauseMode -> "RANGE"+ RowsFrameClauseMode -> "ROWS"+ GroupsFrameClauseMode -> "GROUPS"+ parser _settings =+ asum+ [ RangeFrameClauseMode <$ Parsers.keyword "range",+ RowsFrameClauseMode <$ Parsers.keyword "rows",+ GroupsFrameClauseMode <$ Parsers.keyword "groups"+ ]++instance Qc.Arbitrary FrameClauseMode where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [RangeFrameClauseMode, RowsFrameClauseMode, GroupsFrameClauseMode]
+ library-internal/PostgresqlSyntax/Ast/FrameExtent.hs view
@@ -0,0 +1,37 @@+module PostgresqlSyntax.Ast.FrameExtent where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FrameBound+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- frame_extent:+-- | frame_bound+-- | BETWEEN frame_bound AND frame_bound+-- @+data FrameExtent = SingularFrameExtent FrameBound | BetweenFrameExtent FrameBound FrameBound+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FrameExtent where+ toTextBuilder settings = \case+ SingularFrameExtent a -> toTextBuilder settings a+ BetweenFrameExtent a b -> "BETWEEN " <> toTextBuilder settings a <> " AND " <> toTextBuilder settings b+ parser settings =+ BetweenFrameExtent+ <$> (Parsers.keyword "between" *> Parsers.space1 *> Parser.endHead *> parser settings <* Parsers.space1 <* Parsers.keyword "and" <* Parsers.space1)+ <*> parser settings+ <|> SingularFrameExtent+ <$> parser settings++instance Qc.Arbitrary FrameExtent where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ SingularFrameExtent <$> Qc.arbitrary,+ BetweenFrameExtent <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FromClause.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.FromClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FromList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- from_clause:+-- | FROM from_list+-- | /*EMPTY*/+-- @+newtype FromClause = FromClause FromList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FromClause where+ toTextBuilder settings (FromClause a) = "FROM " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "from"+ Parser.endHead+ Parsers.space1+ FromClause <$> parser settings++instance Qc.Arbitrary FromClause where+ shrink = Qc.genericShrink+ arbitrary = FromClause <$> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/FromList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.FromList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.TableRef+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- from_list:+-- | table_ref+-- | from_list ',' table_ref+-- @+newtype FromList = FromList (NonEmpty TableRef)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FromList where+ toTextBuilder settings (FromList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = FromList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary FromList where+ shrink = Qc.genericShrink+ arbitrary = FromList <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/FuncAliasClause.hs view
@@ -0,0 +1,83 @@+module PostgresqlSyntax.Ast.FuncAliasClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AliasClause+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.TableFuncElementList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_alias_clause:+-- | alias_clause+-- | AS '(' TableFuncElementList ')'+-- | AS ColId '(' TableFuncElementList ')'+-- | ColId '(' TableFuncElementList ')'+-- | EMPTY+-- @+data FuncAliasClause+ = AliasFuncAliasClause AliasClause+ | AsFuncAliasClause TableFuncElementList+ | AsColIdFuncAliasClause Ident TableFuncElementList+ | ColIdFuncAliasClause Ident TableFuncElementList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncAliasClause where+ toTextBuilder settings = \case+ AliasFuncAliasClause a -> toTextBuilder settings a+ AsFuncAliasClause a -> "AS (" <> toTextBuilder settings a <> ")"+ AsColIdFuncAliasClause a b -> "AS " <> toTextBuilder settings a <> " (" <> toTextBuilder settings b <> ")"+ ColIdFuncAliasClause a b -> toTextBuilder settings a <> " (" <> toTextBuilder settings b <> ")"+ parser settings =+ asum+ [ do+ _ <- Parsers.keyword "as"+ asum+ [ do+ Parsers.space+ Parsers.inParens $ do+ Parser.endHead+ AsFuncAliasClause <$> parser settings,+ do+ Parsers.space1+ a <- colId settings+ asum+ [ do+ Parsers.space+ Parsers.inParens $ do+ Parser.endHead+ asum+ [ AsColIdFuncAliasClause a <$> Parser.wrapToHead (parser settings),+ AliasFuncAliasClause . AliasClause True a . Just <$> parser settings+ ],+ pure (AliasFuncAliasClause (AliasClause True a Nothing))+ ]+ ],+ do+ a <- colId settings+ asum+ [ do+ Parsers.space+ Parsers.inParens $ do+ Parser.endHead+ asum+ [ ColIdFuncAliasClause a <$> Parser.wrapToHead (parser settings),+ AliasFuncAliasClause . AliasClause False a . Just <$> parser settings+ ],+ pure (AliasFuncAliasClause (AliasClause False a Nothing))+ ]+ ]++instance Qc.Arbitrary FuncAliasClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ AliasFuncAliasClause <$> Qc.arbitrary,+ AsFuncAliasClause <$> Qc.arbitrary,+ AsColIdFuncAliasClause <$> Qc.arbitrary <*> Qc.arbitrary,+ ColIdFuncAliasClause <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FuncApplication.hs view
@@ -0,0 +1,39 @@+module PostgresqlSyntax.Ast.FuncApplication where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncApplicationParams+import PostgresqlSyntax.Ast.FuncName+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_application:+-- | func_name '(' ')'+-- | func_name '(' func_arg_list opt_sort_clause ')'+-- | func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'+-- | func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'+-- | func_name '(' ALL func_arg_list opt_sort_clause ')'+-- | func_name '(' DISTINCT func_arg_list opt_sort_clause ')'+-- | func_name '(' '*' ')'+-- @+data FuncApplication = FuncApplication FuncName (Maybe FuncApplicationParams)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncApplication where+ toTextBuilder settings (FuncApplication a b) = toTextBuilder settings a <> "(" <> foldMap (toTextBuilder settings) b <> ")"++ -- \"operator\" immediately followed by \"(\" is always parsed as the start+ -- of a qualified operator (@OPERATOR(...)@), never as a call to a function+ -- literally named \"operator\", mirroring how real PostgreSQL's grammar+ -- resolves the conflict between these two productions in favor of qual_op.+ parser settings =+ Parsers.notFollowedBy (Parsers.keyword "operator" *> Parsers.space *> Parsers.char '(')+ *> Parsers.inParensWithLabel FuncApplication (parser settings) (optional (parser settings))++instance Qc.Arbitrary FuncApplication where+ shrink = Qc.genericShrink+ arbitrary = FuncApplication <$> arbitrary <*> Gens.terminatingMaybe arbitrary
+ library-internal/PostgresqlSyntax/Ast/FuncApplicationParams.hs view
@@ -0,0 +1,114 @@+module PostgresqlSyntax.Ast.FuncApplicationParams+ ( FuncApplicationParams (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncArgExpr+import PostgresqlSyntax.Ast.SortClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import qualified PostgresqlSyntax.Predicate as Predicate+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_application:+-- | func_name '(' ')'+-- | func_name '(' func_arg_list opt_sort_clause ')'+-- | func_name '(' VARIADIC func_arg_expr opt_sort_clause ')'+-- | func_name '(' func_arg_list ',' VARIADIC func_arg_expr opt_sort_clause ')'+-- | func_name '(' ALL func_arg_list opt_sort_clause ')'+-- | func_name '(' DISTINCT func_arg_list opt_sort_clause ')'+-- | func_name '(' '*' ')'+-- @+data FuncApplicationParams+ = NormalFuncApplicationParams (Maybe Bool) (NonEmpty FuncArgExpr) (Maybe SortClause)+ | VariadicFuncApplicationParams (Maybe (NonEmpty FuncArgExpr)) FuncArgExpr (Maybe SortClause)+ | StarFuncApplicationParams+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncApplicationParams where+ toTextBuilder settings = \case+ NormalFuncApplicationParams a b c ->+ TextBuilders.optLexemes+ [ fmap TextBuilders.renderAllOrDistinct a,+ Just (TextBuilders.commaNonEmpty (toTextBuilder settings) b),+ fmap (toTextBuilder settings) c+ ]+ VariadicFuncApplicationParams a b c ->+ TextBuilders.optLexemes+ [ fmap (flip mappend "," . TextBuilders.commaNonEmpty (toTextBuilder settings)) a,+ Just "VARIADIC",+ Just (toTextBuilder settings b),+ fmap (toTextBuilder settings) c+ ]+ StarFuncApplicationParams -> "*"+ parser settings =+ asum+ [ starFuncApplicationParams,+ listVariadicFuncApplicationParams,+ singleVariadicFuncApplicationParams,+ normalFuncApplicationParams+ ]+ where+ normalFuncApplicationParams = do+ optAllOrDistinct <- optional (Parsers.allOrDistinct <* Parsers.space1)+ argList <- Parsers.sep1 Parsers.commaSeparator (parser settings)+ Parser.endHead+ optSortClause <- optional (Parsers.space1 *> parser settings)+ return (NormalFuncApplicationParams optAllOrDistinct argList optSortClause)+ singleVariadicFuncApplicationParams = do+ Parsers.keyword "variadic"+ Parsers.space1+ Parser.endHead+ arg <- parser settings+ optSortClause <- optional (Parsers.space1 *> parser settings)+ return (VariadicFuncApplicationParams Nothing arg optSortClause)++ -- @func_arg_list ',' VARIADIC func_arg_expr@: one or more+ -- comma-separated 'FuncArgExpr's, where the final comma is+ -- immediately followed by (and the @VARIADIC@ Parsers.keyword itself consumed+ -- by) the terminating branch - equivalent to the pre-extraction+ -- @sepEnd1 Parsers.commaSeparator (Parsers.keyword "variadic" <* space1) funcArgExpr@.+ listVariadicFuncApplicationParams = do+ argList <- Parser.wrapToHead argListEndingInVariadic+ Parser.endHead+ arg <- parser settings+ optSortClause <- optional (Parsers.space1 *> parser settings)+ return (VariadicFuncApplicationParams (Just argList) arg optSortClause)+ argListEndingInVariadic = do+ a <- parser settings+ Parsers.commaSeparator+ asum+ [ pure (a :| []) <* (Parsers.keyword "variadic" *> Parsers.space1),+ (\(b :| bs) -> a :| b : bs) <$> argListEndingInVariadic+ ]+ -- A bare '*' char can also be the leading char of a longer operator+ -- token (e.g. "*#" in @foo(*# DEFAULT)@'s @PrefixQualOpAExpr@), so+ -- this only commits to the wildcard reading when no further op char+ -- follows - otherwise it falls through to 'normalFuncApplicationParams',+ -- which parses the '*' as the start of that operator instead.+ starFuncApplicationParams =+ Parsers.space+ *> Parsers.char '*'+ *> Parsers.notFollowedBy (Parsers.satisfy Predicate.opChar)+ *> Parser.endHead+ *> Parsers.space+ $> StarFuncApplicationParams++instance Qc.Arbitrary FuncApplicationParams where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ NormalFuncApplicationParams <$> Qc.arbitrary <*> nonEmptyOf 8 <*> Gens.terminatingMaybe Qc.arbitrary,+ VariadicFuncApplicationParams <$> maybeNonEmptyOf 8 <*> Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary,+ pure StarFuncApplicationParams+ ]+ where+ nonEmptyOf hi = Gens.nonEmptyUpTo (hi - 1) Qc.arbitrary+ maybeNonEmptyOf hi = Qc.oneof [pure Nothing, Just <$> nonEmptyOf hi]
+ library-internal/PostgresqlSyntax/Ast/FuncArgExpr.hs view
@@ -0,0 +1,70 @@+module PostgresqlSyntax.Ast.FuncArgExpr 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.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_arg_expr:+-- | a_expr+-- | param_name COLON_EQUALS a_expr+-- | param_name EQUALS_GREATER a_expr+-- param_name:+-- | type_function_name+-- @+data FuncArgExpr+ = ExprFuncArgExpr AExpr+ | ColonEqualsFuncArgExpr Ident AExpr+ | EqualsGreaterFuncArgExpr Ident AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncArgExpr where+ toTextBuilder settings = \case+ ExprFuncArgExpr a -> toTextBuilder settings a+ ColonEqualsFuncArgExpr a b -> toTextBuilder settings a <> " := " <> toTextBuilder settings b+ EqualsGreaterFuncArgExpr a b -> toTextBuilder settings a <> " => " <> toTextBuilder settings b+ parser settings =+ asum+ [ do+ a <- Parser.wrapToHead typeFunctionName+ Parsers.space+ asum+ [ do+ Parsers.string ":="+ Parser.endHead+ b <- Parsers.space *> parser settings+ return (ColonEqualsFuncArgExpr a b),+ do+ Parsers.string "=>"+ Parser.endHead+ b <- Parsers.space *> parser settings+ return (EqualsGreaterFuncArgExpr a b)+ ],+ ExprFuncArgExpr <$> parser settings+ ]+ where+ -- Duplicated from "PostgresqlSyntax.Parsing"'s @typeFunctionName@+ -- (a bare-aliased 'PostgresqlSyntax.Ast.Ident' whose own parser lives+ -- above this module in the dependency order), mirroring the+ -- 'PostgresqlSyntax.Ast.AnyName'\/'PostgresqlSyntax.Ast.NameList'+ -- precedent.+ typeFunctionName =+ Parsers.keywordNameFromSet UnquotedIdent KeywordSet.typeFunctionName+ <|> parser settings++instance Qc.Arbitrary FuncArgExpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExprFuncArgExpr <$> Gens.downscale Qc.arbitrary,+ ColonEqualsFuncArgExpr <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ EqualsGreaterFuncArgExpr <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FuncConstArgs.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.FuncConstArgs where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncArgExpr+import PostgresqlSyntax.Ast.SortClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- The parenthesized-argument-list part of a @func_name '(' func_arg_list+-- opt_sort_clause ')' Sconst@ 'PostgresqlSyntax.Ast.AexprConst' - rendered\/+-- parsed without its enclosing parens, which belong to the caller.+--+-- ==== References+-- @+-- | func_name '(' func_arg_list opt_sort_clause ')' Sconst+-- @+data FuncConstArgs = FuncConstArgs (NonEmpty FuncArgExpr) (Maybe SortClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncConstArgs where+ toTextBuilder settings (FuncConstArgs a b) = TextBuilders.commaNonEmpty (toTextBuilder settings) a <> TextBuilders.suffixMaybe (toTextBuilder settings) b+ parser settings = FuncConstArgs <$> Parsers.sep1 Parsers.commaSeparator (parser settings) <*> optional (Parsers.space1 *> parser settings)++instance Qc.Arbitrary FuncConstArgs where+ shrink = Qc.genericShrink+ arbitrary = FuncConstArgs <$> Gens.nonEmptyUpTo 6 Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/FuncExpr.hs view
@@ -0,0 +1,78 @@+module PostgresqlSyntax.Ast.FuncExpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.FuncApplication+import PostgresqlSyntax.Ast.FuncExprCommonSubexpr+import PostgresqlSyntax.Ast.OverClause+import PostgresqlSyntax.Ast.SortClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_expr:+-- | func_application within_group_clause filter_clause over_clause+-- | func_expr_common_subexpr+-- @+--+-- @within_group_clause@ and @filter_clause@ are bare aliases to+-- 'PostgresqlSyntax.Ast.SortClause' and 'PostgresqlSyntax.Ast.AExpr'+-- respectively.+data FuncExpr+ = ApplicationFuncExpr FuncApplication (Maybe SortClause) (Maybe AExpr) (Maybe OverClause)+ | SubexprFuncExpr FuncExprCommonSubexpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncExpr where+ toTextBuilder settings = \case+ ApplicationFuncExpr a b c d ->+ TextBuilders.optLexemes+ [ Just (toTextBuilder settings a),+ fmap withinGroupClause b,+ fmap filterClause c,+ fmap (toTextBuilder settings) d+ ]+ SubexprFuncExpr a -> toTextBuilder settings a+ where+ withinGroupClause a = "WITHIN GROUP (" <> toTextBuilder settings a <> ")"+ filterClause a = "FILTER (WHERE " <> toTextBuilder settings a <> ")"+ parser settings =+ asum+ [ SubexprFuncExpr <$> parser settings,+ do+ a <- parser settings+ Parser.endHead+ b <- optional (Parsers.space1 *> withinGroupClause)+ c <- optional (Parsers.space1 *> filterClause)+ d <- optional (Parsers.space1 *> parser settings)+ return (ApplicationFuncExpr a b c d)+ ]+ where+ withinGroupClause = do+ Parsers.keyphrase "within group"+ Parser.endHead+ Parsers.space+ Parsers.inParens (parser settings)+ filterClause = do+ Parsers.keyword "filter"+ Parser.endHead+ Parsers.space+ Parsers.inParens (Parsers.keyword "where" *> Parsers.space1 *> parser settings)++instance Qc.Arbitrary FuncExpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ApplicationFuncExpr+ <$> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary)+ <*> Gens.terminatingMaybe Qc.arbitrary,+ SubexprFuncExpr <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FuncExprCommonSubexpr.hs view
@@ -0,0 +1,199 @@+module PostgresqlSyntax.Ast.FuncExprCommonSubexpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.ExtractList+import PostgresqlSyntax.Ast.OverlayList+import PostgresqlSyntax.Ast.PositionList+import PostgresqlSyntax.Ast.SubstrList+import PostgresqlSyntax.Ast.TrimList+import PostgresqlSyntax.Ast.TrimModifier+import PostgresqlSyntax.Ast.Typename+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_expr_common_subexpr:+-- | COLLATION FOR '(' a_expr ')'+-- | CURRENT_DATE+-- | CURRENT_TIME+-- | CURRENT_TIME '(' Iconst ')'+-- | CURRENT_TIMESTAMP+-- | CURRENT_TIMESTAMP '(' Iconst ')'+-- | LOCALTIME+-- | LOCALTIME '(' Iconst ')'+-- | LOCALTIMESTAMP+-- | LOCALTIMESTAMP '(' Iconst ')'+-- | CURRENT_ROLE+-- | CURRENT_USER+-- | SESSION_USER+-- | USER+-- | CURRENT_CATALOG+-- | CURRENT_SCHEMA+-- | CAST '(' a_expr AS Typename ')'+-- | EXTRACT '(' extract_list ')'+-- | OVERLAY '(' overlay_list ')'+-- | POSITION '(' position_list ')'+-- | SUBSTRING '(' substr_list ')'+-- | TREAT '(' a_expr AS Typename ')'+-- | TRIM '(' BOTH trim_list ')'+-- | TRIM '(' LEADING trim_list ')'+-- | TRIM '(' TRAILING trim_list ')'+-- | TRIM '(' trim_list ')'+-- | NULLIF '(' a_expr ',' a_expr ')'+-- | COALESCE '(' expr_list ')'+-- | GREATEST '(' expr_list ')'+-- | LEAST '(' expr_list ')'+-- | XMLCONCAT '(' expr_list ')'+-- | XMLELEMENT '(' NAME_P ColLabel ')'+-- | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'+-- | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'+-- | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'+-- | XMLEXISTS '(' c_expr xmlexists_argument ')'+-- | XMLFOREST '(' xml_attribute_list ')'+-- | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'+-- | XMLPI '(' NAME_P ColLabel ')'+-- | XMLPI '(' NAME_P ColLabel ',' a_expr ')'+-- | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'+-- | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'+--+-- TODO: Implement the XML cases+-- @+data FuncExprCommonSubexpr+ = CollationForFuncExprCommonSubexpr AExpr+ | CurrentDateFuncExprCommonSubexpr+ | CurrentTimeFuncExprCommonSubexpr (Maybe Int64)+ | CurrentTimestampFuncExprCommonSubexpr (Maybe Int64)+ | LocalTimeFuncExprCommonSubexpr (Maybe Int64)+ | LocalTimestampFuncExprCommonSubexpr (Maybe Int64)+ | CurrentRoleFuncExprCommonSubexpr+ | CurrentUserFuncExprCommonSubexpr+ | SessionUserFuncExprCommonSubexpr+ | UserFuncExprCommonSubexpr+ | CurrentCatalogFuncExprCommonSubexpr+ | CurrentSchemaFuncExprCommonSubexpr+ | CastFuncExprCommonSubexpr AExpr Typename+ | ExtractFuncExprCommonSubexpr (Maybe ExtractList)+ | OverlayFuncExprCommonSubexpr OverlayList+ | PositionFuncExprCommonSubexpr (Maybe PositionList)+ | SubstringFuncExprCommonSubexpr (Maybe SubstrList)+ | TreatFuncExprCommonSubexpr AExpr Typename+ | TrimFuncExprCommonSubexpr (Maybe TrimModifier) TrimList+ | NullIfFuncExprCommonSubexpr AExpr AExpr+ | CoalesceFuncExprCommonSubexpr ExprList+ | GreatestFuncExprCommonSubexpr ExprList+ | LeastFuncExprCommonSubexpr ExprList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncExprCommonSubexpr where+ toTextBuilder settings = \case+ CollationForFuncExprCommonSubexpr a -> "COLLATION FOR (" <> toTextBuilder settings a <> ")"+ CurrentDateFuncExprCommonSubexpr -> "CURRENT_DATE"+ CurrentTimeFuncExprCommonSubexpr a -> "CURRENT_TIME" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . TextBuilder.int64Dec) a+ CurrentTimestampFuncExprCommonSubexpr a -> "CURRENT_TIMESTAMP" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . TextBuilder.int64Dec) a+ LocalTimeFuncExprCommonSubexpr a -> "LOCALTIME" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . TextBuilder.int64Dec) a+ LocalTimestampFuncExprCommonSubexpr a -> "LOCALTIMESTAMP" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . TextBuilder.int64Dec) a+ CurrentRoleFuncExprCommonSubexpr -> "CURRENT_ROLE"+ CurrentUserFuncExprCommonSubexpr -> "CURRENT_USER"+ SessionUserFuncExprCommonSubexpr -> "SESSION_USER"+ UserFuncExprCommonSubexpr -> "USER"+ CurrentCatalogFuncExprCommonSubexpr -> "CURRENT_CATALOG"+ CurrentSchemaFuncExprCommonSubexpr -> "CURRENT_SCHEMA"+ CastFuncExprCommonSubexpr a b -> "CAST (" <> toTextBuilder settings a <> " AS " <> toTextBuilder settings b <> ")"+ ExtractFuncExprCommonSubexpr a -> "EXTRACT (" <> foldMap (toTextBuilder settings) a <> ")"+ OverlayFuncExprCommonSubexpr a -> "OVERLAY (" <> toTextBuilder settings a <> ")"+ PositionFuncExprCommonSubexpr a -> "POSITION (" <> foldMap (toTextBuilder settings) a <> ")"+ SubstringFuncExprCommonSubexpr a -> "SUBSTRING (" <> foldMap (toTextBuilder settings) a <> ")"+ TreatFuncExprCommonSubexpr a b -> "TREAT (" <> toTextBuilder settings a <> " AS " <> toTextBuilder settings b <> ")"+ TrimFuncExprCommonSubexpr a b -> "TRIM (" <> TextBuilders.prefixMaybe (toTextBuilder settings) a <> toTextBuilder settings b <> ")"+ NullIfFuncExprCommonSubexpr a b -> "NULLIF (" <> toTextBuilder settings a <> ", " <> toTextBuilder settings b <> ")"+ CoalesceFuncExprCommonSubexpr a -> "COALESCE (" <> toTextBuilder settings a <> ")"+ GreatestFuncExprCommonSubexpr a -> "GREATEST (" <> toTextBuilder settings a <> ")"+ LeastFuncExprCommonSubexpr a -> "LEAST (" <> toTextBuilder settings a <> ")"+ parser settings =+ asum+ [ CollationForFuncExprCommonSubexpr <$> Parsers.inParensWithClause (Parsers.keyphrase "collation for") (parser settings),+ CurrentDateFuncExprCommonSubexpr <$ Parsers.keyword "current_date",+ CurrentTimestampFuncExprCommonSubexpr <$> labeledIconst "current_timestamp",+ CurrentTimeFuncExprCommonSubexpr <$> labeledIconst "current_time",+ LocalTimestampFuncExprCommonSubexpr <$> labeledIconst "localtimestamp",+ LocalTimeFuncExprCommonSubexpr <$> labeledIconst "localtime",+ CurrentRoleFuncExprCommonSubexpr <$ Parsers.keyword "current_role",+ CurrentUserFuncExprCommonSubexpr <$ Parsers.keyword "current_user",+ SessionUserFuncExprCommonSubexpr <$ Parsers.keyword "session_user",+ UserFuncExprCommonSubexpr <$ Parsers.keyword "user",+ CurrentCatalogFuncExprCommonSubexpr <$ Parsers.keyword "current_catalog",+ CurrentSchemaFuncExprCommonSubexpr <$ Parsers.keyword "current_schema",+ Parsers.inParensWithClause (Parsers.keyword "cast") (CastFuncExprCommonSubexpr <$> parser settings <*> (Parsers.space1 *> Parsers.keyword "as" *> Parsers.space1 *> parser settings)),+ Parsers.inParensWithClause (Parsers.keyword "extract") (ExtractFuncExprCommonSubexpr <$> optional (parser settings)),+ Parsers.inParensWithClause (Parsers.keyword "overlay") (OverlayFuncExprCommonSubexpr <$> parser settings),+ Parsers.inParensWithClause (Parsers.keyword "position") (PositionFuncExprCommonSubexpr <$> optional (parser settings)),+ Parsers.inParensWithClause (Parsers.keyword "substring") (SubstringFuncExprCommonSubexpr <$> optional (parser settings)),+ Parsers.inParensWithClause (Parsers.keyword "treat") (TreatFuncExprCommonSubexpr <$> parser settings <*> (Parsers.space1 *> Parsers.keyword "as" *> Parsers.space1 *> parser settings)),+ Parsers.inParensWithClause (Parsers.keyword "trim") (TrimFuncExprCommonSubexpr <$> optional (parser settings <* Parsers.space1) <*> parser settings),+ Parsers.inParensWithClause (Parsers.keyword "nullif") (NullIfFuncExprCommonSubexpr <$> parser settings <*> (Parsers.commaSeparator *> parser settings)),+ Parsers.inParensWithClause (Parsers.keyword "coalesce") (CoalesceFuncExprCommonSubexpr <$> parser settings),+ Parsers.inParensWithClause (Parsers.keyword "greatest") (GreatestFuncExprCommonSubexpr <$> parser settings),+ Parsers.inParensWithClause (Parsers.keyword "least") (LeastFuncExprCommonSubexpr <$> parser settings)+ ]+ where+ labeledIconst lbl = Parsers.keyword lbl *> Parser.endHead *> optional (Parsers.space *> Parsers.inParens Parsers.decimal)++instance Qc.Arbitrary FuncExprCommonSubexpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then+ Qc.oneof+ [ pure CurrentDateFuncExprCommonSubexpr,+ pure CurrentRoleFuncExprCommonSubexpr,+ pure CurrentUserFuncExprCommonSubexpr,+ pure SessionUserFuncExprCommonSubexpr,+ pure UserFuncExprCommonSubexpr,+ pure CurrentCatalogFuncExprCommonSubexpr,+ pure CurrentSchemaFuncExprCommonSubexpr+ ]+ else+ Qc.oneof+ [ CollationForFuncExprCommonSubexpr <$> Gens.downscale Qc.arbitrary,+ pure CurrentDateFuncExprCommonSubexpr,+ -- The @Iconst@ here is parsed via 'Parsers.decimal' (unsigned), so+ -- it must never be negative - mirroring+ -- 'PostgresqlSyntax.Ast.IntervalSecond'\'s own @nonNegative@.+ CurrentTimeFuncExprCommonSubexpr <$> nonNegativeMaybeInt64,+ CurrentTimestampFuncExprCommonSubexpr <$> nonNegativeMaybeInt64,+ LocalTimeFuncExprCommonSubexpr <$> nonNegativeMaybeInt64,+ LocalTimestampFuncExprCommonSubexpr <$> nonNegativeMaybeInt64,+ pure CurrentRoleFuncExprCommonSubexpr,+ pure CurrentUserFuncExprCommonSubexpr,+ pure SessionUserFuncExprCommonSubexpr,+ pure UserFuncExprCommonSubexpr,+ pure CurrentCatalogFuncExprCommonSubexpr,+ pure CurrentSchemaFuncExprCommonSubexpr,+ CastFuncExprCommonSubexpr <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ ExtractFuncExprCommonSubexpr <$> Qc.arbitrary,+ OverlayFuncExprCommonSubexpr <$> Qc.arbitrary,+ PositionFuncExprCommonSubexpr <$> Qc.arbitrary,+ SubstringFuncExprCommonSubexpr <$> Qc.arbitrary,+ TreatFuncExprCommonSubexpr <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ TrimFuncExprCommonSubexpr <$> Qc.arbitrary <*> Qc.arbitrary,+ NullIfFuncExprCommonSubexpr <$> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ CoalesceFuncExprCommonSubexpr <$> Qc.arbitrary,+ GreatestFuncExprCommonSubexpr <$> Qc.arbitrary,+ LeastFuncExprCommonSubexpr <$> Qc.arbitrary+ ]+ where+ nonNegativeMaybeInt64 = Qc.oneof [pure Nothing, Just <$> nonNegativeInt64]+ nonNegativeInt64 = Qc.sized (\n -> Qc.choose (0, cap n))+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/FuncExprCommonSubexpr.hs-boot view
@@ -0,0 +1,19 @@+module PostgresqlSyntax.Ast.FuncExprCommonSubexpr where++import PostgresqlSyntax.Algebra (IsAst)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data FuncExprCommonSubexpr++instance Show FuncExprCommonSubexpr++instance Eq FuncExprCommonSubexpr++instance Ord FuncExprCommonSubexpr++instance Data FuncExprCommonSubexpr++instance IsAst FuncExprCommonSubexpr++instance Arbitrary FuncExprCommonSubexpr
+ library-internal/PostgresqlSyntax/Ast/FuncExprWindowless.hs view
@@ -0,0 +1,37 @@+module PostgresqlSyntax.Ast.FuncExprWindowless where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncApplication+import PostgresqlSyntax.Ast.FuncExprCommonSubexpr+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_expr_windowless:+-- | func_application+-- | func_expr_common_subexpr+-- @+data FuncExprWindowless+ = ApplicationFuncExprWindowless FuncApplication+ | CommonSubexprFuncExprWindowless FuncExprCommonSubexpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncExprWindowless where+ toTextBuilder settings = \case+ ApplicationFuncExprWindowless a -> toTextBuilder settings a+ CommonSubexprFuncExprWindowless a -> toTextBuilder settings a+ parser settings =+ asum+ [ CommonSubexprFuncExprWindowless <$> parser settings,+ ApplicationFuncExprWindowless <$> parser settings+ ]++instance Qc.Arbitrary FuncExprWindowless where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ApplicationFuncExprWindowless <$> Qc.arbitrary,+ CommonSubexprFuncExprWindowless <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FuncName.hs view
@@ -0,0 +1,60 @@+module PostgresqlSyntax.Ast.FuncName where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Indirection+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- 'PostgresqlSyntax.Ast.ColId' and 'PostgresqlSyntax.Ast.TypeFunctionName'+-- are both bare aliases to 'Ident', but their /parsers/ (kept in+-- "PostgresqlSyntax.Parsing" since neither is extracted in this batch) are+-- more permissive than plain 'Ident'. Since this module sits below+-- "PostgresqlSyntax.Parsing" (no import cycle allowed), those flavored+-- element parsers are duplicated here, same as 'PostgresqlSyntax.Ast.NameList'.+--+-- ==== References+-- @+-- func_name:+-- | type_function_name+-- | ColId indirection+-- @+data FuncName+ = TypeFuncName Ident+ | IndirectedFuncName Ident Indirection+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncName where+ toTextBuilder settings = \case+ TypeFuncName a -> toTextBuilder settings a+ IndirectedFuncName a b -> toTextBuilder settings a <> toTextBuilder settings b+ parser settings =+ IndirectedFuncName+ <$> Parser.wrapToHead colIdLikeName+ <*> (Parsers.space *> parser settings)+ <|> TypeFuncName+ <$> typeFunctionNameLikeName+ where+ colIdLikeName =+ Parser.label "identifier" $+ parser settings+ <|> Parsers.keywordNameFromSet UnquotedIdent (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword)+ typeFunctionNameLikeName =+ Parsers.keywordNameFromSet UnquotedIdent KeywordSet.typeFunctionName+ <|> parser settings++instance Qc.Arbitrary FuncName where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then TypeFuncName <$> Qc.arbitrary+ else+ Qc.oneof+ [ TypeFuncName <$> Qc.arbitrary,+ IndirectedFuncName <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/FuncTable.hs view
@@ -0,0 +1,50 @@+module PostgresqlSyntax.Ast.FuncTable where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncExprWindowless+import PostgresqlSyntax.Ast.OptOrdinality+import PostgresqlSyntax.Ast.RowsfromList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- func_table:+-- | func_expr_windowless opt_ordinality+-- | ROWS FROM '(' rowsfrom_list ')' opt_ordinality+-- @+data FuncTable+ = FuncExprFuncTable FuncExprWindowless OptOrdinality+ | RowsFromFuncTable RowsfromList OptOrdinality+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst FuncTable where+ toTextBuilder settings = \case+ FuncExprFuncTable a (OptOrdinality b) -> toTextBuilder settings a <> bool "" " WITH ORDINALITY" b+ RowsFromFuncTable a (OptOrdinality b) -> "ROWS FROM (" <> toTextBuilder settings a <> ")" <> bool "" " WITH ORDINALITY" b+ parser settings =+ asum+ [ do+ Parsers.keyword "rows"+ Parsers.space1+ Parsers.keyword "from"+ Parsers.space+ a <- Parsers.inParens (Parser.endHead *> parser settings)+ b <- OptOrdinality <$> Parsers.trueIfPresent (Parsers.space *> Parsers.keyword "with" *> Parsers.space1 *> Parsers.keyword "ordinality")+ return (RowsFromFuncTable a b),+ do+ a <- parser settings+ b <- OptOrdinality <$> Parsers.trueIfPresent (Parsers.space1 *> Parsers.keyword "with" *> Parsers.space1 *> Parsers.keyword "ordinality")+ return (FuncExprFuncTable a b)+ ]++instance Qc.Arbitrary FuncTable where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ FuncExprFuncTable <$> Qc.arbitrary <*> Qc.arbitrary,+ RowsFromFuncTable <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/GenericType.hs view
@@ -0,0 +1,45 @@+module PostgresqlSyntax.Ast.GenericType where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Attrs+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.Ident+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- GenericType:+-- | type_function_name opt_type_modifiers+-- | type_function_name attrs opt_type_modifiers+-- @+--+-- 'PostgresqlSyntax.Ast.TypeFunctionName' is a bare alias to 'Ident', but its+-- /parser/ (kept in "PostgresqlSyntax.Parsing" since @TypeFunctionName@+-- 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 TypeFunctionName-flavored element parser is+-- duplicated here (mirroring @typeFunctionName@'s definition), same as+-- 'PostgresqlSyntax.Ast.NameList'.+data GenericType = GenericType Ident (Maybe Attrs) (Maybe ExprList)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst GenericType where+ toTextBuilder settings (GenericType a b c) = toTextBuilder settings a <> foldMap (toTextBuilder settings) b <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . toTextBuilder settings) c+ parser settings = do+ a <- typeFunctionNameLikeName+ Parser.endHead+ b <- optional (Parsers.space *> parser settings)+ c <- optional (Parsers.space1 *> Parsers.inParens (parser settings))+ return (GenericType a b c)+ where+ typeFunctionNameLikeName = Parsers.keywordNameFromSet UnquotedIdent KeywordSet.typeFunctionName <|> parser settings++instance Qc.Arbitrary GenericType where+ shrink = Qc.genericShrink+ arbitrary = GenericType <$> arbitrary <*> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/GroupByItem.hs view
@@ -0,0 +1,68 @@+module PostgresqlSyntax.Ast.GroupByItem where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- group_by_item:+-- | a_expr+-- | empty_grouping_set+-- | cube_clause+-- | rollup_clause+-- | grouping_sets_clause+-- empty_grouping_set:+-- | '(' ')'+-- rollup_clause:+-- | ROLLUP '(' expr_list ')'+-- cube_clause:+-- | CUBE '(' expr_list ')'+-- grouping_sets_clause:+-- | GROUPING SETS '(' group_by_list ')'+-- @+data GroupByItem+ = ExprGroupByItem AExpr+ | EmptyGroupingSetGroupByItem+ | RollupGroupByItem ExprList+ | CubeGroupByItem ExprList+ | GroupingSetsGroupByItem (NonEmpty GroupByItem)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst GroupByItem where+ toTextBuilder settings = \case+ ExprGroupByItem a -> toTextBuilder settings a+ EmptyGroupingSetGroupByItem -> "()"+ RollupGroupByItem a -> "ROLLUP (" <> toTextBuilder settings a <> ")"+ CubeGroupByItem a -> "CUBE (" <> toTextBuilder settings a <> ")"+ GroupingSetsGroupByItem a -> "GROUPING SETS (" <> TextBuilders.commaNonEmpty (toTextBuilder settings) a <> ")"+ parser settings =+ asum+ [ EmptyGroupingSetGroupByItem <$ (Parsers.char '(' *> Parsers.space *> Parsers.char ')'),+ RollupGroupByItem . ExprList <$> (Parsers.keyword "rollup" *> Parser.endHead *> Parsers.space *> Parsers.inParens (Parsers.sep1 Parsers.commaSeparator (parser settings))),+ CubeGroupByItem . ExprList <$> (Parsers.keyword "cube" *> Parser.endHead *> Parsers.space *> Parsers.inParens (Parsers.sep1 Parsers.commaSeparator (parser settings))),+ GroupingSetsGroupByItem <$> (Parsers.keyphrase "grouping sets" *> Parser.endHead *> Parsers.space *> Parsers.inParens (Parsers.sep1 Parsers.commaSeparator (parser settings))),+ ExprGroupByItem <$> parser settings+ ]++instance Qc.Arbitrary GroupByItem where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then Qc.oneof [ExprGroupByItem <$> Gens.downscale Qc.arbitrary, pure EmptyGroupingSetGroupByItem]+ else+ Qc.oneof+ [ ExprGroupByItem <$> Gens.downscale Qc.arbitrary,+ pure EmptyGroupingSetGroupByItem,+ RollupGroupByItem <$> Qc.arbitrary,+ CubeGroupByItem <$> Qc.arbitrary,+ GroupingSetsGroupByItem <$> Gens.downscale (Gens.nonEmptyUpTo 2 Qc.arbitrary)+ ]
+ library-internal/PostgresqlSyntax/Ast/GroupClause.hs view
@@ -0,0 +1,35 @@+module PostgresqlSyntax.Ast.GroupClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.GroupByItem+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- group_clause:+-- | GROUP_P BY set_quantifier group_by_list+-- | /*EMPTY*/+-- @+--+-- @set_quantifier@ (@DISTINCT@\/@ALL@) is not modeled here - this+-- codebase's grammar subset doesn't support @GROUP BY DISTINCT@.+newtype GroupClause = GroupClause (NonEmpty GroupByItem)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst GroupClause where+ toTextBuilder settings (GroupClause a) = "GROUP BY " <> TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = do+ Parsers.keyphrase "group by"+ Parser.endHead+ Parsers.space1+ GroupClause <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary GroupClause where+ shrink = Qc.genericShrink+ arbitrary = GroupClause <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/HavingClause.hs view
@@ -0,0 +1,31 @@+module PostgresqlSyntax.Ast.HavingClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- having_clause:+-- | HAVING a_expr+-- | /*EMPTY*/+-- @+newtype HavingClause = HavingClause AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst HavingClause where+ toTextBuilder settings (HavingClause a) = "HAVING " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "having"+ Parser.endHead+ Parsers.space1+ HavingClause <$> parser settings++instance Qc.Arbitrary HavingClause where+ shrink = Qc.genericShrink+ arbitrary = HavingClause <$> Gens.downscale Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/HsTargetEl.hs view
@@ -0,0 +1,138 @@+-- |+-- Kronor extension: Haskell-valued entries in a @SELECT@ list.+--+-- Not Postgres syntax. The productions in this module are only available+-- when 'PostgresqlSyntax.Settings.haskellTargets' is on; with the option+-- off (the 'mempty' default) an 'HsTargetEl' is exactly a+-- 'PostgresqlSyntax.Ast.TargetEl.TargetEl' wrapped in 'SqlTargetEl', so+-- upstream Postgres parsing is unchanged.+module PostgresqlSyntax.Ast.HsTargetEl where++import qualified Data.Text as Text+import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.TargetEl+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.Shrinks as Shrinks+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (Settings, resolveHaskellTargets)+import qualified Test.QuickCheck as Qc+import qualified TextBuilder++-- |+-- ==== References+-- @+-- hs_target_el:+-- | '$' hs_name '{' hs_field_target_list '}'+-- | '$' hs_name '(' hs_target_list? ')'+-- | target_el+-- @+data HsTargetEl+ = -- | @$R {a = x, b = y}@ - a Haskell record construction (uppercase+ -- name) or record update (lowercase name).+ HsRecTargetEl Text (NonEmpty HsFieldTargetEl)+ | -- | @$f (a, b)@ - a Haskell function or constructor applied to the+ -- listed targets. The argument list may be empty: @$f ()@.+ HsFuncTargetEl Text [HsTargetEl]+ | -- | An ordinary SQL target.+ SqlTargetEl TargetEl+ deriving (Show, Generic, Eq, Ord, Data)++-- |+-- ==== References+-- @+-- hs_field_target_el:+-- | hs_name '=' hs_target_el+-- @+data HsFieldTargetEl = HsFieldEl Text HsTargetEl+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst HsTargetEl where+ toTextBuilder settings = \case+ HsRecTargetEl a b -> "$" <> TextBuilder.text a <> " {" <> TextBuilders.commaNonEmpty (toTextBuilder settings) b <> "}"+ HsFuncTargetEl a b -> "$" <> TextBuilder.text a <> " (" <> mconcat (intersperse ", " (fmap (toTextBuilder settings) b)) <> ")"+ SqlTargetEl a -> toTextBuilder settings a++ parser settings =+ if resolveHaskellTargets settings+ then+ -- The '$'-prefixed alternatives come first: neither commits to a+ -- head before its distinguishing delimiter, so both stay+ -- backtrackable, but placing them after the SQL target would let a+ -- committed SQL branch swallow them.+ Parser.label "haskell target" $+ asum+ [ hsRecTarget settings,+ hsFuncTarget settings,+ SqlTargetEl <$> parser settings+ ]+ else SqlTargetEl <$> parser settings++-- |+-- @$R {a = x, b = y}@. The name predicate is deliberately permissive - any+-- run of characters that is neither a space nor the opening brace - so that+-- qualified names like @$Mod.R@ work.+hsRecTarget :: Settings -> Parser HsTargetEl+hsRecTarget settings =+ Parser.label "haskell record target" $ do+ Parsers.char '$'+ recConsName <- Parsers.takeWhile1P (Just "Haskell record constructor") (\x -> not (isSpace x || x == '{'))+ Parsers.space+ Parsers.char '{'+ elts <- Parsers.sep1 Parsers.commaSeparator (parser settings)+ Parsers.space+ Parsers.char '}'+ pure (HsRecTargetEl recConsName elts)++-- |+-- @$f (a, b)@, including the nullary @$f ()@. The name predicate stops at a+-- space or the opening paren, so qualified names like @$Mod.f@ work.+hsFuncTarget :: Settings -> Parser HsTargetEl+hsFuncTarget settings =+ Parser.label "haskell function target" $ do+ Parsers.char '$'+ funcName <- Parsers.takeWhile1P (Just "Haskell function") (\x -> not (isSpace x || x == '('))+ Parsers.space+ Parsers.char '('+ Parsers.space+ elts <- (toList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)) <|> pure []+ Parsers.space+ Parsers.char ')'+ pure (HsFuncTargetEl funcName elts)++instance IsAst HsFieldTargetEl where+ toTextBuilder settings (HsFieldEl a b) = TextBuilder.text a <> " = " <> toTextBuilder settings b+ parser settings =+ Parser.label "haskell field target" $ do+ Parsers.space+ fieldName <- Parsers.takeWhile1P (Just "Haskell field name") (\x -> not (isSpace x || x == '='))+ Parsers.space+ Parsers.char '='+ Parsers.space+ HsFieldEl fieldName <$> parser settings++-- |+-- Only 'SqlTargetEl' is ever generated. The extension constructors parse+-- only under 'PostgresqlSyntax.Settings.haskellTargets', while+-- 'PostgresqlSyntax.Algebra.isAstProperties' roundtrips at 'mempty' - same+-- reasoning as 'PostgresqlSyntax.Ast.Typename.Typename''s hardcoded+-- nullability flags.+instance Qc.Arbitrary HsTargetEl where+ shrink = \case+ SqlTargetEl a -> SqlTargetEl <$> Qc.shrink a+ HsRecTargetEl a b -> HsRecTargetEl a <$> Qc.shrink b+ HsFuncTargetEl a b -> HsFuncTargetEl a <$> Qc.shrink b+ arbitrary = SqlTargetEl <$> Qc.arbitrary++-- |+-- Unreachable from 'HsTargetEl''s generator (which never produces+-- 'HsRecTargetEl'); it exists so this type gets the same per-module+-- property spec as its siblings.+instance Qc.Arbitrary HsFieldTargetEl where+ shrink (HsFieldEl a b) =+ [HsFieldEl a' b | a' <- Shrinks.nonEmptyText a] <> [HsFieldEl a b' | b' <- Qc.shrink b]+ arbitrary = HsFieldEl <$> genFieldName <*> Gens.downscale Qc.arbitrary+ where+ genFieldName = Text.pack <$> Qc.listOf1 (Qc.elements ['a' .. 'z'])
+ library-internal/PostgresqlSyntax/Ast/HsTargetList.hs view
@@ -0,0 +1,30 @@+-- |+-- Kronor extension: the 'PostgresqlSyntax.Ast.HsTargetEl.HsTargetEl'+-- counterpart of 'PostgresqlSyntax.Ast.TargetList.TargetList'.+module PostgresqlSyntax.Ast.HsTargetList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.HsTargetEl+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- hs_target_list:+-- | hs_target_el+-- | hs_target_list ',' hs_target_el+-- @+newtype HsTargetList = HsTargetList (NonEmpty HsTargetEl)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst HsTargetList where+ toTextBuilder settings (HsTargetList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = HsTargetList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary HsTargetList where+ shrink = Qc.genericShrink+ arbitrary = HsTargetList <$> Gens.nonEmptyUpTo 7 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/Iconst.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.Iconst where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- Iconst+-- @+newtype Iconst = Iconst Int64+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Iconst where+ toTextBuilder _settings (Iconst a) = TextBuilder.int64Dec a+ parser _settings = Iconst <$> Parsers.decimal++instance Qc.Arbitrary Iconst where+ shrink = Qc.genericShrink+ arbitrary = Iconst <$> Qc.sized (\n -> Qc.choose (0, cap n))+ where+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/Ident.hs view
@@ -0,0 +1,103 @@+module PostgresqlSyntax.Ast.Ident where++import qualified Data.Text as Text+import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.Shrinks as Shrinks+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import qualified PostgresqlSyntax.Predicate as Predicate+import PostgresqlSyntax.Prelude hiding (filter, try)+import PostgresqlSyntax.Settings (Settings)+import qualified Test.QuickCheck as Qc+import qualified TextBuilder++-- |+-- ==== References+-- @+-- IDENT+-- @+data Ident = QuotedIdent Text | UnquotedIdent Text+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Ident where+ toTextBuilder _settings = \case+ QuotedIdent a -> TextBuilder.char7 '"' <> TextBuilder.text (Text.replace "\"" "\"\"" a) <> TextBuilder.char7 '"'+ UnquotedIdent a -> TextBuilder.text a+ parser _settings = quotedName <|> Parsers.keywordNameByPredicate UnquotedIdent (not . Predicate.keyword)+ where+ quotedName = Parser.filter (const "Empty name") (not . Text.null) (Parsers.quotedString '"') & fmap QuotedIdent++-- |+-- ==== References+-- @+-- ColId:+-- | IDENT+-- | unreserved_keyword+-- | col_name_keyword+-- @+--+-- Most grammar positions that hold an identifier (column\/table\/alias+-- names, ...) are actually @ColId@, not bare @IDENT@ - this is the+-- permissive variant that most 'Ident'-typed fields elsewhere in+-- "PostgresqlSyntax.Ast" should parse with, since 'Ident'\'s own generic+-- 'parser' only accepts the strict @IDENT@ token (no Parsers.keyword fallback).+colId :: Settings -> Parser Ident+colId settings =+ Parser.label "identifier" $+ parser settings+ <|> Parsers.keywordNameFromSet UnquotedIdent (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword)++-- |+-- ==== References+-- @+-- ColLabel:+-- | IDENT+-- | unreserved_keyword+-- | col_name_keyword+-- | type_func_name_keyword+-- | reserved_keyword+-- @+colLabel :: Settings -> Parser Ident+colLabel settings =+ Parser.label "column label" $+ Parsers.keywordNameFromSet UnquotedIdent KeywordSet.keyword+ <|> parser settings++-- |+-- ==== References+-- @+-- type_function_name:+-- | IDENT+-- | unreserved_keyword+-- | type_func_name_keyword+-- @+typeFunctionName :: Settings -> Parser Ident+typeFunctionName settings =+ Parsers.keywordNameFromSet UnquotedIdent KeywordSet.typeFunctionName+ <|> parser settings++instance Qc.Arbitrary Ident where+ shrink = \case+ QuotedIdent a -> QuotedIdent <$> Shrinks.textTail a+ UnquotedIdent a -> UnquotedIdent <$> Shrinks.textTail a+ arbitrary =+ Qc.frequency+ [ (95, UnquotedIdent <$> unquotedIdentText),+ (5, QuotedIdent <$> quotedIdentText)+ ]+ where+ unquotedIdentText =+ ( do+ firstChar <- Qc.elements startChars+ restLength <- Qc.choose (0, 29)+ rest <- Qc.vectorOf restLength (Qc.elements contChars)+ pure (Text.pack (firstChar : rest))+ )+ `Qc.suchThat` (not . Predicate.keyword)+ startChars = ['a' .. 'z'] <> ['_']+ contChars = startChars <> ['0' .. '9'] <> ['$']+ quotedIdentText = do+ len <- Qc.choose (1, 30)+ Text.pack <$> Qc.vectorOf len (Qc.arbitrary `Qc.suchThat` (not . isControl))
+ library-internal/PostgresqlSyntax/Ast/ImplicitRow.hs view
@@ -0,0 +1,38 @@+module PostgresqlSyntax.Ast.ImplicitRow where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Extras.NonEmpty as NonEmpty+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- implicit_row:+-- | '(' expr_list ',' a_expr ')'+-- @+data ImplicitRow = ImplicitRow ExprList AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ImplicitRow where+ toTextBuilder settings (ImplicitRow a b) = TextBuilders.renderInParens (toTextBuilder settings a <> ", " <> toTextBuilder settings b)++ -- Parses the shared @a_expr@ once and then decides, from what follows,+ -- whether it's the sole element of the leading 'ExprList' or the trailing+ -- @a_expr@ - see 'PostgresqlSyntax.Extras.NonEmpty.consAndUnsnoc'.+ parser settings = Parsers.inParens $ do+ a <- Parser.wrapToHead (parser settings)+ Parsers.commaSeparator+ b <- Parsers.sep1 Parsers.commaSeparator (parser settings)+ return $ case NonEmpty.consAndUnsnoc a b of+ (c, d) -> ImplicitRow (ExprList c) d++instance Qc.Arbitrary ImplicitRow where+ shrink = Qc.genericShrink+ arbitrary = ImplicitRow <$> arbitrary <*> Gens.downscale arbitrary
+ library-internal/PostgresqlSyntax/Ast/InExpr.hs view
@@ -0,0 +1,69 @@+module PostgresqlSyntax.Ast.InExpr+ ( InExpr (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc+import qualified Text.Megaparsec as Megaparsec++-- |+-- ==== References+-- @+-- in_expr:+-- | select_with_parens+-- | '(' expr_list ')'+-- @+data InExpr+ = SelectInExpr SelectWithParens+ | ExprListInExpr ExprList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst InExpr where+ toTextBuilder settings = \case+ SelectInExpr a -> toTextBuilder settings a+ ExprListInExpr a -> TextBuilders.renderInParens (toTextBuilder settings a)+ parser settings =+ (ExprListInExpr <$> Parser.parse (Megaparsec.try (Parser.toParsec (Parsers.inParens (parser settings)))))+ <|> (SelectInExpr <$> Parser.wrapToHead (parser settings))++instance Qc.Arbitrary InExpr where+ shrink = fmap canonicalize . Qc.genericShrink+ arbitrary =+ canonicalize+ <$> Qc.sized+ ( \n ->+ if n <= 1+ then ExprListInExpr <$> Qc.arbitrary+ else+ Qc.oneof+ [ SelectInExpr <$> Gens.downscale Qc.arbitrary,+ ExprListInExpr <$> Qc.arbitrary+ ]+ )++-- |+-- Collapses the non-canonical @SelectInExpr@-of-@WithParensSelectWithParens@+-- shape to the @ExprListInExpr@ shape the parser actually produces for it:+-- @in_expr@'s two productions, @select_with_parens@ and+-- @'(' expr_list ')'@, overlap whenever the parenthesised select itself+-- contains another parenthesised select, since a single-element+-- @expr_list@ can itself be that inner parenthesised select wrapped as a+-- @c_expr@. Both render to the same text; the parser's @expr_list@+-- alternative is tried first and so wins. Both 'arbitrary' and 'shrink'+-- can otherwise construct the non-canonical shape, which renders fine but+-- parses back to a different, canonical value and so breaks the+-- roundtrip property.+instance Canonicalizes InExpr where+ canonicalize = \case+ SelectInExpr a+ | Just inner <- project @SelectWithParens a ->+ ExprListInExpr (ExprList (embed @SelectWithParens inner :| []))+ other -> other
+ library-internal/PostgresqlSyntax/Ast/IndexElem.hs view
@@ -0,0 +1,58 @@+module PostgresqlSyntax.Ast.IndexElem where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AnyName+import PostgresqlSyntax.Ast.AscDesc+import PostgresqlSyntax.Ast.IndexElemDef+import PostgresqlSyntax.Ast.NullsOrder+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- index_elem:+-- | ColId opt_collate opt_class opt_asc_desc opt_nulls_order+-- | func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order+-- | '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order+-- @+--+-- @opt_collate@\/@opt_class@ are bare aliases to+-- 'PostgresqlSyntax.Ast.AnyName'.+data IndexElem = IndexElem IndexElemDef (Maybe AnyName) (Maybe AnyName) (Maybe AscDesc) (Maybe NullsOrder)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst IndexElem where+ toTextBuilder settings (IndexElem a b c d e) =+ toTextBuilder settings a+ <> TextBuilders.suffixMaybe collate b+ <> TextBuilders.suffixMaybe (toTextBuilder settings) c+ <> TextBuilders.suffixMaybe (toTextBuilder settings) d+ <> TextBuilders.suffixMaybe (toTextBuilder settings) e+ where+ collate = mappend "COLLATE " . toTextBuilder settings+ parser settings =+ IndexElem+ <$> (parser settings <* Parser.endHead)+ <*> optional (Parsers.space1 *> collate)+ <*> optional (Parsers.space1 *> class_)+ <*> optional (Parsers.space1 *> parser settings)+ <*> optional (Parsers.space1 *> parser settings)+ where+ collate = Parsers.keyword "collate" *> Parsers.space1 *> Parser.endHead *> parser settings++ class_ = parser settings++instance Qc.Arbitrary IndexElem where+ shrink = Qc.genericShrink+ arbitrary =+ IndexElem+ <$> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Qc.arbitrary+ <*> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/IndexElemDef.hs view
@@ -0,0 +1,46 @@+module PostgresqlSyntax.Ast.IndexElemDef where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.FuncExprWindowless+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 PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | ColId opt_collate opt_class opt_asc_desc opt_nulls_order+-- | func_expr_windowless opt_collate opt_class opt_asc_desc opt_nulls_order+-- | '(' a_expr ')' opt_collate opt_class opt_asc_desc opt_nulls_order+-- @+data IndexElemDef+ = IdIndexElemDef Ident+ | FuncIndexElemDef FuncExprWindowless+ | ExprIndexElemDef AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst IndexElemDef where+ toTextBuilder settings = \case+ IdIndexElemDef a -> toTextBuilder settings a+ FuncIndexElemDef a -> toTextBuilder settings a+ ExprIndexElemDef a -> TextBuilders.renderInParens (toTextBuilder settings a)+ parser settings =+ ExprIndexElemDef+ <$> Parsers.inParens (parser settings)+ <|> FuncIndexElemDef+ <$> parser settings+ <|> IdIndexElemDef+ <$> colId settings++instance Qc.Arbitrary IndexElemDef where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ IdIndexElemDef <$> Qc.arbitrary,+ FuncIndexElemDef <$> Qc.arbitrary,+ ExprIndexElemDef <$> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/IndexParams.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.IndexParams where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.IndexElem+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- index_params:+-- | index_elem+-- | index_params ',' index_elem+-- @+newtype IndexParams = IndexParams (NonEmpty IndexElem)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst IndexParams where+ toTextBuilder settings (IndexParams a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = IndexParams <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary IndexParams where+ shrink = Qc.genericShrink+ arbitrary = IndexParams <$> Gens.nonEmptyUpTo 4 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/Indirection.hs view
@@ -0,0 +1,36 @@+module PostgresqlSyntax.Ast.Indirection where++import Control.Applicative.Combinators.NonEmpty (some)+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.IndirectionEl+import qualified PostgresqlSyntax.Ast.IndirectionEl as IndirectionEl+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import PostgresqlSyntax.Prelude hiding (some)+import PostgresqlSyntax.Settings (Settings)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- indirection:+-- | indirection_el+-- | indirection indirection_el+-- @+newtype Indirection = Indirection (NonEmpty IndirectionEl)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Indirection where+ toTextBuilder settings (Indirection a) = foldMap (toTextBuilder settings) a+ parser settings = Indirection <$> some (parser settings)++-- |+-- Like 'parser', but built out of 'IndirectionEl.hsParser', so the Kronor+-- @.$name@ Haskell field selector is accepted (when the option is on).+-- Only used from the @$n@ parameter position - see+-- 'PostgresqlSyntax.Ast.CExpr'.+hsParser :: Settings -> Parser Indirection+hsParser settings = Indirection <$> some (IndirectionEl.hsParser settings)++instance Qc.Arbitrary Indirection where+ shrink = Qc.genericShrink+ arbitrary = Indirection <$> Gens.nonEmptyUpTo 4 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/IndirectionEl.hs view
@@ -0,0 +1,151 @@+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+ ]
+ library-internal/PostgresqlSyntax/Ast/InsertColumnItem.hs view
@@ -0,0 +1,32 @@+module PostgresqlSyntax.Ast.InsertColumnItem where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Indirection+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- insert_column_item:+-- | ColId opt_indirection+-- @+data InsertColumnItem = InsertColumnItem Ident (Maybe Indirection)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst InsertColumnItem where+ toTextBuilder settings (InsertColumnItem a b) = toTextBuilder settings a <> TextBuilders.suffixMaybe (toTextBuilder settings) b+ parser settings = do+ a <- colId settings+ Parser.endHead+ b <- optional (Parsers.space1 *> parser settings)+ return (InsertColumnItem a b)++instance Qc.Arbitrary InsertColumnItem where+ shrink = Qc.genericShrink+ arbitrary = InsertColumnItem <$> arbitrary <*> Gens.terminatingMaybe arbitrary
+ library-internal/PostgresqlSyntax/Ast/InsertColumnList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.InsertColumnList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.InsertColumnItem+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- insert_column_list:+-- | insert_column_item+-- | insert_column_list ',' insert_column_item+-- @+newtype InsertColumnList = InsertColumnList (NonEmpty InsertColumnItem)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst InsertColumnList where+ toTextBuilder settings (InsertColumnList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = InsertColumnList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary InsertColumnList where+ shrink = Qc.genericShrink+ arbitrary = InsertColumnList <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/InsertRest.hs view
@@ -0,0 +1,72 @@+module PostgresqlSyntax.Ast.InsertRest where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.InsertColumnList+import PostgresqlSyntax.Ast.OverrideKind+import PostgresqlSyntax.Ast.SelectStmt+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- insert_rest:+-- | SelectStmt+-- | OVERRIDING override_kind VALUE_P SelectStmt+-- | '(' insert_column_list ')' SelectStmt+-- | '(' insert_column_list ')' OVERRIDING override_kind VALUE_P SelectStmt+-- | DEFAULT VALUES+-- @+data InsertRest+ = SelectInsertRest (Maybe InsertColumnList) (Maybe OverrideKind) SelectStmt+ | DefaultValuesInsertRest+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst InsertRest where+ toTextBuilder settings = \case+ SelectInsertRest a b c ->+ TextBuilders.optLexemes+ [ fmap (TextBuilders.renderInParens . toTextBuilder settings) a,+ fmap insertRestOverriding b,+ Just (toTextBuilder settings c)+ ]+ DefaultValuesInsertRest -> "DEFAULT VALUES"+ where+ insertRestOverriding a = "OVERRIDING " <> toTextBuilder settings a <> " VALUE"+ parser settings =+ asum+ [ DefaultValuesInsertRest <$ (Parsers.keyword "default" *> Parsers.space1 *> Parser.endHead *> Parsers.keyword "values"),+ do+ -- 'Parser.wrapToHead' makes the whole parenthesized column list+ -- (including any 'Parser.endHead' calls inside 'InsertColumnItem')+ -- backtrackable as one unit. Without it, parsing e.g. @(VALUES ...)@+ -- would commit to treating @VALUES@ as a single-column+ -- 'InsertColumnList' entry (since it's a valid 'ColId') right after+ -- reading it, and fail instead of backtracking into the correct+ -- 'SelectStmt' (@select_with_parens@ wrapping a bare @VALUES@+ -- clause) parse below. See gram.y's @insert_rest@.+ a <- optional (Parser.wrapToHead (Parsers.inParens (parser settings) <* Parsers.space1))+ b <- optional $ do+ Parsers.keyword "overriding"+ Parsers.space1+ Parser.endHead+ b <- parser settings+ Parsers.space1+ Parsers.keyword "value"+ Parsers.space1+ return b+ c <- parser settings+ return (SelectInsertRest a b c)+ ]++instance Qc.Arbitrary InsertRest where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ SelectInsertRest <$> Gens.terminatingMaybe Qc.arbitrary <*> Qc.arbitrary <*> Qc.arbitrary,+ pure DefaultValuesInsertRest+ ]
+ library-internal/PostgresqlSyntax/Ast/InsertStmt.hs view
@@ -0,0 +1,57 @@+module PostgresqlSyntax.Ast.InsertStmt where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.InsertRest+import PostgresqlSyntax.Ast.InsertTarget+import PostgresqlSyntax.Ast.OnConflict+import PostgresqlSyntax.Ast.ReturningClause+import {-# SOURCE #-} PostgresqlSyntax.Ast.WithClause (WithClause)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- InsertStmt:+-- | opt_with_clause INSERT INTO insert_target insert_rest+-- opt_on_conflict returning_clause+-- @+data InsertStmt = InsertStmt (Maybe WithClause) InsertTarget InsertRest (Maybe OnConflict) (Maybe ReturningClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst InsertStmt where+ toTextBuilder settings (InsertStmt a b c d e) =+ TextBuilders.prefixMaybe (toTextBuilder settings) a+ <> "INSERT INTO "+ <> toTextBuilder settings b+ <> " "+ <> toTextBuilder settings c+ <> TextBuilders.suffixMaybe (toTextBuilder settings) d+ <> TextBuilders.suffixMaybe (toTextBuilder settings) e+ parser settings = do+ a <- optional (Parser.wrapToHead (parser settings) <* Parsers.space1)+ Parsers.keyword "insert"+ Parsers.space1+ Parser.endHead+ Parsers.keyword "into"+ Parsers.space1+ b <- parser settings+ Parsers.space1+ c <- parser settings+ d <- optional (Parsers.space1 *> parser settings)+ e <- optional (Parsers.space1 *> parser settings)+ return (InsertStmt a b c d e)++instance Qc.Arbitrary InsertStmt where+ shrink = Qc.genericShrink+ arbitrary =+ InsertStmt+ <$> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary)+ <*> Qc.arbitrary+ <*> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/InsertTarget.hs view
@@ -0,0 +1,31 @@+module PostgresqlSyntax.Ast.InsertTarget where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.QualifiedName+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- insert_target:+-- | qualified_name+-- | qualified_name AS ColId+-- @+data InsertTarget = InsertTarget QualifiedName (Maybe Ident)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst InsertTarget where+ toTextBuilder settings (InsertTarget a b) = toTextBuilder settings a <> foldMap (mappend " AS " . toTextBuilder settings) b+ parser settings = do+ a <- parser settings+ Parser.endHead+ b <- optional (Parsers.space1 *> Parsers.keyword "as" *> Parsers.space1 *> Parser.endHead *> colId settings)+ return (InsertTarget a b)++instance Qc.Arbitrary InsertTarget where+ shrink = Qc.genericShrink+ arbitrary = InsertTarget <$> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/Interval.hs view
@@ -0,0 +1,94 @@+module PostgresqlSyntax.Ast.Interval where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.IntervalSecond+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_interval:+-- | YEAR_P+-- | MONTH_P+-- | DAY_P+-- | HOUR_P+-- | MINUTE_P+-- | interval_second+-- | YEAR_P TO MONTH_P+-- | DAY_P TO HOUR_P+-- | DAY_P TO MINUTE_P+-- | DAY_P TO interval_second+-- | HOUR_P TO MINUTE_P+-- | HOUR_P TO interval_second+-- | MINUTE_P TO interval_second+-- | EMPTY+-- @+data Interval+ = YearInterval+ | MonthInterval+ | DayInterval+ | HourInterval+ | MinuteInterval+ | SecondInterval IntervalSecond+ | YearToMonthInterval+ | DayToHourInterval+ | DayToMinuteInterval+ | DayToSecondInterval IntervalSecond+ | HourToMinuteInterval+ | HourToSecondInterval IntervalSecond+ | MinuteToSecondInterval IntervalSecond+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Interval where+ toTextBuilder settings = \case+ YearInterval -> "YEAR"+ MonthInterval -> "MONTH"+ DayInterval -> "DAY"+ HourInterval -> "HOUR"+ MinuteInterval -> "MINUTE"+ SecondInterval a -> toTextBuilder settings a+ YearToMonthInterval -> "YEAR TO MONTH"+ DayToHourInterval -> "DAY TO HOUR"+ DayToMinuteInterval -> "DAY TO MINUTE"+ DayToSecondInterval a -> "DAY TO " <> toTextBuilder settings a+ HourToMinuteInterval -> "HOUR TO MINUTE"+ HourToSecondInterval a -> "HOUR TO " <> toTextBuilder settings a+ MinuteToSecondInterval a -> "MINUTE TO " <> toTextBuilder settings a+ parser settings =+ asum+ [ YearToMonthInterval <$ Parsers.keyphrase "year to month",+ DayToHourInterval <$ Parsers.keyphrase "day to hour",+ DayToMinuteInterval <$ Parsers.keyphrase "day to minute",+ DayToSecondInterval <$> (Parsers.keyphrase "day to" *> Parsers.space1 *> Parser.endHead *> parser settings),+ HourToMinuteInterval <$ Parsers.keyphrase "hour to minute",+ HourToSecondInterval <$> (Parsers.keyphrase "hour to" *> Parsers.space1 *> Parser.endHead *> parser settings),+ MinuteToSecondInterval <$> (Parsers.keyphrase "minute to" *> Parsers.space1 *> Parser.endHead *> parser settings),+ YearInterval <$ Parsers.keyword "year",+ MonthInterval <$ Parsers.keyword "month",+ DayInterval <$ Parsers.keyword "day",+ HourInterval <$ Parsers.keyword "hour",+ MinuteInterval <$ Parsers.keyword "minute",+ SecondInterval <$> parser settings+ ]++instance Qc.Arbitrary Interval where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ pure YearInterval,+ pure MonthInterval,+ pure DayInterval,+ pure HourInterval,+ pure MinuteInterval,+ SecondInterval <$> Qc.arbitrary,+ pure YearToMonthInterval,+ pure DayToHourInterval,+ pure DayToMinuteInterval,+ DayToSecondInterval <$> Qc.arbitrary,+ pure HourToMinuteInterval,+ HourToSecondInterval <$> Qc.arbitrary,+ MinuteToSecondInterval <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/IntervalSecond.hs view
@@ -0,0 +1,36 @@+module PostgresqlSyntax.Ast.IntervalSecond where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- interval_second:+-- | SECOND_P+-- | SECOND_P '(' Iconst ')'+-- @+newtype IntervalSecond = IntervalSecond (Maybe Int64)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst IntervalSecond where+ toTextBuilder _settings (IntervalSecond a) = case a of+ Nothing -> "SECOND"+ Just a' -> "SECOND " <> TextBuilders.renderInParens (TextBuilder.int64Dec a')+ parser _settings = do+ Parsers.keyword "second"+ a <- optional (Parsers.space *> Parsers.inParens Parsers.decimal)+ return (IntervalSecond a)++instance Qc.Arbitrary IntervalSecond where+ shrink = Qc.genericShrink+ arbitrary = IntervalSecond <$> Qc.oneof [pure Nothing, Just <$> nonNegative]+ where+ nonNegative = Qc.sized (\n -> Qc.choose (0, cap n))+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/IntoClause.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.IntoClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.OptTempTableName+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- into_clause:+-- | INTO OptTempTableName+-- | /*EMPTY*/+-- @+newtype IntoClause = IntoClause OptTempTableName+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst IntoClause where+ toTextBuilder settings (IntoClause a) = "INTO " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "into"+ Parser.endHead+ Parsers.space1+ IntoClause <$> parser settings++instance Qc.Arbitrary IntoClause where+ shrink = Qc.genericShrink+ arbitrary = IntoClause <$> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/JoinQual.hs view
@@ -0,0 +1,40 @@+module PostgresqlSyntax.Ast.JoinQual where++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 PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- join_qual:+-- | USING '(' name_list ')'+-- | ON a_expr+-- @+data JoinQual+ = UsingJoinQual (NonEmpty Ident)+ | OnJoinQual AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst JoinQual where+ toTextBuilder settings = \case+ UsingJoinQual a -> "USING (" <> TextBuilders.commaNonEmpty (toTextBuilder settings) a <> ")"+ OnJoinQual a -> "ON " <> toTextBuilder settings a+ parser settings =+ asum+ [ Parsers.keyword "using" *> Parsers.space1 *> Parsers.inParens (Parsers.sep1 Parsers.commaSeparator (colId settings)) <&> UsingJoinQual,+ Parsers.keyword "on" *> Parsers.space1 *> parser settings <&> OnJoinQual+ ]++instance Qc.Arbitrary JoinQual where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ UsingJoinQual <$> Gens.nonEmptyUpTo 7 Qc.arbitrary,+ OnJoinQual <$> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/JoinType.hs view
@@ -0,0 +1,60 @@+module PostgresqlSyntax.Ast.JoinType where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | FULL join_outer+-- | LEFT join_outer+-- | RIGHT join_outer+-- | INNER_P+-- @+data JoinType+ = FullJoinType Bool+ | LeftJoinType Bool+ | RightJoinType Bool+ | InnerJoinType+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst JoinType where+ toTextBuilder _settings = \case+ FullJoinType a -> "FULL" <> if a then " OUTER" else ""+ LeftJoinType a -> "LEFT" <> if a then " OUTER" else ""+ RightJoinType a -> "RIGHT" <> if a then " OUTER" else ""+ InnerJoinType -> "INNER"+ parser _settings =+ asum+ [ do+ Parsers.keyword "full"+ Parser.endHead+ outer <- outerAfterSpace+ return (FullJoinType outer),+ do+ Parsers.keyword "left"+ Parser.endHead+ outer <- outerAfterSpace+ return (LeftJoinType outer),+ do+ Parsers.keyword "right"+ Parser.endHead+ outer <- outerAfterSpace+ return (RightJoinType outer),+ Parsers.keyword "inner" $> InnerJoinType+ ]+ where+ outerAfterSpace = (Parsers.space1 *> Parsers.keyword "outer") $> True <|> pure False++instance Qc.Arbitrary JoinType where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ FullJoinType <$> Qc.arbitrary,+ LeftJoinType <$> Qc.arbitrary,+ RightJoinType <$> Qc.arbitrary,+ pure InnerJoinType+ ]
+ library-internal/PostgresqlSyntax/Ast/JoinedTable.hs view
@@ -0,0 +1,141 @@+{-# OPTIONS_GHC -Wno-orphans #-}++module PostgresqlSyntax.Ast.JoinedTable+ ( JoinedTable (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.JoinQual+import PostgresqlSyntax.Ast.JoinType+import {-# SOURCE #-} PostgresqlSyntax.Ast.TableRef (TableRef)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | '(' joined_table ')'+-- | table_ref CROSS JOIN table_ref+-- | table_ref join_type JOIN table_ref join_qual+-- | table_ref JOIN table_ref join_qual+-- | table_ref NATURAL join_type JOIN table_ref+-- | table_ref NATURAL JOIN table_ref+-- @+data JoinedTable+ = InParensJoinedTable JoinedTable+ | CrossJoinedTable TableRef TableRef+ | QualJoinedTable TableRef (Maybe JoinType) TableRef JoinQual+ | NaturalJoinedTable TableRef (Maybe JoinType) TableRef+ deriving (Show, Generic, Eq, Ord, Data)++-- |+-- Parsing delegates to 'parseExtended' over the 'Extends' instance+-- below - a bare @table_ref@ parse is greedy, absorbing any trailing @CROSS+-- JOIN@\/@JOIN@\/@NATURAL JOIN@ continuation into itself, so a+-- @joined_table@ is never reachable as a bare, zero-extension 'TableRef';+-- it always needs at least one. Failing that, the only remaining+-- @joined_table@ production is the non-left-recursive 'parseBase'.+instance IsAst JoinedTable where+ toTextBuilder settings = \case+ InParensJoinedTable a -> TextBuilders.renderInParens (toTextBuilder settings a)+ CrossJoinedTable a b -> toTextBuilder settings a <> " CROSS JOIN " <> toTextBuilder settings b+ QualJoinedTable a b c d -> toTextBuilder settings a <> TextBuilders.suffixMaybe (toTextBuilder settings) b <> " JOIN " <> toTextBuilder settings c <> " " <> toTextBuilder settings d+ NaturalJoinedTable a b c -> toTextBuilder settings a <> " NATURAL" <> TextBuilders.suffixMaybe (toTextBuilder settings) b <> " JOIN " <> toTextBuilder settings c++ parser settings = parseExtended @TableRef settings <|> parseBase settings++-- |+-- The one @joined_table@ production that doesn't begin with a+-- left-recursive @table_ref@:+--+-- @+-- | '(' joined_table ')'+-- @+--+-- It still recurses - just not on the left, since the opening parenthesis+-- has to be consumed first. "PostgresqlSyntax.Ast.TableRef" reaches it+-- through this class method, which is why 'JoinedTable' needs no helper+-- export.+instance LeftRecursive JoinedTable where+ parseBase settings = InParensJoinedTable <$> Parsers.inParens (parser settings)++instance Qc.Arbitrary JoinedTable where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then joined+ else Qc.oneof [InParensJoinedTable <$> Gens.downscale Qc.arbitrary, joined]+ where+ joined =+ Qc.oneof+ [ CrossJoinedTable <$> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ QualJoinedTable <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary <*> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ NaturalJoinedTable <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary <*> Gens.downscale Qc.arbitrary+ ]++-- |+-- The left-recursion-eliminated form of @table_ref@\/@joined_table@: a+-- 'TableRef' is the non-recursive base (@β@, its own 'parseBase'). All+-- three join kinds sit at the same precedence (@%left JOIN CROSS LEFT FULL+-- RIGHT INNER_P NATURAL@ in @gram.y@), and there's nothing to hold between+-- parsing a join and applying it, so no item type is warranted here -+-- unlike "PostgresqlSyntax.Ast.SimpleSelect", this hub isn't collect-then-fold.+instance Extends TableRef JoinedTable where+ -- ==== References+ -- @+ -- | table_ref CROSS JOIN table_ref+ -- | table_ref join_type JOIN table_ref join_qual+ -- | table_ref JOIN table_ref join_qual+ -- | table_ref NATURAL join_type JOIN table_ref+ -- | table_ref NATURAL JOIN table_ref+ -- @+ --+ -- Parses one join onto 'tr1', then recurses with the built 'JoinedTable'+ -- (embedded back to 'TableRef') as the new left operand, falling back to+ -- what's already built when no further join follows. The+ -- 'Parser.wrapToHead'\/'Parser.endHead' pair around the single join+ -- mirrors 'PostgresqlSyntax.Algebra.parseExtensionChain'\'s per-item+ -- protocol, since this recursive shape can't build on that combinator+ -- directly.+ parseExtensions settings tr1 = do+ built <- Parser.wrapToHead parseOneJoin+ Parser.endHead+ optional (parseExtensions @TableRef settings (embed built)) >>= pure . maybe built id+ where+ parseOneJoin =+ Parsers.space1+ *> asum+ [ do+ Parsers.keyphrase "cross join"+ Parser.endHead+ Parsers.space1+ tr2 <- parseBase @TableRef settings+ return (CrossJoinedTable tr1 tr2),+ do+ jt <- joinTypedJoin+ Parser.endHead+ Parsers.space1+ tr2 <- parser settings+ Parsers.space1+ jq <- parser settings+ return (QualJoinedTable tr1 jt tr2 jq),+ do+ Parsers.keyword "natural"+ Parser.endHead+ Parsers.space1+ jt <- joinTypedJoin+ Parsers.space1+ tr2 <- parseBase @TableRef settings+ return (NaturalJoinedTable tr1 jt tr2)+ ]+ joinTypedJoin =+ Just+ <$> (parser settings <* Parser.endHead <* Parsers.space1 <* Parsers.keyword "join")+ <|> Nothing+ <$ Parsers.keyword "join"
+ library-internal/PostgresqlSyntax/Ast/JoinedTable.hs-boot view
@@ -0,0 +1,3 @@+module PostgresqlSyntax.Ast.JoinedTable where++data JoinedTable
+ library-internal/PostgresqlSyntax/Ast/LimitClause.hs view
@@ -0,0 +1,100 @@+module PostgresqlSyntax.Ast.LimitClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.SelectFetchFirstValue+import PostgresqlSyntax.Ast.SelectLimitValue+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- limit_clause:+-- | LIMIT select_limit_value+-- | LIMIT select_limit_value ',' select_offset_value+-- | FETCH first_or_next select_fetch_first_value row_or_rows ONLY+-- | FETCH first_or_next row_or_rows ONLY+-- select_offset_value:+-- | a_expr+-- first_or_next:+-- | FIRST_P+-- | NEXT+-- row_or_rows:+-- | ROW+-- | ROWS+-- @+data LimitClause+ = LimitLimitClause SelectLimitValue (Maybe AExpr)+ | FetchOnlyLimitClause Bool (Maybe SelectFetchFirstValue) Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst LimitClause where+ toTextBuilder settings = \case+ LimitLimitClause a b -> "LIMIT " <> toTextBuilder settings a <> foldMap (mappend ", " . toTextBuilder settings) b+ FetchOnlyLimitClause a b c ->+ TextBuilders.optLexemes+ [ Just "FETCH",+ Just (firstOrNext a),+ fmap (toTextBuilder settings) b,+ Just (rowOrRows c),+ Just "ONLY"+ ]+ where+ firstOrNext = bool "FIRST" "NEXT"+ rowOrRows = bool "ROW" "ROWS"+ parser settings =+ ( do+ Parsers.keyword "limit"+ Parser.endHead+ Parsers.space1+ a <- parser settings+ b <- optional $ do+ Parsers.commaSeparator+ parser settings+ return (LimitLimitClause a b)+ )+ <|> ( do+ Parsers.keyword "fetch"+ Parser.endHead+ Parsers.space1+ a <- firstOrNext+ Parsers.space1+ asum+ [ do+ b <- rowOrRows+ Parsers.space1+ Parsers.keyword "only"+ return (FetchOnlyLimitClause a Nothing b),+ do+ b <- parser settings+ Parsers.space1+ c <- rowOrRows+ Parsers.space1+ Parsers.keyword "only"+ return (FetchOnlyLimitClause a (Just b) c)+ ]+ )+ where+ firstOrNext =+ False+ <$ Parsers.keyword "first"+ <|> True+ <$ Parsers.keyword "next"+ rowOrRows =+ True+ <$ Parsers.keyword "rows"+ <|> False+ <$ Parsers.keyword "row"++instance Qc.Arbitrary LimitClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ LimitLimitClause <$> Qc.arbitrary <*> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary),+ FetchOnlyLimitClause <$> Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/MathOp.hs view
@@ -0,0 +1,76 @@+module PostgresqlSyntax.Ast.MathOp where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude hiding (many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- MathOp:+-- | '+'+-- | '-'+-- | '*'+-- | '/'+-- | '%'+-- | '^'+-- | '<'+-- | '>'+-- | '='+-- | LESS_EQUALS+-- | GREATER_EQUALS+-- | NOT_EQUALS+-- @+data MathOp+ = PlusMathOp+ | MinusMathOp+ | AsteriskMathOp+ | SlashMathOp+ | PercentMathOp+ | ArrowUpMathOp+ | ArrowLeftMathOp+ | ArrowRightMathOp+ | EqualsMathOp+ | LessEqualsMathOp+ | GreaterEqualsMathOp+ | ArrowLeftArrowRightMathOp+ | ExclamationEqualsMathOp+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst MathOp where+ toTextBuilder _settings = \case+ PlusMathOp -> TextBuilder.char7 '+'+ MinusMathOp -> TextBuilder.char7 '-'+ AsteriskMathOp -> TextBuilder.char7 '*'+ SlashMathOp -> TextBuilder.char7 '/'+ PercentMathOp -> TextBuilder.char7 '%'+ ArrowUpMathOp -> TextBuilder.char7 '^'+ ArrowLeftMathOp -> TextBuilder.char7 '<'+ ArrowRightMathOp -> TextBuilder.char7 '>'+ EqualsMathOp -> TextBuilder.char7 '='+ LessEqualsMathOp -> "<="+ GreaterEqualsMathOp -> ">="+ ArrowLeftArrowRightMathOp -> "<>"+ ExclamationEqualsMathOp -> "!="+ parser _settings =+ asum+ [ ArrowLeftArrowRightMathOp <$ Parsers.string' "<>",+ GreaterEqualsMathOp <$ Parsers.string' ">=",+ ExclamationEqualsMathOp <$ Parsers.string' "!=",+ LessEqualsMathOp <$ Parsers.string' "<=",+ PlusMathOp <$ Parsers.char '+',+ MinusMathOp <$ Parsers.char '-',+ AsteriskMathOp <$ Parsers.char '*',+ SlashMathOp <$ Parsers.char '/',+ PercentMathOp <$ Parsers.char '%',+ ArrowUpMathOp <$ Parsers.char '^',+ ArrowLeftMathOp <$ Parsers.char '<',+ ArrowRightMathOp <$ Parsers.char '>',+ EqualsMathOp <$ Parsers.char '='+ ]++instance Qc.Arbitrary MathOp where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/NameList.hs view
@@ -0,0 +1,43 @@+module PostgresqlSyntax.Ast.NameList where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+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 qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- name_list:+-- | name+-- | name_list ',' name+-- @+--+-- 'Name' is a bare alias to 'PostgresqlSyntax.Ast.ColId' which is a bare+-- alias to 'Ident', but the @ColId@ /parser/ (kept in "PostgresqlSyntax.Parsing"+-- since @ColId@ itself isn't extracted in this batch) is more permissive+-- than plain 'Ident': it additionally accepts the @unreserved_keyword@ and+-- @col_name_keyword@ lexical classes as identifiers. Since this module sits+-- below "PostgresqlSyntax.Parsing" (no import cycle allowed), that+-- ColId-flavored element parser is duplicated here (mirroring @colId@'s+-- definition) rather than reused, to preserve exact parsing behavior.+newtype NameList = NameList (NonEmpty Ident)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst NameList where+ toTextBuilder settings (NameList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = NameList <$> Parsers.sep1 Parsers.commaSeparator colIdLikeName+ where+ colIdLikeName =+ Parser.label "identifier" $+ parser settings+ <|> Parsers.keywordNameFromSet UnquotedIdent (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword)++instance Qc.Arbitrary NameList where+ shrink = Qc.genericShrink+ arbitrary = NameList <$> Gens.nonEmptyUpTo 7 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/NullsOrder.hs view
@@ -0,0 +1,28 @@+module PostgresqlSyntax.Ast.NullsOrder where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_nulls_order:+-- | NULLS_LA FIRST_P+-- | NULLS_LA LAST_P+-- | EMPTY+-- @+data NullsOrder = FirstNullsOrder | LastNullsOrder+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst NullsOrder where+ toTextBuilder _settings = \case+ FirstNullsOrder -> "NULLS FIRST"+ LastNullsOrder -> "NULLS LAST"+ parser _settings = Parsers.keyword "nulls" *> Parsers.space1 *> Parser.endHead *> (FirstNullsOrder <$ Parsers.keyword "first" <|> LastNullsOrder <$ Parsers.keyword "last")++instance Qc.Arbitrary NullsOrder where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/Numeric.hs view
@@ -0,0 +1,99 @@+module PostgresqlSyntax.Ast.Numeric where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- Numeric:+-- | INT_P+-- | INTEGER+-- | SMALLINT+-- | BIGINT+-- | REAL+-- | FLOAT_P opt_float+-- | DOUBLE_P PRECISION+-- | DECIMAL_P opt_type_modifiers+-- | DEC opt_type_modifiers+-- | NUMERIC opt_type_modifiers+-- | BOOLEAN_P+-- opt_float:+-- | '(' Iconst ')'+-- | EMPTY+-- opt_type_modifiers:+-- | '(' expr_list ')'+-- | EMPTY+-- @+data Numeric+ = IntNumeric+ | IntegerNumeric+ | SmallintNumeric+ | BigintNumeric+ | RealNumeric+ | FloatNumeric (Maybe Int64)+ | DoublePrecisionNumeric+ | DecimalNumeric (Maybe ExprList)+ | DecNumeric (Maybe ExprList)+ | NumericNumeric (Maybe ExprList)+ | BooleanNumeric+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Numeric where+ toTextBuilder settings = \case+ IntNumeric -> "INT"+ IntegerNumeric -> "INTEGER"+ SmallintNumeric -> "SMALLINT"+ BigintNumeric -> "BIGINT"+ RealNumeric -> "REAL"+ FloatNumeric a -> "FLOAT" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . TextBuilder.int64Dec) a+ DoublePrecisionNumeric -> "DOUBLE PRECISION"+ DecimalNumeric a -> "DECIMAL" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . toTextBuilder settings) a+ DecNumeric a -> "DEC" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . toTextBuilder settings) a+ NumericNumeric a -> "NUMERIC" <> TextBuilders.suffixMaybe (TextBuilders.renderInParens . toTextBuilder settings) a+ BooleanNumeric -> "BOOLEAN"+ parser settings =+ asum+ [ IntegerNumeric <$ Parsers.keyword "integer",+ IntNumeric <$ Parsers.keyword "int",+ SmallintNumeric <$ Parsers.keyword "smallint",+ BigintNumeric <$ Parsers.keyword "bigint",+ RealNumeric <$ Parsers.keyword "real",+ FloatNumeric <$> (Parsers.keyword "float" *> Parser.endHead *> optional (Parsers.space *> Parsers.inParens Parsers.decimal)),+ DoublePrecisionNumeric <$ Parsers.keyphrase "double precision",+ DecimalNumeric <$> (Parsers.keyword "decimal" *> Parser.endHead *> optional (Parsers.space *> Parsers.inParens (parser settings))),+ DecNumeric <$> (Parsers.keyword "dec" *> Parser.endHead *> optional (Parsers.space *> Parsers.inParens (parser settings))),+ NumericNumeric <$> (Parsers.keyword "numeric" *> Parser.endHead *> optional (Parsers.space *> Parsers.inParens (parser settings))),+ BooleanNumeric <$ Parsers.keyword "boolean"+ ]++instance Qc.Arbitrary Numeric where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ pure IntNumeric,+ pure IntegerNumeric,+ pure SmallintNumeric,+ pure BigintNumeric,+ pure RealNumeric,+ -- The @Iconst@ here is parsed via 'Parsers.decimal' (unsigned), so,+ -- unlike a plain 'Int64', it must never be negative - mirroring+ -- 'PostgresqlSyntax.Ast.IntervalSecond'\'s own @nonNegative@.+ FloatNumeric <$> Qc.oneof [pure Nothing, Just <$> nonNegativeInt64],+ pure DoublePrecisionNumeric,+ DecimalNumeric <$> Qc.arbitrary,+ DecNumeric <$> Qc.arbitrary,+ NumericNumeric <$> Qc.arbitrary,+ pure BooleanNumeric+ ]+ where+ nonNegativeInt64 = Qc.sized (\n -> Qc.choose (0, cap n))+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/OffsetClause.hs view
@@ -0,0 +1,56 @@+module PostgresqlSyntax.Ast.OffsetClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.SelectFetchFirstValue+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- offset_clause:+-- | OFFSET select_offset_value+-- | OFFSET select_fetch_first_value row_or_rows+-- select_offset_value:+-- | a_expr+-- row_or_rows:+-- | ROW+-- | ROWS+-- @+data OffsetClause+ = ExprOffsetClause AExpr+ | FetchFirstOffsetClause SelectFetchFirstValue Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OffsetClause where+ toTextBuilder settings = \case+ ExprOffsetClause a -> "OFFSET " <> toTextBuilder settings a+ FetchFirstOffsetClause a b -> "OFFSET " <> toTextBuilder settings a <> " " <> rowOrRows b+ where+ rowOrRows = bool "ROW" "ROWS"+ parser settings = do+ Parsers.keyword "offset"+ Parser.endHead+ Parsers.space1+ asum+ [ FetchFirstOffsetClause <$> Parser.wrapToHead (parser settings) <*> (Parsers.space1 *> rowOrRows),+ ExprOffsetClause <$> parser settings+ ]+ where+ rowOrRows =+ True+ <$ Parsers.keyword "rows"+ <|> False+ <$ Parsers.keyword "row"++instance Qc.Arbitrary OffsetClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExprOffsetClause <$> Gens.downscale Qc.arbitrary,+ FetchFirstOffsetClause <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/OnConflict.hs view
@@ -0,0 +1,40 @@+module PostgresqlSyntax.Ast.OnConflict where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ConfExpr+import PostgresqlSyntax.Ast.OnConflictDo+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_on_conflict:+-- | ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list where_clause+-- | ON CONFLICT opt_conf_expr DO NOTHING+-- | EMPTY+-- @+data OnConflict = OnConflict (Maybe ConfExpr) OnConflictDo+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OnConflict where+ toTextBuilder settings (OnConflict a b) = "ON CONFLICT" <> TextBuilders.suffixMaybe (toTextBuilder settings) a <> " DO " <> toTextBuilder settings b+ parser settings = do+ Parsers.keyword "on"+ Parsers.space1+ Parsers.keyword "conflict"+ Parsers.space1+ Parser.endHead+ a <- optional (parser settings <* Parsers.space1)+ Parsers.keyword "do"+ Parsers.space1+ b <- parser settings+ return (OnConflict a b)++instance Qc.Arbitrary OnConflict where+ shrink = Qc.genericShrink+ arbitrary = OnConflict <$> Gens.terminatingMaybe arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/OnConflictDo.hs view
@@ -0,0 +1,57 @@+module PostgresqlSyntax.Ast.OnConflictDo where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.SetClauseList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_on_conflict:+-- | ON CONFLICT opt_conf_expr DO UPDATE SET set_clause_list where_clause+-- | ON CONFLICT opt_conf_expr DO NOTHING+-- | EMPTY+-- @+--+-- @where_clause@ is inlined here as a bare 'PostgresqlSyntax.Ast.AExpr'+-- rather than going through 'PostgresqlSyntax.Ast.WhereClause'.+data OnConflictDo+ = UpdateOnConflictDo SetClauseList (Maybe AExpr)+ | NothingOnConflictDo+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OnConflictDo where+ toTextBuilder settings = \case+ UpdateOnConflictDo a b -> "UPDATE SET " <> toTextBuilder settings a <> TextBuilders.suffixMaybe whereClause b+ NothingOnConflictDo -> "NOTHING"+ where+ whereClause a = "WHERE " <> toTextBuilder settings a+ parser settings =+ asum+ [ NothingOnConflictDo <$ Parsers.keyword "nothing",+ do+ Parsers.keyword "update"+ Parsers.space1+ Parser.endHead+ Parsers.keyword "set"+ Parsers.space1+ a <- parser settings+ b <- optional (Parsers.space1 *> whereClause)+ return (UpdateOnConflictDo a b)+ ]+ where+ whereClause = Parsers.keyword "where" *> Parsers.space1 *> Parser.endHead *> parser settings++instance Qc.Arbitrary OnConflictDo where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ UpdateOnConflictDo <$> Qc.arbitrary <*> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary),+ pure NothingOnConflictDo+ ]
+ library-internal/PostgresqlSyntax/Ast/Op.hs view
@@ -0,0 +1,36 @@+module PostgresqlSyntax.Ast.Op where++import qualified Data.Text as Text+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.Shrinks as Shrinks+import qualified PostgresqlSyntax.Predicate as Predicate+import PostgresqlSyntax.Prelude+import qualified PostgresqlSyntax.Validation as Validation+import qualified Test.QuickCheck as Qc+import qualified TextBuilder++-- |+-- ==== References+-- @+-- Op+-- @+newtype Op = Op Text+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Op where+ toTextBuilder _settings (Op a) = TextBuilder.text a+ parser _settings = do+ a <- Parsers.takeWhile1P Nothing Predicate.opChar+ case Validation.op a of+ Nothing -> return (Op a)+ Just err -> fail (Text.unpack err)++instance Qc.Arbitrary Op where+ shrink (Op a) = Op <$> filter (isNothing . Validation.op) (Shrinks.nonEmptyText a)+ arbitrary = Op <$> genOpText `Qc.suchThat` (isNothing . Validation.op)+ where+ genOpText = do+ len <- Qc.choose (1, 7)+ Text.pack <$> Qc.vectorOf len (Qc.elements opChars)+ opChars = "+-*/<>=~!@#%^&|`?"
+ library-internal/PostgresqlSyntax/Ast/OptOrdinality.hs view
@@ -0,0 +1,24 @@+module PostgresqlSyntax.Ast.OptOrdinality where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_ordinality:+-- | WITH_LA ORDINALITY+-- | EMPTY+-- @+newtype OptOrdinality = OptOrdinality Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OptOrdinality where+ toTextBuilder _settings (OptOrdinality a) = if a then "WITH ORDINALITY" else mempty+ parser _settings = OptOrdinality <$> Parsers.trueIfPresent (Parsers.keyword "with" *> Parsers.space1 *> Parsers.keyword "ordinality")++instance Qc.Arbitrary OptOrdinality where+ shrink = Qc.genericShrink+ arbitrary = OptOrdinality <$> arbitrary
+ library-internal/PostgresqlSyntax/Ast/OptTempTableName.hs view
@@ -0,0 +1,85 @@+module PostgresqlSyntax.Ast.OptTempTableName where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.QualifiedName+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- OptTempTableName:+-- | TEMPORARY opt_table qualified_name+-- | TEMP opt_table qualified_name+-- | LOCAL TEMPORARY opt_table qualified_name+-- | LOCAL TEMP opt_table qualified_name+-- | GLOBAL TEMPORARY opt_table qualified_name+-- | GLOBAL TEMP opt_table qualified_name+-- | UNLOGGED opt_table qualified_name+-- | TABLE qualified_name+-- | qualified_name+-- @+data OptTempTableName+ = TemporaryOptTempTableName Bool QualifiedName+ | TempOptTempTableName Bool QualifiedName+ | LocalTemporaryOptTempTableName Bool QualifiedName+ | LocalTempOptTempTableName Bool QualifiedName+ | GlobalTemporaryOptTempTableName Bool QualifiedName+ | GlobalTempOptTempTableName Bool QualifiedName+ | UnloggedOptTempTableName Bool QualifiedName+ | TableOptTempTableName QualifiedName+ | QualifedOptTempTableName QualifiedName+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OptTempTableName where+ toTextBuilder settings = \case+ TemporaryOptTempTableName a b -> TextBuilders.optLexemes [Just "TEMPORARY", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ TempOptTempTableName a b -> TextBuilders.optLexemes [Just "TEMP", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ LocalTemporaryOptTempTableName a b -> TextBuilders.optLexemes [Just "LOCAL TEMPORARY", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ LocalTempOptTempTableName a b -> TextBuilders.optLexemes [Just "LOCAL TEMP", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ GlobalTemporaryOptTempTableName a b -> TextBuilders.optLexemes [Just "GLOBAL TEMPORARY", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ GlobalTempOptTempTableName a b -> TextBuilders.optLexemes [Just "GLOBAL TEMP", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ UnloggedOptTempTableName a b -> TextBuilders.optLexemes [Just "UNLOGGED", bool Nothing (Just "TABLE") a, Just (toTextBuilder settings b)]+ TableOptTempTableName a -> "TABLE " <> toTextBuilder settings a+ QualifedOptTempTableName a -> toTextBuilder settings a+ parser settings =+ asum+ [ do+ a <-+ asum+ [ TemporaryOptTempTableName <$ Parsers.keyword "temporary" <* Parsers.space1,+ TempOptTempTableName <$ Parsers.keyword "temp" <* Parsers.space1,+ LocalTemporaryOptTempTableName <$ Parsers.keyphrase "local temporary" <* Parsers.space1,+ LocalTempOptTempTableName <$ Parsers.keyphrase "local temp" <* Parsers.space1,+ GlobalTemporaryOptTempTableName <$ Parsers.keyphrase "global temporary" <* Parsers.space1,+ GlobalTempOptTempTableName <$ Parsers.keyphrase "global temp" <* Parsers.space1,+ UnloggedOptTempTableName <$ Parsers.keyword "unlogged" <* Parsers.space1+ ]+ b <- option False (True <$ Parsers.keyword "table" <* Parsers.space1)+ c <- parser settings+ return (a b c),+ do+ Parsers.keyword "table"+ Parsers.space1+ Parser.endHead+ TableOptTempTableName <$> parser settings,+ QualifedOptTempTableName <$> parser settings+ ]++instance Qc.Arbitrary OptTempTableName where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ TemporaryOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ TempOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ LocalTemporaryOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ LocalTempOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ GlobalTemporaryOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ GlobalTempOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ UnloggedOptTempTableName <$> Qc.arbitrary <*> Qc.arbitrary,+ TableOptTempTableName <$> Qc.arbitrary,+ QualifedOptTempTableName <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/OptVarying.hs view
@@ -0,0 +1,24 @@+module PostgresqlSyntax.Ast.OptVarying where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_varying:+-- | VARYING+-- | EMPTY+-- @+newtype OptVarying = OptVarying Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OptVarying where+ toTextBuilder _settings (OptVarying a) = if a then "VARYING" else mempty+ parser _settings = OptVarying <$> (True <$ Parsers.space1 <* Parsers.keyword "varying" <|> pure False)++instance Qc.Arbitrary OptVarying where+ shrink = Qc.genericShrink+ arbitrary = OptVarying <$> arbitrary
+ library-internal/PostgresqlSyntax/Ast/OverClause.hs view
@@ -0,0 +1,43 @@+module PostgresqlSyntax.Ast.OverClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.WindowSpecification+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- over_clause:+-- | OVER window_specification+-- | OVER ColId+-- | EMPTY+-- @+data OverClause+ = WindowOverClause WindowSpecification+ | ColIdOverClause Ident+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OverClause where+ toTextBuilder settings = \case+ WindowOverClause a -> "OVER " <> toTextBuilder settings a+ ColIdOverClause a -> "OVER " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "over"+ Parsers.space1+ Parser.endHead+ asum+ [ WindowOverClause <$> parser settings,+ ColIdOverClause <$> colId settings+ ]++instance Qc.Arbitrary OverClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ WindowOverClause <$> Qc.arbitrary,+ ColIdOverClause <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/OverlayList.hs view
@@ -0,0 +1,58 @@+module PostgresqlSyntax.Ast.OverlayList where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- overlay_list:+-- | a_expr overlay_placing substr_from substr_for+-- | a_expr overlay_placing substr_from+-- @+--+-- @overlay_placing@\/@substr_from@\/@substr_for@ are bare aliases to+-- 'PostgresqlSyntax.Ast.AExpr' (@PLACING a_expr@\/@FROM a_expr@\/@FOR+-- a_expr@ respectively); their tiny keyword-prefix wrapping is inlined here+-- rather than named, since 'PostgresqlSyntax.Ast.SubstrListFromFor' has its+-- own (differently-scoped) copy of the @FROM@\/@FOR@ half.+data OverlayList = OverlayList AExpr AExpr AExpr (Maybe AExpr)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst OverlayList where+ toTextBuilder settings (OverlayList a b c d) =+ toTextBuilder settings a+ <> " PLACING "+ <> toTextBuilder settings b+ <> " FROM "+ <> toTextBuilder settings c+ <> TextBuilders.suffixMaybe (mappend "FOR " . toTextBuilder settings) d+ parser settings = do+ a <- parser settings+ Parsers.space1+ Parsers.keyword "placing"+ Parsers.space1+ Parser.endHead+ b <- parser settings+ Parsers.space1+ Parsers.keyword "from"+ Parsers.space1+ Parser.endHead+ c <- parser settings+ d <- optional (Parsers.space1 *> Parsers.keyword "for" *> Parsers.space1 *> Parser.endHead *> parser settings)+ return (OverlayList a b c d)++instance Qc.Arbitrary OverlayList where+ shrink = Qc.genericShrink+ arbitrary =+ OverlayList+ <$> Gens.downscale Qc.arbitrary+ <*> Gens.downscale Qc.arbitrary+ <*> Gens.downscale Qc.arbitrary+ <*> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary)
+ library-internal/PostgresqlSyntax/Ast/OverrideKind.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.OverrideKind where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- override_kind:+-- | USER+-- | SYSTEM_P+-- @+data OverrideKind = UserOverrideKind | SystemOverrideKind+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst OverrideKind where+ toTextBuilder _settings = \case+ UserOverrideKind -> "USER"+ SystemOverrideKind -> "SYSTEM"+ parser _settings =+ asum+ [ UserOverrideKind <$ Parsers.keyword "user",+ SystemOverrideKind <$ Parsers.keyword "system"+ ]++instance Qc.Arbitrary OverrideKind where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/PositionList.hs view
@@ -0,0 +1,26 @@+module PostgresqlSyntax.Ast.PositionList where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.BExpr (BExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- position_list:+-- | b_expr IN_P b_expr+-- | EMPTY+-- @+data PositionList = PositionList BExpr BExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst PositionList where+ toTextBuilder settings (PositionList a b) = toTextBuilder settings a <> " IN " <> toTextBuilder settings b+ parser settings = PositionList <$> parser settings <*> (Parsers.space1 *> Parsers.keyword "in" *> Parsers.space1 *> parser settings)++instance Qc.Arbitrary PositionList where+ shrink = Qc.genericShrink+ arbitrary = PositionList <$> Gens.downscale arbitrary <*> Gens.downscale arbitrary
+ library-internal/PostgresqlSyntax/Ast/PreparableStmt.hs view
@@ -0,0 +1,55 @@+module PostgresqlSyntax.Ast.PreparableStmt where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.CallStmt+import PostgresqlSyntax.Ast.DeleteStmt+import PostgresqlSyntax.Ast.InsertStmt+import PostgresqlSyntax.Ast.SelectStmt+import PostgresqlSyntax.Ast.UpdateStmt+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- PreparableStmt:+-- | SelectStmt+-- | InsertStmt+-- | UpdateStmt+-- | DeleteStmt+-- | CallStmt+-- @+data PreparableStmt+ = SelectPreparableStmt SelectStmt+ | InsertPreparableStmt InsertStmt+ | UpdatePreparableStmt UpdateStmt+ | DeletePreparableStmt DeleteStmt+ | CallPreparableStmt CallStmt+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst PreparableStmt where+ toTextBuilder settings = \case+ SelectPreparableStmt a -> toTextBuilder settings a+ InsertPreparableStmt a -> toTextBuilder settings a+ UpdatePreparableStmt a -> toTextBuilder settings a+ DeletePreparableStmt a -> toTextBuilder settings a+ CallPreparableStmt a -> toTextBuilder settings a+ parser settings =+ asum+ [ SelectPreparableStmt <$> parser settings,+ InsertPreparableStmt <$> parser settings,+ UpdatePreparableStmt <$> parser settings,+ DeletePreparableStmt <$> parser settings,+ CallPreparableStmt <$> parser settings+ ]++instance Qc.Arbitrary PreparableStmt where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ SelectPreparableStmt <$> Qc.arbitrary,+ InsertPreparableStmt <$> Qc.arbitrary,+ UpdatePreparableStmt <$> Qc.arbitrary,+ DeletePreparableStmt <$> Qc.arbitrary,+ CallPreparableStmt <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/QualAllOp.hs view
@@ -0,0 +1,39 @@+module PostgresqlSyntax.Ast.QualAllOp where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AllOp+import PostgresqlSyntax.Ast.AnyOperator+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- qual_all_Op:+-- | all_Op+-- | OPERATOR '(' any_operator ')'+-- @+data QualAllOp+ = AllQualAllOp AllOp+ | AnyQualAllOp AnyOperator+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst QualAllOp where+ toTextBuilder settings = \case+ AllQualAllOp a -> toTextBuilder settings a+ AnyQualAllOp a -> "OPERATOR (" <> toTextBuilder settings a <> ")"+ parser settings =+ asum+ [ AnyQualAllOp <$> (Parsers.keyword "operator" *> Parsers.space *> Parsers.inParens (Parser.endHead *> parser settings)),+ AllQualAllOp <$> parser settings+ ]++instance Qc.Arbitrary QualAllOp where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ AllQualAllOp <$> Qc.arbitrary,+ AnyQualAllOp <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/QualOp.hs view
@@ -0,0 +1,38 @@+module PostgresqlSyntax.Ast.QualOp where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AnyOperator+import PostgresqlSyntax.Ast.Op+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- qual_Op:+-- | Op+-- | OPERATOR '(' any_operator ')'+-- @+data QualOp+ = OpQualOp Op+ | OperatorQualOp AnyOperator+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst QualOp where+ toTextBuilder settings = \case+ OpQualOp a -> toTextBuilder settings a+ OperatorQualOp a -> "OPERATOR (" <> toTextBuilder settings a <> ")"+ parser settings =+ asum+ [ OpQualOp <$> parser settings,+ OperatorQualOp <$> Parsers.inParensWithClause (Parsers.keyword "operator") (parser settings)+ ]++instance Qc.Arbitrary QualOp where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ OpQualOp <$> Qc.arbitrary,+ OperatorQualOp <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/QualifiedName.hs view
@@ -0,0 +1,43 @@+module PostgresqlSyntax.Ast.QualifiedName where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Indirection+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- columnref:+-- | ColId+-- | ColId indirection+-- qualified_name:+-- | ColId+-- | ColId indirection+-- @+data QualifiedName+ = SimpleQualifiedName Ident+ | IndirectedQualifiedName Ident Indirection+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst QualifiedName where+ toTextBuilder settings = \case+ SimpleQualifiedName a -> toTextBuilder settings a+ IndirectedQualifiedName a b -> toTextBuilder settings a <> toTextBuilder settings b+ parser settings =+ IndirectedQualifiedName+ <$> Parser.wrapToHead (colId settings)+ <*> (Parsers.space *> parser settings)+ <|> SimpleQualifiedName+ <$> colId settings++instance Qc.Arbitrary QualifiedName where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ SimpleQualifiedName <$> Qc.arbitrary,+ IndirectedQualifiedName <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/RelationExpr.hs view
@@ -0,0 +1,55 @@+module PostgresqlSyntax.Ast.RelationExpr where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.QualifiedName+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | qualified_name+-- | qualified_name '*'+-- | ONLY qualified_name+-- | ONLY '(' qualified_name ')'+-- @+data RelationExpr+ = -- | Name, then whether an asterisk is present.+ SimpleRelationExpr QualifiedName Bool+ | -- | Name, then whether parentheses are present.+ OnlyRelationExpr QualifiedName Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst RelationExpr where+ toTextBuilder settings = \case+ SimpleRelationExpr a b -> toTextBuilder settings a <> bool "" " *" b+ OnlyRelationExpr a b -> "ONLY " <> bool (toTextBuilder settings) (TextBuilders.renderInParens . toTextBuilder settings) b a+ parser settings =+ Parser.label "relation expression" $+ asum+ [ do+ Parsers.keyword "only"+ Parsers.space1+ name <- parser settings+ return (OnlyRelationExpr name False),+ Parsers.inParensWithClause (Parsers.keyword "only") (parser settings) <&> \a -> OnlyRelationExpr a True,+ do+ name <- parser settings+ asterisk <-+ asum+ [ True <$ (Parsers.space1 *> Parsers.char '*'),+ pure False+ ]+ return (SimpleRelationExpr name asterisk)+ ]++instance Qc.Arbitrary RelationExpr where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ SimpleRelationExpr <$> Qc.arbitrary <*> Qc.arbitrary,+ OnlyRelationExpr <$> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/RelationExprOptAlias.hs view
@@ -0,0 +1,43 @@+module PostgresqlSyntax.Ast.RelationExprOptAlias+ ( RelationExprOptAlias (..),+ )+where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.RelationExpr+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- relation_expr_opt_alias:+-- | relation_expr+-- | relation_expr ColId+-- | relation_expr AS ColId+-- @+data RelationExprOptAlias = RelationExprOptAlias RelationExpr (Maybe (Bool, Ident))+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst RelationExprOptAlias where+ toTextBuilder settings (RelationExprOptAlias a b) = toTextBuilder settings a <> TextBuilders.suffixMaybe optAlias b+ where+ optAlias (c, d) = bool "" "AS " c <> toTextBuilder settings d+ parser settings = do+ a <- parser settings+ b <- optional $ do+ Parsers.space1+ b <- Parsers.trueIfPresent (Parsers.keyword "as" *> Parsers.space1)+ -- Only the bare-alias (no @AS@) branch has the shift/reduce conflict+ -- that Postgres resolves by excluding @SET@; see @gram.y@, comment+ -- above @relation_expr_opt_alias@'s first production.+ c <- Parsers.filteredColIdLike UnquotedIdent (parser settings) (if b then [] else ["set"])+ return (b, c)+ return (RelationExprOptAlias a b)++instance Qc.Arbitrary RelationExprOptAlias where+ shrink = Qc.genericShrink+ arbitrary = RelationExprOptAlias <$> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/ReturningClause.hs view
@@ -0,0 +1,33 @@+module PostgresqlSyntax.Ast.ReturningClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.TargetList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- returning_clause:+-- | RETURNING returning_with_clause target_list+-- | /*EMPTY*/+-- @+--+-- @returning_with_clause@ (the @WITH (...)@ modifier) is not modeled+-- here - not supported by this codebase's grammar subset.+newtype ReturningClause = ReturningClause TargetList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ReturningClause where+ toTextBuilder settings (ReturningClause a) = "RETURNING " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "returning"+ Parsers.space1+ Parser.endHead+ ReturningClause <$> parser settings++instance Qc.Arbitrary ReturningClause where+ shrink = Qc.genericShrink+ arbitrary = ReturningClause <$> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/Row.hs view
@@ -0,0 +1,34 @@+module PostgresqlSyntax.Ast.Row where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExplicitRow+import PostgresqlSyntax.Ast.ImplicitRow+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- row:+-- | ROW '(' expr_list ')'+-- | ROW '(' ')'+-- | '(' expr_list ',' a_expr ')'+-- @+data Row+ = ExplicitRowRow ExplicitRow+ | ImplicitRowRow ImplicitRow+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Row where+ toTextBuilder settings = \case+ ExplicitRowRow a -> toTextBuilder settings a+ ImplicitRowRow a -> toTextBuilder settings a+ parser settings = ExplicitRowRow <$> parser settings <|> ImplicitRowRow <$> parser settings++instance Qc.Arbitrary Row where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExplicitRowRow <$> Qc.arbitrary,+ ImplicitRowRow <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/RowsfromItem.hs view
@@ -0,0 +1,39 @@+module PostgresqlSyntax.Ast.RowsfromItem where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FuncExprWindowless+import PostgresqlSyntax.Ast.TableFuncElementList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- rowsfrom_item:+-- | func_expr_windowless opt_col_def_list+-- @+--+-- @opt_col_def_list@ is a bare alias to+-- 'PostgresqlSyntax.Ast.TableFuncElementList'.+data RowsfromItem = RowsfromItem FuncExprWindowless (Maybe TableFuncElementList)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst RowsfromItem where+ toTextBuilder settings (RowsfromItem a b) = toTextBuilder settings a <> TextBuilders.suffixMaybe colDefList b+ where+ colDefList a' = "AS (" <> toTextBuilder settings a' <> ")"+ parser settings = do+ a <- parser settings+ Parser.endHead+ b <- optional (Parsers.space1 *> colDefList)+ return (RowsfromItem a b)+ where+ colDefList = Parsers.keyword "as" *> Parsers.space *> Parsers.inParens (Parser.endHead *> parser settings)++instance Qc.Arbitrary RowsfromItem where+ shrink = Qc.genericShrink+ arbitrary = RowsfromItem <$> arbitrary <*> Gens.terminatingMaybe arbitrary
+ library-internal/PostgresqlSyntax/Ast/RowsfromList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.RowsfromList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.RowsfromItem+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- rowsfrom_list:+-- | rowsfrom_item+-- | rowsfrom_list ',' rowsfrom_item+-- @+newtype RowsfromList = RowsfromList (NonEmpty RowsfromItem)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst RowsfromList where+ toTextBuilder settings (RowsfromList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = RowsfromList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary RowsfromList where+ shrink = Qc.genericShrink+ arbitrary = RowsfromList <$> Gens.nonEmptyUpTo 7 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/Sconst.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.Sconst where++import qualified Data.Text as Text+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.Shrinks as Shrinks+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc+import qualified TextBuilder++-- |+-- ==== References+-- @+-- Sconst+-- @+newtype Sconst = Sconst Text+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Sconst where+ toTextBuilder _settings (Sconst a) = "'" <> TextBuilder.text (Text.replace "'" "''" a) <> "'"+ parser _settings = Sconst <$> (Parsers.quotedString '\'' <|> Parsers.dollarQuotedSconst)++instance Qc.Arbitrary Sconst where+ shrink (Sconst a) = Sconst <$> Shrinks.text a+ arbitrary = do+ len <- Qc.sized (\n -> Qc.choose (0, min 1000 (n * 20)))+ Sconst . Text.pack <$> Qc.vectorOf len (Qc.arbitrary `Qc.suchThat` (not . isControl))
+ library-internal/PostgresqlSyntax/Ast/SelectBinOp.hs view
@@ -0,0 +1,32 @@+module PostgresqlSyntax.Ast.SelectBinOp where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | select_clause UNION all_or_distinct select_clause+-- | select_clause INTERSECT all_or_distinct select_clause+-- | select_clause EXCEPT all_or_distinct select_clause+-- @+data SelectBinOp = UnionSelectBinOp | IntersectSelectBinOp | ExceptSelectBinOp+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectBinOp where+ toTextBuilder _settings = \case+ UnionSelectBinOp -> "UNION"+ IntersectSelectBinOp -> "INTERSECT"+ ExceptSelectBinOp -> "EXCEPT"+ parser _settings =+ asum+ [ Parsers.keyword "union" $> UnionSelectBinOp,+ Parsers.keyword "intersect" $> IntersectSelectBinOp,+ Parsers.keyword "except" $> ExceptSelectBinOp+ ]++instance Qc.Arbitrary SelectBinOp where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [UnionSelectBinOp, IntersectSelectBinOp, ExceptSelectBinOp]
+ library-internal/PostgresqlSyntax/Ast/SelectClause.hs view
@@ -0,0 +1,74 @@+module PostgresqlSyntax.Ast.SelectClause where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import {-# SOURCE #-} PostgresqlSyntax.Ast.SimpleSelect (SimpleSelect)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- select_clause:+-- | simple_select+-- | select_with_parens+-- @+--+-- This type's own 'IsAst' instance is a plain, non-recursive-suffix-aware+-- dispatch - the real @UNION@\/@INTERSECT@\/@EXCEPT@-chaining grammar+-- (where a @select_clause@ extends into a bigger+-- 'PostgresqlSyntax.Ast.SimpleSelect' via its @BinSimpleSelect@+-- constructor) is hosted in "PostgresqlSyntax.Ast.SimpleSelect" instead,+-- since only that module can construct @BinSimpleSelect@ values. See its+-- module documentation.+data SelectClause+ = SimpleSelectSelectClause SimpleSelect+ | WithParensSelectClause SelectWithParens+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectClause where+ toTextBuilder settings = \case+ SimpleSelectSelectClause a -> toTextBuilder settings a+ WithParensSelectClause a -> toTextBuilder settings a++ -- ==== Law+ --+ -- @parser = parseMaybeExtended \@SelectClause@ - see+ -- 'PostgresqlSyntax.Ast.SimpleSelect'\'s 'PostgresqlSyntax.Algebra.Extends'+ -- instance for the real @select_clause@ grammar, including+ -- @UNION@\/@INTERSECT@\/@EXCEPT@-chaining.+ parser settings = parseMaybeExtended @SimpleSelect settings++-- |+-- Every @select_clause@ production except the left-recursive ones (those+-- are the @UNION@\/@INTERSECT@\/@EXCEPT@ continuations, hosted by+-- "PostgresqlSyntax.Ast.SimpleSelect"\'s+-- 'PostgresqlSyntax.Algebra.Extends' instance).+instance LeftRecursive SelectClause where+ parseBase settings =+ asum+ [ WithParensSelectClause <$> parser settings,+ SimpleSelectSelectClause <$> parseBase @SimpleSelect settings+ ]++-- |+-- A 'SimpleSelect' embeds trivially into a 'SelectClause' (it's one of its+-- two alternatives), and a 'SelectClause' of that exact shape is+-- recognizable back as one. Needed so+-- "PostgresqlSyntax.Ast.SimpleSelect"\'s+-- 'PostgresqlSyntax.Algebra.Extends' instance can fold a chain of+-- @UNION@\/@INTERSECT@\/@EXCEPT@ items onto a leading 'SelectClause'.+instance Refines SimpleSelect SelectClause where+ embed = SimpleSelectSelectClause+ project = \case+ SimpleSelectSelectClause a -> Just a+ _ -> Nothing++instance Qc.Arbitrary SelectClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.frequency+ [ (3, SimpleSelectSelectClause <$> Gens.downscale Qc.arbitrary),+ (1, WithParensSelectClause <$> Gens.downscale Qc.arbitrary)+ ]
+ library-internal/PostgresqlSyntax/Ast/SelectClause.hs-boot view
@@ -0,0 +1,19 @@+module PostgresqlSyntax.Ast.SelectClause where++import PostgresqlSyntax.Algebra (IsAst)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data SelectClause++instance Show SelectClause++instance Eq SelectClause++instance Ord SelectClause++instance Data SelectClause++instance IsAst SelectClause++instance Arbitrary SelectClause
+ library-internal/PostgresqlSyntax/Ast/SelectFetchFirstValue.hs view
@@ -0,0 +1,61 @@+module PostgresqlSyntax.Ast.SelectFetchFirstValue where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.CExpr+import PostgresqlSyntax.Ast.Fconst+import qualified PostgresqlSyntax.Extras.TextBuilder as TextBuilder+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- select_fetch_first_value:+-- | c_expr+-- | '+' I_or_F_const+-- | '-' I_or_F_const+-- @+data SelectFetchFirstValue+ = ExprSelectFetchFirstValue CExpr+ | NumSelectFetchFirstValue Bool (Either Int64 Double)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectFetchFirstValue where+ toTextBuilder settings = \case+ ExprSelectFetchFirstValue a -> toTextBuilder settings a+ NumSelectFetchFirstValue a b -> bool "+" "-" a <> intOrFloat b+ where+ intOrFloat = either TextBuilder.int64Dec TextBuilder.doubleDec+ parser settings =+ ExprSelectFetchFirstValue+ <$> parser settings+ <|> NumSelectFetchFirstValue+ <$> (plusOrMinus <* Parser.endHead <* Parsers.space)+ <*> iconstOrFconst+ where+ plusOrMinus = False <$ Parsers.char '+' <|> True <$ Parsers.char '-'+ iconstOrFconst = Right <$> (coerce <$> (parser settings :: Parser Fconst)) <|> Left <$> Parsers.decimal++instance Qc.Arbitrary SelectFetchFirstValue where+ shrink = Qc.genericShrink++ -- The magnitude is parsed via unsigned 'Parser.decimal'\/'Fconst' (the+ -- sign is this type's own separate @Bool@ field), so, like+ -- 'PostgresqlSyntax.Ast.IntervalSecond'\'s @nonNegative@, it must never be+ -- negative itself - otherwise e.g. @NumSelectFetchFirstValue True (Left+ -- (-1))@ renders as @-1@ with no space (a valid unsigned-magnitude+ -- rendering would be @- 1@ or just @-1@ for magnitude 1), doubling up+ -- into @--1@, which reparses as a line comment.+ arbitrary =+ Qc.oneof+ [ ExprSelectFetchFirstValue <$> Qc.arbitrary,+ NumSelectFetchFirstValue <$> Qc.arbitrary <*> Qc.oneof [Left <$> nonNegativeInt64, Right <$> nonNegativeDouble]+ ]+ where+ nonNegativeInt64 = Qc.sized (\n -> Qc.choose (0, cap n))+ nonNegativeDouble = abs <$> Qc.arbitrary+ cap n+ | n >= 62 = maxBound+ | otherwise = 2 ^ n
+ library-internal/PostgresqlSyntax/Ast/SelectLimit.hs view
@@ -0,0 +1,51 @@+module PostgresqlSyntax.Ast.SelectLimit where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.LimitClause+import PostgresqlSyntax.Ast.OffsetClause+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- select_limit:+-- | limit_clause offset_clause+-- | offset_clause limit_clause+-- | limit_clause+-- | offset_clause+-- @+data SelectLimit+ = LimitOffsetSelectLimit LimitClause OffsetClause+ | OffsetLimitSelectLimit OffsetClause LimitClause+ | LimitSelectLimit LimitClause+ | OffsetSelectLimit OffsetClause+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectLimit where+ toTextBuilder settings = \case+ LimitOffsetSelectLimit a b -> TextBuilders.lexemes [toTextBuilder settings a, toTextBuilder settings b]+ OffsetLimitSelectLimit a b -> TextBuilders.lexemes [toTextBuilder settings a, toTextBuilder settings b]+ LimitSelectLimit a -> toTextBuilder settings a+ OffsetSelectLimit a -> toTextBuilder settings a+ parser settings =+ asum+ [ do+ a <- parser settings+ LimitOffsetSelectLimit a <$> (Parsers.space1 *> parser settings) <|> pure (LimitSelectLimit a),+ do+ a <- parser settings+ OffsetLimitSelectLimit a <$> (Parsers.space1 *> parser settings) <|> pure (OffsetSelectLimit a)+ ]++instance Qc.Arbitrary SelectLimit where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ LimitOffsetSelectLimit <$> Qc.arbitrary <*> Qc.arbitrary,+ OffsetLimitSelectLimit <$> Qc.arbitrary <*> Qc.arbitrary,+ LimitSelectLimit <$> Qc.arbitrary,+ OffsetSelectLimit <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SelectLimitValue.hs view
@@ -0,0 +1,38 @@+module PostgresqlSyntax.Ast.SelectLimitValue where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- select_limit_value:+-- | a_expr+-- | ALL+-- @+data SelectLimitValue+ = ExprSelectLimitValue AExpr+ | AllSelectLimitValue+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectLimitValue where+ toTextBuilder settings = \case+ ExprSelectLimitValue a -> toTextBuilder settings a+ AllSelectLimitValue -> "ALL"+ parser settings =+ AllSelectLimitValue+ <$ Parsers.keyword "all"+ <|> ExprSelectLimitValue+ <$> parser settings++instance Qc.Arbitrary SelectLimitValue where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExprSelectLimitValue <$> Gens.downscale Qc.arbitrary,+ pure AllSelectLimitValue+ ]
+ library-internal/PostgresqlSyntax/Ast/SelectNoParens.hs view
@@ -0,0 +1,106 @@+module PostgresqlSyntax.Ast.SelectNoParens+ ( SelectNoParens (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ForLockingClause+import PostgresqlSyntax.Ast.SelectClause+import PostgresqlSyntax.Ast.SelectLimit+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import {-# SOURCE #-} PostgresqlSyntax.Ast.SimpleSelect (SimpleSelect)+import PostgresqlSyntax.Ast.SortClause+import {-# SOURCE #-} PostgresqlSyntax.Ast.WithClause (WithClause)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (Settings)+import qualified Test.QuickCheck as Qc++-- |+-- Covers the following cases:+--+-- @+-- select_no_parens:+-- | simple_select+-- | select_clause sort_clause+-- | select_clause opt_sort_clause for_locking_clause opt_select_limit+-- | select_clause opt_sort_clause select_limit opt_for_locking_clause+-- | with_clause select_clause+-- | with_clause select_clause sort_clause+-- | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit+-- | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause+-- @+data SelectNoParens+ = SelectNoParens (Maybe WithClause) SelectClause (Maybe SortClause) (Maybe SelectLimit) (Maybe ForLockingClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectNoParens where+ toTextBuilder settings (SelectNoParens a b c d e) =+ TextBuilders.optLexemes+ [ fmap (toTextBuilder settings) a,+ Just (toTextBuilder settings b),+ fmap (toTextBuilder settings) c,+ fmap (toTextBuilder settings) d,+ fmap (toTextBuilder settings) e+ ]+ parser settings = withSelectNoParens <|> simpleSelectNoParens+ where+ simpleSelectNoParens = sharedSelectNoParens settings Nothing+ withSelectNoParens = do+ with <- Parser.wrapToHead (parser settings)+ Parsers.space1+ sharedSelectNoParens settings (Just with)++-- |+-- Parses the @select_clause@ (see+-- 'PostgresqlSyntax.Ast.SimpleSelect'\'s 'Extends' instance for the+-- @UNION@\/@INTERSECT@\/@EXCEPT@-chaining grammar) plus everything that can+-- follow it.+sharedSelectNoParens :: Settings -> Maybe WithClause -> Parser SelectNoParens+sharedSelectNoParens settings with = do+ select <- parseMaybeExtended @SimpleSelect settings+ sort <- optional (Parsers.space1 *> parser settings)+ (limit, forLocking) <- limitFirst <|> forLockingFirst <|> pure (Nothing, Nothing)+ return (SelectNoParens with select sort limit forLocking)+ where+ limitFirst = do+ limit <- Parsers.space1 *> parser settings+ forLocking <- optional (Parsers.space1 *> parser settings)+ pure (Just limit, forLocking)+ forLockingFirst = do+ forLocking <- Parsers.space1 *> parser settings+ limit <- optional (Parsers.space1 *> parser settings)+ pure (limit, Just forLocking)++-- |+-- If a 'SelectNoParens' is merely a trivial wrapper around a single+-- parenthesized select - no with-clause, sort, limit or locking clause of+-- its own - returns the wrapped 'SelectWithParens'. Used by the+-- 'Refines' instance to canonicalize such wrappers.+refineToSelectWithParens :: SelectNoParens -> Maybe SelectWithParens+refineToSelectWithParens = \case+ SelectNoParens Nothing (WithParensSelectClause c) Nothing Nothing Nothing -> Just c+ _ -> Nothing++instance Refines SelectWithParens SelectNoParens where+ embed c = SelectNoParens Nothing (WithParensSelectClause c) Nothing Nothing Nothing+ project = refineToSelectWithParens++instance Qc.Arbitrary SelectNoParens where+ shrink = Qc.genericShrink+ arbitrary =+ sized $ \size ->+ if size <= 1+ then do+ selectClause <- Qc.arbitrary+ return (SelectNoParens Nothing selectClause Nothing Nothing Nothing)+ else+ SelectNoParens+ <$> Gens.downscale Qc.arbitrary+ <*> Qc.arbitrary+ <*> Qc.arbitrary+ <*> Qc.arbitrary+ <*> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/SelectNoParens.hs-boot view
@@ -0,0 +1,22 @@+module PostgresqlSyntax.Ast.SelectNoParens where++import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import PostgresqlSyntax.Algebra (IsAst, Refines)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data SelectNoParens++instance Show SelectNoParens++instance Eq SelectNoParens++instance Ord SelectNoParens++instance Data SelectNoParens++instance IsAst SelectNoParens++instance Arbitrary SelectNoParens++instance Refines SelectWithParens SelectNoParens
+ library-internal/PostgresqlSyntax/Ast/SelectStmt.hs view
@@ -0,0 +1,39 @@+module PostgresqlSyntax.Ast.SelectStmt where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectNoParens (SelectNoParens)+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- SelectStmt:+-- | select_no_parens+-- | select_with_parens+-- @+data SelectStmt+ = NoParensSelectStmt SelectNoParens+ | WithParensSelectStmt SelectWithParens+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectStmt where+ toTextBuilder settings = \case+ NoParensSelectStmt a -> toTextBuilder settings a+ WithParensSelectStmt a -> toTextBuilder settings a+ parser settings = NoParensSelectStmt <$> parser settings <|> WithParensSelectStmt <$> parser settings++instance Qc.Arbitrary SelectStmt where+ shrink = Qc.genericShrink++ -- @WithParensSelectStmt@ is unreachable via this type's own @parser@:+ -- @NoParensSelectStmt@'s alternative is tried first, and+ -- 'PostgresqlSyntax.Ast.SelectClause' (reachable from any+ -- @select_no_parens@ with every other clause absent) always accepts a+ -- parenthesized select too - so any @'(' select ')'@ text always parses+ -- as @NoParensSelectStmt (SelectNoParens Nothing (WithParensSelectClause+ -- _) Nothing Nothing Nothing)@, never as a bare @WithParensSelectStmt@.+ -- Generating the latter would therefore never round-trip.+ arbitrary = NoParensSelectStmt <$> Gens.downscale Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/SelectWithParens.hs view
@@ -0,0 +1,70 @@+module PostgresqlSyntax.Ast.SelectWithParens+ ( SelectWithParens (..),+ )+where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectNoParens (SelectNoParens)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- select_with_parens:+-- | '(' select_no_parens ')'+-- | '(' select_with_parens ')'+-- @+data SelectWithParens+ = NoParensSelectWithParens SelectNoParens+ | WithParensSelectWithParens SelectWithParens+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SelectWithParens where+ toTextBuilder settings =+ TextBuilders.renderInParens . \case+ NoParensSelectWithParens a -> toTextBuilder settings a+ WithParensSelectWithParens a -> toTextBuilder settings a++ -- ==== Canonical shape+ --+ -- Because @select_with_parens@ (@'(' select_with_parens ')'@) and+ -- @'(' select_no_parens ')'@ both parse @((select 1))@, there are two+ -- possible representations: @WithParensSelectWithParens@ wrapping the+ -- inner parenthesised select, or @NoParensSelectWithParens@ of a+ -- @SelectNoParens@ whose clause is that inner select. Both render back+ -- to the same text. __The first (@WithParensSelectWithParens@) is+ -- canonical.__ The 'Canonicalizes' instance normalizes shrunk or generated+ -- values to this shape.+ parser settings = Parsers.inParens (canonicalize . NoParensSelectWithParens <$> parser settings)++instance Qc.Arbitrary SelectWithParens where+ shrink = fmap canonicalize . Qc.genericShrink+ arbitrary =+ canonicalize+ <$> Qc.frequency+ [ (3, NoParensSelectWithParens <$> Gens.downscale Qc.arbitrary),+ (1, WithParensSelectWithParens <$> Gens.downscale Qc.arbitrary)+ ]++-- |+-- Collapses the non-canonical @NoParensSelectWithParens@ shape described+-- above to the @WithParensSelectWithParens@ shape the parser actually+-- produces for it. Both 'arbitrary' and 'shrink' can otherwise construct+-- the non-canonical shape (shrinking the inner 'SelectNoParens' toward+-- @Nothing@s is exactly how it arises), which renders fine but parses back+-- to a different, canonical value and so breaks the roundtrip property.+instance Canonicalizes SelectWithParens where+ canonicalize = \case+ NoParensSelectWithParens a+ | Just c <- project @SelectWithParens a -> WithParensSelectWithParens c+ other -> other++instance Refines SelectWithParens SelectWithParens where+ embed = WithParensSelectWithParens+ project = \case+ WithParensSelectWithParens a -> Just a+ _ -> Nothing
+ library-internal/PostgresqlSyntax/Ast/SelectWithParens.hs-boot view
@@ -0,0 +1,21 @@+module PostgresqlSyntax.Ast.SelectWithParens where++import PostgresqlSyntax.Algebra (IsAst, Refines)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data SelectWithParens++instance Show SelectWithParens++instance Eq SelectWithParens++instance Ord SelectWithParens++instance Data SelectWithParens++instance IsAst SelectWithParens++instance Arbitrary SelectWithParens++instance Refines SelectWithParens SelectWithParens
+ library-internal/PostgresqlSyntax/Ast/SetClause.hs view
@@ -0,0 +1,53 @@+module PostgresqlSyntax.Ast.SetClause where++import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.SetTarget+import PostgresqlSyntax.Ast.SetTargetList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- set_clause:+-- | set_target '=' a_expr+-- | '(' set_target_list ')' '=' a_expr+-- @+data SetClause+ = TargetSetClause SetTarget AExpr+ | TargetListSetClause SetTargetList AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SetClause where+ toTextBuilder settings = \case+ TargetSetClause a b -> toTextBuilder settings a <> " = " <> toTextBuilder settings b+ TargetListSetClause a b -> TextBuilders.renderInParens (toTextBuilder settings a) <> " = " <> toTextBuilder settings b+ parser settings =+ asum+ [ do+ a <- Parsers.inParens (parser settings)+ Parsers.space+ Parsers.char '='+ Parsers.space+ b <- parser settings+ return (TargetListSetClause a b),+ do+ a <- parser settings+ Parsers.space+ Parsers.char '='+ Parsers.space+ b <- parser settings+ return (TargetSetClause a b)+ ]++instance Qc.Arbitrary SetClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ TargetSetClause <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ TargetListSetClause <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SetClauseList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.SetClauseList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.SetClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- set_clause_list:+-- | set_clause+-- | set_clause_list ',' set_clause+-- @+newtype SetClauseList = SetClauseList (NonEmpty SetClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SetClauseList where+ toTextBuilder settings (SetClauseList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = SetClauseList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary SetClauseList where+ shrink = Qc.genericShrink+ arbitrary = SetClauseList <$> Gens.nonEmptyUpTo 9 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/SetTarget.hs view
@@ -0,0 +1,32 @@+module PostgresqlSyntax.Ast.SetTarget where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Indirection+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- set_target:+-- | ColId opt_indirection+-- @+data SetTarget = SetTarget Ident (Maybe Indirection)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SetTarget where+ toTextBuilder settings (SetTarget a b) = toTextBuilder settings a <> TextBuilders.suffixMaybe (toTextBuilder settings) b+ parser settings = do+ a <- colId settings+ Parser.endHead+ b <- optional (Parsers.space1 *> parser settings)+ return (SetTarget a b)++instance Qc.Arbitrary SetTarget where+ shrink = Qc.genericShrink+ arbitrary = SetTarget <$> arbitrary <*> Gens.terminatingMaybe arbitrary
+ library-internal/PostgresqlSyntax/Ast/SetTargetList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.SetTargetList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.SetTarget+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- set_target_list:+-- | set_target+-- | set_target_list ',' set_target+-- @+newtype SetTargetList = SetTargetList (NonEmpty SetTarget)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SetTargetList where+ toTextBuilder settings (SetTargetList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = SetTargetList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary SetTargetList where+ shrink = Qc.genericShrink+ arbitrary = SetTargetList <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/SimpleSelect.hs view
@@ -0,0 +1,232 @@+{-# OPTIONS_GHC -Wno-orphans #-}++module PostgresqlSyntax.Ast.SimpleSelect+ ( SimpleSelect (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FromClause+import PostgresqlSyntax.Ast.GroupClause+import PostgresqlSyntax.Ast.HavingClause+import PostgresqlSyntax.Ast.IntoClause+import PostgresqlSyntax.Ast.RelationExpr+import PostgresqlSyntax.Ast.SelectBinOp+import PostgresqlSyntax.Ast.SelectClause+import PostgresqlSyntax.Ast.Targeting+import PostgresqlSyntax.Ast.ValuesClause+import PostgresqlSyntax.Ast.WhereClause+import PostgresqlSyntax.Ast.WindowClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (Settings)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- simple_select:+-- | SELECT opt_all_clause opt_target_list+-- into_clause from_clause where_clause+-- group_clause having_clause window_clause+-- | SELECT distinct_clause target_list+-- into_clause from_clause where_clause+-- group_clause having_clause window_clause+-- | values_clause+-- | TABLE relation_expr+-- | select_clause UNION all_or_distinct select_clause+-- | select_clause INTERSECT all_or_distinct select_clause+-- | select_clause EXCEPT all_or_distinct select_clause+-- @+--+-- Hosts the real @select_clause@ grammar (including its+-- @UNION@\/@INTERSECT@\/@EXCEPT@-chaining) for both itself and+-- "PostgresqlSyntax.Ast.SelectNoParens", which shares it - see+-- 'PostgresqlSyntax.Ast.SelectClause'\'s module documentation for why.+data SimpleSelect+ = NormalSimpleSelect (Maybe Targeting) (Maybe IntoClause) (Maybe FromClause) (Maybe WhereClause) (Maybe GroupClause) (Maybe HavingClause) (Maybe WindowClause)+ | ValuesSimpleSelect ValuesClause+ | TableSimpleSelect RelationExpr+ | BinSimpleSelect SelectBinOp SelectClause (Maybe Bool) SelectClause+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SimpleSelect where+ toTextBuilder settings = \case+ NormalSimpleSelect a b c d e f g ->+ TextBuilders.optLexemes+ [ Just "SELECT",+ fmap (toTextBuilder settings) a,+ fmap (toTextBuilder settings) b,+ fmap (toTextBuilder settings) c,+ fmap (toTextBuilder settings) d,+ fmap (toTextBuilder settings) e,+ fmap (toTextBuilder settings) f,+ fmap (toTextBuilder settings) g+ ]+ ValuesSimpleSelect a -> toTextBuilder settings a+ TableSimpleSelect a -> "TABLE " <> toTextBuilder settings a+ BinSimpleSelect a b c d -> toTextBuilder settings b <> " " <> toTextBuilder settings a <> foldMap (mappend " " . TextBuilders.renderAllOrDistinct) c <> " " <> toTextBuilder settings d++ -- ==== Law+ --+ -- @parser = parseExtended \@SelectClause \<|\> baseSimpleSelect@ - a bare+ -- 'SimpleSelect' is either a @select_clause@ chain of at least one+ -- @UNION@\/@INTERSECT@\/@EXCEPT@ (see 'Extends' below), or, failing+ -- that (no continuation follows), one of the non-chain base cases; it's+ -- never a bare, zero-extension @select_clause@ (that's not a+ -- 'SimpleSelect' at all - see 'SelectClause'). The chain alternative has+ -- to come first: trying 'baseSimpleSelect' alone would succeed on just+ -- the head of a chain and never look for what follows.+ parser settings = parseExtended @SelectClause settings <|> parseBase settings++-- |+-- The @simple_select@ productions that don't left-recurse through+-- @select_clause@ - i.e. everything but @select_clause BINOP+-- select_clause@. "PostgresqlSyntax.Ast.SelectClause" reaches these+-- through this class method, which is why 'SimpleSelect' needs no helper+-- export.+instance LeftRecursive SimpleSelect where+ parseBase settings =+ asum+ [ do+ Parsers.keyword "select"+ Parsers.notFollowedBy $ Parsers.satisfy isAlphaNum+ Parser.endHead+ targeting <- optional (Parsers.space1 *> parser settings)+ intoClause <- optional (Parsers.space1 *> parser settings)+ fromClause <- optional (Parsers.space1 *> parser settings)+ whereClause <- optional (Parsers.space1 *> parser settings)+ groupClause <- optional (Parsers.space1 *> parser settings)+ havingClause <- optional (Parsers.space1 *> parser settings)+ windowClause <- optional (Parsers.space1 *> parser settings)+ return (NormalSimpleSelect targeting intoClause fromClause whereClause groupClause havingClause windowClause),+ do+ Parsers.keyword "table"+ Parsers.space1+ Parser.endHead+ TableSimpleSelect <$> parser settings,+ ValuesSimpleSelect <$> parser settings+ ]++-- |+-- The left-recursion-eliminated form of @select_clause@: a bare+-- 'SelectClause' (either a parenthesized select, or one of+-- 'SimpleSelect'\'s non-chain cases) is the non-recursive base (@β@),+-- and each @UNION@\/@INTERSECT@\/@EXCEPT@ continuation is a+-- 'SelectBinOp' plus its @ALL@\/@DISTINCT@ qualifier and right operand,+-- applied via 'BinSimpleSelect'.+--+-- Keeps the collect-then-fold shape - parsing every 'SelectChainLink' up+-- front via 'parseExtensionChain', then folding via 'foldChain' - rather+-- than folding as it goes, because this hub's items aren't all one+-- precedence level: @gram.y@ declares @%left UNION EXCEPT@ before (i.e.+-- binding looser than) @%left INTERSECT@ (gram.y:813-814), both+-- left-associative, so a uniform left-to-right fold-as-you-parse would+-- root @a INTERSECT b UNION c@ at @INTERSECT@ and nest @a EXCEPT b EXCEPT+-- c@ to the right - both wrong. 'foldChain' needs the whole flat sequence+-- in hand to sort that out; see its own docs above.+instance Extends SelectClause SimpleSelect where+ parseExtensions settings lhs = foldChain lhs <$> parseLinks settings++-- |+-- One @UNION@\/@INTERSECT@\/@EXCEPT@ continuation, minus the left operand+-- it applies to.+data SelectChainLink = SelectChainLink SelectBinOp (Maybe Bool) SelectClause++parseLinks :: Settings -> Parser (NonEmpty SelectChainLink)+parseLinks settings = parseExtensionChain $ do+ op <- Parsers.space1 *> parser settings <* Parsers.space1+ Parser.endHead+ distinct <- optional (Parsers.allOrDistinct <* Parsers.space1)+ rhs <- parseBase @SelectClause settings+ return (SelectChainLink op distinct rhs)++-- |+-- ==== The precedence fold+--+-- @go@ applies items to the accumulator one at a time, left to right -+-- which by itself is already left-associative for a run of same-operator+-- items, INTERSECT included. What needs help is a /low-precedence/ item+-- (@UNION@\/@EXCEPT@) immediately followed by @INTERSECT@ items: those+-- bind tighter, so they must combine into that item's right operand+-- before @go@ applies it, not become separate steps of @go@'s own fold.+-- @absorbIntersect@ does exactly that - and only that: it leaves an+-- @INTERSECT@ item itself untouched (its rest is handled by @go@'s next+-- iteration, one item at a time), and otherwise absorbs a maximal+-- trailing run of @INTERSECT@ items into the current item's right+-- operand. @go@ then continues from whatever @absorbIntersect@ left+-- unconsumed, and either finishes (if nothing's left - the whole point of+-- ending on 'applyLink' rather than wrapping it back into a+-- @SelectClause@ is that the final combination must be the returned+-- @SimpleSelect@) or continues.+foldChain :: SelectClause -> NonEmpty SelectChainLink -> SimpleSelect+foldChain base (i0 :| is0) = go base i0 is0+ where+ go acc item rest =+ let (absorbedItem, rest') = absorbIntersect item rest+ in case rest' of+ [] -> applyLink acc absorbedItem+ item' : rest'' -> go (SimpleSelectSelectClause (applyLink acc absorbedItem)) item' rest''++ applyLink lhs (SelectChainLink op distinct rhs) = BinSimpleSelect op lhs distinct rhs++ absorbIntersect item@(SelectChainLink IntersectSelectBinOp _ _) rest = (item, rest)+ absorbIntersect (SelectChainLink op distinct rhs) (SelectChainLink IntersectSelectBinOp d rhs' : rest) =+ absorbIntersect (SelectChainLink op distinct (SimpleSelectSelectClause (BinSimpleSelect IntersectSelectBinOp rhs d rhs'))) rest+ absorbIntersect item rest = (item, rest)++instance Qc.Arbitrary SimpleSelect where+ shrink = fmap canonicalize . Qc.genericShrink+ arbitrary =+ canonicalize+ <$> Qc.sized+ ( \n ->+ if n <= 1+ then TableSimpleSelect <$> Qc.arbitrary+ else+ Qc.oneof+ [ NormalSimpleSelect+ <$> Qc.arbitrary+ <*> Qc.arbitrary+ <*> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Qc.arbitrary,+ ValuesSimpleSelect <$> Qc.arbitrary,+ TableSimpleSelect <$> Qc.arbitrary,+ BinSimpleSelect <$> Qc.arbitrary <*> Qc.arbitrary <*> Qc.arbitrary <*> Qc.arbitrary+ ]+ )++-- |+-- Collapses an arbitrary-shaped @BinSimpleSelect@ chain to the shape+-- 'foldChain' actually produces for it (left-associated within each+-- precedence level, @INTERSECT@ binding tighter than @UNION@\/@EXCEPT@ -+-- see 'foldChain' above): 'flattenChain' reduces the chain to its flat+-- sequence of operators and operands regardless of how it's currently+-- nested, and re-folding that sequence with the same 'foldChain' the+-- parser itself uses is by construction the shape @parse . toText@+-- produces. Both 'arbitrary' and 'shrink' can otherwise construct a+-- non-canonical shape, which renders fine but parses back to a+-- different, canonical value and so breaks the roundtrip property.+instance Canonicalizes SimpleSelect where+ canonicalize s@(BinSimpleSelect {}) =+ case flattenChain (SimpleSelectSelectClause s) of+ (headClause, i : is) -> foldChain headClause (i :| is)+ (_, []) -> s+ canonicalize other = other++-- |+-- Reduces a @BinSimpleSelect@ chain, in whatever shape it's currently+-- nested, to its leading operand and the flat, left-to-right sequence of+-- 'SelectChainLink' items that follow it - the inverse of 'foldChain'.+flattenChain :: SelectClause -> (SelectClause, [SelectChainLink])+flattenChain (SimpleSelectSelectClause (BinSimpleSelect op lhs distinct rhs)) =+ let (lhsHead, lhsRest) = flattenChain lhs+ (rhsHead, rhsRest) = flattenChain rhs+ in (lhsHead, lhsRest <> [SelectChainLink op distinct rhsHead] <> rhsRest)+flattenChain c = (c, [])
+ library-internal/PostgresqlSyntax/Ast/SimpleSelect.hs-boot view
@@ -0,0 +1,26 @@+{-# OPTIONS_GHC -Wno-orphans #-}++module PostgresqlSyntax.Ast.SimpleSelect where++import PostgresqlSyntax.Algebra (Extends, IsAst, LeftRecursive)+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectClause (SelectClause)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data SimpleSelect++instance Show SimpleSelect++instance Eq SimpleSelect++instance Ord SimpleSelect++instance Data SimpleSelect++instance IsAst SimpleSelect++instance Arbitrary SimpleSelect++instance LeftRecursive SimpleSelect++instance Extends SelectClause SimpleSelect
+ library-internal/PostgresqlSyntax/Ast/SimpleTypename.hs view
@@ -0,0 +1,74 @@+module PostgresqlSyntax.Ast.SimpleTypename where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Bit+import PostgresqlSyntax.Ast.Character+import PostgresqlSyntax.Ast.ConstDatetime+import PostgresqlSyntax.Ast.GenericType+import PostgresqlSyntax.Ast.Iconst+import PostgresqlSyntax.Ast.Interval+import PostgresqlSyntax.Ast.Numeric+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- SimpleTypename:+-- | GenericType+-- | Numeric+-- | Bit+-- | Character+-- | ConstDatetime+-- | ConstInterval opt_interval+-- | ConstInterval '(' Iconst ')'+-- ConstInterval:+-- | INTERVAL+-- @+data SimpleTypename+ = GenericTypeSimpleTypename GenericType+ | NumericSimpleTypename Numeric+ | BitSimpleTypename Bit+ | CharacterSimpleTypename Character+ | ConstDatetimeSimpleTypename ConstDatetime+ | ConstIntervalSimpleTypename (Either (Maybe Interval) Iconst)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SimpleTypename where+ toTextBuilder settings = \case+ GenericTypeSimpleTypename a -> toTextBuilder settings a+ NumericSimpleTypename a -> toTextBuilder settings a+ BitSimpleTypename a -> toTextBuilder settings a+ CharacterSimpleTypename a -> toTextBuilder settings a+ ConstDatetimeSimpleTypename a -> toTextBuilder settings a+ ConstIntervalSimpleTypename a -> "INTERVAL" <> either (TextBuilders.suffixMaybe (toTextBuilder settings)) (mappend " " . TextBuilders.renderInParens . toTextBuilder settings) a+ parser settings =+ asum+ [ do+ Parsers.keyword "interval"+ Parser.endHead+ asum+ [ ConstIntervalSimpleTypename <$> Right <$> (Parsers.space *> Parsers.inParens (parser settings)),+ ConstIntervalSimpleTypename <$> Left <$> optional (Parsers.space *> parser settings)+ ],+ ConstDatetimeSimpleTypename <$> parser settings,+ NumericSimpleTypename <$> parser settings,+ BitSimpleTypename <$> parser settings,+ CharacterSimpleTypename <$> parser settings,+ GenericTypeSimpleTypename <$> parser settings+ ]++instance Qc.Arbitrary SimpleTypename where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ GenericTypeSimpleTypename <$> Qc.arbitrary,+ NumericSimpleTypename <$> Qc.arbitrary,+ BitSimpleTypename <$> Qc.arbitrary,+ CharacterSimpleTypename <$> Qc.arbitrary,+ ConstDatetimeSimpleTypename <$> Qc.arbitrary,+ ConstIntervalSimpleTypename <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SortBy.hs view
@@ -0,0 +1,54 @@+module PostgresqlSyntax.Ast.SortBy where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.AscDesc+import PostgresqlSyntax.Ast.NullsOrder+import PostgresqlSyntax.Ast.QualAllOp+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude hiding (filter, many, some, sortBy, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- sortby:+-- | a_expr USING qual_all_Op opt_nulls_order+-- | a_expr opt_asc_desc opt_nulls_order+-- @+data SortBy+ = UsingSortBy AExpr QualAllOp (Maybe NullsOrder)+ | AscDescSortBy AExpr (Maybe AscDesc) (Maybe NullsOrder)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SortBy where+ toTextBuilder settings = \case+ UsingSortBy a b c -> toTextBuilder settings a <> " USING " <> toTextBuilder settings b <> TextBuilders.suffixMaybe (toTextBuilder settings) c+ AscDescSortBy a b c -> toTextBuilder settings a <> TextBuilders.suffixMaybe (toTextBuilder settings) b <> TextBuilders.suffixMaybe (toTextBuilder settings) c+ parser settings = do+ a <- parser settings+ asum+ [ do+ Parsers.space1+ Parsers.keyword "using"+ Parsers.space1+ Parser.endHead+ b <- parser settings+ c <- optional (Parsers.space1 *> parser settings)+ return (UsingSortBy a b c),+ do+ b <- optional (Parsers.space1 *> parser settings)+ c <- optional (Parsers.space1 *> parser settings)+ return (AscDescSortBy a b c)+ ]++instance Qc.Arbitrary SortBy where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ UsingSortBy <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary <*> Qc.arbitrary,+ AscDescSortBy <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SortClause.hs view
@@ -0,0 +1,35 @@+module PostgresqlSyntax.Ast.SortClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.SortBy+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude hiding (filter, many, some, sortBy, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- sort_clause:+-- | ORDER BY sortby_list+--+-- sortby_list:+-- | sortby+-- | sortby_list ',' sortby+-- @+newtype SortClause = SortClause (NonEmpty SortBy)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SortClause where+ toTextBuilder settings (SortClause a) = "ORDER BY " <> TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = do+ Parsers.keyphrase "order by"+ Parser.endHead+ Parsers.space1+ SortClause <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary SortClause where+ shrink = Qc.genericShrink+ arbitrary = SortClause <$> Gens.nonEmptyUpTo 7 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/SubType.hs view
@@ -0,0 +1,33 @@+module PostgresqlSyntax.Ast.SubType where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- sub_type:+-- | ANY+-- | SOME+-- | ALL+-- @+data SubType = AnySubType | SomeSubType | AllSubType+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst SubType where+ toTextBuilder _settings = \case+ AnySubType -> "ANY"+ SomeSubType -> "SOME"+ AllSubType -> "ALL"+ parser _settings =+ asum+ [ AnySubType <$ Parsers.keyword "any",+ SomeSubType <$ Parsers.keyword "some",+ AllSubType <$ Parsers.keyword "all"+ ]++instance Qc.Arbitrary SubType where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/SubqueryOp.hs view
@@ -0,0 +1,53 @@+module PostgresqlSyntax.Ast.SubqueryOp where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AllOp+import PostgresqlSyntax.Ast.AnyOperator+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- subquery_Op:+-- | all_Op+-- | OPERATOR '(' any_operator ')'+-- | LIKE+-- | NOT_LA LIKE+-- | ILIKE+-- | NOT_LA ILIKE+-- @+data SubqueryOp+ = AllSubqueryOp AllOp+ | AnySubqueryOp AnyOperator+ | LikeSubqueryOp Bool+ | IlikeSubqueryOp Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SubqueryOp where+ toTextBuilder settings = \case+ AllSubqueryOp a -> toTextBuilder settings a+ AnySubqueryOp a -> "OPERATOR " <> TextBuilders.renderInParens (toTextBuilder settings a)+ LikeSubqueryOp a -> bool "" "NOT " a <> "LIKE"+ IlikeSubqueryOp a -> bool "" "NOT " a <> "ILIKE"+ parser settings =+ asum+ [ AnySubqueryOp <$> (Parsers.keyword "operator" *> Parsers.space *> Parser.endHead *> Parsers.inParens (parser settings)),+ do+ a <- Parsers.trueIfPresent (Parsers.keyword "not" *> Parsers.space1)+ LikeSubqueryOp a <$ Parsers.keyword "like" <|> IlikeSubqueryOp a <$ Parsers.keyword "ilike",+ AllSubqueryOp <$> parser settings+ ]++instance Qc.Arbitrary SubqueryOp where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ AllSubqueryOp <$> Qc.arbitrary,+ AnySubqueryOp <$> Qc.arbitrary,+ LikeSubqueryOp <$> Qc.arbitrary,+ IlikeSubqueryOp <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SubstrList.hs view
@@ -0,0 +1,45 @@+module PostgresqlSyntax.Ast.SubstrList where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.SubstrListFromFor+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- substr_list:+-- | a_expr substr_from substr_for+-- | a_expr substr_for substr_from+-- | a_expr substr_from+-- | a_expr substr_for+-- | expr_list+-- | EMPTY+-- @+data SubstrList+ = ExprSubstrList AExpr SubstrListFromFor+ | ExprListSubstrList ExprList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SubstrList where+ toTextBuilder settings = \case+ ExprSubstrList a b -> toTextBuilder settings a <> " " <> toTextBuilder settings b+ ExprListSubstrList a -> toTextBuilder settings a+ parser settings =+ asum+ [ ExprSubstrList <$> Parser.wrapToHead (parser settings) <*> (Parsers.space1 *> parser settings),+ ExprListSubstrList <$> parser settings+ ]++instance Qc.Arbitrary SubstrList where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExprSubstrList <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ ExprListSubstrList <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SubstrListFromFor.hs view
@@ -0,0 +1,63 @@+module PostgresqlSyntax.Ast.SubstrListFromFor where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | a_expr substr_from substr_for+-- | a_expr substr_for substr_from+-- | a_expr substr_from+-- | a_expr substr_for+-- @+data SubstrListFromFor+ = FromForSubstrListFromFor AExpr AExpr+ | ForFromSubstrListFromFor AExpr AExpr+ | FromSubstrListFromFor AExpr+ | ForSubstrListFromFor AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SubstrListFromFor where+ toTextBuilder settings = \case+ FromForSubstrListFromFor a b -> "FROM " <> toTextBuilder settings a <> " FOR " <> toTextBuilder settings b+ ForFromSubstrListFromFor a b -> "FOR " <> toTextBuilder settings a <> " FROM " <> toTextBuilder settings b+ FromSubstrListFromFor a -> "FROM " <> toTextBuilder settings a+ ForSubstrListFromFor a -> "FOR " <> toTextBuilder settings a+ parser settings =+ asum+ [ do+ a <- substrFrom+ asum+ [ do+ b <- Parsers.space1 *> substrFor+ return (FromForSubstrListFromFor a b),+ return (FromSubstrListFromFor a)+ ],+ do+ a <- substrFor+ asum+ [ do+ b <- Parsers.space1 *> substrFrom+ return (ForFromSubstrListFromFor a b),+ return (ForSubstrListFromFor a)+ ]+ ]+ where+ substrFrom = Parsers.keyword "from" *> Parsers.space1 *> Parser.endHead *> parser settings+ substrFor = Parsers.keyword "for" *> Parsers.space1 *> Parser.endHead *> parser settings++instance Qc.Arbitrary SubstrListFromFor where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ FromForSubstrListFromFor <$> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ ForFromSubstrListFromFor <$> Gens.downscale Qc.arbitrary <*> Gens.downscale Qc.arbitrary,+ FromSubstrListFromFor <$> Gens.downscale Qc.arbitrary,+ ForSubstrListFromFor <$> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/SymbolicExprBinOp.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.SymbolicExprBinOp where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.MathOp+import PostgresqlSyntax.Ast.QualOp+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++data SymbolicExprBinOp+ = MathSymbolicExprBinOp MathOp+ | QualSymbolicExprBinOp QualOp+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst SymbolicExprBinOp where+ toTextBuilder settings = \case+ MathSymbolicExprBinOp a -> toTextBuilder settings a+ QualSymbolicExprBinOp a -> toTextBuilder settings a+ parser settings =+ QualSymbolicExprBinOp+ <$> parser settings+ <|> MathSymbolicExprBinOp+ <$> parser settings++instance Qc.Arbitrary SymbolicExprBinOp where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ MathSymbolicExprBinOp <$> Qc.arbitrary,+ QualSymbolicExprBinOp <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/TableFuncElement.hs view
@@ -0,0 +1,40 @@+module PostgresqlSyntax.Ast.TableFuncElement where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AnyName+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.Typename+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- TableFuncElement:+-- | ColId Typename opt_collate_clause+-- @+--+-- @opt_collate_clause@ is a bare alias to 'PostgresqlSyntax.Ast.AnyName'.+data TableFuncElement = TableFuncElement Ident Typename (Maybe AnyName)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TableFuncElement where+ toTextBuilder settings (TableFuncElement a b c) = toTextBuilder settings a <> " " <> toTextBuilder settings b <> TextBuilders.suffixMaybe collateClause c+ where+ collateClause a' = "COLLATE " <> toTextBuilder settings a'+ parser settings = do+ a <- Parser.wrapToHead (colId settings)+ Parsers.space1+ b <- parser settings+ c <- optional (Parsers.space1 *> collateClause)+ return (TableFuncElement a b c)+ where+ collateClause = Parsers.keyword "collate" *> Parsers.space1 *> Parser.endHead *> parser settings++instance Qc.Arbitrary TableFuncElement where+ shrink = Qc.genericShrink+ arbitrary = TableFuncElement <$> arbitrary <*> arbitrary <*> Gens.terminatingMaybe arbitrary
+ library-internal/PostgresqlSyntax/Ast/TableFuncElementList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.TableFuncElementList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.TableFuncElement+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- TableFuncElementList:+-- | TableFuncElement+-- | TableFuncElementList ',' TableFuncElement+-- @+newtype TableFuncElementList = TableFuncElementList (NonEmpty TableFuncElement)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TableFuncElementList where+ toTextBuilder settings (TableFuncElementList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = TableFuncElementList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary TableFuncElementList where+ shrink = Qc.genericShrink+ arbitrary = TableFuncElementList <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/TableRef.hs view
@@ -0,0 +1,165 @@+module PostgresqlSyntax.Ast.TableRef+ ( TableRef (..),+ )+where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.AliasClause+import PostgresqlSyntax.Ast.FuncAliasClause+import PostgresqlSyntax.Ast.FuncTable+import PostgresqlSyntax.Ast.JoinedTable+import PostgresqlSyntax.Ast.RelationExpr+import {-# SOURCE #-} PostgresqlSyntax.Ast.SelectWithParens (SelectWithParens)+import PostgresqlSyntax.Ast.TablesampleClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude hiding (filter, head, many, some, tail, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | relation_expr opt_alias_clause+-- | relation_expr opt_alias_clause tablesample_clause+-- | func_table func_alias_clause+-- | LATERAL_P func_table func_alias_clause+-- | xmltable opt_alias_clause+-- | LATERAL_P xmltable opt_alias_clause+-- | select_with_parens opt_alias_clause+-- | LATERAL_P select_with_parens opt_alias_clause+-- | joined_table+-- | '(' joined_table ')' alias_clause+--+-- TODO: Add xmltable+-- @+data TableRef+ = -- |+ -- @+ -- | relation_expr opt_alias_clause+ -- | relation_expr opt_alias_clause tablesample_clause+ -- @+ RelationExprTableRef RelationExpr (Maybe AliasClause) (Maybe TablesampleClause)+ | -- |+ -- @+ -- | func_table func_alias_clause+ -- | LATERAL_P func_table func_alias_clause+ -- @+ FuncTableRef Bool FuncTable (Maybe FuncAliasClause)+ | -- |+ -- @+ -- | select_with_parens opt_alias_clause+ -- | LATERAL_P select_with_parens opt_alias_clause+ -- @+ SelectTableRef Bool SelectWithParens (Maybe AliasClause)+ | -- |+ -- @+ -- | joined_table+ -- | '(' joined_table ')' alias_clause+ -- @+ JoinTableRef JoinedTable (Maybe AliasClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TableRef where+ toTextBuilder settings = \case+ RelationExprTableRef a b c ->+ TextBuilders.optLexemes+ [ Just (toTextBuilder settings a),+ fmap (toTextBuilder settings) b,+ fmap (toTextBuilder settings) c+ ]+ FuncTableRef a b c ->+ TextBuilders.optLexemes+ [ if a then Just "LATERAL" else Nothing,+ Just (toTextBuilder settings b),+ fmap (toTextBuilder settings) c+ ]+ SelectTableRef a b c ->+ TextBuilders.optLexemes+ [ if a then Just "LATERAL" else Nothing,+ Just (toTextBuilder settings b),+ fmap (toTextBuilder settings) c+ ]+ JoinTableRef a b -> case b of+ Just c -> TextBuilders.renderInParens (toTextBuilder settings a) <> " " <> toTextBuilder settings c+ Nothing -> toTextBuilder settings a++ parser settings = Parser.label "table reference" (parseMaybeExtended @JoinedTable settings)++-- |+-- 'PostgresqlSyntax.Ast.JoinedTable' embeds trivially into a bare,+-- alias-less 'TableRef' (@joined_table@ is one of @table_ref@'s+-- alternatives), and a 'TableRef' of that exact shape is recognizable back+-- as one. See 'PostgresqlSyntax.Algebra.Extends' for how this is used to+-- fold a chain of joins onto a leading 'TableRef'.+instance Refines JoinedTable TableRef where+ embed a = JoinTableRef a Nothing+ project = \case+ JoinTableRef a Nothing -> Just a+ _ -> Nothing++-- |+-- Every @table_ref@ production except the left-recursive ones (those are+-- the @joined_table@ continuations, hosted by+-- "PostgresqlSyntax.Ast.JoinedTable"\'s+-- 'PostgresqlSyntax.Algebra.Extends' instance).+--+-- The two @joined_table@-shaped alternatives here are /not/ left-recursive:+-- both begin with a parenthesis, so neither can loop back into this parser+-- without consuming input. They reach @'(' joined_table ')'@ through+-- 'JoinedTable'\'s own 'parseBase' rather than through a+-- 'JoinedTable' helper export.+instance LeftRecursive TableRef where+ parseBase settings =+ asum+ [ lateralTableRef,+ Parser.wrapToHead nonLateralTableRef,+ relationExprTableRef,+ joinedTableWithAliasTableRef,+ inParensJoinedTableTableRef+ ]+ where+ relationExprTableRef = do+ relationExpr <- parser settings+ Parser.endHead+ optAliasClause <- optional (Parsers.space1 *> parser settings)+ optTablesampleClause <- optional (Parsers.space1 *> parser settings)+ return (RelationExprTableRef relationExpr optAliasClause optTablesampleClause)+ lateralTableRef = do+ Parsers.keyword "lateral"+ Parsers.space1+ Parser.endHead+ lateralableTableRef True+ nonLateralTableRef = lateralableTableRef False+ lateralableTableRef lateral =+ asum+ [ do+ a <- parser settings+ b <- optional (Parsers.space1 *> parser settings)+ return (FuncTableRef lateral a b),+ do+ select <- parser settings+ optAliasClause <- optional $ Parsers.space1 *> parser settings+ return (SelectTableRef lateral select optAliasClause)+ ]+ inParensJoinedTableTableRef = JoinTableRef <$> parseBase @JoinedTable settings <*> pure Nothing+ joinedTableWithAliasTableRef = do+ jt <- Parser.wrapToHead (Parsers.inParens (parser settings))+ Parsers.space1+ alias <- parser settings+ return (JoinTableRef jt (Just alias))++instance Qc.Arbitrary TableRef where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.sized $ \n ->+ if n <= 1+ then RelationExprTableRef <$> Qc.arbitrary <*> pure Nothing <*> pure Nothing+ else+ Qc.oneof+ [ RelationExprTableRef <$> Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary,+ FuncTableRef <$> Qc.arbitrary <*> Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary,+ SelectTableRef <$> Qc.arbitrary <*> Gens.downscale Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary,+ JoinTableRef <$> Qc.arbitrary <*> Gens.terminatingMaybe Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/TableRef.hs-boot view
@@ -0,0 +1,24 @@+module PostgresqlSyntax.Ast.TableRef where++import PostgresqlSyntax.Algebra (IsAst, LeftRecursive, Refines)+import {-# SOURCE #-} PostgresqlSyntax.Ast.JoinedTable (JoinedTable)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data TableRef++instance Show TableRef++instance Eq TableRef++instance Ord TableRef++instance Data TableRef++instance IsAst TableRef++instance Arbitrary TableRef++instance Refines JoinedTable TableRef++instance LeftRecursive TableRef
+ library-internal/PostgresqlSyntax/Ast/TablesampleClause.hs view
@@ -0,0 +1,47 @@+module PostgresqlSyntax.Ast.TablesampleClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.FuncName+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- tablesample_clause:+-- | TABLESAMPLE func_name '(' expr_list ')' opt_repeatable_clause+-- @+--+-- @opt_repeatable_clause@ is a bare alias to 'PostgresqlSyntax.Ast.AExpr'.+data TablesampleClause = TablesampleClause FuncName ExprList (Maybe AExpr)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TablesampleClause where+ toTextBuilder settings (TablesampleClause a b c) =+ "TABLESAMPLE " <> toTextBuilder settings a <> " (" <> toTextBuilder settings b <> ")" <> TextBuilders.suffixMaybe repeatableClause c+ where+ repeatableClause a' = "REPEATABLE (" <> toTextBuilder settings a' <> ")"+ parser settings = do+ Parsers.keyword "tablesample"+ Parsers.space1+ Parser.endHead+ a <- parser settings+ Parsers.space+ b <- Parsers.inParens (parser settings)+ c <- optional (Parsers.space *> repeatableClause)+ return (TablesampleClause a b c)+ where+ repeatableClause = do+ Parsers.keyword "repeatable"+ Parsers.space+ Parsers.inParens (Parser.endHead *> parser settings)++instance Qc.Arbitrary TablesampleClause where+ shrink = Qc.genericShrink+ arbitrary = TablesampleClause <$> arbitrary <*> arbitrary <*> Gens.terminatingMaybe (Gens.downscale arbitrary)
+ library-internal/PostgresqlSyntax/Ast/TargetEl.hs view
@@ -0,0 +1,70 @@+module PostgresqlSyntax.Ast.TargetEl 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.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- target_el:+-- | a_expr AS ColLabel+-- | a_expr IDENT+-- | a_expr+-- | '*'+-- @+data TargetEl+ = AliasedExprTargetEl AExpr Ident+ | ImplicitlyAliasedExprTargetEl AExpr Ident+ | ExprTargetEl AExpr+ | AsteriskTargetEl+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TargetEl where+ toTextBuilder settings = \case+ AliasedExprTargetEl a b -> toTextBuilder settings a <> " AS " <> toTextBuilder settings b+ ImplicitlyAliasedExprTargetEl a b -> toTextBuilder settings a <> " " <> toTextBuilder settings b+ ExprTargetEl a -> toTextBuilder settings a+ AsteriskTargetEl -> "*"++ parser settings =+ Parser.label "target" $+ asum+ [ do+ expr <- parser settings+ asum+ [ do+ Parsers.space1+ asum+ [ AliasedExprTargetEl expr <$> (Parsers.keyword "as" *> Parsers.space1 *> Parser.endHead *> colLabel),+ ImplicitlyAliasedExprTargetEl expr <$> parser settings+ ],+ pure (ExprTargetEl expr)+ ],+ AsteriskTargetEl <$ Parsers.char '*'+ ]+ where+ -- Duplicated from "PostgresqlSyntax.Parsing"'s @colLabel@ (a+ -- bare-aliased 'PostgresqlSyntax.Ast.Ident' whose own, more+ -- permissive parser lives above this module in the dependency+ -- order), mirroring the 'PostgresqlSyntax.Ast.AnyName' precedent.+ colLabel =+ Parser.label "column label" $+ Parsers.keywordNameFromSet UnquotedIdent KeywordSet.keyword+ <|> parser settings++instance Qc.Arbitrary TargetEl where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ pure AsteriskTargetEl,+ AliasedExprTargetEl <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ ImplicitlyAliasedExprTargetEl <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ ExprTargetEl <$> Gens.downscale Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/TargetList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.TargetList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.TargetEl+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- target_list:+-- | target_el+-- | target_list ',' target_el+-- @+newtype TargetList = TargetList (NonEmpty TargetEl)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TargetList where+ toTextBuilder settings (TargetList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = TargetList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary TargetList where+ shrink = Qc.genericShrink+ arbitrary = TargetList <$> Gens.nonEmptyUpTo 7 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/Targeting.hs view
@@ -0,0 +1,66 @@+module PostgresqlSyntax.Ast.Targeting where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.HsTargetList+import PostgresqlSyntax.Ast.TargetList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- simple_select:+-- | SELECT opt_all_clause opt_target_list ...+-- | SELECT distinct_clause target_list ...+--+-- distinct_clause:+-- | DISTINCT+-- | DISTINCT ON '(' expr_list ')'+-- @+data Targeting+ = NormalTargeting HsTargetList+ | AllTargeting (Maybe TargetList)+ | DistinctTargeting (Maybe ExprList) TargetList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Targeting where+ toTextBuilder settings = \case+ NormalTargeting a -> toTextBuilder settings a+ AllTargeting a -> "ALL" <> TextBuilders.suffixMaybe (toTextBuilder settings) a+ DistinctTargeting a b -> "DISTINCT" <> TextBuilders.suffixMaybe onExpressionsClause a <> " " <> toTextBuilder settings b+ where+ onExpressionsClause a = "ON (" <> toTextBuilder settings a <> ")"+ parser settings = distinct <|> allWithTargetList <|> allP <|> normal+ where+ normal = NormalTargeting <$> parser settings+ allWithTargetList = do+ Parsers.keyword "all"+ Parsers.space1+ AllTargeting . Just <$> parser settings+ allP = Parsers.keyword "all" $> AllTargeting Nothing+ distinct = do+ Parsers.keyword "distinct"+ Parsers.space1+ Parser.endHead+ optOn <- optional (onExpressionsClause <* Parsers.space1)+ targetList <- parser settings+ return (DistinctTargeting optOn targetList)+ onExpressionsClause = do+ Parsers.keyword "on"+ Parsers.space1+ Parser.endHead+ ExprList <$> Parsers.inParens (Parsers.sep1 Parsers.commaSeparator (parser settings))++instance Qc.Arbitrary Targeting where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ NormalTargeting <$> Qc.arbitrary,+ AllTargeting <$> Qc.arbitrary,+ DistinctTargeting <$> Gens.terminatingMaybe Qc.arbitrary <*> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/Timezone.hs view
@@ -0,0 +1,30 @@+module PostgresqlSyntax.Ast.Timezone where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_timezone:+-- | WITH_LA TIME ZONE+-- | WITHOUT TIME ZONE+-- | EMPTY+-- @+newtype Timezone = Timezone Bool+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Timezone where+ toTextBuilder _settings (Timezone a) = if a then "WITHOUT TIME ZONE" else "WITH TIME ZONE"+ parser _settings =+ Timezone+ <$> asum+ [ False <$ Parsers.keyphrase "with time zone",+ True <$ Parsers.keyphrase "without time zone"+ ]++instance Qc.Arbitrary Timezone where+ shrink = Qc.genericShrink+ arbitrary = Timezone <$> arbitrary
+ library-internal/PostgresqlSyntax/Ast/TrimList.hs view
@@ -0,0 +1,45 @@+module PostgresqlSyntax.Ast.TrimList where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- trim_list:+-- | a_expr FROM expr_list+-- | FROM expr_list+-- | expr_list+-- @+data TrimList+ = ExprFromExprListTrimList AExpr ExprList+ | FromExprListTrimList ExprList+ | ExprListTrimList ExprList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TrimList where+ toTextBuilder settings = \case+ ExprFromExprListTrimList a b -> toTextBuilder settings a <> " FROM " <> toTextBuilder settings b+ FromExprListTrimList a -> "FROM " <> toTextBuilder settings a+ ExprListTrimList a -> toTextBuilder settings a+ parser settings =+ asum+ [ ExprFromExprListTrimList <$> Parser.wrapToHead (parser settings) <*> (Parsers.space1 *> Parsers.keyword "from" *> Parsers.space1 *> Parser.endHead *> parser settings),+ FromExprListTrimList <$> (Parsers.keyword "from" *> Parsers.space1 *> Parser.endHead *> parser settings),+ ExprListTrimList <$> parser settings+ ]++instance Qc.Arbitrary TrimList where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExprFromExprListTrimList <$> Gens.downscale Qc.arbitrary <*> Qc.arbitrary,+ FromExprListTrimList <$> Qc.arbitrary,+ ExprListTrimList <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/TrimModifier.hs view
@@ -0,0 +1,33 @@+module PostgresqlSyntax.Ast.TrimModifier where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | TRIM '(' BOTH trim_list ')'+-- | TRIM '(' LEADING trim_list ')'+-- | TRIM '(' TRAILING trim_list ')'+-- @+data TrimModifier = BothTrimModifier | LeadingTrimModifier | TrailingTrimModifier+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst TrimModifier where+ toTextBuilder _settings = \case+ BothTrimModifier -> "BOTH"+ LeadingTrimModifier -> "LEADING"+ TrailingTrimModifier -> "TRAILING"+ parser _settings =+ BothTrimModifier+ <$ Parsers.keyword "both"+ <|> LeadingTrimModifier+ <$ Parsers.keyword "leading"+ <|> TrailingTrimModifier+ <$ Parsers.keyword "trailing"++instance Qc.Arbitrary TrimModifier where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/TypeList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.TypeList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Typename+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- type_list:+-- | Typename+-- | type_list ',' Typename+-- @+newtype TypeList = TypeList (NonEmpty Typename)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TypeList where+ toTextBuilder settings (TypeList a) = TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = TypeList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary TypeList where+ shrink = Qc.genericShrink+ arbitrary = TypeList <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/Typename.hs view
@@ -0,0 +1,46 @@+module PostgresqlSyntax.Ast.Typename where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.SimpleTypename+import PostgresqlSyntax.Ast.TypenameArrayDimensions+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import PostgresqlSyntax.Settings (resolveNullabilityMarkers)+import qualified Test.QuickCheck as Qc++data Typename+ = Typename+ Bool+ SimpleTypename+ Bool+ (Maybe (TypenameArrayDimensions, Bool))+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Typename where+ toTextBuilder settings (Typename setof base typeNullable arrayDims) =+ bool "" "SETOF " setof+ <> toTextBuilder settings base+ <> marker typeNullable+ <> foldMap renderArray arrayDims+ where+ markersOn = resolveNullabilityMarkers settings+ marker flag = if markersOn && flag then "?" else mempty+ renderArray (dims, flag) = toTextBuilder settings dims <> marker flag+ parser settings = do+ setof <- option False (Parsers.keyword "setof" *> Parsers.space1 $> True)+ base <- parser settings+ Parser.endHead+ let marker = if resolveNullabilityMarkers settings then Parsers.trueIfPresent (Parsers.char '?') else pure False+ typeNullable <- marker+ arrayDims <- optional $ do+ dims <- parser settings+ flag <- marker+ pure (dims, flag)+ pure (Typename setof base typeNullable arrayDims)++instance Qc.Arbitrary Typename where+ shrink = Qc.genericShrink+ arbitrary = Typename <$> arbitrary <*> arbitrary <*> pure False <*> ((\a -> (,) a False) <$$> arbitrary)+ where+ (<$$>) = fmap . fmap
+ library-internal/PostgresqlSyntax/Ast/TypenameArrayDimensions.hs view
@@ -0,0 +1,45 @@+module PostgresqlSyntax.Ast.TypenameArrayDimensions where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ArrayBounds+import PostgresqlSyntax.Ast.Iconst+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- Part of the Typename specification responsible for the choice between the following:+-- | opt_array_bounds+-- | ARRAY '[' Iconst ']'+-- | ARRAY+-- @+data TypenameArrayDimensions+ = BoundsTypenameArrayDimensions ArrayBounds+ | ExplicitTypenameArrayDimensions (Maybe Iconst)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst TypenameArrayDimensions where+ toTextBuilder settings = \case+ BoundsTypenameArrayDimensions a -> toTextBuilder settings a+ ExplicitTypenameArrayDimensions a -> " ARRAY" <> foldMap (TextBuilders.renderInBrackets . toTextBuilder settings) a+ parser settings =+ asum+ [ do+ Parsers.space1+ Parsers.keyword "array"+ Parser.endHead+ ExplicitTypenameArrayDimensions <$> optional (Parsers.space *> Parsers.inBrackets (parser settings)),+ BoundsTypenameArrayDimensions <$> (Parsers.space *> parser settings)+ ]++instance Qc.Arbitrary TypenameArrayDimensions where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ BoundsTypenameArrayDimensions <$> Qc.arbitrary,+ ExplicitTypenameArrayDimensions <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/UpdateStmt.hs view
@@ -0,0 +1,65 @@+module PostgresqlSyntax.Ast.UpdateStmt where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FromClause+import PostgresqlSyntax.Ast.RelationExprOptAlias (RelationExprOptAlias)+import PostgresqlSyntax.Ast.ReturningClause+import PostgresqlSyntax.Ast.SetClauseList+import PostgresqlSyntax.Ast.WhereOrCurrentClause+import {-# SOURCE #-} PostgresqlSyntax.Ast.WithClause (WithClause)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- UpdateStmt:+-- | opt_with_clause UPDATE relation_expr_opt_alias+-- SET set_clause_list+-- from_clause+-- where_or_current_clause+-- returning_clause+-- @+data UpdateStmt = UpdateStmt (Maybe WithClause) RelationExprOptAlias SetClauseList (Maybe FromClause) (Maybe WhereOrCurrentClause) (Maybe ReturningClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst UpdateStmt where+ toTextBuilder settings (UpdateStmt a b c d e f) =+ TextBuilders.prefixMaybe (toTextBuilder settings) a+ <> "UPDATE "+ <> toTextBuilder settings b+ <> " "+ <> "SET "+ <> toTextBuilder settings c+ <> TextBuilders.suffixMaybe (toTextBuilder settings) d+ <> TextBuilders.suffixMaybe (toTextBuilder settings) e+ <> TextBuilders.suffixMaybe (toTextBuilder settings) f+ parser settings = do+ a <- optional (Parser.wrapToHead (parser settings) <* Parsers.space1)+ Parsers.keyword "update"+ Parsers.space1+ Parser.endHead+ b <- parser settings+ Parsers.space1+ Parsers.keyword "set"+ Parsers.space1+ c <- parser settings+ d <- optional (Parsers.space1 *> parser settings)+ e <- optional (Parsers.space1 *> parser settings)+ f <- optional (Parsers.space1 *> parser settings)+ return (UpdateStmt a b c d e f)++instance Qc.Arbitrary UpdateStmt where+ shrink = Qc.genericShrink+ arbitrary =+ UpdateStmt+ <$> Gens.terminatingMaybe (Gens.downscale Qc.arbitrary)+ <*> Qc.arbitrary+ <*> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/UsingClause.hs view
@@ -0,0 +1,28 @@+module PostgresqlSyntax.Ast.UsingClause where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.FromList+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- using_clause:+-- | USING from_list+-- | /*EMPTY*/+-- @+newtype UsingClause = UsingClause FromList+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst UsingClause where+ toTextBuilder settings (UsingClause a) = "USING " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "using"+ Parsers.space1+ UsingClause <$> parser settings++instance Qc.Arbitrary UsingClause where+ shrink = Qc.genericShrink+ arbitrary = UsingClause <$> Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/ValuesClause.hs view
@@ -0,0 +1,40 @@+module PostgresqlSyntax.Ast.ValuesClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- values_clause:+-- | VALUES '(' expr_list ')'+-- | values_clause ',' '(' expr_list ')'+-- @+newtype ValuesClause = ValuesClause (NonEmpty ExprList)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst ValuesClause where+ toTextBuilder settings (ValuesClause a) = "VALUES " <> TextBuilders.commaNonEmpty (TextBuilders.renderInParens . toTextBuilder settings) a+ parser settings = do+ Parsers.keyword "values"+ Parsers.space+ ValuesClause <$> Parsers.sep1 Parsers.commaSeparator row+ where+ row = do+ Parsers.char '('+ Parser.endHead+ Parsers.space+ a <- ExprList <$> Parsers.sep1 Parsers.commaSeparator (parser settings)+ Parsers.space+ Parsers.char ')'+ return a++instance Qc.Arbitrary ValuesClause where+ shrink = Qc.genericShrink+ arbitrary = ValuesClause <$> Gens.nonEmptyUpTo 7 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/VerbalExprBinOp.hs view
@@ -0,0 +1,38 @@+module PostgresqlSyntax.Ast.VerbalExprBinOp where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | LIKE+-- | NOT_LA LIKE+-- | ILIKE+-- | NOT_LA ILIKE+-- | SIMILAR TO+-- | NOT_LA SIMILAR TO+-- @+data VerbalExprBinOp+ = LikeVerbalExprBinOp+ | IlikeVerbalExprBinOp+ | SimilarToVerbalExprBinOp+ deriving (Show, Generic, Eq, Ord, Data, Enum, Bounded)++instance IsAst VerbalExprBinOp where+ toTextBuilder _settings = \case+ LikeVerbalExprBinOp -> "LIKE"+ IlikeVerbalExprBinOp -> "ILIKE"+ SimilarToVerbalExprBinOp -> "SIMILAR TO"+ parser _settings =+ asum+ [ LikeVerbalExprBinOp <$ Parsers.keyword "like",+ IlikeVerbalExprBinOp <$ Parsers.keyword "ilike",+ SimilarToVerbalExprBinOp <$ Parsers.keyphrase "similar to"+ ]++instance Qc.Arbitrary VerbalExprBinOp where+ shrink = Qc.genericShrink+ arbitrary = Qc.elements [minBound .. maxBound]
+ library-internal/PostgresqlSyntax/Ast/WhenClause.hs view
@@ -0,0 +1,35 @@+module PostgresqlSyntax.Ast.WhenClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- when_clause:+-- | WHEN a_expr THEN a_expr+-- @+data WhenClause = WhenClause AExpr AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WhenClause where+ toTextBuilder settings (WhenClause a b) = "WHEN " <> toTextBuilder settings a <> " THEN " <> toTextBuilder settings b+ parser settings = do+ Parsers.keyword "when"+ Parsers.space1+ Parser.endHead+ a <- parser settings+ Parsers.space1+ Parsers.keyword "then"+ Parsers.space1+ b <- parser settings+ return (WhenClause a b)++instance Qc.Arbitrary WhenClause where+ shrink = Qc.genericShrink+ arbitrary = WhenClause <$> Gens.downscale arbitrary <*> Gens.downscale arbitrary
+ library-internal/PostgresqlSyntax/Ast/WhenClauseList.hs view
@@ -0,0 +1,27 @@+module PostgresqlSyntax.Ast.WhenClauseList where++import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.WhenClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- when_clause_list:+-- | when_clause+-- | when_clause_list when_clause+-- @+newtype WhenClauseList = WhenClauseList (NonEmpty WhenClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WhenClauseList where+ toTextBuilder settings (WhenClauseList a) = TextBuilders.spaceNonEmpty (toTextBuilder settings) a+ parser settings = WhenClauseList <$> Parsers.sep1 Parsers.space1 (parser settings)++instance Qc.Arbitrary WhenClauseList where+ shrink = Qc.genericShrink+ arbitrary = WhenClauseList <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/WhereClause.hs view
@@ -0,0 +1,31 @@+module PostgresqlSyntax.Ast.WhereClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import {-# SOURCE #-} PostgresqlSyntax.Ast.AExpr (AExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- where_clause:+-- | WHERE a_expr+-- | /*EMPTY*/+-- @+newtype WhereClause = WhereClause AExpr+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WhereClause where+ toTextBuilder settings (WhereClause a) = "WHERE " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "where"+ Parsers.space1+ Parser.endHead+ WhereClause <$> parser settings++instance Qc.Arbitrary WhereClause where+ shrink = Qc.genericShrink+ arbitrary = WhereClause <$> Gens.downscale Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/WhereOrCurrentClause.hs view
@@ -0,0 +1,52 @@+module PostgresqlSyntax.Ast.WhereOrCurrentClause 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 PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- | WHERE a_expr+-- | WHERE CURRENT_P OF cursor_name+-- | /*EMPTY*/+-- @+--+-- @cursor_name@ is a bare alias to 'PostgresqlSyntax.Ast.Ident'.+data WhereOrCurrentClause+ = ExprWhereOrCurrentClause AExpr+ | CursorWhereOrCurrentClause Ident+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WhereOrCurrentClause where+ toTextBuilder settings = \case+ ExprWhereOrCurrentClause a -> "WHERE " <> toTextBuilder settings a+ CursorWhereOrCurrentClause a -> "WHERE CURRENT OF " <> toTextBuilder settings a+ parser settings = do+ Parsers.keyword "where"+ Parsers.space1+ Parser.endHead+ asum+ [ do+ Parsers.keyword "current"+ Parsers.space1+ Parsers.keyword "of"+ Parsers.space1+ Parser.endHead+ a <- colId settings+ return (CursorWhereOrCurrentClause a),+ ExprWhereOrCurrentClause <$> parser settings+ ]++instance Qc.Arbitrary WhereOrCurrentClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.oneof+ [ ExprWhereOrCurrentClause <$> Gens.downscale Qc.arbitrary,+ CursorWhereOrCurrentClause <$> Qc.arbitrary+ ]
+ library-internal/PostgresqlSyntax/Ast/WindowClause.hs view
@@ -0,0 +1,32 @@+module PostgresqlSyntax.Ast.WindowClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.WindowDefinition+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude hiding (filter, many, some, try)+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- window_clause:+-- | WINDOW window_definition_list+-- | /*EMPTY*/+-- @+newtype WindowClause = WindowClause (NonEmpty WindowDefinition)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WindowClause where+ toTextBuilder settings (WindowClause a) = "WINDOW " <> TextBuilders.commaNonEmpty (toTextBuilder settings) a+ parser settings = do+ Parsers.keyword "window"+ Parser.endHead+ Parsers.space1+ WindowClause <$> Parsers.sep1 Parsers.commaSeparator (parser settings)++instance Qc.Arbitrary WindowClause where+ shrink = Qc.genericShrink+ arbitrary = WindowClause <$> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/WindowDefinition.hs view
@@ -0,0 +1,26 @@+module PostgresqlSyntax.Ast.WindowDefinition where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.WindowSpecification+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- window_definition:+-- | ColId AS window_specification+-- @+data WindowDefinition = WindowDefinition Ident WindowSpecification+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WindowDefinition where+ toTextBuilder settings (WindowDefinition a b) = toTextBuilder settings a <> " AS " <> toTextBuilder settings b+ parser settings = WindowDefinition <$> (colId settings <* Parsers.space1 <* Parsers.keyword "as" <* Parsers.space1 <* Parser.endHead) <*> parser settings++instance Qc.Arbitrary WindowDefinition where+ shrink = Qc.genericShrink+ arbitrary = WindowDefinition <$> arbitrary <*> arbitrary
+ library-internal/PostgresqlSyntax/Ast/WindowExclusionClause.hs view
@@ -0,0 +1,49 @@+module PostgresqlSyntax.Ast.WindowExclusionClause where++import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- opt_window_exclusion_clause:+-- | EXCLUDE CURRENT_P ROW+-- | EXCLUDE GROUP_P+-- | EXCLUDE TIES+-- | EXCLUDE NO OTHERS+-- | EMPTY+-- @+data WindowExclusionClause+ = CurrentRowWindowExclusionClause+ | GroupWindowExclusionClause+ | TiesWindowExclusionClause+ | NoOthersWindowExclusionClause+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WindowExclusionClause where+ toTextBuilder _settings = \case+ CurrentRowWindowExclusionClause -> "EXCLUDE CURRENT ROW"+ GroupWindowExclusionClause -> "EXCLUDE GROUP"+ TiesWindowExclusionClause -> "EXCLUDE TIES"+ NoOthersWindowExclusionClause -> "EXCLUDE NO OTHERS"+ parser _settings =+ CurrentRowWindowExclusionClause+ <$ Parsers.keyphrase "exclude current row"+ <|> GroupWindowExclusionClause+ <$ Parsers.keyphrase "exclude group"+ <|> TiesWindowExclusionClause+ <$ Parsers.keyphrase "exclude ties"+ <|> NoOthersWindowExclusionClause+ <$ Parsers.keyphrase "exclude no others"++instance Qc.Arbitrary WindowExclusionClause where+ shrink = Qc.genericShrink+ arbitrary =+ Qc.elements+ [ CurrentRowWindowExclusionClause,+ GroupWindowExclusionClause,+ TiesWindowExclusionClause,+ NoOthersWindowExclusionClause+ ]
+ library-internal/PostgresqlSyntax/Ast/WindowSpecification.hs view
@@ -0,0 +1,79 @@+module PostgresqlSyntax.Ast.WindowSpecification where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.ExprList+import PostgresqlSyntax.Ast.FrameClause+import PostgresqlSyntax.Ast.Ident+import PostgresqlSyntax.Ast.SortClause+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- window_specification:+-- | '(' opt_existing_window_name opt_partition_clause+-- opt_sort_clause opt_frame_clause ')'+--+-- opt_existing_window_name:+-- | ColId+-- | EMPTY+--+-- opt_partition_clause:+-- | PARTITION BY expr_list+-- | EMPTY+-- @+--+-- @existing_window_name@ and @partition_clause@ are bare aliases to+-- 'PostgresqlSyntax.Ast.Ident' (ColId) and 'PostgresqlSyntax.Ast.ExprList'+-- respectively.+data WindowSpecification = WindowSpecification (Maybe Ident) (Maybe ExprList) (Maybe SortClause) (Maybe FrameClause)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WindowSpecification where+ toTextBuilder settings (WindowSpecification a b c d) =+ TextBuilders.renderInParens $+ TextBuilders.optLexemes+ [ fmap (toTextBuilder settings) a,+ fmap (mappend "PARTITION BY " . toTextBuilder settings) b,+ fmap (toTextBuilder settings) c,+ fmap (toTextBuilder settings) d+ ]+ parser settings =+ Parsers.inParens $+ asum+ [ do+ a <- parser settings+ return (WindowSpecification Nothing Nothing Nothing (Just a)),+ do+ a <- parser settings+ b <- optional (Parsers.space1 *> parser settings)+ return (WindowSpecification Nothing Nothing (Just a) b),+ do+ a <- partitionByClause+ b <- optional (Parsers.space1 *> parser settings)+ c <- optional (Parsers.space1 *> parser settings)+ return (WindowSpecification Nothing (Just a) b c),+ do+ a <- colId settings+ b <- optional (Parsers.space1 *> partitionByClause)+ c <- optional (Parsers.space1 *> parser settings)+ d <- optional (Parsers.space1 *> parser settings)+ return (WindowSpecification (Just a) b c d),+ pure (WindowSpecification Nothing Nothing Nothing Nothing)+ ]+ where+ partitionByClause = Parsers.keyphrase "partition by" *> Parsers.space1 *> Parser.endHead *> (ExprList <$> Parsers.sep1 Parsers.commaSeparator (parser settings))++instance Qc.Arbitrary WindowSpecification where+ shrink = Qc.genericShrink+ arbitrary =+ WindowSpecification+ <$> Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary+ <*> Gens.terminatingMaybe Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/WithClause.hs view
@@ -0,0 +1,36 @@+module PostgresqlSyntax.Ast.WithClause where++import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import PostgresqlSyntax.Ast.CommonTableExpr (CommonTableExpr)+import qualified PostgresqlSyntax.Helpers.Gens as Gens+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.TextBuilders as TextBuilders+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- ==== References+-- @+-- with_clause:+-- | WITH cte_list+-- | WITH_LA cte_list+-- | WITH RECURSIVE cte_list+-- @+data WithClause = WithClause Bool (NonEmpty CommonTableExpr)+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst WithClause where+ toTextBuilder settings (WithClause a b) =+ "WITH " <> bool "" "RECURSIVE " a <> TextBuilders.commaNonEmpty (toTextBuilder settings) b+ parser settings = Parser.label "with clause" $ do+ Parsers.keyword "with"+ Parsers.space1+ Parser.endHead+ recursive <- option False (True <$ Parsers.keyword "recursive" <* Parsers.space1)+ cteList <- Parsers.sep1 Parsers.commaSeparator (parser settings)+ return (WithClause recursive cteList)++instance Qc.Arbitrary WithClause where+ shrink = Qc.genericShrink+ arbitrary = WithClause <$> arbitrary <*> Gens.nonEmptyUpTo 6 Qc.arbitrary
+ library-internal/PostgresqlSyntax/Ast/WithClause.hs-boot view
@@ -0,0 +1,19 @@+module PostgresqlSyntax.Ast.WithClause where++import PostgresqlSyntax.Algebra (IsAst)+import PostgresqlSyntax.Prelude (Data, Eq, Ord, Show)+import Test.QuickCheck (Arbitrary)++data WithClause++instance Show WithClause++instance Eq WithClause++instance Ord WithClause++instance Data WithClause++instance IsAst WithClause++instance Arbitrary WithClause
+ library-internal/PostgresqlSyntax/Ast/Xconst.hs view
@@ -0,0 +1,34 @@+module PostgresqlSyntax.Ast.Xconst where++import qualified Data.Text as Text+import qualified HeadedMegaparsec as Parser+import PostgresqlSyntax.Algebra+import qualified PostgresqlSyntax.Helpers.Parsers as Parsers+import qualified PostgresqlSyntax.Helpers.Shrinks as Shrinks+import qualified PostgresqlSyntax.Predicate as Predicate+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc+import qualified TextBuilder++-- |+-- ==== References+-- @+-- XCONST+-- @+newtype Xconst = Xconst Text+ deriving (Show, Generic, Eq, Ord, Data)++instance IsAst Xconst where+ toTextBuilder _settings (Xconst a) = "X'" <> TextBuilder.text a <> "'"+ parser _settings = Parser.label "hex literal" $ do+ Parsers.string' "x'"+ Parser.endHead+ a <- Parsers.takeWhile1P (Just "Hex digit") Predicate.hexDigit+ Parsers.char '\''+ return (Xconst a)++instance Qc.Arbitrary Xconst where+ shrink (Xconst a) = Xconst <$> Shrinks.nonEmptyText a+ arbitrary = do+ len <- Qc.choose (1, 100)+ Xconst . Text.pack <$> Qc.vectorOf len (Qc.elements "0123456789abcdefABCDEF")
+ library-internal/PostgresqlSyntax/CharSet.hs view
@@ -0,0 +1,21 @@+module PostgresqlSyntax.CharSet where++import qualified Data.Text as Text+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude++{-# NOINLINE symbolicBinOp #-}+symbolicBinOp :: HashSet Char+symbolicBinOp = KeywordSet.symbolicBinOp & toList & mconcat & Text.unpack & fromList++{-# NOINLINE hexDigit #-}+hexDigit :: HashSet Char+hexDigit = fromList "0123456789abcdefABCDEF"++{-# NOINLINE op #-}+op :: HashSet Char+op = fromList "+-*/<>=~!@#%^&|`?"++{-# NOINLINE prohibitionLiftingOp #-}+prohibitionLiftingOp :: HashSet Char+prohibitionLiftingOp = fromList "~!@#%^&|`?"
+ library-internal/PostgresqlSyntax/Extras/Error.hs view
@@ -0,0 +1,40 @@+-- |+-- Generic helpers for Error handling in HeadedMegaparsec.+module PostgresqlSyntax.Extras.Error where++import PostgresqlSyntax.Prelude hiding (head)+import Text.Megaparsec++-- | Render all Megaparsec parsing errors as a list of position ('Text.Megaparsec.SourcePos') and messages.+errorBundlePrettyStruct ::+ forall s e.+ ( VisualStream s,+ TraversableStream s,+ ShowErrorComponent e+ ) =>+ -- | Parse error bundle to display+ ParseErrorBundle s e ->+ -- | Textual rendition of the bundle+ NonEmpty (SourcePos, String)+errorBundlePrettyStruct ParseErrorBundle {..} =+ fst $ attachSourcePosAndMessage renderError bundleErrors bundlePosState+ where+ renderError epos e = (epos, parseErrorTextPretty e)++-- | A custom version of 'Text.Megaparsec.attachSourcePos' to provide the NonEmpty list of errors with their position while only traversing the errors list once.+attachSourcePosAndMessage ::+ (TraversableStream s) =>+ -- | Format function for a single 'ParseError' and its 'SourcePos'+ (SourcePos -> ParseError s e -> (SourcePos, String)) ->+ -- | The collection of items+ NonEmpty (ParseError s e) ->+ -- | Initial 'PosState'+ PosState s ->+ -- | The collection with 'SourcePos'es added and the final 'PosState'+ (NonEmpty (SourcePos, String), PosState s)+attachSourcePosAndMessage format xs pst0 =+ swap $ mapAccumL step pst0 xs+ where+ step pst a =+ let pst' = reachOffsetNoLine (errorOffset a) pst+ in (pst', format (pstateSourcePos pst') a)
+ library-internal/PostgresqlSyntax/Extras/HeadedMegaparsec.hs view
@@ -0,0 +1,133 @@+{-# OPTIONS_GHC -Wno-redundant-constraints -Wno-dodgy-imports -Wno-unused-imports #-}++-- |+-- Generic helpers for HeadedMegaparsec.+module PostgresqlSyntax.Extras.HeadedMegaparsec where++import Control.Applicative.Combinators hiding (some)+import Control.Applicative.Combinators.NonEmpty+import qualified Data.Text as Text+import HeadedMegaparsec hiding (string)+import PostgresqlSyntax.Extras.Error (errorBundlePrettyStruct)+import PostgresqlSyntax.Prelude hiding (bit, expr, filter, head, many, option, some, sortBy, tail, try)+import Text.Megaparsec (Parsec, Stream, TraversableStream, VisualStream)+import qualified Text.Megaparsec as Megaparsec+import qualified Text.Megaparsec.Char as MegaparsecChar+import qualified Text.Megaparsec.Char.Lexer as MegaparsecLexer++-- $setup+-- >>> testParser parser = either putStr print . run parser++-- * Executors++run :: (Ord err, VisualStream strm, TraversableStream strm, Megaparsec.ShowErrorComponent err) => HeadedParsec err strm a -> strm -> Either String a+run p = first Megaparsec.errorBundlePretty . runParser p++-- |+-- Run the parser but returns all the error messages separated, along with their offset in the parsed message.+runParserWithErrorPos :: (Show (Megaparsec.Token strm), Show e, Ord e, VisualStream strm, TraversableStream strm, Megaparsec.ShowErrorComponent e) => HeadedParsec e strm a -> strm -> Either (NonEmpty (Int, String)) a+runParserWithErrorPos p s = case runParser p s of+ Left err -> Left (extractor <$> Megaparsec.bundleErrors err)+ Right v -> Right v+ where+ extractor x = (Megaparsec.errorOffset x, Megaparsec.parseErrorPretty x)++-- |+-- Like 'runParserWithErrorPos' but pairs each error with its+-- ('Megaparsec.SourcePos') in the parsed message instead of a raw offset.+runParserWithSourcePosError :: (Show (Megaparsec.Token strm), Show e, Ord e, VisualStream strm, TraversableStream strm, Megaparsec.ShowErrorComponent e) => HeadedParsec e strm a -> strm -> Either (NonEmpty (Megaparsec.SourcePos, String)) a+runParserWithSourcePosError p s = case runParser p s of+ Left err -> Left (errorBundlePrettyStruct err)+ Right v -> Right v++runParser :: (Ord e, VisualStream strm, TraversableStream strm) => HeadedParsec e strm a -> strm -> Either (Megaparsec.ParseErrorBundle strm e) a+runParser p = Megaparsec.runParser (toParsec p <* Megaparsec.eof) ""++-- |+-- Require the given parser to consume all remaining input (after optional+-- surrounding whitespace), asserting the parse reaches end-of-input.+totally :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => HeadedParsec err strm a -> HeadedParsec err strm a+totally p = space *> p <* HeadedMegaparsec.endHead <* space <* eof++-- * Primitives++-- |+-- Lifted megaparsec\'s `Megaparsec.eof`.+eof :: (Ord err, Stream strm) => HeadedParsec err strm ()+eof = parse Megaparsec.eof++-- |+-- Lifted megaparsec\'s `Megaparsec.space`.+space :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => HeadedParsec err strm ()+space = parse MegaparsecChar.space++-- |+-- Lifted megaparsec\'s `Megaparsec.space1`.+space1 :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => HeadedParsec err strm ()+space1 = parse MegaparsecChar.space1++-- |+-- Lifted megaparsec\'s `Megaparsec.char`.+char :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => Char -> HeadedParsec err strm Char+char a = parse (MegaparsecChar.char a)++-- |+-- Lifted megaparsec\'s `Megaparsec.char'`.+char' :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => Char -> HeadedParsec err strm Char+char' a = parse (MegaparsecChar.char' a)++-- |+-- Lifted megaparsec\'s `Megaparsec.string`.+string :: (Ord err, Stream strm) => Megaparsec.Tokens strm -> HeadedParsec err strm (Megaparsec.Tokens strm)+string = parse . MegaparsecChar.string++-- |+-- Lifted megaparsec\'s `Megaparsec.string'`.+string' :: (Ord err, Stream strm, FoldCase (Megaparsec.Tokens strm)) => Megaparsec.Tokens strm -> HeadedParsec err strm (Megaparsec.Tokens strm)+string' = parse . MegaparsecChar.string'++-- |+-- Lifted megaparsec\'s `Megaparsec.takeWhileP`.+takeWhileP :: (Ord err, Stream strm) => Maybe String -> (Megaparsec.Token strm -> Bool) -> HeadedParsec err strm (Megaparsec.Tokens strm)+takeWhileP label predicate = parse (Megaparsec.takeWhileP label predicate)++-- |+-- Lifted megaparsec\'s `Megaparsec.takeWhile1P`.+takeWhile1P :: (Ord err, Stream strm) => Maybe String -> (Megaparsec.Token strm -> Bool) -> HeadedParsec err strm (Megaparsec.Tokens strm)+takeWhile1P label predicate = parse (Megaparsec.takeWhile1P label predicate)++satisfy :: (Ord err, Stream strm) => (Megaparsec.Token strm -> Bool) -> HeadedParsec err strm (Megaparsec.Token strm)+satisfy = parse . Megaparsec.satisfy++decimal :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char, Integral decimal) => HeadedParsec err strm decimal+decimal = parse MegaparsecLexer.decimal++float :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char, RealFloat float) => HeadedParsec err strm float+float = parse MegaparsecLexer.float++-- * Combinators++sep1 :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => HeadedParsec err strm separtor -> HeadedParsec err strm a -> HeadedParsec err strm (NonEmpty a)+sep1 separator parser = do+ head <- parser+ endHead+ tail <- many $ separator *> parser+ return (head :| tail)++sepEnd1 :: (Ord err, Stream strm, Megaparsec.Token strm ~ Char) => HeadedParsec err strm separator -> HeadedParsec err strm end -> HeadedParsec err strm el -> HeadedParsec err strm (NonEmpty el, end)+sepEnd1 sepP endP elP = do+ headEl <- elP+ let loop !list = do+ _ <- sepP+ asum+ [ do+ end <- endP+ return (headEl :| reverse list, end),+ do+ el <- elP+ loop (el : list)+ ]+ in loop []++notFollowedBy :: (Ord err, Stream strm) => HeadedParsec err strm a -> HeadedParsec err strm ()+notFollowedBy a = parse (Megaparsec.notFollowedBy (toParsec a))
+ library-internal/PostgresqlSyntax/Extras/NonEmpty.hs view
@@ -0,0 +1,33 @@+{-# OPTIONS_GHC -Wno-dodgy-imports #-}++module PostgresqlSyntax.Extras.NonEmpty where++import Data.List.NonEmpty+import PostgresqlSyntax.Prelude hiding (cons, fromList, head, init, last, reverse, tail, uncons)++-- |+-- >>> intersperseFoldMap ", " id (fromList ["a"])+-- "a"+--+-- >>> intersperseFoldMap ", " id (fromList ["a", "b", "c"])+-- "a, b, c"+intersperseFoldMap :: (Monoid m) => m -> (a -> m) -> NonEmpty a -> m+intersperseFoldMap a b (c :| d) = b c <> foldMap (mappend a . b) d++unsnoc :: NonEmpty a -> (Maybe (NonEmpty a), a)+unsnoc =+ let build1 = \case+ a :| b -> build2 a b+ build2 a = \case+ b : c -> build3 b (a :| []) c+ _ -> (Nothing, a)+ build3 a b = \case+ c : d -> build3 c (cons a b) d+ _ -> (Just (reverse b), a)+ in build1++consAndUnsnoc :: a -> NonEmpty a -> (NonEmpty a, a)+consAndUnsnoc a b = case unsnoc b of+ (c, d) -> case c of+ Just e -> (cons a e, d)+ Nothing -> (pure a, d)
+ library-internal/PostgresqlSyntax/Extras/TextBuilder.hs view
@@ -0,0 +1,16 @@+module PostgresqlSyntax.Extras.TextBuilder where++import PostgresqlSyntax.Prelude+import TextBuilder++char7 :: Char -> TextBuilder+char7 = char++intDec :: Int -> TextBuilder+intDec = decimal++int64Dec :: Int64 -> TextBuilder+int64Dec = decimal++doubleDec :: Double -> TextBuilder+doubleDec = fromString . show
+ library-internal/PostgresqlSyntax/Helpers/Gens.hs view
@@ -0,0 +1,51 @@+module PostgresqlSyntax.Helpers.Gens where++import PostgresqlSyntax.Prelude+import Test.QuickCheck++downscale :: Gen a -> Gen a+downscale = scale (`div` 2)++recursive :: Gen a -> Gen a -> Gen a+recursive nonRecursiveGen recursiveGen = sized $ \size ->+ if size <= 1+ then nonRecursiveGen+ else downscale recursiveGen++oneofRec ::+ (Arbitrary a) =>+ [Gen a] ->+ [Gen a] ->+ Gen a+oneofRec nonRecursiveGens recursiveGens = sized $ \size ->+ if size <= 1+ then oneof nonRecursiveGens+ else+ frequency+ [ (1, oneof nonRecursiveGens),+ (3, downscale (oneof recursiveGens))+ ]++-- | Generate a non-empty list of at most @n + 1@ elements, splitting the size+-- budget across them.+--+-- The split is what keeps growth bounded: generating every element at the+-- undiminished size would multiply the subtree's cost by the list length at no+-- size cost, and those multipliers compound through the AST.+nonEmptyUpTo :: Int -> Gen a -> Gen (NonEmpty a)+nonEmptyUpTo n gen = sized $ \size -> do+ -- The 'max 0' matters: at size 0 the upper bound is negative, and 'choose'+ -- silently swaps inverted bounds instead of failing.+ tailLen <- choose (0, max 0 (min n (size - 1)))+ let totalLen = tailLen + 1+ subsize = size `div` totalLen+ subgen = resize subsize gen+ x <- subgen+ xs <- vectorOf tailLen subgen+ pure (x :| xs)++terminatingMaybe :: Gen a -> Gen (Maybe a)+terminatingMaybe gen = sized $ \size ->+ if size <= 1+ then pure Nothing+ else Just <$> gen
+ library-internal/PostgresqlSyntax/Helpers/Parsers.hs view
@@ -0,0 +1,229 @@+{-# OPTIONS_GHC -Wno-redundant-constraints -Wno-missing-signatures -Wno-dodgy-imports #-}++module PostgresqlSyntax.Helpers.Parsers+ ( module PostgresqlSyntax.Extras.HeadedMegaparsec,+ module PostgresqlSyntax.Helpers.Parsers,+ )+where++import Control.Applicative.Combinators hiding (some)+import qualified Data.HashSet as HashSet+import qualified Data.Text as Text+import HeadedMegaparsec hiding (string)+import PostgresqlSyntax.Algebra hiding (parse, parseWithPosError, toText)+import PostgresqlSyntax.Extras.HeadedMegaparsec hiding (run)+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import qualified PostgresqlSyntax.Predicate as Predicate+import PostgresqlSyntax.Prelude hiding (bit, expr, filter, fromList, head, many, option, some, sortBy, tail, try)+import PostgresqlSyntax.Settings (Settings)+import qualified Text.Megaparsec as Megaparsec+import qualified Text.Megaparsec.Char as MegaparsecChar+import qualified TextBuilder++-- $setup+-- >>> import qualified PostgresqlSyntax.Extras.HeadedMegaparsec as HeadedMegaparsec+-- >>> testParser p = either putStr print . HeadedMegaparsec.run p++inSpace :: Parser a -> Parser a+inSpace p = space *> p <* space++commaSeparator :: Parser ()+commaSeparator = space *> char ',' *> endHead *> space++dotSeparator :: Parser ()+dotSeparator = space *> char '.' *> endHead *> space++inBrackets :: Parser a -> Parser a+inBrackets p = char '[' *> space *> p <* endHead <* space <* char ']'++inBracketsCont :: Parser a -> Parser (Parser a)+inBracketsCont p = char '[' *> endHead *> pure (space *> p <* endHead <* space <* char ']')++inParens :: Parser a -> Parser a+inParens p = char '(' *> space *> p <* endHead <* space <* char ')'++inParensCont :: Parser a -> Parser (Parser a)+inParensCont p = char '(' *> endHead *> pure (space *> p <* endHead <* space <* char ')')++inParensWithLabel :: (label -> content -> result) -> Parser label -> Parser content -> Parser result+inParensWithLabel result labelParser contentParser = do+ label <- wrapToHead labelParser+ space+ char '('+ endHead+ space+ content <- contentParser+ space+ char ')'+ pure (result label content)++inParensWithClause :: Parser clause -> Parser content -> Parser content+inParensWithClause = inParensWithLabel (const id)++trueIfPresent :: Parser a -> Parser Bool+trueIfPresent p = option False (True <$ p)++-- |+-- >>> testParser (quotedString '\'') "'abc''d'"+-- "abc'd"+quotedString :: Char -> Parser Text+quotedString q = do+ char q+ endHead+ tail <-+ parse $+ let collectChunks !bdr = do+ chunk <- Megaparsec.takeWhileP Nothing (/= q)+ let bdr' = bdr <> TextBuilder.text chunk+ Megaparsec.try (consumeEscapedQuote bdr') <|> finish bdr'+ consumeEscapedQuote bdr = do+ MegaparsecChar.char q+ MegaparsecChar.char q+ collectChunks (bdr <> TextBuilder.char q)+ finish bdr = do+ MegaparsecChar.char q+ return (TextBuilder.toText bdr)+ in collectChunks mempty+ return tail++-- |+-- >>> testParser dollarQuotedSconst "$$it's good$$"+-- "it's good"+dollarQuotedSconst :: Parser Text+dollarQuotedSconst = do+ char '$'+ quoteTag <- takeWhileP Nothing (/= '$')+ let terminator = Megaparsec.chunk $ "$" <> quoteTag <> "$"+ char '$'+ endHead+ tail <-+ parse $ do+ body <- many $ Megaparsec.notFollowedBy terminator *> Megaparsec.anySingle+ terminator+ return $ Text.pack body+ return tail++-- * Keyword-matching infrastructure++--+-- Ported from "PostgresqlSyntax.Parsing". @keywordNameFromSet@\/@keywordNameByPredicate@+-- originally built an @Ident@ directly via its @UnquotedIdent@ constructor;+-- here that constructor is taken as an explicit argument (@wrap@) so this+-- module doesn't need to depend on the (still-monolithic-for-now)+-- 'PostgresqlSyntax.Ast.Ident' type. Callers pass @UnquotedIdent@ for it.++keywordNameFromSet wrap set = keywordNameByPredicate wrap (Predicate.inSet set)++keywordNameByPredicate wrap predicate =+ fmap wrap $+ filter+ (\a -> "Reserved keyword " <> show a <> " used as an identifier. If that's what you intend, you have to wrap it in double quotes.")+ predicate+ anyKeyword++anyKeyword = parse $+ Megaparsec.label "keyword" $+ do+ firstChar <- Megaparsec.satisfy Predicate.firstIdentifierChar+ remainder <- Megaparsec.takeWhileP Nothing Predicate.notFirstIdentifierChar+ let parsedKeyword = Text.toLower (Text.cons firstChar remainder)+ when (parsedKeyword == "nulls") (Megaparsec.notFollowedBy nullsLa)+ return parsedKeyword+ where+ nullsLa =+ MegaparsecChar.space1+ *> (MegaparsecChar.string' "first" <|> MegaparsecChar.string' "last")+ *> Megaparsec.notFollowedBy (Megaparsec.satisfy Predicate.notFirstIdentifierChar)++-- | Expected keyword+--+-- Wraps the head in 'Megaparsec.region' to pin the reported error offset to+-- where this keyword check began. Without this, megaparsec >=9.8's stricter+-- (and correct) '(<|>)' error-merging (fix for+-- <https://github.com/mrkkrp/megaparsec/issues/412>) normalizes any error+-- whose offset lands past the enclosing alternative's start back down to+-- that start, discarding its \"expecting\" set unless the offset already+-- matches exactly. Since every failed keyword branch consumes some+-- identifier text before comparing, its offset always landed past the+-- alternative's start, so wide 'asum'/'choice' chains of 'keyword' lost+-- their combined expected-token sets under 9.8. Pinning the offset up front+-- keeps every branch's offset aligned with the alternative's start, so+-- their expected sets still union correctly.+keyword a = parse $ do+ off <- Megaparsec.getOffset+ Megaparsec.region (Megaparsec.setErrorOffset off) $ do+ firstChar <- Megaparsec.satisfy Predicate.firstIdentifierChar+ remainder <- Megaparsec.takeWhileP Nothing Predicate.notFirstIdentifierChar+ let parsedKeyword = Text.toLower (Text.cons firstChar remainder)+ if a == parsedKeyword then return parsedKeyword else empty++-- |+-- Consume a keyphrase, ignoring case and types of spaces between words.+keyphrase a =+ Text.words a+ & fmap (void . MegaparsecChar.string')+ & intersperse MegaparsecChar.space1+ & sequence_+ & (<* Megaparsec.notFollowedBy (Megaparsec.satisfy Predicate.notFirstIdentifierChar))+ & fmap (const (Text.toUpper a))+ & Megaparsec.label (show a)+ & parse+ & (<* endHead)++-- * Cross-type-family helpers++--+-- Ported from "PostgresqlSyntax.Parsing". Each of these originally called a+-- single concrete node's parser directly (@typename@, @qualOp@,+-- @symbolicExprBinOp@, @fconst@\/@iconst@); here they're generalized over+-- 'IsAst' so any node module can instantiate them for its own node type(s)+-- without this module depending on any of them.++typecastExpr :: (IsAst typename) => Settings -> a -> (a -> typename -> a) -> Parser a+typecastExpr settings prefix constr = do+ space+ string "::"+ endHead+ space+ type' <- parser settings+ return (constr prefix type')++plusedExpr expr = char '+' *> space *> expr++minusedExpr expr = char '-' *> space *> expr++qualOpExpr :: (IsAst qualOp) => Settings -> Parser b -> (qualOp -> b -> a) -> Parser a+qualOpExpr settings expr constr = constr <$> wrapToHead (parser settings) <*> (space *> expr)++symbolicBinOpExpr :: (IsAst symbolicExprBinOp) => Settings -> a -> Parser b -> (a -> symbolicExprBinOp -> b -> c) -> Parser c+symbolicBinOpExpr settings a bParser constr = do+ binOp <- label "binary operator" (space *> wrapToHead (parser settings) <* space)+ b <- bParser+ return (constr a binOp b)++iconstOrFconst :: (IsAst iconst, IsAst fconst) => Settings -> Parser (Either iconst fconst)+iconstOrFconst settings = Right <$> parser settings <|> Left <$> parser settings++-- |+-- Shared by 'PostgresqlSyntax.Ast.FuncApplicationParams' and+-- 'PostgresqlSyntax.Ast.SimpleSelect' (@ALL@\/@DISTINCT@ before a func-arg+-- list, and before a @UNION@\/@INTERSECT@\/@EXCEPT@ right-hand side,+-- respectively).+allOrDistinct :: Parser Bool+allOrDistinct = keyword "all" $> False <|> keyword "distinct" $> True++-- |+-- A ColId-like identifier parser (unreserved keyword ∪ col-name keyword)+-- restricted to exclude the given reserved words - needed wherever a+-- trailing bare word must terminate a construct instead of being consumed+-- as an identifier. Its only caller is+-- 'PostgresqlSyntax.Ast.RelationExprOptAlias', which excludes @SET@ only in+-- its bare (non-@AS@) alias branch, per @gram.y@'s+-- @relation_expr_opt_alias@ shift/reduce resolution. @identParser@ is the+-- identifier type's own plain (unfiltered) parser, tried first, same as+-- plain @ColId@ does.+filteredColIdLike :: (Text -> a) -> Parser a -> [Text] -> Parser a+filteredColIdLike wrap identParser excluded =+ label "identifier" $+ identParser+ <|> keywordNameFromSet wrap (foldr HashSet.delete (KeywordSet.unreservedKeyword <> KeywordSet.colNameKeyword) excluded)
+ library-internal/PostgresqlSyntax/Helpers/Shrinks.hs view
@@ -0,0 +1,30 @@+-- |+-- Shrink helpers shared by 2+ AST node modules.+module PostgresqlSyntax.Helpers.Shrinks where++import qualified Data.List as List+import qualified Data.Text as Text+import PostgresqlSyntax.Prelude+import qualified Test.QuickCheck as Qc++-- |+-- Shrinks a 'Text' value by shrinking it as a 'String' (dropping\/simplifying+-- characters).+text :: Text -> [Text]+text = fmap Text.pack . Qc.shrink . Text.unpack++-- |+-- Shrinks a 'Text' value, excluding empty results.+nonEmptyText :: Text -> [Text]+nonEmptyText = List.filter (not . Text.null) . text++-- |+-- Shrinks the tail of a 'Text' value, keeping its first character fixed.+-- Useful for text with constraints on the first character (e.g. identifiers),+-- where shrinking it away could produce an invalid result.+textTail :: Text -> [Text]+textTail a =+ case Text.uncons a of+ Nothing -> []+ Just (head, tail) ->+ Text.cons head <$> nonEmptyText tail
+ library-internal/PostgresqlSyntax/Helpers/TextBuilders.hs view
@@ -0,0 +1,35 @@+-- |+-- Rendering helpers shared by 2+ AST node modules.+module PostgresqlSyntax.Helpers.TextBuilders where++import qualified PostgresqlSyntax.Extras.NonEmpty as NonEmpty+import PostgresqlSyntax.Prelude++commaNonEmpty :: (a -> TextBuilder) -> NonEmpty a -> TextBuilder+commaNonEmpty = NonEmpty.intersperseFoldMap ", "++spaceNonEmpty :: (a -> TextBuilder) -> NonEmpty a -> TextBuilder+spaceNonEmpty = NonEmpty.intersperseFoldMap " "++lexemes :: [TextBuilder] -> TextBuilder+lexemes = mconcat . intersperse " "++optLexemes :: [Maybe TextBuilder] -> TextBuilder+optLexemes = lexemes . catMaybes++renderInParens :: TextBuilder -> TextBuilder+renderInParens a = "(" <> a <> ")"++renderInBrackets :: TextBuilder -> TextBuilder+renderInBrackets a = "[" <> a <> "]"++prefixMaybe :: (a -> TextBuilder) -> Maybe a -> TextBuilder+prefixMaybe a = foldMap (flip mappend " " . a)++suffixMaybe :: (a -> TextBuilder) -> Maybe a -> TextBuilder+suffixMaybe a = foldMap (mappend " " . a)++renderAllOrDistinct :: Bool -> TextBuilder+renderAllOrDistinct = \case+ False -> "ALL"+ True -> "DISTINCT"
+ library-internal/PostgresqlSyntax/KeywordSet.hs view
@@ -0,0 +1,66 @@+module PostgresqlSyntax.KeywordSet where++import Data.HashSet+import PostgresqlSyntax.Prelude hiding (fromList, toList)++{-# NOINLINE keyword #-}+{-+From https://github.com/postgres/postgres/blob/1aac32df89eb19949050f6f27c268122833ad036/src/include/parser/kwlist.h+-}+keyword :: HashSet Text+keyword = fromList ["abort", "absolute", "access", "action", "add", "admin", "after", "aggregate", "all", "also", "alter", "always", "analyse", "analyze", "and", "any", "array", "as", "asc", "assertion", "assignment", "asymmetric", "at", "attach", "attribute", "authorization", "backward", "before", "begin", "between", "bigint", "binary", "bit", "boolean", "both", "by", "cache", "call", "called", "cascade", "cascaded", "case", "cast", "catalog", "chain", "char", "character", "characteristics", "check", "checkpoint", "class", "close", "cluster", "coalesce", "collate", "collation", "column", "columns", "comment", "comments", "commit", "committed", "concurrently", "configuration", "conflict", "connection", "constraint", "constraints", "content", "continue", "conversion", "copy", "cost", "create", "cross", "csv", "cube", "current", "current_catalog", "current_date", "current_role", "current_schema", "current_time", "current_timestamp", "current_user", "cursor", "cycle", "data", "database", "day", "deallocate", "dec", "decimal", "declare", "default", "defaults", "deferrable", "deferred", "definer", "delete", "delimiter", "delimiters", "depends", "desc", "detach", "dictionary", "disable", "discard", "distinct", "do", "document", "domain", "double", "drop", "each", "else", "enable", "encoding", "encrypted", "end", "enum", "escape", "event", "except", "exclude", "excluding", "exclusive", "execute", "exists", "explain", "expression", "extension", "external", "extract", "false", "family", "fetch", "filter", "first", "float", "following", "for", "force", "foreign", "forward", "freeze", "from", "full", "function", "functions", "generated", "global", "grant", "granted", "greatest", "group", "grouping", "groups", "handler", "having", "header", "hold", "hour", "identity", "if", "ilike", "immediate", "immutable", "implicit", "import", "in", "include", "including", "increment", "index", "indexes", "inherit", "inherits", "initially", "inline", "inner", "inout", "input", "insensitive", "insert", "instead", "int", "integer", "intersect", "interval", "into", "invoker", "is", "isnull", "isolation", "join", "key", "label", "language", "large", "last", "lateral", "leading", "leakproof", "least", "left", "level", "like", "limit", "listen", "load", "local", "localtime", "localtimestamp", "location", "lock", "locked", "logged", "mapping", "match", "materialized", "maxvalue", "method", "minute", "minvalue", "mode", "month", "move", "name", "names", "national", "natural", "nchar", "new", "next", "nfc", "nfd", "nfkc", "nfkd", "no", "none", "normalize", "normalized", "not", "nothing", "notify", "notnull", "nowait", "null", "nullif", "nulls", "numeric", "object", "of", "off", "offset", "oids", "old", "on", "only", "operator", "option", "options", "or", "order", "ordinality", "others", "out", "outer", "over", "overlaps", "overlay", "overriding", "owned", "owner", "parallel", "parser", "partial", "partition", "passing", "password", "placing", "plans", "policy", "position", "preceding", "precision", "prepare", "prepared", "preserve", "primary", "prior", "privileges", "procedural", "procedure", "procedures", "program", "publication", "quote", "range", "read", "real", "reassign", "recheck", "recursive", "ref", "references", "referencing", "refresh", "reindex", "relative", "release", "rename", "repeatable", "replace", "replica", "reset", "restart", "restrict", "returning", "returns", "revoke", "right", "role", "rollback", "rollup", "routine", "routines", "row", "rows", "rule", "savepoint", "schema", "schemas", "scroll", "search", "second", "security", "select", "sequence", "sequences", "serializable", "server", "session", "session_user", "set", "setof", "sets", "share", "show", "similar", "simple", "skip", "smallint", "snapshot", "some", "sql", "stable", "standalone", "start", "statement", "statistics", "stdin", "stdout", "storage", "stored", "strict", "strip", "subscription", "substring", "support", "symmetric", "sysid", "system", "table", "tables", "tablesample", "tablespace", "temp", "template", "temporary", "text", "then", "ties", "time", "timestamp", "to", "trailing", "transaction", "transform", "treat", "trigger", "trim", "true", "truncate", "trusted", "type", "types", "uescape", "unbounded", "uncommitted", "unencrypted", "union", "unique", "unknown", "unlisten", "unlogged", "until", "update", "user", "using", "vacuum", "valid", "validate", "validator", "value", "values", "varchar", "variadic", "varying", "verbose", "version", "view", "views", "volatile", "when", "where", "whitespace", "window", "with", "within", "without", "work", "wrapper", "write", "xml", "xmlattributes", "xmlconcat", "xmlelement", "xmlexists", "xmlforest", "xmlnamespaces", "xmlparse", "xmlpi", "xmlroot", "xmlserialize", "xmltable", "year", "yes", "zone"]++{-# NOINLINE unreservedKeyword #-}+unreservedKeyword :: HashSet Text+unreservedKeyword = fromList ["abort", "absolute", "access", "action", "add", "admin", "after", "aggregate", "also", "alter", "always", "assertion", "assignment", "at", "attach", "attribute", "backward", "before", "begin", "by", "cache", "call", "called", "cascade", "cascaded", "catalog", "chain", "characteristics", "checkpoint", "class", "close", "cluster", "columns", "comment", "comments", "commit", "committed", "configuration", "conflict", "connection", "constraints", "content", "continue", "conversion", "copy", "cost", "csv", "cube", "current", "cursor", "cycle", "data", "database", "day", "deallocate", "declare", "defaults", "deferred", "definer", "delete", "delimiter", "delimiters", "depends", "detach", "dictionary", "disable", "discard", "document", "domain", "double", "drop", "each", "enable", "encoding", "encrypted", "enum", "escape", "event", "exclude", "excluding", "exclusive", "execute", "explain", "extension", "external", "family", "filter", "first", "following", "force", "forward", "function", "functions", "generated", "global", "granted", "groups", "handler", "header", "hold", "hour", "identity", "if", "immediate", "immutable", "implicit", "import", "include", "including", "increment", "index", "indexes", "inherit", "inherits", "inline", "input", "insensitive", "insert", "instead", "invoker", "isolation", "key", "label", "language", "large", "last", "leakproof", "level", "listen", "load", "local", "location", "lock", "locked", "logged", "mapping", "match", "materialized", "maxvalue", "method", "minute", "minvalue", "mode", "month", "move", "name", "names", "new", "next", "no", "nothing", "notify", "nowait", "nulls", "object", "of", "off", "oids", "old", "operator", "option", "options", "ordinality", "others", "over", "overriding", "owned", "owner", "parallel", "parser", "partial", "partition", "passing", "password", "plans", "policy", "preceding", "prepare", "prepared", "preserve", "prior", "privileges", "procedural", "procedure", "procedures", "program", "publication", "quote", "range", "read", "reassign", "recheck", "recursive", "ref", "referencing", "refresh", "reindex", "relative", "release", "rename", "repeatable", "replace", "replica", "reset", "restart", "restrict", "returns", "revoke", "role", "rollback", "rollup", "routine", "routines", "rows", "rule", "savepoint", "schema", "schemas", "scroll", "search", "second", "security", "sequence", "sequences", "serializable", "server", "session", "set", "sets", "share", "show", "simple", "skip", "snapshot", "sql", "stable", "standalone", "start", "statement", "statistics", "stdin", "stdout", "storage", "stored", "strict", "strip", "subscription", "support", "sysid", "system", "tables", "tablespace", "temp", "template", "temporary", "text", "ties", "transaction", "transform", "trigger", "truncate", "trusted", "type", "types", "unbounded", "uncommitted", "unencrypted", "unknown", "unlisten", "unlogged", "until", "update", "vacuum", "valid", "validate", "validator", "value", "varying", "version", "view", "views", "volatile", "whitespace", "within", "without", "work", "wrapper", "write", "xml", "year", "yes", "zone"]++{-# NOINLINE colNameKeyword #-}+colNameKeyword :: HashSet Text+colNameKeyword = fromList ["between", "bigint", "bit", "boolean", "char", "character", "coalesce", "dec", "decimal", "exists", "extract", "float", "greatest", "grouping", "inout", "int", "integer", "interval", "least", "national", "nchar", "none", "normalize", "nullif", "numeric", "out", "overlay", "position", "precision", "real", "row", "setof", "smallint", "substring", "time", "timestamp", "treat", "trim", "values", "varchar", "xmlattributes", "xmlconcat", "xmlelement", "xmlexists", "xmlforest", "xmlnamespaces", "xmlparse", "xmlpi", "xmlroot", "xmlserialize", "xmltable"]++{-# NOINLINE typeFuncNameKeyword #-}+typeFuncNameKeyword :: HashSet Text+typeFuncNameKeyword = fromList ["authorization", "binary", "collation", "concurrently", "cross", "current_schema", "freeze", "full", "ilike", "inner", "is", "isnull", "join", "left", "like", "natural", "notnull", "outer", "overlaps", "right", "similar", "tablesample", "verbose"]++{-# NOINLINE reservedKeyword #-}+reservedKeyword :: HashSet Text+reservedKeyword = fromList ["all", "analyse", "analyze", "and", "any", "array", "as", "asc", "asymmetric", "both", "case", "cast", "check", "collate", "column", "constraint", "create", "current_catalog", "current_date", "current_role", "current_time", "current_timestamp", "current_user", "default", "deferrable", "desc", "distinct", "do", "else", "end", "except", "false", "fetch", "for", "foreign", "from", "grant", "group", "having", "in", "initially", "intersect", "into", "lateral", "leading", "limit", "localtime", "localtimestamp", "not", "null", "offset", "on", "only", "or", "order", "placing", "primary", "references", "returning", "select", "session_user", "some", "symmetric", "table", "then", "to", "trailing", "true", "union", "unique", "user", "using", "variadic", "when", "where", "window", "with"]++{-# NOINLINE symbolicBinOp #-}+symbolicBinOp :: HashSet Text+symbolicBinOp = fromList ["+", "-", "*", "/", "%", "^", "<", ">", "=", "<=", ">=", "<>", "~~", "~~*", "!~~", "!~~*", "~", "~*", "!~", "!~*"]++{-# NOINLINE lexicalBinOp #-}+lexicalBinOp :: HashSet Text+lexicalBinOp = fromList ["and", "or"]++{-# NOINLINE colId #-}+colId :: HashSet Text+colId = unions [unreservedKeyword, colNameKeyword]++{-+type_function_name:+ | IDENT+ | unreserved_keyword+ | type_func_name_keyword+-}+{-# NOINLINE typeFunctionName #-}+typeFunctionName :: HashSet Text+typeFunctionName = unions [unreservedKeyword, typeFuncNameKeyword]++-- |+-- As per the following comment from the original scanner definition:+--+-- /*+-- * Likewise, if what we have left is two chars, and+-- * those match the tokens ">=", "<=", "=>", "<>" or+-- * "!=", then we must return the appropriate token+-- * rather than the generic Op.+-- */+{-# NOINLINE nonOp #-}+nonOp :: HashSet Text+nonOp = fromList [">=", "<=", "=>", "<>", "!="] <> mathOp++{-# NOINLINE mathOp #-}+mathOp :: HashSet Text+mathOp = fromList ["<>", ">=", "!=", "<=", "+", "-", "*", "/", "%", "^", "<", ">", "="]
+ library-internal/PostgresqlSyntax/Lowering.hs view
@@ -0,0 +1,235 @@+-- |+-- Lowering a parsed-with-extensions AST back to something that renders as+-- plain Postgres.+--+-- The Kronor extensions ('PostgresqlSyntax.Settings.haskellTargets' and+-- 'PostgresqlSyntax.Settings.haskellParamFields') let a statement carry+-- Haskell-level information that Postgres itself cannot see: which Haskell+-- function or record the selected columns feed, and which field of a Haskell+-- record each @$n@ placeholder takes its value from. Rendering is faithful,+-- so an AST straight out of the parser still renders that extra syntax back.+-- 'lower' is the pass that strips it: it erases the Haskell targets down to+-- their SQL leaves and renumbers the placeholders densely, handing back the+-- placeholder mapping the caller needs to build its parameter encoder.+module PostgresqlSyntax.Lowering+ ( InputParams,+ lower,+ renameParams,+ eraseHaskellTargets,+ )+where++import qualified Data.List.NonEmpty as NonEmpty+import Data.Map.Strict (Map)+import qualified Data.Map.Strict as Map+import PostgresqlSyntax.Ast.CExpr+import PostgresqlSyntax.Ast.HsTargetEl+import PostgresqlSyntax.Ast.HsTargetList+import PostgresqlSyntax.Ast.Indirection+import PostgresqlSyntax.Ast.IndirectionEl+import PostgresqlSyntax.Ast.JoinedTable+import PostgresqlSyntax.Ast.TargetEl+import PostgresqlSyntax.Prelude++-- |+-- Maps each distinct placeholder - keyed by its original @$n@ index and, for+-- an interpolated one, the Haskell field it selects - to the dense index it+-- was renumbered to. @Map.size@ is therefore the number of placeholders the+-- lowered statement actually has.+type InputParams = Map (Int, Maybe Text) Int++-- |+-- Renumber the placeholders of a statement the way its rendered SQL will+-- need them, /without/ erasing anything.+--+-- The numbering is computed from the erased tree, so it is the numbering the+-- SQL the server finally sees will carry. It is then applied to the original+-- tree, which still has its Haskell targets. That is what the caller needs:+-- the decoder is built by walking those targets, and they have to survive+-- until it has. __Render 'eraseHaskellTargets' of this result__, not the+-- result itself - otherwise the Haskell syntax reaches the server.+--+-- Computing the numbering from the erased tree rather than the original is+-- belt and braces. Erasure only flattens a target list into the same+-- 'SqlTargetEl' leaves, in the same depth-first left-to-right order the+-- traversal would have visited them in anyway, so the two numberings agree -+-- on all 1512 statements of the Kronor corpus, they do. Deriving it from the+-- tree that will actually be rendered makes that a guarantee instead of an+-- argument.+lower :: (Data a) => a -> (a, InputParams)+lower a =+ let (_, params) = renameParams (eraseHaskellTargets a)+ in (applyParams params a, params)++-- |+-- Rewrite every placeholder according to an already-computed mapping,+-- changing nothing else. A placeholder the mapping does not mention is left+-- alone, which cannot happen for a mapping taken from the same statement.+--+-- Not idempotent: the mapping is keyed by original index, so applying it to+-- an already-renumbered tree renumbers again.+applyParams :: (Data a) => InputParams -> a -> a+applyParams params = everywhere (mkT step)+ where+ step :: CExpr -> CExpr+ step = \case+ original@(ParamCExpr n Nothing) ->+ maybe original (`ParamCExpr` Nothing) (Map.lookup (n, Nothing) params)+ original@(ParamCExpr n (Just indirection@(Indirection (el :| _)))) -> case el of+ HsAttrNameIndirectionEl field ->+ maybe original (`ParamCExpr` Nothing) (Map.lookup (n, Just field) params)+ _ ->+ maybe original (\n' -> ParamCExpr n' (Just indirection)) (Map.lookup (n, Nothing) params)+ other -> other++-- * Placeholder renumbering++-- |+-- Renumber every @$n@ placeholder in the tree to a dense @$1..$k@, in order+-- of first appearance, and return the mapping that says how.+--+-- Two placeholders share a slot exactly when they have the same original+-- index /and/ the same Haskell field, so @$1.$a@ and @$1.$b@ get separate+-- slots while a repeated @$1.$a@, or a bare @$1@ used twice, does not.+--+-- A placeholder whose indirection /starts/ with a Haskell field selector+-- loses the rest of its indirection chain: the whole chain is Haskell-level+-- addressing that the caller resolves, not SQL the server should see. So+-- @$1.$foo[2]@ lowers to a bare @$3@ (say), keyed @(1, Just "foo")@. An+-- ordinary indirection keeps its chain untouched.+--+-- The traversal is pre-order, so an enclosing placeholder is numbered before+-- one nested in its indirection, and left-to-right in constructor field+-- order, which throughout this AST is the order the node renders in. The one+-- exception is 'QualJoinedTable', whose @ON@ qualifier is numbered ahead of+-- the two joined tables even though it renders after them. That is not+-- arbitrary: the pre-0.5 fork's hand-written traversal did the same, because+-- its constructor put the join method and its qualifier in the first field,+-- and matching it keeps the numbering of every existing quasiquote unchanged.+-- Nothing downstream depends on which order is chosen - the placeholder+-- mapping returned here is what the caller permutes its parameter tuple with,+-- so any bijection is self-consistent - but a statement that needs no+-- renumbering should not get any.+renameParams :: (Data a) => a -> (a, InputParams)+renameParams a = runAlloc (go a) Map.empty+ where+ -- Pre-order: renumber the node, then descend into its children.+ go :: (Data d) => d -> Alloc d+ go x = mkM step x >>= descend++ step :: CExpr -> Alloc CExpr+ step = \case+ ParamCExpr n Nothing ->+ (`ParamCExpr` Nothing) <$> getNextParam n Nothing+ ParamCExpr n (Just indirection@(Indirection (el :| _))) -> case el of+ HsAttrNameIndirectionEl field ->+ (`ParamCExpr` Nothing) <$> getNextParam n (Just field)+ _ ->+ (\n' -> ParamCExpr n' (Just indirection)) <$> getNextParam n Nothing+ other -> pure other++ -- Children left-to-right in field order, which in this AST is source+ -- order, except for the one node called out below.+ descend :: forall d. (Data d) => d -> Alloc d+ descend x = case cast x of+ Just (QualJoinedTable left joinType right qual) -> do+ qual' <- go qual+ left' <- go left+ joinType' <- go joinType+ right' <- go right+ pure (generalize (QualJoinedTable left' joinType' right' qual') x)+ _ -> gmapM go x++-- |+-- Look up the slot for a placeholder, allocating the next one if it is the+-- first time this @(index, field)@ pair is seen.+getNextParam :: Int -> Maybe Text -> Alloc Int+getNextParam param field =+ Alloc $ \seen ->+ let key = (param, field)+ in case Map.lookup key seen of+ Just slot -> (slot, seen)+ Nothing ->+ let slot = Map.size seen + 1+ in (slot, Map.insert key slot seen)++-- * Haskell-target erasure++-- |+-- Replace every 'HsTargetList' in the tree by the flat list of the+-- 'SqlTargetEl' leaves reachable from it, collected depth-first and+-- left-to-right - i.e. in the order the columns come back from the server.+--+-- The type is kept: what changes is that afterwards every element is a+-- 'SqlTargetEl', so rendering emits an ordinary SQL target list.+--+-- A list with no SQL leaves at all (only a nullary Haskell function target,+-- say) is left alone rather than turned into an empty target list, which the+-- 'NonEmpty' cannot represent. Such a statement selects no columns and is not+-- valid SQL either way.+eraseHaskellTargets :: (Data a) => a -> a+eraseHaskellTargets = everywhere (mkT flattenHsTargetList)++flattenHsTargetList :: HsTargetList -> HsTargetList+flattenHsTargetList original@(HsTargetList els) =+ case NonEmpty.nonEmpty (foldMap sqlTargetEls els) of+ Nothing -> original+ Just leaves -> HsTargetList (fmap SqlTargetEl leaves)++sqlTargetEls :: HsTargetEl -> [TargetEl]+sqlTargetEls = \case+ SqlTargetEl a -> [a]+ HsFuncTargetEl _ as -> foldMap sqlTargetEls as+ HsRecTargetEl _ as -> foldMap (\(HsFieldEl _ a) -> sqlTargetEls a) as++-- * Generic traversal+--+-- $generics+--+-- Hand-rolled against "Data.Data" rather than pulled in from @syb@: it is+-- four functions, and this package would otherwise gain a dependency purely+-- for them.++-- |+-- Apply a generic transformation to every node, bottom-up.+everywhere :: (Data a) => (forall d. (Data d) => d -> d) -> a -> a+everywhere f = f . gmapT (everywhere f)++-- |+-- Lift a monadic transformation on one type into a generic one that is the+-- identity everywhere else.+mkM :: forall a b m. (Typeable a, Typeable b, Typeable m, Monad m) => (b -> m b) -> a -> m a+mkM f = fromMaybe pure (cast f)++-- | Lift a transformation on one type into a generic one.+mkT :: forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a+mkT f = fromMaybe id (cast f)++-- |+-- Put a value back into the type it was 'cast' out of. The fallback never+-- fires: it is only ever called on the result of a successful 'cast'.+generalize :: (Typeable a, Typeable b) => b -> a -> a+generalize replacement original = fromMaybe original (cast replacement)++-- * The allocation monad+--+-- $alloc+--+-- A state monad over 'InputParams', spelled out here so that neither @mtl@+-- nor @transformers@ has to be added to this package's dependencies.++newtype Alloc a = Alloc {runAlloc :: InputParams -> (a, InputParams)}++instance Functor Alloc where+ fmap f (Alloc g) = Alloc $ \s -> case g s of+ (a, s') -> (f a, s')++instance Applicative Alloc where+ pure a = Alloc $ \s -> (a, s)+ Alloc f <*> Alloc g = Alloc $ \s -> case f s of+ (h, s') -> case g s' of+ (a, s'') -> (h a, s'')++instance Monad Alloc where+ Alloc g >>= f = Alloc $ \s -> case g s of+ (a, s') -> runAlloc (f a) s'
+ library-internal/PostgresqlSyntax/Predicate.hs view
@@ -0,0 +1,71 @@+{-# OPTIONS_GHC -Wno-redundant-constraints #-}++module PostgresqlSyntax.Predicate where++import qualified Data.HashSet as HashSet+import qualified PostgresqlSyntax.CharSet as CharSet+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import PostgresqlSyntax.Prelude++-- * Generic++-- |+-- >>> test = oneOf [(==3), (==7), (==3), (==5)]+-- >>> test 1+-- False+--+-- >>> test 3+-- True+--+-- >>> test 5+-- True+oneOf :: [a -> Bool] -> a -> Bool+oneOf = foldr (\a b c -> a c || b c) (const False)++inSet :: (Eq a, Hashable a) => HashSet a -> a -> Bool+inSet = flip HashSet.member++hexDigit :: Char -> Bool+hexDigit = inSet CharSet.hexDigit++{-+ident_start [A-Za-z\200-\377_]+-}+firstIdentifierChar :: Char -> Bool+firstIdentifierChar x = isAlpha x || x == '_' || x >= '\200' && x <= '\377'++{-+ident_cont [A-Za-z\200-\377_0-9\$]+-}+notFirstIdentifierChar :: Char -> Bool+notFirstIdentifierChar x = isAlphaNum x || x == '_' || x == '$' || x >= '\200' && x <= '\377'++keyword :: Text -> Bool+keyword = inSet KeywordSet.keyword++unreservedKeyword :: Text -> Bool+unreservedKeyword = inSet KeywordSet.unreservedKeyword++colNameKeyword :: Text -> Bool+colNameKeyword = inSet KeywordSet.colNameKeyword++typeFuncNameKeyword :: Text -> Bool+typeFuncNameKeyword = inSet KeywordSet.typeFuncNameKeyword++reservedKeyword :: Text -> Bool+reservedKeyword = inSet KeywordSet.reservedKeyword++{-# NOINLINE symbolicBinOpChar #-}+symbolicBinOpChar :: Char -> Bool+symbolicBinOpChar = inSet CharSet.symbolicBinOp++-- ** Op chars++opChar :: Char -> Bool+opChar = inSet CharSet.op++prohibitedOpChar :: Char -> Bool+prohibitedOpChar a = a == '+' || a == '-'++prohibitionLiftingOpChar :: Char -> Bool+prohibitionLiftingOpChar = inSet CharSet.prohibitionLiftingOp
+ library-internal/PostgresqlSyntax/Prelude.hs view
@@ -0,0 +1,95 @@+module PostgresqlSyntax.Prelude+ ( module Exports,+ suffixRec,+ extendMany,+ )+where++import Control.Applicative as Exports+import Control.Applicative.Combinators as Exports (option)+import Control.Arrow as Exports hiding (first, second)+import Control.Category as Exports+import Control.Concurrent as Exports+import Control.Exception as Exports+import Control.Monad as Exports hiding (fail, forM, forM_, mapM, mapM_, msum, sequence, sequence_)+import Control.Monad.Fail as Exports+import Control.Monad.Fix as Exports hiding (fix)+import Control.Monad.IO.Class as Exports+import Control.Monad.ST as Exports+import Data.Bifunctor as Exports+import Data.Bits as Exports+import Data.Bool as Exports+import Data.ByteString as Exports (ByteString)+import Data.CaseInsensitive as Exports (CI, FoldCase)+import Data.Char as Exports+import Data.Coerce as Exports+import Data.Complex as Exports+import Data.Data as Exports+import Data.Dynamic as Exports+import Data.Either as Exports+import Data.Fixed as Exports+import Data.Foldable as Exports+import Data.Function as Exports hiding (id, (.))+import Data.Functor as Exports hiding (unzip)+import Data.Functor.Identity as Exports+import Data.HashMap.Strict as Exports (HashMap)+import Data.HashSet as Exports (HashSet)+import Data.Hashable as Exports (Hashable)+import Data.IORef as Exports+import Data.Int as Exports+import Data.Ix as Exports+import Data.List as Exports hiding (all, and, any, concat, concatMap, elem, find, foldl, foldl', foldl1, foldr, foldr1, isSubsequenceOf, mapAccumL, mapAccumR, maximum, maximumBy, minimum, minimumBy, notElem, or, product, sortOn, sum, uncons, unsnoc)+import Data.List.NonEmpty as Exports (NonEmpty (..))+import Data.Maybe as Exports+import Data.Monoid as Exports hiding (First (..), Last (..), (<>))+import Data.Ord as Exports+import Data.Proxy as Exports+import Data.Ratio as Exports+import Data.STRef as Exports+import Data.Semigroup as Exports (Semigroup (..), stimes, stimesIdempotent, stimesIdempotentMonoid, stimesMonoid)+import Data.String as Exports+import Data.Text as Exports (Text)+import Data.Traversable as Exports+import Data.Tuple as Exports+import Data.Unique as Exports+import Data.Version as Exports+import Data.Void as Exports+import Data.Word as Exports+import Debug.Trace as Exports+import Foreign.ForeignPtr as Exports+import Foreign.Ptr as Exports+import Foreign.StablePtr as Exports+import Foreign.Storable as Exports hiding (alignment, sizeOf)+import GHC.Conc as Exports hiding (orElse, threadWaitRead, threadWaitReadSTM, threadWaitWrite, threadWaitWriteSTM, withMVar)+import GHC.Exts as Exports (IsList (Item, fromList), groupWith, inline, lazy, sortWith)+import GHC.Generics as Exports (Generic, Generic1)+import GHC.IO.Exception as Exports+import Numeric as Exports+import Prelude as Exports hiding (all, and, any, concat, concatMap, elem, fail, foldl, foldl1, foldr, foldr1, id, mapM, mapM_, maximum, minimum, notElem, or, product, sequence, sequence_, sum, (.))+import System.Environment as Exports+import System.Exit as Exports+import System.IO as Exports+import System.IO.Error as Exports+import System.IO.Unsafe as Exports+import System.Mem as Exports+import System.Mem.StableName as Exports+import System.Timeout as Exports+import Test.QuickCheck as Exports (Arbitrary (..), Gen, choose, elements, listOf, oneof, sized, vectorOf)+import Text.Printf as Exports (hPrintf, printf)+import Text.Read as Exports (Read (..), readEither, readMaybe)+import TextBuilder as Exports (TextBuilder)+import Unsafe.Coerce as Exports++-- |+-- Compose a monad, which attempts to extend a value, based on the following input.+-- It does so recursively until the suffix alternative fails.+suffixRec :: (MonadPlus m) => m a -> (a -> m a) -> m a+suffixRec base suffix = base >>= extendMany suffix++extendMany :: (MonadPlus m) => (a -> m a) -> a -> m a+extendMany attempt = loop+ where+ loop !state =+ optional (attempt state) >>= \case+ Nothing -> pure state+ Just newState -> loop newState
+ library-internal/PostgresqlSyntax/Settings.hs view
@@ -0,0 +1,81 @@+-- |+-- Parse\/render options for the 'PostgresqlSyntax.Algebra' machinery.+--+-- The type is abstract: build a 'Settings' value with 'nullabilityMarkers',+-- 'haskellTargets' or 'haskellParamFields' and combine values with their+-- 'Semigroup'\/'Monoid' instances ('mempty' is faithful, standard Postgres).+-- Because no constructor or field selector is exported, a resolved+-- configuration indirection can be introduced later without a breaking change.+module PostgresqlSyntax.Settings+ ( Settings,+ nullabilityMarkers,+ haskellTargets,+ haskellParamFields,+ resolveNullabilityMarkers,+ resolveHaskellTargets,+ resolveHaskellParamFields,+ )+where++import PostgresqlSyntax.Prelude++-- |+-- Collection of parse\/render options. Use 'nullabilityMarkers',+-- 'haskellTargets' or 'haskellParamFields' to build a value, and combine+-- values with their 'Semigroup'\/'Monoid' instances ('mempty' is faithful,+-- standard Postgres).+data Settings = Settings+ { optNullabilityMarkers :: Maybe Bool,+ optHaskellTargets :: Maybe Bool,+ optHaskellParamFields :: Maybe Bool+ }+ deriving (Show, Eq)++-- | Per option the right operand wins; an unset ('Nothing') option falls back+-- to the left operand's value.+instance Semigroup Settings where+ a <> b =+ Settings+ { optNullabilityMarkers = optNullabilityMarkers b <|> optNullabilityMarkers a,+ optHaskellTargets = optHaskellTargets b <|> optHaskellTargets a,+ optHaskellParamFields = optHaskellParamFields b <|> optHaskellParamFields a+ }++instance Monoid Settings where+ mempty = Settings Nothing Nothing Nothing++-- |+-- Opt into (or out of) the 'PostgresqlSyntax.Ast.Typename' @?@ nullability+-- markers.+nullabilityMarkers :: Bool -> Settings+nullabilityMarkers x = mempty {optNullabilityMarkers = Just x}++-- |+-- Opt into (or out of) Haskell targets in a @SELECT@ list: @$f (a::int8, b::text)@+-- applies a Haskell function or constructor to the listed columns and+-- @$R {field = a::int8}@ constructs or updates a Haskell record. These are+-- Kronor extensions for the hasql-th quasiquoters; they are not Postgres syntax.+haskellTargets :: Bool -> Settings+haskellTargets x = mempty {optHaskellTargets = Just x}++-- |+-- Opt into (or out of) Haskell field selectors on parameter placeholders:+-- @$1.$field@ names a field of the first Haskell parameter. A Kronor extension+-- for the hasql-th quasiquoters; not Postgres syntax.+haskellParamFields :: Bool -> Settings+haskellParamFields x = mempty {optHaskellParamFields = Just x}++-- |+-- Resolve the nullability-marker option to its effective value, defaulting to+-- @False@ (standard Postgres). Internal - used at parse\/render sites, not+-- re-exported from the "PostgresqlSyntax" facade.+resolveNullabilityMarkers :: Settings -> Bool+resolveNullabilityMarkers = fromMaybe False . optNullabilityMarkers++-- | Resolve the Haskell-targets option, defaulting to @False@. Internal.+resolveHaskellTargets :: Settings -> Bool+resolveHaskellTargets = fromMaybe False . optHaskellTargets++-- | Resolve the Haskell-param-fields option, defaulting to @False@. Internal.+resolveHaskellParamFields :: Settings -> Bool+resolveHaskellParamFields = fromMaybe False . optHaskellParamFields
+ library-internal/PostgresqlSyntax/Validation.hs view
@@ -0,0 +1,54 @@+module PostgresqlSyntax.Validation where++import qualified Data.Text as Text+import qualified PostgresqlSyntax.KeywordSet as KeywordSet+import qualified PostgresqlSyntax.Predicate as Predicate+import PostgresqlSyntax.Prelude++{-+The operator name is a sequence of up to NAMEDATALEN-1 (63 by default)+characters from the following list:+++ - * / < > = ~ ! @ # % ^ & | ` ?++There are a few restrictions on your choice of name:+-- and /* cannot appear anywhere in an operator name,+since they will be taken as the start of a comment.++A multicharacter operator name cannot end in + or -,+unless the name also contains at least one of these characters:++~ ! @ # % ^ & | ` ?++For example, @- is an allowed operator name, but *- is not.+This restriction allows PostgreSQL to parse SQL-compliant+commands without requiring spaces between tokens.+The use of => as an operator name is deprecated.+It may be disallowed altogether in a future release.++The operator != is mapped to <> on input,+so these two names are always equivalent.+-}+op :: Text -> Maybe Text+op a =+ if Text.null a+ then Just ("Operator is empty")+ else+ if Text.any (not . Predicate.opChar) a+ then Just ("Operator contains a char that's not a valid operator char: " <> a)+ else+ if Text.isInfixOf "--" a+ then Just ("Operator contains a prohibited \"--\" sequence: " <> a)+ else+ if Text.isInfixOf "/*" a+ then Just ("Operator contains a prohibited \"/*\" sequence: " <> a)+ else+ if Predicate.inSet KeywordSet.nonOp a+ then Just ("Operator is not generic: " <> a)+ else+ if Text.find Predicate.prohibitionLiftingOpChar a & isJust+ then Nothing+ else+ if Predicate.prohibitedOpChar (Text.last a)+ then Just ("Operator ends with a prohibited char: " <> a)+ else Nothing
+ library/PostgresqlSyntax.hs view
@@ -0,0 +1,41 @@+-- |+-- Parse Postgres SQL fragments into an AST and render that AST back to text.+--+-- Pick the 'PostgresqlSyntax.Ast' node type that corresponds to the syntax+-- you want to parse (e.g. 'PostgresqlSyntax.Ast.AExpr' for an expression),+-- then use 'parse' (or one of its error-detail variants) to get a value of+-- that type. Use 'toText' to go the other way and render an AST value back+-- into Postgres SQL syntax.+--+-- 'Settings' (built via 'nullabilityMarkers' and combined with+-- 'Semigroup'\/'Monoid') control optional parsing\/rendering behavior; pass+-- 'mempty' for standard Postgres syntax.+module PostgresqlSyntax+ ( -- * Parsing and rendering+ Algebra.IsAst (..),+ Algebra.toText,+ Algebra.parse,+ Algebra.parseWithPosError,+ Algebra.parseWithSourcePosError,++ -- * Settings+ Settings.Settings,+ Settings.nullabilityMarkers,+ Settings.haskellTargets,+ Settings.haskellParamFields,++ -- * Lowering the extensions away+ Lowering.InputParams,+ Lowering.lower,+ Lowering.renameParams,+ Lowering.eraseHaskellTargets,++ -- * AST+ module PostgresqlSyntax.Ast,+ )+where++import qualified PostgresqlSyntax.Algebra as Algebra+import PostgresqlSyntax.Ast+import qualified PostgresqlSyntax.Lowering as Lowering+import qualified PostgresqlSyntax.Settings as Settings
+ nesting-bench/Main.hs view
@@ -0,0 +1,79 @@+{-# OPTIONS_GHC -Wno-missing-signatures #-}++-- |+-- Repeatable timing harness for the nesting-depth blowup described in+-- @docs/tasks/parser-exponential-time-on-nesting.md@.+--+-- Run with @cabal run nesting-bench@. Prints a table of parse times so the+-- before\/after effect of a fix is measurable rather than asserted.+--+-- Each case is given a wall-clock budget; a case that exceeds it is reported+-- as a timeout instead of hanging the run.+module Main where++import Control.Concurrent.MVar+import qualified Data.Text as Text+import PostgresqlSyntax (AExpr, parse)+import Prelude+import System.Clock++-- * Inputs++-- | Case 1: @((((a + b))))@ with @n@ layers of redundant parentheses.+nestedParens :: Int -> Text+nestedParens n =+ Text.replicate n "(" <> "a + b" <> Text.replicate n ")"++-- | Case 2: the user-reported shape - a sum of @COALESCE@ terms, split into+-- two parenthesised groups subtracted from one another, wrapped in a few+-- redundant parentheses.+coalesceSum :: Int -> Int -> Text+coalesceSum terms wrap =+ Text.replicate wrap "("+ <> group 0+ <> " - "+ <> group terms+ <> Text.replicate wrap ")"+ where+ group off =+ "(" <> Text.intercalate " + " [term (off + i) | i <- [1 .. terms]] <> ")"+ term i =+ "coalesce(c" <> Text.pack (show i) <> ", 0)"++-- * Timing++-- | Run an action under a wall-clock budget. 'Nothing' on timeout.+withTimeout :: Int -> IO a -> IO (Maybe a)+withTimeout micros action = do+ slot <- newEmptyMVar+ _ <- forkIO (action >>= \x -> putMVar slot (Just x))+ _ <- forkIO (threadDelay micros >> putMVar slot Nothing)+ takeMVar slot++-- | Parse and fully force the result, returning elapsed seconds.+time :: Text -> IO Double+time input = do+ start <- getTime Monotonic+ _ <- evaluate (force (either (const "err") (const "ok") (parse @AExpr mempty input) :: String))+ end <- getTime Monotonic+ pure (fromIntegral (toNanoSecs (diffTimeSpec end start)) / 1e9)++budgetSeconds :: Double+budgetSeconds = 10++measure :: String -> Text -> IO ()+measure label input = do+ result <- withTimeout (round (budgetSeconds * 1e6)) (time input)+ putStrLn $ case result of+ Nothing -> pad label <> " TIMEOUT (>" <> show (round budgetSeconds :: Int) <> "s), " <> show (Text.length input) <> " chars"+ Just seconds -> pad label <> " " <> show seconds <> "s, " <> show (Text.length input) <> " chars"+ where+ pad s = s <> replicate (max 0 (28 - length s)) ' '++main :: IO ()+main = do+ putStrLn "Case 1: ((((a + b)))) at increasing nesting depth"+ mapM_ (\n -> measure (" depth " <> show n) (nestedParens n)) [1, 2, 4, 6, 8, 10, 12, 14, 16, 20, 30, 50, 100, 500]+ putStrLn ""+ putStrLn "Case 2: sum-of-COALESCE, two groups, 6 redundant wraps"+ mapM_ (\t -> measure (" " <> show (2 * t) <> " terms") (coalesceSum t 6)) [4, 8, 12, 16, 20, 24]
+ postgresql-syntax-kronor.cabal view
@@ -0,0 +1,473 @@+cabal-version: 3.0+name: postgresql-syntax-kronor+version: 0.5.0.3+category: Database, PostgreSQL, Parsing+synopsis: PostgreSQL AST parsing and rendering, with Kronor's Haskell target extension+description:+ Kronor's fork of \"postgresql-syntax\": the Postgres syntax tree and+ related utils extracted from the \"hasql-th\" package, extended with the+ Haskell targets and parameter fields that \"hasql-th-kronor\" quasiquotes+ accept. It exposes the same modules as \"postgresql-syntax\".++homepage: https://github.com/kronor-io/postgresql-syntax+bug-reports: https://github.com/kronor-io/postgresql-syntax/issues+author: Nikita Volkov <nikita.y.volkov@mail.ru>+maintainer: José Lorenzo Rodríguez <lorenzo@kronor.io>+copyright: (c) 2020, Nikita Volkov+license: MIT+license-file: LICENSE+extra-doc-files:+ CHANGELOG.md+ LICENSE+ README.md++source-repository head+ type: git+ location: https://github.com/kronor-io/postgresql-syntax+ branch: hackage++common base-settings+ default-language: Haskell2010+ default-extensions:+ ApplicativeDo+ BangPatterns+ ConstraintKinds+ DataKinds+ DefaultSignatures+ DeriveDataTypeable+ DeriveFoldable+ DeriveFunctor+ DeriveGeneric+ DeriveTraversable+ DuplicateRecordFields+ EmptyDataDecls+ FlexibleContexts+ FlexibleInstances+ FunctionalDependencies+ GADTs+ GeneralizedNewtypeDeriving+ LambdaCase+ LiberalTypeSynonyms+ MagicHash+ MultiParamTypeClasses+ MultiWayIf+ NoImplicitPrelude+ NoMonomorphismRestriction+ OverloadedStrings+ ParallelListComp+ PatternGuards+ RankNTypes+ RecordWildCards+ ScopedTypeVariables+ StandaloneDeriving+ TupleSections+ TypeApplications+ TypeFamilies+ TypeOperators+ UnboxedTuples++library internal+ import: base-settings+ hs-source-dirs: library-internal+ exposed-modules:+ PostgresqlSyntax.Algebra+ PostgresqlSyntax.Ast+ PostgresqlSyntax.Ast.AExpr+ PostgresqlSyntax.Ast.AexprConst+ PostgresqlSyntax.Ast.AExprReversableOp+ PostgresqlSyntax.Ast.AliasClause+ PostgresqlSyntax.Ast.AllOp+ PostgresqlSyntax.Ast.AnyName+ PostgresqlSyntax.Ast.AnyOperator+ PostgresqlSyntax.Ast.ArrayBounds+ PostgresqlSyntax.Ast.ArrayExpr+ PostgresqlSyntax.Ast.ArrayExprList+ PostgresqlSyntax.Ast.AscDesc+ PostgresqlSyntax.Ast.Attrs+ PostgresqlSyntax.Ast.Bconst+ PostgresqlSyntax.Ast.BExpr+ PostgresqlSyntax.Ast.BExprIsOp+ PostgresqlSyntax.Ast.Bit+ PostgresqlSyntax.Ast.CallStmt+ PostgresqlSyntax.Ast.CaseExpr+ PostgresqlSyntax.Ast.CExpr+ PostgresqlSyntax.Ast.Character+ PostgresqlSyntax.Ast.Columnref+ PostgresqlSyntax.Ast.CommonTableExpr+ PostgresqlSyntax.Ast.ConfExpr+ PostgresqlSyntax.Ast.ConstCharacter+ PostgresqlSyntax.Ast.ConstDatetime+ PostgresqlSyntax.Ast.ConstTypename+ PostgresqlSyntax.Ast.DeleteStmt+ PostgresqlSyntax.Ast.ExplicitRow+ PostgresqlSyntax.Ast.ExprList+ PostgresqlSyntax.Ast.ExtractArg+ PostgresqlSyntax.Ast.ExtractList+ PostgresqlSyntax.Ast.Fconst+ PostgresqlSyntax.Ast.ForLockingClause+ PostgresqlSyntax.Ast.ForLockingItem+ PostgresqlSyntax.Ast.ForLockingStrength+ PostgresqlSyntax.Ast.FrameBound+ PostgresqlSyntax.Ast.FrameClause+ PostgresqlSyntax.Ast.FrameClauseMode+ PostgresqlSyntax.Ast.FrameExtent+ PostgresqlSyntax.Ast.FromClause+ PostgresqlSyntax.Ast.FromList+ PostgresqlSyntax.Ast.FuncAliasClause+ PostgresqlSyntax.Ast.FuncApplication+ PostgresqlSyntax.Ast.FuncApplicationParams+ PostgresqlSyntax.Ast.FuncArgExpr+ PostgresqlSyntax.Ast.FuncConstArgs+ PostgresqlSyntax.Ast.FuncExpr+ PostgresqlSyntax.Ast.FuncExprCommonSubexpr+ PostgresqlSyntax.Ast.FuncExprWindowless+ PostgresqlSyntax.Ast.FuncName+ PostgresqlSyntax.Ast.FuncTable+ PostgresqlSyntax.Ast.GenericType+ PostgresqlSyntax.Ast.GroupByItem+ PostgresqlSyntax.Ast.GroupClause+ PostgresqlSyntax.Ast.HavingClause+ PostgresqlSyntax.Ast.HsTargetEl+ PostgresqlSyntax.Ast.HsTargetList+ PostgresqlSyntax.Ast.Iconst+ PostgresqlSyntax.Ast.Ident+ PostgresqlSyntax.Ast.ImplicitRow+ PostgresqlSyntax.Ast.IndexElem+ PostgresqlSyntax.Ast.IndexElemDef+ PostgresqlSyntax.Ast.IndexParams+ PostgresqlSyntax.Ast.Indirection+ PostgresqlSyntax.Ast.IndirectionEl+ PostgresqlSyntax.Ast.InExpr+ PostgresqlSyntax.Ast.InsertColumnItem+ PostgresqlSyntax.Ast.InsertColumnList+ PostgresqlSyntax.Ast.InsertRest+ PostgresqlSyntax.Ast.InsertStmt+ PostgresqlSyntax.Ast.InsertTarget+ PostgresqlSyntax.Ast.Interval+ PostgresqlSyntax.Ast.IntervalSecond+ PostgresqlSyntax.Ast.IntoClause+ PostgresqlSyntax.Ast.JoinedTable+ PostgresqlSyntax.Ast.JoinQual+ PostgresqlSyntax.Ast.JoinType+ PostgresqlSyntax.Ast.LimitClause+ PostgresqlSyntax.Ast.MathOp+ PostgresqlSyntax.Ast.NameList+ PostgresqlSyntax.Ast.NullsOrder+ PostgresqlSyntax.Ast.Numeric+ PostgresqlSyntax.Ast.OffsetClause+ PostgresqlSyntax.Ast.OnConflict+ PostgresqlSyntax.Ast.OnConflictDo+ PostgresqlSyntax.Ast.Op+ PostgresqlSyntax.Ast.OptOrdinality+ PostgresqlSyntax.Ast.OptTempTableName+ PostgresqlSyntax.Ast.OptVarying+ PostgresqlSyntax.Ast.OverClause+ PostgresqlSyntax.Ast.OverlayList+ PostgresqlSyntax.Ast.OverrideKind+ PostgresqlSyntax.Ast.PositionList+ PostgresqlSyntax.Ast.PreparableStmt+ PostgresqlSyntax.Ast.QualAllOp+ PostgresqlSyntax.Ast.QualifiedName+ PostgresqlSyntax.Ast.QualOp+ PostgresqlSyntax.Ast.RelationExpr+ PostgresqlSyntax.Ast.RelationExprOptAlias+ PostgresqlSyntax.Ast.ReturningClause+ PostgresqlSyntax.Ast.Row+ PostgresqlSyntax.Ast.RowsfromItem+ PostgresqlSyntax.Ast.RowsfromList+ PostgresqlSyntax.Ast.Sconst+ PostgresqlSyntax.Ast.SelectBinOp+ PostgresqlSyntax.Ast.SelectClause+ PostgresqlSyntax.Ast.SelectFetchFirstValue+ PostgresqlSyntax.Ast.SelectLimit+ PostgresqlSyntax.Ast.SelectLimitValue+ PostgresqlSyntax.Ast.SelectNoParens+ PostgresqlSyntax.Ast.SelectStmt+ PostgresqlSyntax.Ast.SelectWithParens+ PostgresqlSyntax.Ast.SetClause+ PostgresqlSyntax.Ast.SetClauseList+ PostgresqlSyntax.Ast.SetTarget+ PostgresqlSyntax.Ast.SetTargetList+ PostgresqlSyntax.Ast.SimpleSelect+ PostgresqlSyntax.Ast.SimpleTypename+ PostgresqlSyntax.Ast.SortBy+ PostgresqlSyntax.Ast.SortClause+ PostgresqlSyntax.Ast.SubqueryOp+ PostgresqlSyntax.Ast.SubstrList+ PostgresqlSyntax.Ast.SubstrListFromFor+ PostgresqlSyntax.Ast.SubType+ PostgresqlSyntax.Ast.SymbolicExprBinOp+ PostgresqlSyntax.Ast.TableFuncElement+ PostgresqlSyntax.Ast.TableFuncElementList+ PostgresqlSyntax.Ast.TableRef+ PostgresqlSyntax.Ast.TablesampleClause+ PostgresqlSyntax.Ast.TargetEl+ PostgresqlSyntax.Ast.Targeting+ PostgresqlSyntax.Ast.TargetList+ PostgresqlSyntax.Ast.Timezone+ PostgresqlSyntax.Ast.TrimList+ PostgresqlSyntax.Ast.TrimModifier+ PostgresqlSyntax.Ast.TypeList+ PostgresqlSyntax.Ast.Typename+ PostgresqlSyntax.Ast.TypenameArrayDimensions+ PostgresqlSyntax.Ast.UpdateStmt+ PostgresqlSyntax.Ast.UsingClause+ PostgresqlSyntax.Ast.ValuesClause+ PostgresqlSyntax.Ast.VerbalExprBinOp+ PostgresqlSyntax.Ast.WhenClause+ PostgresqlSyntax.Ast.WhenClauseList+ PostgresqlSyntax.Ast.WhereClause+ PostgresqlSyntax.Ast.WhereOrCurrentClause+ PostgresqlSyntax.Ast.WindowClause+ PostgresqlSyntax.Ast.WindowDefinition+ PostgresqlSyntax.Ast.WindowExclusionClause+ PostgresqlSyntax.Ast.WindowSpecification+ PostgresqlSyntax.Ast.WithClause+ PostgresqlSyntax.Ast.Xconst+ PostgresqlSyntax.CharSet+ PostgresqlSyntax.Extras.Error+ PostgresqlSyntax.Extras.HeadedMegaparsec+ PostgresqlSyntax.Extras.NonEmpty+ PostgresqlSyntax.Extras.TextBuilder+ PostgresqlSyntax.Helpers.Gens+ PostgresqlSyntax.Helpers.Parsers+ PostgresqlSyntax.Helpers.Shrinks+ PostgresqlSyntax.Helpers.TextBuilders+ PostgresqlSyntax.KeywordSet+ PostgresqlSyntax.Lowering+ PostgresqlSyntax.Predicate+ PostgresqlSyntax.Prelude+ PostgresqlSyntax.Settings+ PostgresqlSyntax.Validation++ build-depends:+ base >=4.12 && <5,+ bytestring >=0.10 && <0.13,+ case-insensitive >=1.2.1 && <2,+ containers >=0.6 && <0.9,+ hashable >=1.3.5 && <2,+ headed-megaparsec >=0.2.0.1 && <0.3,+ megaparsec >=9.2 && <10,+ parser-combinators >=1.3 && <1.4,+ QuickCheck >=2.17 && <3,+ text >=1 && <3,+ text-builder >=1 && <1.1,+ unordered-containers >=0.2.16 && <0.3,++library+ import: base-settings+ hs-source-dirs: library+ exposed-modules:+ PostgresqlSyntax++ build-depends:+ postgresql-syntax-kronor:internal++test-suite hspec-test+ import: base-settings+ type: exitcode-stdio-1.0+ hs-source-dirs: hspec-test+ main-is: Main.hs+ other-modules:+ Ast.AexprConstSpec+ Ast.AExprReversableOpSpec+ Ast.AExprSpec+ Ast.AliasClauseSpec+ Ast.AllOpSpec+ Ast.AnyNameSpec+ Ast.AnyOperatorSpec+ Ast.ArrayBoundsSpec+ Ast.ArrayExprListSpec+ Ast.ArrayExprSpec+ Ast.AscDescSpec+ Ast.AttrsSpec+ Ast.BconstSpec+ Ast.BExprIsOpSpec+ Ast.BExprSpec+ Ast.BitSpec+ Ast.CallStmtSpec+ Ast.CaseExprSpec+ Ast.CExprSpec+ Ast.CharacterSpec+ Ast.ColumnrefSpec+ Ast.CommonTableExprSpec+ Ast.ConfExprSpec+ Ast.ConstCharacterSpec+ Ast.ConstDatetimeSpec+ Ast.ConstTypenameSpec+ Ast.DeleteStmtSpec+ Ast.ExplicitRowSpec+ Ast.ExprListSpec+ Ast.ExtractArgSpec+ Ast.ExtractListSpec+ Ast.FconstSpec+ Ast.ForLockingClauseSpec+ Ast.ForLockingItemSpec+ Ast.ForLockingStrengthSpec+ Ast.FrameBoundSpec+ Ast.FrameClauseModeSpec+ Ast.FrameClauseSpec+ Ast.FrameExtentSpec+ Ast.FromClauseSpec+ Ast.FromListSpec+ Ast.FuncAliasClauseSpec+ Ast.FuncApplicationParamsSpec+ Ast.FuncApplicationSpec+ Ast.FuncArgExprSpec+ Ast.FuncConstArgsSpec+ Ast.FuncExprCommonSubexprSpec+ Ast.FuncExprSpec+ Ast.FuncExprWindowlessSpec+ Ast.FuncNameSpec+ Ast.FuncTableSpec+ Ast.GenericTypeSpec+ Ast.GroupByItemSpec+ Ast.GroupClauseSpec+ Ast.HavingClauseSpec+ Ast.HsTargetElSpec+ Ast.HsTargetListSpec+ Ast.IconstSpec+ Ast.IdentSpec+ Ast.ImplicitRowSpec+ Ast.IndexElemDefSpec+ Ast.IndexElemSpec+ Ast.IndexParamsSpec+ Ast.IndirectionElSpec+ Ast.IndirectionSpec+ Ast.InExprSpec+ Ast.InsertColumnItemSpec+ Ast.InsertColumnListSpec+ Ast.InsertRestSpec+ Ast.InsertStmtSpec+ Ast.InsertTargetSpec+ Ast.IntervalSecondSpec+ Ast.IntervalSpec+ Ast.IntoClauseSpec+ Ast.JoinedTableSpec+ Ast.JoinQualSpec+ Ast.JoinTypeSpec+ Ast.LimitClauseSpec+ Ast.MathOpSpec+ Ast.NameListSpec+ Ast.NullsOrderSpec+ Ast.NumericSpec+ Ast.OffsetClauseSpec+ Ast.OnConflictDoSpec+ Ast.OnConflictSpec+ Ast.OpSpec+ Ast.OptOrdinalitySpec+ Ast.OptTempTableNameSpec+ Ast.OptVaryingSpec+ Ast.OverClauseSpec+ Ast.OverlayListSpec+ Ast.OverrideKindSpec+ Ast.PositionListSpec+ Ast.PreparableStmtSpec+ Ast.QualAllOpSpec+ Ast.QualifiedNameSpec+ Ast.QualOpSpec+ Ast.RelationExprOptAliasSpec+ Ast.RelationExprSpec+ Ast.ReturningClauseSpec+ Ast.RowsfromItemSpec+ Ast.RowsfromListSpec+ Ast.RowSpec+ Ast.SconstSpec+ Ast.SelectBinOpSpec+ Ast.SelectClauseSpec+ Ast.SelectFetchFirstValueSpec+ Ast.SelectLimitSpec+ Ast.SelectLimitValueSpec+ Ast.SelectNoParensSpec+ Ast.SelectStmtSpec+ Ast.SelectWithParensSpec+ Ast.SetClauseListSpec+ Ast.SetClauseSpec+ Ast.SetTargetListSpec+ Ast.SetTargetSpec+ Ast.SimpleSelectSpec+ Ast.SimpleTypenameSpec+ Ast.SortBySpec+ Ast.SortClauseSpec+ Ast.SubqueryOpSpec+ Ast.SubstrListFromForSpec+ Ast.SubstrListSpec+ Ast.SubTypeSpec+ Ast.SymbolicExprBinOpSpec+ Ast.TableFuncElementListSpec+ Ast.TableFuncElementSpec+ Ast.TableRefSpec+ Ast.TablesampleClauseSpec+ Ast.TargetElSpec+ Ast.TargetingSpec+ Ast.TargetListSpec+ Ast.TimezoneSpec+ Ast.TrimListSpec+ Ast.TrimModifierSpec+ Ast.TypeListSpec+ Ast.TypenameArrayDimensionsSpec+ Ast.TypenameSpec+ Ast.UpdateStmtSpec+ Ast.UsingClauseSpec+ Ast.ValuesClauseSpec+ Ast.VerbalExprBinOpSpec+ Ast.WhenClauseListSpec+ Ast.WhenClauseSpec+ Ast.WhereClauseSpec+ Ast.WhereOrCurrentClauseSpec+ Ast.WindowClauseSpec+ Ast.WindowDefinitionSpec+ Ast.WindowExclusionClauseSpec+ Ast.WindowSpecificationSpec+ Ast.WithClauseSpec+ Ast.XconstSpec+ Helpers.Expectations+ Helpers.Specs+ LoweringSpec+ Spec++ ghc-options:+ -threaded+ -rtsopts+ -with-rtsopts=-N++ build-tool-depends:+ hspec-discover:hspec-discover >=2.10 && <3++ build-depends:+ hspec >=2.10 && <3,+ megaparsec >=9.2 && <10,+ postgresql-syntax-kronor:internal,+ QuickCheck >=2.17 && <3,+ rerebase >=1.6.1 && <2,++benchmark keyword-bench+ import: base-settings+ type: exitcode-stdio-1.0+ hs-source-dirs:+ bench++ main-is: Main.hs+ ghc-options: -O2+ build-depends:+ containers >=0.4 && <1,+ criterion >=1.6 && <1.7,+ headed-megaparsec >=0.2.0.1 && <0.3,+ megaparsec >=9.2 && <10,+ postgresql-syntax-kronor,+ QuickCheck >=2.17 && <3,+ rerebase >=1.6.1 && <2,++benchmark nesting-bench+ import: base-settings+ type: exitcode-stdio-1.0+ hs-source-dirs: nesting-bench+ main-is: Main.hs+ ghc-options:+ -O2+ -threaded++ build-depends:+ clock >=0.8 && <1,+ postgresql-syntax-kronor,+ rerebase >=1.6.1 && <2,