HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Modulehledger-lib-1.52.4GHC2021

Hledger.Read.Common

File reading/parsing utilities used by multiple readers, and a good amount of the parsers for journal format, to avoid import cycles when JournalReader imports other readers.

Some of these might belong in Hledger.Read.JournalReader or Hledger.Read.

  • 3 types
  • 1 class
  • 69 values
datadata Reader (m :: Type -> Type)
#

A hledger journal reader is a storage format name, a list of file extensions assumed to be in this format, and an IO action that reads data in this format, returning a Journal.

The journal parser used by the latter is also stored separately for direct use by the journal reader's includedirectivep to parse included files. The type variable m is needed for this parser. Lately it requires an InputOpts, basically to support --old-timeclock.

Instances1Show
  • Show (Reader m)Defined in hledger-lib-1.52.4 · Hledger.Read.Common
typetype PrefixedFilePath = FilePath
#

A file path optionally prefixed by a reader name and colon (journal:, csv:, timedot:, etc.). The file path part can also be - meaning standard input.

datadata InputOpts
#

Constructors

  • InputOpts
    • mformat_ :: Maybe StorageFormat

      a file/storage format to try, unless overridden by a filename prefix. Nothing means try all.

    • mrules_file_ :: Maybe FilePath

      a conversion rules file to use (when reading CSV)

    • aliases_ :: [String]

      account name aliases to apply

    • anon_ :: Bool

      do light obfuscation of the data ? Now corresponds to --obfuscate, not the old --anon flag.

    • new_ :: Bool

      read only new transactions since this file was last read ?

    • new_save_ :: Bool

      save latest new transactions state for next time ?

    • pivot_ :: String

      use the given field's value as the account name

    • forecast_ :: Maybe DateSpan

      span in which to generate forecast transactions

    • auto_posting_tags_ :: Bool

      propagate commodity and account tags to postings ? Can be disabled (for beancount export).

    • verbose_tags_ :: Bool

      add user-visible tags when generating/modifying transactions & postings ?

    • reportspan_ :: DateSpan

      a dirty hack keeping the query dates in InputOpts. This rightfully lives in ReportSpec, but is duplicated here.

    • auto_ :: Bool

      generate extra postings according to auto posting rules ?

    • infer_equity_ :: Bool

      infer equity conversion postings from costs ?

    • infer_costs_ :: Bool

      infer costs from equity conversion postings ? distinct from BalancingOpts{infer_balancing_costs_}

    • balancingopts_ :: BalancingOpts

      options for transaction balancing

    • strict_ :: Bool

      do extra correctness checks ?

    • _defer :: Bool

      internal flag: postpone checks, because we are processing multiple files ?

    • _ioDay :: Day

      today's date, for use with forecast transactions XXX this duplicates _rsDay, and should eventually be removed when it's not needed anymore.

    • _oldtimeclock :: Bool

      parse with the old timeclock pairing rules?

Instances5Eq, Ord, Show, HasBalancingOpts, HasInputOpts
classclass HasInputOpts c where
#
Instances1HasInputOpts

parsing utilities

20 declarations

Post-process a Journal that has just been parsed or generated, in this order:

  • add misc info (file path, read time)

  • reverse transactions into their original parse order

  • apply canonical commodity styles

  • propagate account tags to postings

  • maybe add forecast transactions

  • propagate account tags to postings (again to affect forecast transactions)

  • maybe add auto postings

  • propagate account tags to postings (again to affect auto postings)

  • evaluate balance assignments and balance each transaction

  • maybe check balance assertions

  • maybe infer costs from equity postings

  • maybe infer equity postings from costs

  • manye infer market prices from costs

One correctness check (parseable) has already passed when this function is called. Up to four more are performed here:

  • ordereddates (when enabled)

  • assertions (when enabled)

  • autobalanced (and with --strict, balanced ?), in the journalBalanceTransactions step.

Others (commodities, accounts..) are done later by journalStrictChecks.

Generate periodic transactions from all periodic transaction rules in the journal. These transactions are added to the in-memory Journal (but not the on-disk file).

The start & end date for generated periodic transactions are determined in a somewhat complicated way; see the hledger manual -> Periodic transactions.

parsers

0 declarations

transaction bits

Parse possibly empty text until a semicolon or newline. Whitespace is preserved (for now - perhaps helps preserve alignment of same-line comments ?).

dates

valuedatep :: JournalParser m Day
#

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.

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

account names

