elm/compiler/Metadata/Prelude.hs

60 lines
2.1 KiB
Haskell
Raw Normal View History

{-# OPTIONS_GHC -W #-}
2013-07-25 18:53:22 +00:00
module Metadata.Prelude (interfaces, add) where
import qualified Data.Map as Map
2013-07-25 18:39:23 +00:00
import qualified Control.Exception as E
import System.Exit
import System.IO
import SourceSyntax.Module
import qualified Build.Interface as Interface
import Build.Utils (getDataFile)
add :: Bool -> Module def -> Module def
add noPrelude (Module name exs ims decls) = Module name exs (customIms ++ ims) decls
2013-07-25 18:39:23 +00:00
where
customIms = if noPrelude then [] else concatMap addModule prelude
2013-07-25 18:39:23 +00:00
addModule (n, method) = case lookup n ims of
Nothing -> [(n, method)]
Just (As _) -> [(n, method)]
2013-07-25 18:39:23 +00:00
Just _ -> []
2013-07-25 18:39:23 +00:00
prelude :: [(String, ImportMethod)]
prelude = string : text ++ map (\n -> (n, Hiding [])) modules
where
text = map ((,) "Text") [ As "Text", Hiding ["link", "color", "height"] ]
string = ("String", As "String")
modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
, "Graphics.Element", "Color", "Graphics.Collage", "Native.Ports" ]
interfaces :: Bool -> IO Interfaces
interfaces noPrelude =
if noPrelude
then return Map.empty
else safeReadDocs =<< getDataFile "interfaces.data"
2013-07-25 18:39:23 +00:00
safeReadDocs :: FilePath -> IO Interfaces
safeReadDocs name =
E.catch (readDocs name) $ \err -> do
2013-08-02 08:53:40 +00:00
let _ = err :: IOError
hPutStrLn stderr $ unlines $
[ "Error reading types for standard library from file " ++ name
, " If you are using a stable version of Elm, please report an issue at"
, " <http://github.com/evancz/Elm/issues> specifying version numbers for"
, " Elm and your OS." ]
2013-07-25 18:39:23 +00:00
exitFailure
readDocs :: FilePath -> IO Interfaces
readDocs filePath = do
interfaces <- Interface.load filePath
case mapM (Interface.isValid filePath) (interfaces :: [(String, ModuleInterface)]) of
2014-01-04 10:34:09 +00:00
Left err -> do
hPutStrLn stderr err
exitFailure
Right [] -> do
hPutStrLn stderr "No interfaces found in serialized Prelude!"
exitFailure
Right ifaces -> return $ Map.fromList ifaces