|
1 | 1 | {-# LANGUAGE DataKinds #-} |
2 | 2 | {-# LANGUAGE DeriveGeneric #-} |
3 | 3 | {-# LANGUAGE DeriveTraversable #-} |
| 4 | +{-# LANGUAGE LambdaCase #-} |
| 5 | +{-# LANGUAGE PatternSynonyms #-} |
4 | 6 |
|
5 | 7 | ----------------------------------------------------------------------------- |
6 | 8 |
|
@@ -54,11 +56,13 @@ module Distribution.Simple.Compiler |
54 | 56 |
|
55 | 57 | -- * Support for optimisation levels |
56 | 58 | , OptimisationLevel (..) |
57 | | - , flagToOptimisationLevel |
| 59 | + , toOptimisationLevel |
| 60 | + , fromOptimisationLevel |
58 | 61 |
|
59 | 62 | -- * Support for debug info levels |
60 | 63 | , DebugInfoLevel (..) |
61 | | - , flagToDebugInfoLevel |
| 64 | + , toDebugInfoLevel |
| 65 | + , fromDebugInfoLevel |
62 | 66 |
|
63 | 67 | -- * Support for language extensions |
64 | 68 | , CompilerFlag |
@@ -112,6 +116,7 @@ import Language.Haskell.Extension |
112 | 116 |
|
113 | 117 | import Data.Bool (bool) |
114 | 118 | import qualified Data.Map as Map (lookup) |
| 119 | +import Distribution.Simple.Flag (Flag, pattern Flag, pattern NoFlag) |
115 | 120 | import System.Directory (canonicalizePath) |
116 | 121 |
|
117 | 122 | data Compiler = Compiler |
@@ -329,12 +334,16 @@ parsecOptimisationLevel = boolParser <|> intParser |
329 | 334 | boolParser = bool NoOptimisation NormalOptimisation <$> parsec |
330 | 335 | intParser = intToOptimisationLevel <$> integral |
331 | 336 |
|
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 |
335 | 339 | [(i, "")] -> intToOptimisationLevel i |
336 | 340 | _ -> error $ "Can't parse optimisation level " ++ s |
337 | 341 |
|
| 342 | +fromOptimisationLevel :: Flag OptimisationLevel -> String |
| 343 | +fromOptimisationLevel = \case |
| 344 | + Flag op -> show $ fromEnum op |
| 345 | + NoFlag -> "1" |
| 346 | + |
338 | 347 | intToOptimisationLevel :: Int -> OptimisationLevel |
339 | 348 | intToOptimisationLevel i |
340 | 349 | | i >= minLevel && i <= maxLevel = toEnum i |
@@ -374,22 +383,33 @@ instance Parsec DebugInfoLevel where |
374 | 383 | parsec = parsecDebugInfoLevel |
375 | 384 |
|
376 | 385 | 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 |
391 | 394 | _ -> error $ "Can't parse debug info level " ++ s |
392 | 395 |
|
| 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 | + |
393 | 413 | -- ------------------------------------------------------------ |
394 | 414 |
|
395 | 415 | -- * Languages and Extensions |
|
0 commit comments