| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Hledger.Read.JournalReader
Description
A reader for hledger's journal file format (http://hledger.org/hledger.html#the-journal-file). hledger's journal format is a compatible subset of c++ ledger's (http://ledger-cli.org/3.0/doc/ledger3.html#Journal-Format), so this reader should handle many ledger files as well. Example:
2012/3/24 gift
expenses:gifts $10
assets:cash
Journal format supports the include directive which can read files in other formats, so the other file format readers need to be importable and invocable here.
Some important parts of journal parsing are therefore kept in Hledger.Read.Common, to avoid import cycles.
Synopsis
- findReader :: forall (m :: Type -> Type). MonadIO m => Maybe StorageFormat -> Maybe FilePath -> Maybe (Reader m)
- splitReaderPrefix :: PrefixedFilePath -> (Maybe StorageFormat, FilePath)
- reader :: forall (m :: Type -> Type). MonadIO m => Reader m
- parseAndFinaliseJournal :: ErroringJournalParser IO ParsedJournal -> InputOpts -> FilePath -> Text -> ExceptT String IO Journal
- runJournalParser :: Monad m => JournalParser m a -> Text -> m (Either HledgerParseErrors a)
- rjp :: Monad m => JournalParser m a -> Text -> m (Either HledgerParseErrors a)
- runErroringJournalParser :: Monad m => ErroringJournalParser m a -> Text -> m (Either FinalParseError (Either HledgerParseErrors a))
- rejp :: Monad m => ErroringJournalParser m a -> Text -> m (Either FinalParseError (Either HledgerParseErrors a))
- getParentAccount :: forall (m :: Type -> Type). JournalParser m AccountName
- journalp :: forall (m :: Type -> Type). MonadIO m => InputOpts -> ErroringJournalParser m ParsedJournal
- directivep :: forall (m :: Type -> Type). MonadIO m => InputOpts -> ErroringJournalParser m ()
- defaultyeardirectivep :: forall (m :: Type -> Type). JournalParser m ()
- marketpricedirectivep :: forall (m :: Type -> Type). JournalParser m PriceDirective
- datetimep :: forall (m :: Type -> Type). JournalParser m LocalTime
- datep :: forall (m :: Type -> Type). JournalParser m Day
- modifiedaccountnamep :: forall (m :: Type -> Type). Bool -> JournalParser m AccountName
- tmpostingrulep :: forall (m :: Type -> Type). Maybe Year -> JournalParser m TMPostingRule
- statusp :: forall (m :: Type -> Type). TextParser m Status
- emptyorcommentlinep :: forall (m :: Type -> Type). TextParser m ()
- followingcommentp :: forall (m :: Type -> Type). TextParser m Text
- accountaliasp :: forall (m :: Type -> Type). TextParser m AccountAlias
- parseAssertDirective :: Day -> String -> Text -> Either String AssertDirective
- tests_JournalReader :: TestTree
Reader-finding utils
findReader :: forall (m :: Type -> Type). MonadIO m => Maybe StorageFormat -> Maybe FilePath -> Maybe (Reader m) Source #
findReader mformat mpath
Find the reader named by mformat, if provided.
("ssv" and "tsv" are recognised as alternate names for the csv reader,
which also handles those formats.)
Or, if a file path is provided, find the first reader that handles
its file extension, if any.
splitReaderPrefix :: PrefixedFilePath -> (Maybe StorageFormat, FilePath) Source #
Separate a file path and its reader prefix, if any.
>>>splitReaderPrefix "csv:-"(Just csv,"-")
Reader
Parsing utils
parseAndFinaliseJournal :: ErroringJournalParser IO ParsedJournal -> InputOpts -> FilePath -> Text -> ExceptT String IO Journal Source #
Given a parser to ParsedJournal, input options, file path and content: run the parser on the content, and finalise the result to get a Journal; or throw an error.
runJournalParser :: Monad m => JournalParser m a -> Text -> m (Either HledgerParseErrors a) Source #
Run a journal parser in some monad. See also: parseWithState.
rjp :: Monad m => JournalParser m a -> Text -> m (Either HledgerParseErrors a) Source #
Run a journal parser in some monad. See also: parseWithState.
runErroringJournalParser :: Monad m => ErroringJournalParser m a -> Text -> m (Either FinalParseError (Either HledgerParseErrors a)) Source #
Run an erroring journal parser in some monad. See also: parseWithState.
rejp :: Monad m => ErroringJournalParser m a -> Text -> m (Either FinalParseError (Either HledgerParseErrors a)) Source #
Run an erroring journal parser in some monad. See also: parseWithState.
Parsers used elsewhere
getParentAccount :: forall (m :: Type -> Type). JournalParser m AccountName Source #
journalp :: forall (m :: Type -> Type). MonadIO m => InputOpts -> ErroringJournalParser m ParsedJournal Source #
A journal parser. Accumulates and returns a ParsedJournal, which should be finalised/validated before use.
>>>rejp (journalp definputopts <* eof) "2015/1/1\n a 0\n"Right (Right Journal (unknown) with 1 transactions, 1 accounts)
directivep :: forall (m :: Type -> Type). MonadIO m => InputOpts -> ErroringJournalParser m () Source #
Parse any journal directive and update the parse state accordingly, and record it as a journal item (exported or not by print --export). Cf http://hledger.org/hledger.html#directives, http://ledger-cli.org/3.0/doc/ledger3.html#Command-Directives
defaultyeardirectivep :: forall (m :: Type -> Type). JournalParser m () Source #
marketpricedirectivep :: forall (m :: Type -> Type). JournalParser m PriceDirective Source #
datetimep :: forall (m :: Type -> Type). JournalParser m LocalTime Source #
Parse a date and time in YYYY-MM-DD HH:MM[:SS][+-ZZZZ] format. Slash (/) and period (.) are also allowed as date separators. The year may be omitted if a default year has been set. Seconds are optional. The timezone is optional and ignored (the time is always interpreted as a local time). Leading zeroes may be omitted (except in a timezone).
datep :: forall (m :: Type -> Type). JournalParser m Day Source #
Parse a date in YYYY-MM-DD format. Slash (/) and period (.) are also allowed as separators. The year may be omitted if a default year has been set. Leading zeroes may be omitted.
modifiedaccountnamep :: forall (m :: Type -> Type). Bool -> JournalParser m AccountName Source #
Parse an account name plus one following space if present (see accountnamep);
then apply the alias directives, parent account prefix and --alias options currently in effect,
in that order (see accountNameApplyModifiers, which explains this).
This calls error if any account alias with an invalid regular expression exists.
The flag says whether account names may include semicolons; currently account names
in journal format may, but account names in timeclock/timedot formats may not.
tmpostingrulep :: forall (m :: Type -> Type). Maybe Year -> JournalParser m TMPostingRule Source #
statusp :: forall (m :: Type -> Type). TextParser m Status Source #
emptyorcommentlinep :: forall (m :: Type -> Type). TextParser m () Source #
A blank or comment line in journal format: a line that's empty or containing only whitespace or whose first non-whitespace character is semicolon, hash, or star. See also emptyorcommentlinep2.
followingcommentp :: forall (m :: Type -> Type). TextParser m Text Source #
Parse a comment following a journal item, possibly continued on multiple lines, and return the comment text.
>>>rtp followingcommentp "" -- no commentRight "">>>rtp followingcommentp ";" -- just a (empty) same-line comment. newline is addedRight "\n">>>rtp followingcommentp "; \n"Right "\n">>>rtp followingcommentp ";\n ;\n" -- a same-line and a next-line commentRight "\n\n">>>rtp followingcommentp "\n ;\n" -- just a next-line comment. Insert an empty same-line comment so the next-line comment doesn't become a same-line comment.Right "\n\n"
accountaliasp :: forall (m :: Type -> Type). TextParser m AccountAlias Source #
parseAssertDirective :: Day -> String -> Text -> Either String AssertDirective Source #
Parse one assert directive given as text, such as a check command argument, naming its source in error messages with the given name. Relative dates are relative to the given day (today).