Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrosinger authored Apr 18, 2023
2 parents 318d7af + e66c120 commit 7844c76
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
21 changes: 13 additions & 8 deletions grammar/ledger.ne
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
@preprocessor typescript

### Partially supported ###
# Transactions
# - Balance assertions (https://www.ledger-cli.org/3.0/doc/ledger3.html#Balance-assertions)
# - parsed but ignored in output
### Not supported ###
#
# Transactions:
# - Metadata (https://www.ledger-cli.org/3.0/doc/ledger3.html#Metadata)
# - Virtual postings (https://www.ledger-cli.org/3.0/doc/ledger3.html#Virtual-postings)
# - Expression amounts (https://www.ledger-cli.org/3.0/doc/ledger3.html#Expression-amounts)
# - Balance assertions (https://www.ledger-cli.org/3.0/doc/ledger3.html#Balance-assertions)
# - Balance assignments (https://www.ledger-cli.org/3.0/doc/ledger3.html#Balance-assignments)
# - Commodities (https://www.ledger-cli.org/3.0/doc/ledger3.html#Commodity-prices)
#
Expand Down Expand Up @@ -41,6 +44,7 @@
currency: /[$£₤€₿₹¥¥₩Р₴]/, // Note: Р != P
reconciled: /[!*]/,
comment: { match: /[;#|][^\n]+/, value: (s:string) => s.slice(1).trim() },
assertion: {match: /==?\*?/},
account: { match: /[^$£₤€₿₹¥¥₩Р₴;#|\n]+/, value: (s:string) => s.trim() },
},
alias: {
Expand Down Expand Up @@ -81,20 +85,21 @@ expenselines ->
| expenselines %newline expenseline {% ([rest,,l]) => { return [rest,l].flat(1) } %}

expenseline ->
%ws:+ reconciled:? %account amount:? %ws:* %comment:?
%ws:+ reconciled:? %account amount:? balance:? %ws:* %comment:?
{%
function(d) {
function([,r,acct,amt,_ba,,cmt]) {
return {
reconcile: d[1] || '',
account: d[2].value,
currency: d[3]?.currency,
amount: d[3]?.amount,
comment: d[5]?.value,
reconcile: r || '',
account: acct.value,
currency: amt?.currency,
amount: amt?.amount,
comment: cmt?.value,
}
}
%}
| %ws:+ %comment {% ([,c]) => { return {comment: c.value} } %}

balance -> %ws:* %assertion %ws:+ amount {% (d) => {return {}} %}
reconciled -> %reconciled %ws:+ {% ([r,]) => r.value %}
alias -> "alias" %account %equal %account {% ([,l,,r]) => { return { blockLine: l.line, left: l.value, right: r.value } } %}
amount -> %currency %number {% ([c,a]) => { return {currency: c.value, amount: parseFloat(a.value)} } %}
Expand Down
34 changes: 34 additions & 0 deletions tests/nearly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ beforeEach(() => {
parser = new Parser(Grammar.fromCompiled(grammar));
});

const ASSERTIONS = ['=', '=*', '==', '==*']

describe('parsing multiple blocks', () => {
test('when there are not newlines separating blocks', () => {
parser.feed('; This is a comment\n');
Expand Down Expand Up @@ -299,4 +301,36 @@ describe('parsing a transaction', () => {
],
]);
});
test.each(ASSERTIONS)('when there is a "%s" balance assertion', (assertion) => {
parser.feed('2018-04-03 Half Price Books\n');
parser.feed(' Expenses:Books $300\n');
parser.feed(` Assets:Checking $300 ${assertion} $300`);

expect(parser.results).toEqual([
[
{
type: 'tx',
blockLine: 1,
value: {
date: '2018-04-03',
payee: 'Half Price Books',
expenselines: [
{
amount: 300,
currency: '$',
account: 'Expenses:Books',
reconcile: '',
},
{
amount: 300,
currency: '$',
account: 'Assets:Checking',
reconcile: '',
},
],
},
},
],
]);
});
});
2 changes: 2 additions & 0 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ alias c=Credit
],
},
};

expect(txCache.parsingErrors).toHaveLength(0);
expect(txCache.transactions).toHaveLength(1);
expect(txCache.transactions[0]).toEqual(expected);
expect(txCache.payees).toEqual(['Costco']);
Expand Down

0 comments on commit 7844c76

Please sign in to comment.