1 {-# OPTIONS -fno-warn-incomplete-patterns #-}
2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 -----------------------------------------------------------------------------
10 -- Parsing the top of a Haskell source file to get its module name,
11 -- imports and options.
13 -- (c) Simon Marlow 2005
16 -----------------------------------------------------------------------------
18 module HeaderInfo ( getImports
19 , getOptionsFromFile, getOptions
21 checkProcessArgsResult ) where
23 #include "HsVersions.h"
26 import Parser ( parseHeader )
29 import HsSyn ( ImportDecl(..), HsModule(..) )
30 import Module ( ModuleName, moduleName )
31 import PrelNames ( gHC_PRIM, mAIN_NAME )
32 import StringBuffer ( StringBuffer(..), hGetStringBufferBlock
33 , appendStringBuffers )
41 import Bag ( emptyBag, listToBag, unitBag )
43 import MonadUtils ( MonadIO )
49 ------------------------------------------------------------------------------
51 -- | Parse the imports of a source file.
53 -- Throws a 'SourceError' if parsing fails.
54 getImports :: GhcMonad m =>
56 -> StringBuffer -- ^ Parse this.
57 -> FilePath -- ^ Filename the buffer came from. Used for
58 -- reporting parse error locations.
59 -> FilePath -- ^ The original source filename (used for locations
60 -- in the function result)
61 -> m ([Located ModuleName], [Located ModuleName], Located ModuleName)
62 -- ^ The source imports, normal imports, and the module name.
63 getImports dflags buf filename source_filename = do
64 let loc = mkSrcLoc (mkFastString filename) 1 0
65 case unP parseHeader (mkPState buf loc dflags) of
66 PFailed span err -> parseError span err
67 POk pst rdr_module -> do
68 let ms@(warns, errs) = getMessages pst
70 if errorsFound dflags ms
71 then liftIO $ throwIO $ mkSrcErr errs
74 L _ (HsModule mb_mod _ imps _ _ _ _) ->
76 main_loc = mkSrcLoc (mkFastString source_filename) 1 0
77 mod = mb_mod `orElse` L (srcLocSpan main_loc) mAIN_NAME
78 imps' = filter isHomeImp (map unLoc imps)
79 (src_idecls, ord_idecls) = partition isSourceIdecl imps'
80 source_imps = map getImpMod src_idecls
81 ordinary_imps = filter ((/= moduleName gHC_PRIM) . unLoc)
82 (map getImpMod ord_idecls)
83 -- GHC.Prim doesn't exist physically, so don't go looking for it.
85 return (source_imps, ordinary_imps, mod)
87 parseError :: GhcMonad m => SrcSpan -> Message -> m a
88 parseError span err = throwOneError $ mkPlainErrMsg span err
90 -- we aren't interested in package imports here, filter them out
91 isHomeImp :: ImportDecl name -> Bool
92 isHomeImp (ImportDecl _ (Just p) _ _ _ _) = p == fsLit "this"
93 isHomeImp (ImportDecl _ Nothing _ _ _ _) = True
95 isSourceIdecl :: ImportDecl name -> Bool
96 isSourceIdecl (ImportDecl _ _ s _ _ _) = s
98 getImpMod :: ImportDecl name -> Located ModuleName
99 getImpMod (ImportDecl located_mod _ _ _ _ _) = located_mod
101 --------------------------------------------------------------
103 --------------------------------------------------------------
106 getOptionsFromFile :: DynFlags
107 -> FilePath -- input file
108 -> IO [Located String] -- options, if any
109 getOptionsFromFile dflags filename
111 (openBinaryFile filename ReadMode)
114 do buf <- hGetStringBufferBlock handle blockSize
116 where blockSize = 1024
118 | len buf == 0 = return []
120 = case getOptions' dflags buf filename of
121 (Nothing, opts) -> return opts
122 (Just buf', opts) -> do nextBlock <- hGetStringBufferBlock handle blockSize
123 newBuf <- appendStringBuffers buf' nextBlock
124 if len newBuf == len buf
126 else do opts' <- loop handle newBuf
129 getOptions :: DynFlags -> StringBuffer -> FilePath -> [Located String]
130 getOptions dflags buf filename
131 = case getOptions' dflags buf filename of
134 -- The token parser is written manually because Happy can't
135 -- return a partial result when it encounters a lexer error.
136 -- We want to extract options before the buffer is passed through
137 -- CPP, so we can't use the same trick as 'getImports'.
138 getOptions' :: DynFlags
139 -> StringBuffer -- Input buffer
140 -> FilePath -- Source file. Used for msgs only.
141 -> ( Maybe StringBuffer -- Just => we can use more input
142 , [Located String] -- Options.
144 getOptions' dflags buf filename
145 = parseToks (lexAll (pragState dflags buf loc))
146 where loc = mkSrcLoc (mkFastString filename) 1 0
148 getToken (_buf,L _loc tok) = tok
149 getLoc (_buf,L loc _tok) = loc
150 getBuf (buf,_tok) = buf
151 combine opts (flag, opts') = (flag, opts++opts')
152 add opt (flag, opts) = (flag, opt:opts)
154 parseToks (open:close:xs)
155 | IToptions_prag str <- getToken open
156 , ITclose_prag <- getToken close
157 = map (L (getLoc open)) (words str) `combine`
159 parseToks (open:close:xs)
160 | ITinclude_prag str <- getToken open
161 , ITclose_prag <- getToken close
162 = map (L (getLoc open)) ["-#include",removeSpaces str] `combine`
164 parseToks (open:close:xs)
165 | ITdocOptions str <- getToken open
166 , ITclose_prag <- getToken close
167 = map (L (getLoc open)) ["-haddock-opts", removeSpaces str]
168 `combine` parseToks xs
170 | ITdocOptionsOld str <- getToken open
171 = map (L (getLoc open)) ["-haddock-opts", removeSpaces str]
172 `combine` parseToks xs
174 | ITlanguage_prag <- getToken open
176 -- The last token before EOF could have been truncated.
177 -- We ignore it to be on the safe side.
179 | ITeof <- getToken eof
180 = (Just (getBuf tok),[])
182 | ITeof <- getToken eof
183 = (Just (getBuf eof),[])
184 parseToks _ = (Nothing,[])
185 parseLanguage ((_buf,L loc (ITconid fs)):rest)
186 = checkExtension (L loc fs) `add`
188 (_,L _loc ITcomma):more -> parseLanguage more
189 (_,L _loc ITclose_prag):more -> parseToks more
190 (_,L loc _):_ -> languagePragParseError loc
191 parseLanguage (tok:_)
192 = languagePragParseError (getLoc tok)
193 lexToken t = return t
194 lexAll state = case unP (lexer lexToken) state of
195 POk _ t@(L _ ITeof) -> [(buffer state,t)]
196 POk state' t -> (buffer state,t):lexAll state'
197 _ -> [(buffer state,L (last_loc state) ITeof)]
199 -----------------------------------------------------------------------------
200 -- Complain about non-dynamic flags in OPTIONS pragmas
202 checkProcessArgsResult :: MonadIO m => [Located String] -> m ()
203 checkProcessArgsResult flags
204 = when (notNull flags) $
205 liftIO $ throwIO $ mkSrcErr $ listToBag $ map mkMsg flags
206 where mkMsg (L loc flag)
207 = mkPlainErrMsg loc $
208 (text "unknown flag in {-# OPTIONS #-} pragma:" <+>
211 -----------------------------------------------------------------------------
213 checkExtension :: Located FastString -> Located String
214 checkExtension (L l ext)
215 -- Checks if a given extension is valid, and if so returns
216 -- its corresponding flag. Otherwise it throws an exception.
217 = let ext' = unpackFS ext in
218 if ext' `elem` supportedLanguages
219 || ext' `elem` (map ("No"++) supportedLanguages)
220 then L l ("-X"++ext')
221 else unsupportedExtnError l ext'
223 languagePragParseError :: SrcSpan -> a
224 languagePragParseError loc =
225 throw $ mkSrcErr $ unitBag $
227 text "cannot parse LANGUAGE pragma: comma-separated list expected")
229 unsupportedExtnError :: SrcSpan -> String -> a
230 unsupportedExtnError loc unsup =
231 throw $ mkSrcErr $ unitBag $
233 text "unsupported extension: " <> text unsup
236 optionsErrorMsgs :: [String] -> [Located String] -> FilePath -> Messages
237 optionsErrorMsgs unhandled_flags flags_lines _filename
238 = (emptyBag, listToBag (map mkMsg unhandled_flags_lines))
239 where unhandled_flags_lines = [ L l f | f <- unhandled_flags,
240 L l f' <- flags_lines, f == f' ]
241 mkMsg (L flagSpan flag) =
242 ErrUtils.mkPlainErrMsg flagSpan $
243 text "unknown flag in {-# OPTIONS #-} pragma:" <+> text flag