git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #662 from ardumont/add-browse-module-in-psci

[psci] Add :b(rowse) and some :s(how) options to psci

commit32fc5a09c928b76efd490e2b087a918605303537 authorPhil Freeman <paf31@cantab.net> dateFri 31 Oct 2014, 13:09 (12 years ago) parent78d6f7aea21695
3 files changed+88−4
psci/Commands.hs+11 psci/Main.hs+61−1 psci/Parser.hs+16−3
psci/Commands.hs+11
@@ -34,6 +34,10 @@ data Command
3434 --
3535 | Import ModuleName
3636 -- |
37+ -- Browse a module
38+ --
39+ | Browse ModuleName
40+ -- |
3741 -- Load a file for use with importing
3842 --
3943 | LoadFile FilePath
@@ -57,6 +61,10 @@ data Command
5761 -- Find the kind of an expression
5862 --
5963 | KindOf Type
64+ -- |
65+ -- Show command
66+ --
67+ | Show String
6068
6169-- |
6270-- The help menu.
@@ -65,9 +73,12 @@ help :: [[String]]
6573help =
6674 [ [":? ", "Show this help menu"]
6775 , [":i <module> ", "Import <module> for use in PSCI"]
76+ , [":b <module> ", "Browse <module>"]
6877 , [":m <file> ", "Load <file> for importing"]
6978 , [":q ", "Quit PSCi"]
7079 , [":r ", "Reset"]
80+ , [":s import ", "Show imported modules"]
81+ , [":s loaded ", "Show loaded modules"]
7182 , [":t <expr> ", "Show the type of <expr>"]
7283 , [":k <type> ", "Show the kind of <type>"]
7384 ]
psci/Main.hs+61−1
@@ -28,7 +28,7 @@ import qualified Control.Monad.Trans.State.Lazy as L
2828import Control.Monad.Error (ErrorT(..), MonadError)
2929import Control.Monad.Error.Class (MonadError(..))
3030
31−import Data.List (intercalate, isPrefixOf, nub, sortBy)
31+import Data.List (intercalate, isPrefixOf, nub, sortBy, sort)
3232import Data.Maybe (mapMaybe)
3333import Data.Foldable (traverse_)
3434import Data.Version (showVersion)
@@ -54,6 +54,8 @@ import qualified Language.PureScript as P
5454import qualified Paths_purescript as Paths
5555import qualified System.IO.UTF8 as U
5656 (writeFile, putStrLn, print, readFile)
57+import qualified Language.PureScript.Names as N
58+import qualified Language.PureScript.Declarations as D
5759
5860-- |
5961-- The PSCI state.
@@ -302,6 +304,27 @@ handleDeclaration value = do
302304 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
303305 Nothing -> PSCI $ outputStrLn "Couldn't find node.js"
304306
307+-- |
308+-- Show actual loaded modules in psci.
309+--
310+handleShowLoadedModules :: PSCI ()
311+handleShowLoadedModules = do
312+ PSCiState { psciLoadedModules = loadedModules } <- PSCI $ lift get
313+ psciIO $ readModules loadedModules >>= putStrLn
314+ return ()
315+ where readModules = return . unlines . sort . nub . map toModuleName
316+ toModuleName = N.runModuleName . (\ (D.Module mdName _ _) -> mdName) . snd
317+
318+-- |
319+-- Show the imported modules in psci.
320+--
321+handleShowImportedModules :: PSCI ()
322+handleShowImportedModules = do
323+ PSCiState { psciImportedModuleNames = importedModuleNames } <- PSCI $ lift get
324+ psciIO $ readModules importedModuleNames >>= putStrLn
325+ return ()
326+ where readModules = return . unlines . sort . map N.runModuleName
327+
305328-- |
306329-- Imports a module, preserving the initial state on failure.
307330--
@@ -331,6 +354,40 @@ handleTypeOf value = do
331354 Just (ty, _, _) -> PSCI . outputStrLn . P.prettyPrintType $ ty
332355 Nothing -> PSCI $ outputStrLn "Could not find type"
333356
357+-- |
358+-- Pretty print a module's signatures
359+--
360+printModuleSignatures :: P.ModuleName -> P.Environment -> PSCI ()
361+printModuleSignatures moduleName env =
362+ PSCI $ let namesEnv = P.names env
363+ moduleNamesIdent = (filter ((== moduleName) . fst) . M.keys) namesEnv
364+ in case moduleNamesIdent of
365+ [] -> outputStrLn $ "This module '"++ P.runModuleName moduleName ++"' does not export functions."
366+ _ -> ( outputStrLn
367+ . unlines
368+ . sort
369+ . map (showType . findType namesEnv)) moduleNamesIdent
370+ where findType :: M.Map (P.ModuleName, P.Ident) (P.Type, P.NameKind, P.NameVisibility) -> (P.ModuleName, P.Ident) -> (P.Ident, Maybe (P.Type, P.NameKind, P.NameVisibility))
371+ findType envNames m@(_, mIdent) = (mIdent, M.lookup m envNames)
372+ showType :: (P.Ident, Maybe (P.Type, P.NameKind, P.NameVisibility)) -> String
373+ showType (mIdent, Just (mType, _, _)) = show mIdent ++ " :: " ++ P.prettyPrintType mType
374+ showType _ = error "The impossible happened in printModuleSignatures."
375+
376+-- |
377+-- Browse a module and displays its signature (if module exists).
378+--
379+handleBrowse :: P.ModuleName -> PSCI ()
380+handleBrowse moduleName = do
381+ st <- PSCI $ lift get
382+ let loadedModules = psciLoadedModules st
383+ env <- psciIO . runMake $ P.make modulesDir options loadedModules []
384+ case env of
385+ Left err -> PSCI $ outputStrLn err
386+ Right env' ->
387+ if moduleName `notElem` (nub . map ((\ (P.Module modName _ _ ) -> modName) . snd)) loadedModules
388+ then PSCI $ outputStrLn $ "Module '" ++ N.runModuleName moduleName ++ "' is not valid."
389+ else printModuleSignatures moduleName env'
390+
334391-- |
335392-- Takes a value and prints its kind
336393--
@@ -397,6 +454,9 @@ handleCommand Reset = do
397454 Right modules -> PSCI . lift $ put (PSCiState files defaultImports modules [])
398455handleCommand (TypeOf val) = handleTypeOf val
399456handleCommand (KindOf typ) = handleKindOf typ
457+handleCommand (Browse moduleName) = handleBrowse moduleName
458+handleCommand (Show "loaded") = handleShowLoadedModules
459+handleCommand (Show "import") = handleShowImportedModules
400460handleCommand _ = PSCI $ outputStrLn "Unknown command"
401461
402462singleLineFlag :: Cmd.Term Bool
psci/Parser.hs+16−3
@@ -42,7 +42,7 @@ psciLet = Let <$> (P.Let <$> (P.reserved "let" *> P.indented *> C.mark (many1 (C
4242--
4343parseCommand :: String -> Either ParseError Command
4444parseCommand = P.runIndentParser "" $ choice
45− [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf)
45+ [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf <|> psciBrowse <|> psciShowModules)
4646 , try psciLet
4747 , psciExpression
4848 ] <* eof
@@ -70,8 +70,10 @@ psciImport = Import <$> (char 'i' *> P.whiteSpace *> P.moduleName)
7070--
7171psciLoadFile :: Parsec String P.ParseState Command
7272psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> P.whiteSpace *> manyTill anyChar eof)
73− where
74− trimEnd = reverse . dropWhile isSpace . reverse
73+
74+-- | Trim end of input string
75+trimEnd :: String -> String
76+trimEnd = reverse . dropWhile isSpace . reverse
7577
7678-- |
7779-- Parses 'Commands.Quit' command.
@@ -97,3 +99,14 @@ psciTypeOf = TypeOf <$> (char 't' *> P.whiteSpace *> P.parseValue)
9799--
98100psciKindOf :: Parsec String P.ParseState Command
99101psciKindOf = KindOf <$> (char 'k' *> P.whiteSpace *> P.parseType)
102+
103+-- |
104+-- Parses 'Commands.Browse' command.
105+--
106+psciBrowse :: Parsec String P.ParseState Command
107+psciBrowse = Browse <$> (char 'b' *> P.whiteSpace *> P.moduleName)
108+
109+-- |
110+-- Show Command
111+psciShowModules :: Parsec String P.ParseState Command
112+psciShowModules = Show . trimEnd <$> (char 's' *> P.whiteSpace *> manyTill anyChar eof)