ghc-mod/GHCMod.hs

73 lines
2.1 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-04-30 07:27:10 +00:00
import Control.Applicative
2010-01-06 05:38:06 +00:00
import Control.Exception hiding (try)
2010-04-30 07:27:10 +00:00
import Lang
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-04-23 09:39:52 +00:00
usage = "ghc-mod version 0.4.0\n"
2010-01-06 05:38:06 +00:00
++ "Usage:\n"
++ "\t ghc-mod list\n"
2010-04-11 03:25:28 +00:00
++ "\t ghc-mod lang\n"
2010-01-06 05:38:06 +00:00
++ "\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
, ghcPkg = "ghc-pkg"
2010-01-06 05:38:06 +00:00
}
argspec :: [OptDescr (Options -> Options)]
2010-04-23 09:09:38 +00:00
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-04-23 09:09:38 +00:00
, Option "p" ["ghc-pkg"]
2010-03-11 13:39:07 +00:00
(ReqArg (\str opts -> opts { ghcPkg = str }) "ghc-pkg")
"ghc-pkg path"
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-04-23 09:09:38 +00:00
res <- case head cmdArg of
2010-04-30 07:27:10 +00:00
"browse" -> concat <$> mapM (browseModule opt) (tail cmdArg)
2010-03-11 13:39:07 +00:00
"list" -> listModules opt
"check" -> checkSyntax opt (cmdArg !! 1)
2010-04-02 05:40:06 +00:00
"lang" -> listLanguages opt
2010-03-11 13:39:07 +00:00
_ -> 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