remember all included file paths, if any

This commit is contained in:
Simon Michael 2010-09-22 23:02:19 +00:00
parent 96036e56a0
commit 8429df0f32
4 changed files with 12 additions and 4 deletions

View File

@ -41,6 +41,7 @@ nulljournal = Journal { jmodifiertxns = []
, historical_prices = [] , historical_prices = []
, final_comment_lines = [] , final_comment_lines = []
, filepath = "" , filepath = ""
, allfilepaths = []
, filereadtime = TOD 0 0 , filereadtime = TOD 0 0
, jtext = "" , jtext = ""
} }
@ -216,7 +217,7 @@ journalFinalise :: ClockTime -> LocalTime -> FilePath -> String -> Journal -> Jo
journalFinalise tclock tlocal path txt j = journalCanonicaliseAmounts $ journalFinalise tclock tlocal path txt j = journalCanonicaliseAmounts $
journalApplyHistoricalPrices $ journalApplyHistoricalPrices $
journalCloseTimeLogEntries tlocal journalCloseTimeLogEntries tlocal
j{filepath=path, filereadtime=tclock, jtext=txt} j{filepath=path, allfilepaths=path:(allfilepaths j), filereadtime=tclock, jtext=txt}
-- | Convert all the journal's amounts to their canonical display -- | Convert all the journal's amounts to their canonical display
-- settings. Ie, all amounts in a given commodity will use (a) the -- settings. Ie, all amounts in a given commodity will use (a) the

View File

@ -129,6 +129,7 @@ data Journal = Journal {
historical_prices :: [HistoricalPrice], historical_prices :: [HistoricalPrice],
final_comment_lines :: String, -- ^ any trailing comments from the journal file final_comment_lines :: String, -- ^ any trailing comments from the journal file
filepath :: FilePath, -- ^ file path of this journal filepath :: FilePath, -- ^ file path of this journal
allfilepaths :: [FilePath], -- ^ file paths of this and any included journals
filereadtime :: ClockTime, -- ^ when this journal was read from its file filereadtime :: ClockTime, -- ^ when this journal was read from its file
jtext :: String -- ^ the raw text read from the journal's file jtext :: String -- ^ the raw text read from the journal's file
} deriving (Eq, Typeable) } deriving (Eq, Typeable)

View File

@ -28,6 +28,9 @@ data Reader = Reader {rFormat :: String
-- or raise an error. -- or raise an error.
type JournalUpdate = ErrorT String IO (Journal -> Journal) type JournalUpdate = ErrorT String IO (Journal -> Journal)
juSequence :: [JournalUpdate] -> JournalUpdate
juSequence us = liftM (foldr (.) id) $ sequence us
-- | Given a JournalUpdate-generating parsec parser, file path and data string, -- | Given a JournalUpdate-generating parsec parser, file path and data string,
-- parse and post-process a Journal so that it's ready to use, or give an error. -- parse and post-process a Journal so that it's ready to use, or give an error.
parseJournalWith :: (GenParser Char JournalContext JournalUpdate) -> FilePath -> String -> ErrorT String IO Journal parseJournalWith :: (GenParser Char JournalContext JournalUpdate) -> FilePath -> String -> ErrorT String IO Journal

View File

@ -156,9 +156,9 @@ parse = parseJournalWith journalFile
-- error-raising "JournalUpdate" which can be applied to an empty journal -- error-raising "JournalUpdate" which can be applied to an empty journal
-- to get the final result. -- to get the final result.
journalFile :: GenParser Char JournalContext JournalUpdate journalFile :: GenParser Char JournalContext JournalUpdate
journalFile = do items <- many journalItem journalFile = do journalupdates <- many journalItem
eof eof
return $ liftM (foldr (.) id) $ sequence items return $ juSequence journalupdates
where where
-- As all journal line types can be distinguished by the first -- As all journal line types can be distinguished by the first
-- character, excepting transactions versus empty (blank or -- character, excepting transactions versus empty (blank or
@ -175,6 +175,9 @@ journalFile = do items <- many journalItem
, emptyLine >> return (return id) , emptyLine >> return (return id)
] <?> "journal transaction or directive" ] <?> "journal transaction or directive"
journalAddFilePath :: FilePath -> Journal -> Journal
journalAddFilePath f j@Journal{allfilepaths=fs} = j{allfilepaths=fs++[f]}
emptyLine :: GenParser Char st () emptyLine :: GenParser Char st ()
emptyLine = do many spacenonewline emptyLine = do many spacenonewline
optional $ (char ';' <?> "comment") >> many (noneOf "\n") optional $ (char ';' <?> "comment") >> many (noneOf "\n")
@ -217,7 +220,7 @@ ledgerInclude = do
contents <- readFileOrError outerPos filepath contents <- readFileOrError outerPos filepath
let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n" let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n"
case runParser journalFile outerState filepath contents of case runParser journalFile outerState filepath contents of
Right ju -> ju `catchError` (throwError . (inIncluded ++)) Right ju -> juSequence [return $ journalAddFilePath filepath, ju] `catchError` (throwError . (inIncluded ++))
Left err -> throwError $ inIncluded ++ show err Left err -> throwError $ inIncluded ++ show err
where readFileOrError pos fp = where readFileOrError pos fp =
ErrorT $ liftM Right (readFile fp) `catch` ErrorT $ liftM Right (readFile fp) `catch`