@@ -217,6 +217,7 @@ import Control.Monad.Reader
217
217
(Reader
218
218
,runReader
219
219
,ask
220
+ ,asks
220
221
)
221
222
222
223
import qualified Data.Set as Set
@@ -227,7 +228,7 @@ import Control.Applicative ((<**>))
227
228
import Data.Char (isDigit )
228
229
import Data.List (sort ,groupBy )
229
230
import Data.Function (on )
230
- import Data.Maybe (catMaybes , isJust )
231
+ import Data.Maybe (catMaybes , isJust , mapMaybe )
231
232
import Data.Text (Text )
232
233
import qualified Data.Text as T
233
234
@@ -379,7 +380,7 @@ u&"example quoted"
379
380
380
381
name :: Parser Name
381
382
name = do
382
- bl <- queryDialect diKeywords
383
+ bl <- askDialect diKeywords
383
384
uncurry Name <$> identifierTok bl
384
385
385
386
-- todo: replace (:[]) with a named function all over
@@ -561,7 +562,7 @@ typeName =
561
562
-- type names, plus all the type names which are
562
563
-- reserved words
563
564
reservedTypeNames = do
564
- stn <- queryDialect diSpecialTypeNames
565
+ stn <- askDialect diSpecialTypeNames
565
566
(: [] ) . Name Nothing . T. unwords <$> makeKeywordTree stn
566
567
567
568
@@ -771,7 +772,7 @@ idenExpr =
771
772
-- special cases for keywords that can be parsed as an iden or app
772
773
keywordFunctionOrIden = try $ do
773
774
x <- unquotedIdentifierTok [] Nothing
774
- d <- queryDialect id
775
+ d <- askDialect id
775
776
let i = T. toLower x `elem` diIdentifierKeywords d
776
777
a = T. toLower x `elem` diAppKeywords d
777
778
case () of
@@ -923,7 +924,7 @@ app =
923
924
openParen *> choice
924
925
[duplicates
925
926
<**> (commaSep1 scalarExpr
926
- <**> ((( option [] orderBy) <* closeParen)
927
+ <**> ((option [] orderBy <* closeParen)
927
928
<**> (optional afilter <$$$$$> AggregateApp )))
928
929
-- separate cases with no all or distinct which must have at
929
930
-- least one scalar expr
@@ -967,17 +968,17 @@ window :: Parser ([ScalarExpr] -> [Name] -> ScalarExpr)
967
968
window =
968
969
keyword_ " over" *> openParen *> option [] partitionBy
969
970
<**> (option [] orderBy
970
- <**> ((( optional frameClause) <* closeParen) <$$$$$> WindowApp ))
971
+ <**> ((optional frameClause <* closeParen) <$$$$$> WindowApp ))
971
972
where
972
973
partitionBy = keywords_ [" partition" ," by" ] *> commaSep1 scalarExpr
973
974
frameClause =
974
975
frameRowsRange -- TODO: this 'and' could be an issue
975
- <**> ( choice [(keyword_ " between" *> frameLimit True )
976
+ <**> choice [(keyword_ " between" *> frameLimit True )
976
977
<**> ((keyword_ " and" *> frameLimit True )
977
978
<$$$> FrameBetween )
978
979
-- maybe this should still use a b expression
979
980
-- for consistency
980
- ,frameLimit False <**> pure (flip FrameFrom )])
981
+ ,frameLimit False <**> pure (flip FrameFrom )]
981
982
frameRowsRange = FrameRows <$ keyword_ " rows"
982
983
<|> FrameRange <$ keyword_ " range"
983
984
frameLimit useB =
@@ -1013,7 +1014,7 @@ inSuffix =
1013
1014
where
1014
1015
inty = choice [True <$ keyword_ " in"
1015
1016
,False <$ keywords_ [" not" ," in" ]]
1016
- mkIn i v = \ e -> In i e v
1017
+ mkIn i v e = In i e v
1017
1018
1018
1019
{-
1019
1020
=== between
@@ -1034,14 +1035,15 @@ and operator. This is the call to scalarExprB.
1034
1035
1035
1036
betweenSuffix :: Parser (ScalarExpr -> ScalarExpr )
1036
1037
betweenSuffix =
1037
- makeOp <$> Name Nothing <$> opName
1038
+ makeOp . Name Nothing
1039
+ <$> opName
1038
1040
<*> scalarExprB
1039
1041
<*> (keyword_ " and" *> scalarExprB)
1040
1042
where
1041
1043
opName = choice
1042
1044
[" between" <$ keyword_ " between"
1043
1045
," not between" <$ try (keywords_ [" not" ," between" ])]
1044
- makeOp n b c = \ a -> SpecialOp [n] [a,b,c]
1046
+ makeOp n b c a = SpecialOp [n] [a,b,c]
1045
1047
1046
1048
{-
1047
1049
=== quantified comparison
@@ -1224,7 +1226,7 @@ opTable bExpr =
1224
1226
1225
1227
,[prefixKeyword " not" ]
1226
1228
1227
- ,if bExpr then [] else [ binaryKeywordL " and" ]
1229
+ ,[ binaryKeywordL " and" | not bExpr ]
1228
1230
1229
1231
,[binaryKeywordL " or" ]
1230
1232
@@ -1368,7 +1370,7 @@ from = keyword_ "from" *> commaSep1 tref
1368
1370
where
1369
1371
-- TODO: use P (a->) for the join tref suffix
1370
1372
-- chainl or buildexpressionparser
1371
- tref = (nonJoinTref <?> " table ref" ) >>= optionSuffix ( joinTrefSuffix)
1373
+ tref = (nonJoinTref <?> " table ref" ) >>= optionSuffix joinTrefSuffix
1372
1374
nonJoinTref = choice
1373
1375
[parens $ choice
1374
1376
[TRQueryExpr <$> queryExpr
@@ -1519,7 +1521,7 @@ queryExpr = E.makeExprParser qeterm qeOpTable
1519
1521
mkSelect
1520
1522
<$> option SQDefault duplicates
1521
1523
<*> selectList
1522
- <*> ( optional tableExpression) <?> " table expression"
1524
+ <*> optional tableExpression <?> " table expression"
1523
1525
mkSelect d sl Nothing =
1524
1526
toQueryExpr $ makeSelect {msSetQuantifier = d, msSelectList = sl}
1525
1527
mkSelect d sl (Just (TableExpression f w g h od ofs fe)) =
@@ -1621,15 +1623,15 @@ statementWithoutSemicolon = choice
1621
1623
]
1622
1624
1623
1625
statement :: Parser Statement
1624
- statement = statementWithoutSemicolon <* optional semi <|> semi *> pure EmptyStatement
1626
+ statement = statementWithoutSemicolon <* optional semi <|> EmptyStatement <$ semi
1625
1627
1626
1628
createSchema :: Parser Statement
1627
1629
createSchema = keyword_ " schema" >>
1628
1630
CreateSchema <$> names
1629
1631
1630
1632
createTable :: Parser Statement
1631
1633
createTable = do
1632
- d <- queryDialect id
1634
+ d <- askDialect id
1633
1635
let
1634
1636
parseColumnDef = TableColumnDef <$> columnDef
1635
1637
parseConstraintDef = uncurry TableConstraintDef <$> tableConstraintDef
@@ -1675,7 +1677,7 @@ columnDef = ColumnDef <$> name <*> typeName
1675
1677
tableConstraintDef :: Parser (Maybe [Name ], TableConstraint )
1676
1678
tableConstraintDef =
1677
1679
(,)
1678
- <$> ( optional (keyword_ " constraint" *> names) )
1680
+ <$> optional (keyword_ " constraint" *> names)
1679
1681
<*> (unique <|> primaryKey <|> check <|> references)
1680
1682
where
1681
1683
unique = keyword_ " unique" >>
@@ -1725,7 +1727,7 @@ colConstraintDef =
1725
1727
unique = ColUniqueConstraint <$ keyword_ " unique"
1726
1728
primaryKey = do
1727
1729
keywords_ [" primary" , " key" ]
1728
- d <- queryDialect id
1730
+ d <- askDialect id
1729
1731
autoincrement <- if diAutoincrement d
1730
1732
then optional (keyword_ " autoincrement" )
1731
1733
else pure Nothing
@@ -2099,9 +2101,9 @@ makeKeywordTree sets =
2099
2101
parseGroup :: [[Text ]] -> Parser [Text ]
2100
2102
parseGroup l@ ((k: _): _) = do
2101
2103
keyword_ k
2102
- let tls = catMaybes $ map safeTail l
2104
+ let tls = mapMaybe safeTail l
2103
2105
pr = (k: ) <$> parseTrees tls
2104
- if ( or $ map null tls)
2106
+ if any null tls
2105
2107
then pr <|> pure [k]
2106
2108
else pr
2107
2109
parseGroup _ = guard False >> fail " impossible"
@@ -2175,7 +2177,7 @@ unsignedInteger = read . T.unpack <$> sqlNumberTok True <?> "natural number"
2175
2177
-- todo: work out the symbol parsing better
2176
2178
2177
2179
symbol :: Text -> Parser Text
2178
- symbol s = symbolTok (Just s) <?> ( T. unpack s)
2180
+ symbol s = symbolTok (Just s) <?> T. unpack s
2179
2181
2180
2182
singleCharSymbol :: Char -> Parser Char
2181
2183
singleCharSymbol c = c <$ symbol (T. singleton c)
@@ -2205,12 +2207,12 @@ semi = singleCharSymbol ';' <?> ""
2205
2207
-- = helper functions
2206
2208
2207
2209
keyword :: Text -> Parser Text
2208
- keyword k = unquotedIdentifierTok [] (Just k) <?> ( T. unpack k)
2210
+ keyword k = unquotedIdentifierTok [] (Just k) <?> T. unpack k
2209
2211
2210
2212
-- helper function to improve error messages
2211
2213
2212
2214
keywords_ :: [Text ] -> Parser ()
2213
- keywords_ ks = mapM_ keyword_ ks <?> ( T. unpack (T. unwords ks) )
2215
+ keywords_ ks = mapM_ keyword_ ks <?> T. unpack (T. unwords ks)
2214
2216
2215
2217
2216
2218
parens :: Parser a -> Parser a
@@ -2270,7 +2272,7 @@ stringTokExtend = do
2270
2272
guard (s == " '" && e == " '" )
2271
2273
(s',e',y) <- stringTokExtend
2272
2274
guard (s' == " '" && e' == " '" )
2273
- pure $ (s,e,x <> y)
2275
+ pure (s,e,x <> y)
2274
2276
,pure (s,e,x)
2275
2277
]
2276
2278
@@ -2361,12 +2363,8 @@ unquotedIdentifierTok blackList kw = token test Set.empty <?> ""
2361
2363
-- dialect
2362
2364
2363
2365
guardDialect :: (Dialect -> Bool ) -> Parser ()
2364
- guardDialect p = do
2365
- d <- ask
2366
- guard (p d)
2367
-
2368
- queryDialect :: (Dialect -> a ) -> Parser a
2369
- queryDialect f = do
2370
- d <- ask
2371
- pure $ f d
2366
+ guardDialect p = guard . p =<< ask
2367
+
2368
+ askDialect :: (Dialect -> a ) -> Parser a
2369
+ askDialect = asks
2372
2370
0 commit comments