summaryrefslogtreecommitdiff
path: root/psci/Directive.hs
blob: 366fc66189aa033bedf93084b419da0a17467b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-----------------------------------------------------------------------------
--
-- Module      :  Directive
-- Copyright   :
-- License     :  MIT
--
-- Maintainer  :
-- Stability   :  experimental
-- Portability :
--
-- |
-- Directives for PSCI.
--
-----------------------------------------------------------------------------

module Directive where

import Data.Maybe (fromJust, listToMaybe)
import Data.List (isPrefixOf)
import Data.Tuple (swap)

import Types

-- |
-- List of all avaliable directives.
--
directives :: [Directive]
directives = map fst directiveStrings

-- |
-- A mapping of directives to the different strings that can be used to invoke
-- them.
--
directiveStrings :: [(Directive, [String])]
directiveStrings =
    [ (Help   , ["?", "help"])
    , (Quit   , ["quit"])
    , (Reset  , ["reset"])
    , (Browse , ["browse"])
    , (Load   , ["load", "module"])
    , (Type   , ["type"])
    , (Kind   , ["kind"])
    , (Show   , ["show"])
    ]

-- |
-- Like directiveStrings, but the other way around.
--
directiveStrings' :: [(String, Directive)]
directiveStrings' = concatMap go directiveStrings
  where
  go (dir, strs) = map (\s -> (s, dir)) strs

-- |
-- List of all directive strings.
--
strings :: [String]
strings = concatMap snd directiveStrings

-- |
-- Returns all possible string representations of a directive.
--
stringsFor :: Directive -> [String]
stringsFor d = fromJust (lookup d directiveStrings)

-- |
-- Returns the default string representation of a directive.
--
stringFor :: Directive -> String
stringFor = head . stringsFor

-- |
-- Returns the list of directives which could be expanded from the string
-- argument, together with the string alias that matched.
--
directivesFor' :: String -> [(Directive, String)]
directivesFor' str = go directiveStrings'
  where
  go = map swap . filter ((str `isPrefixOf`) . fst)

directivesFor :: String -> [Directive]
directivesFor = map fst . directivesFor'

directiveStringsFor :: String -> [String]
directiveStringsFor = map snd . directivesFor'

parseDirective :: String -> Maybe Directive
parseDirective = listToMaybe . directivesFor

-- |
-- True if the given directive takes an argument, false otherwise.
hasArgument :: Directive -> Bool
hasArgument Help = False
hasArgument Quit = False
hasArgument Reset = False
hasArgument _ = True

-- |
-- The help menu.
--
help :: [(Directive, String, String)]
help =
  [ (Help,   "",         "Show this help menu")
  , (Quit,   "",         "Quit PSCi")
  , (Reset,  "",         "Discard all imported modules and declared bindings")
  , (Browse, "<module>", "See all functions in <module>")
  , (Load,   "<file>",   "Load <file> for importing")
  , (Type,   "<expr>",   "Show the type of <expr>")
  , (Kind,   "<type>",   "Show the kind of <type>")
  , (Show,   "import",   "Show all imported modules")
  , (Show,   "loaded",   "Show all loaded modules")
  ]