In the Compiling/Skipping message, print ModuleNames not Modules
[ghc-hetmet.git] / compiler / main / CmdLineParser.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Command-line parser
4 --
5 -- This is an abstract command-line parser used by both StaticFlags and
6 -- DynFlags.
7 --
8 -- (c) The University of Glasgow 2005
9 --
10 -----------------------------------------------------------------------------
11
12 module CmdLineParser (
13         processArgs, OptKind(..), 
14         CmdLineP(..), getCmdLineState, putCmdLineState
15   ) where
16
17 #include "HsVersions.h"
18
19 import Util     ( maybePrefixMatch, notNull, removeSpaces )
20 #ifdef DEBUG
21 import Panic    ( assertPanic )
22 #endif
23
24 data OptKind m          -- Suppose the flag is -f
25         = NoArg (m ())                  -- -f all by itself
26         | HasArg    (String -> m ())    -- -farg or -f arg
27         | SepArg    (String -> m ())    -- -f arg
28         | Prefix    (String -> m ())    -- -farg 
29         | OptPrefix (String -> m ())    -- -f or -farg (i.e. the arg is optional)
30         | OptIntSuffix (Maybe Int -> m ())      -- -f or -f=n; pass n to fn
31         | PassFlag  (String -> m ())    -- -f; pass "-f" fn
32         | AnySuffix (String -> m ())    -- -f or -farg; pass entire "-farg" to fn
33         | PrefixPred    (String -> Bool) (String -> m ())
34         | AnySuffixPred (String -> Bool) (String -> m ())
35
36 processArgs :: Monad m
37             => [(String, OptKind m)]    -- cmdline parser spec
38             -> [String]                 -- args
39             -> m (
40                 [String],  -- spare args
41                 [String]   -- errors
42                 )
43 processArgs spec args = process spec args [] []
44   where
45     process _spec [] spare errs =
46       return (reverse spare, reverse errs)
47     
48     process spec (dash_arg@('-':arg):args) spare errs =
49       case findArg spec arg of
50         Just (rest,action) -> 
51            case processOneArg action rest arg args of
52              Left err            -> process spec args spare (err:errs)
53              Right (action,rest) -> action >> process spec rest spare errs
54         Nothing -> process spec args (dash_arg:spare) errs
55     
56     process spec (arg:args) spare errs = 
57       process spec args (arg:spare) errs
58
59
60 processOneArg :: OptKind m -> String -> String -> [String]
61               -> Either String (m (), [String])
62 processOneArg action rest arg args 
63   = let dash_arg = '-' : arg
64     in case action of
65         NoArg  a -> ASSERT(null rest) Right (a, args)
66
67         HasArg f | notNull rest -> Right (f rest, args)
68                  | otherwise    -> case args of
69                                     [] -> missingArgErr dash_arg
70                                     (arg1:args1) -> Right (f arg1, args1)
71
72         SepArg f -> case args of
73                         [] -> unknownFlagErr dash_arg
74                         (arg1:args1) -> Right (f arg1, args1)
75
76         Prefix f | notNull rest -> Right (f rest, args)
77                  | otherwise  -> unknownFlagErr dash_arg
78         
79         PrefixPred p f | notNull rest -> Right (f rest, args)
80                        | otherwise    -> unknownFlagErr dash_arg
81         
82         PassFlag f  | notNull rest -> unknownFlagErr dash_arg
83                     | otherwise    -> Right (f dash_arg, args)
84
85         OptIntSuffix f | Just n <- parseInt rest -> Right (f n, args)
86                        | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
87
88         OptPrefix f       -> Right (f rest, args)
89         AnySuffix f       -> Right (f dash_arg, args)
90         AnySuffixPred p f -> Right (f dash_arg, args)
91
92
93
94 findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
95 findArg spec arg
96   = case [ (removeSpaces rest, k) 
97          | (pat,k)   <- spec, 
98            Just rest <- [maybePrefixMatch pat arg],
99            arg_ok k rest arg ] 
100     of
101         []      -> Nothing
102         (one:_) -> Just one
103
104 arg_ok (NoArg _)            rest arg = null rest
105 arg_ok (HasArg _)           rest arg = True
106 arg_ok (SepArg _)           rest arg = null rest
107 arg_ok (Prefix _)           rest arg = notNull rest
108 arg_ok (PrefixPred p _)     rest arg = notNull rest && p rest
109 arg_ok (OptIntSuffix _)     rest arg = True
110 arg_ok (OptPrefix _)        rest arg = True
111 arg_ok (PassFlag _)         rest arg = null rest 
112 arg_ok (AnySuffix _)        rest arg = True
113 arg_ok (AnySuffixPred p _)  rest arg = p arg
114
115 parseInt :: String -> Maybe (Maybe Int)
116 -- Looks for "433" or "=342", with no trailing gubbins
117 --   empty string => Just Nothing
118 --   n or =n      => Just (Just n)
119 --   gibberish    => Nothing
120 parseInt s 
121   | null s    = Just Nothing
122   | otherwise = case reads (dropEq s) of
123                   ((n,""):_) -> Just (Just n)
124                   other      -> Nothing
125
126 dropEq :: String -> String
127 -- Discards a leading equals sign
128 dropEq ('=' : s) = s
129 dropEq s         = s
130
131 unknownFlagErr :: String -> Either String a
132 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
133
134 missingArgErr :: String -> Either String a
135 missingArgErr f = Left ("missing argument for flag: " ++ f)
136
137 -- -----------------------------------------------------------------------------
138 -- A state monad for use in the command-line parser
139
140 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
141
142 instance Monad (CmdLineP s) where
143         return a = CmdLineP $ \s -> (a, s)
144         m >>= k  = CmdLineP $ \s -> let
145                 (a, s') = runCmdLine m s
146                 in runCmdLine (k a) s'
147
148 getCmdLineState   = CmdLineP $ \s -> (s,s)
149 putCmdLineState s = CmdLineP $ \_ -> ((),s)