module Language.PureScript.Declarations where
import Language.PureScript.Types
import Language.PureScript.Names
import Language.PureScript.Kinds
import Language.PureScript.TypeClassDictionaries
import Language.PureScript.CodeGen.JS.AST
import Language.PureScript.Environment
import qualified Data.Data as D
import Data.Generics (mkQ, everything)
type Precedence = Integer
data Associativity = Infixl | Infixr | Infix deriving (D.Data, D.Typeable)
instance Show Associativity where
show Infixl = "infixl"
show Infixr = "infixr"
show Infix = "infix"
data SourcePos = SourcePos
{
sourceName :: String
, sourcePosLine :: Int
, sourcePosColumn :: Int
} deriving (D.Data, D.Typeable)
instance Show SourcePos where
show sp = (sourceName sp) ++ " line " ++ show (sourcePosLine sp) ++ ", column " ++ show (sourcePosColumn sp)
data Fixity = Fixity Associativity Precedence deriving (Show, D.Data, D.Typeable)
data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
data DeclarationRef
= TypeRef ProperName (Maybe [ProperName])
| ValueRef Ident
| TypeClassRef ProperName
| TypeInstanceRef Ident
| PositionedDeclarationRef SourcePos DeclarationRef
deriving (Show, D.Data, D.Typeable)
instance Eq DeclarationRef where
(TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
(ValueRef name) == (ValueRef name') = name == name'
(TypeClassRef name) == (TypeClassRef name') = name == name'
(TypeInstanceRef name) == (TypeInstanceRef name') = name == name'
(PositionedDeclarationRef _ r) == r' = r == r'
r == (PositionedDeclarationRef _ r') = r == r'
_ == _ = False
data Declaration
= DataDeclaration ProperName [String] [(ProperName, [Type])]
| DataBindingGroupDeclaration [Declaration]
| TypeSynonymDeclaration ProperName [String] Type
| TypeDeclaration Ident Type
| ValueDeclaration Ident NameKind [Binder] (Maybe Guard) Value
| BindingGroupDeclaration [(Ident, NameKind, Value)]
| ExternDeclaration ForeignImportType Ident (Maybe JS) Type
| ExternDataDeclaration ProperName Kind
| ExternInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type]
| FixityDeclaration Fixity String
| ImportDeclaration ModuleName (Maybe [DeclarationRef]) (Maybe ModuleName)
| TypeClassDeclaration ProperName [String] [(Qualified ProperName, [Type])] [Declaration]
| TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
| PositionedDeclaration SourcePos Declaration
deriving (Show, D.Data, D.Typeable)
isValueDecl :: Declaration -> Bool
isValueDecl ValueDeclaration{} = True
isValueDecl (PositionedDeclaration _ d) = isValueDecl d
isValueDecl _ = False
isDataDecl :: Declaration -> Bool
isDataDecl DataDeclaration{} = True
isDataDecl TypeSynonymDeclaration{} = True
isDataDecl (PositionedDeclaration _ d) = isDataDecl d
isDataDecl _ = False
isImportDecl :: Declaration -> Bool
isImportDecl ImportDeclaration{} = True
isImportDecl (PositionedDeclaration _ d) = isImportDecl d
isImportDecl _ = False
isExternDataDecl :: Declaration -> Bool
isExternDataDecl ExternDataDeclaration{} = True
isExternDataDecl (PositionedDeclaration _ d) = isExternDataDecl d
isExternDataDecl _ = False
isExternInstanceDecl :: Declaration -> Bool
isExternInstanceDecl ExternInstanceDeclaration{} = True
isExternInstanceDecl (PositionedDeclaration _ d) = isExternInstanceDecl d
isExternInstanceDecl _ = False
isFixityDecl :: Declaration -> Bool
isFixityDecl FixityDeclaration{} = True
isFixityDecl (PositionedDeclaration _ d) = isFixityDecl d
isFixityDecl _ = False
isExternDecl :: Declaration -> Bool
isExternDecl ExternDeclaration{} = True
isExternDecl (PositionedDeclaration _ d) = isExternDecl d
isExternDecl _ = False
isTypeClassDeclaration :: Declaration -> Bool
isTypeClassDeclaration TypeClassDeclaration{} = True
isTypeClassDeclaration TypeInstanceDeclaration{} = True
isTypeClassDeclaration (PositionedDeclaration _ d) = isTypeClassDeclaration d
isTypeClassDeclaration _ = False
type Guard = Value
data Value
= NumericLiteral (Either Integer Double)
| StringLiteral String
| BooleanLiteral Bool
| UnaryMinus Value
| BinaryNoParens (Qualified Ident) Value Value
| Parens Value
| ArrayLiteral [Value]
| ObjectLiteral [(String, Value)]
| Accessor String Value
| ObjectUpdate Value [(String, Value)]
| Abs (Either Ident Binder) Value
| App Value Value
| Var (Qualified Ident)
| IfThenElse Value Value Value
| Constructor (Qualified ProperName)
| Case [Value] [CaseAlternative]
| TypedValue Bool Value Type
| Let [Declaration] Value
| Do [DoNotationElement]
| TypeClassDictionary Bool (Qualified ProperName, [Type]) [TypeClassDictionaryInScope]
| SuperClassDictionary (Qualified ProperName) [Type]
| PositionedValue SourcePos Value deriving (Show, D.Data, D.Typeable)
data CaseAlternative = CaseAlternative
{
caseAlternativeBinders :: [Binder]
, caseAlternativeGuard :: Maybe Guard
, caseAlternativeResult :: Value
} deriving (Show, D.Data, D.Typeable)
canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm
data DoNotationElement
= DoNotationValue Value
| DoNotationBind Binder Value
| DoNotationLet [Declaration]
| PositionedDoNotationElement SourcePos DoNotationElement deriving (Show, D.Data, D.Typeable)
data Binder
= NullBinder
| BooleanBinder Bool
| StringBinder String
| NumberBinder (Either Integer Double)
| VarBinder Ident
| ConstructorBinder (Qualified ProperName) [Binder]
| ObjectBinder [(String, Binder)]
| ArrayBinder [Binder]
| ConsBinder Binder Binder
| NamedBinder Ident Binder
| PositionedBinder SourcePos Binder deriving (Show, D.Data, D.Typeable)
binderNames :: (D.Data d) => d -> [Ident]
binderNames = everything (++) (mkQ [] go)
where
go (VarBinder ident) = [ident]
go (NamedBinder ident _) = [ident]
go _ = []