Skip to content

Commit

Permalink
Warn if -- token is found. Don't warn if it's "--".
Browse files Browse the repository at this point in the history
Resolves haskell#2681

Cabal files don't have trailing line comments. In many fields they
simply cause parse errors, but e.g. in extra-source-files
virtually everything is accepted. As there is simple
work around if people actually want double-dash, let's warn
about bare one.
  • Loading branch information
phadej committed Dec 25, 2017
1 parent 9705f68 commit 93a2171
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extra-source-files:
tests/ParserTests/warnings/bom.cabal
tests/ParserTests/warnings/bool.cabal
tests/ParserTests/warnings/deprecatedfield.cabal
tests/ParserTests/warnings/doubledash.cabal
tests/ParserTests/warnings/extratestmodule.cabal
tests/ParserTests/warnings/gluedop.cabal
tests/ParserTests/warnings/nbsp.cabal
Expand Down
14 changes: 12 additions & 2 deletions Cabal/Distribution/Parsec/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,21 @@ instance Parsec Bool where

-- | @[^ ,]@
parsecToken :: CabalParsing m => m String
parsecToken = parsecHaskellString <|> (P.munch1 (\x -> not (isSpace x) && x /= ',') P.<?> "identifier" )
parsecToken = parsecHaskellString <|> ((P.munch1 (\x -> not (isSpace x) && x /= ',') P.<?> "identifier" ) >>= checkNotDoubleDash)

-- | @[^ ]@
parsecToken' :: CabalParsing m => m String
parsecToken' = parsecHaskellString <|> (P.munch1 (not . isSpace) P.<?> "token")
parsecToken' = parsecHaskellString <|> ((P.munch1 (not . isSpace) P.<?> "token") >>= checkNotDoubleDash)

checkNotDoubleDash :: CabalParsing m => String -> m String
checkNotDoubleDash s = do
when (s == "--") $ parsecWarning PWTDoubleDash $ unwords
[ "Double-dash token found."
, "Note: there are no end-of-line comments in .cabal files, only whole line comments."
, "Use \"--\" (quoted double dash) to silence this warning, if you actually want -- token"
]

return s

parsecFilePath :: CabalParsing m => m FilePath
parsecFilePath = parsecToken
Expand Down
1 change: 1 addition & 0 deletions Cabal/Distribution/Parsec/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data PWarnType
| PWTLexNBSP
| PWTLexBOM
| PWTQuirkyCabalFile -- ^ legacy cabal file that we know how to patch
| PWTDoubleDash -- ^ Double dash token, most likely it's a mistake - it's not a comment
deriving (Eq, Ord, Show, Enum, Bounded)

-- | Parser warning.
Expand Down
1 change: 1 addition & 0 deletions Cabal/tests/ParserTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ warningTests = testGroup "warnings triggered"
, warningTest PWTUnknownField "unknownfield.cabal"
, warningTest PWTUnknownSection "unknownsection.cabal"
, warningTest PWTTrailingFields "trailingfield.cabal"
, warningTest PWTDoubleDash "doubledash.cabal"
-- TODO: not implemented yet
-- , warningTest PWTExtraTestModule "extratestmodule.cabal"
]
Expand Down
5 changes: 5 additions & 0 deletions Cabal/tests/ParserTests/regressions/encoding-0.8.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Name: encoding
Version: 0.8
cabal-version: >=1.12
-- double-dash files
extra-source-files:
-- this is comment
README.md "--"
"--"

custom-setup
setup-depends:
Expand Down
4 changes: 4 additions & 0 deletions Cabal/tests/ParserTests/regressions/encoding-0.8.format
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: encoding
version: 0.8
cabal-version: >=1.12
extra-source-files:
README.md
"--"
"--"

custom-setup
setup-depends: base <5,
Expand Down
9 changes: 9 additions & 0 deletions Cabal/tests/ParserTests/warnings/doubledash.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: bool
version: 1
cabal-version: >= 1.6
extra-source-files:
README.md -- we include it

library
build-depends: base >= 4.9 && <4.10
hs-source-dirs: .

0 comments on commit 93a2171

Please sign in to comment.