@@ -1,131 +0,0 @@
1−{-# LANGUAGE TupleSections #-}
3−-- | Functions and types relevant to producing some form of output after
4−-- rendering documentation.
8−import qualified Data.Map as M
9−import Data.Maybe (mapMaybe)
10−import Data.List.Split (splitOn)
11−import Data.List (stripPrefix)
13−import Control.Applicative
15−import Control.Arrow (first)
17−import qualified Language.PureScript as P
18−import qualified Language.PureScript.Constants as C
20−import System.Exit (exitFailure)
21−import System.IO (hPutStrLn, stderr)
23−import Language.PureScript.Docs
24−import Web.Bower.PackageMeta hiding (Version)
28−-- | Specifies whether a PureScript source file is considered as:
30−-- 1) with the `Local` constructor, a target source file, i.e., we want to see
31−-- its modules in the output
32−-- 2) with the `FromDep` constructor, a dependencies source file, i.e. we do
33−-- not want its modules in the output; it is there to enable desugaring, and
34−-- to ensure that links between modules are constructed correctly.
35−type FileInfo = InPackage FilePath
37−fileInfoToString :: FileInfo -> FilePath
38−fileInfoToString (Local fn) = fn
39−fileInfoToString (FromDep _ fn) = fn
41−addDefaultImport :: P.ModuleName -> P.Module -> P.Module
42−addDefaultImport toImport m@(P.Module coms mn decls exps) =
43− if isExistingImport `any` decls || mn == toImport then m
44− else P.Module coms mn (P.ImportDeclaration toImport P.Implicit Nothing : decls) exps
46− isExistingImport (P.ImportDeclaration mn' _ _) | mn' == toImport = True
47− isExistingImport (P.PositionedDeclaration _ _ d) = isExistingImport d
48− isExistingImport _ = False
50−importPrim :: P.Module -> P.Module
51−importPrim = addDefaultImport (P.ModuleName [P.ProperName C.prim])
53−importPrelude :: P.Module -> P.Module
54−importPrelude = addDefaultImport (P.ModuleName [P.ProperName C.prelude])
56−desugar :: [P.Module] -> Either P.MultipleErrors [P.Module]
57−desugar = P.evalSupplyT 0 . desugar'
59− desugar' :: [P.Module] -> P.SupplyT (Either P.MultipleErrors) [P.Module]
60− desugar' = mapM P.desugarDoModule >=> P.desugarCasesModule >=> P.desugarImports
62−parseFile :: FilePath -> IO (FilePath, String)
63−parseFile input' = (,) input' <$> readFile input'
67− -> [(PackageName, FilePath)]
68− -> ([Bookmark] -> [P.Module] -> IO a)
70−parseAndDesugar inputFiles depsFiles callback = do
71− inputFiles' <- mapM (parseAs Local) inputFiles
72− depsFiles' <- mapM (\(pkgName, f) -> parseAs (FromDep pkgName) f) depsFiles
74− case P.parseModulesFromFiles fileInfoToString (inputFiles' ++ depsFiles') of
76− hPutStrLn stderr "parse failed:"
77− hPutStrLn stderr $ show err
80− let depsModules = getDepsModuleNames (map (\(fp, m) -> (,m) <$> fp) ms)
81− case P.sortModules . map (importPrim . importPrelude . snd) $ ms of
83− hPutStrLn stderr "sortModules failed:"
84− hPutStrLn stderr (P.prettyPrintMultipleErrors False e')
89− hPutStrLn stderr "desugar failed:"
90− hPutStrLn stderr (P.prettyPrintMultipleErrors False err)
93− let modules' = map (addPackage depsModules) modules
94− bookmarks = concatMap collectBookmarks modules'
95− in callback bookmarks (takeLocals modules')
98− parseAs :: (FilePath -> a) -> FilePath -> IO (a, String)
99− parseAs g = fmap (first g) . parseFile
101− getDepsModuleNames :: [InPackage (FilePath, P.Module)] -> M.Map P.ModuleName PackageName
102− getDepsModuleNames = foldl go M.empty
104− go deps p = deps # case p of
106− FromDep pkgName (_, m) -> M.insert (P.getModuleName m) pkgName
109− addPackage :: M.Map P.ModuleName PackageName -> P.Module -> InPackage P.Module
110− addPackage depsModules m =
111− case M.lookup (P.getModuleName m) depsModules of
112− Just pkgName -> FromDep pkgName m
115−getInputAndDepsFiles :: IO ([FilePath], [(PackageName, FilePath)])
116−getInputAndDepsFiles = do
117− inputFiles <- globRelative purescriptSourceFiles
118− depsFiles' <- globRelative purescriptDepsFiles
119− return (inputFiles, mapMaybe withPackageName depsFiles')
121−withPackageName :: FilePath -> Maybe (PackageName, FilePath)
122−withPackageName fp = (,fp) <$> getPackageName fp
124−getPackageName :: FilePath -> Maybe PackageName
125−getPackageName fp = do
126− let xs = splitOn "/" fp
127− ys <- stripPrefix ["bower_components"] xs
129− case mkPackageName y of
130− Right name -> Just name