Minor refactoring of placeHolderPunRhs
[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         Flag(..), Deprecated(..),
16         errorsToGhcException
17   ) where
18
19 #include "HsVersions.h"
20
21 import Util
22 import Outputable
23 import Panic
24 import SrcLoc
25
26 import Data.List
27
28 data Flag m = Flag
29     {
30         flagName :: String,           -- flag, without the leading -
31         flagOptKind :: (OptKind m),   -- what to do if we see it
32         flagDeprecated :: Deprecated  -- is the flag deprecated?
33     }
34
35 data Deprecated = Supported | Deprecated String
36
37 data OptKind m                      -- Suppose the flag is -f
38  = NoArg (m ())                     -- -f all by itself
39  | HasArg    (String -> m ())       -- -farg or -f arg
40  | SepArg    (String -> m ())       -- -f arg
41  | Prefix    (String -> m ())       -- -farg
42  | OptPrefix (String -> m ())       -- -f or -farg (i.e. the arg is optional)
43  | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
44  | IntSuffix (Int -> m ())          -- -f or -f=n; pass n to fn
45  | PassFlag  (String -> m ())       -- -f; pass "-f" fn
46  | AnySuffix (String -> m ())       -- -f or -farg; pass entire "-farg" to fn
47  | PrefixPred    (String -> Bool) (String -> m ())
48  | AnySuffixPred (String -> Bool) (String -> m ())
49
50 processArgs :: Monad m
51             => [Flag m] -- cmdline parser spec
52             -> [Located String]      -- args
53             -> m (
54                   [Located String],  -- spare args
55                   [Located String],  -- errors
56                   [Located String]   -- warnings
57                  )
58 processArgs spec args = process spec args [] [] []
59   where
60     process _spec [] spare errs warns =
61       return (reverse spare, reverse errs, reverse warns)
62
63     process spec (locArg@(L loc dash_arg@('-' : arg)) : args) spare errs warns =
64       case findArg spec arg of
65         Just (rest, action, deprecated) ->
66            let warns' = case deprecated of
67                         Deprecated warning ->
68                             L loc ("Warning: " ++ dash_arg ++ " is deprecated: " ++ warning) : warns
69                         Supported -> warns
70            in case processOneArg action rest arg args of
71               Left err            -> process spec args spare (L loc err : errs) warns'
72               Right (action,rest) -> do action
73                                         process spec rest spare errs warns'
74         Nothing -> process spec args (locArg : spare) errs warns
75
76     process spec (arg : args) spare errs warns =
77       process spec args (arg : spare) errs warns
78
79
80 processOneArg :: OptKind m -> String -> String -> [Located String]
81               -> Either String (m (), [Located String])
82 processOneArg action rest arg args
83   = let dash_arg = '-' : arg
84         rest_no_eq = dropEq rest
85     in case action of
86         NoArg  a -> ASSERT(null rest) Right (a, args)
87
88         HasArg f | notNull rest_no_eq -> Right (f rest_no_eq, args)
89                  | otherwise    -> case args of
90                                     [] -> missingArgErr dash_arg
91                                     (L _ arg1:args1) -> Right (f arg1, args1)
92
93         SepArg f -> case args of
94                         [] -> unknownFlagErr dash_arg
95                         (L _ arg1:args1) -> Right (f arg1, args1)
96
97         Prefix f | notNull rest_no_eq -> Right (f rest_no_eq, args)
98                  | otherwise  -> unknownFlagErr dash_arg
99
100         PrefixPred _ f | notNull rest_no_eq -> Right (f rest_no_eq, args)
101                        | otherwise          -> unknownFlagErr dash_arg
102
103         PassFlag f  | notNull rest -> unknownFlagErr dash_arg
104                     | otherwise    -> Right (f dash_arg, args)
105
106         OptIntSuffix f | null rest                     -> Right (f Nothing,  args)
107                        | Just n <- parseInt rest_no_eq -> Right (f (Just n), args)
108                        | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
109
110         IntSuffix f | Just n <- parseInt rest_no_eq -> Right (f n, args)
111                     | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
112
113         OptPrefix f       -> Right (f rest_no_eq, args)
114         AnySuffix f       -> Right (f dash_arg, args)
115         AnySuffixPred _ f -> Right (f dash_arg, args)
116
117
118 findArg :: [Flag m] -> String -> Maybe (String, OptKind m, Deprecated)
119 findArg spec arg
120   = case [ (removeSpaces rest, optKind, flagDeprecated flag)
121          | flag <- spec,
122            let optKind = flagOptKind flag,
123            Just rest <- [stripPrefix (flagName flag) arg],
124            arg_ok optKind rest arg ]
125     of
126         []      -> Nothing
127         (one:_) -> Just one
128
129 arg_ok :: OptKind t -> [Char] -> String -> Bool
130 arg_ok (NoArg _)            rest _   = null rest
131 arg_ok (HasArg _)           _    _   = True
132 arg_ok (SepArg _)           rest _   = null rest
133 arg_ok (Prefix _)           rest _   = notNull rest
134 arg_ok (PrefixPred p _)     rest _   = notNull rest && p (dropEq rest)
135 arg_ok (OptIntSuffix _)     _    _   = True
136 arg_ok (IntSuffix _)        _    _   = True
137 arg_ok (OptPrefix _)        _    _   = True
138 arg_ok (PassFlag _)         rest _   = null rest
139 arg_ok (AnySuffix _)        _    _   = True
140 arg_ok (AnySuffixPred p _)  _    arg = p arg
141
142 parseInt :: String -> Maybe Int
143 -- Looks for "433" or "=342", with no trailing gubbins
144 --   n or =n      => Just n
145 --   gibberish    => Nothing
146 parseInt s = case reads s of
147                 ((n,""):_) -> Just n
148                 _          -> Nothing
149
150 dropEq :: String -> String
151 -- Discards a leading equals sign
152 dropEq ('=' : s) = s
153 dropEq s         = s
154
155 unknownFlagErr :: String -> Either String a
156 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
157
158 missingArgErr :: String -> Either String a
159 missingArgErr f = Left ("missing argument for flag: " ++ f)
160
161 -- -----------------------------------------------------------------------------
162 -- A state monad for use in the command-line parser
163
164 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
165
166 instance Monad (CmdLineP s) where
167         return a = CmdLineP $ \s -> (a, s)
168         m >>= k  = CmdLineP $ \s -> let
169                 (a, s') = runCmdLine m s
170                 in runCmdLine (k a) s'
171
172 getCmdLineState :: CmdLineP s s
173 getCmdLineState   = CmdLineP $ \s -> (s,s)
174 putCmdLineState :: s -> CmdLineP s ()
175 putCmdLineState s = CmdLineP $ \_ -> ((),s)
176
177 -- ---------------------------------------------------------------------
178 -- Utils
179
180 errorsToGhcException :: [Located String] -> GhcException
181 errorsToGhcException errs =
182    let errors = vcat [ ppr l <> text ": " <> text e | L l e <- errs ]
183    in UsageError (showSDoc errors)
184