Skip to content

Commit

Permalink
better handling of whitespaces when parsing mixin stanza.
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush-kurur committed May 3, 2018
1 parent e8c4228 commit 05105c2
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Cabal/Distribution/Types/ModuleRenaming.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Distribution.Types.ModuleRenaming (
isDefaultRenaming,
) where

import Distribution.CabalSpecVersion
import Distribution.Compat.Prelude hiding (empty)
import Prelude ()

Expand Down Expand Up @@ -67,6 +68,19 @@ isDefaultRenaming :: ModuleRenaming -> Bool
isDefaultRenaming DefaultRenaming = True
isDefaultRenaming _ = False

-- | For versions < 3.0 white spaces were not skipped after the '('
-- and ')' tokens in the mixin field. This parser checks the cabal file version
-- and does the correct skipping of spaces.
betweenParens :: CabalParsing m => m a -> m a
betweenParens p = do
csv <- askCabalSpecVersion
if csv >= CabalSpecV3_0
then P.between (P.char '(' >> P.spaces) (P.char ')' >> P.spaces) p
else P.between (P.char '(') (P.char ')') p

-- TODO: Give a sensible error on input "( A as B) instead of (A as B)
-- for cabal files that are declared to be older than 3.0.

instance Binary ModuleRenaming where

instance NFData ModuleRenaming where rnf = genericRnf
Expand All @@ -87,18 +101,18 @@ instance Parsec ModuleRenaming where
-- NB: try not necessary as the first token is obvious
parsec = P.choice [ parseRename, parseHiding, return DefaultRenaming ]
where
cma = P.char ',' >> P.spaces
parseRename = do
rns <- P.between (P.char '(') (P.char ')') parseList
rns <- betweenParens parseList
P.spaces
return (ModuleRenaming rns)
parseHiding = do
_ <- P.string "hiding"
P.spaces
hides <- P.between (P.char '(') (P.char ')')
(P.sepBy parsec (P.char ',' >> P.spaces))
hides <- betweenParens (P.sepBy parsec cma)
return (HidingRenaming hides)
parseList =
P.sepBy parseEntry (P.char ',' >> P.spaces)
P.sepBy parseEntry cma
parseEntry = do
orig <- parsec
P.spaces
Expand Down

0 comments on commit 05105c2

Please sign in to comment.