Skip to content

Commit 05105c2

Browse files
committed
better handling of whitespaces when parsing mixin stanza.
1 parent e8c4228 commit 05105c2

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Cabal/Distribution/Types/ModuleRenaming.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Distribution.Types.ModuleRenaming (
88
isDefaultRenaming,
99
) where
1010

11+
import Distribution.CabalSpecVersion
1112
import Distribution.Compat.Prelude hiding (empty)
1213
import Prelude ()
1314

@@ -67,6 +68,19 @@ isDefaultRenaming :: ModuleRenaming -> Bool
6768
isDefaultRenaming DefaultRenaming = True
6869
isDefaultRenaming _ = False
6970

71+
-- | For versions < 3.0 white spaces were not skipped after the '('
72+
-- and ')' tokens in the mixin field. This parser checks the cabal file version
73+
-- and does the correct skipping of spaces.
74+
betweenParens :: CabalParsing m => m a -> m a
75+
betweenParens p = do
76+
csv <- askCabalSpecVersion
77+
if csv >= CabalSpecV3_0
78+
then P.between (P.char '(' >> P.spaces) (P.char ')' >> P.spaces) p
79+
else P.between (P.char '(') (P.char ')') p
80+
81+
-- TODO: Give a sensible error on input "( A as B) instead of (A as B)
82+
-- for cabal files that are declared to be older than 3.0.
83+
7084
instance Binary ModuleRenaming where
7185

7286
instance NFData ModuleRenaming where rnf = genericRnf
@@ -87,18 +101,18 @@ instance Parsec ModuleRenaming where
87101
-- NB: try not necessary as the first token is obvious
88102
parsec = P.choice [ parseRename, parseHiding, return DefaultRenaming ]
89103
where
104+
cma = P.char ',' >> P.spaces
90105
parseRename = do
91-
rns <- P.between (P.char '(') (P.char ')') parseList
106+
rns <- betweenParens parseList
92107
P.spaces
93108
return (ModuleRenaming rns)
94109
parseHiding = do
95110
_ <- P.string "hiding"
96111
P.spaces
97-
hides <- P.between (P.char '(') (P.char ')')
98-
(P.sepBy parsec (P.char ',' >> P.spaces))
112+
hides <- betweenParens (P.sepBy parsec cma)
99113
return (HidingRenaming hides)
100114
parseList =
101-
P.sepBy parseEntry (P.char ',' >> P.spaces)
115+
P.sepBy parseEntry cma
102116
parseEntry = do
103117
orig <- parsec
104118
P.spaces

0 commit comments

Comments
 (0)