git.fiddlerwoaroof.com
Browse code

fixing up lint warnings

fiddlerwoaroof authored on 07/02/2017 10:11:50
Showing 1 changed files
... ...
@@ -35,7 +35,7 @@
35 35
 module CssParser
36 36
   where
37 37
 
38
-import Prelude
38
+import Prelude hiding (init,fst,length)
39 39
 import Control.Monad
40 40
 import qualified Text.Parsec as NP
41 41
 import Text.ParserCombinators.Parsec
... ...
@@ -76,7 +76,7 @@ data CSSSelector =
76 76
   deriving (Show,Eq)
77 77
 
78 78
 matchUpToX :: (Eq a,Num a) => GenParser Char st Char -> a -> GenParser Char st String
79
-matchUpToX p n = scan id n
79
+matchUpToX p = scan id
80 80
   where
81 81
     scan f x = if x == 0
82 82
       then return (f [])
... ...
@@ -102,14 +102,13 @@ nonascii  = satisfy isNonAscii
102 102
 
103 103
 unicode :: GenParser Char st String
104 104
 unicode = do
105
-  char '\\'
105
+  _ <- char '\\'
106 106
   result <- match1ToX h (6 :: Integer)
107 107
   spaces
108 108
   return result
109 109
 
110 110
 escape :: GenParser Char st String
111
-escape = (liftM ('\\':) $ try unicode) <|> do
112
-  char '\\' <:> (many1 $ noneOf "\r\n\f0123456789abcdef")
111
+escape = liftM ('\\':) (try unicode) <|> char '\\' <:> many1 (noneOf "\r\n\f0123456789abcdef")
113 112
 
114 113
 nmstart :: GenParser Char st Char
115 114
 nmstart = oneOf "-" <|> letter
... ...
@@ -128,15 +127,15 @@ w :: GenParser Char st String
128 127
 w = many space_
129 128
 
130 129
 nl :: GenParser Char st String
131
-nl = (string "\n" <|> (try $ string "\r\n") <|> string "\r" <|> string "\f") >> return "\n"
130
+nl = (string "\n" <|> try (string "\r\n") <|> string "\r" <|> string "\f") >> return "\n"
132 131
 
133 132
 genBadString :: Char -> GenParser Char st String
134 133
 genBadString c = do
135
-  char c
136
-  result <- liftM (foldr (++) []) $
137
-    many $ (many1 $ noneOf (c:"\n\r\f")) <|> (char '\\' >> nl) <|> escape
138
-  char '\\'
139
-  anyChar
134
+  _ <- char c
135
+  result <- liftM concat $
136
+    many $ many1 (noneOf (c:"\n\r\f")) <|> (char '\\' >> nl) <|> escape
137
+  _ <- char '\\'
138
+  _ <- anyChar
140 139
   return result
141 140
 
142 141
 badstring :: GenParser Char st String
... ...
@@ -145,10 +144,9 @@ badstring = genBadString '"' <|> genBadString '\''
145 144
 string_ :: GenParser Char st String
146 145
 string_ = do
147 146
   a <- char '"' <|> char '\''
148
-  result <- liftM (foldr (++) []) $ many $ do
149
-    res <- (try $ char '\\' <:> nl) <|> (try escape) <|> (liftM (:[]) $ noneOf (a:"\n\r\f"))
150
-    return res
151
-  char a
147
+  result <- liftM concat $ many $
148
+    try (char '\\' <:> nl) <|> try escape <|> liftM (:[]) (noneOf (a:"\n\r\f"))
149
+  _ <- char a
152 150
   return result
153 151
 
154 152
 -- badcomment, baduri, comment undefined
... ...
@@ -159,7 +157,7 @@ ident = do
159 157
   head_ <- optionMaybe $ string "-"
160 158
   result <- nmstart
161 159
   tail_ <- many nmchar
162
-  return $ (fromMaybe "" head_) ++ (result:tail_)
160
+  return $ fromMaybe "" head_ ++ (result:tail_)
163 161
 
164 162
 --tested
165 163
 name :: GenParser Char st String
... ...
@@ -182,7 +180,7 @@ cdc = string "-->"
182 180
 
183 181
 -- TODO: figger out if / should be here
184 182
 url :: GenParser Char st String
185
-url = liftM (foldr (++) []) $ many $ many1 (oneOf "!#$%&*-~" <|> nonascii) <|> escape
183
+url = liftM concat $ many $ many1 (oneOf "!#$%&*-~" <|> nonascii) <|> escape
186 184
 
187 185
 atEnd :: forall b s u (m :: * -> *) t. (Show t, NP.Stream s m t) => NP.ParsecT s u m b -> NP.ParsecT s u m b
188 186
 atEnd p = p >>= ((eof >>) . return)
... ...
@@ -190,47 +188,46 @@ atEnd p = p >>= ((eof >>) . return)
190 188
 -- partially tested
191 189
 urlParse :: GenParser Char st URL
