2014-01-04 09:54:46 +00:00
|
|
|
{-# OPTIONS_GHC -W #-}
|
2013-07-25 18:53:22 +00:00
|
|
|
module Metadata.Prelude (interfaces, add) where
|
2013-03-17 05:24:18 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as Map
|
2013-07-25 18:39:23 +00:00
|
|
|
import qualified Control.Exception as E
|
|
|
|
import System.Exit
|
2013-08-06 07:36:07 +00:00
|
|
|
import System.IO
|
2013-06-14 03:45:12 +00:00
|
|
|
import SourceSyntax.Module
|
2014-01-04 14:19:30 +00:00
|
|
|
import qualified Build.Interface as Interface
|
2014-02-13 08:31:14 +00:00
|
|
|
import Build.Utils (getDataFile)
|
2013-03-17 05:24:18 +00:00
|
|
|
|
2014-01-04 08:35:26 +00:00
|
|
|
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
|
2014-01-04 08:35:26 +00:00
|
|
|
customIms = if noPrelude then [] else concatMap addModule prelude
|
2013-04-04 17:37:43 +00:00
|
|
|
|
2013-07-25 18:39:23 +00:00
|
|
|
addModule (n, method) = case lookup n ims of
|
|
|
|
Nothing -> [(n, method)]
|
2014-01-04 09:54:46 +00:00
|
|
|
Just (As _) -> [(n, method)]
|
2013-07-25 18:39:23 +00:00
|
|
|
Just _ -> []
|
2013-04-04 17:37:43 +00:00
|
|
|
|
2013-07-25 18:39:23 +00:00
|
|
|
prelude :: [(String, ImportMethod)]
|
2014-03-01 18:58:02 +00:00
|
|
|
prelude = string ++ text ++ map (\n -> (n, Hiding [])) modules
|
2013-04-04 08:09:35 +00:00
|
|
|
where
|
2013-08-01 06:47:29 +00:00
|
|
|
text = map ((,) "Text") [ As "Text", Hiding ["link", "color", "height"] ]
|
2014-03-01 18:58:02 +00:00
|
|
|
string = map ((,) "String") [ As "String", Importing ["show"] ]
|
|
|
|
modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Color"
|
|
|
|
, "Graphics.Element", "Graphics.Collage", "Native.Ports" ]
|
2013-04-04 08:09:35 +00:00
|
|
|
|
2014-01-04 08:35:26 +00:00
|
|
|
interfaces :: Bool -> IO Interfaces
|
|
|
|
interfaces noPrelude =
|
2014-01-04 14:59:24 +00:00
|
|
|
if noPrelude
|
2014-02-10 22:03:13 +00:00
|
|
|
then return Map.empty
|
2014-02-13 08:31:14 +00:00
|
|
|
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
|
2014-01-04 14:59:24 +00:00
|
|
|
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
|
2013-10-30 22:44:47 +00:00
|
|
|
readDocs filePath = do
|
2014-01-04 14:59:24 +00:00
|
|
|
interfaces <- Interface.load filePath
|
|
|
|
case mapM (Interface.isValid filePath) (interfaces :: [(String, ModuleInterface)]) of
|
2014-01-04 10:34:09 +00:00
|
|
|
Left err -> do
|
2013-10-30 22:44:47 +00:00
|
|
|
hPutStrLn stderr err
|
|
|
|
exitFailure
|
2014-01-04 14:59:24 +00:00
|
|
|
|
|
|
|
Right [] -> do
|
|
|
|
hPutStrLn stderr "No interfaces found in serialized Prelude!"
|
|
|
|
exitFailure
|
|
|
|
|
|
|
|
Right ifaces -> return $ Map.fromList ifaces
|