Skip to content

Commit 03d13bf

Browse files
zlonastjappeace
andcommitted
Allow case insensitive bools and move to numeric render
Co-authored-by: Jappie Klooster <jappieklooster@hotmail.com>
1 parent 07b96a2 commit 03d13bf

12 files changed

Lines changed: 125 additions & 219 deletions

File tree

Cabal-syntax/src/Distribution/Parsec.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,11 @@ instance Parsec Bool where
252252
parsec = P.munch1 isAlpha >>= postprocess
253253
where
254254
postprocess str
255-
| str == "True" = pure True
256-
| str == "False" = pure False
257-
| lstr == "true" = parsecWarning PWTBoolCase caseWarning *> pure True
258-
| lstr == "false" = parsecWarning PWTBoolCase caseWarning *> pure False
255+
| lstr == "true" = pure True
256+
| lstr == "false" = pure False
259257
| otherwise = fail $ "Not a boolean: " ++ str
260258
where
261259
lstr = map toLower str
262-
caseWarning =
263-
"Boolean values are case sensitive, use 'True' or 'False'."
264260

265261
instance Parsec a => Parsec (Last a) where
266262
parsec = parsecLast

Cabal-syntax/src/Distribution/Parsec/Warning.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ data PWarnType
2525
PWTOther
2626
| -- | Invalid UTF encoding
2727
PWTUTF
28-
| -- | @true@ or @false@, not @True@ or @False@
29-
PWTBoolCase
3028
| -- | there are version with tags
3129
PWTVersionTag
3230
| -- | New syntax used, but no @cabal-version: >= 1.2@ specified

Cabal-tests/tests/ParserTests.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ warningTests = testGroup "warnings triggered"
6868
, warningTest PWTLexNBSP "nbsp.cabal"
6969
, warningTest PWTLexTab "tab.cabal"
7070
, warningTest PWTUTF "utf8.cabal"
71-
, warningTest PWTBoolCase "bool.cabal"
7271
, warningTest PWTVersionTag "versiontag.cabal"
7372
, warningTest PWTNewSyntax "newsyntax.cabal"
7473
, warningTest PWTOldSyntax "oldsyntax.cabal"

Cabal-tests/tests/ParserTests/warnings/bool.cabal

Lines changed: 0 additions & 12 deletions
This file was deleted.

Cabal/src/Distribution/Simple/Command.hs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ module Distribution.Simple.Command
7979
, reqArg'
8080
, optArg
8181
, optArg'
82-
, optArgDef'
8382
, noArg
8483
, boolOpt
8584
, boolOpt'
@@ -280,15 +279,6 @@ optArg'
280279
optArg' ad mkflag showflag =
281280
optArg ad (succeedReadE (mkflag . Just)) ("", mkflag Nothing) showflag
282281

283-
optArgDef'
284-
:: Monoid b
285-
=> ArgPlaceHolder
286-
-> (String, Maybe String -> b)
287-
-> (b -> [Maybe String])
288-
-> MkOptDescr (a -> b) (b -> a -> a) a
289-
optArgDef' ad (dv, mkflag) showflag =
290-
optArg ad (succeedReadE (mkflag . Just)) (dv, mkflag Nothing) showflag
291-
292282
noArg :: Eq b => b -> MkOptDescr (a -> b) (b -> a -> a) a
293283
noArg flag sf lf d = choiceOpt [(flag, (sf, lf), d)] sf lf d
294284

