module Language.PureScript.ModuleDependencies
( sortModules
, ModuleGraph
, ModuleSignature(..)
, moduleSignature
) where
import Protolude hiding (head)
import Data.Graph
import qualified Data.Set as S
import Language.PureScript.AST
import qualified Language.PureScript.Constants as C
import Language.PureScript.Crash
import Language.PureScript.Errors hiding (nonEmpty)
import Language.PureScript.Names
type ModuleGraph = [(ModuleName, [ModuleName])]
data ModuleSignature = ModuleSignature
{ sigSourceSpan :: SourceSpan
, sigModuleName :: ModuleName
, sigImports :: [(ModuleName, SourceSpan)]
}
sortModules
:: forall m a
. MonadError MultipleErrors m
=> (a -> ModuleSignature)
-> [a]
-> m ([a], ModuleGraph)
sortModules toSig ms = do
let
ms' = (\m -> (m, toSig m)) <$> ms
mns = S.fromList $ map (sigModuleName . snd) ms'
verts <- parU ms' (toGraphNode mns)
ms'' <- parU (stronglyConnComp verts) toModule
let (graph, fromVertex, toVertex) = graphFromEdges verts
moduleGraph = do (_, mn, _) <- verts
let v = fromMaybe (internalError "sortModules: vertex not found") (toVertex mn)
deps = reachable graph v
toKey i = case fromVertex i of (_, key, _) -> key
return (mn, filter (/= mn) (map toKey deps))
return (fst <$> ms'', moduleGraph)
where
toGraphNode :: S.Set ModuleName -> (a, ModuleSignature) -> m ((a, ModuleSignature), ModuleName, [ModuleName])
toGraphNode mns m@(_, ModuleSignature _ mn deps) = do
void . parU deps $ \(dep, pos) ->
when (dep `notElem` C.primModules && S.notMember dep mns) .
throwError
. addHint (ErrorInModule mn)
. errorMessage' pos
$ ModuleNotFound dep
pure (m, mn, map fst deps)
usedModules :: Declaration -> Maybe (ModuleName, SourceSpan)
usedModules (ImportDeclaration (ss, _) mn _ _) = pure (mn, ss)
usedModules _ = Nothing
toModule :: MonadError MultipleErrors m => SCC (a, ModuleSignature) -> m (a, ModuleSignature)
toModule (AcyclicSCC m) = return m
toModule (CyclicSCC ms) =
case nonEmpty ms of
Nothing ->
internalError "toModule: empty CyclicSCC"
Just ms' ->
throwError
. errorMessage'' (fmap (sigSourceSpan . snd) ms')
$ CycleInModules (map (sigModuleName . snd) ms)
moduleSignature :: Module -> ModuleSignature
moduleSignature (Module ss _ mn ds _) = ModuleSignature ss mn (ordNub (mapMaybe usedModules ds))