Skip to content

Commit aed4155

Browse files
committed
Fix: #24 handle comments in lists
1 parent 45165dc commit aed4155

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Hie/Cabal/Parser.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE LambdaCase #-}
12
{-# LANGUAGE OverloadedStrings #-}
23

34
module Hie.Cabal.Parser where
@@ -93,25 +94,23 @@ parseString = parseQuoted <|> unqualName
9394
unqualName :: Parser Text
9495
unqualName = takeWhile1 (not . (\c -> isSpace c || c == ','))
9596

96-
-- | Skip spaces and if enf of line is reached, skip it as well and require that
97+
-- | Skip spaces and if end of line is reached, skip it as well and require that
9798
-- next one starts with indent.
9899
--
99100
-- Used for parsing fields.
100101
optSkipToNextLine :: Indent -> Parser ()
101102
optSkipToNextLine i = do
102103
skipMany $ satisfy (\c -> isSpace c && not (isEndOfLine c))
103-
mChar <- peekChar
104-
case mChar of
104+
skipMany $ skipSpace >> "--" >> takeTill isEndOfLine
105+
peekChar >>= \case
105106
Just c
106107
| isEndOfLine c ->
107-
char c *> indent i $> ()
108+
endOfLine *> indent i $> ()
108109
_ -> pure ()
109110

110111
-- | Comma or space separated list, with optional new lines.
111112
parseList :: Indent -> Parser [Text]
112-
parseList i = items <|> (emptyOrComLine >> indent i >> items)
113-
where
114-
items = sepBy parseString (optSkipToNextLine i *> skipMany (char ',') *> optSkipToNextLine i)
113+
parseList i = sepBy parseString (optSkipToNextLine i *> skipMany (char ',') *> optSkipToNextLine i)
115114

116115
pathMain :: Indent -> [Text] -> Text -> [Text] -> [Text] -> Parser [Text]
117116
pathMain i p m o a =

test/Spec.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ spec = do
8989
it "list with leading commas" $
9090
("one\n , two\n , three3" :: Text) ~> parseList 1
9191
`shouldParse` ["one", "two", "three3"]
92+
describe "Should Succeed" $
93+
it "list with a comment" $
94+
("foo\n -- need to include this too\n bar\n" :: Text) ~> parseList 1
95+
`shouldParse` ["foo", "bar"]
96+
describe "Should Succeed" $
97+
it "list with a comment" $
98+
("foo -- need to include this too\n bar\n" :: Text) ~> parseList 1
99+
`shouldParse` ["foo", "bar"]
100+
describe "Should Succeed" $
101+
it "list with a comment" $
102+
("foo -- need to include this too\n bar" :: Text) ~> parseList 1
103+
`shouldParse` ["foo", "bar"]
104+
describe "Should Succeed" $
105+
it "list with a comment" $
106+
("foo\n bar\n -- need to include this too" :: Text) ~> parseList 1
107+
`shouldParse` ["foo", "bar"]
108+
describe "Should Succeed" $
109+
it "list with a comment" $
110+
("foo\n bar -- need to include this too" :: Text) ~> parseList 1
111+
`shouldParse` ["foo", "bar"]
92112
describe "Should Succeed" $
93113
it "succesfully parses exe component with other-modules containing dots" $
94114
exeSection2 ~> parseExe 0

0 commit comments

Comments
 (0)