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