import Char (isSpace) import List (isPrefixOf) import System (getArgs) main = do args <- getArgs countFile args countFile [] = putStrLn "" countFile (s:xs) = do cs <- readFile s putStrLn $ s ++ " " ++ (show . countSrcLine) cs rest <- countFile(xs) return rest countSrcLine :: String -> Int countSrcLine = length . lines . removeAllComment isSpaceLine :: String -> Bool isSpaceLine = all isSpace removeBlank :: String -> String removeBlank = dropWhile isSpace isSingleComment :: String -> Bool isSingleComment = (isPrefixOf "--") . removeBlank removeAllComment :: String -> String removeAllComment = unlines . filter isSrcLine . lines . removeMultiComment where isSrcLine = (\x -> (not . isSpaceLine) x && (not . isSingleComment) x) removeMultiComment :: String -> String removeMultiComment str = result where (i, isStr, (result, rest)) = removeBaseMultiComment (0, False, ("", str)) nskip :: Int -> ([a], [a]) -> ([a], [a]) nskip n (dest, src) = (dest, drop n src) nmove :: Int -> ([a], [a]) -> ([a], [a]) nmove n (dest, src) = (dest ++ take n src, drop n src) removeBaseMultiComment :: (Int, Bool, (String, String)) -> (Int, Bool, (String, String)) removeBaseMultiComment (n, isStr, (result, "")) = (n, isStr, (result, "")) removeBaseMultiComment (n, isStr, (result, rest)) | (isPrefixOf "\\\"" rest) = if (n == 0) then removeBaseMultiComment(n, isStr, nmove 2 (result, rest)) else removeBaseMultiComment(n, isStr, nskip 2 (result, rest)) | (isPrefixOf "\"" rest) = if (n == 0) then removeBaseMultiComment (n, not isStr, nmove 1 (result, rest)) else removeBaseMultiComment (n, isStr, nskip 1 (result, rest)) | (isPrefixOf "{-" rest) = if (isStr) then removeBaseMultiComment (n, isStr, nmove 2 (result, rest)) else removeBaseMultiComment (n + 1, isStr, nskip 2(result, rest)) | (isPrefixOf "-}" rest) = if (isStr) then removeBaseMultiComment (n, isStr, nmove 2 (result, rest)) else removeBaseMultiComment (n - 1, isStr, nskip 2(result, rest)) | otherwise = if (n == 0) then removeBaseMultiComment (n, isStr, nmove 1 (result, rest)) else removeBaseMultiComment (n, isStr, nskip 1 (result, rest))