lib: parser cleanups

This commit is contained in:
Simon Michael 2015-10-17 12:09:03 -07:00
parent bc43036117
commit d24b1b96f7
4 changed files with 14 additions and 12 deletions

View File

@ -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@.

View File

@ -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
]

View File

@ -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

View File

@ -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