merge up to ghc HEAD 16-Apr-2011
[ghc-hetmet.git] / distrib / compare / Utils.hs
1
2 module Utils where
3
4 import Data.Function
5 import Data.List
6 import System.Exit
7 import System.IO
8 import Text.Regex.Posix
9
10 die :: Errors -> IO a
11 die errs = do mapM_ (hPutStrLn stderr) errs
12               exitFailure
13
14 dieOnErrors :: Either Errors a -> IO a
15 dieOnErrors (Left errs) = die errs
16 dieOnErrors (Right x) = return x
17
18 type Errors = [String]
19
20 maybeRead :: Read a => String -> Maybe a
21 maybeRead str = case reads str of
22                 [(x, "")] -> Just x
23                 _ -> Nothing
24
25 re :: String -> String -> Maybe [String]
26 re r str = case matchM r' str :: Maybe (String, String, String, [String]) of
27            Just (_, _, _, ms) -> Just ms
28            Nothing -> Nothing
29     where r' = makeRegex r :: Regex
30
31 unSepList :: Eq a => a -> [a] -> [[a]]
32 unSepList x xs = case break (x ==) xs of
33                  (this, _ : xs') ->
34                      this : unSepList x xs'
35                  (this, []) ->
36                      [this]
37
38 sortByFst :: Ord a => [(a, b)] -> [(a, b)]
39 sortByFst = sortBy (compare `on` fst)
40