git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Started parsing commands.
55f98d9joneshf
psci/Commands.hs+28−26
@@ -15,8 +15,9 @@
15
15
16module Commands where
16module Commands where
17
17
18−import Control.Applicative
18+import Language.PureScript
19−import System.Console.Haskeline
19+
20+import Text.Parsec
20
21
21-- |
22-- |
22-- Valid Meta-commands for PSCI
23-- Valid Meta-commands for PSCI
@@ -37,7 +38,7 @@ data Command
37 -- |
38 -- |
38 -- Import a module from a loaded file
39 -- Import a module from a loaded file
39 --
40 --
40− | Import String
41+ | Import ProperName
41 -- |
42 -- |
42 -- Load a file for use with importing
43 -- Load a file for use with importing
43 --
44 --
@@ -57,30 +58,31 @@ data Command
57 -- |
58 -- |
58 -- An attempt at a meta command that wasn't recognized.
59 -- An attempt at a meta command that wasn't recognized.
59 --
60 --
60− | Unknown deriving (Show, Eq)
61+ | Unknown ParseError
61
62
62−-- |
63+---- |
63−-- Parses the input and returns either a Metacommand or an expression.
64+---- Parses the input and returns either a Metacommand or an expression.
64−--
65+----
65−getCommand :: InputT IO Command
66+--getCommand :: InputT IO Command
66−getCommand = do
67+--getCommand = do
67− firstLine <- getInputLine "> "
68+-- firstLine <- getInputLine "> "
68− case firstLine of
69+-- case firstLine of
69− Nothing -> return Empty
70+-- Nothing -> return Empty
71+-- Just line -> psciParser psciHelp
70− Just ":?" -> return Help
72+-- --Just ":?" -> return Help
71− Just (':':'i':' ':moduleName) -> return $ Import moduleName
73+-- --Just (':':'i':' ':moduleName) -> return $ Import moduleName
72− Just (':':'m':' ':filePath) -> return $ LoadFile filePath
74+-- --Just (':':'m':' ':filePath) -> return $ LoadFile filePath
73− Just ":q" -> return Quit
75+-- --Just ":q" -> return Quit
74− Just ":r" -> return Reload
76+-- --Just ":r" -> return Reload
75− Just (':':'t':' ':expr) -> return $ TypeOf expr
77+-- --Just (':':'t':' ':expr) -> return $ TypeOf expr
76− Just (':':_) -> return Unknown
78+-- --Just (':':_) -> return Unknown
77− Just other -> Expression <$> go [other]
79+-- --Just other -> Expression <$> go [other]
78− where
80+-- where
79− go ls = do
81+-- go ls = do
80− l <- getInputLine " "
82+-- l <- getInputLine " "
81− case l of
83+-- case l of
82− Nothing -> return $ reverse ls
84+-- Nothing -> return $ reverse ls
83− Just l' -> go (l' : ls)
85+-- Just l' -> go (l' : ls)
84
86
85-- |
87-- |
86-- The help menu.
88-- The help menu.
psci/Main.hs+24−2
@@ -72,8 +72,8 @@ ioToState = lift . lift
72-- |
72-- |
73-- Updates the state to have more imported modules.
73-- Updates the state to have more imported modules.
74--
74--
75−updateImports :: String -> PSCI -> PSCI
75+updateImports :: P.ProperName -> PSCI -> PSCI
76−updateImports name (PSCI i m b) = PSCI (i ++ [P.ProperName name]) m b
76+updateImports name (PSCI i m b) = PSCI (i ++ [name]) m b
77
77
78-- |
78-- |
79-- Updates the state to have more loaded files.
79-- Updates the state to have more loaded files.
@@ -299,3 +299,25 @@ main = do
299 case c of
299 case c of
300 Quit -> outputTStrLn quitMessage
300 Quit -> outputTStrLn quitMessage
301 _ -> handleCommand c >> go
301 _ -> 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 (parseCommands line)
312+ --Just (':':'m':' ':filePath) -> return $ LoadFile filePath
313+ --Just ":q" -> return Quit
314+ --Just ":r" -> return Reload
315+ --Just (':':'t':' ':expr) -> return $ TypeOf expr
316+ --Just (':':_) -> return Unknown
317+ --Just other -> Expression <$> go [other]
318+ where
319+ go ls = do
320+ l <- getInputLine " "
321+ case l of
322+ Nothing -> return $ reverse ls
323+ Just l' -> go (l' : ls)
psci/Parser.hs+17
@@ -15,6 +15,8 @@
15
15
16module Parser where
16module Parser where
17
17
18+import Commands
19+
18import Control.Applicative
20import Control.Applicative
19
21
20import Text.Parsec hiding ((<|>))
22import Text.Parsec hiding ((<|>))
@@ -66,3 +68,18 @@ parseLet = psciParser psciLet
66--
68--
67parseExpression :: String -> Either ParseError P.Value
69parseExpression :: String -> Either ParseError P.Value
68parseExpression = psciParser psciExpression
70parseExpression = psciParser psciExpression
71+
72+parseCommands :: String -> Either ParseError (IO Command)
73+parseCommands = either Left (Right . return) . psciParser (choice availableCommands)
74+
75+psciHelp :: Parsec String P.ParseState Command
76+psciHelp = Help <$ (string ":?" >> spaces)
77+
78+psciImport :: Parsec String P.ParseState Command
79+psciImport = Import <$> (string ":i" >> spaces >> P.properName)
80+
81+availableCommands :: [Parsec String P.ParseState Command]
82+availableCommands =
83+ map try [ psciHelp
84+ , psciImport
85+ ]