@@ -1,4 +1,5 @@
1{-# LANGUAGE TupleSections #-}
1{-# LANGUAGE TupleSections #-}
2+{-# LANGUAGE FlexibleContexts #-}
3module Language.PureScript.Docs.ParseAndDesugar
4module Language.PureScript.Docs.ParseAndDesugar
4 ( parseAndDesugar
5 ( parseAndDesugar
@@ -11,7 +12,6 @@ import qualified Data.Map as M
11import Control.Arrow (first)
12import Control.Arrow (first)
12import Control.Monad
13import Control.Monad
14−import Control.Monad.Trans.Except
15import Control.Monad.Writer.Strict (runWriterT)
15import Control.Monad.Writer.Strict (runWriterT)
16import Control.Monad.Error.Class (MonadError(..))
16import Control.Monad.Error.Class (MonadError(..))
17import Control.Monad.IO.Class (MonadIO(..))
17import Control.Monad.IO.Class (MonadIO(..))
@@ -29,40 +29,40 @@ import Language.PureScript.Docs.Convert (collectBookmarks)
29-- * A list of local source files
29-- * A list of local source files
30-- * A list of source files from external dependencies, together with their
30-- * A list of source files from external dependencies, together with their
31-- package names
31-- package names
32−-- * A callback, taking a list of bookmarks, and a list of desugared modules
34-- This function does the following:
33-- This function does the following:
36-- * Parse all of the input and dependency source files
35-- * Parse all of the input and dependency source files
37−-- * Partially desugar all of the resulting modules
36+-- * Partially desugar all of the resulting modules (just enough for
37+-- producing documentation from them)
38-- * Collect a list of bookmarks from the whole set of source files
38-- * Collect a list of bookmarks from the whole set of source files
39-- * Collect a list of desugared modules from just the input source files (not
39-- * Collect a list of desugared modules from just the input source files (not
40-- dependencies)
40-- dependencies)
41−-- * Call the callback with the bookmarks and desugared module list.
41+-- * Return the desugared modules and the bookmarks.
42parseAndDesugar ::
42parseAndDesugar ::
43+ (Functor m, Applicative m, MonadError P.MultipleErrors m, MonadIO m) =>
43 [FilePath]
44 [FilePath]
44 -> [(PackageName, FilePath)]
45 -> [(PackageName, FilePath)]
45− -> ([Bookmark] -> [P.Module] -> IO a)
46+ -> m ([P.Module], [Bookmark])
46− -> IO (Either P.MultipleErrors a)
47−parseAndDesugar inputFiles depsFiles callback = do
47+parseAndDesugar inputFiles depsFiles = do
48 inputFiles' <- traverse (parseAs Local) inputFiles
48 inputFiles' <- traverse (parseAs Local) inputFiles
49 depsFiles' <- traverse (\(pkgName, f) -> parseAs (FromDep pkgName) f) depsFiles
49 depsFiles' <- traverse (\(pkgName, f) -> parseAs (FromDep pkgName) f) depsFiles
52− ms <- parseFiles (inputFiles' ++ depsFiles')
51+ ms <- parseFiles (inputFiles' ++ depsFiles')
53− ms' <- sortModules (map snd ms)
52+ ms' <- sortModules (map snd ms)
54− (bs, ms'') <- desugarWithBookmarks ms ms'
53+ desugarWithBookmarks ms ms'
55− liftIO $ callback bs ms''
57parseFiles ::
55parseFiles ::
56+ (MonadError P.MultipleErrors m, MonadIO m) =>
58 [(FileInfo, FilePath)]
57 [(FileInfo, FilePath)]
59− -> ExceptT P.MultipleErrors IO [(FileInfo, P.Module)]
58+ -> m [(FileInfo, P.Module)]
60parseFiles =
59parseFiles =
61 throwLeft . P.parseModulesFromFiles fileInfoToString
60 throwLeft . P.parseModulesFromFiles fileInfoToString
63sortModules ::
62sortModules ::
63+ (Functor m, MonadError P.MultipleErrors m, MonadIO m) =>
64 [P.Module]
64 [P.Module]
65− -> ExceptT P.MultipleErrors IO [P.Module]
65+ -> m [P.Module]
66sortModules =
66sortModules =
67 fmap fst . throwLeft . sortModules' . map importPrim
67 fmap fst . throwLeft . sortModules' . map importPrim
@@ -70,9 +70,10 @@ sortModules =
70 sortModules' = P.sortModules
70 sortModules' = P.sortModules
72desugarWithBookmarks ::
72desugarWithBookmarks ::
73+ (MonadError P.MultipleErrors m, MonadIO m) =>
73 [(FileInfo, P.Module)]
74 [(FileInfo, P.Module)]
74 -> [P.Module]
75 -> [P.Module]
75− -> ExceptT P.MultipleErrors IO ([Bookmark], [P.Module])
76+ -> m ([P.Module], [Bookmark])
76desugarWithBookmarks msInfo msSorted = do
77desugarWithBookmarks msInfo msSorted = do
77 msDesugared <- throwLeft (desugar msSorted)
78 msDesugared <- throwLeft (desugar msSorted)
@@ -80,7 +81,7 @@ desugarWithBookmarks msInfo msSorted = do
80 msPackages = map (addPackage msDeps) msDesugared
81 msPackages = map (addPackage msDeps) msDesugared
81 bookmarks = concatMap collectBookmarks msPackages
82 bookmarks = concatMap collectBookmarks msPackages
83− return (bookmarks, takeLocals msPackages)
84+ return (takeLocals msPackages, bookmarks)
85throwLeft :: (MonadError l m) => Either l r -> m r
86throwLeft :: (MonadError l m) => Either l r -> m r
86throwLeft = either throwError return
87throwLeft = either throwError return
@@ -101,18 +102,24 @@ fileInfoToString (FromDep _ fn) = fn
101importPrim :: P.Module -> P.Module
102importPrim :: P.Module -> P.Module
102importPrim = P.addDefaultImport (P.ModuleName [P.ProperName C.prim])
103importPrim = P.addDefaultImport (P.ModuleName [P.ProperName C.prim])
104−desugar :: [P.Module] -> Either P.MultipleErrors [P.Module]
105+desugar ::
106+ (Functor m, Applicative m, MonadError P.MultipleErrors m) =>
105desugar = P.evalSupplyT 0 . desugar'
109desugar = P.evalSupplyT 0 . desugar'
107− desugar' :: [P.Module] -> P.SupplyT (Either P.MultipleErrors) [P.Module]
111+ desugar' =
112+ traverse P.desugarDoModule
113+ >=> P.desugarCasesModule
108− desugar' = traverse P.desugarDoModule >=> P.desugarCasesModule >=> ignoreWarnings . P.desugarImports []
114+ >=> ignoreWarnings . P.desugarImports []
109 ignoreWarnings m = liftM fst (runWriterT m)
116 ignoreWarnings m = liftM fst (runWriterT m)
111parseFile :: FilePath -> IO (FilePath, String)
118parseFile :: FilePath -> IO (FilePath, String)
112parseFile input' = (,) input' <$> readFile input'
119parseFile input' = (,) input' <$> readFile input'
114−parseAs :: (FilePath -> a) -> FilePath -> IO (a, String)
121+parseAs :: (Functor m, MonadIO m) => (FilePath -> a) -> FilePath -> m (a, String)
115−parseAs g = fmap (first g) . parseFile
122+parseAs g = fmap (first g) . liftIO . parseFile
117getDepsModuleNames :: [InPackage (FilePath, P.Module)] -> M.Map P.ModuleName PackageName
124getDepsModuleNames :: [InPackage (FilePath, P.Module)] -> M.Map P.ModuleName PackageName
118getDepsModuleNames = foldl go M.empty
125getDepsModuleNames = foldl go M.empty