imp: at --debug 5, log account declarations info while parsing (#1909)

This commit is contained in:
Simon Michael 2022-08-07 10:41:24 +01:00
parent 3d6e363461
commit 07e3dca735

View File

@ -185,9 +185,11 @@ reader = Reader
-- | Parse and post-process a "Journal" from hledger's journal file -- | Parse and post-process a "Journal" from hledger's journal file
-- format, or give an error. -- format, or give an error.
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
parse iopts = parseAndFinaliseJournal journalp' iopts parse iopts f = parseAndFinaliseJournal journalp' iopts f
where where
journalp' = do journalp' =
-- debug logging for account display order
dbgJournalAcctDeclOrder (takeFileName f <> " acct decls: ") <$> do
-- reverse parsed aliases to ensure that they are applied in order given on commandline -- reverse parsed aliases to ensure that they are applied in order given on commandline
mapM_ addAccountAlias (reverse $ aliasesFromOpts iopts) mapM_ addAccountAlias (reverse $ aliasesFromOpts iopts)
journalp journalp
@ -302,20 +304,40 @@ includedirectivep = do
`orRethrowIOError` (show parentpos ++ " reading " ++ filepath) `orRethrowIOError` (show parentpos ++ " reading " ++ filepath)
let initChildj = newJournalWithParseStateFrom filepath parentj let initChildj = newJournalWithParseStateFrom filepath parentj
-- Choose a reader/format based on the file path, or fall back -- Choose a reader/parser based on the file path prefix or file extension,
-- on journal. Duplicating readJournal a bit here. -- defaulting to JournalReader. Duplicating readJournal a bit here.
let r = fromMaybe reader $ findReader Nothing (Just prefixedpath) let r = fromMaybe reader $ findReader Nothing (Just prefixedpath)
parser = rParser r parser = rParser r
dbg6IO "trying reader" (rFormat r) dbg6IO "trying reader" (rFormat r)
-- Included files's lists are still reversed, because not yet journalFinalise'd,
-- which confuses the calculation of account declaration order across multiple files (#1909). -- Parse the file (of whichever format) to a Journal, with file path and source text attached.
-- Unreverse just the acct declarations to fix that without disturbing anything else. updatedChildj <- (journalAddFile (filepath, childInput)) <$>
let reversedecls j = j{jdeclaredaccounts = reverse $ jdeclaredaccounts j}
updatedChildj <- (journalAddFile (filepath, childInput) . reversedecls) <$>
parseIncludeFile parser initChildj filepath childInput parseIncludeFile parser initChildj filepath childInput
-- discard child's parse info, combine other fields -- Merge this child journal into the parent journal using Journal's Semigroup instance
put $ updatedChildj <> parentj -- (with lots of debug logging for troubleshooting account display order).
let
parentj' =
dbgJournalAcctDeclOrder (" " <> parentfilename <> " acct decls now : ")
$
(
-- The child journal has not yet been finalises and its lists are still reversed.
-- To help calculate account declaration order across files (#1909),
-- unreverse just the acct declarations without disturbing anything else.
-- XXX still shows wrong order in some cases
reverseAcctDecls $
dbgJournalAcctDeclOrder (childfilename <> " include file acct decls: ") updatedChildj
)
<>
dbgJournalAcctDeclOrder (" " <> parentfilename <> " acct decls were: ") parentj
where
reverseAcctDecls j = j{jdeclaredaccounts = reverse $ jdeclaredaccounts j}
childfilename = takeFileName filepath
parentfilename = maybe "" takeFileName $ headMay $ jincludefilestack parentj -- more accurate than journalFilePath parentj somehow
-- Update the parse state.
put parentj'
newJournalWithParseStateFrom :: FilePath -> Journal -> Journal newJournalWithParseStateFrom :: FilePath -> Journal -> Journal
newJournalWithParseStateFrom filepath j = nulljournal{ newJournalWithParseStateFrom filepath j = nulljournal{
@ -330,6 +352,22 @@ includedirectivep = do
,jincludefilestack = filepath : jincludefilestack j ,jincludefilestack = filepath : jincludefilestack j
} }
dbgJournalAcctDeclOrder :: String -> Journal -> Journal
dbgJournalAcctDeclOrder prefix
| debugLevel >= 5 = traceWith ((prefix++) . showAcctDeclsSummary . jdeclaredaccounts)
| otherwise = id
where
showAcctDeclsSummary :: [(AccountName,AccountDeclarationInfo)] -> String
showAcctDeclsSummary adis
| length adis < (2*num+2) = "[" <> showadis adis <> "]"
| otherwise =
"[" <> showadis (take num adis) <> " ... " <> showadis (takelast num adis) <> "]"
where
num = 3
showadis = intercalate ", " . map showadi
showadi (a,adi) = "("<>show (adideclarationorder adi)<>","<>T.unpack a<>")"
takelast n = reverse . take n . reverse
-- | Lift an IO action into the exception monad, rethrowing any IO -- | Lift an IO action into the exception monad, rethrowing any IO
-- error with the given message prepended. -- error with the given message prepended.
orRethrowIOError :: MonadIO m => IO a -> String -> TextParser m a orRethrowIOError :: MonadIO m => IO a -> String -> TextParser m a