-
Notifications
You must be signed in to change notification settings - Fork 25
/
Imports.hs
141 lines (126 loc) · 4.68 KB
/
Imports.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
module Main (main) where
import Control.Monad.IO.Class
import Data.List (stripPrefix)
#if __GLASGOW_HASKELL__ >= 810
import qualified GHC.Hs
import qualified GHC.Hs.ImpExp
#else
import qualified HsImpExp
import qualified HsSyn
import qualified Bag
import qualified Outputable
import System.IO (stderr)
#endif
#if __GLASGOW_HASKELL__ >= 804
#else
import Data.Semigroup
#endif
import System.Environment
import Control.Exception
import qualified DriverPipeline
import qualified DynFlags
import qualified FastString
import qualified GHC
import qualified ErrUtils
import qualified GHC.IO.Handle.Text as Handle
import qualified HscTypes
import qualified Lexer
import qualified Module
import qualified Parser
import qualified SrcLoc
import qualified StringBuffer
import qualified System.Process as Process
main :: IO ()
main = do
(fp:exts) <- getArgs >>= \case
args@(_:_) -> pure args
[] -> fail "Please provide at least one argument (got none)"
-- Read the output of @--print-libdir@ for 'runGhc'
(_,Just ho1, _, hdl) <- Process.createProcess
(Process.shell "ghc --print-libdir"){Process.std_out=Process.CreatePipe}
libdir <- filter (/= '\n') <$> Handle.hGetContents ho1
_ <- Process.waitForProcess hdl
-- Some gymnastics to make the parser happy
res <- GHC.runGhc (Just libdir)
$ do
-- We allow passing some extra extensions to be parsed by GHC.
-- Otherwise modules that have e.g. @RankNTypes@ enabled will fail to
-- parse. Note: if anybody gets rid of this: even without this it /is/
-- necessary to run getSessionFlags/setSessionFlags at least once,
-- otherwise GHC parsing fails with the following error message:
-- <command line>: unknown package: rts
dflags0 <- GHC.getSessionDynFlags
(dflags1, _leftovers, _warns) <-
DynFlags.parseDynamicFlagsCmdLine dflags0 (map (SrcLoc.mkGeneralLocated "on the commandline") exts)
_ <- GHC.setSessionDynFlags dflags1
hsc_env <- GHC.getSession
-- XXX: We need to preprocess the file so that all extensions are
-- loaded
(dflags2, fp2) <- liftIO $
#if __GLASGOW_HASKELL__ >= 808
do
eitherResult <- DriverPipeline.preprocess hsc_env fp Nothing Nothing
case eitherResult of
Left errors -> throwIO $ HscTypes.mkSrcErr errors
Right result -> return result
#else
DriverPipeline.preprocess hsc_env (fp, Nothing)
#endif
_ <- GHC.setSessionDynFlags dflags2
-- Read the file that we want to parse
str <- liftIO $ filterBOM <$> readFile fp2
runParser fp2 str Parser.parseModule >>= \case
Lexer.POk _ (SrcLoc.L _ res) -> pure res
#if __GLASGOW_HASKELL__ >= 810
Lexer.PFailed pst -> liftIO $ do
let errors = Lexer.getErrorMessages pst dflags2
ErrUtils.printBagOfErrors dflags2 errors
HscTypes.throwErrors errors
#else
#if __GLASGOW_HASKELL__ >= 804
Lexer.PFailed _ spn e -> liftIO $ do
#else
Lexer.PFailed spn e -> liftIO $ do
#endif
Handle.hPutStrLn stderr $ unlines
[ "Could not parse module: "
, fp2
, " (originally " <> fp <> ")"
, " because " <> Outputable.showSDocUnsafe e
, " src span "
, show spn
]
throwIO $ HscTypes.mkSrcErr $
Bag.unitBag $ ErrUtils.mkPlainErrMsg dflags2 spn e
#endif
-- Extract the imports from the parsed module
let imports' =
map (\(SrcLoc.L _ idecl) ->
#if __GLASGOW_HASKELL__ >= 810
let SrcLoc.L _ n = GHC.Hs.ImpExp.ideclName idecl
in Module.moduleNameString n) (GHC.Hs.hsmodImports res)
#else
let SrcLoc.L _ n = HsImpExp.ideclName idecl
in Module.moduleNameString n) (HsSyn.hsmodImports res)
#endif
-- here we pretend that @show :: [String] -> String@ outputs JSON
print imports'
-- | Filter out the Byte Order Mark to avoid the following error:
-- lexical error at character '\65279'
filterBOM :: String -> String
filterBOM = \case
[] -> []
str@(x:xs) -> case stripPrefix "\65279" str of
Just str' -> filterBOM str'
Nothing -> x : filterBOM xs
runParser :: FilePath -> String -> Lexer.P a -> GHC.Ghc (Lexer.ParseResult a)
runParser filename str parser = do
dynFlags <- DynFlags.getDynFlags
pure $ Lexer.unP parser (parseState dynFlags)
where
location = SrcLoc.mkRealSrcLoc (FastString.mkFastString filename) 1 1
buffer = StringBuffer.stringToStringBuffer str
parseState flags = Lexer.mkPState flags buffer location