on 9efd173 help sign in
89 lines 2.6 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module SourceSyntax.Module where
4import Data.Binary
5import qualified Data.Map as Map
6import Control.Applicative ((<$>), (<*>))
8import SourceSyntax.Expression (LExpr)
9import SourceSyntax.Declaration
10import SourceSyntax.Type
12import qualified Elm.Internal.Version as Version
14data Module def =
15 Module [String] Exports Imports [def]
16 deriving (Show)
18type Exports = [String]
20type Imports = [(String, ImportMethod)]
21data ImportMethod = As String | Importing [String] | Hiding [String]
22 deriving (Eq, Ord, Show)
24instance Binary ImportMethod where
25 put method =
26 let put' n info = putWord8 n >> put info in
27 case method of
28 As s -> put' 0 s
29 Importing ss -> put' 1 ss
30 Hiding ss -> put' 2 ss
32 get = do tag <- getWord8
33 case tag of
34 0 -> As <$> get
35 1 -> Importing <$> get
36 2 -> Hiding <$> get
37 _ -> error "Error reading valid ImportMethod type from serialized string"
39data MetadataModule =
40 MetadataModule
41 { names :: [String]
42 , path :: FilePath
43 , exports :: [String]
44 , imports :: [(String, ImportMethod)]
45 , program :: LExpr
46 , types :: Map.Map String Type
47 , fixities :: [(Assoc, Int, String)]
48 , aliases :: [Alias]
49 , datatypes :: [ADT]
50 , ports :: [String]
51 } deriving Show
53type Interfaces = Map.Map String ModuleInterface
54type ADT = (String, [String], [(String,[Type])])
55type Alias = (String, [String], Type)
57data ModuleInterface =
58 ModuleInterface
59 { iVersion :: Version.Version
60 , iTypes :: Map.Map String Type
61 , iImports :: [(String, ImportMethod)]
62 , iAdts :: [ADT]
63 , iAliases :: [Alias]
64 , iFixities :: [(Assoc, Int, String)]
65 , iPorts :: [String]
66 } deriving Show
68metaToInterface :: MetadataModule -> ModuleInterface
69metaToInterface metaModule =
70 ModuleInterface
71 { iVersion = Version.elmVersion
72 , iTypes = types metaModule
73 , iImports = imports metaModule
74 , iAdts = datatypes metaModule
75 , iAliases = aliases metaModule
76 , iFixities = fixities metaModule
77 , iPorts = ports metaModule
78 }
80instance Binary ModuleInterface where
81 get = ModuleInterface <$> get <*> get <*> get <*> get <*> get <*> get <*> get
82 put modul = do
83 put (iVersion modul)
84 put (iTypes modul)
85 put (iImports modul)
86 put (iAdts modul)
87 put (iAliases modul)
88 put (iFixities modul)
89 put (iPorts modul)