/
ParseTree.pm6
338 lines (292 loc) · 8.34 KB
/
ParseTree.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use v6;
use TXN::Parser::Types;
use X::TXN::Parser;
# Entry::ID {{{
class Entry::ID
{
has UInt:D @.number is required;
has XXHash:D $.xxhash is required;
# causal text from accounting ledger
has Str:D $.text is required;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<number> = @.number;
%hash<text> = $.text;
%hash<xxhash> = $.xxhash;
%hash;
}
method Str(::?CLASS:D: --> Str:D)
{
my Str:D $xxhash = sprintf(Q{0x%s}, $.xxhash.base(16));
my Str:D $s = sprintf(Q{[%s]:%s}, @.number.join(' '), $xxhash);
}
}
# end Entry::ID }}}
# Entry::Header {{{
class Entry::Header
{
has Dateish:D $.date is required;
has Str $.description;
has UInt:D $.important = 0;
has VarName:D @.tag;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<date> = ~$.date;
%hash<description> = $.description if $.description;
%hash<important> = $.important;
%hash<tag> = @.tag if @.tag;
%hash;
}
}
# end Entry::Header }}}
# Entry::Posting::Account {{{
class Entry::Posting::Account
{
has Silo:D $.silo is required;
has VarName:D $.entity is required;
has VarName:D @.path;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<entity> = $.entity;
%hash<path> = @.path if @.path;
%hash<silo> = $.silo.gist;
%hash;
}
}
# end Entry::Posting::Account }}}
# Entry::Posting::Amount {{{
role Entry::Posting::Amount[ASSET]
{
has AssetCode:D $.asset-code is required;
has Quantity:D $.asset-quantity is required;
has AssetSymbol $.asset-symbol;
has PlusMinus $.plus-or-minus;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<asset-code> = $.asset-code;
%hash<asset-quantity> = $.asset-quantity;
%hash<asset-symbol> = $.asset-symbol if $.asset-symbol;
%hash<plus-or-minus> = $.plus-or-minus if $.plus-or-minus;
%hash;
}
}
role Entry::Posting::Amount[COMMODITY]
{
has UnitOfMeasure:D $.unit-of-measure is required;
has AssetCode:D $.asset-code is required;
has Quantity:D $.asset-quantity is required;
has PlusMinus $.plus-or-minus;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<unit-of-measure> = $.unit-of-measure;
%hash<asset-code> = $.asset-code;
%hash<asset-quantity> = $.asset-quantity;
%hash<plus-or-minus> = $.plus-or-minus if $.plus-or-minus;
%hash;
}
}
# end Entry::Posting::Amount }}}
# Entry::Posting::Annot::XE {{{
class Entry::Posting::Annot::XE
{
has AssetCode:D $.asset-code is required;
has Price:D $.asset-price is required;
has AssetSymbol $.asset-symbol;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<asset-code> = $.asset-code;
%hash<asset-price> = $.asset-price;
%hash<asset-symbol> = $.asset-symbol if $.asset-symbol;
%hash;
}
}
# end Entry::Posting::Annot::XE }}}
# Entry::Posting::Annot::Inherit {{{
class Entry::Posting::Annot::Inherit
{
also is Entry::Posting::Annot::XE;
}
# end Entry::Posting::Annot::Inherit }}}
# Entry::Posting::Annot::Lot {{{
class Entry::Posting::Annot::Lot
{
has VarName:D $.name is required;
# is this lot being drawn down or filled up?
has DecInc:D $.decinc is required;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<decinc> = $.decinc.gist;
%hash<name> = $.name;
%hash;
}
}
# end Entry::Posting::Annot::Lot }}}
# Entry::Posting::Annot {{{
class Entry::Posting::Annot
{
has Entry::Posting::Annot::Inherit $.inherit;
has Entry::Posting::Annot::Lot $.lot;
has Entry::Posting::Annot::XE $.xe;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<inherit> = $.inherit.hash if $.inherit;
%hash<lot> = $.lot.hash if $.lot;
%hash<xe> = $.xe.hash if $.xe;
%hash;
}
}
# end Entry::Posting::Annot }}}
# Entry::Posting::ID {{{
class Entry::Posting::ID
{
# parent
has Entry::ID:D $.entry-id is required;
# scalar, because C<include>'d postings are forbidden
has UInt:D $.number is required;
has XXHash:D $.xxhash is required;
# causal text from accounting ledger
has Str:D $.text is required;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<entry-id> = $.entry-id.hash;
%hash<number> = $.number;
%hash<text> = $.text;
%hash<xxhash> = $.xxhash;
%hash;
}
method Str(::?CLASS:D: --> Str:D)
{
my Str:D $xxhash = sprintf(Q{0x%s}, $.xxhash.base(16));
my Str:D $s = sprintf(Q{%s|%s:%s}, $.entry-id.Str, $.number, $xxhash);
}
}
# end Entry::Posting::ID }}}
# Entry::Posting {{{
class Entry::Posting
{
has Entry::Posting::ID:D $.id is required;
has Entry::Posting::Account:D $.account is required;
has Entry::Posting::Amount:D $.amount is required;
has DecInc:D $.decinc is required;
has DrCr:D $.drcr is required;
has Entry::Posting::Annot $.annot;
submethod BUILD(
Entry::Posting::Account:D :$!account!,
Entry::Posting::Amount:D :$!amount!,
Entry::Posting::ID:D :$!id!,
DecInc:D :$!decinc!,
Entry::Posting::Annot :$annot
--> Nil
)
{
$!annot = $annot if $annot;
$!drcr = gen-drcr($!account.silo, $!decinc);
}
method new(
*%opts (
Entry::Posting::Account:D :account($)!,
Entry::Posting::Amount:D :amount($)!,
Entry::Posting::ID:D :id($)!,
DecInc:D :decinc($)!,
Entry::Posting::Annot :annot($),
# ignore potential drcr key for C<TXN::Remarshal>
*%
)
--> Entry::Posting:D
)
{
self.bless(|%opts);
}
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<id> = $.id.hash;
%hash<account> = $.account.hash;
%hash<amount> = $.amount.hash;
%hash<decinc> = $.decinc.gist;
%hash<drcr> = $.drcr.gist;
%hash<annot> = $.annot.hash if $.annot;
%hash;
}
# assets and expenses increase on the debit side
# +assets/expenses
multi sub gen-drcr(ASSETS, INC --> DrCr:D) { DEBIT }
multi sub gen-drcr(EXPENSES, INC --> DrCr:D) { DEBIT }
# -assets/expenses
multi sub gen-drcr(ASSETS, DEC --> DrCr:D) { CREDIT }
multi sub gen-drcr(EXPENSES, DEC --> DrCr:D) { CREDIT }
# income, liabilities and equity increase on the credit side
# +income/liabilities/equity
multi sub gen-drcr(INCOME, INC --> DrCr:D) { CREDIT }
multi sub gen-drcr(LIABILITIES, INC --> DrCr:D) { CREDIT }
multi sub gen-drcr(EQUITY, INC --> DrCr:D) { CREDIT }
# -income/liabilities/equity
multi sub gen-drcr(INCOME, DEC --> DrCr:D) { DEBIT }
multi sub gen-drcr(LIABILITIES, DEC --> DrCr:D) { DEBIT }
multi sub gen-drcr(EQUITY, DEC --> DrCr:D) { DEBIT }
}
# end Entry::Posting }}}
# Entry {{{
class Entry
{
has Entry::ID:D $.id is required;
has Entry::Header:D $.header is required;
has Entry::Posting:D @.posting is required;
submethod BUILD(
Entry::ID:D :$!id!,
Entry::Header:D :$!header!,
Entry::Posting:D :@!posting!
--> Nil
)
{*}
method new(
*%opts (
Entry::ID:D :$id!,
Entry::Header:D :header($)!,
Entry::Posting:D :@posting!
)
--> Entry:D
)
{
# verify entry is limited to one entity
my UInt:D $number-entities =
@posting.hyper.map({ .account.entity }).unique.elems;
$number-entities == 1 or do {
my Exception:U $exception-type =
X::TXN::Parser::Entry::MultipleEntities;
die($exception-type.new(:$number-entities, :entry-text($id.text)));
}
self.bless(|%opts);
}
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<header> = $.header.hash;
%hash<id> = $.id.hash;
%hash<posting> = @.posting.hyper.map({ .hash }).Array;
%hash;
}
}
# end Entry }}}
# Ledger {{{
class Ledger
{
has Entry:D @.entry is required;
method hash(::?CLASS:D: --> Hash:D)
{
my %hash;
%hash<entry> = @.entry.hyper.map({ .hash }).Array;
%hash;
}
}
# end Ledger }}}
# vim: set filetype=perl6 foldmethod=marker foldlevel=0: