Skip to content

Commit 0a4e6ae

Browse files
committed
Better parsing of source-repository & flag sections
1 parent 722c9ee commit 0a4e6ae

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/MicroCabal/Normalize.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ libName (Cabal (g@(Section _ _ gs):ss)) = Cabal $ g : map set ss
5151
reduce :: FlagInfo -> Cabal -> Cabal
5252
reduce info c = reduce' (addFlags c) c
5353
where addFlags (Cabal ss) = info{ flags = flags info ++ concatMap sect ss }
54-
sect (Section "flag" n fs) = [(n', dflt n' fs)] where n' = map toLower n
54+
sect (Section "flag" n fs) = [(map toLower n, dflt fs)]
5555
sect _ = []
56-
dflt n fs = head $ [ b | Field "default" (VBool b) <- fs ] ++ [error $ "no default for flag " ++ show n]
56+
dflt fs = head $ [ b | Field "default" (VBool b) <- fs ] ++ [True]
5757

5858
reduce' :: FlagInfo -> Cabal -> Cabal
5959
reduce' info = mapField red

src/MicroCabal/Parse.hs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,48 @@ pSection = pWhite *> (
352352
<|< Section <$> pKeyWordNC "library" <*> libName <*> pFields
353353
<|< Section <$> pKeyWordNC "foreign-library" <*> pName <*> pFields
354354
<|< Section <$> pKeyWordNC "executable" <*> pName <*> pFields
355-
<|< Section <$> pKeyWordNC "source-repository" <*> pName <*> pFields
356-
<|< Section <$> pKeyWordNC "flag" <*> pName <*> pFields
355+
<|< Section <$> pKeyWordNC "source-repository" <*> pName <*> pSourceRepositoryFields
356+
<|< Section <$> pKeyWordNC "flag" <*> pName <*> pFlagFields
357357
<|< Section <$> pKeyWordNC "test-suite" <*> pName <*> pFields
358358
<|< Section <$> pKeyWordNC "benchmark" <*> pName <*> pFields
359359
)
360360
where libName = pName <|< pure ""
361361

362+
pSourceRepositoryFields :: P [Field]
363+
pSourceRepositoryFields = pSpaces *> pNewLine *> emany pSourceRepositoryField
364+
where
365+
pSourceRepositoryField = do
366+
pWhite
367+
pushColumn
368+
fn <- lower <$> pFieldName
369+
pColon
370+
v <- case fn of
371+
"type" -> VItem <$> pItem
372+
"location" -> VItem <$> pItem
373+
"module" -> VItem <$> pItem
374+
"branch" -> VItem <$> pItem
375+
"tag" -> VItem <$> pItem
376+
"subdir" -> VItem <$> pItem
377+
_ -> error $ "Unknown source-repository field: " ++ fn
378+
pFieldSep
379+
pure $ Field fn v
380+
381+
pFlagFields :: P [Field]
382+
pFlagFields = pSpaces *> pNewLine *> emany pFlagField
383+
where
384+
pFlagField = do
385+
pWhite
386+
pushColumn
387+
fn <- lower <$> pFieldName
388+
pColon
389+
v <- case fn of
390+
"description" -> pFreeText
391+
"default" -> VBool <$> pBoolNL
392+
"manual" -> VBool <$> pBoolNL
393+
_ -> error $ "Unknown flag field: " ++ fn
394+
pFieldSep
395+
pure $ Field fn v
396+
362397
getParser :: FieldName -> P Value
363398
getParser f =
364399
if "x-" `isPrefixOf` f then pFreeTextX else
@@ -448,12 +483,6 @@ parsers =
448483
, "main-is" # (VItem <$> pItem)
449484
, "test-module" # (VItem <$> pItem)
450485
, "type" # (VItem <$> pItem)
451-
-- source-repository fields
452-
, "location" # (VItem <$> pItem)
453-
-- flag fields
454-
, "manual" # (VBool <$> pBoolNL)
455-
, "default" # (VBool <$> pBoolNL)
456-
, "tag" # pFreeText
457486
]
458487
where ( # ) = (,)
459488
-- XXX use local fixity

0 commit comments

Comments
 (0)