git.iko.soy/rowtype-yoga/purescript-native
on 43463a8 help sign in
192 lines 5.4 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
24import Language.PureScript.Environment
26import qualified Data.Data as D
28-- |
29-- A precedence level for an infix operator
30--
31type Precedence = Integer
33-- |
34-- Associativity for infix operators
35--
36data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
38instance Show Associativity where
39 show Infixl = "infixl"
40 show Infixr = "infixr"
41 show Infix = "infix"
43-- |
44-- Fixity data for infix operators
45--
46data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
48-- |
49-- A module declaration, consisting of a module name, a list of declarations, and a list of the
50-- declarations that are explicitly imported. If the export list is Nothing, everything is exported.
51--
52data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
54-- |
55-- An item in a list of explicit imports or exports
56--
57data DeclarationRef
58 -- |
59 -- A type constructor with data constructors
60 --
61 = TypeRef ProperName (Maybe [ProperName])
62 -- |
63 -- A value
64 --
65 | ValueRef Ident
66 -- |
67 -- A type class
68 --
69 | TypeClassRef ProperName
70 -- |
71 -- A type class instance, created during typeclass desugaring (name, class name, instance types)
72 --
73 | TypeInstanceRef Ident
74 deriving (Show, Eq, D.Data, D.Typeable)
76-- |
77-- The data type of declarations
78--
79data Declaration
80 -- |
81 -- A data type declaration (name, arguments, data constructors)
82 --
83 = DataDeclaration ProperName [String] [(ProperName, [Type])]
84 -- |
85 -- A minimal mutually recursive set of data type declarations
86 --
87 | DataBindingGroupDeclaration [Declaration]
88 -- |
89 -- A type synonym declaration (name, arguments, type)
90 --
91 | TypeSynonymDeclaration ProperName [String] Type
92 -- |
93 -- A type declaration for a value (name, ty)
94 --
95 | TypeDeclaration Ident Type
96 -- |
97 -- A value declaration (name, top-level binders, optional guard, value)
98 --
99 | ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
100 -- |
101 -- A minimal mutually recursive set of value declarations
102 --
103 | BindingGroupDeclaration [(Ident, NameKind, Value)]
104 -- |
105 -- A foreign import declaration (type, name, optional inline Javascript, type)
106 --
107 | ExternDeclaration ForeignImportType Ident (Maybe JS) Type
108 -- |
109 -- A data type foreign import (name, kind)
110 --
111 | ExternDataDeclaration ProperName Kind
112 -- |
113 -- A type class instance foreign import
114 --
115 | ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
116 -- |
117 -- A fixity declaration (fixity data, operator name)
118 --
119 | FixityDeclaration Fixity String
120 -- |
121 -- A module import (module name, optional set of identifiers to import, optional "qualified as"
122 -- name)
123 --
124 | ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
125 -- |
126 -- A type class declaration (name, argument, member declarations)
127 --
128 | TypeClassDeclaration ProperName [String] [Declaration]
129 -- |
130 -- A type instance declaration (name, dependencies, class name, instance types, member
131 -- declarations)
132 --
133 | TypeInstanceDeclaration Ident [(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 type class instance foreign import
167--
168isExternInstanceDecl :: Declaration -> Bool
169isExternInstanceDecl ExternInstanceDeclaration{} = True
170isExternInstanceDecl _ = False
172-- |
173-- Test if a declaration is a fixity declaration
174--
175isFixityDecl :: Declaration -> Bool
176isFixityDecl FixityDeclaration{} = True
177isFixityDecl _ = False
179-- |
180-- Test if a declaration is a foreign import
181--
182isExternDecl :: Declaration -> Bool
183isExternDecl ExternDeclaration{} = True
184isExternDecl _ = False
186-- |
187-- Test if a declaration is a type class or instance declaration
188--
189isTypeClassDeclaration :: Declaration -> Bool
190isTypeClassDeclaration TypeClassDeclaration{} = True
191isTypeClassDeclaration TypeInstanceDeclaration{} = True
192isTypeClassDeclaration _ = False