@@ -13,124 +13,80 @@
1313--
1414-----------------------------------------------------------------------------
1515
16−module Parser where
16+module Parser (
17+ parseCommand
18+ ) where
1719
1820import Commands
1921
22+import Data.Char (isSpace)
23+
2024import Control.Applicative hiding (many)
2125
2226import Text.Parsec hiding ((<|>))
2327
2428import qualified Language.PureScript as P
2529
26−-- |
27−-- PSCI version of modules
28−--
29−psciModules :: Parsec String P.ParseState [P.Module]
30−psciModules = P.parseModules
31−
3230-- |
3331-- PSCI version of @let@.
3432-- This is essentially let from do-notation.
3533-- However, since we don't support the @Eff@ monad,
3634-- we actually want the normal @let@.
3735--
38−psciLet :: Parsec String P.ParseState (P.Value -> P.Value)
39−psciLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
40− <*> (P.indented *> P.reservedOp "=" *> P.parseValue)
41−
42−-- |
43−-- PSCI version of any other valid expression.
44−--
45−psciExpression :: Parsec String P.ParseState P.Value
46−psciExpression = P.whiteSpace *> P.parseValue <* eof
47−
48−-- |
49−-- Parser for PSCI.
50−-- This is really just a wrapper around 'Language.PureScript.runIndentParser'
51−--
52−psciParser :: Parsec String P.ParseState a -> String -> Either ParseError a
53−psciParser = P.runIndentParser ""
54−
55−-- |
56−-- Parses PSCI modules.
57−--
58−parseModules :: String -> Either ParseError [P.Module]
59−parseModules = psciParser psciModules
60−
61−-- |
62−-- Parses PSCI @let@ bindings.
63−--
64−parseLet :: String -> Either ParseError (P.Value -> P.Value)
65−parseLet = psciParser psciLet
66−
67−-- |
68−-- Parses PSCI @expressions@.
69−--
70−parseExpression :: String -> Either ParseError P.Value
71−parseExpression = psciParser psciExpression
36+psciLet :: Parsec String P.ParseState Command
37+psciLet = Let <$> (P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
38+ <*> (P.indented *> P.reservedOp "=" *> P.parseValue))
7239
7340-- |
7441-- Parses PSCI metacommands or expressions input from the user.
7542--
76−parseCommands :: String -> Either ParseError Command
77−parseCommands = psciParser $ choice (map try availableCommands)
43+parseCommand :: String -> Either ParseError Command
44+parseCommand = P.runIndentParser "" $ choice
45+ [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf)
46+ , try psciLet
47+ , psciExpression
48+ ] <* eof
7849
7950-- |
8051-- Parses expressions entered at the PSCI repl.
8152--
82−psciExpr :: Parsec String P.ParseState Command
83−psciExpr = Expression <$> (lookAhead (try (noneOf ":")) *> many1 anyChar)
53+psciExpression :: Parsec String P.ParseState Command
54+psciExpression = Expression <$> P.parseValue
8455
8556-- |
8657-- Parses 'Commands.Help' command.
8758--
8859psciHelp :: Parsec String P.ParseState Command
89−psciHelp = Help <$ (spaces *> string ":?" *> spaces)
60+psciHelp = Help <$ char '?'
9061
9162-- |
9263-- Parses 'Commands.Import' command.
9364--
9465psciImport :: Parsec String P.ParseState Command
95−psciImport = Import <$> (spaces *> string ":i" *> spaces *> P.moduleName)
66+psciImport = Import <$> (char 'i' *> P.whiteSpace *> P.moduleName)
9667
9768-- |
9869-- Parses 'Commands.LoadFile' command.
9970--
10071psciLoadFile :: Parsec String P.ParseState Command
101−psciLoadFile = LoadFile <$> do
102− spaces
103− string ":m"
104− spaces
105− try (manyTill anyChar space) <|> many1 anyChar
72+psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> P.whiteSpace *> manyTill anyChar eof)
73+ where
74+ trimEnd = reverse . dropWhile isSpace . reverse
10675
10776-- |
10877-- Parses 'Commands.Quit' command.
10978--
11079psciQuit :: Parsec String P.ParseState Command
111−psciQuit = Quit <$ (spaces *> string ":q" *> spaces)
80+psciQuit = Quit <$ char 'q'
11281
11382-- |
11483-- Parses 'Commands.Reload' command.
11584--
11685psciReload :: Parsec String P.ParseState Command
117−psciReload = Reload <$ (spaces *> string ":r" *> spaces)
86+psciReload = Reload <$ char 'r'
11887
11988-- |
12089-- Parses 'Commands.TypeOf' command.
12190--
12291psciTypeOf :: Parsec String P.ParseState Command
123−psciTypeOf = TypeOf <$> (spaces *> string ":t" *> spaces *> many anyChar)
124−
125−-- |
126−-- List of commands that are available from PSCI.
127−--
128−availableCommands :: [Parsec String P.ParseState Command]
129−availableCommands = [ psciExpr
130− , psciHelp
131− , psciImport
132− , psciLoadFile
133− , psciQuit
134− , psciReload
135− , psciTypeOf
136− ]
92+psciTypeOf = TypeOf <$> (char 't' *> P.whiteSpace *> P.parseValue)