git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Expression no longer consumes colons.
commit4b9abe3b721f91693c336a6732f55cb42bcb96e3 authorjoneshf <jones3.hardy@gmail.com> dateFri 21 Feb 2014, 15:03 (13 years ago) parent558d43d
3 files changed+33−50
psci/Commands.hs+1−25 psci/Main.hs+19−18 psci/Parser.hs+13−7
psci/Commands.hs+1−25
@@ -30,7 +30,7 @@ data Command
3030 -- |
3131 -- A purescript expression
3232 --
33− | Expression [String]
33+ | Expression String
3434 -- |
3535 -- Show the help command
3636 --
@@ -60,30 +60,6 @@ data Command
6060 --
6161 | Unknown ParseError
6262
63−---- |
64−---- Parses the input and returns either a Metacommand or an expression.
65−----
66−--getCommand :: InputT IO Command
67−--getCommand = do
68−-- firstLine <- getInputLine "> "
69−-- case firstLine of
70−-- Nothing -> return Empty
71−-- Just line -> psciParser psciHelp
72−-- --Just ":?" -> return Help
73−-- --Just (':':'i':' ':moduleName) -> return $ Import moduleName
74−-- --Just (':':'m':' ':filePath) -> return $ LoadFile filePath
75−-- --Just ":q" -> return Quit
76−-- --Just ":r" -> return Reload
77−-- --Just (':':'t':' ':expr) -> return $ TypeOf expr
78−-- --Just (':':_) -> return Unknown
79−-- --Just other -> Expression <$> go [other]
80−-- where
81−-- go ls = do
82−-- l <- getInputLine " "
83−-- case l of
84−-- Nothing -> return $ reverse ls
85−-- Just l' -> go (l' : ls)
86−
8763-- |
8864-- The help menu.
8965--
psci/Main.hs+19−18
@@ -238,13 +238,31 @@ handleTypeOf value (PSCI imports loadedModules lets) = do
238238
239239-- Commands
240240
241+-- |
242+-- Parses the input and returns either a Metacommand or an expression.
243+--
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 =
257+ maybe (return . unlines $ reverse ls) (go . (:ls)) =<< getInputLine " "
258+
241259-- |
242260-- Performs an action for each meta-command given, and also for expressions..
243261--
244262handleCommand :: Command -> StateT PSCI (InputT IO) ()
245263handleCommand Empty = return ()
246264handleCommand (Expression ls) =
247− case psciParser (choice [Left <$> psciLet, Right <$> psciExpression]) (unlines ls) of
265+ case psciParser (choice [Left <$> psciLet, Right <$> psciExpression]) ls of
248266 Left err -> inputTToState $ outputStrLn (show err)
249267 Right (Left l) -> modify (updateLets l)
250268 Right (Right decl) -> get >>= inputTToState . handleDeclaration decl
@@ -299,20 +317,3 @@ main = do
299317 case c of
300318 Quit -> outputTStrLn quitMessage
301319 _ -> handleCommand c >> go
302−
303−-- |
304−-- Parses the input and returns either a Metacommand or an expression.
305−--
306−getCommand :: InputT IO Command
307−getCommand = do
308− firstLine <- getInputLine "> "
309− case firstLine of
310− Nothing -> return Empty
311− Just line -> either (return . Unknown) (lift . return) (parseCommands line)
312− --Just other -> Expression <$> go [other]
313− --where
314− --go ls = do
315− -- l <- getInputLine " "
316− -- case l of
317− -- Nothing -> return $ reverse ls
318− -- Just l' -> go (l' : ls)
psci/Parser.hs+13−7
@@ -32,7 +32,8 @@ psciModules = P.parseModules
3232-- |
3333-- PSCI version of 'let'.
3434-- This is essentially let from do-notation.
35−-- However, since we don't support the 'Eff' monad, we actually want the normal 'let'.
35+-- However, since we don't support the 'Eff' monad,
36+-- we actually want the normal 'let'.
3637--
3738psciLet :: Parsec String P.ParseState (P.Value -> P.Value)
3839psciLet = P.Let <$> (P.reserved "let" *> P.indented *> P.parseBinder)
@@ -72,30 +73,35 @@ parseExpression = psciParser psciExpression
7273parseCommands :: String -> Either ParseError Command
7374parseCommands = psciParser $ choice availableCommands
7475
76+psciExpr :: Parsec String P.ParseState Command
77+psciExpr = Expression <$> (lookAhead (try (satisfy (/= ':'))) *> many1 anyChar)
78+
7579psciHelp :: Parsec String P.ParseState Command
76−psciHelp = Help <$ (string ":?" *> spaces)
80+psciHelp = Help <$ (spaces *> string ":?" *> spaces)
7781
7882psciImport :: Parsec String P.ParseState Command
79−psciImport = Import <$> (string ":i" *> spaces *> P.properName)
83+psciImport = Import <$> (spaces *> string ":i" *> spaces *> P.properName)
8084
8185psciLoadFile :: Parsec String P.ParseState Command
8286psciLoadFile = LoadFile <$> do
87+ spaces
8388 string ":m"
8489 spaces
8590 try (manyTill anyChar space) <|> many1 anyChar
8691
8792psciQuit :: Parsec String P.ParseState Command
88−psciQuit = Quit <$ (string ":q" *> spaces)
93+psciQuit = Quit <$ (spaces *> string ":q" *> spaces)
8994
9095psciReload :: Parsec String P.ParseState Command
91−psciReload = Reload <$ (string ":r" *> spaces)
96+psciReload = Reload <$ (spaces *> string ":r" *> spaces)
9297
9398psciTypeOf :: Parsec String P.ParseState Command
94−psciTypeOf = TypeOf <$> (string ":t" *> spaces *> many anyChar)
99+psciTypeOf = TypeOf <$> (spaces *> string ":t" *> spaces *> many anyChar)
95100
96101availableCommands :: [Parsec String P.ParseState Command]
97102availableCommands =
98− map try [ psciHelp
103+ map try [ psciExpr
104+ , psciHelp
99105 , psciImport
100106 , psciLoadFile
101107 , psciQuit