ghc-mod/GHCMod.hs

81 lines
2.5 KiB
Haskell
Raw Normal View History

2010-01-06 05:38:06 +00:00
module Main where
2010-03-11 10:03:17 +00:00
import Browse
import Check
2010-01-06 05:38:06 +00:00
import Control.Exception hiding (try)
2010-03-11 10:03:17 +00:00
import List
2010-03-11 13:39:07 +00:00
import Param
2010-01-06 05:38:06 +00:00
import Prelude hiding (catch)
import System.Console.GetOpt
2010-03-11 10:03:17 +00:00
import System.Environment (getArgs)
2010-01-06 05:38:06 +00:00
----------------------------------------------------------------
usage :: String
2010-03-11 10:03:17 +00:00
usage = "ghc-mod version 0.2.0\n"
2010-01-06 05:38:06 +00:00
++ "Usage:\n"
++ "\t ghc-mod list\n"
++ "\t ghc-mod browse <module>\n"
2010-03-11 10:03:17 +00:00
++ "\t ghc-mod check <HaskellFile>\n"
2010-01-06 05:38:06 +00:00
++ "\t ghc-mod help\n"
----------------------------------------------------------------
defaultOptions :: Options
2010-03-11 13:39:07 +00:00
defaultOptions = Options { convert = toPlain
, ghc = "ghc"
, ghci = "ghci"
, ghcPkg = "ghc-pkg"
2010-03-11 15:20:02 +00:00
, outDir = "dist/flymake"
2010-01-06 05:38:06 +00:00
}
argspec :: [OptDescr (Options -> Options)]
argspec = [ Option ['l'] ["tolisp"]
2010-03-11 13:39:07 +00:00
(NoArg (\opts -> opts { convert = toLisp }))
2010-01-06 05:38:06 +00:00
"print as a list of Lisp"
2010-03-11 13:39:07 +00:00
, Option ['g'] ["ghc"]
(ReqArg (\str opts -> opts { ghc = str }) "ghc")
"GHC path"
, Option ['i'] ["ghci"]
(ReqArg (\str opts -> opts { ghci = str }) "ghci")
"ghci path"
, Option ['p'] ["ghc-pkg"]
(ReqArg (\str opts -> opts { ghcPkg = str }) "ghc-pkg")
"ghc-pkg path"
2010-03-11 15:20:02 +00:00
, Option ['o'] ["output-dir"]
(ReqArg (\str opts -> opts { outDir = str }) "dist/flymake")
"output directory"
2010-01-06 05:38:06 +00:00
]
parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String])
parseArgs spec argv
= case getOpt Permute spec argv of
(o,n,[] ) -> (foldl (flip id) defaultOptions o, n)
2010-03-11 10:03:17 +00:00
(_,_,errs) -> error $ concat errs ++ usageInfo usage argspec
2010-01-06 05:38:06 +00:00
----------------------------------------------------------------
main :: IO ()
main = flip catch handler $ do
args <- getArgs
let (opt,cmdArg) = parseArgs argspec args
2010-03-11 13:39:07 +00:00
res <- case cmdArg !! 0 of
"browse" -> browseModule opt (cmdArg !! 1)
"list" -> listModules opt
"check" -> checkSyntax opt (cmdArg !! 1)
_ -> error usage
putStr res
2010-01-06 05:38:06 +00:00
where
handler :: ErrorCall -> IO ()
2010-03-10 09:31:07 +00:00
handler _ = putStr usage
2010-01-06 05:38:06 +00:00
----------------------------------------------------------------
toLisp :: [String] -> String
toLisp ms = "(" ++ unwords quoted ++ ")\n"
where
quote x = "\"" ++ x ++ "\""
quoted = map quote ms
toPlain :: [String] -> String
toPlain = unlines