Skip to content

Commit c265bbf

Browse files
committed
Improve list parser
Current parser for lists, like `packages:` in `cabal.project.local`, fails to process some input, for example: packages: foo/foo.cabal , bar/bar.cabal , baz/baz.cabal This commit simplifies the parser by adapting the code from `cabal-install` itself: https://github.com/haskell/cabal/blob/06c3eff242154cdf6b3d1bbaffcfc1a5cbea9da8/cabal-install/Distribution/Deprecated/ParseUtils.hs#L555
1 parent 5f2ed1c commit c265bbf

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/Hie/Cabal/Parser.hs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,17 @@ parseString = parseQuoted <|> unqualName
9292
unqualName :: Parser Text
9393
unqualName = takeWhile1 (not . (\c -> isSpace c || c == ','))
9494

95+
-- | Comma separated list.
9596
parseList :: Indent -> Parser [Text]
9697
parseList i = items <|> (emptyOrComLine >> indent i >> items)
9798
where
98-
items = do
99-
skipMany tabOrSpace
100-
h <- parseString
101-
skipMany tabOrSpace
102-
skipMany (char ',')
103-
t <-
104-
items
105-
<|> (skipToNextLine >> indent i >> parseList i)
106-
<|> pure []
107-
pure $ h : t
99+
items = sepBy parseString (skipSpace *> char ',' *> skipSpace)
100+
101+
-- | Comma or space separated list.
102+
parseOptCommaList :: Indent -> Parser [Text]
103+
parseOptCommaList i = items <|> (emptyOrComLine >> indent i >> items)
104+
where
105+
items = sepBy parseString ((skipSpace *> char ',' *> skipSpace) <|> (space *> skipSpace))
108106

109107
pathMain :: Indent -> [Text] -> Text -> [Text] -> [Text] -> Parser [Text]
110108
pathMain i p m o a =
@@ -185,4 +183,4 @@ indent i = do
185183
if c >= i then pure c else fail "insufficient indent"
186184

187185
extractPkgs :: Parser [T.Text]
188-
extractPkgs = join . catMaybes <$> many' (Just <$> field 0 "packages" parseList <|> (skipToNextLine >> pure Nothing))
186+
extractPkgs = join . catMaybes <$> many' (Just <$> field 0 "packages" parseOptCommaList <|> (skipToNextLine >> pure Nothing))

0 commit comments

Comments
 (0)