on 7552469 help sign in
elk/src/AST/Module.hs
161 lines 3.7 kB history raw
1module AST.Module
2 ( Interfaces
3 , Types, Aliases, ADTs
4 , AdtInfo, CanonicalAdt
5 , SourceModule, ValidModule, CanonicalModule
6 , Module(..), CanonicalBody(..)
7 , Header(..)
8 , Name, nameToString, nameIsNative
9 , Interface(..), toInterface
10 , UserImport, DefaultImport, ImportMethod(..)
11 ) where
13import Control.Applicative ((<$>),(<*>))
14import Data.Binary
15import qualified Data.List as List
16import qualified Data.Map as Map
18import qualified AST.Declaration as Decl
19import qualified AST.Expression.Canonical as Canonical
20import qualified AST.Type as Type
21import qualified AST.Variable as Var
22import qualified Docs.AST as Docs
23import qualified Elm.Compiler.Version as Compiler
24import qualified Reporting.Annotation as A
27-- HELPFUL TYPE ALIASES
29type Interfaces = Map.Map Name Interface
31type Types = Map.Map String Type.Canonical
32type Aliases = Map.Map String ([String], Type.Canonical)
33type ADTs = Map.Map String (AdtInfo String)
35type AdtInfo v = ( [String], [(v, [Type.Canonical])] )
36type CanonicalAdt = (Var.Canonical, AdtInfo Var.Canonical)
39-- MODULES
41type SourceModule =
42 Module
43 String
44 [UserImport]
45 (Var.Listing (A.Located Var.Value))
46 [Decl.SourceDecl]
49type ValidModule =
50 Module
51 String
52 ([DefaultImport], [UserImport])
53 (Var.Listing (A.Located Var.Value))
54 [Decl.ValidDecl]
57type CanonicalModule =
58 Module Docs.Centralized [Name] [Var.Value] CanonicalBody
61data Module docs imports exports body = Module
62 { names :: Name
63 , path :: FilePath
64 , docs :: A.Located (Maybe docs)
65 , exports :: exports
66 , imports :: imports
67 , body :: body
68 }
71data CanonicalBody = CanonicalBody
72 { program :: Canonical.Expr
73 , types :: Types
74 , fixities :: [(Decl.Assoc, Int, String)]
75 , aliases :: Aliases
76 , datatypes :: ADTs
77 , ports :: [String]
78 }
81-- HEADERS
83{-| Basic info needed to identify modules and determine dependencies. -}
84data Header imports = Header
85 { _names :: Name
86 , _docs :: A.Located (Maybe String)
87 , _exports :: Var.Listing (A.Located Var.Value)
88 , _imports :: imports
89 }
92type Name = [String] -- must be non-empty
95nameToString :: Name -> String
96nameToString =
97 List.intercalate "."
100nameIsNative :: Name -> Bool
101nameIsNative name =
102 case name of
103 "Native" : _ -> True
104 _ -> False
107-- IMPORTs
109type UserImport = A.Located (Name, ImportMethod)
112type DefaultImport = (Name, ImportMethod)
115data ImportMethod = ImportMethod
116 { alias :: Maybe String
117 , exposedVars :: !(Var.Listing Var.Value)
118 }
121-- INTERFACES
123{-| Key facts about a module, used when reading info from .elmi files. -}
124data Interface = Interface
125 { iVersion :: String
126 , iExports :: [Var.Value]
127 , iTypes :: Types
128 , iImports :: [Name]
129 , iAdts :: ADTs
130 , iAliases :: Aliases
131 , iFixities :: [(Decl.Assoc, Int, String)]
132 , iPorts :: [String]
133 }
136toInterface :: CanonicalModule -> Interface
137toInterface modul =
138 let body' = body modul in
139 Interface
140 { iVersion = Compiler.version
141 , iExports = exports modul
142 , iTypes = types body'
143 , iImports = imports modul
144 , iAdts = datatypes body'
145 , iAliases = aliases body'
146 , iFixities = fixities body'
147 , iPorts = ports body'
148 }
151instance Binary Interface where
152 get = Interface <$> get <*> get <*> get <*> get <*> get <*> get <*> get <*> get
153 put modul = do
154 put (iVersion modul)
155 put (iExports modul)
156 put (iTypes modul)
157 put (iImports modul)
158 put (iAdts modul)
159 put (iAliases modul)
160 put (iFixities modul)
161 put (iPorts modul)