@@ -29,6 +29,8 @@ import Data.List (intercalate, isPrefixOf, nub, sortBy)
2929import Data.Maybe (mapMaybe)
3030import Data.Traversable (traverse)
3131
32+import Parser
33+
3234import System.Console.Haskeline
3335import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
3436import System.Exit
@@ -36,11 +38,12 @@ import System.Environment.XDG.BaseDir
3638import System.FilePath ((</>), isPathSeparator)
3739import System.Process
3840
41+import Text.Parsec (choice)
42+
3943import qualified Data.Map as M
4044import qualified Language.PureScript as P
4145import qualified Paths_purescript as Paths
4246import qualified System.IO.UTF8 as U (readFile)
43−import qualified Text.Parsec as Parsec (Parsec, eof, try)
4447
4548-- |
4649-- The PSCI state.
@@ -69,8 +72,8 @@ ioToState = lift . lift
6972-- |
7073-- Updates the state to have more imported modules.
7174--
72−updateImports :: String -> PSCI -> PSCI
73−updateImports name (PSCI i m b) = PSCI (P.moduleNameFromString name : i) m b
75+updateImports :: P.ModuleName -> PSCI -> PSCI
76+updateImports name (PSCI i m b) = PSCI (name : i) m b
7477
7578-- |
7679-- Updates the state to have more loaded files.
@@ -115,7 +118,7 @@ getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
115118-- Loads a file for use with imports.
116119--
117120loadModule :: FilePath -> IO (Either String [P.Module])
118−loadModule = fmap (either (Left . show) Right . P.runIndentParser "" P.parseModules) . U.readFile
121+loadModule = fmap (either (Left . show) Right . parseModules) . U.readFile
119122
120123-- |
121124-- Expands tilde in path.
@@ -196,7 +199,7 @@ createTemporaryModule exec imports lets value =
196199 let
197200 moduleName = P.ModuleName [P.ProperName "Main"]
198201 importDecl m = P.ImportDeclaration m Nothing
199− traceModule = P.ModuleName [P.ProperName "Trace"]
202+ traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
200203 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
201204 value' = foldr ($) value lets
202205 itDecl = P.ValueDeclaration (P.Ident "it") [] Nothing value'
@@ -233,24 +236,24 @@ handleTypeOf value (PSCI imports loadedModules lets) = do
233236 Just (ty, _) -> outputStrLn . P.prettyPrintType $ ty
234237 Nothing -> outputStrLn "Could not find type"
235238
236−-- Parser helpers
237−
238−-- |
239−-- Parser for our PSCI version of @let@.
240−-- This is essentially let from do-notation.
241−-- However, since we don't support the @Eff@ monad, we actually want the normal @let@.
242−--
243−parseLet :: Parsec.Parsec String P.ParseState (P.Value -> P.Value)
244−parseLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
245− <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
239+-- Commands
246240
247241-- |
248−-- Parser for any other valid expression.
242+-- Parses the input and returns either a Metacommand or an expression.
249243--
250−parseExpression :: Parsec.Parsec String P.ParseState P.Value
251−parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
252−
253−-- Commands
244+getCommand :: InputT IO Command
245+getCommand = do
246+ firstLine <- getInputLine "> "
247+ case firstLine of
248+ Nothing -> return Empty
249+ Just line -> case parseCommands line of
250+ Left err -> return $ Unknown err
251+ Right c -> case c of
252+ Expression expr -> Expression <$> go [expr]
253+ _ -> lift $ return c
254+ where
255+ go :: [String] -> InputT IO String
256+ go ls = maybe (return . unlines $ reverse ls) (go . (:ls)) =<< getInputLine " "
254257
255258-- |
256259-- Performs an action for each meta-command given, and also for expressions..
@@ -258,9 +261,9 @@ parseExpression = P.whiteSpace *> P.parseValue <* Parsec.eof
258261handleCommand :: Command -> StateT PSCI (InputT IO) ()
259262handleCommand Empty = return ()
260263handleCommand (Expression ls) =
261− case P.runIndentParser "" (Left <$> Parsec.try parseLet <|> Right <$> parseExpression) (unlines ls) of
262− Left err -> inputTToState $ outputStrLn (show err)
263− Right (Left l) -> modify (updateLets l)
264+ case psciParser (choice [Left <$> psciLet, Right <$> psciExpression]) ls of
265+ Left err -> inputTToState $ outputStrLn (show err)
266+ Right (Left l) -> modify (updateLets l)
264267 Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
265268handleCommand Help = inputTToState $ outputStrLn helpMessage
266269handleCommand (Import moduleName) = modify (updateImports moduleName)
@@ -275,8 +278,8 @@ handleCommand Reload = do
275278 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
276279 put (PSCI defaultImports prelude [])
277280handleCommand (TypeOf expr) =
278− case P.runIndentParser "" parseExpression expr of
279− Left err -> inputTToState $ outputStrLn (show err)
281+ case parseExpression expr of
282+ Left err -> inputTToState $ outputStrLn (show err)
280283 Right expr' -> get >>= inputTToState . handleTypeOf expr'
281284handleCommand _ = outputTStrLn "Unknown command"
282285