Skip to content

Commit 6db53df

Browse files
piyush-kururphadej
authored andcommitted
better handling of whitespaces when parsing mixin stanza.
1 parent 51285fc commit 6db53df

17 files changed

+505
-6
lines changed

Cabal/Cabal.cabal

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ extra-source-files:
5757
tests/ParserTests/errors/leading-comma-2c.errors
5858
tests/ParserTests/errors/leading-comma.cabal
5959
tests/ParserTests/errors/leading-comma.errors
60+
tests/ParserTests/errors/mixin-1.cabal
61+
tests/ParserTests/errors/mixin-1.errors
62+
tests/ParserTests/errors/mixin-2.cabal
63+
tests/ParserTests/errors/mixin-2.errors
6064
tests/ParserTests/errors/multiple-libs.cabal
6165
tests/ParserTests/errors/multiple-libs.errors
6266
tests/ParserTests/errors/noVersion.cabal
@@ -73,6 +77,8 @@ extra-source-files:
7377
tests/ParserTests/errors/spdx-2.errors
7478
tests/ParserTests/errors/spdx-3.cabal
7579
tests/ParserTests/errors/spdx-3.errors
80+
tests/ParserTests/errors/undefined-flag.cabal
81+
tests/ParserTests/errors/undefined-flag.errors
7682
tests/ParserTests/errors/version-sets-1.cabal
7783
tests/ParserTests/errors/version-sets-1.errors
7884
tests/ParserTests/errors/version-sets-2.cabal
@@ -81,8 +87,6 @@ extra-source-files:
8187
tests/ParserTests/errors/version-sets-3.errors
8288
tests/ParserTests/errors/version-sets-4.cabal
8389
tests/ParserTests/errors/version-sets-4.errors
84-
tests/ParserTests/errors/undefined-flag.cabal
85-
tests/ParserTests/errors/undefined-flag.errors
8690
tests/ParserTests/ipi/Includes2.cabal
8791
tests/ParserTests/ipi/Includes2.expr
8892
tests/ParserTests/ipi/Includes2.format
@@ -152,6 +156,15 @@ extra-source-files:
152156
tests/ParserTests/regressions/leading-comma.cabal
153157
tests/ParserTests/regressions/leading-comma.expr
154158
tests/ParserTests/regressions/leading-comma.format
159+
tests/ParserTests/regressions/mixin-1.cabal
160+
tests/ParserTests/regressions/mixin-1.expr
161+
tests/ParserTests/regressions/mixin-1.format
162+
tests/ParserTests/regressions/mixin-2.cabal
163+
tests/ParserTests/regressions/mixin-2.expr
164+
tests/ParserTests/regressions/mixin-2.format
165+
tests/ParserTests/regressions/mixin-3.cabal
166+
tests/ParserTests/regressions/mixin-3.expr
167+
tests/ParserTests/regressions/mixin-3.format
155168
tests/ParserTests/regressions/multiple-libs-2.cabal
156169
tests/ParserTests/regressions/multiple-libs-2.check
157170
tests/ParserTests/regressions/multiple-libs-2.expr

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

@@ -64,6 +65,19 @@ isDefaultRenaming :: ModuleRenaming -> Bool
6465
isDefaultRenaming DefaultRenaming = True
6566
isDefaultRenaming _ = False
6667

68+
-- | For cabal spec versions < 2.4 white spaces were not skipped after the '('
69+
-- and ')' tokens in the mixin field. This parser checks the cabal file version
70+
-- and does the correct skipping of spaces.
71+
betweenParens :: CabalParsing m => m a -> m a
72+
betweenParens p = do
73+
csv <- askCabalSpecVersion
74+
if csv >= CabalSpecV3_0
75+
then P.between (P.char '(' >> P.spaces) (P.spaces >> P.char ')' >> P.spaces) p
76+
else P.between (P.char '(' >> warnSpaces) (P.char ')') p
77+
where
78+
warnSpaces = P.optional $
79+
P.space *> fail "space after parenthesis, use cabal-version: 2.4 or higher"
80+
6781
instance Binary ModuleRenaming where
6882

