From 4b36d852b4dc0fa5f4d9a2aed0e207747f3d4328 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 22 Dec 2022 20:06:28 -1000 Subject: [PATCH] imp: journal: ignore ((valuation expressions)) after amounts (ledger compat) --- hledger-lib/Hledger/Read/Common.hs | 31 ++++++++++++++----- hledger/hledger.m4.md | 5 +++ .../ledger-compat/syntax/hledger-other.test | 10 +++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index ef462168e..b20faac7f 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -685,10 +685,12 @@ spaceandamountormissingp = -- | Parse a single-commodity amount, applying the default commodity if there is no commodity symbol; -- optionally followed by, in any order: --- a Ledger-style cost and/or one or more parts of a Ledger-style cost basis: +-- a Ledger-style cost, Ledger-style valuation expression, and/or Ledger-style cost basis, which is one or more of -- lot cost, lot date, and/or lot note (we loosely call this triple the lot's cost basis). --- The cost basis makes it a lot rather than just an amount. The cost basis info is discarded for now. --- The main amount's sign is significant; here are the possibilities and their interpretation: +-- The cost basis makes it a lot rather than just an amount. Both cost basis info and valuation expression +-- are discarded for now. +-- The main amount's sign is significant; here are the possibilities and their interpretation. +-- Also imagine an optional VALUATIONEXPR added to any of these (omitted for clarity): -- @ -- -- AMT -- acquiring an amount @@ -707,9 +709,12 @@ spaceandamountormissingp = -- COSTBASIS is one or more of {LOTCOST}, [LOTDATE], (LOTNOTE), in any order, with LOTCOST defaulting to COST. -- COSTBASISSEL is one or more of {LOTCOST}, [LOTDATE], (LOTNOTE), in any order. -- {LOTCOST} can be {UNITAMT}, {{TOTALAMT}}, {=UNITAMT}, or {{=TOTALAMT}}. The = is ignored. --- Rule of thumb: curly braces, parentheses, and/or square brackets in an amount means a Ledger-style cost basis is involved. +-- VALUATIONEXPR can be ((VALUE AMOUNT)) or ((VALUE FUNCTION)). -- -- @ +-- Ledger amount syntax is really complex. +-- Rule of thumb: curly braces, parentheses, and/or square brackets +-- in an amount means a Ledger-style cost basis is involved. -- -- To parse an amount's numeric quantity we need to know which character -- represents a decimal mark. We find it in one of three ways: @@ -729,7 +734,7 @@ spaceandamountormissingp = amountp :: JournalParser m Amount amountp = amountp' False --- An amount with optional cost and/or cost basis, as described above. +-- An amount with optional cost, valuation, and/or cost basis, as described above. -- A flag indicates whether we are parsing a multiplier amount; -- if not, a commodity-less amount will have the default commodity applied to it. amountp' :: Bool -> JournalParser m Amount @@ -738,9 +743,10 @@ amountp' mult = label "amount" $ do let spaces = lift $ skipNonNewlineSpaces amt <- simpleamountp mult <* spaces - (mcost, _mlotcost, _mlotdate, _mlotnote) <- runPermutation $ - -- need a try on costp so that it doesn't consume the ( of a lot note - (,,,) <$> toPermutationWithDefault Nothing (Just <$> try (costp amt) <* spaces) + (mcost, _valuationexpr, _mlotcost, _mlotdate, _mlotnote) <- runPermutation $ + -- costp, valuationexprp, lotnotep all parse things beginning with parenthesis, try needed + (,,,,) <$> toPermutationWithDefault Nothing (Just <$> try (costp amt) <* spaces) + <*> toPermutationWithDefault Nothing (Just <$> valuationexprp <* spaces) -- XXX no try needed here ? <*> toPermutationWithDefault Nothing (Just <$> lotcostp <* spaces) <*> toPermutationWithDefault Nothing (Just <$> lotdatep <* spaces) <*> toPermutationWithDefault Nothing (Just <$> lotnotep <* spaces) @@ -899,6 +905,15 @@ costp baseAmt = then TotalPrice priceAmount{aquantity=amtsign * aquantity priceAmount} else UnitPrice priceAmount +-- | A valuation function or value can be written in double parentheses after an amount. +valuationexprp :: JournalParser m () +valuationexprp = + -- dbg "valuationexprp" $ + label "valuation expression" $ do + string "((" + _ <- T.strip . T.pack <$> (many $ noneOf [')','\n']) -- XXX other line endings ? + string "))" + return () balanceassertionp :: JournalParser m BalanceAssertion balanceassertionp = do diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 784769ccf..e33463320 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -2696,6 +2696,11 @@ And switching to Emacs org mode just for folding/unfolding meant losing the bene nowadays you can add outshine mode to ledger mode to get folding without losing ledger mode's features. +### Valuation expressions + +Ledger allows a valuation function or value to be written in double parentheses after an amount. +hledger ignores these. + ### Virtual postings A posting with parentheses around the account name is called a *virtual posting* or *unbalanced posting*, diff --git a/hledger/test/ledger-compat/syntax/hledger-other.test b/hledger/test/ledger-compat/syntax/hledger-other.test index b86af42ef..ece6e7578 100644 --- a/hledger/test/ledger-compat/syntax/hledger-other.test +++ b/hledger/test/ledger-compat/syntax/hledger-other.test @@ -79,12 +79,20 @@ eval foo $ hledger -f- check +# amount valuation, ignored +< +;; Lastly, you can specify the valuation function/value for any specific +;; amount using the (( )) commodity annotation. +2012-03-02 KFC + Expenses:Food $1 ((2 EUR)) + Assets:Cash +$ hledger -f- check # lot notation < 2022-01-01 sell 5 AAPL acquired at $50 for $375, for a $125 gain Assets:Brokerage:Cash $375.00 - Assets:Brokerage -5 AAPL {$50.00} [2012-04-10] (a lot note) (@@) $375.00 ; using (@@) to make parsing harder + Assets:Brokerage -5 AAPL {$50.00} [2012-04-10] (a lot note) (($400)) (@@) $375.00 ; use lots of tricky syntax Income:Capital Gains $-125.00 $ hledger -f- check