summaryrefslogtreecommitdiff
path: root/compiler/main/CmdLineParser.hs
diff options
context:
space:
mode:
authorsimonpj@microsoft.com <unknown>2006-07-27 08:04:22 +0000
committersimonpj@microsoft.com <unknown>2006-07-27 08:04:22 +0000
commitbec18cb3a1dcbc70b0257a367091c9a5948da6f6 (patch)
treecec7039bea0c18f3e4a2f12821879e278114a883 /compiler/main/CmdLineParser.hs
parent5edf58c10a0144fa8b328e18d0b7fffec2319424 (diff)
downloadhaskell-bec18cb3a1dcbc70b0257a367091c9a5948da6f6.tar.gz
Make -fcontext-stack into a dynamic flag
This allows you to put -fcontext-stack into an options pragma, as requested by Trac #829 While I was at it, I added OptIntPrefix to the forms allowed in CmdLineParser.
Diffstat (limited to 'compiler/main/CmdLineParser.hs')
-rw-r--r--compiler/main/CmdLineParser.hs92
1 files changed, 51 insertions, 41 deletions
diff --git a/compiler/main/CmdLineParser.hs b/compiler/main/CmdLineParser.hs
index e34b8c0857..dfe3125bd1 100644
--- a/compiler/main/CmdLineParser.hs
+++ b/compiler/main/CmdLineParser.hs
@@ -21,14 +21,15 @@ import Util ( maybePrefixMatch, notNull, removeSpaces )
import Panic ( assertPanic )
#endif
-data OptKind m
- = NoArg (m ()) -- flag with no argument
- | HasArg (String -> m ()) -- flag has an argument (maybe prefix)
- | SepArg (String -> m ()) -- flag has a separate argument
- | Prefix (String -> m ()) -- flag is a prefix only
- | OptPrefix (String -> m ()) -- flag may be a prefix
- | AnySuffix (String -> m ()) -- flag is a prefix, pass whole arg to fn
- | PassFlag (String -> m ()) -- flag with no arg, pass flag to fn
+data OptKind m -- Suppose the flag is -f
+ = NoArg (m ()) -- -f all by itself
+ | HasArg (String -> m ()) -- -farg or -f arg
+ | SepArg (String -> m ()) -- -f arg
+ | Prefix (String -> m ()) -- -farg
+ | OptPrefix (String -> m ()) -- -f or -farg (i.e. the arg is optional)
+ | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
+ | PassFlag (String -> m ()) -- -f; pass "-f" fn
+ | AnySuffix (String -> m ()) -- -f or -farg; pass entire "-farg" to fn
| PrefixPred (String -> Bool) (String -> m ())
| AnySuffixPred (String -> Bool) (String -> m ())
@@ -44,58 +45,50 @@ processArgs spec args = process spec args [] []
process _spec [] spare errs =
return (reverse spare, reverse errs)
- process spec args@(('-':arg):args') spare errs =
+ process spec (dash_arg@('-':arg):args) spare errs =
case findArg spec arg of
Just (rest,action) ->
- case processOneArg action rest args of
- Left err -> process spec args' spare (err:errs)
- Right (action,rest) -> do
- action >> process spec rest spare errs
- Nothing ->
- process spec args' (('-':arg):spare) errs
+ case processOneArg action rest arg args of
+ Left err -> process spec args spare (err:errs)
+ Right (action,rest) -> action >> process spec rest spare errs
+ Nothing -> process spec args (dash_arg:spare) errs
process spec (arg:args) spare errs =
process spec args (arg:spare) errs
-processOneArg :: OptKind m -> String -> [String]
- -> Either String (m (), [String])
-processOneArg action rest (dash_arg@('-':arg):args) =
- case action of
+processOneArg :: OptKind m -> String -> String -> [String]
+ -> Either String (m (), [String])
+processOneArg action rest arg args
+ = let dash_arg = '-' : arg
+ in case action of
NoArg a -> ASSERT(null rest) Right (a, args)
- HasArg f ->
- if rest /= ""
- then Right (f rest, args)
- else case args of
- [] -> missingArgErr dash_arg
- (arg1:args1) -> Right (f arg1, args1)
+ HasArg f | notNull rest -> Right (f rest, args)
+ | otherwise -> case args of
+ [] -> missingArgErr dash_arg
+ (arg1:args1) -> Right (f arg1, args1)
- SepArg f ->
- case args of
+ SepArg f -> case args of
[] -> unknownFlagErr dash_arg
(arg1:args1) -> Right (f arg1, args1)
- Prefix f ->
- if rest /= ""
- then Right (f rest, args)
- else unknownFlagErr dash_arg
+ Prefix f | notNull rest -> Right (f rest, args)
+ | otherwise -> unknownFlagErr dash_arg
- PrefixPred p f ->
- if rest /= ""
- then Right (f rest, args)
- else unknownFlagErr dash_arg
+ PrefixPred p f | notNull rest -> Right (f rest, args)
+ | otherwise -> unknownFlagErr dash_arg
- OptPrefix f -> Right (f rest, args)
+ PassFlag f | notNull rest -> unknownFlagErr dash_arg
+ | otherwise -> Right (f dash_arg, args)
- AnySuffix f -> Right (f dash_arg, args)
+ OptIntSuffix f | Just n <- parseInt rest -> Right (f n, args)
+ | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
+ OptPrefix f -> Right (f rest, args)
+ AnySuffix f -> Right (f dash_arg, args)
AnySuffixPred p f -> Right (f dash_arg, args)
- PassFlag f ->
- if rest /= ""
- then unknownFlagErr dash_arg
- else Right (f dash_arg, args)
findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
@@ -113,11 +106,28 @@ arg_ok (HasArg _) rest arg = True
arg_ok (SepArg _) rest arg = null rest
arg_ok (Prefix _) rest arg = notNull rest
arg_ok (PrefixPred p _) rest arg = notNull rest && p rest
+arg_ok (OptIntSuffix _) rest arg = True
arg_ok (OptPrefix _) rest arg = True
arg_ok (PassFlag _) rest arg = null rest
arg_ok (AnySuffix _) rest arg = True
arg_ok (AnySuffixPred p _) rest arg = p arg
+parseInt :: String -> Maybe (Maybe Int)
+-- Looks for "433" or "=342", with no trailing gubbins
+-- empty string => Just Nothing
+-- n or =n => Just (Just n)
+-- gibberish => Nothing
+parseInt s
+ | null s = Just Nothing
+ | otherwise = case reads (dropEq s) of
+ ((n,""):_) -> Just (Just n)
+ other -> Nothing
+
+dropEq :: String -> String
+-- Discards a leading equals sign
+dropEq ('=' : s) = s
+dropEq s = s
+
unknownFlagErr :: String -> Either String a
unknownFlagErr f = Left ("unrecognised flag: " ++ f)