Improve command-line parser (add OptIntSuffix); make -fliberate-case-threshold dynamic
[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         | IntSuffix (Int -> m ())               -- -f or -f=n; pass n to fn
32         | PassFlag  (String -> m ())    -- -f; pass "-f" fn
33         | AnySuffix (String -> m ())    -- -f or -farg; pass entire "-farg" to fn
34         | PrefixPred    (String -> Bool) (String -> m ())
35         | AnySuffixPred (String -> Bool) (String -> m ())
36
37 processArgs :: Monad m
38             => [(String, OptKind m)]    -- cmdline parser spec
39             -> [String]                 -- args
40             -> m (
41                 [String],  -- spare args
42                 [String]   -- errors
43                 )
44 processArgs spec args = process spec args [] []
45   where
46     process _spec [] spare errs =
47       return (reverse spare, reverse errs)
48     
49     process spec (dash_arg@('-':arg):args) spare errs =
50       case findArg spec arg of
51         Just (rest,action) -> 
52            case processOneArg action rest arg args of
53              Left err            -> process spec args spare (err:errs)
54              Right (action,rest) -> action >> process spec rest spare errs
55         Nothing -> process spec args (dash_arg:spare) errs
56     
57     process spec (arg:args) spare errs = 
58       process spec args (arg:spare) errs
59
60
61 processOneArg :: OptKind m -> String -> String -> [String]
62               -> Either String (m (), [String])
63 processOneArg action rest arg args 
64   = let dash_arg = '-' : arg
65     in case action of
66         NoArg  a -> ASSERT(null rest) Right (a, args)
67
68         HasArg f | notNull rest -> Right (f rest, args)
69                  | otherwise    -> case args of
70                                     [] -> missingArgErr dash_arg
71                                     (arg1:args1) -> Right (f arg1, args1)
72
73         SepArg f -> case args of
74                         [] -> unknownFlagErr dash_arg
75                         (arg1:args1) -> Right (f arg1, args1)
76
77         Prefix f | notNull rest -> Right (f rest, args)
78                  | otherwise  -> unknownFlagErr dash_arg
79         
80         PrefixPred p f | notNull rest -> Right (f rest, args)
81                        | otherwise    -> unknownFlagErr dash_arg
82         
83         PassFlag f  | notNull rest -> unknownFlagErr dash_arg
84                     | otherwise    -> Right (f dash_arg, args)
85
86         OptIntSuffix f | null rest               -> Right (f Nothing,  args)
87                        | Just n <- parseInt rest -> Right (f (Just n), args)
88                        | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
89
90         IntSuffix f | Just n <- parseInt rest -> Right (f n, args)
91                     | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
92
93         OptPrefix f       -> Right (f rest, args)
94         AnySuffix f       -> Right (f dash_arg, args)
95         AnySuffixPred p f -> Right (f dash_arg, args)
96
97
98 findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
99 findArg spec arg
100   = case [ (removeSpaces rest, k) 
101          | (pat,k)   <- spec, 
102            Just rest <- [maybePrefixMatch pat arg],
103            arg_ok k rest arg ] 
104     of
105         []      -> Nothing
106         (one:_) -> Just one
107
108 arg_ok (NoArg _)            rest arg = null rest
109 arg_ok (HasArg _)           rest arg = True
110 arg_ok (SepArg _)           rest arg = null rest
111 arg_ok (Prefix _)           rest arg = notNull rest
112 arg_ok (PrefixPred p _)     rest arg = notNull rest && p rest
113 arg_ok (OptIntSuffix _)     rest arg = True
114 arg_ok (IntSuffix _)        rest arg = True
115 arg_ok (OptPrefix _)        rest arg = True
116 arg_ok (PassFlag _)         rest arg = null rest 
117 arg_ok (AnySuffix _)        rest arg = True
118 arg_ok (AnySuffixPred p _)  rest 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 (dropEq s) of
125                 ((n,""):_) -> Just n
126                 other      -> 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,s)
151 putCmdLineState s = CmdLineP $ \_ -> ((),s)