4f98556176b8f024f9abb2de77b1207e453f033c
[ghc.git] / utils / hpc / HpcUtils.hs
1 module HpcUtils where
2
3 import Trace.Hpc.Util
4 import qualified Data.Map as Map
5
6 dropWhileEndLE :: (a -> Bool) -> [a] -> [a]
7 -- Spec: dropWhileEndLE p = reverse . dropWhile p . reverse
8 dropWhileEndLE p = foldr (\x r -> if null r && p x then [] else x:r) []
9
10 -- turns \n into ' '
11 -- | grab's the text behind a HpcPos;
12 grabHpcPos :: Map.Map Int String -> HpcPos -> String
13 grabHpcPos hsMap srcspan =
14 case lns of
15 [ln] -> (take ((c2 - c1) + 1) $ drop (c1 - 1) ln)
16 _ -> let lns1 = drop (c1 -1) (head lns) : tail lns
17 lns2 = init lns1 ++ [take (c2 + 1) (last lns1) ]
18 in foldl1 (\ xs ys -> xs ++ "\n" ++ ys) lns2
19 where (l1,c1,l2,c2) = fromHpcPos srcspan
20 lns = map (\ n -> case Map.lookup n hsMap of
21 Just ln -> ln
22 Nothing -> error $ "bad line number : " ++ show n
23 ) [l1..l2]
24
25
26 readFileFromPath :: (String -> IO String) -> String -> [String] -> IO String
27 readFileFromPath _ filename@('/':_) _ = readFile filename
28 readFileFromPath err filename path0 = readTheFile path0
29 where
30 readTheFile [] = err $ "could not find " ++ show filename
31 ++ " in path " ++ show path0
32 readTheFile (dir:dirs) =
33 catchIO (do str <- readFile (dir ++ "/" ++ filename)
34 return str)
35 (\ _ -> readTheFile dirs)