From d24b1b96f7726a9c1dba573590090aa1b7d5b348 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 17 Oct 2015 12:09:03 -0700 Subject: [PATCH] lib: parser cleanups --- hledger-lib/Hledger/Data/Types.hs | 2 +- hledger-lib/Hledger/Read.hs | 6 +++--- hledger-lib/Hledger/Read/JournalReader.hs | 14 ++++++++------ hledger-lib/Hledger/Read/TimelogReader.hs | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 585d7e345..0da51a82c 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -262,7 +262,7 @@ data Journal = Journal { instance NFData Journal -- | A JournalUpdate is some transformation of a Journal. It can do I/O or --- raise an error. +-- raise an exception. type JournalUpdate = ExceptT String IO (Journal -> Journal) -- | The id of a data format understood by hledger, eg @journal@ or @csv@. diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index 1ff9d10fb..3fb3fe026 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -244,9 +244,9 @@ tests_Hledger_Read = TestList $ tests_Hledger_Read_CsvReader, "journal" ~: do - r <- runExceptT $ parseWithCtx nullctx JournalReader.journal "" - assertBool "journal should parse an empty file" (isRight $ r) + r <- runExceptT $ parseWithCtx nullctx JournalReader.journalp "" + assertBool "journalp should parse an empty file" (isRight $ r) jE <- readJournal Nothing Nothing True Nothing "" -- don't know how to get it from journal - either error' (assertBool "journal parsing an empty file should give an empty journal" . null . jtxns) jE + either error' (assertBool "journalp parsing an empty file should give an empty journal" . null . jtxns) jE ] diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 93a548f18..581ac2d7e 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -21,7 +21,7 @@ module Hledger.Read.JournalReader ( -- * Reader reader, -- * Parsers used elsewhere - parseJournalWith, + parseAndFinaliseJournal, genericSourcePos, getParentAccount, journalp, @@ -94,7 +94,7 @@ detect f s -- | Parse and post-process a "Journal" from hledger's journal file -- format, or give an error. parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal -parse _ = parseJournalWith journalp +parse _ = parseAndFinaliseJournal journalp -- parsing utils @@ -162,15 +162,17 @@ combineJournalUpdates us = foldl' (flip (.)) id <$> sequence us -- | 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. -parseJournalWith :: (ParsecT [Char] JournalContext (ExceptT String IO) (JournalUpdate,JournalContext)) -> Bool -> FilePath -> String -> ExceptT String IO Journal -parseJournalWith p assrt f s = do +parseAndFinaliseJournal :: + (ParsecT [Char] JournalContext (ExceptT String IO) (JournalUpdate,JournalContext)) + -> Bool -> FilePath -> String -> ExceptT String IO Journal +parseAndFinaliseJournal parser assrt f s = do tc <- liftIO getClockTime tl <- liftIO getCurrentLocalTime y <- liftIO getCurrentYear - r <- runParserT p nullctx{ctxYear=Just y} f s + r <- runParserT parser nullctx{ctxYear=Just y} f s case r of Right (updates,ctx) -> do - j <- updates `ap` return nulljournal + j <- ap updates (return nulljournal) case journalFinalise tc tl f s ctx assrt j of Right j' -> return j' Left estr -> throwError estr diff --git a/hledger-lib/Hledger/Read/TimelogReader.hs b/hledger-lib/Hledger/Read/TimelogReader.hs index 16b352949..bf9e253d9 100644 --- a/hledger-lib/Hledger/Read/TimelogReader.hs +++ b/hledger-lib/Hledger/Read/TimelogReader.hs @@ -61,7 +61,7 @@ import Hledger.Data -- XXX too much reuse ? import Hledger.Read.JournalReader ( directivep, marketpricedirectivep, defaultyeardirectivep, emptyorcommentlinep, datetimep, - parseJournalWith, modifiedaccountnamep, genericSourcePos + parseAndFinaliseJournal, modifiedaccountnamep, genericSourcePos ) import Hledger.Utils @@ -82,7 +82,7 @@ detect f s -- format, saving the provided file path and the current time, or give an -- error. parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal -parse _ = parseJournalWith timelogfilep +parse _ = parseAndFinaliseJournal timelogfilep timelogfilep :: ParsecT [Char] JournalContext (ExceptT String IO) (JournalUpdate, JournalContext) timelogfilep = do items <- many timelogitemp