elm/compiler/Metadata/Prelude.hs

58 lines
2.2 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 qualified Paths_Elm as Path
import System.Exit
import System.IO
import SourceSyntax.Module
import qualified Build.Interface as Interface
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 = text ++ map (\n -> (n, Hiding [])) modules
where
text = map ((,) "Text") [ As "Text", Hiding ["link", "color", "height"] ]
modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
, "Graphics.Element", "Color", "Graphics.Collage" ]
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-07-25 18:39:23 +00:00
readDocs :: FilePath -> IO Interfaces
readDocs filePath = do
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
hPutStrLn stderr err
exitFailure