Skip to content

Commit b14a783

Browse files
JakeWheatprescientmoon
authored andcommitted
hlint pass
1 parent ef8438a commit b14a783

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

Language/SQL/SimpleSQL/Lex.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ try again to add annotation to the ast
7171
-}
7272

7373
-- | Lexer for SQL.
74-
{-# LANGUAGE TupleSections #-}
7574
{-# LANGUAGE OverloadedStrings #-}
7675
{-# LANGUAGE RecordWildCards #-}
7776
{-# LANGUAGE TypeFamilies #-}
@@ -150,6 +149,7 @@ import Data.Char
150149
import Control.Monad (void, guard)
151150
import Data.Text (Text)
152151
import qualified Data.Text as T
152+
import Data.Maybe (fromMaybe)
153153

154154
------------------------------------------------------------------------------
155155

@@ -223,13 +223,13 @@ lexSQL
223223
-- ^ the SQL source to lex
224224
-> Either ParseError [Token]
225225
lexSQL dialect fn p src =
226-
fmap (map tokenVal) $ lexSQLWithPositions dialect fn p src
226+
map tokenVal <$> lexSQLWithPositions dialect fn p src
227227

228-
myParse :: Text -> (Maybe (Int,Int)) -> Parser a -> Text -> Either ParseError a
228+
myParse :: Text -> Maybe (Int,Int) -> Parser a -> Text -> Either ParseError a
229229
myParse name sp' p s =
230-
let sp = maybe (1,1) id sp'
230+
let sp = fromMaybe (1,1) sp'
231231
ps = SourcePos (T.unpack name) (mkPos $ fst sp) (mkPos $ snd sp)
232-
is = (initialState (T.unpack name) s)
232+
is = initialState (T.unpack name) s
233233
sps = (statePosState is) {pstateSourcePos = ps}
234234
is' = is {statePosState = sps}
235235
in snd $ runParser' p is'
@@ -352,7 +352,7 @@ sqlString d = dollarString <|> csString <|> normalString
352352
<$> try cs
353353
<*> pure "'"
354354
<*> normalStringSuffix False ""
355-
csPrefixes = (map (flip T.cons "'") "nNbBxX") ++ ["u&'", "U&'"]
355+
csPrefixes = map (`T.cons` "'") "nNbBxX" ++ ["u&'", "U&'"]
356356
cs :: Parser Text
357357
cs = choice $ map string csPrefixes
358358

@@ -387,7 +387,7 @@ identifier d =
387387
-- try is used here to avoid a conflict with identifiers
388388
-- and quoted strings which also start with a 'u'
389389
unicodeQuotedIden = Identifier
390-
<$> (f <$> try ((oneOf "uU") <* string "&"))
390+
<$> (f <$> try (oneOf "uU" <* string "&"))
391391
<*> qidenPart
392392
where f x = Just (T.cons x "&\"", "\"")
393393
qidenPart = char '"' *> qidenSuffix ""
@@ -404,7 +404,7 @@ identifierString :: Parser Text
404404
identifierString = (do
405405
c <- satisfy isFirstLetter
406406
choice
407-
[T.cons c <$> (takeWhileP (Just "identifier char") isIdentifierChar)
407+
[T.cons c <$> takeWhileP (Just "identifier char") isIdentifierChar
408408
,pure $ T.singleton c]) <?> "identifier"
409409
where
410410
isFirstLetter c = c == '_' || isAlpha c
@@ -486,7 +486,7 @@ sqlNumber d =
486486
-- this is for definitely avoiding possibly ambiguous source
487487
<* choice [-- special case to allow e.g. 1..2
488488
guard (diPostgresSymbols d)
489-
*> (void $ lookAhead $ try $ (string ".." <?> ""))
489+
*> void (lookAhead $ try (string ".." <?> ""))
490490
<|> void (notFollowedBy (oneOf "eE."))
491491
,notFollowedBy (oneOf "eE.")
492492
]
@@ -737,7 +737,7 @@ two symbols next to eachother will fail if the symbols can combine and
737737
| diPostgresSymbols d
738738
, Symbol a' <- a
739739
, Symbol b' <- b
740-
, b' `notElem` ["+", "-"] || or (map (`T.elem` a') "~!@#%^&|`?") = False
740+
, b' `notElem` ["+", "-"] || any (`T.elem` a') ("~!@#%^&|`?" :: [Char]) = False
741741

742742
{-
743743
check two adjacent symbols in non postgres where the combination

Language/SQL/SimpleSQL/Parse.hs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ import Control.Monad.Reader
217217
(Reader
218218
,runReader
219219
,ask
220+
,asks
220221
)
221222

222223
import qualified Data.Set as Set
@@ -227,7 +228,7 @@ import Control.Applicative ((<**>))
227228
import Data.Char (isDigit)
228229
import Data.List (sort,groupBy)
229230
import Data.Function (on)
230-
import Data.Maybe (catMaybes, isJust)
231+
import Data.Maybe (catMaybes, isJust, mapMaybe)
231232
import Data.Text (Text)
232233
import qualified Data.Text as T
233234

@@ -379,7 +380,7 @@ u&"example quoted"
379380

380381
name :: Parser Name
381382
name = do
382-
bl <- queryDialect diKeywords
383+
bl <- askDialect diKeywords
383384
uncurry Name <$> identifierTok bl
384385

385386
-- todo: replace (:[]) with a named function all over
@@ -561,7 +562,7 @@ typeName =
561562
-- type names, plus all the type names which are
562563
-- reserved words
563564
reservedTypeNames = do
564-
stn <- queryDialect diSpecialTypeNames
565+
stn <- askDialect diSpecialTypeNames
565566
(:[]) . Name Nothing . T.unwords <$> makeKeywordTree stn
566567

567568

@@ -771,7 +772,7 @@ idenExpr =
771772
-- special cases for keywords that can be parsed as an iden or app
772773
keywordFunctionOrIden = try $ do
773774
x <- unquotedIdentifierTok [] Nothing
774-
d <- queryDialect id
775+
d <- askDialect id
775776
let i = T.toLower x `elem` diIdentifierKeywords d
776777
a = T.toLower x `elem` diAppKeywords d
777778
case () of
@@ -923,7 +924,7 @@ app =
923924
openParen *> choice
924925
[duplicates
925926
<**> (commaSep1 scalarExpr
926-
<**> (((option [] orderBy) <* closeParen)
927+
<**> ((option [] orderBy <* closeParen)
927928
<**> (optional afilter <$$$$$> AggregateApp)))
928929
-- separate cases with no all or distinct which must have at
929930
-- least one scalar expr
@@ -967,17 +968,17 @@ window :: Parser ([ScalarExpr] -> [Name] -> ScalarExpr)
967968
window =
968969
keyword_ "over" *> openParen *> option [] partitionBy
969970
<**> (option [] orderBy
970-
<**> (((optional frameClause) <* closeParen) <$$$$$> WindowApp))
971+
<**> ((optional frameClause <* closeParen) <$$$$$> WindowApp))
971972
where
972973
partitionBy = keywords_ ["partition","by"] *> commaSep1 scalarExpr
973974
frameClause =
974975
frameRowsRange -- TODO: this 'and' could be an issue
975-
<**> (choice [(keyword_ "between" *> frameLimit True)
976+
<**> choice [(keyword_ "between" *> frameLimit True)
976977
<**> ((keyword_ "and" *> frameLimit True)
977978
<$$$> FrameBetween)
978979
-- maybe this should still use a b expression
979980
-- for consistency
980-
,frameLimit False <**> pure (flip FrameFrom)])
981+
,frameLimit False <**> pure (flip FrameFrom)]
981982
frameRowsRange = FrameRows <$ keyword_ "rows"
982983
<|> FrameRange <$ keyword_ "range"
983984
frameLimit useB =
@@ -1013,7 +1014,7 @@ inSuffix =
10131014
where
10141015
inty = choice [True <$ keyword_ "in"
10151016
,False <$ keywords_ ["not","in"]]
1016-
mkIn i v = \e -> In i e v
1017+
mkIn i v e = In i e v
10171018

10181019
{-
10191020
=== between
@@ -1034,14 +1035,15 @@ and operator. This is the call to scalarExprB.
10341035

10351036
betweenSuffix :: Parser (ScalarExpr -> ScalarExpr)
10361037
betweenSuffix =
1037-
makeOp <$> Name Nothing <$> opName
1038+
makeOp . Name Nothing
1039+
<$> opName
10381040
<*> scalarExprB
10391041
<*> (keyword_ "and" *> scalarExprB)
10401042
where
10411043
opName = choice
10421044
["between" <$ keyword_ "between"
10431045
,"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]
10451047

10461048
{-
10471049
=== quantified comparison
@@ -1224,7 +1226,7 @@ opTable bExpr =
12241226

12251227
,[prefixKeyword "not"]
12261228

1227-
,if bExpr then [] else [binaryKeywordL "and"]
1229+
,[binaryKeywordL "and" | not bExpr]
12281230

12291231
,[binaryKeywordL "or"]
12301232

@@ -1368,7 +1370,7 @@ from = keyword_ "from" *> commaSep1 tref
13681370
where
13691371
-- TODO: use P (a->) for the join tref suffix
13701372
-- chainl or buildexpressionparser
1371-
tref = (nonJoinTref <?> "table ref") >>= optionSuffix (joinTrefSuffix)
1373+
tref = (nonJoinTref <?> "table ref") >>= optionSuffix joinTrefSuffix
13721374
nonJoinTref = choice
13731375
[parens $ choice
13741376
[TRQueryExpr <$> queryExpr
@@ -1519,7 +1521,7 @@ queryExpr = E.makeExprParser qeterm qeOpTable
15191521
mkSelect
15201522
<$> option SQDefault duplicates
15211523
<*> selectList
1522-
<*> (optional tableExpression) <?> "table expression"
1524+
<*> optional tableExpression <?> "table expression"
15231525
mkSelect d sl Nothing =
15241526
toQueryExpr $ makeSelect {msSetQuantifier = d, msSelectList = sl}
15251527
mkSelect d sl (Just (TableExpression f w g h od ofs fe)) =
@@ -1621,15 +1623,15 @@ statementWithoutSemicolon = choice
16211623
]
16221624

16231625
statement :: Parser Statement
1624-
statement = statementWithoutSemicolon <* optional semi <|> semi *> pure EmptyStatement
1626+
statement = statementWithoutSemicolon <* optional semi <|> EmptyStatement <$ semi
16251627

16261628
createSchema :: Parser Statement
16271629
createSchema = keyword_ "schema" >>
16281630
CreateSchema <$> names
16291631

16301632
createTable :: Parser Statement
16311633
createTable = do
1632-
d <- queryDialect id
1634+
d <- askDialect id
16331635
let
16341636
parseColumnDef = TableColumnDef <$> columnDef
16351637
parseConstraintDef = uncurry TableConstraintDef <$> tableConstraintDef
@@ -1675,7 +1677,7 @@ columnDef = ColumnDef <$> name <*> typeName
16751677
tableConstraintDef :: Parser (Maybe [Name], TableConstraint)
16761678
tableConstraintDef =
16771679
(,)
1678-
<$> (optional (keyword_ "constraint" *> names))
1680+
<$> optional (keyword_ "constraint" *> names)
16791681
<*> (unique <|> primaryKey <|> check <|> references)
16801682
where
16811683
unique = keyword_ "unique" >>
@@ -1725,7 +1727,7 @@ colConstraintDef =
17251727
unique = ColUniqueConstraint <$ keyword_ "unique"
17261728
primaryKey = do
17271729
keywords_ ["primary", "key"]
1728-
d <- queryDialect id
1730+
d <- askDialect id
17291731
autoincrement <- if diAutoincrement d
17301732
then optional (keyword_ "autoincrement")
17311733
else pure Nothing
@@ -2099,9 +2101,9 @@ makeKeywordTree sets =
20992101
parseGroup :: [[Text]] -> Parser [Text]
21002102
parseGroup l@((k:_):_) = do
21012103
keyword_ k
2102-
let tls = catMaybes $ map safeTail l
2104+
let tls = mapMaybe safeTail l
21032105
pr = (k:) <$> parseTrees tls
2104-
if (or $ map null tls)
2106+
if any null tls
21052107
then pr <|> pure [k]
21062108
else pr
21072109
parseGroup _ = guard False >> fail "impossible"
@@ -2175,7 +2177,7 @@ unsignedInteger = read . T.unpack <$> sqlNumberTok True <?> "natural number"
21752177
-- todo: work out the symbol parsing better
21762178

21772179
symbol :: Text -> Parser Text
2178-
symbol s = symbolTok (Just s) <?> (T.unpack s)
2180+
symbol s = symbolTok (Just s) <?> T.unpack s
21792181

21802182
singleCharSymbol :: Char -> Parser Char
21812183
singleCharSymbol c = c <$ symbol (T.singleton c)
@@ -2205,12 +2207,12 @@ semi = singleCharSymbol ';' <?> ""
22052207
-- = helper functions
22062208

22072209
keyword :: Text -> Parser Text
2208-
keyword k = unquotedIdentifierTok [] (Just k) <?> (T.unpack k)
2210+
keyword k = unquotedIdentifierTok [] (Just k) <?> T.unpack k
22092211

22102212
-- helper function to improve error messages
22112213

22122214
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)
22142216

22152217

22162218
parens :: Parser a -> Parser a
@@ -2270,7 +2272,7 @@ stringTokExtend = do
22702272
guard (s == "'" && e == "'")
22712273
(s',e',y) <- stringTokExtend
22722274
guard (s' == "'" && e' == "'")
2273-
pure $ (s,e,x <> y)
2275+
pure (s,e,x <> y)
22742276
,pure (s,e,x)
22752277
]
22762278

@@ -2361,12 +2363,8 @@ unquotedIdentifierTok blackList kw = token test Set.empty <?> ""
23612363
-- dialect
23622364

23632365
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
23722370

Language/SQL/SimpleSQL/Pretty.hs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-- source from ASTs. The code attempts to format the output in a
44
-- readable way.
55
{-# LANGUAGE OverloadedStrings #-}
6+
{-# LANGUAGE LambdaCase #-}
67
module Language.SQL.SimpleSQL.Pretty
78
(prettyQueryExpr
89
,prettyScalarExpr
@@ -120,7 +121,7 @@ scalarExpr d (WindowApp f es pb od fr) =
120121
<+> pretty "over"
121122
<+> parens ((case pb of
122123
[] -> mempty
123-
_ -> (pretty "partition by") <+> align
124+
_ -> pretty "partition by" <+> align
124125
(commaSep $ map (scalarExpr d) pb))
125126
<+> orderBy d od
126127
<+> me frd fr)
@@ -142,7 +143,7 @@ scalarExpr dia (SpecialOp nm [a,b,c])
142143
| nm `elem` [[Name Nothing "between"]
143144
,[Name Nothing "not between"]] =
144145
sep [scalarExpr dia a
145-
,names nm <+> nest ((T.length (unnames nm) - 3)) (sep
146+
,names nm <+> nest (T.length (unnames nm) - 3) (sep
146147
[scalarExpr dia b
147148
,pretty "and" <+> scalarExpr dia c])]
148149

@@ -206,10 +207,10 @@ scalarExpr d (SubQueryExpr ty qe) =
206207
scalarExpr d (QuantifiedComparison v c cp sq) =
207208
scalarExpr d v
208209
<+> names c
209-
<+> (pretty $ case cp of
210-
CPAny -> "any"
211-
CPSome -> "some"
212-
CPAll -> "all")
210+
<+> pretty (case cp of
211+
CPAny -> "any"
212+
CPSome -> "some"
213+
CPAll -> "all")
213214
<+> parens (queryExpr d sq)
214215

215216
scalarExpr d (Match v u sq) =
@@ -306,13 +307,13 @@ typeName (PrecScaleTypeName t a b) =
306307
typeName (PrecLengthTypeName t i m u) =
307308
names t
308309
<> parens (pretty (show i)
309-
<> me (\x -> case x of
310+
<> me (\case
310311
PrecK -> pretty "K"
311312
PrecM -> pretty "M"
312313
PrecG -> pretty "G"
313314
PrecT -> pretty "T"
314315
PrecP -> pretty "P") m
315-
<+> me (\x -> case x of
316+
<+> me (\case
316317
PrecCharacters -> pretty "CHARACTERS"
317318
PrecOctets -> pretty "OCTETS") u)
318319
typeName (CharTypeName t i cs col) =
@@ -350,7 +351,7 @@ intervalTypeField (Itf n p) =
350351
pretty n
351352
<+> me (\(x,x1) ->
352353
parens (pretty (show x)
353-
<+> me (\y -> (sep [comma,pretty (show y)])) x1)) p
354+
<+> me (\y -> sep [comma,pretty (show y)]) x1)) p
354355

355356

356357
-- = query expressions
@@ -524,7 +525,7 @@ statement d (AlterDomain nm act) =
524525
<+> a act
525526
where
526527
a (ADSetDefault v) = texts ["set","default"] <+> scalarExpr d v
527-
a (ADDropDefault) = texts ["drop","default"]
528+
a ADDropDefault = texts ["drop","default"]
528529
a (ADAddConstraint cnm e) =
529530
pretty "add"
530531
<+> maybe mempty (\cnm' -> pretty "constraint" <+> names cnm') cnm
@@ -603,7 +604,7 @@ statement _ (DropTable n b) =
603604
statement d (CreateView r nm al q co) =
604605
pretty "create" <+> (if r then pretty "recursive" else mempty)
605606
<+> pretty "view" <+> names nm
606-
<+> (maybe mempty (\al' -> parens $ commaSep $ map name al')) al
607+
<+> maybe mempty (parens . commaSep . map name) al
607608
<+> pretty "as"
608609
<+> queryExpr d q
609610
<+> case co of
@@ -731,7 +732,7 @@ columnDef d (ColumnDef n t mdef cons) =
731732
pcon (ColReferencesConstraint tb c m u del) =
732733
pretty "references"
733734
<+> names tb
734-
<+> maybe mempty (\c' -> parens (name c')) c
735+
<+> maybe mempty (parens . name) c
735736
<+> refMatch m
736737
<+> refAct "update" u
737738
<+> refAct "delete" del

0 commit comments

Comments
 (0)