on 63243ca help sign in
elk/compiler/Metadata/Prelude.hs
59 lines 2.1 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Metadata.Prelude (interfaces, add) where
4import qualified Data.Map as Map
5import qualified Control.Exception as E
6import qualified Paths_Elm as Path
7import System.Exit
8import System.IO
9import SourceSyntax.Module
10import qualified Build.Interface as Interface
12add :: Bool -> Module def -> Module def
13add noPrelude (Module name exs ims decls) = Module name exs (customIms ++ ims) decls
14 where
15 customIms = if noPrelude then [] else concatMap addModule prelude
17 addModule (n, method) = case lookup n ims of
18 Nothing -> [(n, method)]
19 Just (As _) -> [(n, method)]
20 Just _ -> []
22prelude :: [(String, ImportMethod)]
23prelude = string : text ++ map (\n -> (n, Hiding [])) modules
24 where
25 text = map ((,) "Text") [ As "Text", Hiding ["link", "color", "height"] ]
26 string = ("String", As "String")
27 modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
28 , "Graphics.Element", "Color", "Graphics.Collage", "Native.Ports" ]
30interfaces :: Bool -> IO Interfaces
31interfaces noPrelude =
32 if noPrelude
33 then return $ Map.empty
34 else safeReadDocs =<< Path.getDataFileName "interfaces.data"
36safeReadDocs :: FilePath -> IO Interfaces
37safeReadDocs name =
38 E.catch (readDocs name) $ \err -> do
39 let _ = err :: IOError
40 hPutStrLn stderr $ unlines $
41 [ "Error reading types for standard library from file " ++ name
42 , " If you are using a stable version of Elm, please report an issue at"
43 , " <http://github.com/evancz/Elm/issues> specifying version numbers for"
44 , " Elm and your OS." ]
45 exitFailure
47readDocs :: FilePath -> IO Interfaces
48readDocs filePath = do
49 interfaces <- Interface.load filePath
50 case mapM (Interface.isValid filePath) (interfaces :: [(String, ModuleInterface)]) of
51 Left err -> do
52 hPutStrLn stderr err
53 exitFailure
55 Right [] -> do
56 hPutStrLn stderr "No interfaces found in serialized Prelude!"
57 exitFailure
59 Right ifaces -> return $ Map.fromList ifaces