6983
instance NFData ModuleRenaming where rnf = genericRnf
@@ -84,18 +98,18 @@ instance Parsec ModuleRenaming where
8498
-- NB: try not necessary as the first token is obvious
8599
parsec = P.choice [ parseRename, parseHiding, return DefaultRenaming ]
86100
where
101+
cma = P.char ',' >> P.spaces
87102
parseRename = do
88-
rns <- P.between (P.char '(') (P.char ')') parseList
103+
rns <- betweenParens parseList
89104
P.spaces
90105
return (ModuleRenaming rns)
91106
parseHiding = do
92107
_ <- P.string "hiding"
93108
P.spaces
94-
hides <- P.between (P.char '(') (P.char ')')
95-
(P.sepBy parsec (P.char ',' >> P.spaces))
109+
hides <- betweenParens (P.sepBy parsec cma)
96110
return (HidingRenaming hides)
97111
parseList =
98-
P.sepBy parseEntry (P.char ',' >> P.spaces)
112+
P.sepBy parseEntry cma
99113
parseEntry = do
100114
orig <- parsec
101115
P.spaces

Cabal/doc/file-format-changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ relative to the respective preceding *published* version.
5858
* New set-notation syntax for ``==`` and ``^>=`` operators, see
5959
:pkg-field:`build-depends` field documentation for examples.
6060

61+
* Allow more whitespace in :pkg-field: `mixins` field
62+
6163
``cabal-version: 2.4``
6264
----------------------
6365

Cabal/tests/ParserTests.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ errorTests = testGroup "errors"
117117
, errorTest "version-sets-3.cabal"
118118
, errorTest "version-sets-4.cabal"
119119
, errorTest "undefined-flag.cabal"
120+
, errorTest "mixin-1.cabal"
121+
, errorTest "mixin-2.cabal"
120122
]
121123

122124
errorTest :: FilePath -> TestTree
@@ -165,6 +167,9 @@ regressionTests = testGroup "regressions"
165167
, regressionTest "hidden-main-lib.cabal"
166168
, regressionTest "jaeger-flamegraph.cabal"
167169
, regressionTest "version-sets.cabal"
170+
, regressionTest "mixin-1.cabal"
171+
, regressionTest "mixin-2.cabal"
172+
, regressionTest "mixin-3.cabal"
168173
]
169174

