ghc-mod/src/GHCModi.hs

284 lines
8.9 KiB
Haskell
Raw Normal View History

2014-04-07 02:06:17 +00:00
{-# LANGUAGE DeriveDataTypeable #-}
2014-03-25 02:14:25 +00:00
2014-03-27 01:34:43 +00:00
-- Commands:
-- check <file>
-- find <symbol>
2014-04-19 11:48:44 +00:00
-- info <file> <expr>
-- type <file> <line> <column>
2014-03-28 04:44:44 +00:00
-- lint [hlint options] <file>
-- the format of hlint options is [String] because they may contain
2014-04-21 08:33:53 +00:00
-- spaces and also <file> may contain spaces.
2014-04-21 07:30:31 +00:00
-- boot
-- browse [<package>:]<module>
2014-04-28 00:29:24 +00:00
-- quit
2014-03-27 01:34:43 +00:00
--
-- Session separators:
-- OK -- success
-- NG -- failure
2014-03-19 01:23:47 +00:00
module Main where
2014-04-25 13:03:09 +00:00
import Config (cProjectVersion)
2014-03-25 03:28:39 +00:00
import Control.Applicative ((<$>))
2014-03-27 05:55:24 +00:00
import Control.Concurrent (forkIO, MVar, newEmptyMVar, putMVar, readMVar)
2014-04-23 05:44:05 +00:00
import Control.Exception (SomeException(..), Exception)
2014-03-27 05:55:24 +00:00
import qualified Control.Exception as E
2014-03-24 08:32:06 +00:00
import Control.Monad (when, void)
2014-03-27 06:08:07 +00:00
import CoreMonad (liftIO)
2014-04-24 08:02:50 +00:00
import Data.List (find)
2014-03-28 04:53:58 +00:00
import Data.Maybe (fromMaybe)
2014-03-25 02:14:25 +00:00
import Data.Set (Set)
import qualified Data.Set as S
2014-04-11 07:07:36 +00:00
import Data.Typeable (Typeable)
2014-04-07 02:06:17 +00:00
import Data.Version (showVersion)
import GHC (GhcMonad)
2014-03-27 05:55:24 +00:00
import qualified GHC as G
2014-03-19 01:23:47 +00:00
import Language.Haskell.GhcMod
import Language.Haskell.GhcMod.Ghc
2014-05-10 11:51:35 +00:00
import Language.Haskell.GhcMod.Monad
2014-04-07 02:06:17 +00:00
import Paths_ghc_mod
import System.Console.GetOpt
2014-04-03 07:18:35 +00:00
import System.Directory (setCurrentDirectory)
2014-04-07 02:06:17 +00:00
import System.Environment (getArgs)
2014-03-24 08:32:06 +00:00
import System.IO (hFlush,stdout)
2014-03-19 01:23:47 +00:00
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
2014-04-21 05:04:58 +00:00
type Logger = IO String
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
2014-04-25 13:03:09 +00:00
progVersion :: String
progVersion = "ghc-modi version " ++ showVersion version ++ " compiled by GHC " ++ cProjectVersion ++ "\n"
2014-04-07 02:06:17 +00:00
argspec :: [OptDescr (Options -> Options)]
argspec = [ Option "b" ["boundary"]
(ReqArg (\s opts -> opts { lineSeparator = LineSeparator s }) "sep")
"specify line separator (default is Nul string)"
2014-04-18 08:28:12 +00:00
, Option "l" ["tolisp"]
(NoArg (\opts -> opts { outputStyle = LispStyle }))
"print as a list of Lisp"
2014-04-10 13:21:30 +00:00
, Option "g" []
(ReqArg (\s opts -> opts { ghcOpts = s : ghcOpts opts }) "flag") "specify a ghc flag"
2014-04-07 02:06:17 +00:00
]
usage :: String
2014-04-25 13:03:09 +00:00
usage = progVersion
2014-04-07 02:06:17 +00:00
++ "Usage:\n"
2014-04-18 08:28:12 +00:00
++ "\t ghc-modi [-l] [-b sep] [-g flag]\n"
2014-04-25 05:09:32 +00:00
++ "\t ghc-modi version\n"
2014-04-07 02:06:17 +00:00
++ "\t ghc-modi help\n"
parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String])
parseArgs spec argv
= case getOpt Permute spec argv of
(o,n,[] ) -> (foldr id defaultOptions o, n)
2014-04-23 05:44:05 +00:00
(_,_,errs) -> E.throw (CmdArg errs)
2014-04-07 02:06:17 +00:00
----------------------------------------------------------------
data GHCModiError = CmdArg [String]
deriving (Show, Typeable)
2014-04-23 05:44:05 +00:00
instance Exception GHCModiError
2014-04-07 02:06:17 +00:00
----------------------------------------------------------------
2014-03-25 02:34:58 +00:00
-- Running two GHC monad threads disables the handling of
-- C-c since installSignalHandlers is called twice, sigh.
2014-03-19 01:23:47 +00:00
main :: IO ()
2014-04-23 05:44:05 +00:00
main = E.handle cmdHandler $
2014-04-07 02:06:17 +00:00
go =<< parseArgs argspec <$> getArgs
2014-03-19 01:23:47 +00:00
where
2014-04-23 05:44:05 +00:00
cmdHandler (CmdArg _) = putStr $ usageInfo usage argspec
go (_,"help":_) = putStr $ usageInfo usage argspec
2014-04-25 13:03:09 +00:00
go (_,"version":_) = putStr progVersion
2014-04-23 05:44:05 +00:00
go (opt,_) = E.handle someHandler $ do
2014-04-07 02:06:17 +00:00
cradle0 <- findCradle
let rootdir = cradleRootDir cradle0
-- c = cradle0 { cradleCurrentDir = rootdir } TODO: ?????
2014-04-07 02:06:17 +00:00
setCurrentDirectory rootdir
mvar <- liftIO newEmptyMVar
2014-07-17 03:25:10 +00:00
void $ forkIO $ setupDB mvar
runGhcModT opt $ loop S.empty mvar
2014-04-23 05:44:05 +00:00
where
2014-04-24 03:53:14 +00:00
-- this is just in case.
-- If an error is caught here, it is a bug of GhcMod library.
2014-04-25 02:08:29 +00:00
someHandler (SomeException e) = do
putStrLn $ "NG " ++ replace (show e)
replace :: String -> String
replace [] = []
replace ('\n':xs) = ';' : replace xs
replace (x:xs) = x : replace xs
2014-03-20 08:40:06 +00:00
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
2014-07-17 03:25:10 +00:00
setupDB :: MVar SymbolDb -> IO ()
setupDB mvar = getSymbolDb >>= putMVar mvar
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
loop :: IOish m => Set FilePath -> MVar SymbolDb -> GhcModT m ()
loop set mvar = do
2014-03-28 04:53:58 +00:00
cmdArg <- liftIO getLine
2014-03-24 08:32:06 +00:00
let (cmd,arg') = break (== ' ') cmdArg
arg = dropWhile (== ' ') arg'
2014-04-19 12:23:01 +00:00
(ret,ok,set') <- case cmd of
"check" -> checkStx set arg
2014-05-14 16:05:40 +00:00
"find" -> findSym set arg mvar
"lint" -> lintStx set arg
"info" -> showInfo set arg
"type" -> showType set arg
"split" -> doSplit set arg
"sig" -> doSig set arg
2014-05-10 11:51:35 +00:00
"boot" -> bootIt set
2014-05-10 13:10:34 +00:00
"browse" -> browseIt set arg
2014-04-25 02:08:29 +00:00
"quit" -> return ("quit", False, set)
"" -> return ("quit", False, set)
_ -> return ([], True, set)
if ok then do
liftIO $ putStr ret
liftIO $ putStrLn "OK"
else do
liftIO $ putStrLn $ "NG " ++ replace ret
2014-03-24 08:32:06 +00:00
liftIO $ hFlush stdout
when ok $ loop set' mvar
2014-03-24 08:32:06 +00:00
2014-03-25 02:14:25 +00:00
----------------------------------------------------------------
checkStx :: IOish m
=> Set FilePath
2014-03-24 08:32:06 +00:00
-> FilePath
-> GhcModT m (String, Bool, Set FilePath)
checkStx set file = do
2014-05-10 13:10:34 +00:00
set' <- toGhcMod $ newFileSet set file
2014-04-28 07:31:28 +00:00
let files = S.toList set'
2014-05-10 13:10:34 +00:00
eret <- check files
2014-04-28 04:52:28 +00:00
case eret of
Right ret -> return (ret, True, set')
Left ret -> return (ret, True, set) -- fxime: set
2014-04-26 12:46:11 +00:00
newFileSet :: GhcMonad m => Set FilePath -> FilePath -> m (Set FilePath)
2014-04-28 07:31:28 +00:00
newFileSet set file = do
let set1
| S.member file set = set
| otherwise = S.insert file set
mx <- isSameMainFile file <$> getModSummaryForMain
return $ case mx of
Nothing -> set1
Just mainfile -> S.delete mainfile set1
getModSummaryForMain :: GhcMonad m => m (Maybe G.ModSummary)
2014-04-28 07:31:28 +00:00
getModSummaryForMain = find isMain <$> G.getModuleGraph
2014-04-26 12:46:11 +00:00
where
2014-03-27 05:55:24 +00:00
isMain m = G.moduleNameString (G.moduleName (G.ms_mod m)) == "Main"
2014-04-28 07:31:28 +00:00
isSameMainFile :: FilePath -> (Maybe G.ModSummary) -> Maybe FilePath
isSameMainFile _ Nothing = Nothing
isSameMainFile file (Just x)
| mainfile == file = Nothing
| otherwise = Just mainfile
where
mmainfile = G.ml_hs_file (G.ms_location x)
-- G.ms_hspp_file x is a temporary file with CPP.
-- this is a just fake.
mainfile = fromMaybe (G.ms_hspp_file x) mmainfile
----------------------------------------------------------------
2014-03-25 02:14:25 +00:00
findSym :: IOish m => Set FilePath -> String -> MVar SymbolDb
-> GhcModT m (String, Bool, Set FilePath)
2014-05-14 16:05:40 +00:00
findSym set sym mvar = do
2014-03-25 02:14:25 +00:00
db <- liftIO $ readMVar mvar
2014-05-14 16:54:56 +00:00
opt <- options
let ret = lookupSymbol opt sym db
2014-03-25 02:14:25 +00:00
return (ret, True, set)
2014-03-27 01:34:43 +00:00
lintStx :: IOish m => Set FilePath
-> FilePath
-> GhcModT m (String, Bool, Set FilePath)
lintStx set optFile = do
ret <- local env' $ lint file
2014-04-19 12:23:01 +00:00
return (ret, True, set)
2014-03-27 01:34:43 +00:00
where
2014-04-19 12:23:01 +00:00
(opts,file) = parseLintOptions optFile
hopts = if opts == "" then [] else read opts
env' e = e { gmOptions = opt' $ gmOptions e }
opt' o = o { hlintOpts = hopts }
2014-03-28 05:41:01 +00:00
-- |
-- >>> parseLintOptions "[\"--ignore=Use camelCase\", \"--ignore=Eta reduce\"] file name"
-- (["--ignore=Use camelCase", "--ignore=Eta reduce"], "file name")
-- >>> parseLintOptions "file name"
-- ([], "file name")
parseLintOptions :: String -> (String, String)
parseLintOptions optFile = case brk (== ']') (dropWhile (/= '[') optFile) of
("","") -> ([], optFile)
2014-03-28 06:03:41 +00:00
(opt',file') -> (opt', dropWhile (== ' ') file')
2014-03-28 05:41:01 +00:00
where
brk _ [] = ([],[])
brk p (x:xs')
| p x = ([x],xs')
| otherwise = let (ys,zs) = brk p xs' in (x:ys,zs)
2014-04-11 07:07:36 +00:00
2014-04-21 07:30:31 +00:00
----------------------------------------------------------------
showInfo :: IOish m
=> Set FilePath
2014-04-11 07:07:36 +00:00
-> FilePath
-> GhcModT m (String, Bool, Set FilePath)
showInfo set fileArg = do
2014-04-11 07:07:36 +00:00
let [file, expr] = words fileArg
2014-04-28 07:31:28 +00:00
set' <- newFileSet set file
ret <- info file expr
2014-04-21 00:45:41 +00:00
return (ret, True, set')
2014-04-11 07:07:36 +00:00
showType :: IOish m
=> Set FilePath
2014-04-11 07:07:36 +00:00
-> FilePath
-> GhcModT m (String, Bool, Set FilePath)
showType set fileArg = do
2014-04-11 07:07:36 +00:00
let [file, line, column] = words fileArg
2014-04-28 07:31:28 +00:00
set' <- newFileSet set file
ret <- types file (read line) (read column)
2014-04-19 12:23:01 +00:00
return (ret, True, set')
2014-04-21 07:30:31 +00:00
doSplit :: IOish m
=> Set FilePath
-> FilePath
-> GhcModT m (String, Bool, Set FilePath)
doSplit set fileArg = do
let [file, line, column] = words fileArg
set' <- newFileSet set file
ret <- splits file (read line) (read column)
return (ret, True, set')
doSig :: IOish m
=> Set FilePath
-> FilePath
-> GhcModT m (String, Bool, Set FilePath)
doSig set fileArg = do
let [file, line, column] = words fileArg
set' <- newFileSet set file
ret <- sig file (read line) (read column)
return (ret, True, set')
2014-04-21 07:30:31 +00:00
----------------------------------------------------------------
bootIt :: IOish m
=> Set FilePath
-> GhcModT m (String, Bool, Set FilePath)
2014-05-10 11:51:35 +00:00
bootIt set = do
ret <- boot
2014-04-21 07:30:31 +00:00
return (ret, True, set)
2014-04-21 08:33:53 +00:00
browseIt :: IOish m
=> Set FilePath
2014-04-21 08:33:53 +00:00
-> ModuleString
-> GhcModT m (String, Bool, Set FilePath)
2014-05-10 13:10:34 +00:00
browseIt set mdl = do
ret <- browse mdl
2014-04-21 08:33:53 +00:00
return (ret, True, set)