@@ -30,95 +30,53 @@ import Text.Parsec hiding ((<|>))
30import qualified Language.PureScript as P
30import qualified Language.PureScript as P
31import qualified Language.PureScript.Parser.Common as C (mark, same)
31import qualified Language.PureScript.Parser.Common as C (mark, same)
33−parseRestWith :: P.TokenParser a -> Parsec String () a
36− case P.lex "" s >>= P.runTokenParser "" p of
37− Left err -> fail (show err)
41−-- PSCI version of @let@.
42−-- This is essentially let from do-notation.
43−-- However, since we don't support the @Eff@ monad,
44−-- we actually want the normal @let@.
46−psciLet :: Parsec String () Command
47−psciLet = Let <$> (P.Let <$> (string "let" *> spaces *> parseRestWith manyDecls))
49− manyDecls :: P.TokenParser [P.Declaration]
50− manyDecls = C.mark (many1 (C.same *> P.parseDeclaration))
53-- Parses PSCI metacommands or expressions input from the user.
34-- Parses PSCI metacommands or expressions input from the user.
55−parseCommand :: String -> Either ParseError Command
36+parseCommand :: String -> Either String Command
56−parseCommand = flip parse "" $ choice
37+parseCommand cmdString =
57− [ spaces *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf <|> psciBrowse <|> psciShowModules)
38+ case splitCommand cmdString of
58− , try psciLet
39+ Just ('?', _) -> return Help
40+ Just ('q', _) -> return Quit
41+ Just ('r', _) -> return Reset
42+ Just ('i', moduleName) -> Import <$> parseRest P.moduleName moduleName
43+ Just ('b', moduleName) -> Browse <$> parseRest P.moduleName moduleName
44+ Just ('m', filename) -> return $ LoadFile (trimEnd filename)
45+ Just ('s', command) -> return $ Show (trimEnd command)
46+ Just ('t', expr) -> TypeOf <$> parseRest P.parseValue expr
47+ Just ('k', ty) -> KindOf <$> parseRest P.parseType ty
48+ Just _ -> Left $ "Unrecognized command. Type :? for help."
59− , psciExpression
49+ Nothing -> parseRest (psciLet <|> psciExpression) cmdString
61−
51+ parseRest :: P.TokenParser a -> String -> Either String a
52+ parseRest p s = either (Left . show) Right $ do
54+ P.runTokenParser "" (p <* eof) ts
56+ trimEnd :: String -> String
57+ trimEnd = reverse . dropWhile isSpace . reverse
60+ -- Split a command into a command char and the trailing string
62+ splitCommand :: String -> Maybe (Char, String)
63+ splitCommand (':' : c : s) = Just (c, dropWhile isSpace s)
64+ splitCommand _ = Nothing
63−-- Parses expressions entered at the PSCI repl.
67+ -- Parses expressions entered at the PSCI repl.
65−psciExpression :: Parsec String () Command
69+ psciExpression :: P.TokenParser Command
66−psciExpression = Expression <$> parseRestWith P.parseValue
70+ psciExpression = Expression <$> P.parseValue
69−-- Parses 'Commands.Help' command.
71−psciHelp :: Parsec String () Command
72−psciHelp = Help <$ char '?'
75−-- Parses 'Commands.Import' command.
77−psciImport :: Parsec String () Command
78−psciImport = Import <$> (char 'i' *> spaces *> parseRestWith P.moduleName)
81−-- Parses 'Commands.LoadFile' command.
83−psciLoadFile :: Parsec String () Command
84−psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> spaces *> manyTill anyChar eof)
86−-- | Trim end of input string
87−trimEnd :: String -> String
88−trimEnd = reverse . dropWhile isSpace . reverse
91−-- Parses 'Commands.Quit' command.
93−psciQuit :: Parsec String () Command
94−psciQuit = Quit <$ char 'q'
97−-- Parses 'Commands.Reload' command.
99−psciReload :: Parsec String () Command
100−psciReload = Reset <$ char 'r'
103−-- Parses 'Commands.TypeOf' command.
73+ -- PSCI version of @let@.
104−--
74+ -- This is essentially let from do-notation.
105−psciTypeOf :: Parsec String () Command
75+ -- However, since we don't support the @Eff@ monad,
106−psciTypeOf = TypeOf <$> (char 't' *> spaces *> parseRestWith P.parseValue)
76+ -- we actually want the normal @let@.
110−-- Parses 'Commands.KindOf' command.
112−psciKindOf :: Parsec String () Command
78+ psciLet :: P.TokenParser Command
113−psciKindOf = KindOf <$> (char 'k' *> spaces *> parseRestWith P.parseType)
79+ psciLet = Let <$> (P.Let <$> (P.reserved "let" *> P.indented *> manyDecls))
115−-- |
81+ manyDecls :: P.TokenParser [P.Declaration]
116−-- Parses 'Commands.Browse' command.
82+ manyDecls = C.mark (many1 (C.same *> P.parseDeclaration))
118−psciBrowse :: Parsec String () Command
119−psciBrowse = Browse <$> (char 'b' *> spaces *> parseRestWith P.moduleName)
123−psciShowModules :: Parsec String () Command
124−psciShowModules = Show . trimEnd <$> (char 's' *> spaces *> manyTill anyChar eof)