Parse an account name plus one following space if present (see accountnamep); then apply any parent account prefix and/or account aliases currently in effect, in that order. Ie first add the parent account prefix, then rewrite with aliases. 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.

Parse an account name, plus one following space if present. Account names have one or more parts separated by the account separator character, and are terminated by two or more spaces (or end of input). Each part is at least one character long, may have single spaces inside it, and starts with a non-whitespace. (We should have required them to start with an alphanumeric, but didn't.) Note, this means account names can contain all kinds of punctuation, including ; which usually starts a following comment. Parent parsers usually remove the following comment before using this parser.

account aliases

amounts

Parse a single-commodity amount, applying the default commodity if there is no commodity symbol; optionally followed by, in any order: a Ledger-style cost, Ledger-style valuation expression, and/or Ledger-style cost basis, which is one or more of lot cost, lot date, and/or lot note (we loosely call this triple the lot's cost basis). The cost basis makes it a lot rather than just an amount. Both cost basis info and valuation expression are discarded for now. The main amount's sign is significant; here are the possibilities and their interpretation. Also imagine an optional VALUATIONEXPR added to any of these (omitted for clarity): @

AMT -- acquiring an amount AMT COST -- acquiring an amount at some cost AMT COST COSTBASIS -- acquiring a lot at some cost, saving its cost basis AMT COSTBASIS COST -- like the above AMT COSTBASIS -- like the above with cost same as the cost basis

  • AMT -- releasing an amount

  • AMT SELLPRICE -- releasing an amount at some selling price

  • AMT SELLPRICE COSTBASISSEL -- releasing a lot at some selling price, selecting it by its cost basis

  • AMT COSTBASISSEL SELLPRICE -- like the above

  • AMT COSTBASISSEL -- like the above with selling price same as the selected lot's cost basis amount

COST/SELLPRICE can be UNITAMT, TOTALAMT, () UNITAMT, or (@@) TOTALAMT. The () are ignored. COSTBASIS is one or more of {LOTCOST}, [LOTDATE], (LOTNOTE), in any order, with LOTCOST defaulting to COST. COSTBASISSEL is one or more of {LOTCOST}, [LOTDATE], (LOTNOTE), in any order. {LOTCOST} can be {UNITAMT}, {{TOTALAMT}}, {=UNITAMT}, or {{=TOTALAMT}}. The = is ignored. VALUATIONEXPR can be ((VALUE AMOUNT)) or ((VALUE FUNCTION)).

@ Ledger amount syntax is really complex. Rule of thumb: curly braces, parentheses, and/or square brackets in an amount means a Ledger-style cost basis is involved.

To parse an amount's numeric quantity we need to know which character represents a decimal mark. We find it in one of three ways:

  1. If a decimal mark has been set explicitly in the journal parse state, we use that

  2. Or if the journal has a commodity declaration for the amount's commodity, we get the decimal mark from that

  3. Otherwise we will parse any valid decimal mark appearing in the number, as long as the number appears well formed. (This means we handle files with any supported decimal mark without configuration, but it also allows different decimal marks in different amounts, which is a bit too loose. There's an open issue.)

Parse a string representation of a number for its value and display attributes.

Some international number formats are accepted, eg either period or comma may be used for the decimal mark, and the other of these may be used for separating digit groups in the integer part. See http://en.wikipedia.org/wiki/Decimal_separator for more examples.

This returns: the parsed numeric value, the precision (number of digits seen following the decimal mark), the decimal mark character used if any, and the digit group style if any.

valuerawnumberp :: TextParser m (Either AmbiguousNumber RawNumber)
#

Parse and interpret the structure of a number without external hints. Numbers are digit strings, possibly separated into digit groups by one of two types of separators. (1) Numbers may optionally have a decimal mark, which may be either a period or comma. (2) Numbers may optionally contain digit group marks, which must all be either a period, a comma, or a space.

It is our task to deduce the characters used as decimal mark and digit group mark, based on the allowed syntax. For instance, we make use of the fact that a decimal mark can occur at most once and must be to the right of all digit group marks.

Example3 expressions
parseTest rawnumberp "1,234,567.89"Right (WithSeparators ',' ["1","234","567"] (Just ('.',"89")))parseTest rawnumberp "1,000"Left (AmbiguousNumber "1" ',' "000")parseTest rawnumberp "1 000"Right (WithSeparators ' ' ["1","000"] Nothing)

comments

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.

valueemptyorcommentlinep2 :: [Char] -> TextParser m ()
#

A newer comment line parser. Parses a line which is empty, all blanks, or whose first non-blank character is one of those provided. A final newline is optional.

Parse a comment following a journal item, possibly continued on multiple lines, and return the comment text.

Example5 expressions
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"

Parse a transaction comment and extract its tags.

The first line of a transaction may be followed a 1-or-more-lines comment, beginning with a semicolon possibly preceded by whitespace on the current line, or with an indented semicolon on the next line. Additional lines also must begin with an indented semicolon. See also followingcommentpWith.

200011 ; a transaction comment starting on the same line ... ; extending to the next line account1 $1 account2

Tags are name-value pairs.

Example2 expressions
let getTags (_,tags) = tagslet parseTags = fmap getTags . rtp transactioncommentp
Example1 expression
parseTags "; name1: val1, name2:all this is value2"Right [("name1","val1"),("name2","all this is value2")]

A tag's name must be immediately followed by a colon, without separating whitespace. The corresponding value consists of all the text following the colon up until the next colon or newline, stripped of leading and trailing whitespace.

Parse a posting comment and extract its tags and dates.

Postings may be followed by comments, which begin with semicolons and extend to the end of the line. Posting comments may span multiple lines, but comment lines below the posting must be preceded by leading whitespace.

200011 account1 $1 ; a posting comment starting on the same line ... ; extending to the next line

account2 ; a posting comment beginning on the next line

Tags are name-value pairs.

Example2 expressions
let getTags (_,tags,_,_) = tagslet parseTags = fmap getTags . rtp (postingcommentp Nothing)
Example1 expression
parseTags "; name1: val1, name2:all this is value2"Right [("name1","val1"),("name2","all this is value2")]

A tag's name must be immediately followed by a colon, without separating whitespace. The corresponding value consists of all the text following the colon up until the next colon or newline, stripped of leading and trailing whitespace.

Posting dates may be expressed with "date"/"date2" tags or with bracketed date syntax. Posting dates will inherit their year from the transaction date if the year is not specified. We throw parse errors on invalid dates.

Example2 expressions
let getDates (_,_,d1,d2) = (d1, d2)let parseDates = fmap getDates . rtp (postingcommentp (Just 2000))
Example2 expressions
parseDates "; date: 1/2, date2: 1999/12/31"Right (Just 2000-01-02,Just 1999-12-31)parseDates "; [1/2=1999/12/31]"Right (Just 2000-01-02,Just 1999-12-31)

Example: tags, date tags, and bracketed dates >>> rtp (postingcommentp (Just 2000)) "; a:b, date:34, [=56]" Right ("a:b, date:34, [=56]n",[("a","b"),("date","3/4")],Just 2000-03-04,Just 2000-05-06)

Example: extraction of dates from date tags ignores trailing text >>> rtp (postingcommentp (Just 2000)) "; date:34=56" Right ("date:34=56n",[("date","34=56")],Just 2000-03-04,Nothing)

bracketed dates

Parse Ledger-style bracketed posting dates ([DATE=DATE2]), as "date" and/or "date2" tags. Anything that looks like an attempt at this (a square-bracketed sequence of 0123456789/-.= containing at least one digit and one date separator) is also parsed, and will throw an appropriate error.

The dates are parsed in full here so that errors are reported in the right position. A missing year in DATE can be inferred if a default date is provided. A missing year in DATE2 will be inferred from DATE.

Example1 expression
either (Left . customErrorBundlePretty) Right $ rtp (bracketeddatetagsp Nothing) "[2016/1/2=3/4]"Right [("date",2016-01-02),("date2",2016-03-04)]
Example1 expression
either (Left . customErrorBundlePretty) Right $ rtp (bracketeddatetagsp Nothing) "[1]"Left ...not a bracketed date...
Example1 expression
either (Left . customErrorBundlePretty) Right $ rtp (bracketeddatetagsp Nothing) "[2016/1/32]"Left ...1:2:...This is not a valid date...
Example1 expression
either (Left . customErrorBundlePretty) Right $ rtp (bracketeddatetagsp Nothing) "[1/31]"Left ...1:2:...This partial date can not be parsed because the current year is unknown...
Example1 expression
either (Left . customErrorBundlePretty) Right $ rtp (bracketeddatetagsp Nothing) "[0123456789/-.=/-.=]"Left ...1:13:...expecting month or day...

misc

valuesinglespacep :: TextParser m ()
#

Parse one non-newline whitespace character that is not followed by another one.

tests

1 declaration