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