From d22e0aa4a79ddd8182c81898b790c9b9bb850937 Mon Sep 17 00:00:00 2001 From: "Joseph C. Sible" Date: Sun, 5 Apr 2020 16:38:52 -0400 Subject: [PATCH] Simplify process Note to self: This is a lot like foldr or traverse, and would be trivial to implement as such if it didn't need to peek ahead when takesArg is true. I wonder if there's a clean way to implement it in terms of one of them anyway. --- src/ShellCheck/AnalyzerLib.hs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/ShellCheck/AnalyzerLib.hs b/src/ShellCheck/AnalyzerLib.hs index a3fa29d..7c2440b 100644 --- a/src/ShellCheck/AnalyzerLib.hs +++ b/src/ShellCheck/AnalyzerLib.hs @@ -919,20 +919,15 @@ getOpts string flags = process flags flagMap = Map.fromList $ ("", False) : flagList string process [] = return [] - process [(token, flag)] = do + process ((token1, flag):rest1) = do takesArg <- Map.lookup flag flagMap - guard $ not takesArg - return [(flag, token)] - process ((token1, flag1):rest2@((token2, flag2):rest)) = do - takesArg <- Map.lookup flag1 flagMap - if takesArg - then do - guard $ null flag2 - more <- process rest - return $ (flag1, token2) : more - else do - more <- process rest2 - return $ (flag1, token1) : more + (token, rest) <- if takesArg + then case rest1 of + (token2, ""):rest2 -> return (token2, rest2) + _ -> fail "takesArg without valid arg" + else return (token1, rest1) + more <- process rest + return $ (flag, token) : more supportsArrays Bash = True supportsArrays Ksh = True