lib: parser cleanups
This commit is contained in:
parent
bc43036117
commit
d24b1b96f7
@ -262,7 +262,7 @@ data Journal = Journal {
|
|||||||
instance NFData Journal
|
instance NFData Journal
|
||||||
|
|
||||||
-- | A JournalUpdate is some transformation of a Journal. It can do I/O or
|
-- | 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)
|
type JournalUpdate = ExceptT String IO (Journal -> Journal)
|
||||||
|
|
||||||
-- | The id of a data format understood by hledger, eg @journal@ or @csv@.
|
-- | The id of a data format understood by hledger, eg @journal@ or @csv@.
|
||||||
|
|||||||
@ -244,9 +244,9 @@ tests_Hledger_Read = TestList $
|
|||||||
tests_Hledger_Read_CsvReader,
|
tests_Hledger_Read_CsvReader,
|
||||||
|
|
||||||
"journal" ~: do
|
"journal" ~: do
|
||||||
r <- runExceptT $ parseWithCtx nullctx JournalReader.journal ""
|
r <- runExceptT $ parseWithCtx nullctx JournalReader.journalp ""
|
||||||
assertBool "journal should parse an empty file" (isRight $ r)
|
assertBool "journalp should parse an empty file" (isRight $ r)
|
||||||
jE <- readJournal Nothing Nothing True Nothing "" -- don't know how to get it from journal
|
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
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
@ -21,7 +21,7 @@ module Hledger.Read.JournalReader (
|
|||||||
-- * Reader
|
-- * Reader
|
||||||
reader,
|
reader,
|
||||||
-- * Parsers used elsewhere
|
-- * Parsers used elsewhere
|
||||||
parseJournalWith,
|
parseAndFinaliseJournal,
|
||||||
genericSourcePos,
|
genericSourcePos,
|
||||||
getParentAccount,
|
getParentAccount,
|
||||||
journalp,
|
journalp,
|
||||||
@ -94,7 +94,7 @@ detect f s
|
|||||||
-- | 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 :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal
|
parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal
|
||||||
parse _ = parseJournalWith journalp
|
parse _ = parseAndFinaliseJournal journalp
|
||||||
|
|
||||||
-- parsing utils
|
-- parsing utils
|
||||||
|
|
||||||
@ -162,15 +162,17 @@ combineJournalUpdates us = foldl' (flip (.)) 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 :: (ParsecT [Char] JournalContext (ExceptT String IO) (JournalUpdate,JournalContext)) -> Bool -> FilePath -> String -> ExceptT String IO Journal
|
parseAndFinaliseJournal ::
|
||||||
parseJournalWith p assrt f s = do
|
(ParsecT [Char] JournalContext (ExceptT String IO) (JournalUpdate,JournalContext))
|
||||||
|
-> Bool -> FilePath -> String -> ExceptT String IO Journal
|
||||||
|
parseAndFinaliseJournal parser assrt f s = do
|
||||||
tc <- liftIO getClockTime
|
tc <- liftIO getClockTime
|
||||||
tl <- liftIO getCurrentLocalTime
|
tl <- liftIO getCurrentLocalTime
|
||||||
y <- liftIO getCurrentYear
|
y <- liftIO getCurrentYear
|
||||||
r <- runParserT p nullctx{ctxYear=Just y} f s
|
r <- runParserT parser nullctx{ctxYear=Just y} f s
|
||||||
case r of
|
case r of
|
||||||
Right (updates,ctx) -> do
|
Right (updates,ctx) -> do
|
||||||
j <- updates `ap` return nulljournal
|
j <- ap updates (return nulljournal)
|
||||||
case journalFinalise tc tl f s ctx assrt j of
|
case journalFinalise tc tl f s ctx assrt j of
|
||||||
Right j' -> return j'
|
Right j' -> return j'
|
||||||
Left estr -> throwError estr
|
Left estr -> throwError estr
|
||||||
|
|||||||
@ -61,7 +61,7 @@ import Hledger.Data
|
|||||||
-- XXX too much reuse ?
|
-- XXX too much reuse ?
|
||||||
import Hledger.Read.JournalReader (
|
import Hledger.Read.JournalReader (
|
||||||
directivep, marketpricedirectivep, defaultyeardirectivep, emptyorcommentlinep, datetimep,
|
directivep, marketpricedirectivep, defaultyeardirectivep, emptyorcommentlinep, datetimep,
|
||||||
parseJournalWith, modifiedaccountnamep, genericSourcePos
|
parseAndFinaliseJournal, modifiedaccountnamep, genericSourcePos
|
||||||
)
|
)
|
||||||
import Hledger.Utils
|
import Hledger.Utils
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ detect f s
|
|||||||
-- format, saving the provided file path and the current time, or give an
|
-- format, saving the provided file path and the current time, or give an
|
||||||
-- error.
|
-- error.
|
||||||
parse :: Maybe FilePath -> Bool -> FilePath -> String -> ExceptT String IO Journal
|
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 :: ParsecT [Char] JournalContext (ExceptT String IO) (JournalUpdate, JournalContext)
|
||||||
timelogfilep = do items <- many timelogitemp
|
timelogfilep = do items <- many timelogitemp
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user