170175
regressionTest :: FilePath -> TestTree
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cabal-version: 2.4
2+
name: mixin
3+
version: 0
4+
5+
-- mixin field:
6+
-- in 2.2 we got leading/trailing commas
7+
-- in 3.0 we got lax space parsing
8+
--
9+
-- This should fail
10+
executable str-example
11+
main-is: Main.hs
12+
build-depends: base, str-string, str-bytestring
13+
mixins: str-string ( Str as Str.String ),
14+
str-bytestring ( Str as Str.ByteString ),
15+
hs-source-dirs: str-example
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
VERSION: Just (mkVersion [2,4])
2+
mixin-1.cabal:13:41:
3+
unexpected space after parenthesis, use cabal-version: 2.4 or higher
4+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cabal-version: 2.2
2+
name: mixin
3+
version: 0
4+
5+
-- mixin field:
6+
-- in 2.2 we got leading/trailing commas
7+
-- in 2.2 we got lax space parsing
8+
--
9+
-- This should fail
10+
executable str-example
11+
main-is: Main.hs
12+
build-depends: base, str-string, str-bytestring
13+
mixins: str-string hiding ( Foo )
14+
hs-source-dirs: str-example
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
VERSION: Just (mkVersion [2,2])
2+
mixin-2.cabal:13:48:
3+
unexpected space after parenthesis, use cabal-version: 2.4 or higher
4+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cabal-version: 2.0
2+
name: mixin
3+
version: 0
4+
5+
executable str-example
6+
main-is: Main.hs
7+
build-depends: base, str-string, str-bytestring
8+
mixins: str-string (Str as Str.String),
9+
str-bytestring (Str as Str.ByteString)
10+
hs-source-dirs: str-example
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
GenericPackageDescription
2+
{condBenchmarks = [],
3+
condExecutables = [_×_
4+
`UnqualComponentName "str-example"`
5+
CondNode
6+
{condTreeComponents = [],
7+
condTreeConstraints = [Dependency
8+
`PackageName "base"`
9+
AnyVersion
10+
(Set.fromList [LMainLibName]),
11+
Dependency
12+
`PackageName "str-string"`
13+
AnyVersion
14+
(Set.fromList [LMainLibName]),
15+
Dependency
16+
`PackageName "str-bytestring"`
17+
AnyVersion
18+
(Set.fromList [LMainLibName])],
19+
condTreeData = Executable
20+
{buildInfo = BuildInfo
21+
{asmOptions = [],
22+
asmSources = [],
23+
autogenModules = [],
24+
buildToolDepends = [],
25+
buildTools = [],
26+
buildable = True,
27+
cSources = [],
28+
ccOptions = [],
29+
cmmOptions = [],
30+
cmmSources = [],
31+
cppOptions = [],
32+
customFieldsBI = [],
33+
cxxOptions = [],
34+
cxxSources = [],
35+
defaultExtensions = [],
36+
defaultLanguage = Nothing,
37+
extraBundledLibs = [],
38+
extraDynLibFlavours = [],
39+
extraFrameworkDirs = [],
40+
extraGHCiLibs = [],
41+
extraLibDirs = [],
42+
extraLibFlavours = [],
43+
extraLibs = [],
44+
frameworks = [],
45+
hsSourceDirs = ["str-example"],
46+
includeDirs = [],
47+
includes = [],
48+
installIncludes = [],
49+
jsSources = [],
50+
ldOptions = [],
51+
mixins = [`Mixin {mixinPackageName = PackageName "str-string", mixinIncludeRenaming = IncludeRenaming {includeProvidesRn = ModuleRenaming [(ModuleName ["Str"],ModuleName ["Str","String"])], includeRequiresRn = DefaultRenaming}}`,
52+
`Mixin {mixinPackageName = PackageName "str-bytestring", mixinIncludeRenaming = IncludeRenaming {includeProvidesRn = ModuleRenaming [(ModuleName ["Str"],ModuleName ["Str","ByteString"])], includeRequiresRn = DefaultRenaming}}`],
53+
oldExtensions = [],
54+
options = PerCompilerFlavor [] [],
55+
otherExtensions = [],
56+
otherLanguages = [],
57+
otherModules = [],
58+
pkgconfigDepends = [],
59+
profOptions = PerCompilerFlavor [] [],
60+
sharedOptions = PerCompilerFlavor [] [],
61+
staticOptions = PerCompilerFlavor [] [],
62+
targetBuildDepends = [Dependency
63+
`PackageName "base"`
64+
AnyVersion
65+
(Set.fromList
66+
[LMainLibName]),
67+
Dependency
68+
`PackageName "str-string"`
69+
AnyVersion
70+
(Set.fromList
71+
[LMainLibName]),
72+
Dependency
73+
`PackageName "str-bytestring"`
74+
AnyVersion
75+
(Set.fromList
76+
[LMainLibName])],
77+
virtualModules = []},
78+
exeName = `UnqualComponentName "str-example"`,
79+
exeScope = ExecutablePublic,
80+
modulePath = "Main.hs"}}],
81+
condForeignLibs = [],
82+
condLibrary = Nothing,
83+
condSubLibraries = [],
84+
condTestSuites = [],
85+
genPackageFlags = [],
86+
packageDescription = PackageDescription
87+
{author = "",
88+
benchmarks = [],
89+
bugReports = "",
90+
buildTypeRaw = Nothing,
91+
category = "",
92+
copyright = "",
93+
customFieldsPD = [],
94+
dataDir = "",
95+
dataFiles = [],
96+
description = "",
97+
executables = [],
98+
extraDocFiles = [],
99+
extraSrcFiles = [],
100+
extraTmpFiles = [],
101+
foreignLibs = [],
102+
homepage = "",
103+
library = Nothing,
104+
licenseFiles = [],
105+
licenseRaw = Left NONE,
106+
maintainer = "",
107+
package = PackageIdentifier
108+
{pkgName = `PackageName "mixin"`,
109+
pkgVersion = `mkVersion [0]`},
110+
pkgUrl = "",
111+
setupBuildInfo = Nothing,
112+
sourceRepos = [],
113+
specVersionRaw = Left `mkVersion [2,0]`,
114+
stability = "",
115+
subLibraries = [],
116+
synopsis = "",
117+
testSuites = [],
118+
testedWith = []}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cabal-version: 2.0
2+
name: mixin
3+
version: 0
4+
5+
executable str-example
6+
main-is: Main.hs
7+
hs-source-dirs: str-example
8+
build-depends:
9+
base -any,
10+
str-string -any,
11+
str-bytestring -any
12+
mixins:
13+
str-string (Str as Str.String),
14+
str-bytestring (Str as Str.ByteString)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cabal-version: 3.0
2+
name: mixin
3+
version: 0
4+
5+
-- mixin field:
6+
-- in 2.2 we got leading/trailing commas
7+
-- in 3.0 we got lax space parsing
8+
executable str-example
9+
main-is: Main.hs
10+
build-depends: base, str-string, str-bytestring
11+
mixins: str-string ( Str as Str.String ),
12+
str-bytestring ( Str as Str.ByteString ),
13+
hs-source-dirs: str-example

0 commit comments

Comments
 (0)