git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Refactor PSCI parser module to use new directive utilities
6291fa0Łukasz Hanuszczak
psci/Parser.hs+24−31
@@ -19,10 +19,10 @@ module Parser (
19
19
20import Prelude hiding (lex)
20import Prelude hiding (lex)
21
21
22−import Commands
22+import qualified Commands as C
23+import qualified Directive as D
23
24
24import Data.Char (isSpace)
25import Data.Char (isSpace)
25−import Data.List (isPrefixOf)
26
26
27import Control.Applicative hiding (many)
27import Control.Applicative hiding (many)
28
28
@@ -34,24 +34,11 @@ import qualified Language.PureScript.Parser.Common as C (mark, same)
34-- |
34-- |
35-- Parses PSCI metacommands or expressions input from the user.
35-- Parses PSCI metacommands or expressions input from the user.
36--
36--
37−parseCommand :: String -> Either String Command
37+parseCommand :: String -> Either String C.Command
38parseCommand cmdString =
38parseCommand cmdString =
39− case splitCommand cmdString of
39+ case cmdString of
40− Just (cmd, arg)
40+ (':' : cmd) -> parseDirective cmd
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
41+ _ -> parseRest (psciLet <|> psciExpression) cmdString
55 where
42 where
56 parseRest :: P.TokenParser a -> String -> Either String a
43 parseRest :: P.TokenParser a -> String -> Either String a
57 parseRest p s = either (Left . show) Right $ do
44 parseRest p s = either (Left . show) Right $ do
@@ -64,20 +51,26 @@ parseCommand cmdString =
64 trimEnd :: String -> String
51 trimEnd :: String -> String
65 trimEnd = reverse . trimStart . reverse
52 trimEnd = reverse . trimStart . reverse
66
53
67− -- |
54+ parseDirective :: String -> Either String C.Command
68− -- Tries to split a command into a directive and the argument.
55+ parseDirective cmd =
69− --
56+ case D.parseDirective dstr of
70− splitCommand :: String -> Maybe (String, String)
57+ Just D.Help -> return C.Help
71− splitCommand (':' : cmd) = Just (directive, trimStart arg)
58+ Just D.Quit -> return C.Quit
72− where
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."
73− (directive, arg) = break isSpace cmd
67+ where (dstr, arg) = break isSpace cmd
74− splitCommand _ = Nothing
75
68
76 -- |
69 -- |
77 -- Parses expressions entered at the PSCI repl.
70 -- Parses expressions entered at the PSCI repl.
78 --
71 --
79− psciExpression :: P.TokenParser Command
72+ psciExpression :: P.TokenParser C.Command
80− psciExpression = Expression <$> P.parseValue
73+ psciExpression = C.Expression <$> P.parseValue
81
74
82 -- |
75 -- |
83 -- PSCI version of @let@.
76 -- PSCI version of @let@.
@@ -85,8 +78,8 @@ parseCommand cmdString =
85 -- However, since we don't support the @Eff@ monad,
78 -- However, since we don't support the @Eff@ monad,
86 -- we actually want the normal @let@.
79 -- we actually want the normal @let@.
87 --
80 --
88− psciLet :: P.TokenParser Command
81+ psciLet :: P.TokenParser C.Command
89− psciLet = Let <$> (P.reserved "let" *> P.indented *> manyDecls)
82+ psciLet = C.Let <$> (P.reserved "let" *> P.indented *> manyDecls)
90 where
83 where
91 manyDecls :: P.TokenParser [P.Declaration]
84 manyDecls :: P.TokenParser [P.Declaration]
92 manyDecls = C.mark (many1 (C.same *> P.parseDeclaration))
85 manyDecls = C.mark (many1 (C.same *> P.parseDeclaration))