Skip to content

Commit

Permalink
Fix optimisationLevel parsec instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jgotoh committed Apr 2, 2024
1 parent e12f848 commit de32ff6
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Cabal/src/Distribution/Simple/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module Distribution.Simple.Compiler
, showProfDetailLevel
) where

import Distribution.Compat.CharParsing
import Distribution.Compat.Prelude
import Distribution.Parsec
import Distribution.Pretty
Expand All @@ -87,6 +88,7 @@ import Distribution.Simple.Utils
import Distribution.Version
import Language.Haskell.Extension

import Data.Bool (bool)
import qualified Data.Map as Map (lookup)
import System.Directory (canonicalizePath)

Expand Down Expand Up @@ -248,22 +250,28 @@ instance Parsec OptimisationLevel where
parsec = parsecOptimisationLevel

parsecOptimisationLevel :: CabalParsing m => m OptimisationLevel
parsecOptimisationLevel = flagToOptimisationLevel <$> pure <$> parsecToken
parsecOptimisationLevel = boolParser <|> intParser
where
boolParser = (bool NoOptimisation NormalOptimisation) <$> parsec
intParser = intToOptimisationLevel <$> integral

flagToOptimisationLevel :: Maybe String -> OptimisationLevel
flagToOptimisationLevel Nothing = NormalOptimisation
flagToOptimisationLevel (Just s) = case reads s of
[(i, "")]
| i >= fromEnum (minBound :: OptimisationLevel)
&& i <= fromEnum (maxBound :: OptimisationLevel) ->
toEnum i
| otherwise ->
error $
"Bad optimisation level: "
++ show i
++ ". Valid values are 0..2"
[(i, "")] -> intToOptimisationLevel i
_ -> error $ "Can't parse optimisation level " ++ s

intToOptimisationLevel :: Int -> OptimisationLevel
intToOptimisationLevel i
| i >= fromEnum (minBound :: OptimisationLevel)
&& i <= fromEnum (maxBound :: OptimisationLevel) =
toEnum i
| otherwise =
error $
"Bad optimisation level: "
++ show i
++ ". Valid values are 0..2"

-- ------------------------------------------------------------

-- * Debug info levels
Expand Down

0 comments on commit de32ff6

Please sign in to comment.