Skip to content

Improve list parser #12

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

Merged
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
26 changes: 16 additions & 10 deletions src/Hie/Cabal/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Control.Applicative
import Control.Monad
import Data.Attoparsec.Text
import Data.Char
import Data.Functor
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -92,19 +93,24 @@ parseString = parseQuoted <|> unqualName
unqualName :: Parser Text
unqualName = takeWhile1 (not . (\c -> isSpace c || c == ','))

-- | Skip spaces and if enf of line is reached, skip it as well and require that
-- next one starts with indent.
--
-- Used for parsing fields.
optSkipToNextLine :: Indent -> Parser ()
optSkipToNextLine i = do
skipMany $ satisfy (\c -> isSpace c && not (isEndOfLine c))
mChar <- peekChar
case mChar of
Just c | isEndOfLine c ->
char c *> indent i $> ()
_ -> pure ()

-- | Comma or space separated list, with optional new lines.
parseList :: Indent -> Parser [Text]
parseList i = items <|> (emptyOrComLine >> indent i >> items)
where
items = do
skipMany tabOrSpace
h <- parseString
skipMany tabOrSpace
skipMany (char ',')
t <-
items
<|> (skipToNextLine >> indent i >> parseList i)
<|> pure []
pure $ h : t
items = sepBy parseString (optSkipToNextLine i *> skipMany (char ',') *> optSkipToNextLine i)

pathMain :: Indent -> [Text] -> Text -> [Text] -> [Text] -> Parser [Text]
pathMain i p m o a =
Expand Down
4 changes: 4 additions & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ spec = do
$ it "quoted list"
$ ("\"one\"\n two\n three3" :: Text) ~> parseList 1
`shouldParse` ["one", "two", "three3"]
describe "Should Succeed"
$ it "list with leading commas"
$ ("one\n , two\n , three3" :: Text) ~> parseList 1
`shouldParse` ["one", "two", "three3"]

exeSection :: Text
exeSection =
Expand Down