ExLedger.Parser.Core (ex_ledger v0.5.6)

NimbleParsec definitions and parsing primitives for ledger format.

Provides the core parsing infrastructure used by other parser modules.

Summary

Functions

Parses the given binary as account_declaration_parser.

Parses the given binary as amount_parser.

Regex pattern for matching amounts in ledger format.

Parses the given binary as automated_transaction_parser.

Parses the given binary as date_parser.

Parses the given binary as note_parser.

Parses the given binary as periodic_transaction_parser.

Parses the given binary as posting_parser.

Parses the given binary as transaction_parser.

Types

account_declaration()

@type account_declaration() :: %{
  name: String.t(),
  type: :expense | :revenue | :asset | :liability | :equity,
  aliases: [String.t()],
  assertions: [String.t()]
}

amount()

@type amount() :: %{
  value: Decimal.t(),
  currency: String.t() | nil,
  currency_position: :leading | :trailing | nil
}

commodity_definition()

@type commodity_definition() :: %{
  symbol: String.t(),
  format: String.t() | nil,
  note: String.t() | nil,
  alias: String.t() | nil,
  default: boolean(),
  nomarket: boolean()
}

cost()

@type cost() :: %{type: :per_unit | :total, amount: amount()}

ledger_error()

@type ledger_error() ::
  {:include_not_found, String.t()}
  | {:circular_include, String.t()}
  | {:include_outside_base, String.t()}
  | parse_error_detail()

parse_error()

@type parse_error() ::
  :missing_date
  | :missing_payee
  | :missing_predicate
  | :missing_period
  | :invalid_indentation
  | :insufficient_postings
  | :insufficient_spacing
  | :parse_error
  | :unbalanced
  | :multiple_nil_amounts
  | :multi_currency_missing_amount
  | :invalid_account_type
  | {:unexpected_input, String.t()}

parse_error_detail()

@type parse_error_detail() :: %{
  reason: parse_error(),
  line: non_neg_integer(),
  file: String.t() | nil,
  import_chain: [{String.t(), non_neg_integer()}] | nil
}

posting()

@type posting() :: %{
  account: String.t(),
  amount: amount() | nil,
  metadata: %{required(String.t()) => String.t()},
  tags: [String.t()],
  comments: [String.t()],
  virtual: boolean(),
  must_balance: boolean(),
  cost: cost() | nil,
  actual_date: Date.t() | nil,
  effective_date: Date.t() | nil,
  assertion: amount() | nil
}

price_directive()

@type price_directive() :: %{date: Date.t(), commodity: String.t(), price: amount()}

transaction()

@type transaction() :: %{
  kind: :regular | :automated | :periodic,
  date: Date.t() | nil,
  aux_date: Date.t() | nil,
  state: :cleared | :pending | :uncleared,
  code: String.t(),
  payee: String.t() | nil,
  comment: String.t() | nil,
  metadata: %{required(String.t()) => String.t() | [String.t()]},
  predicate: String.t() | nil,
  period: String.t() | nil,
  postings: [posting()],
  source_file: String.t() | nil,
  source_line: non_neg_integer() | nil
}

Functions

account_declaration_parser(binary, opts \\ [])

@spec account_declaration_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as account_declaration_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the account_declaration_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

amount_parser(binary, opts \\ [])

@spec amount_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as amount_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the amount_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

amount_regex()

Regex pattern for matching amounts in ledger format.

automated_transaction_parser(binary, opts \\ [])

@spec automated_transaction_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as automated_transaction_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the automated_transaction_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

date_parser(binary, opts \\ [])

@spec date_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as date_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the date_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

note_parser(binary, opts \\ [])

@spec note_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as note_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the note_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

periodic_transaction_parser(binary, opts \\ [])

@spec periodic_transaction_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as periodic_transaction_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the periodic_transaction_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

posting_parser(binary, opts \\ [])

@spec posting_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as posting_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the posting_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map

transaction_parser(binary, opts \\ [])

@spec transaction_parser(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as transaction_parser.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the transaction_parser (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map