module Language.PureScript.AST.Declarations where
import Prelude ()
import Prelude.Compat
import Data.Aeson.TH
import Data.List (nub, (\\))
import Data.Maybe (mapMaybe)
import qualified Data.Map as M
import Control.Monad.Identity
import Language.PureScript.AST.Binders
import Language.PureScript.AST.Operators
import Language.PureScript.AST.SourcePos
import Language.PureScript.Types
import Language.PureScript.Names
import Language.PureScript.Kinds
import Language.PureScript.TypeClassDictionaries
import Language.PureScript.Comments
import Language.PureScript.Environment
data Module = Module SourceSpan [Comment] ModuleName [Declaration] (Maybe [DeclarationRef])
deriving (Show, Read)
getModuleName :: Module -> ModuleName
getModuleName (Module _ _ name _ _) = name
addDefaultImport :: ModuleName -> Module -> Module
addDefaultImport toImport m@(Module ss coms mn decls exps) =
if isExistingImport `any` decls || mn == toImport then m
else Module ss coms mn (ImportDeclaration toImport Implicit Nothing False : decls) exps
where
isExistingImport (ImportDeclaration mn' _ _ _) | mn' == toImport = True
isExistingImport (PositionedDeclaration _ _ d) = isExistingImport d
isExistingImport _ = False
data DeclarationRef
= TypeRef (ProperName 'TypeName) (Maybe [ProperName 'ConstructorName])
| ValueRef Ident
| TypeClassRef (ProperName 'ClassName)
| TypeInstanceRef Ident
| ModuleRef ModuleName
| ProperRef String
| PositionedDeclarationRef SourceSpan [Comment] DeclarationRef
deriving (Show, Read)
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'
(ModuleRef name) == (ModuleRef name') = name == name'
(ProperRef name) == (ProperRef name') = name == name'
(PositionedDeclarationRef _ _ r) == r' = r == r'
r == (PositionedDeclarationRef _ _ r') = r == r'
_ == _ = False
isModuleRef :: DeclarationRef -> Bool
isModuleRef (PositionedDeclarationRef _ _ r) = isModuleRef r
isModuleRef (ModuleRef _) = True
isModuleRef _ = False
findDuplicateRefs :: [DeclarationRef] -> ([DeclarationRef], [ProperName 'ConstructorName])
findDuplicateRefs refs =
let positionless = stripPosInfo `map` refs
simplified = simplifyTypeRefs `map` positionless
dupeRefs = nub $ simplified \\ nub simplified
dupeCtors = concat $ flip mapMaybe positionless $ \case
TypeRef _ (Just dctors) ->
let dupes = dctors \\ nub dctors
in if null dupes then Nothing else Just dupes
_ -> Nothing
in (dupeRefs, dupeCtors)
where
stripPosInfo (PositionedDeclarationRef _ _ ref) = stripPosInfo ref
stripPosInfo other = other
simplifyTypeRefs (TypeRef pn _) = TypeRef pn Nothing
simplifyTypeRefs other = other
data ImportDeclarationType
= Implicit
| Explicit [DeclarationRef]
| Hiding [DeclarationRef]
deriving (Eq, Show, Read)
isImplicit :: ImportDeclarationType -> Bool
isImplicit Implicit = True
isImplicit _ = False
isExplicit :: ImportDeclarationType -> Bool
isExplicit (Explicit _) = True
isExplicit _ = False
data Declaration
= DataDeclaration DataDeclType (ProperName 'TypeName) [(String, Maybe Kind)] [(ProperName 'ConstructorName, [Type])]
| DataBindingGroupDeclaration [Declaration]
| TypeSynonymDeclaration (ProperName 'TypeName) [(String, Maybe Kind)] Type
| TypeDeclaration Ident Type
| ValueDeclaration Ident NameKind [Binder] (Either [(Guard, Expr)] Expr)
| BindingGroupDeclaration [(Ident, NameKind, Expr)]
| ExternDeclaration Ident Type
| ExternDataDeclaration (ProperName 'TypeName) Kind
| FixityDeclaration Fixity String (Maybe (Either (Qualified Ident) (Qualified (ProperName 'ConstructorName))))
| ImportDeclaration ModuleName ImportDeclarationType (Maybe ModuleName) Bool
| TypeClassDeclaration (ProperName 'ClassName) [(String, Maybe Kind)] [Constraint] [Declaration]
| TypeInstanceDeclaration Ident [Constraint] (Qualified (ProperName 'ClassName)) [Type] TypeInstanceBody
| PositionedDeclaration SourceSpan [Comment] Declaration
deriving (Show, Read)
data TypeInstanceBody
= DerivedInstance
| ExplicitInstance [Declaration]
deriving (Show, Read)
mapTypeInstanceBody :: ([Declaration] -> [Declaration]) -> TypeInstanceBody -> TypeInstanceBody
mapTypeInstanceBody f = runIdentity . traverseTypeInstanceBody (Identity . f)
traverseTypeInstanceBody :: (Applicative f) => ([Declaration] -> f [Declaration]) -> TypeInstanceBody -> f TypeInstanceBody
traverseTypeInstanceBody _ DerivedInstance = pure DerivedInstance
traverseTypeInstanceBody f (ExplicitInstance ds) = ExplicitInstance <$> f ds
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
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
isTypeClassInstanceDeclaration :: Declaration -> Bool
isTypeClassInstanceDeclaration TypeInstanceDeclaration{} = True
isTypeClassInstanceDeclaration (PositionedDeclaration _ _ d) = isTypeClassInstanceDeclaration d
isTypeClassInstanceDeclaration _ = False
isTypeClassDeclaration :: Declaration -> Bool
isTypeClassDeclaration TypeClassDeclaration{} = True
isTypeClassDeclaration (PositionedDeclaration _ _ d) = isTypeClassDeclaration d
isTypeClassDeclaration _ = False
flattenDecls :: [Declaration] -> [Declaration]
flattenDecls = concatMap flattenOne
where flattenOne :: Declaration -> [Declaration]
flattenOne (DataBindingGroupDeclaration decls) = concatMap flattenOne decls
flattenOne d = [d]
type Guard = Expr
data Expr
= NumericLiteral (Either Integer Double)
| StringLiteral String
| CharLiteral Char
| BooleanLiteral Bool
| UnaryMinus Expr
| BinaryNoParens Expr Expr Expr
| Parens Expr
| OperatorSection Expr (Either Expr Expr)
| ArrayLiteral [Expr]
| ObjectLiteral [(String, Expr)]
| ObjectGetter String
| Accessor String Expr
| ObjectUpdate Expr [(String, Expr)]
| Abs (Either Ident Binder) Expr
| App Expr Expr
| Var (Qualified Ident)
| IfThenElse Expr Expr Expr
| Constructor (Qualified (ProperName 'ConstructorName))
| Case [Expr] [CaseAlternative]
| TypedValue Bool Expr Type
| Let [Declaration] Expr
| Do [DoNotationElement]
| TypeClassDictionaryConstructorApp (Qualified (ProperName 'ClassName)) Expr
| TypeClassDictionary Constraint (M.Map (Maybe ModuleName) (M.Map (Qualified (ProperName 'ClassName)) (M.Map (Qualified Ident) TypeClassDictionaryInScope)))
| TypeClassDictionaryAccessor (Qualified (ProperName 'ClassName)) Ident
| SuperClassDictionary (Qualified (ProperName 'ClassName)) [Type]
| AnonymousArgument
| PositionedValue SourceSpan [Comment] Expr
deriving (Show, Read)
data CaseAlternative = CaseAlternative
{
caseAlternativeBinders :: [Binder]
, caseAlternativeResult :: Either [(Guard, Expr)] Expr
} deriving (Show, Read)
data DoNotationElement
= DoNotationValue Expr
| DoNotationBind Binder Expr
| DoNotationLet [Declaration]
| PositionedDoNotationElement SourceSpan [Comment] DoNotationElement
deriving (Show, Read)
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''DeclarationRef)
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ImportDeclarationType)