lib: save account directive positions, for troubleshooting (#1909)

This commit is contained in:
Simon Michael 2022-08-07 10:36:25 +01:00
parent f5680529cd
commit 3d6e363461
2 changed files with 12 additions and 7 deletions

View File

@ -49,7 +49,7 @@ import Data.Time.Clock.POSIX (POSIXTime)
import Data.Time.LocalTime (LocalTime) import Data.Time.LocalTime (LocalTime)
import Data.Word (Word8) import Data.Word (Word8)
import Text.Blaze (ToMarkup(..)) import Text.Blaze (ToMarkup(..))
import Text.Megaparsec (SourcePos) import Text.Megaparsec (SourcePos(SourcePos), mkPos)
import Hledger.Utils.Regex import Hledger.Utils.Regex
@ -585,12 +585,14 @@ data AccountDeclarationInfo = AccountDeclarationInfo {
,aditags :: [Tag] -- ^ tags extracted from the account comment, if any ,aditags :: [Tag] -- ^ tags extracted from the account comment, if any
,adideclarationorder :: Int -- ^ the order in which this account was declared, ,adideclarationorder :: Int -- ^ the order in which this account was declared,
-- relative to other account declarations, during parsing (1..) -- relative to other account declarations, during parsing (1..)
,adisourcepos :: SourcePos -- ^ source file and position
} deriving (Eq,Show,Generic) } deriving (Eq,Show,Generic)
nullaccountdeclarationinfo = AccountDeclarationInfo { nullaccountdeclarationinfo = AccountDeclarationInfo {
adicomment = "" adicomment = ""
,aditags = [] ,aditags = []
,adideclarationorder = 0 ,adideclarationorder = 0
,adisourcepos = SourcePos "" (mkPos 1) (mkPos 1)
} }
-- | An account, with its balances, parent/subaccount relationships, etc. -- | An account, with its balances, parent/subaccount relationships, etc.

View File

@ -344,6 +344,7 @@ orRethrowIOError io msg = do
accountdirectivep :: JournalParser m () accountdirectivep :: JournalParser m ()
accountdirectivep = do accountdirectivep = do
off <- getOffset -- XXX figure out a more precise position later off <- getOffset -- XXX figure out a more precise position later
pos <- getSourcePos
string "account" string "account"
lift skipNonNewlineSpaces1 lift skipNonNewlineSpaces1
@ -363,7 +364,7 @@ accountdirectivep = do
metype = parseAccountTypeCode <$> lookup accountTypeTagName tags metype = parseAccountTypeCode <$> lookup accountTypeTagName tags
-- update the journal -- update the journal
addAccountDeclaration (acct, cmt, tags) addAccountDeclaration (acct, cmt, tags, pos)
unless (null tags) $ addDeclaredAccountTags acct tags unless (null tags) $ addDeclaredAccountTags acct tags
case metype of case metype of
Nothing -> return () Nothing -> return ()
@ -396,18 +397,19 @@ parseAccountTypeCode s =
T.intercalate ", " ["A","L","E","R","X","C","V","Asset","Liability","Equity","Revenue","Expense","Cash","Conversion"] 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. -- Add an account declaration to the journal, auto-numbering it.
addAccountDeclaration :: (AccountName,Text,[Tag]) -> JournalParser m () addAccountDeclaration :: (AccountName,Text,[Tag],SourcePos) -> JournalParser m ()
addAccountDeclaration (a,cmt,tags) = addAccountDeclaration (a,cmt,tags,pos) = do
modify' (\j -> modify' (\j ->
let let
decls = jdeclaredaccounts j decls = jdeclaredaccounts j
d = (a, nullaccountdeclarationinfo{ d = (a, nullaccountdeclarationinfo{
adicomment = cmt adicomment = cmt
,aditags = tags ,aditags = tags
,adideclarationorder = length decls + 1
-- this restarts from 1 in each file, which is not that useful -- this restarts from 1 in each file, which is not that useful
-- when there are multiple files; so it gets renumbered -- when there are multiple files; so it gets renumbered
-- automatically when combining Journals with <> -- automatically when combining Journals with <>
,adideclarationorder = length decls + 1
,adisourcepos = pos
}) })
in in
j{jdeclaredaccounts = d:decls}) j{jdeclaredaccounts = d:decls})
@ -1017,6 +1019,7 @@ tests_JournalReader = testGroup "JournalReader" [
[("a:b", AccountDeclarationInfo{adicomment = "type:asset\n" [("a:b", AccountDeclarationInfo{adicomment = "type:asset\n"
,aditags = [("type","asset")] ,aditags = [("type","asset")]
,adideclarationorder = 1 ,adideclarationorder = 1
,adisourcepos = fst nullsourcepos
}) })
] ]
] ]