192 190
 urlParse = do
193
-  result <- between (string "url(" >> spaces) (char ')')
194
-                      (withSpace $ string_ <|> (many1 $ oneOf "=?.,:/" <|> alphaNum) <|> url)
195
-
191
+  result <- between (string "url(" >> spaces) (char ')') urlChars
196 192
   return $ URLFunc result
197
-
193
+  where
194
+    urlChars = withSpace $ string_ <|> many1 (oneOf "=?.,:/" <|> alphaNum) <|> url
198 195
 stylesheet :: GenParser Char st CSSDeclaration
199 196
 stylesheet = do -- TODO: allow the letters to be escaped in these strings
200 197
   chrset <- charset
201 198
   let cset  = case chrset of
202 199
               Nothing -> [];
203 200
               Just x -> [x]
204
-  many $ s <|> cdo <|> cdc
201
+  _ <- many $ s <|> cdo <|> cdc
205 202
 
206
-  imports <- liftM (foldr (++) []) $ many $ do
207
-    imports <- many1 $ import_
208
-    many ((cdo >> spaces) <|> (cdc >> spaces))
209
-    return $ imports
203
+  imports <- liftM concat $ many $ do
204
+    imports <- many1 import_
205
+    _ <- many ((cdo >> spaces) <|> (cdc >> spaces))
206
+    return imports
210 207
 
211 208
   result <- many $ ruleset  <|> media <|> page
212
-  many $ (cdo >> many s) <|> (cdc >> many s)
209
+  _ <- many $ (cdo >> many s) <|> (cdc >> many s)
213 210
 
214 211
   return $ DeclarationList $ cset ++ imports ++ result
215 212
 
216 213
 page :: GenParser Char st CSSDeclaration
217 214
 page = do
218
-  string "@page"
215
+  _ <- string "@page"
219 216
   spaces
220
-  pseudo_ <- optionMaybe pseudo_page
217
+  pseudo_ <- optionMaybe pseudoPage
221 218
 
222 219
   body <- rulebody
223 220
   return $ PageDeclaration pseudo_ $ fromList body
224 221
 
225
-pseudo_page :: GenParser Char st String
226
-pseudo_page = char ':' >> ident >>= (spaces >>) . return 
222
+pseudoPage :: GenParser Char st String
223
+pseudoPage = char ':' >> ident >>= (spaces >>) . return 
227 224
 
228 225
 
229 226
 charset :: GenParser Char st (Maybe CSSDeclaration)
230 227
 charset = optionMaybe $ do
231
-  try $ string "@charset "
228
+  _ <- try $ string "@charset "
232 229
   result <- string_
233
-  char ';'
230
+  _ <- char ';'
234 231
   return $ Charset result
235 232
 
236 233
 medium :: GenParser Char st String
... ...
@@ -241,7 +238,7 @@ mediaList = sepBy medium (char ',' >> spaces)
241 238
 
242 239
 media :: GenParser Char st CSSDeclaration
243 240
 media =  do
244
-  try $ string "@media"
241
+  _ <- try $ string "@media"
245 242
   spaces
246 243
   query <- mediaList
247 244
   body <- between (char '{') (char '}') $ do
... ...
@@ -253,12 +250,12 @@ media =  do
253 250
 
254 251
 import_ :: GenParser Char st CSSDeclaration
255 252
 import_ = do
256
-  try $ string "@import "
253
+  _ <- try $ string "@import "
257 254
   spaces
258
-  result <- (liftM URLString string_) <|> urlParse
255
+  result <- liftM URLString string_ <|> urlParse
259 256
   spaces
260
-  tail_ <- optionMaybe $ mediaList
261
-  char ';'
257
+  tail_ <- optionMaybe mediaList
258
+  _ <- char ';'
262 259
   spaces
263 260
   return $ Import $ case tail_ of
264 261
     Nothing -> result
... ...
@@ -267,7 +264,7 @@ import_ = do
267 264
 
268 265
 
269 266
 cssRule :: GenParser Char st CSSSelector
270
-cssRule = (sepBy1 selector $ char ',' >> spaces)  >>= (return . SelectorList)
267
+cssRule = liftM SelectorList (sepBy1 selector $ char ',' >> spaces)
271 268
 
272 269
 selector :: GenParser Char st CSSSelector
273 270
 selector = do
... ...
@@ -291,20 +288,21 @@ operator = oneOfWithSpace "/,"
291 288
 combinator :: GenParser Char st Char
292 289
 combinator = oneOfWithSpace "+>"
293 290
 
294
-unary_operator :: GenParser Char st Char
295
-unary_operator = oneOf "-+"
291
+unaryOperator :: GenParser Char st Char
292
+unaryOperator = oneOf "-+"
296 293
 
297 294
 -- parse an optional combinator
298 295
 parseOCombinator :: CSSSelector -> GenParser Char st CSSSelector
299 296
 parseOCombinator head_ = do
