Initial implementation of bindist comparison tool
[ghc-hetmet.git] / distrib / compare / Utils.hs
diff --git a/distrib/compare/Utils.hs b/distrib/compare/Utils.hs
new file mode 100644 (file)
index 0000000..58298c1
--- /dev/null
@@ -0,0 +1,28 @@
+
+module Utils where
+
+import System.Exit
+import System.IO
+import Text.Regex.Posix
+
+die :: Errors -> IO a
+die errs = do mapM_ (hPutStrLn stderr) errs
+              exitFailure
+
+dieOnErrors :: Either Errors a -> IO a
+dieOnErrors (Left errs) = die errs
+dieOnErrors (Right x) = return x
+
+type Errors = [String]
+
+maybeRead :: Read a => String -> Maybe a
+maybeRead str = case reads str of
+                [(x, "")] -> Just x
+                _ -> Nothing
+
+re :: String -> String -> Maybe [String]
+re r str = case matchM r' str :: Maybe (String, String, String, [String]) of
+           Just (_, _, _, ms) -> Just ms
+           Nothing -> Nothing
+    where r' = makeRegex r :: Regex
+