Skip to content

Commit

Permalink
Make Cartfile Parser tolerant to missing new line at end of file
Browse files Browse the repository at this point in the history
  • Loading branch information
tmspzz committed Apr 7, 2017
1 parent 9d485a8 commit c02df6e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Data/Carthage/Cartfile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ parseCartfileResolvedLine = do

parseMaybeCartfileEntry :: Parsec.Parsec String () (Maybe CartfileEntry)
parseMaybeCartfileEntry = Parsec.optional Parsec.spaces
*> (parseCartfileResolvedLine `Parsec.onceOrConsumeTill` Parsec.endOfLine)
*> (parseCartfileResolvedLine `Parsec.onceAndConsumeTill` Parsec.endOfLine)

parseCartfileResolved :: MonadIO m => String -> m (Either Parsec.ParseError [CartfileEntry])
parseCartfileResolved = liftIO . Parsec.parseFromFile (catMaybes <$> (parseMaybeCartfileEntry `Parsec.manyTill` Parsec.eof))
parseCartfileResolved = liftIO . Parsec.parseFromFile (catMaybes <$> Parsec.many (Parsec.try parseMaybeCartfileEntry))
15 changes: 7 additions & 8 deletions src/Text/Parsec/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Text.Parsec.Utils
( parseWhiteSpaces
, parseUnquotedString
, onceOrConsumeTill
, onceAndConsumeTill
) where


Expand All @@ -24,11 +24,10 @@ parseUnquotedString = Parsec.many1 (Parsec.noneOf ['"', ' ', '\t', '\n', '\'', '
-- | @onceOrConsumeTill p@ end@ tries to apply the parser @p@ /once/ and consumes
-- | the input until @end@. Returns a `Maybe` of the value of @p@.
-- | Thanks to Tobias Mayer, Berlin Haskell Group.
onceOrConsumeTill :: (Parsec.Stream s Identity t, Show t)
=> Parsec.Parsec s u a
-> Parsec.Parsec s u b
-> Parsec.Parsec s u (Maybe a)
onceOrConsumeTill p end = Parsec.optionMaybe p <* consume
onceAndConsumeTill :: (Parsec.Stream s Identity Char)
=> Parsec.Parsec s u a
-> Parsec.Parsec s u b
-> Parsec.Parsec s u (Maybe a)
onceAndConsumeTill p end = Parsec.optionMaybe (Parsec.try p) <* consume
where
consume = do { end; return ()}
<|> Parsec.anyToken *> consume
consume = do { end; return () } <|> Parsec.anyChar *> consume

0 comments on commit c02df6e

Please sign in to comment.