From 188583e23255475e841b7da5645d227c4d9f0bc6 Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Wed, 16 May 2018 14:18:10 -0600 Subject: [PATCH] lib: cleanup: remove redundant comment parsing alternatives --- hledger-lib/Hledger/Read/Common.hs | 2 +- hledger-lib/Hledger/Read/JournalReader.hs | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 5e25f456e..34a62812a 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -835,7 +835,7 @@ followingcommentlinesp :: TextParser m [(SourcePos, Text)] followingcommentlinesp = do skipMany spacenonewline samelineComment@(_, samelineCommentText) - <- try commentp <|> (,) <$> (getPosition <* newline) <*> pure "" + <- try commentp <|> (,) <$> (getPosition <* eolof) <*> pure "" newlineComments <- many $ try $ do skipSome spacenonewline -- leading whitespace is required commentp diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index f01df37bf..b58ab7827 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -281,7 +281,7 @@ commoditydirectiveonelinep = do pos <- getPosition Amount{acommodity,astyle} <- amountp lift (skipMany spacenonewline) - _ <- lift followingcommentp <|> (lift eolof >> return "") + _ <- lift followingcommentp let comm = Commodity{csymbol=acommodity, cformat=Just $ dbg2 "style from commodity directive" astyle} if asdecimalpoint astyle == Nothing then parserErrorAt pos pleaseincludedecimalpoint @@ -298,7 +298,7 @@ commoditydirectivemultilinep = do string "commodity" lift (skipSome spacenonewline) sym <- lift commoditysymbolp - _ <- lift followingcommentp <|> (lift eolof >> return "") + _ <- lift followingcommentp mformat <- lastMay <$> many (indented $ formatdirectivep sym) let comm = Commodity{csymbol=sym, cformat=mformat} modify' (\j -> j{jcommodities=M.insert sym comm $ jcommodities j}) @@ -313,7 +313,7 @@ formatdirectivep expectedsym = do lift (skipSome spacenonewline) pos <- getPosition Amount{acommodity,astyle} <- amountp - _ <- lift followingcommentp <|> (lift eolof >> return "") + _ <- lift followingcommentp if acommodity==expectedsym then if asdecimalpoint astyle == Nothing @@ -463,7 +463,7 @@ periodictransactionp = do char '~' "periodic transaction" lift (skipMany spacenonewline) periodexpr <- T.pack . strip <$> descriptionp - _ <- try (lift followingcommentp) <|> (newline >> return "") + _ <- lift followingcommentp postings <- postingsp Nothing return $ PeriodicTransaction periodexpr postings @@ -478,7 +478,7 @@ transactionp = do status <- lift statusp "cleared status" code <- T.pack <$> lift codep "transaction code" description <- T.pack . strip <$> descriptionp - comment <- try (lift followingcommentp) <|> (newline >> return "") + comment <- lift followingcommentp let tags = commentTags comment postings <- postingsp (Just date) pos' <- getPosition @@ -606,8 +606,7 @@ postingp mtdate = do massertion <- partialbalanceassertionp _ <- fixedlotpricep lift (skipMany spacenonewline) - (comment,tags,mdate,mdate2) <- - try (followingcommentandtagsp mtdate) <|> (newline >> return ("",[],Nothing,Nothing)) + (comment,tags,mdate,mdate2) <- followingcommentandtagsp mtdate return posting { pdate=mdate , pdate2=mdate2