Skip to content

Commit

Permalink
lib: save account directive positions, for troubleshooting (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Aug 7, 2022
1 parent 950c85c commit 91e8914
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion hledger-lib/Hledger/Data/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import Data.Time.Clock.POSIX (POSIXTime)
import Data.Time.LocalTime (LocalTime)
import Data.Word (Word8)
import Text.Blaze (ToMarkup(..))
import Text.Megaparsec (SourcePos)
import Text.Megaparsec (SourcePos(SourcePos), mkPos)

import Hledger.Utils.Regex

Expand Down Expand Up @@ -585,12 +585,14 @@ data AccountDeclarationInfo = AccountDeclarationInfo {
,aditags :: [Tag] -- ^ tags extracted from the account comment, if any
,adideclarationorder :: Int -- ^ the order in which this account was declared,
-- relative to other account declarations, during parsing (1..)
,adisourcepos :: SourcePos -- ^ source file and position
} deriving (Eq,Show,Generic)

nullaccountdeclarationinfo = AccountDeclarationInfo {
adicomment = ""
,aditags = []
,adideclarationorder = 0
,adisourcepos = SourcePos "" (mkPos 1) (mkPos 1)
}

-- | An account, with its balances, parent/subaccount relationships, etc.
Expand Down
15 changes: 9 additions & 6 deletions hledger-lib/Hledger/Read/JournalReader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ orRethrowIOError io msg = do
accountdirectivep :: JournalParser m ()
accountdirectivep = do
off <- getOffset -- XXX figure out a more precise position later
pos <- getSourcePos

string "account"
lift skipNonNewlineSpaces1
Expand All @@ -363,7 +364,7 @@ accountdirectivep = do
metype = parseAccountTypeCode <$> lookup accountTypeTagName tags

-- update the journal
addAccountDeclaration (acct, cmt, tags)
addAccountDeclaration (acct, cmt, tags, pos)
unless (null tags) $ addDeclaredAccountTags acct tags
case metype of
Nothing -> return ()
Expand Down Expand Up @@ -396,18 +397,19 @@ parseAccountTypeCode s =
T.intercalate ", " ["A","L","E","R","X","C","V","Asset","Liability","Equity","Revenue","Expense","Cash","Conversion"]

-- Add an account declaration to the journal, auto-numbering it.
addAccountDeclaration :: (AccountName,Text,[Tag]) -> JournalParser m ()
addAccountDeclaration (a,cmt,tags) =
addAccountDeclaration :: (AccountName,Text,[Tag],SourcePos) -> JournalParser m ()
addAccountDeclaration (a,cmt,tags,pos) = do
modify' (\j ->
let
decls = jdeclaredaccounts j
d = (a, nullaccountdeclarationinfo{
adicomment = cmt
,aditags = tags
-- this restarts from 1 in each file, which is not that useful
-- when there are multiple files; so it gets renumbered
-- automatically when combining Journals with <>
,adideclarationorder = length decls + 1
-- this restarts from 1 in each file, which is not that useful
-- when there are multiple files; so it gets renumbered
-- automatically when combining Journals with <>
,adisourcepos = pos
})
in
j{jdeclaredaccounts = d:decls})
Expand Down Expand Up @@ -1017,6 +1019,7 @@ tests_JournalReader = testGroup "JournalReader" [
[("a:b", AccountDeclarationInfo{adicomment = "type:asset\n"
,aditags = [("type","asset")]
,adideclarationorder = 1
,adisourcepos = fst nullsourcepos
})
]
]
Expand Down

0 comments on commit 91e8914

Please sign in to comment.