@@ -28,6 +28,7 @@ module Cabal.Project (
2828
2929import Control.DeepSeq (NFData (.. ))
3030import Control.Exception (Exception (.. ), throwIO )
31+ import Control.Monad (unless )
3132import Control.Monad.IO.Class (liftIO )
3233import Control.Monad.Trans.Except (ExceptT , runExceptT , throwE )
3334import Data.Bifoldable (Bifoldable (.. ))
@@ -235,6 +236,12 @@ parseProject = parseWith $ \fields0 -> do
235236-- >>> pp $ fmap (fmap prjPackages) $ parseProjectWithConditionals "cabal.project" $ fromString $ unlines [ "packages: foo bar/*.cabal", "if impl(ghc >=9)", " packages: quu", "if impl(ghc >=10)", " packages: zoo" ]
236237-- CondTree ["foo","bar/*.cabal"] _ [CondBranch _ (CondTree ["quu"] _ []) Nothing,CondBranch _ (CondTree ["zoo"] _ []) Nothing]
237238--
239+ -- >>> pp $ fmap (fmap prjPackages) $ parseProjectWithConditionals "cabal.project" $ fromString $ unlines [ "packages: foo bar/*.cabal", "if impl(ghc >=9)", " packages: quu", "else", " packages: zoo" ]
240+ -- CondTree ["foo","bar/*.cabal"] _ [CondBranch _ (CondTree ["quu"] _ []) (Just CondTree ["zoo"] _ [])]
241+ --
242+ -- >>> pp $ fmap (fmap prjPackages) $ parseProjectWithConditionals "cabal.project" $ fromString $ unlines [ "packages: foo bar/*.cabal", "if impl(ghc >=9)", " packages: quu", "elif impl(ghc >=10)", " packages: zoo", "else", " packages: yyz" ]
243+ -- CondTree ["foo","bar/*.cabal"] _ [CondBranch _ (CondTree ["quu"] _ []) (Just CondTree [] _ [CondBranch _ (CondTree ["zoo"] _ []) (Just CondTree ["yyz"] _ [])])]
244+ --
238245parseProjectWithConditionals :: FilePath -> ByteString -> Either (ParseError NonEmpty ) (C. CondTree C. ConfVar () (Project Void String String ))
239246parseProjectWithConditionals = parseWith $ \ fields0 -> flip parseCondTree fields0 $ \ fields1 sections -> do
240247 let fields2 = M. filterWithKey (\ k _ -> k `elem` knownFields) fields1
@@ -452,9 +459,9 @@ parseCondTree subparse = go
452459 -> C. ParseResult ([C. Section C. Position ], [C. CondBranch C. ConfVar () a ])
453460 goIfs accS accB [] = do
454461 return (accS [] , accB [] )
455- goIfs accS accB (C. MkSection (C. Name pos name) test fields : sections)
462+ goIfs accS accB (C. MkSection (C. Name pos name) args fields : sections)
456463 | name == " if" = do
457- test' <- C. parseConditionConfVar test
464+ test' <- C. parseConditionConfVar args
458465 fields' <- go fields
459466 goElse (C. CondBranch test' fields') accS accB sections
460467 | name == " else" = do
@@ -472,13 +479,17 @@ parseCondTree subparse = go
472479 -> ([C. CondBranch C. ConfVar () a ] -> [C. CondBranch C. ConfVar () a ])
473480 -> [C. Section C. Position ]
474481 -> C. ParseResult ([C. Section C. Position ], [C. CondBranch C. ConfVar () a ])
475- goElse _make _accS _accB (C. MkSection (C. Name pos name) _args _fields : _sections )
482+ goElse make accS accB (C. MkSection (C. Name pos name) args fields : sections )
476483 | name == " else" = do
477- C. parseFailure pos " else is not supported yet"
478- return ([] , [] )
484+ unless (null args) $ C. parseFailure pos " arguments passed to else"
485+ fields' <- go fields
486+ let condTree = make (Just fields')
487+ goIfs accS (accB . (condTree : )) sections
479488 | name == " elif" = do
480- C. parseFailure pos " elif is not supported yet"
481- return ([] , [] )
489+ test' <- C. parseConditionConfVar args
490+ fields' <- go fields
491+ emptyA <- subparse mempty []
492+ goElse (make . Just . C. CondNode emptyA () . pure . C. CondBranch test' fields') accS accB sections
482493 goElse make accS accB sections = do
483494 let condTree = make Nothing
484495 goIfs accS (accB . (condTree : )) sections
0 commit comments