From 5e1f0ba6f7b229b84f9139530afd40e9fad5f4cc Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Sat, 29 Sep 2018 20:33:17 -0600 Subject: [PATCH] lib: add a field to 'Journal' for a stack of include files - In anticipation of megaparsec 7, which removes support for stacks of include files (as far as I can tell) - Intended for the 'StateT Journal' layer of the parser - A stack of include files would be better in a 'ReaderT' layer, but I don't want to add another layer to the parser - Intended for detecting cycles of include files - Potential issue: for proper error messages for include file cycles, we must remember to provide the filepath of the root journal file via the initial journal state passed to a 'JournalParser'; I imagine that we may forget to do so because in all other cases it is okay not to do so. --- hledger-lib/Hledger/Data/Journal.hs | 6 ++++-- hledger-lib/Hledger/Data/Types.hs | 1 + hledger-lib/Hledger/Read/Common.hs | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index bf6986f14..41e04cdaf 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -163,6 +163,7 @@ instance Sem.Semigroup Journal where ,jparsealiases = jparsealiases j2 -- ,jparsetransactioncount = jparsetransactioncount j1 + jparsetransactioncount j2 ,jparsetimeclockentries = jparsetimeclockentries j1 <> jparsetimeclockentries j2 + ,jincludefilestack = jincludefilestack j2 ,jdeclaredaccounts = jdeclaredaccounts j1 <> jdeclaredaccounts j2 ,jcommodities = jcommodities j1 <> jcommodities j2 ,jinferredcommodities = jinferredcommodities j1 <> jinferredcommodities j2 @@ -189,8 +190,9 @@ nulljournal = Journal { ,jparseparentaccounts = [] ,jparsealiases = [] -- ,jparsetransactioncount = 0 - ,jparsetimeclockentries = [] - ,jdeclaredaccounts = [] + ,jparsetimeclockentries = [] + ,jincludefilestack = [] + ,jdeclaredaccounts = [] ,jcommodities = M.fromList [] ,jinferredcommodities = M.fromList [] ,jmarketprices = [] diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index ee24fcf52..8d41ec9cd 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -363,6 +363,7 @@ data Journal = Journal { ,jparsealiases :: [AccountAlias] -- ^ the current account name aliases in effect, specified by alias directives (& options ?) -- ,jparsetransactioncount :: Integer -- ^ the current count of transactions parsed so far (only journal format txns, currently) ,jparsetimeclockentries :: [TimeclockEntry] -- ^ timeclock sessions which have not been clocked out + ,jincludefilestack :: [FilePath] -- principal data ,jdeclaredaccounts :: [AccountName] -- ^ Accounts declared by account directives, in parse order (after journal finalisation) ,jcommodities :: M.Map CommoditySymbol Commodity -- ^ commodities and formats declared by commodity directives diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index c760c3d3a..1cf574f21 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -226,7 +226,10 @@ parseAndFinaliseJournal :: JournalParser IO ParsedJournal -> InputOpts parseAndFinaliseJournal parser iopts f txt = do t <- liftIO getClockTime y <- liftIO getCurrentYear - ep <- liftIO $ runParserT (evalStateT parser nulljournal {jparsedefaultyear=Just y}) f txt + let initJournal = nulljournal + { jparsedefaultyear = Just y + , jincludefilestack = [f] } + ep <- liftIO $ runParserT (evalStateT parser initJournal) f txt case ep of Right pj -> let pj' = if auto_ iopts then applyTransactionModifiers pj else pj in