From b06d22a418dd3ed0474f4a855268e565662d99dd Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Thu, 10 May 2018 15:23:19 -0600 Subject: [PATCH] lib: Refactor common comment parsers --- hledger-lib/Hledger/Read/Common.hs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index d12461159..72419da65 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -795,26 +795,26 @@ whitespaceChar = charCategory Space --- ** comments multilinecommentp :: JournalParser m () -multilinecommentp = do - string "comment" >> lift (skipMany spacenonewline) >> newline - go +multilinecommentp = startComment *> anyLine `skipManyTill` endComment where - go = try (eof <|> (string "end comment" >> newline >> return ())) - <|> (anyLine >> go) + emptylinep = lift (skipMany spacenonewline) *> newline *> pure () + startComment = string "comment" >> emptylinep + endComment = eof <|> (string "end comment" >> emptylinep) anyLine = anyChar `manyTill` newline emptyorcommentlinep :: JournalParser m () emptyorcommentlinep = do - lift (skipMany spacenonewline) >> (linecommentp <|> (lift (skipMany spacenonewline) >> newline >> return "")) - return () + lift (skipMany spacenonewline) + void linecommentp <|> void newline -- | Parse a possibly multi-line comment following a semicolon. followingcommentp :: JournalParser m Text -followingcommentp = +followingcommentp = do -- ptrace "followingcommentp" - do samelinecomment <- lift (skipMany spacenonewline) >> (try commentp <|> (newline >> return "")) - newlinecomments <- many (try (lift (skipSome spacenonewline) >> commentp)) - return $ T.unlines $ samelinecomment:newlinecomments + lift (skipMany spacenonewline) + samelinecomment <- try commentp <|> (newline >> return "") + newlinecomments <- many $ try $ lift (skipSome spacenonewline) >> commentp + return $ T.unlines $ samelinecomment:newlinecomments -- | Parse a possibly multi-line comment following a semicolon, and -- any tags and/or posting dates within it. Posting dates can be