Fix a bug to do with recursive modules in one-shot mode
[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
20 import Panic
21
22 data OptKind m                      -- Suppose the flag is -f
23  = NoArg (m ())                     -- -f all by itself
24  | HasArg    (String -> m ())       -- -farg or -f arg
25  | SepArg    (String -> m ())       -- -f arg
26  | Prefix    (String -> m ())       -- -farg
27  | OptPrefix (String -> m ())       -- -f or -farg (i.e. the arg is optional)
28  | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
29  | IntSuffix (Int -> m ())          -- -f or -f=n; pass n to fn
30  | PassFlag  (String -> m ())       -- -f; pass "-f" fn
31  | AnySuffix (String -> m ())       -- -f or -farg; pass entire "-farg" to fn
32  | PrefixPred    (String -> Bool) (String -> m ())
33  | AnySuffixPred (String -> Bool) (String -> m ())
34
35 processArgs :: Monad m
36             => [(String, OptKind m)] -- cmdline parser spec
37             -> [String]              -- args
38             -> m (
39                   [String],  -- spare args
40                   [String]   -- errors
41                  )
42 processArgs spec args = process spec args [] []
43   where
44     process _spec [] spare errs =
45       return (reverse spare, reverse errs)
46
47     process spec (dash_arg@('-':arg):args) spare errs =
48       case findArg spec arg of
49         Just (rest,action) ->
50            case processOneArg action rest arg args of
51              Left err            -> process spec args spare (err:errs)
52              Right (action,rest) -> action >> process spec rest spare errs
53         Nothing -> process spec args (dash_arg:spare) errs
54
55     process spec (arg:args) spare errs =
56       process spec args (arg:spare) errs
57
58
59 processOneArg :: OptKind m -> String -> String -> [String]
60               -> Either String (m (), [String])
61 processOneArg action rest arg args
62   = let dash_arg = '-' : arg
63         rest_no_eq = dropEq rest
64     in case action of
65         NoArg  a -> ASSERT(null rest) Right (a, args)
66
67         HasArg f | notNull rest_no_eq -> Right (f rest_no_eq, 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_no_eq -> Right (f rest_no_eq, args)
77                  | otherwise  -> unknownFlagErr dash_arg
78
79         PrefixPred _ f | notNull rest_no_eq -> Right (f rest_no_eq, 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 | null rest                     -> Right (f Nothing,  args)
86                        | Just n <- parseInt rest_no_eq -> Right (f (Just n), args)
87                        | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
88
89         IntSuffix f | Just n <- parseInt rest_no_eq -> Right (f n, args)
90                     | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
91
92         OptPrefix f       -> Right (f rest_no_eq, args)
93         AnySuffix f       -> Right (f dash_arg, args)
94         AnySuffixPred _ f -> Right (f dash_arg, args)
95
96
97 findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
98 findArg spec arg
99   = case [ (removeSpaces rest, k)
100          | (pat,k)   <- spec,
101            Just rest <- [maybePrefixMatch pat arg],
102            arg_ok k rest arg ]
103     of
104         []      -> Nothing
105         (one:_) -> Just one
106
107 arg_ok :: OptKind t -> [Char] -> String -> Bool
108 arg_ok (NoArg _)            rest _   = null rest
109 arg_ok (HasArg _)           _    _   = True
110 arg_ok (SepArg _)           rest _   = null rest
111 arg_ok (Prefix _)           rest _   = notNull rest
112 arg_ok (PrefixPred p _)     rest _   = notNull rest && p (dropEq rest)
113 arg_ok (OptIntSuffix _)     _    _   = True
114 arg_ok (IntSuffix _)        _    _   = True
115 arg_ok (OptPrefix _)        _    _   = True
116 arg_ok (PassFlag _)         rest _   = null rest
117 arg_ok (AnySuffix _)        _    _   = True
118 arg_ok (AnySuffixPred p _)  _    arg = p arg
119
120 parseInt :: String -> Maybe Int
121 -- Looks for "433" or "=342", with no trailing gubbins
122 --   n or =n      => Just n
123 --   gibberish    => Nothing
124 parseInt s = case reads s of
125                 ((n,""):_) -> Just n
126                 _          -> Nothing
127
128 dropEq :: String -> String
129 -- Discards a leading equals sign
130 dropEq ('=' : s) = s
131 dropEq s         = s
132
133 unknownFlagErr :: String -> Either String a
134 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
135
136 missingArgErr :: String -> Either String a
137 missingArgErr f = Left ("missing argument for flag: " ++ f)
138
139 -- -----------------------------------------------------------------------------
140 -- A state monad for use in the command-line parser
141
142 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
143
144 instance Monad (CmdLineP s) where
145         return a = CmdLineP $ \s -> (a, s)
146         m >>= k  = CmdLineP $ \s -> let
147                 (a, s') = runCmdLine m s
148                 in runCmdLine (k a) s'
149
150 getCmdLineState :: CmdLineP s s
151 getCmdLineState   = CmdLineP $ \s -> (s,s)
152 putCmdLineState :: s -> CmdLineP s ()
153 putCmdLineState s = CmdLineP $ \_ -> ((),s)