@@ -20,6 +20,8 @@
2020
2121module Language.PureScript.Externs
2222 ( ExternsFile(..)
23+ , ExternsImport(..)
24+ , ExternsFixity(..)
2325 , ExternsDeclaration(..)
2426 , moduleToExternsFile
2527 , applyExternsFileToEnvironment
@@ -55,27 +57,76 @@ data ExternsFile = ExternsFile
5557 -- ^ List of module exports
5658 , efExports :: [DeclarationRef]
5759 -- ^ List of module imports
58− , efImports :: [(ModuleName, ImportDeclarationType, Maybe ModuleName)]
60+ , efImports :: [ExternsImport]
5961 -- ^ List of operators and their fixities
60− , efFixities :: [(Associativity, Precedence, String)]
62+ , efFixities :: [ExternsFixity]
6163 -- ^ List of type and value declaration
6264 , efDeclarations :: [ExternsDeclaration]
6365 } deriving (Show, Read)
6466
67+-- | A module import in an externs file
68+data ExternsImport = ExternsImport
69+ {
70+ -- ^ The imported module
71+ eiModule :: ModuleName
72+ -- ^ The import type: regular, qualified or hiding
73+ , eiImportType :: ImportDeclarationType
74+ -- ^ The imported-as name, for qualified imports
75+ , eiImportedAs :: Maybe ModuleName
76+ } deriving (Show, Read)
77+
78+-- | A fixity declaration in an externs file
79+data ExternsFixity = ExternsFixity
80+ {
81+ -- ^ The associativity of the operator
82+ efAssociativity :: Associativity
83+ -- ^ The precedence level of the operator
84+ , efPrecedence :: Precedence
85+ -- ^ The operator symbol
86+ , efOperator :: String
87+ } deriving (Show, Read)
88+
6589-- | A type or value declaration appearing in an externs file
6690data ExternsDeclaration =
6791 -- ^ A type declaration
68− EDType ProperName Kind TypeKind
92+ EDType
93+ { edTypeName :: ProperName
94+ , edTypeKind :: Kind
95+ , edTypeDeclarationKind :: TypeKind
96+ }
6997 -- ^ A type synonym
70− | EDTypeSynonym ProperName [(String, Maybe Kind)] Type
98+ | EDTypeSynonym
99+ { edTypeSynonymName :: ProperName
100+ , edTypeSynonymArguments :: [(String, Maybe Kind)]
101+ , edTypeSynonymType :: Type
102+ }
71103 -- ^ A data construtor
72− | EDDataConstructor ProperName DataDeclType ProperName Type [Ident]
104+ | EDDataConstructor
105+ { edDataCtorName :: ProperName
106+ , edDataCtorOrigin :: DataDeclType
107+ , edDataCtorTypeCtor :: ProperName
108+ , edDataCtorType :: Type
109+ , edDataCtorFields :: [Ident]
110+ }
73111 -- ^ A value declaration
74− | EDValue Ident Type
112+ | EDValue
113+ { edValueName :: Ident
114+ , edValueType :: Type
115+ }
75116 -- ^ A type class declaration
76− | EDClass ProperName [(String, Maybe Kind)] [(Ident, Type)] [Constraint]
117+ | EDClass
118+ { edClassName :: ProperName
119+ , edClassTypeArguments :: [(String, Maybe Kind)]
120+ , edClassMembers :: [(Ident, Type)]
121+ , edClassConstraints :: [Constraint]
122+ }
77123 -- ^ An instance declaration
78− | EDInstance (Qualified ProperName) Ident [Type] (Maybe [Constraint])
124+ | EDInstance
125+ { edInstanceClassName :: Qualified ProperName
126+ , edInstanceName :: Ident
127+ , edInstanceTypes :: [Type]
128+ , edInstanceConstraints :: Maybe [Constraint]
129+ }
79130 deriving (Show, Read)
80131
81132-- | Convert an externs file back into a module
@@ -111,8 +162,8 @@ moduleToExternsFile (Module _ _ mn ds (Just exps)) env = ExternsFile{..}
111162 efFixities = mapMaybe fixityDecl ds
112163 efDeclarations = concatMap toExternsDeclaration efExports
113164
114− fixityDecl :: Declaration -> Maybe (Associativity, Precedence, String)
115− fixityDecl (FixityDeclaration (Fixity assoc prec) op) = fmap (const (assoc, prec, op)) (find exportsOp exps)
165+ fixityDecl :: Declaration -> Maybe ExternsFixity
166+ fixityDecl (FixityDeclaration (Fixity assoc prec) op) = fmap (const (ExternsFixity assoc prec op)) (find exportsOp exps)
116167 where
117168 exportsOp :: DeclarationRef -> Bool
118169 exportsOp (PositionedDeclarationRef _ _ r) = exportsOp r
@@ -121,8 +172,8 @@ moduleToExternsFile (Module _ _ mn ds (Just exps)) env = ExternsFile{..}
121172 fixityDecl (PositionedDeclaration _ _ d) = fixityDecl d
122173 fixityDecl _ = Nothing
123174
124− importDecl :: Declaration -> Maybe (ModuleName, ImportDeclarationType, Maybe ModuleName)
125− importDecl (ImportDeclaration m mt qmn) = Just (m, mt, qmn)
175+ importDecl :: Declaration -> Maybe ExternsImport
176+ importDecl (ImportDeclaration m mt qmn) = Just (ExternsImport m mt qmn)
126177 importDecl (PositionedDeclaration _ _ d) = importDecl d
127178 importDecl _ = Nothing
128179
@@ -159,5 +210,7 @@ moduleToExternsFile (Module _ _ mn ds (Just exps)) env = ExternsFile{..}
159210 ]
160211 toExternsDeclaration _ = []
161212
213+$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsImport)
214+$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsFixity)
162215$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsDeclaration)
163216$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsFile)