git.iko.soy/rowtype-yoga/purescript-native
on 7abdae9 help sign in
208 lines 5.6 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Declarations
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- | Data types for modules and declarations
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable #-}
17module Language.PureScript.Declarations where
19import Language.PureScript.Values
20import Language.PureScript.Types
21import Language.PureScript.Names
22import Language.PureScript.Kinds
23import Language.PureScript.CodeGen.JS.AST
25import qualified Data.Data as D
27-- |
28-- A precedence level for an infix operator
29--
30type Precedence = Integer
32-- |
33-- Associativity for infix operators
34--
35data Associativity = Infixl | Infixr deriving (Show, D.Data, D.Typeable)
37-- |
38-- Fixity data for infix operators
39--
40data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
42-- |
43-- A module declaration, consisting of a module name and a list of declarations
44--
45data Module = Module ModuleName [Declaration] deriving (Show, D.Data, D.Typeable)
47-- |
48-- The type of a foreign import
49--
50data ForeignImportType
51 -- |
52 -- A regular foreign import
53 --
54 = ForeignImport
55 -- |
56 -- A foreign import which contains inline Javascript as a string literal
57 --
58 | InlineJavascript
59 -- |
60 -- A type class dictionary import, generated during desugaring of type class declarations
61 --
62 | TypeClassDictionaryImport
63 -- |
64 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
65 --
66 | TypeClassAccessorImport deriving (Show, Eq, D.Data, D.Typeable)
68-- |
69-- An item in a list of explicit imports
70--
71data ImportType
72 -- |
73 -- A type constructor import
74 --
75 = TypeImport ProperName
76 -- |
77 -- A declaration import
78 --
79 | NameImport Ident
80 deriving (Show, D.Data, D.Typeable)
82-- |
83-- The data type of declarations
84--
85data Declaration
86 -- |
87 -- A data type declaration (name, arguments, data constructors)
88 --
89 = DataDeclaration ProperName [String] [(ProperName, [Type])]
90 -- |
91 -- A minimal mutually recursive set of data type declarations
92 --
93 | DataBindingGroupDeclaration [Declaration]
94 -- |
95 -- A type synonym declaration (name, arguments, type)
96 --
97 | TypeSynonymDeclaration ProperName [String] Type
98 -- |
99 -- A type declaration for a value (name, ty)
100 --
101 | TypeDeclaration Ident Type
102 -- |
103 -- A value declaration (name, top-level binders, optional guard, value)
104 --
105 | ValueDeclaration Ident [Binder] (Maybe Guard) Value
106 -- |
107 -- A minimal mutually recursive set of value declarations
108 --
109 | BindingGroupDeclaration [(Ident, Value)]
110 -- |
111 -- A foreign import declaration (type, name, optional inline Javascript, type)
112 --
113 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
114 -- |
115 -- A data type foreign import (name, kind)
116 --
117 | ExternDataDeclaration ProperName Kind
118 -- |
119 -- A fixity declaration (fixity data, operator name)
120 --
121 | FixityDeclaration Fixity String
122 -- |
123 -- A module import (module name, optional set of identifiers to import)
124 --
125 | ImportDeclaration ModuleName (Maybe [ImportType])
126 -- |
127 -- A type class declaration (name, argument, member declarations)
128 --
129 | TypeClassDeclaration ProperName [String] [Declaration]
130 -- |
131 -- A type instance declaration (dependencies, class name, instance type, member declarations)
132 --
133 | TypeInstanceDeclaration [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
134 deriving (Show, D.Data, D.Typeable)
136-- |
137-- Test if a declaration is a value declaration
138--
139isValueDecl :: Declaration -> Bool
140isValueDecl ValueDeclaration{} = True
141isValueDecl _ = False
143-- |
144-- Test if a declaration is a data type or type synonym declaration
145--
146isDataDecl :: Declaration -> Bool
147isDataDecl DataDeclaration{} = True
148isDataDecl TypeSynonymDeclaration{} = True
149isDataDecl _ = False
151-- |
152-- Test if a declaration is a module import
153--
154isImportDecl :: Declaration -> Bool
155isImportDecl ImportDeclaration{} = True
156isImportDecl _ = False
158-- |
159-- Test if a declaration is a data type foreign import
160--
161isExternDataDecl :: Declaration -> Bool
162isExternDataDecl ExternDataDeclaration{} = True
163isExternDataDecl _ = False
165-- |
166-- Test if a declaration is a fixity declaration
167--
168isFixityDecl :: Declaration -> Bool
169isFixityDecl FixityDeclaration{} = True
170isFixityDecl _ = False
172-- |
173-- Test if a declaration is a foreign import
174--
175isExternDecl :: Declaration -> Bool
176isExternDecl ExternDeclaration{} = True
177isExternDecl _ = False
179-- |
180-- Test if a declaration is a type class or instance declaration
181--
182isTypeClassDeclaration :: Declaration -> Bool
183isTypeClassDeclaration TypeClassDeclaration{} = True
184isTypeClassDeclaration TypeInstanceDeclaration{} = True
185isTypeClassDeclaration _ = False
187-- |
188-- Extracts names from a list of explicit imports.
189--
190nameImports :: [ImportType] -> [Ident]
191nameImports ((NameImport ident):xs) = ident : nameImports xs
192nameImports (_ : xs) = nameImports xs
193nameImports _ = []
195-- |
196-- Extracts types from a list of explicit imports.
197--
198typeImports :: [ImportType] -> [ProperName]
199typeImports (TypeImport ident : xs) = ident : typeImports xs
200typeImports (_ : xs) = typeImports xs
201typeImports _ = []
203-- |
204-- Extracts classes from a list of explicit imports.
205-- note: class imports are indistinguishable from types at the moment
206--
207classImports :: [ImportType] -> [ProperName]
208classImports = typeImports