FIX #3153: we lost an EOF sentinel in the event of a lexical error
[ghc.git] / compiler / main / HeaderInfo.hs
1 -----------------------------------------------------------------------------
2 --
3 -- | Parsing the top of a Haskell source file to get its module name,
4 -- imports and options.
5 --
6 -- (c) Simon Marlow 2005
7 -- (c) Lemmih 2006
8 --
9 -----------------------------------------------------------------------------
10
11 module HeaderInfo ( getImports
12 , getOptionsFromFile, getOptions
13 , optionsErrorMsgs,
14 checkProcessArgsResult ) where
15
16 #include "HsVersions.h"
17
18 import RdrName
19 import HscTypes
20 import Parser ( parseHeader )
21 import Lexer
22 import FastString
23 import HsSyn ( ImportDecl(..), HsModule(..) )
24 import Module ( ModuleName, moduleName )
25 import PrelNames ( gHC_PRIM, mAIN_NAME )
26 import StringBuffer
27 import SrcLoc
28 import DynFlags
29 import ErrUtils
30 import Util
31 import Outputable
32 import Pretty ()
33 import Maybes
34 import Bag ( emptyBag, listToBag, unitBag )
35
36 import MonadUtils ( MonadIO )
37 import Exception
38 import Control.Monad
39 import System.IO
40 import System.IO.Unsafe
41 import Data.List
42
43 ------------------------------------------------------------------------------
44
45 -- | Parse the imports of a source file.
46 --
47 -- Throws a 'SourceError' if parsing fails.
48 getImports :: GhcMonad m =>
49 DynFlags
50 -> StringBuffer -- ^ Parse this.
51 -> FilePath -- ^ Filename the buffer came from. Used for
52 -- reporting parse error locations.
53 -> FilePath -- ^ The original source filename (used for locations
54 -- in the function result)
55 -> m ([Located (ImportDecl RdrName)], [Located (ImportDecl RdrName)], Located ModuleName)
56 -- ^ The source imports, normal imports, and the module name.
57 getImports dflags buf filename source_filename = do
58 let loc = mkSrcLoc (mkFastString filename) 1 0
59 case unP parseHeader (mkPState buf loc dflags) of
60 PFailed span err -> parseError span err
61 POk pst rdr_module -> do
62 let ms@(warns, errs) = getMessages pst
63 logWarnings warns
64 if errorsFound dflags ms
65 then liftIO $ throwIO $ mkSrcErr errs
66 else
67 case rdr_module of
68 L _ (HsModule mb_mod _ imps _ _ _ _) ->
69 let
70 main_loc = mkSrcLoc (mkFastString source_filename) 1 0
71 mod = mb_mod `orElse` L (srcLocSpan main_loc) mAIN_NAME
72 (src_idecls, ord_idecls) = partition (ideclSource.unLoc) imps
73 ordinary_imps = filter ((/= moduleName gHC_PRIM) . unLoc . ideclName . unLoc)
74 ord_idecls
75 -- GHC.Prim doesn't exist physically, so don't go looking for it.
76 in
77 return (src_idecls, ordinary_imps, mod)
78
79 parseError :: GhcMonad m => SrcSpan -> Message -> m a
80 parseError span err = throwOneError $ mkPlainErrMsg span err
81
82 --------------------------------------------------------------
83 -- Get options
84 --------------------------------------------------------------
85
86 -- | Parse OPTIONS and LANGUAGE pragmas of the source file.
87 --
88 -- Throws a 'SourceError' if flag parsing fails (including unsupported flags.)
89 getOptionsFromFile :: DynFlags
90 -> FilePath -- ^ Input file
91 -> IO [Located String] -- ^ Parsed options, if any.
92 getOptionsFromFile dflags filename
93 = Exception.bracket
94 (openBinaryFile filename ReadMode)
95 (hClose)
96 (\handle -> do
97 opts <- fmap getOptions' $ lazyGetToks dflags filename handle
98 seqList opts $ return opts)
99
100 blockSize :: Int
101 -- blockSize = 17 -- for testing :-)
102 blockSize = 1024
103
104 lazyGetToks :: DynFlags -> FilePath -> Handle -> IO [Located Token]
105 lazyGetToks dflags filename handle = do
106 buf <- hGetStringBufferBlock handle blockSize
107 unsafeInterleaveIO $ lazyLexBuf handle (pragState dflags buf loc) False
108 where
109 loc = mkSrcLoc (mkFastString filename) 1 0
110
111 lazyLexBuf :: Handle -> PState -> Bool -> IO [Located Token]
112 lazyLexBuf handle state eof = do
113 case unP (lexer return) state of
114 POk state' t -> do
115 -- pprTrace "lazyLexBuf" (text (show (buffer state'))) (return ())
116 if atEnd (buffer state') && not eof
117 -- if this token reached the end of the buffer, and we haven't
118 -- necessarily read up to the end of the file, then the token might
119 -- be truncated, so read some more of the file and lex it again.
120 then getMore handle state
121 else case t of
122 L _ ITeof -> return [t]
123 _other -> do rest <- lazyLexBuf handle state' eof
124 return (t : rest)
125 _ | not eof -> getMore handle state
126 | otherwise -> return [L (last_loc state) ITeof]
127 -- parser assumes an ITeof sentinel at the end
128
129 getMore :: Handle -> PState -> IO [Located Token]
130 getMore handle state = do
131 -- pprTrace "getMore" (text (show (buffer state))) (return ())
132 nextbuf <- hGetStringBufferBlock handle blockSize
133 if (len nextbuf == 0) then lazyLexBuf handle state True else do
134 newbuf <- appendStringBuffers (buffer state) nextbuf
135 unsafeInterleaveIO $ lazyLexBuf handle state{buffer=newbuf} False
136
137
138 getToks :: DynFlags -> FilePath -> StringBuffer -> [Located Token]
139 getToks dflags filename buf = lexAll (pragState dflags buf loc)
140 where
141 loc = mkSrcLoc (mkFastString filename) 1 0
142
143 lexAll state = case unP (lexer return) state of
144 POk _ t@(L _ ITeof) -> [t]
145 POk state' t -> t : lexAll state'
146 _ -> [L (last_loc state) ITeof]
147
148
149 -- | Parse OPTIONS and LANGUAGE pragmas of the source file.
150 --
151 -- Throws a 'SourceError' if flag parsing fails (including unsupported flags.)
152 getOptions :: DynFlags
153 -> StringBuffer -- ^ Input Buffer
154 -> FilePath -- ^ Source filename. Used for location info.
155 -> [Located String] -- ^ Parsed options.
156 getOptions dflags buf filename
157 = getOptions' (getToks dflags filename buf)
158
159 -- The token parser is written manually because Happy can't
160 -- return a partial result when it encounters a lexer error.
161 -- We want to extract options before the buffer is passed through
162 -- CPP, so we can't use the same trick as 'getImports'.
163 getOptions' :: [Located Token] -- Input buffer
164 -> [Located String] -- Options.
165 getOptions' toks
166 = parseToks toks
167 where
168 getToken (L _loc tok) = tok
169 getLoc (L loc _tok) = loc
170
171 parseToks (open:close:xs)
172 | IToptions_prag str <- getToken open
173 , ITclose_prag <- getToken close
174 = map (L (getLoc open)) (words str) ++
175 parseToks xs
176 parseToks (open:close:xs)
177 | ITinclude_prag str <- getToken open
178 , ITclose_prag <- getToken close
179 = map (L (getLoc open)) ["-#include",removeSpaces str] ++
180 parseToks xs
181 parseToks (open:close:xs)
182 | ITdocOptions str <- getToken open
183 , ITclose_prag <- getToken close
184 = map (L (getLoc open)) ["-haddock-opts", removeSpaces str]
185 ++ parseToks xs
186 parseToks (open:xs)
187 | ITdocOptionsOld str <- getToken open
188 = map (L (getLoc open)) ["-haddock-opts", removeSpaces str]
189 ++ parseToks xs
190 parseToks (open:xs)
191 | ITlanguage_prag <- getToken open
192 = parseLanguage xs
193 parseToks _ = []
194 parseLanguage (L loc (ITconid fs):rest)
195 = checkExtension (L loc fs) :
196 case rest of
197 (L _loc ITcomma):more -> parseLanguage more
198 (L _loc ITclose_prag):more -> parseToks more
199 (L loc _):_ -> languagePragParseError loc
200 [] -> panic "getOptions'.parseLanguage(1) went past eof token"
201 parseLanguage (tok:_)
202 = languagePragParseError (getLoc tok)
203 parseLanguage []
204 = panic "getOptions'.parseLanguage(2) went past eof token"
205
206 -----------------------------------------------------------------------------
207
208 -- | Complain about non-dynamic flags in OPTIONS pragmas.
209 --
210 -- Throws a 'SourceError' if the input list is non-empty claiming that the
211 -- input flags are unknown.
212 checkProcessArgsResult :: MonadIO m => [Located String] -> m ()
213 checkProcessArgsResult flags
214 = when (notNull flags) $
215 liftIO $ throwIO $ mkSrcErr $ listToBag $ map mkMsg flags
216 where mkMsg (L loc flag)
217 = mkPlainErrMsg loc $
218 (text "unknown flag in {-# OPTIONS #-} pragma:" <+>
219 text flag)
220
221 -----------------------------------------------------------------------------
222
223 checkExtension :: Located FastString -> Located String
224 checkExtension (L l ext)
225 -- Checks if a given extension is valid, and if so returns
226 -- its corresponding flag. Otherwise it throws an exception.
227 = let ext' = unpackFS ext in
228 if ext' `elem` supportedLanguages
229 || ext' `elem` (map ("No"++) supportedLanguages)
230 then L l ("-X"++ext')
231 else unsupportedExtnError l ext'
232
233 languagePragParseError :: SrcSpan -> a
234 languagePragParseError loc =
235 throw $ mkSrcErr $ unitBag $
236 (mkPlainErrMsg loc $
237 text "cannot parse LANGUAGE pragma: comma-separated list expected")
238
239 unsupportedExtnError :: SrcSpan -> String -> a
240 unsupportedExtnError loc unsup =
241 throw $ mkSrcErr $ unitBag $
242 mkPlainErrMsg loc $
243 text "unsupported extension: " <> text unsup
244
245
246 optionsErrorMsgs :: [String] -> [Located String] -> FilePath -> Messages
247 optionsErrorMsgs unhandled_flags flags_lines _filename
248 = (emptyBag, listToBag (map mkMsg unhandled_flags_lines))
249 where unhandled_flags_lines = [ L l f | f <- unhandled_flags,
250 L l f' <- flags_lines, f == f' ]
251 mkMsg (L flagSpan flag) =
252 ErrUtils.mkPlainErrMsg flagSpan $
253 text "unknown flag in {-# OPTIONS #-} pragma:" <+> text flag
254