@@ -13,124 +13,80 @@
14-----------------------------------------------------------------------------
14-----------------------------------------------------------------------------
16−module Parser where
16+module Parser (
18import Commands
20import Commands
22+import Data.Char (isSpace)
20import Control.Applicative hiding (many)
24import Control.Applicative hiding (many)
22import Text.Parsec hiding ((<|>))
26import Text.Parsec hiding ((<|>))
24import qualified Language.PureScript as P
28import qualified Language.PureScript as P
27−-- PSCI version of modules
29−psciModules :: Parsec String P.ParseState [P.Module]
30−psciModules = P.parseModules
33-- PSCI version of @let@.
31-- PSCI version of @let@.
34-- This is essentially let from do-notation.
32-- This is essentially let from do-notation.
35-- However, since we don't support the @Eff@ monad,
33-- However, since we don't support the @Eff@ monad,
36-- we actually want the normal @let@.
34-- we actually want the normal @let@.
38−psciLet :: Parsec String P.ParseState (P.Value -> P.Value)
36+psciLet :: Parsec String P.ParseState Command
39−psciLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
37+psciLet = Let <$> (P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
40− <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
38+ <*> (P.indented *> P.reservedOp "=" *> P.parseValue))
43−-- PSCI version of any other valid expression.
45−psciExpression :: Parsec String P.ParseState P.Value
46−psciExpression = P.whiteSpace *> P.parseValue <* eof
50−-- This is really just a wrapper around 'Language.PureScript.runIndentParser'
52−psciParser :: Parsec String P.ParseState a -> String -> Either ParseError a
53−psciParser = P.runIndentParser ""
56−-- Parses PSCI modules.
58−parseModules :: String -> Either ParseError [P.Module]
59−parseModules = psciParser psciModules
62−-- Parses PSCI @let@ bindings.
64−parseLet :: String -> Either ParseError (P.Value -> P.Value)
65−parseLet = psciParser psciLet
68−-- Parses PSCI @expressions@.
70−parseExpression :: String -> Either ParseError P.Value
71−parseExpression = psciParser psciExpression
74-- Parses PSCI metacommands or expressions input from the user.
41-- Parses PSCI metacommands or expressions input from the user.
76−parseCommands :: String -> Either ParseError Command
43+parseCommand :: String -> Either ParseError Command
77−parseCommands = psciParser $ choice (map try availableCommands)
44+parseCommand = P.runIndentParser "" $ choice
45+ [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf)
80-- Parses expressions entered at the PSCI repl.
51-- Parses expressions entered at the PSCI repl.
82−psciExpr :: Parsec String P.ParseState Command
53+psciExpression :: Parsec String P.ParseState Command
83−psciExpr = Expression <$> (lookAhead (try (noneOf ":")) *> many1 anyChar)
54+psciExpression = Expression <$> P.parseValue
86-- Parses 'Commands.Help' command.
57-- Parses 'Commands.Help' command.
88psciHelp :: Parsec String P.ParseState Command
59psciHelp :: Parsec String P.ParseState Command
89−psciHelp = Help <$ (spaces *> string ":?" *> spaces)
60+psciHelp = Help <$ char '?'
92-- Parses 'Commands.Import' command.
63-- Parses 'Commands.Import' command.
94psciImport :: Parsec String P.ParseState Command
65psciImport :: Parsec String P.ParseState Command
95−psciImport = Import <$> (spaces *> string ":i" *> spaces *> P.moduleName)
66+psciImport = Import <$> (char 'i' *> P.whiteSpace *> P.moduleName)
98-- Parses 'Commands.LoadFile' command.
69-- Parses 'Commands.LoadFile' command.
100psciLoadFile :: Parsec String P.ParseState Command
71psciLoadFile :: Parsec String P.ParseState Command
101−psciLoadFile = LoadFile <$> do
72+psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> P.whiteSpace *> manyTill anyChar eof)
103− string ":m"
74+ trimEnd = reverse . dropWhile isSpace . reverse
105− try (manyTill anyChar space) <|> many1 anyChar
108-- Parses 'Commands.Quit' command.
77-- Parses 'Commands.Quit' command.
110psciQuit :: Parsec String P.ParseState Command
79psciQuit :: Parsec String P.ParseState Command
111−psciQuit = Quit <$ (spaces *> string ":q" *> spaces)
80+psciQuit = Quit <$ char 'q'
114-- Parses 'Commands.Reload' command.
83-- Parses 'Commands.Reload' command.
116psciReload :: Parsec String P.ParseState Command
85psciReload :: Parsec String P.ParseState Command
117−psciReload = Reload <$ (spaces *> string ":r" *> spaces)
86+psciReload = Reload <$ char 'r'
120-- Parses 'Commands.TypeOf' command.
89-- Parses 'Commands.TypeOf' command.
122psciTypeOf :: Parsec String P.ParseState Command
91psciTypeOf :: Parsec String P.ParseState Command
123−psciTypeOf = TypeOf <$> (spaces *> string ":t" *> spaces *> many anyChar)
92+psciTypeOf = TypeOf <$> (char 't' *> P.whiteSpace *> P.parseValue)
126−-- List of commands that are available from PSCI.
128−availableCommands :: [Parsec String P.ParseState Command]
129−availableCommands = [ psciExpr