300 297
   spaces
301
-  combine <- optionMaybe $ combinator
298
+  combine <- optionMaybe combinator
302 299
   sel <- selector
303 300
   return $ case combine of
304 301
     Nothing -> Child head_ sel
305 302
     (Just x) -> case x of
306 303
       '+' -> Sibling head_ sel
307 304
       '>' -> DirectChild head_ sel
305
+      _ -> undefined
308 306
 
309 307
 -- parse a combinator
310 308
 parseCombinator :: CSSSelector -> GenParser Char st CSSSelector
... ...
@@ -314,8 +312,7 @@ parseCombinator head_ = do
314 312
   return $ case combine of
315 313
     '+' -> Sibling head_ sel
316 314
     '>' -> DirectChild head_ sel
317
-
318
-
315
+    _ -> undefined
319 316
 
320 317
 simpleSelector :: GenParser Char st CSSSelector
321 318
 simpleSelector = do
... ...
@@ -341,8 +338,7 @@ attrib = between (char '[') (char ']') $ do
341 338
   spaces
342 339
   key <- ident
343 340
   spaces
344
-  char '='
345
-  value <- ident
341
+  value <- char '=' >> ident
346 342
   spaces
347 343
   return $ Attribute key value
348 344
 
... ...
@@ -352,7 +348,7 @@ pseudo = char ':' >> liftM Pseudo selectorIdent
352 348
     selectorIdent = do
353 349
       init <- optionMaybe $ string ":"
354 350
       idnt <- ident
355
-      return $ (fromMaybe "" init) ++ idnt
351
+      return $ fromMaybe "" init ++ idnt
356 352
 
357 353
 
358 354
 {-# ANN (<++>) "HLint: ignore" #-}
... ...
@@ -374,17 +370,15 @@ rulebody :: GenParser Char st [(String, String)]
374 370
 rulebody = between (withSpace $ char '{') (withSpace $ char '}') $ do
375 371
     head_ <- declaration
376 372
     tail_ <- many $ do
377
-      char ';'
378
-      spaces
373
+      char ';' >> spaces
379 374
       optionMaybe declaration
380
-    spaces
381
-    return $ (head_:catMaybes tail_)
375
+    _ <- spaces
376
+    return (head_:catMaybes tail_)
382 377
 
383 378
 declaration :: GenParser Char st (String,String)
384 379
 declaration = do
385 380
   prop <- property
386
-  withSpace $ char ':'
387
-  value <- expr
381
+  value <- withSpace (char ':') >> expr
388 382
   optional prio
389 383
   return (prop,value)
390 384
 
... ...
@@ -405,12 +399,11 @@ expr = do
405 399
   return $ fst ++ foldr (\x y -> (' ':x++(' ':y))) [] rst
406 400
 
407 401
 term :: GenParser Char st String
408
-term = do
409
-  withSpace numerical <|> withSpace uri <|> withSpace string_ <|> withSpace ident <|> hexcolor <|> function
402
+term = withSpace numerical <|> withSpace uri <|> withSpace string_ <|> withSpace ident <|> hexcolor <|> function
410 403
 
411 404
 numerical :: GenParser Char st String
412 405
 numerical = do
413
-  unop <- optionMaybe unary_operator
406
+  unop <- optionMaybe unaryOperator
414 407
   value <- withSpace (try percentage) <|> withSpace (try length) <|> withSpace (try ems) <|> withSpace (try exs)
415 408
            <|> withSpace (try angle) <|> withSpace (try time) <|> withSpace (try freq) <|> withSpace number 
416 409
   return $ case unop of
... ...
@@ -424,7 +417,7 @@ numerical = do
424 417
     percentage = num <++> string "%"
425 418
 
426 419
     length :: GenParser Char st String
427
-    length = num <++> (foldr1 (<|>) $ map string ["px", "cm", "mm", "in", "pt", "pc", "rem", "vw", "vh"])
420
+    length = num <++> foldr1 (<|>) (map string ["px", "cm", "mm", "in", "pt", "pc", "rem", "vw", "vh"])
428 421
 
429 422
     ems  :: GenParser Char st String
430 423
     ems = num <++> string "em"
... ...
@@ -450,7 +443,7 @@ function = do
450 443
   d <- expr
451 444
   _ <- string ")"
452 445
   spaces
453
-  return $ d
446
+  return d
454 447
 
455 448
 hexcolor :: GenParser Char st String
456 449
 hexcolor = char '#' <:> many1 hexDigit
... ...
@@ -458,10 +451,8 @@ hexcolor = char '#' <:> many1 hexDigit
458 451
 uri :: GenParser Char st String
459 452
 uri = do
460 453
   a <- string "url("
461
-  w
462
-  result <- try string_ <|> url
463
-  w
464
-  d <- string ")"
454
+  result <- w >> (try string_ <|> url)
455
+  d <- w >> string ")"
465 456
   return $ a ++ result ++ d
466 457
 
467 458