@@ -419,30 +419,35 @@ parseFields v fields grammar = do
419
419
420
420
warnInvalidSubsection :: Section Position -> ParseResult ()
421
421
warnInvalidSubsection (MkSection (Name pos name) _ _) =
422
- void ( parseFailure pos $ " invalid subsection " ++ show name)
422
+ void $ parseFailure pos $ " invalid subsection " ++ show name
423
423
424
424
parseCondTree
425
- :: forall a c .
426
- CabalSpecVersion
427
- -> HasElif -- ^ accept @elif@
428
- -> ParsecFieldGrammar' a -- ^ grammar
429
- -> (a -> c ) -- ^ condition extractor
425
+ :: forall a . FromBuildInfo a
426
+ => CabalSpecVersion
427
+ -> HasElif -- ^ accept @elif@
428
+ -> ParsecFieldGrammar' a -- ^ grammar
429
+ -> Map String CondTreeBuildInfo -- ^ common stanzas
430
+ -> (a -> [Dependency ]) -- ^ condition extractor
430
431
-> [Field Position ]
431
- -> ParseResult (CondTree ConfVar c a )
432
- parseCondTree v hasElif grammar cond = go
432
+ -> ParseResult (CondTree ConfVar [ Dependency ] a )
433
+ parseCondTree v hasElif grammar commonStanzas cond = go
433
434
where
434
- go fields = do
435
+ go fields0 = do
436
+ (fields, endo) <-
437
+ if v >= CabalSpecV3_0
438
+ then processImports v commonStanzas fields0
439
+ else traverse_ (warnImport v) fields0 >> return (fields0, id )
440
+
435
441
let (fs, ss) = partitionFields fields
436
442
x <- parseFieldGrammar v fs grammar
437
443
branches <- concat <$> traverse parseIfs ss
438
- return ( CondNode x (cond x) branches) -- TODO: branches
444
+ return $ endo $ CondNode x (cond x) branches
439
445
440
- parseIfs :: [Section Position ] -> ParseResult [CondBranch ConfVar c a ]
446
+ parseIfs :: [Section Position ] -> ParseResult [CondBranch ConfVar [ Dependency ] a ]
441
447
parseIfs [] = return []
442
448
parseIfs (MkSection (Name _ name) test fields : sections) | name == " if" = do
443
449
test' <- parseConditionConfVar test
444
450
fields' <- go fields
445
- -- TODO: else
446
451
(elseFields, sections') <- parseElseIfs sections
447
452
return (CondBranch test' fields' elseFields : sections')
448
453
parseIfs (MkSection (Name pos name) _ _ : sections) = do
@@ -451,7 +456,7 @@ parseCondTree v hasElif grammar cond = go
451
456
452
457
parseElseIfs
453
458
:: [Section Position ]
454
- -> ParseResult (Maybe (CondTree ConfVar c a ), [CondBranch ConfVar c a ])
459
+ -> ParseResult (Maybe (CondTree ConfVar [ Dependency ] a ), [CondBranch ConfVar [ Dependency ] a ])
455
460
parseElseIfs [] = return (Nothing , [] )
456
461
parseElseIfs (MkSection (Name pos name) args fields : sections) | name == " else" = do
457
462
unless (null args) $
@@ -460,10 +465,7 @@ parseCondTree v hasElif grammar cond = go
460
465
sections' <- parseIfs sections
461
466
return (Just elseFields, sections')
462
467
463
-
464
-
465
468
parseElseIfs (MkSection (Name _ name) test fields : sections) | hasElif == HasElif , name == " elif" = do
466
- -- TODO: check cabal-version
467
469
test' <- parseConditionConfVar test
468
470
fields' <- go fields
469
471
(elseFields, sections') <- parseElseIfs sections
@@ -560,21 +562,31 @@ parseCondTreeWithCommonStanzas
560
562
-> Map String CondTreeBuildInfo -- ^ common stanzas
561
563
-> [Field Position ]
562
564
-> ParseResult (CondTree ConfVar [Dependency ] a )
563
- parseCondTreeWithCommonStanzas v grammar commonStanzas = goImports []
565
+ parseCondTreeWithCommonStanzas v grammar commonStanzas fields = do
566
+ (fields', endo) <- processImports v commonStanzas fields
567
+ x <- parseCondTree v hasElif grammar commonStanzas (view L. targetBuildDepends) fields'
568
+ return (endo x)
564
569
where
565
570
hasElif = specHasElif v
571
+
572
+ processImports
573
+ :: forall a . FromBuildInfo a
574
+ => CabalSpecVersion
575
+ -> Map String CondTreeBuildInfo -- ^ common stanzas
576
+ -> [Field Position ]
577
+ -> ParseResult ([Field Position ], CondTree ConfVar [Dependency ] a -> CondTree ConfVar [Dependency ] a )
578
+ processImports v commonStanzas = go []
579
+ where
566
580
hasCommonStanzas = specHasCommonStanzas v
567
581
568
582
getList' :: List CommaFSep Token String -> [String ]
569
583
getList' = Newtype. unpack
570
584
571
- -- parse leading imports
572
- -- not supported:
573
- goImports acc (Field (Name pos name) _ : fields) | name == " import" , hasCommonStanzas == NoCommonStanzas = do
585
+ go acc (Field (Name pos name) _ : fields) | name == " import" , hasCommonStanzas == NoCommonStanzas = do
574
586
parseWarning pos PWTUnknownField " Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas"
575
- goImports acc fields
587
+ go acc fields
576
588
-- supported:
577
- goImports acc (Field (Name pos name) fls : fields) | name == " import" = do
589
+ go acc (Field (Name pos name) fls : fields) | name == " import" = do
578
590
names <- getList' <$> runFieldParser pos parsec v fls
579
591
names' <- for names $ \ commonName ->
580
592
case Map. lookup commonName commonStanzas of
@@ -584,16 +596,19 @@ parseCondTreeWithCommonStanzas v grammar commonStanzas = goImports []
584
596
Just commonTree ->
585
597
pure (Just commonTree)
586
598
587
- goImports (acc ++ catMaybes names') fields
599
+ go (acc ++ catMaybes names') fields
588
600
589
- -- Go to parsing condTree after first non-import 'Field'.
590
- goImports acc fields = go acc fields
601
+ -- TODO: and filter!
602
+ go acc fields = do
603
+ traverse_ (warnImport v) fields
604
+ return (fields, \ x -> foldr mergeCommonStanza x acc)
591
605
592
- -- parse actual CondTree
593
- go :: [CondTreeBuildInfo ] -> [Field Position ] -> ParseResult (CondTree ConfVar [Dependency ] a )
594
- go bis fields = do
595
- x <- parseCondTree v hasElif grammar (view L. targetBuildDepends) fields
596
- pure $ foldr mergeCommonStanza x bis
606
+ warnImport :: CabalSpecVersion -> Field Position -> ParseResult ()
607
+ warnImport v (Field (Name pos name) _) | name == " import" =
608
+ if specHasCommonStanzas v == NoCommonStanzas
609
+ then parseWarning pos PWTUnknownField " Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas"
610
+ else parseWarning pos PWTUnknownField " Unknown field: import. Common stanza imports should be at the top of the enclosing section"
611
+ warnImport _ _ = pure ()
597
612
598
613
mergeCommonStanza
599
614
:: forall a . FromBuildInfo a
0 commit comments