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 qualified Paths_Elm as Path
|
|
|
|
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
|
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)]
|
2013-08-01 06:47:29 +00:00
|
|
|
prelude = 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"] ]
|
2013-08-04 19:22:58 +00:00
|
|
|
modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
|
2013-04-29 03:57:48 +00:00
|
|
|
, "Graphics.Element", "Color", "Graphics.Collage" ]
|
2013-04-04 08:09:35 +00:00
|
|
|
|
2014-01-04 08:35:26 +00:00
|
|
|
interfaces :: Bool -> IO Interfaces
|
|
|
|
interfaces noPrelude =
|
|
|
|
do ifaces <- safeReadDocs =<< Path.getDataFileName "interfaces.data"
|
|
|
|
return $ if noPrelude then Map.empty else ifaces
|
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
|
2013-07-25 18:39:23 +00:00
|
|
|
putStrLn $ unlines [ "Error reading types for standard library!"
|
2013-08-02 08:53:40 +00:00
|
|
|
, " The file should be at " ++ name
|
|
|
|
, " If you are using a stable version of Elm,"
|
|
|
|
, " please report an issue at github.com/evancz/Elm"
|
|
|
|
, " and specify your versions of Elm and your OS" ]
|
2013-07-25 18:39:23 +00:00
|
|
|
exitFailure
|
|
|
|
|
2014-01-04 10:34:09 +00:00
|
|
|
hasInterfaces :: [(String, ModuleInterface)] -> Either String [(String, ModuleInterface)]
|
|
|
|
hasInterfaces [] = Left "No interfaces found in serialized Prelude!"
|
|
|
|
hasInterfaces ifaces = Right ifaces
|
2013-10-30 22:44:47 +00:00
|
|
|
|
2013-07-25 18:39:23 +00:00
|
|
|
readDocs :: FilePath -> IO Interfaces
|
2013-10-30 22:44:47 +00:00
|
|
|
readDocs filePath = do
|
2014-01-04 14:19:30 +00:00
|
|
|
bytes <- Interface.load filePath
|
|
|
|
let interfaces = Interface.decode filePath =<< bytes
|
|
|
|
case mapM (Interface.isValid filePath) =<< hasInterfaces =<< interfaces of
|
2014-01-04 10:34:09 +00:00
|
|
|
Right ifaces -> return $ Map.fromList ifaces
|
|
|
|
Left err -> do
|
2013-10-30 22:44:47 +00:00
|
|
|
hPutStrLn stderr err
|
|
|
|
exitFailure
|