Remove unnecessary monadicity from wordToPseudoGlob

This commit is contained in:
Joseph C. Sible 2020-04-05 19:29:40 -04:00
parent b6cff5ea0e
commit 322842b57e
2 changed files with 18 additions and 25 deletions

View File

@ -419,25 +419,25 @@ data PseudoGlob = PGAny | PGMany | PGChar Char
-- Turn a word into a PG pattern, replacing all unknown/runtime values with -- Turn a word into a PG pattern, replacing all unknown/runtime values with
-- PGMany. -- PGMany.
wordToPseudoGlob :: Token -> Maybe [PseudoGlob] wordToPseudoGlob :: Token -> [PseudoGlob]
wordToPseudoGlob word = wordToPseudoGlob word =
simplifyPseudoGlob . concat <$> mapM f (getWordParts word) simplifyPseudoGlob . concatMap f $ getWordParts word
where where
f x = case x of f x = case x of
T_Literal _ s -> return $ map PGChar s T_Literal _ s -> map PGChar s
T_SingleQuoted _ s -> return $ map PGChar s T_SingleQuoted _ s -> map PGChar s
T_DollarBraced {} -> return [PGMany] T_DollarBraced {} -> [PGMany]
T_DollarExpansion {} -> return [PGMany] T_DollarExpansion {} -> [PGMany]
T_Backticked {} -> return [PGMany] T_Backticked {} -> [PGMany]
T_Glob _ "?" -> return [PGAny] T_Glob _ "?" -> [PGAny]
T_Glob _ ('[':_) -> return [PGAny] T_Glob _ ('[':_) -> [PGAny]
T_Glob {} -> return [PGMany] T_Glob {} -> [PGMany]
T_Extglob {} -> return [PGMany] T_Extglob {} -> [PGMany]
_ -> return [PGMany] _ -> [PGMany]
-- Turn a word into a PG pattern, but only if we can preserve -- Turn a word into a PG pattern, but only if we can preserve
-- exact semantics. -- exact semantics.
@ -500,8 +500,7 @@ pseudoGlobIsSuperSetof = matchable
matchable (PGMany : rest) [] = matchable rest [] matchable (PGMany : rest) [] = matchable rest []
matchable _ _ = False matchable _ _ = False
wordsCanBeEqual x y = fromMaybe True $ wordsCanBeEqual x y = pseudoGlobsCanOverlap (wordToPseudoGlob x) (wordToPseudoGlob y)
liftM2 pseudoGlobsCanOverlap (wordToPseudoGlob x) (wordToPseudoGlob y)
-- Is this an expansion that can be quoted, -- Is this an expansion that can be quoted,
-- e.g. $(foo) `foo` $foo (but not {foo,})? -- e.g. $(foo) `foo` $foo (but not {foo,})?

View File

@ -3137,9 +3137,7 @@ checkUnmatchableCases params t =
if isConstant word if isConstant word
then warn (getId word) 2194 then warn (getId word) 2194
"This word is constant. Did you forget the $ on a variable?" "This word is constant. Did you forget the $ on a variable?"
else sequence_ $ do else mapM_ (check $ wordToPseudoGlob word) allpatterns
pg <- wordToPseudoGlob word
return $ mapM_ (check pg) allpatterns
let exactGlobs = tupMap wordToExactPseudoGlob breakpatterns let exactGlobs = tupMap wordToExactPseudoGlob breakpatterns
let fuzzyGlobs = tupMap wordToPseudoGlob breakpatterns let fuzzyGlobs = tupMap wordToPseudoGlob breakpatterns
@ -3152,15 +3150,13 @@ checkUnmatchableCases params t =
fst3 (x,_,_) = x fst3 (x,_,_) = x
snd3 (_,x,_) = x snd3 (_,x,_) = x
tp = tokenPositions params tp = tokenPositions params
check target candidate = sequence_ $ do check target candidate = unless (pseudoGlobsCanOverlap target $ wordToPseudoGlob candidate) $
candidateGlob <- wordToPseudoGlob candidate warn (getId candidate) 2195
guard . not $ pseudoGlobsCanOverlap target candidateGlob
return $ warn (getId candidate) 2195
"This pattern will never match the case statement's word. Double check them." "This pattern will never match the case statement's word. Double check them."
tupMap f l = map (\x -> (x, f x)) l tupMap f l = map (\x -> (x, f x)) l
checkDoms ((glob, Just x), rest) = checkDoms ((glob, Just x), rest) =
forM_ (find (\(_, p) -> x `pseudoGlobIsSuperSetof` p) valids) $ forM_ (find (\(_, p) -> x `pseudoGlobIsSuperSetof` p) rest) $
\(first,_) -> do \(first,_) -> do
warn (getId glob) 2221 $ "This pattern always overrides a later one" <> patternContext (getId first) warn (getId glob) 2221 $ "This pattern always overrides a later one" <> patternContext (getId first)
warn (getId first) 2222 $ "This pattern never matches because of a previous pattern" <> patternContext (getId glob) warn (getId first) 2222 $ "This pattern never matches because of a previous pattern" <> patternContext (getId glob)
@ -3170,8 +3166,6 @@ checkUnmatchableCases params t =
case posLine . fst <$> Map.lookup id tp of case posLine . fst <$> Map.lookup id tp of
Just l -> " on line " <> show l <> "." Just l -> " on line " <> show l <> "."
_ -> "." _ -> "."
valids = [(x,y) | (x, Just y) <- rest]
checkDoms _ = return () checkDoms _ = return ()