Fix: #24 handle comments in lists

This commit is contained in:
Avi Dessauer 2020-11-11 20:20:48 -05:00
parent 45165dcb21
commit aed4155c91
2 changed files with 26 additions and 7 deletions

View File

@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Hie.Cabal.Parser where module Hie.Cabal.Parser where
@ -93,25 +94,23 @@ parseString = parseQuoted <|> unqualName
unqualName :: Parser Text unqualName :: Parser Text
unqualName = takeWhile1 (not . (\c -> isSpace c || c == ',')) unqualName = takeWhile1 (not . (\c -> isSpace c || c == ','))
-- | Skip spaces and if enf of line is reached, skip it as well and require that -- | Skip spaces and if end of line is reached, skip it as well and require that
-- next one starts with indent. -- next one starts with indent.
-- --
-- Used for parsing fields. -- Used for parsing fields.
optSkipToNextLine :: Indent -> Parser () optSkipToNextLine :: Indent -> Parser ()
optSkipToNextLine i = do optSkipToNextLine i = do
skipMany $ satisfy (\c -> isSpace c && not (isEndOfLine c)) skipMany $ satisfy (\c -> isSpace c && not (isEndOfLine c))
mChar <- peekChar skipMany $ skipSpace >> "--" >> takeTill isEndOfLine
case mChar of peekChar >>= \case
Just c Just c
| isEndOfLine c -> | isEndOfLine c ->
char c *> indent i $> () endOfLine *> indent i $> ()
_ -> pure () _ -> pure ()
-- | Comma or space separated list, with optional new lines. -- | Comma or space separated list, with optional new lines.
parseList :: Indent -> Parser [Text] parseList :: Indent -> Parser [Text]
parseList i = items <|> (emptyOrComLine >> indent i >> items) parseList i = sepBy parseString (optSkipToNextLine i *> skipMany (char ',') *> optSkipToNextLine i)
where
items = sepBy parseString (optSkipToNextLine i *> skipMany (char ',') *> optSkipToNextLine i)
pathMain :: Indent -> [Text] -> Text -> [Text] -> [Text] -> Parser [Text] pathMain :: Indent -> [Text] -> Text -> [Text] -> [Text] -> Parser [Text]
pathMain i p m o a = pathMain i p m o a =

View File

@ -89,6 +89,26 @@ spec = do
it "list with leading commas" $ it "list with leading commas" $
("one\n , two\n , three3" :: Text) ~> parseList 1 ("one\n , two\n , three3" :: Text) ~> parseList 1
`shouldParse` ["one", "two", "three3"] `shouldParse` ["one", "two", "three3"]
describe "Should Succeed" $
it "list with a comment" $
("foo\n -- need to include this too\n bar\n" :: Text) ~> parseList 1
`shouldParse` ["foo", "bar"]
describe "Should Succeed" $
it "list with a comment" $
("foo -- need to include this too\n bar\n" :: Text) ~> parseList 1
`shouldParse` ["foo", "bar"]
describe "Should Succeed" $
it "list with a comment" $
("foo -- need to include this too\n bar" :: Text) ~> parseList 1
`shouldParse` ["foo", "bar"]
describe "Should Succeed" $
it "list with a comment" $
("foo\n bar\n -- need to include this too" :: Text) ~> parseList 1
`shouldParse` ["foo", "bar"]
describe "Should Succeed" $
it "list with a comment" $
("foo\n bar -- need to include this too" :: Text) ~> parseList 1
`shouldParse` ["foo", "bar"]
describe "Should Succeed" $ describe "Should Succeed" $
it "succesfully parses exe component with other-modules containing dots" $ it "succesfully parses exe component with other-modules containing dots" $
exeSection2 ~> parseExe 0 exeSection2 ~> parseExe 0