@@ -17,6 +17,8 @@ module Parser (
1717 parseCommand
1818 ) where
1919
20+ import Prelude hiding (lex )
21+
2022import Commands
2123
2224import Data.Char (isSpace )
@@ -28,48 +30,58 @@ import Text.Parsec hiding ((<|>))
2830import qualified Language.PureScript as P
2931import qualified Language.PureScript.Parser.Common as C (mark , same )
3032
33+ parseRestWith :: P. TokenParser a -> Parsec String () a
34+ parseRestWith p = do
35+ s <- many anyChar
36+ case P. lex " " s >>= P. runTokenParser " " p of
37+ Left err -> fail (show err)
38+ Right a -> return a
39+
3140-- |
3241-- PSCI version of @let@.
3342-- This is essentially let from do-notation.
3443-- However, since we don't support the @Eff@ monad,
3544-- we actually want the normal @let@.
3645--
37- psciLet :: Parsec String P. ParseState Command
38- psciLet = Let <$> (P. Let <$> (P. reserved " let" *> P. indented *> C. mark (many1 (C. same *> P. parseDeclaration))))
46+ psciLet :: Parsec String () Command
47+ psciLet = Let <$> (P. Let <$> (string " let" *> spaces *> parseRestWith manyDecls))
48+ where
49+ manyDecls :: P. TokenParser [P. Declaration ]
50+ manyDecls = C. mark (many1 (C. same *> P. parseDeclaration))
3951
4052-- |
4153-- Parses PSCI metacommands or expressions input from the user.
4254--
4355parseCommand :: String -> Either ParseError Command
44- parseCommand = P. runIndentParser " " $ choice
45- [ P. whiteSpace *> char ' :' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf <|> psciBrowse <|> psciShowModules)
56+ parseCommand = flip parse " " $ choice
57+ [ spaces *> char ' :' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf <|> psciBrowse <|> psciShowModules)
4658 , try psciLet
4759 , psciExpression
4860 ] <* eof
4961
5062-- |
5163-- Parses expressions entered at the PSCI repl.
5264--
53- psciExpression :: Parsec String P. ParseState Command
54- psciExpression = Expression <$> P. parseValue
65+ psciExpression :: Parsec String () Command
66+ psciExpression = Expression <$> parseRestWith P. parseValue
5567
5668-- |
5769-- Parses 'Commands.Help' command.
5870--
59- psciHelp :: Parsec String P. ParseState Command
71+ psciHelp :: Parsec String () Command
6072psciHelp = Help <$ char ' ?'
6173
6274-- |
6375-- Parses 'Commands.Import' command.
6476--
65- psciImport :: Parsec String P. ParseState Command
66- psciImport = Import <$> (char ' i' *> P. whiteSpace *> P. moduleName)
77+ psciImport :: Parsec String () Command
78+ psciImport = Import <$> (char ' i' *> spaces *> parseRestWith P. moduleName)
6779
6880-- |
6981-- Parses 'Commands.LoadFile' command.
7082--
71- psciLoadFile :: Parsec String P. ParseState Command
72- psciLoadFile = LoadFile . trimEnd <$> (char ' m' *> P. whiteSpace *> manyTill anyChar eof)
83+ psciLoadFile :: Parsec String () Command
84+ psciLoadFile = LoadFile . trimEnd <$> (char ' m' *> spaces *> manyTill anyChar eof)
7385
7486-- | Trim end of input string
7587trimEnd :: String -> String
@@ -78,35 +90,35 @@ trimEnd = reverse . dropWhile isSpace . reverse
7890-- |
7991-- Parses 'Commands.Quit' command.
8092--
81- psciQuit :: Parsec String P. ParseState Command
93+ psciQuit :: Parsec String () Command
8294psciQuit = Quit <$ char ' q'
8395
8496-- |
8597-- Parses 'Commands.Reload' command.
8698--
87- psciReload :: Parsec String P. ParseState Command
99+ psciReload :: Parsec String () Command
88100psciReload = Reset <$ char ' r'
89101
90102-- |
91103-- Parses 'Commands.TypeOf' command.
92104--
93- psciTypeOf :: Parsec String P. ParseState Command
94- psciTypeOf = TypeOf <$> (char ' t' *> P. whiteSpace *> P. parseValue)
105+ psciTypeOf :: Parsec String () Command
106+ psciTypeOf = TypeOf <$> (char ' t' *> spaces *> parseRestWith P. parseValue)
95107
96108
97109-- |
98110-- Parses 'Commands.KindOf' command.
99111--
100- psciKindOf :: Parsec String P. ParseState Command
101- psciKindOf = KindOf <$> (char ' k' *> P. whiteSpace *> P. parseType)
112+ psciKindOf :: Parsec String () Command
113+ psciKindOf = KindOf <$> (char ' k' *> spaces *> parseRestWith P. parseType)
102114
103115-- |
104116-- Parses 'Commands.Browse' command.
105117--
106- psciBrowse :: Parsec String P. ParseState Command
107- psciBrowse = Browse <$> (char ' b' *> P. whiteSpace *> P. moduleName)
118+ psciBrowse :: Parsec String () Command
119+ psciBrowse = Browse <$> (char ' b' *> spaces *> parseRestWith P. moduleName)
108120
109121-- |
110122-- Show Command
111- psciShowModules :: Parsec String P. ParseState Command
112- psciShowModules = Show . trimEnd <$> (char ' s' *> P. whiteSpace *> manyTill anyChar eof)
123+ psciShowModules :: Parsec String () Command
124+ psciShowModules = Show . trimEnd <$> (char ' s' *> spaces *> manyTill anyChar eof)
0 commit comments