GHC API for list!

This commit is contained in:
Kazu Yamamoto 2010-04-30 18:17:20 +09:00
parent c047b9523b
commit 17a97aa1dd
3 changed files with 15 additions and 45 deletions

View File

@ -25,17 +25,12 @@ usage = "ghc-mod version 0.4.0\n"
---------------------------------------------------------------- ----------------------------------------------------------------
defaultOptions :: Options defaultOptions :: Options
defaultOptions = Options { convert = toPlain defaultOptions = Options { convert = toPlain }
, ghcPkg = "ghc-pkg"
}
argspec :: [OptDescr (Options -> Options)] argspec :: [OptDescr (Options -> Options)]
argspec = [ Option "l" ["tolisp"] argspec = [ Option "l" ["tolisp"]
(NoArg (\opts -> opts { convert = toLisp })) (NoArg (\opts -> opts { convert = toLisp }))
"print as a list of Lisp" "print as a list of Lisp"
, Option "p" ["ghc-pkg"]
(ReqArg (\str opts -> opts { ghcPkg = str }) "ghc-pkg")
"ghc-pkg path"
] ]
parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String]) parseArgs :: [OptDescr (Options -> Options)] -> [String] -> (Options, [String])

52
List.hs
View File

@ -2,48 +2,24 @@ module List (listModules) where
import Control.Applicative import Control.Applicative
import Data.List import Data.List
import Exception
import GHC
import GHC.Paths (libdir)
import Packages
import Param import Param
import System.IO import UniqFM
import System.Process
---------------------------------------------------------------- ----------------------------------------------------------------
listModules :: Options -> IO String listModules :: Options -> IO String
listModules opt = convert opt . nub . sort . exposedModules <$> getDump opt listModules opt = convert opt . nub . sort <$> getModules
getDump :: Options -> IO String getModules :: IO [String]
getDump opt = do getModules = ghandle ignore $ runGhc (Just libdir) $ do
(_,hout,_,_) <- runInteractiveProcess (ghcPkg opt) ["dump"] Nothing Nothing initSession
hGetContents hout getExposedModules <$> getSessionDynFlags
exposedModules :: String -> [String]
exposedModules cs = concatMap words ms
where where
ls = unfoldLines cs initSession = getSessionDynFlags >>= setSessionDynFlags
ms = values "exposed-modules: " ls getExposedModules = map moduleNameString . concatMap exposedModules . eltsUFM . pkgIdMap . pkgState
ignore :: SomeException -> IO [String]
values :: String -> [String] -> [String] ignore _ = return []
values tag ls = value
where
value = map (drop len) fs
len = length tag
fs = filter (tag `isPrefixOf`) ls
----------------------------------------------------------------
unfoldLines :: String -> [String]
unfoldLines xs = self xs
where
splitNL = break (== '\n')
self "" = []
self s = let (l, s') = splitNL s
in case s' of
[] -> [l]
(_:' ':s'') -> cont s'' l
(_:s'') -> l : self s''
cont s a = let (l, s') = splitNL $ dropWhile (== ' ') s
a' = a ++ " " ++ l
in case s' of
[] -> [a']
(_:' ':s'') -> cont s'' a'
(_:s'') -> a' : self s''

View File

@ -2,5 +2,4 @@ module Param where
data Options = Options { data Options = Options {
convert :: [String] -> String convert :: [String] -> String
, ghcPkg :: FilePath
} }