Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #6728: TotalIndexState cannot be empty string #6733

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cabal/Distribution/FieldGrammar/Described.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Distribution.FieldGrammar.Described (
-- * Lists
reSpacedList,
reCommaList,
reCommaNonEmpty,
reOptCommaList,
-- * Character Sets
csChar,
Expand Down Expand Up @@ -72,6 +73,9 @@ reSpacedList = REMunch RESpaces1
reCommaList :: GrammarRegex a -> GrammarRegex a
reCommaList = RECommaList

reCommaNonEmpty :: GrammarRegex a -> GrammarRegex a
reCommaNonEmpty = RECommaNonEmpty

reOptCommaList :: GrammarRegex a -> GrammarRegex a
reOptCommaList = REOptCommaList

Expand Down
14 changes: 14 additions & 0 deletions Cabal/Distribution/Parsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Distribution.Parsec (
parsecMaybeQuoted,
parsecCommaList,
parsecLeadingCommaList,
parsecLeadingCommaNonEmpty,
parsecOptCommaList,
parsecLeadingOptCommaList,
parsecStandard,
Expand Down Expand Up @@ -309,6 +310,19 @@ parsecLeadingCommaList p = do
lp = p <* P.spaces
comma = P.char ',' *> P.spaces P.<?> "comma"

-- |
--
-- @since 3.4.0.0
parsecLeadingCommaNonEmpty :: CabalParsing m => m a -> m (NonEmpty a)
parsecLeadingCommaNonEmpty p = do
c <- P.optional comma
case c of
Nothing -> P.sepEndByNonEmpty lp comma
Just _ -> P.sepByNonEmpty lp comma
where
lp = p <* P.spaces
comma = P.char ',' *> P.spaces P.<?> "comma"

parsecOptCommaList :: CabalParsing m => m a -> m [a]
parsecOptCommaList p = P.sepBy (p <* P.spaces) (P.optional comma)
where
Expand Down
3 changes: 3 additions & 0 deletions Cabal/Distribution/Utils/GrammarRegex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ data GrammarRegex a
| RESpaces -- ^ zero-or-more spaces
| RESpaces1 -- ^ one-or-more spaces
| RECommaList (GrammarRegex a) -- ^ comma list (note, leading or trailing commas)
| RECommaNonEmpty (GrammarRegex a) -- ^ comma non-empty list
| REOptCommaList (GrammarRegex a) -- ^ opt comma list

| RETodo -- ^ unspecified
Expand Down Expand Up @@ -146,6 +147,8 @@ regexDoc = go 0 . vacuous where

go _ (RECommaList r) =
"\\mathrm{commalist}" <<>> go 4 r
go _ (RECommaNonEmpty r) =
"\\mathrm{commanonempty}" <<>> go 4 r
go _ (REOptCommaList r) =
"\\mathrm{optcommalist}" <<>> go 4 r

Expand Down
8 changes: 8 additions & 0 deletions Cabal/tests/UnitTests/Distribution/Described.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,21 @@ convert = go id . vacuous where
go _ RESpaces1 = RE.ch_ ' ' RE.\/ " " RE.\/ "\n"

go f (RECommaList r) = go f (expandedCommaList r)
go f (RECommaNonEmpty r)= go f (expandedCommaNonEmpty r)
go f (REOptCommaList r) = go f (expandedOptCommaList r)

go _ RETodo = RE.Null

expandedCommaList :: GrammarRegex a -> GrammarRegex a
expandedCommaList = REUnion . expandedCommaList'

expandedCommaNonEmpty :: GrammarRegex a -> GrammarRegex a
expandedCommaNonEmpty r = REUnion
[ REMunch1 reSpacedComma r
, reComma <> RESpaces <> REMunch1 reSpacedComma r
, REMunch1 reSpacedComma r <> RESpaces <> reComma
]

expandedCommaList' :: GrammarRegex a -> [GrammarRegex a]
expandedCommaList' r =
[ REMunch reSpacedComma r
Expand Down
8 changes: 4 additions & 4 deletions cabal-install/Distribution/Client/IndexUtils/IndexState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Distribution.Client.IndexUtils.Timestamp (Timestamp)
import Distribution.Client.Types.RepoName (RepoName (..))

import Distribution.FieldGrammar.Described
import Distribution.Parsec (Parsec (..), parsecLeadingCommaList)
import Distribution.Parsec (Parsec (..), parsecLeadingCommaNonEmpty)
import Distribution.Pretty (Pretty (..))

import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -60,7 +60,7 @@ instance Pretty TotalIndexState where
-- Just (TIS IndexStateHead (fromList []))
--
-- >>> simpleParsec "" :: Maybe TotalIndexState
-- Just (TIS IndexStateHead (fromList []))
-- Nothing
--
-- >>> simpleParsec "hackage.haskell.org HEAD" :: Maybe TotalIndexState
-- Just (TIS IndexStateHead (fromList []))
Expand All @@ -72,7 +72,7 @@ instance Pretty TotalIndexState where
-- Just (TIS IndexStateHead (fromList [(RepoName "hackage.haskell.org",IndexStateTime (TS 1580819696))]))
--
instance Parsec TotalIndexState where
parsec = normalise . foldl' add headTotalIndexState <$> parsecLeadingCommaList single0 where
parsec = normalise . foldl' add headTotalIndexState <$> parsecLeadingCommaNonEmpty single0 where
single0 = startsWithRepoName <|> TokTimestamp <$> parsec
startsWithRepoName = do
reponame <- parsec
Expand All @@ -89,7 +89,7 @@ instance Parsec TotalIndexState where
add (TIS def m) (TokRepo rn idx) = TIS def (Map.insert rn idx m)

instance Described TotalIndexState where
describe _ = reCommaList $ REUnion
describe _ = reCommaNonEmpty $ REUnion
[ describe (Proxy :: Proxy RepoName) <> RESpaces1 <> ris
, ris
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,21 @@ convert = go id . vacuous where
go _ RESpaces1 = RE.ch_ ' ' RE.\/ " " RE.\/ "\n"

go f (RECommaList r) = go f (expandedCommaList r)
go f (RECommaNonEmpty r)= go f (expandedCommaNonEmpty r)
go f (REOptCommaList r) = go f (expandedOptCommaList r)

go _ RETodo = RE.Null

expandedCommaList :: GrammarRegex a -> GrammarRegex a
expandedCommaList = REUnion . expandedCommaList'

expandedCommaNonEmpty :: GrammarRegex a -> GrammarRegex a
expandedCommaNonEmpty r = REUnion
[ REMunch1 reSpacedComma r
, reComma <> RESpaces <> REMunch1 reSpacedComma r
, REMunch1 reSpacedComma r <> RESpaces <> reComma
]

expandedCommaList' :: GrammarRegex a -> [GrammarRegex a]
expandedCommaList' r =
[ REMunch reSpacedComma r
Expand Down