git.iko.soy/rowtype-yoga/purescript-native
on b21773b help sign in
444 lines 12 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.Types
20import Language.PureScript.Names
21import Language.PureScript.Kinds
22import Language.PureScript.TypeClassDictionaries
23import Language.PureScript.CodeGen.JS.AST
24import Language.PureScript.Environment
26import qualified Data.Data as D
27import Data.Generics (mkQ, everything)
29-- |
30-- A precedence level for an infix operator
31--
32type Precedence = Integer
34-- |
35-- Associativity for infix operators
36--
37data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
39instance Show Associativity where
40 show Infixl = "infixl"
41 show Infixr = "infixr"
42 show Infix = "infix"
44-- |
45-- Source position information
46--
47data SourcePos = SourcePos
48 { -- |
49 -- Source name
50 --
51 sourceName :: String
52 -- |
53 -- Line number
54 --
55 , sourcePosLine :: Int
56 -- |
57 -- Column number
58 --
59 , sourcePosColumn :: Int
60 } deriving (D.Data, D.Typeable)
62instance Show SourcePos where
63 show sp = (sourceName sp) ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
65-- |
66-- Fixity data for infix operators
67--
68data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
70-- |
71-- A module declaration, consisting of a module name, a list of declarations, and a list of the
72-- declarations that are explicitly exported. If the export list is Nothing, everything is exported.
73--
74data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
76-- |
77-- An item in a list of explicit imports or exports
78--
79data DeclarationRef
80 -- |
81 -- A type constructor with data constructors
82 --
83 = TypeRef ProperName (Maybe [ProperName])
84 -- |
85 -- A value
86 --
87 | ValueRef Ident
88 -- |
89 -- A type class
90 --
91 | TypeClassRef ProperName
92 -- |
93 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
94 --
95 | TypeInstanceRef Ident
96 -- |
97 -- A declaration reference with source position information
98 --
99 | PositionedDeclarationRef SourcePos DeclarationRef
100 deriving (Show, D.Data, D.Typeable)
102instance Eq DeclarationRef where
103 (TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
104 (ValueRef name) == (ValueRef name') = name == name'
105 (TypeClassRef name) == (TypeClassRef name') = name == name'
106 (TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
107 (PositionedDeclarationRef _ r) == r' = r == r'
108 r == (PositionedDeclarationRef _ r') = r == r'
109 _ == _ = False
111-- |
112-- The data type of declarations
113--
114data Declaration
115 -- |
116 -- A data type declaration (name, arguments, data constructors)
117 --
118 = DataDeclaration ProperName [String] [(ProperName, [Type])]
119 -- |
120 -- A minimal mutually recursive set of data type declarations
121 --
122 | DataBindingGroupDeclaration [Declaration]
123 -- |
124 -- A type synonym declaration (name, arguments, type)
125 --
126 | TypeSynonymDeclaration ProperName [String] Type
127 -- |
128 -- A type declaration for a value (name, ty)
129 --
130 | TypeDeclaration Ident Type
131 -- |
132 -- A value declaration (name, top-level binders, optional guard, value)
133 --
134 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
135 -- |
136 -- A minimal mutually recursive set of value declarations
137 --
138 | BindingGroupDeclaration [(Ident, NameKind, Value)]
139 -- |
140 -- A foreign import declaration (type, name, optional inline Javascript, type)
141 --
142 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
143 -- |
144 -- A data type foreign import (name, kind)
145 --
146 | ExternDataDeclaration ProperName Kind
147 -- |
148 -- A type class instance foreign import
149 --
150 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
151 -- |
152 -- A fixity declaration (fixity data, operator name)
153 --
154 | FixityDeclaration Fixity String
155 -- |
156 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
157 -- name)
158 --
159 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
160 -- |
161 -- A type class declaration (name, argument, member declarations)
162 --
163 | TypeClassDeclaration ProperName [String] [Declaration]
164 -- |
165 -- A type instance declaration (name, dependencies, class name, instance types, member
166 -- declarations)
167 --
168 | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
169 -- |
170 -- A declaration with source position information
171 --
172 | PositionedDeclaration SourcePos Declaration
173 deriving (Show, D.Data, D.Typeable)
175-- |
176-- Test if a declaration is a value declaration
177--
178isValueDecl :: Declaration -> Bool
179isValueDecl ValueDeclaration{} = True
180isValueDecl (PositionedDeclaration _ d) = isValueDecl d
181isValueDecl _ = False
183-- |
184-- Test if a declaration is a data type or type synonym declaration
185--
186isDataDecl :: Declaration -> Bool
187isDataDecl DataDeclaration{} = True
188isDataDecl TypeSynonymDeclaration{} = True
189isDataDecl (PositionedDeclaration _ d) = isDataDecl d
190isDataDecl _ = False
192-- |
193-- Test if a declaration is a module import
194--
195isImportDecl :: Declaration -> Bool
196isImportDecl ImportDeclaration{} = True
197isImportDecl (PositionedDeclaration _ d) = isImportDecl d
198isImportDecl _ = False
200-- |
201-- Test if a declaration is a data type foreign import
202--
203isExternDataDecl :: Declaration -> Bool
204isExternDataDecl ExternDataDeclaration{} = True
205isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
206isExternDataDecl _ = False
208-- |
209-- Test if a declaration is a type class instance foreign import
210--
211isExternInstanceDecl :: Declaration -> Bool
212isExternInstanceDecl ExternInstanceDeclaration{} = True
213isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
214isExternInstanceDecl _ = False
216-- |
217-- Test if a declaration is a fixity declaration
218--
219isFixityDecl :: Declaration -> Bool
220isFixityDecl FixityDeclaration{} = True
221isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
222isFixityDecl _ = False
224-- |
225-- Test if a declaration is a foreign import
226--
227isExternDecl :: Declaration -> Bool
228isExternDecl ExternDeclaration{} = True
229isExternDecl (PositionedDeclaration _ d) = isExternDecl d
230isExternDecl _ = False
232-- |
233-- Test if a declaration is a type class or instance declaration
234--
235isTypeClassDeclaration :: Declaration -> Bool
236isTypeClassDeclaration TypeClassDeclaration{} = True
237isTypeClassDeclaration TypeInstanceDeclaration{} = True
238isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
239isTypeClassDeclaration _ = False
241-- |
242-- A guard is just a boolean-valued expression that appears alongside a set of binders
243--
244type Guard = Value
246-- |
247-- Data type for values
248--
249data Value
250 -- |
251 -- A numeric literal
252 --
253 = NumericLiteral (Either Integer Double)
254 -- |
255 -- A string literal
256 --
257 | StringLiteral String
258 -- |
259 -- A boolean literal
260 --
261 | BooleanLiteral Bool
262 -- |
263 -- A prefix -, will be desugared
264 --
265 | UnaryMinus Value
266 -- |
267 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
268 -- will be removed.
269 --
270 | BinaryNoParens (Qualified Ident) Value Value
271 -- |
272 -- Explicit parentheses. During the rebracketing phase of desugaring, this data constructor
273 -- will be removed.
274 --
275 | Parens Value
276 -- |
277 -- An array literal
278 --
279 | ArrayLiteral [Value]
280 -- |
281 -- An object literal
282 --
283 | ObjectLiteral [(String, Value)]
284 -- |
285 -- An record property accessor expression
286 --
287 | Accessor String Value
288 -- |
289 -- Partial record update
290 --
291 | ObjectUpdate Value [(String, Value)]
292 -- |
293 -- Function introduction
294 --
295 | Abs (Either Ident Binder) Value
296 -- |
297 -- Function application
298 --
299 | App Value Value
300 -- |
301 -- Variable
302 --
303 | Var (Qualified Ident)
304 -- |
305 -- Conditional (if-then-else expression)
306 --
307 | IfThenElse Value Value Value
308 -- |
309 -- A data constructor
310 --
311 | Constructor (Qualified ProperName)
312 -- |
313 -- A case expression. During the case expansion phase of desugaring, top-level binders will get
314 -- desugared into case expressions, hence the need for guards and multiple binders per branch here.
315 --
316 | Case [Value] [CaseAlternative]
317 -- |
318 -- A value with a type annotation
319 --
320 | TypedValue Bool Value Type
321 -- |
322 -- A let binding
323 --
324 | Let [Declaration] Value
325 -- |
326 -- A do-notation block
327 --
328 | Do [DoNotationElement]
329 -- |
330 -- A placeholder for a type class dictionary to be inserted later. At the end of type checking, these
331 -- placeholders will be replaced with actual expressions representing type classes dictionaries which
332 -- can be evaluated at runtime. The constructor arguments represent (in order): the type class name and
333 -- instance type, and the type class dictionaries in scope.
334 --
335 | TypeClassDictionary (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
336 -- |
337 -- A value with source position information
338 --
339 | PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
341-- |
342-- An alternative in a case statement
343--
344data CaseAlternative = CaseAlternative
345 { -- |
346 -- A collection of binders with which to match the inputs
347 --
348 caseAlternativeBinders :: [Binder]
349 -- |
350 -- An optional guard
351 --
352 , caseAlternativeGuard :: Maybe Guard
353 -- |
354 -- The result expression
355 --
356 , caseAlternativeResult :: Value
357 } deriving (Show, D.Data, D.Typeable)
359-- |
360-- Find the original dictionary which a type class dictionary in scope refers to
361--
362canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
363canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
364canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
366-- |
367-- A statement in a do-notation block
368--
369data DoNotationElement
370 -- |
371 -- A monadic value without a binder
372 --
373 = DoNotationValue Value
374 -- |
375 -- A monadic value with a binder
376 --
377 | DoNotationBind Binder Value
378 -- |
379 -- A let statement, i.e. a pure value with a binder
380 --
381 | DoNotationLet [Declaration]
382 -- |
383 -- A do notation element with source position information
384 --
385 | PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
387-- |
388-- Data type for binders
389--
390data Binder
391 -- |
392 -- Wildcard binder
393 --
394 = NullBinder
395 -- |
396 -- A binder which matches a boolean literal
397 --
398 | BooleanBinder Bool
399 -- |
400 -- A binder which matches a string literal
401 --
402 | StringBinder String
403 -- |
404 -- A binder which matches a numeric literal
405 --
406 | NumberBinder (Either Integer Double)
407 -- |
408 -- A binder which binds an identifier
409 --
410 | VarBinder Ident
411 -- |
412 -- A binder which matches a data constructor
413 --
414 | ConstructorBinder (Qualified ProperName) [Binder]
415 -- |
416 -- A binder which matches a record and binds its properties
417 --
418 | ObjectBinder [(String, Binder)]
419 -- |
420 -- A binder which matches an array and binds its elements
421 --
422 | ArrayBinder [Binder]
423 -- |
424 -- A binder which matches an array and binds its head and tail
425 --
426 | ConsBinder Binder Binder
427 -- |
428 -- A binder which binds its input to an identifier
429 --
430 | NamedBinder Ident Binder
431 -- |
432 -- A binder with source position information
433 --
434 | PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
436-- |
437-- Collect all names introduced in binders in an expression
438--
439binderNames :: (D.Data d) => d -> [Ident]
440binderNames = everything (++) (mkQ [] go)
441 where
442 go (VarBinder ident) = [ident]
443 go (NamedBinder ident _) = [ident]
444 go _ = []