Cabal/src/Distribution/Simple/Compiler.hs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DeriveGeneric #-}
33
{-# LANGUAGE DeriveTraversable #-}
4+
{-# LANGUAGE LambdaCase #-}
5+
{-# LANGUAGE PatternSynonyms #-}
46

57
-----------------------------------------------------------------------------
68

@@ -54,11 +56,13 @@ module Distribution.Simple.Compiler
5456

5557
-- * Support for optimisation levels
5658
, OptimisationLevel (..)
57-
, flagToOptimisationLevel
59+
, toOptimisationLevel
60+
, fromOptimisationLevel
5861

5962
-- * Support for debug info levels
6063
, DebugInfoLevel (..)
61-
, flagToDebugInfoLevel
64+
, toDebugInfoLevel
65+
, fromDebugInfoLevel
6266

6367
-- * Support for language extensions
6468
, CompilerFlag
@@ -112,6 +116,7 @@ import Language.Haskell.Extension
112116

113117
import Data.Bool (bool)
114118
import qualified Data.Map as Map (lookup)
119+
import Distribution.Simple.Flag (Flag, pattern Flag, pattern NoFlag)
115120
import System.Directory (canonicalizePath)
116121

117122
data Compiler = Compiler
@@ -329,12 +334,16 @@ parsecOptimisationLevel = boolParser <|> intParser
329334
boolParser = bool NoOptimisation NormalOptimisation <$> parsec
330335
intParser = intToOptimisationLevel <$> integral
331336

332-
flagToOptimisationLevel :: Maybe String -> OptimisationLevel
333-
flagToOptimisationLevel Nothing = NormalOptimisation
334-
flagToOptimisationLevel (Just s) = case reads s of
337+
toOptimisationLevel :: String -> OptimisationLevel
338+
toOptimisationLevel s = case reads s of
335339
[(i, "")] -> intToOptimisationLevel i
336340
_ -> error $ "Can't parse optimisation level " ++ s
337341

342+
fromOptimisationLevel :: Flag OptimisationLevel -> String
343+
fromOptimisationLevel = \case
344+
Flag op -> show $ fromEnum op
345+
NoFlag -> "1"
346+
338347
intToOptimisationLevel :: Int -> OptimisationLevel
339348
intToOptimisationLevel i
340349
| i >= minLevel && i <= maxLevel = toEnum i
@@ -374,22 +383,33 @@ instance Parsec DebugInfoLevel where
374383
parsec = parsecDebugInfoLevel
375384

376385
parsecDebugInfoLevel :: CabalParsing m => m DebugInfoLevel
377-
parsecDebugInfoLevel = flagToDebugInfoLevel . pure <$> parsecToken
378-
379-
flagToDebugInfoLevel :: Maybe String -> DebugInfoLevel
380-
flagToDebugInfoLevel Nothing = NormalDebugInfo
381-
flagToDebugInfoLevel (Just s) = case reads s of
382-
[(i, "")]
383-
| i >= fromEnum (minBound :: DebugInfoLevel)
384-
&& i <= fromEnum (maxBound :: DebugInfoLevel) ->
385-
toEnum i
386-
| otherwise ->
387-
error $
388-
"Bad debug info level: "
389-
++ show i
390-
++ ". Valid values are 0..3"
386+
parsecDebugInfoLevel = boolParser <|> intParser
387+
where
388+
boolParser = bool NoDebugInfo NormalDebugInfo <$> parsec
389+
intParser = intToDebugInfoLevel <$> integral
390+
391+
toDebugInfoLevel :: String -> DebugInfoLevel
392+
toDebugInfoLevel s = case reads s of
393+
[(i, "")] -> intToDebugInfoLevel i
391394
_ -> error $ "Can't parse debug info level " ++ s
392395

396+
fromDebugInfoLevel :: Flag DebugInfoLevel -> String
397+
fromDebugInfoLevel = \case
398+
Flag db -> show $ fromEnum db
399+
NoFlag -> "0"
400+
401+
intToDebugInfoLevel :: Int -> DebugInfoLevel
402+
intToDebugInfoLevel i
403+
| i >= minLevel && i <= maxLevel = toEnum i
404+
| otherwise =
405+
error $
406+
"Bad debug info level: "
407+
++ show i
408+
++ ". Valid values are 0..3"
409+
where
410+
minLevel = fromEnum (minBound :: DebugInfoLevel)
411+
maxLevel = fromEnum (maxBound :: DebugInfoLevel)
412+
393413
-- ------------------------------------------------------------
394414

395415
-- * Languages and Extensions

Cabal/src/Distribution/Simple/Setup/Config.hs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,12 @@ configureOptions showOrParseArgs =
570570
"optimization"
571571
configOptimization
572572
(\v flags -> flags{configOptimization = v})
573-
[ optArgDef'
573+
[ reqArg'
574574
"n"
575-
(show NoOptimisation, Flag . flagToOptimisationLevel)
575+
(Flag . toOptimisationLevel)
576576
( \case
577-
Flag NoOptimisation -> []
578-
Flag NormalOptimisation -> [Nothing]
579-
Flag MaximumOptimisation -> [Just "2"]
580-
_ -> []
577+
NoFlag -> []
578+
flag -> [fromOptimisationLevel flag]
581579
)
582580
"O"
583581
["enable-optimization", "enable-optimisation"]
@@ -592,17 +590,14 @@ configureOptions showOrParseArgs =
592590
"debug-info"
593591
configDebugInfo
594592
(\v flags -> flags{configDebugInfo = v})
595-
[ optArg'
593+
[ reqArg'
596594
"n"
597-
(Flag . flagToDebugInfoLevel)
595+
(Flag . toDebugInfoLevel)
598596
( \case
599-
Flag NoDebugInfo -> []
600-
Flag MinimalDebugInfo -> [Just "1"]
601-
Flag NormalDebugInfo -> [Nothing]
602-
Flag MaximalDebugInfo -> [Just "3"]
603-
_ -> []
597+
NoFlag -> []
598+
flag -> [fromDebugInfoLevel flag]
604599
)
605-
""
600+
"g"
606601
["enable-debug-info"]
607602
"Emit debug info (n is 0--3, default is 0)"
608603
, noArg

Cabal/src/Distribution/Types/DumpBuildInfo.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE LambdaCase #-}
23

34
module Distribution.Types.DumpBuildInfo
45
( DumpBuildInfo (..)
6+
, fromDumpBuildInfo
57
) where
68

7-
import Distribution.Compat.Prelude
8-
import Distribution.Parsec
9+
import Distribution.Compat.Prelude (Binary, Generic, NFData, Structured)
10+
import Distribution.Parsec (CabalParsing, Parsec (..))
911

1012
data DumpBuildInfo
1113
= NoDumpBuildInfo
@@ -24,3 +26,8 @@ parsecDumpBuildInfo = boolToDumpBuildInfo <$> parsec
2426

2527
boolToDumpBuildInfo :: Bool -> DumpBuildInfo
2628
boolToDumpBuildInfo bool = if bool then DumpBuildInfo else NoDumpBuildInfo
29+
30+
fromDumpBuildInfo :: DumpBuildInfo -> String
31+
fromDumpBuildInfo = \case
32+
NoDumpBuildInfo -> "False"
33+
DumpBuildInfo -> "True"

cabal-install/src/Distribution/Client/Config.hs

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ import Distribution.Compiler
138138
import Distribution.Deprecated.ParseUtils
139139
( FieldDescr (..)
140140
, PError (..)
141-
, PWarning (..)
142141
, ParseResult (..)
143142
, liftField
144143
, lineNo
@@ -167,8 +166,8 @@ import Distribution.Simple.Command
167166
, commandDefaultFlags
168167
)
169168
import Distribution.Simple.Compiler
170-
( DebugInfoLevel (..)
171-
, OptimisationLevel (..)
169+
( fromDebugInfoLevel
170+
, fromOptimisationLevel
172171
)
173172
import Distribution.Simple.InstallDirs
174173
( InstallDirs (..)
@@ -1190,75 +1189,29 @@ configFieldDescriptions src =
11901189
(Flag <$> parsec <|> pure NoFlag)
11911190
configHcFlavor
11921191
(\v flags -> flags{configHcFlavor = v})
1193-
, -- TODO: The following is a temporary fix. The "optimization"
1194-
-- and "debug-info" fields are OptArg, and viewAsFieldDescr
1195-
-- fails on that. Instead of a hand-written hackaged parser
1196-
-- and printer, we should handle this case properly in the
1197-
-- library.
1198-
liftField
1199-
configOptimization
1200-
( \v flags ->
1201-
flags{configOptimization = v}
1202-
)
1203-
$ let name = "optimization"
1204-
in FieldDescr
1205-
name
1206-
( \case
1207-
Flag NoOptimisation -> Disp.text "False"
1208-
Flag NormalOptimisation -> Disp.text "True"
1209-
Flag MaximumOptimisation -> Disp.text "2"
1210-
_ -> Disp.empty
1211-
)
1212-
( \line str _ -> case () of
1213-
_
1214-
| str == "False" -> ParseOk [] (Flag NoOptimisation)
1215-
| str == "True" -> ParseOk [] (Flag NormalOptimisation)
1216-
| str == "0" -> ParseOk [] (Flag NoOptimisation)
1217-
| str == "1" -> ParseOk [] (Flag NormalOptimisation)
1218-
| str == "2" -> ParseOk [] (Flag MaximumOptimisation)
1219-
| lstr == "false" -> ParseOk [caseWarning] (Flag NoOptimisation)
1220-
| lstr == "true" ->
1221-
ParseOk
1222-
[caseWarning]
1223-
(Flag NormalOptimisation)
1224-
| otherwise -> ParseFailed (NoParse name line)
1225-
where
1226-
lstr = lowercase str
1227-
caseWarning =
1228-
PWarning $
1229-
"The '"
1230-
++ name
1231-
++ "' field is case sensitive, use 'True' or 'False'."
1232-
)
1192+
, liftField configOptimization (\v flags -> flags{configOptimization = v}) $
1193+
let name = "optimization"
1194+
in FieldDescr
1195+
name
1196+
( \case
1197+
NoFlag -> Disp.empty
1198+
flag -> Disp.text $ fromOptimisationLevel flag
1199+
)
1200+
( \line str _ -> case maybe NoFlag Flag (simpleParsec str) of
1201+
NoFlag -> ParseFailed (NoParse name line)
1202+
flag -> ParseOk [] flag
1203+
)
12331204
, liftField configDebugInfo (\v flags -> flags{configDebugInfo = v}) $
12341205
let name = "debug-info"
12351206
in FieldDescr
12361207
name
12371208
( \case
1238-
Flag NoDebugInfo -> Disp.text "False"
1239-
Flag MinimalDebugInfo -> Disp.text "1"
1240-
Flag NormalDebugInfo -> Disp.text "True"
1241-
Flag MaximalDebugInfo -> Disp.text "3"
1242-
_ -> Disp.empty
1209+
NoFlag -> Disp.empty
1210+
flag -> Disp.text $ fromDebugInfoLevel flag
12431211
)
1244-
( \line str _ -> case () of
1245-
_
1246-
| str == "False" -> ParseOk [] (Flag NoDebugInfo)
1247-
| str == "True" -> ParseOk [] (Flag NormalDebugInfo)
1248-
| str == "0" -> ParseOk [] (Flag NoDebugInfo)
1249-
| str == "1" -> ParseOk [] (Flag MinimalDebugInfo)
1250-
| str == "2" -> ParseOk [] (Flag NormalDebugInfo)
1251-
| str == "3" -> ParseOk [] (Flag MaximalDebugInfo)
1252-
| lstr == "false" -> ParseOk [caseWarning] (Flag NoDebugInfo)
1253-
| lstr == "true" -> ParseOk [caseWarning] (Flag NormalDebugInfo)
1254-
| otherwise -> ParseFailed (NoParse name line)
1255-
where
1256-
lstr = lowercase str
1257-
caseWarning =
1258-
PWarning $
1259-
"The '"
1260-
++ name
1261-
++ "' field is case sensitive, use 'True' or 'False'."
1212+
( \line str _ -> case maybe NoFlag Flag (simpleParsec str) of
1213+
NoFlag -> ParseFailed (NoParse name line)
1214+
flag -> ParseOk [] flag
12621215
)
12631216
]
12641217
++ toSavedConfig

0 commit comments

Comments
 (0)