Skip to content

Commit c846c51

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

12 files changed

Lines changed: 131 additions & 223 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: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE DeriveGeneric #-}
44
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE LambdaCase #-}
56
{-# LANGUAGE PatternSynonyms #-}
67
{-# LANGUAGE RankNTypes #-}
78
{-# LANGUAGE ViewPatterns #-}
@@ -569,14 +570,12 @@ configureOptions showOrParseArgs =
569570
"optimization"
570571
configOptimization
571572
(\v flags -> flags{configOptimization = v})
572-
[ optArgDef'
573+
[ reqArg'
573574
"n"
574-
(show NoOptimisation, Flag . flagToOptimisationLevel)
575-
( \f -> case f of
576-
Flag NoOptimisation -> []
577-
Flag NormalOptimisation -> [Nothing]
578-
Flag MaximumOptimisation -> [Just "2"]
579-
_ -> []
575+
(Flag . toOptimisationLevel)
576+
( \case
577+
NoFlag -> []
578+
flag -> [fromOptimisationLevel flag]
580579
)
581580
"O"
582581
["enable-optimization", "enable-optimisation"]
@@ -591,17 +590,14 @@ configureOptions showOrParseArgs =
591590
"debug-info"
592591
configDebugInfo
593592
(\v flags -> flags{configDebugInfo = v})
594-
[ optArg'
593+
[ reqArg'
595594
"n"
596-
(Flag . flagToDebugInfoLevel)
597-
( \f -> case f of
598-
Flag NoDebugInfo -> []
599-
Flag MinimalDebugInfo -> [Just "1"]
600-
Flag NormalDebugInfo -> [Nothing]
601-
Flag MaximalDebugInfo -> [Just "3"]
602-
_ -> []
595+
(Flag . toDebugInfoLevel)
596+
( \case
597+
NoFlag -> []
598+
flag -> [fromDebugInfoLevel flag]
603599
)
604-
""
600+
"g"
605601
["enable-debug-info"]
606602
"Emit debug info (n is 0--3, default is 0)"
607603
, 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: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE LambdaCase #-}
23
{-# LANGUAGE PatternSynonyms #-}
34

45
-----------------------------------------------------------------------------
@@ -137,7 +138,6 @@ import Distribution.Compiler
137138
import Distribution.Deprecated.ParseUtils
138139
( FieldDescr (..)
139140
, PError (..)
140-
, PWarning (..)
141141
, ParseResult (..)
142142
, liftField
143143
, lineNo
@@ -166,8 +166,8 @@ import Distribution.Simple.Command
166166
, commandDefaultFlags
167167
)
168168
import Distribution.Simple.Compiler
169-
( DebugInfoLevel (..)
170-
, OptimisationLevel (..)
169+
( fromDebugInfoLevel
170+
, fromOptimisationLevel
171171
)
172172
import Distribution.Simple.InstallDirs
173173
( InstallDirs (..)
@@ -1189,75 +1189,29 @@ configFieldDescriptions src =
11891189
(Flag <$> parsec <|> pure NoFlag)
11901190
configHcFlavor
11911191
(\v flags -> flags{configHcFlavor = v})
1192-
, -- TODO: The following is a temporary fix. The "optimization"
1193-
-- and "debug-info" fields are OptArg, and viewAsFieldDescr
1194-
-- fails on that. Instead of a hand-written hackaged parser
1195-
-- and printer, we should handle this case properly in the
1196-
-- library.
1197-
liftField
1198-
configOptimization
1199-
( \v flags ->
1200-
flags{configOptimization = v}
1201-
)
1202-
$ let name = "optimization"
1203-
in FieldDescr
1204-
name
1205-
( \f -> case f of
1206-
Flag NoOptimisation -> Disp.text "False"
1207-
Flag NormalOptimisation -> Disp.text "True"
1208-
Flag MaximumOptimisation -> Disp.text "2"
1209-
_ -> Disp.empty
1210-
)
1211-
( \line str _ -> case () of
1212-
_
1213-
| str == "False" -> ParseOk [] (Flag NoOptimisation)
1214-
| str == "True" -> ParseOk [] (Flag NormalOptimisation)
1215-
| str == "0" -> ParseOk [] (Flag NoOptimisation)
1216-
| str == "1" -> ParseOk [] (Flag NormalOptimisation)
1217-
| str == "2" -> ParseOk [] (Flag MaximumOptimisation)
1218-
| lstr == "false" -> ParseOk [caseWarning] (Flag NoOptimisation)
1219-
| lstr == "true" ->
1220-
ParseOk
1221-
[caseWarning]
1222-
(Flag NormalOptimisation)
1223-
| otherwise -> ParseFailed (NoParse name line)
1224-
where
1225-
lstr = lowercase str
1226-
caseWarning =
1227-
PWarning $
1228-
"The '"
1229-
++ name
1230-
++ "' field is case sensitive, use 'True' or 'False'."
1231-
)
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+
)
12321204
, liftField configDebugInfo (\v flags -> flags{configDebugInfo = v}) $
12331205
let name = "debug-info"
12341206
in FieldDescr
12351207
name
1236-
( \f -> case f of
1237-
Flag NoDebugInfo -> Disp.text "False"
1238-
Flag MinimalDebugInfo -> Disp.text "1"
1239-
Flag NormalDebugInfo -> Disp.text "True"
1240-
Flag MaximalDebugInfo -> Disp.text "3"
1241-
_ -> Disp.empty
1208+
( \case
1209+
NoFlag -> Disp.empty
1210+
flag -> Disp.text $ fromDebugInfoLevel flag
12421211
)
1243-
( \line str _ -> case () of
1244-
_
1245-
| str == "False" -> ParseOk [] (Flag NoDebugInfo)
1246-
| str == "True" -> ParseOk [] (Flag NormalDebugInfo)
1247-
| str == "0" -> ParseOk [] (Flag NoDebugInfo)
1248-
| str == "1" -> ParseOk [] (Flag MinimalDebugInfo)
1249-
| str == "2" -> ParseOk [] (Flag NormalDebugInfo)
1250-
| str == "3" -> ParseOk [] (Flag MaximalDebugInfo)
1251-
| lstr == "false" -> ParseOk [caseWarning] (Flag NoDebugInfo)
1252-
| lstr == "true" -> ParseOk [caseWarning] (Flag NormalDebugInfo)
1253-
| otherwise -> ParseFailed (NoParse name line)
1254-
where
1255-
lstr = lowercase str
1256-
caseWarning =
1257-
PWarning $
1258-
"The '"
1259-
++ name
1260-
++ "' 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
12611215
)
12621216
]
12631217
++ toSavedConfig

0 commit comments

Comments
 (0)