From 28baf926f7fb33d984cebbdbc38a57c4d4b8209b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 2 Nov 2012 16:50:36 +0000 Subject: [PATCH] parsing: ignore ledger-style balance assertions ("= BAL" after a posting's amount) --- hledger-lib/Hledger/Read/JournalReader.hs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 05ac8b3eb..f9c86631c 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -488,6 +488,7 @@ posting = do account <- modifiedaccountname let (ptype, account') = (accountNamePostingType account, unbracket account) amount <- spaceandamountormissing + _ <- balanceassertion many spacenonewline (inlinecomment, inlinetag) <- inlinecomment (nextlinecomments, nextlinetags) <- commentlines @@ -516,6 +517,9 @@ tests_posting = [ assertBool "posting parses a quoted commodity with numbers" (isRight $ parseWithCtx nullctx posting " a 1 \"DE123\"\n") + + ,"posting parses a balance assertion" ~: do + assertBool "" (isRight $ parseWithCtx nullctx posting " a 1 \"DE123\" =$1\n") ] -- | Parse an account name, then apply any parent account prefix and/or account aliases currently in effect. @@ -564,8 +568,8 @@ tests_spaceandamountormissing = [ assertParseEqual (parseWithCtx nullctx spaceandamountormissing "") missingmixedamt ] --- | Parse an amount, with an optional left or right currency symbol and --- optional price. +-- | Parse an amount, optionally with a left or right currency symbol, +-- price, and/or (ignored) ledger-style balance assertion. amount :: GenParser Char JournalContext MixedAmount amount = try leftsymbolamount <|> try rightsymbolamount <|> nosymbolamount @@ -656,6 +660,16 @@ priceamount = return $ Just $ UnitPrice a)) <|> return Nothing +balanceassertion :: GenParser Char JournalContext (Maybe MixedAmount) +balanceassertion = + try (do + many spacenonewline + char '=' + many spacenonewline + a <- amount -- XXX should restrict to a simple amount + return $ Just a) + <|> return Nothing + -- | Parse a numeric quantity for its value and display attributes. Some -- international number formats (cf -- http://en.wikipedia.org/wiki/Decimal_separator) are accepted: either