Skip to content

Commit 6291fa0

Browse files
committed
Refactor PSCI parser module to use new directive utilities
1 parent 7f5b3fa commit 6291fa0

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

psci/Parser.hs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ module Parser (
1919

2020
import Prelude hiding (lex)
2121

22-
import Commands
22+
import qualified Commands as C
23+
import qualified Directive as D
2324

2425
import Data.Char (isSpace)
25-
import Data.List (isPrefixOf)
2626

2727
import Control.Applicative hiding (many)
2828

@@ -34,24 +34,11 @@ import qualified Language.PureScript.Parser.Common as C (mark, same)
3434
-- |
3535
-- Parses PSCI metacommands or expressions input from the user.
3636
--
37-
parseCommand :: String -> Either String Command
37+
parseCommand :: String -> Either String C.Command
3838
parseCommand cmdString =
39-
case splitCommand cmdString of
40-
Just (cmd, arg)
41-
| matches "?" -> return Help
42-
| matches "help" -> return Help
43-
| matches "quit" -> return Quit
44-
| matches "reset" -> return Reset
45-
| matches "import" -> Import <$> parseRest P.moduleName arg
46-
| matches "browse" -> Browse <$> parseRest P.moduleName arg
47-
| matches "module" -> return $ LoadFile (trimEnd arg)
48-
| matches "show" -> return $ Show (trimEnd cmd)
49-
| matches "type" -> TypeOf <$> parseRest P.parseValue arg
50-
| matches "kind" -> KindOf <$> parseRest P.parseType arg
51-
| otherwise -> Left $ "Unrecognized command. Type :? for help."
52-
where
53-
matches = isPrefixOf cmd
54-
Nothing -> parseRest (psciLet <|> psciExpression) cmdString
39+
case cmdString of
40+
(':' : cmd) -> parseDirective cmd
41+
_ -> parseRest (psciLet <|> psciExpression) cmdString
5542
where
5643
parseRest :: P.TokenParser a -> String -> Either String a
5744
parseRest p s = either (Left . show) Right $ do
@@ -64,29 +51,35 @@ parseCommand cmdString =
6451
trimEnd :: String -> String
6552
trimEnd = reverse . trimStart . reverse
6653

67-
-- |
68-
-- Tries to split a command into a directive and the argument.
69-
--
70-
splitCommand :: String -> Maybe (String, String)
71-
splitCommand (':' : cmd) = Just (directive, trimStart arg)
72-
where
73-
(directive, arg) = break isSpace cmd
74-
splitCommand _ = Nothing
54+
parseDirective :: String -> Either String C.Command
55+
parseDirective cmd =
56+
case D.parseDirective dstr of
57+
Just D.Help -> return C.Help
58+
Just D.Quit -> return C.Quit
59+
Just D.Reset -> return C.Reset
60+
Just D.Import -> C.Import <$> parseRest P.moduleName arg
61+
Just D.Browse -> C.Browse <$> parseRest P.moduleName arg
62+
Just D.Load -> return $ C.LoadFile (trimEnd arg)
63+
Just D.Show -> return $ C.Show (trimEnd arg)
64+
Just D.Type -> C.TypeOf <$> parseRest P.parseValue arg
65+
Just D.Kind -> C.KindOf <$> parseRest P.parseType arg
66+
Nothing -> Left $ "Unrecognized command. Type :? for help."
67+
where (dstr, arg) = break isSpace cmd
7568

7669
-- |
7770
-- Parses expressions entered at the PSCI repl.
7871
--
79-
psciExpression :: P.TokenParser Command
80-
psciExpression = Expression <$> P.parseValue
72+
psciExpression :: P.TokenParser C.Command
73+
psciExpression = C.Expression <$> P.parseValue
8174

8275
-- |
8376
-- PSCI version of @let@.
8477
-- This is essentially let from do-notation.
8578
-- However, since we don't support the @Eff@ monad,
8679
-- we actually want the normal @let@.
8780
--
88-
psciLet :: P.TokenParser Command
89-
psciLet = Let <$> (P.reserved "let" *> P.indented *> manyDecls)
81+
psciLet :: P.TokenParser C.Command
82+
psciLet = C.Let <$> (P.reserved "let" *> P.indented *> manyDecls)
9083
where
9184
manyDecls :: P.TokenParser [P.Declaration]
9285
manyDecls = C.mark (many1 (C.same *> P.parseDeclaration))

0 commit comments

Comments
 (0)