all repos

clerk @ 7136c82232d67825b9ccd590797f85659c3a23bd

missing tooling for ledger/hledger

clerk/journal/ast/dump.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
parser: some directives were missing inline comment & improve memory layout of some directives, 13 days ago
1
package ast
2
3
import (
4
	"fmt"
5
	"strings"
6
)
7
8
func Dump(f *Journal) string {
9
	var b strings.Builder
10
	dumpJournal(&b, f)
11
	return b.String()
12
}
13
14
func dumpJournal(b *strings.Builder, f *Journal) {
15
	fmt.Fprintf(b, "Journal\n")
16
	for _, e := range f.Entries {
17
		dumpEntry(b, e, 1)
18
	}
19
	if len(f.Errors) > 0 {
20
		fmt.Fprintf(b, "  Errors\n")
21
		for _, err := range f.Errors {
22
			fmt.Fprintf(b, "    %s: %s\n", err.Span, err.Message)
23
		}
24
	}
25
}
26
27
func dumpEntry(b *strings.Builder, e Entry, depth int) {
28
	switch e := e.(type) {
29
	case *Transaction:
30
		dumpTransaction(b, e, depth)
31
	case *PeriodicTransaction:
32
		dumpPeriodicTransaction(b, e, depth)
33
	case *AutomatedTransaction:
34
		dumpAutomatedTransaction(b, e, depth)
35
	case *BlankLine:
36
		indent(b, depth)
37
		fmt.Fprintf(b, "BlankLine %s\n", e.Span)
38
	case *Comment:
39
		dumpComment(b, e, depth)
40
	case *AccountDirective:
41
		dumpAccountDirective(b, e, depth)
42
	case *CommodityDirective:
43
		dumpCommodityDirective(b, e, depth)
44
	case *IncludeDirective:
45
		indent(b, depth)
46
		fmt.Fprintf(b, "IncludeDirective %s\n", e.Span)
47
		indent(b, depth+1)
48
		fmt.Fprintf(b, "Path: %q\n", e.Path)
49
		dumpOptComment(b, e.Comment, depth+1)
50
	case *AliasDirective:
51
		indent(b, depth)
52
		fmt.Fprintf(b, "AliasDirective %s\n", e.Span)
53
		indent(b, depth+1)
54
		fmt.Fprintf(b, "From: %q\n", e.From)
55
		indent(b, depth+1)
56
		fmt.Fprintf(b, "To: %q\n", e.To)
57
	case *PayeeDirective:
58
		indent(b, depth)
59
		fmt.Fprintf(b, "PayeeDirective %s\n", e.Span)
60
		indent(b, depth+1)
61
		fmt.Fprintf(b, "Name: %q\n", e.Name)
62
		dumpOptComment(b, e.Comment, depth+1)
63
	case *TagDirective:
64
		indent(b, depth)
65
		fmt.Fprintf(b, "TagDirective %s\n", e.Span)
66
		indent(b, depth+1)
67
		fmt.Fprintf(b, "Name: %q\n", e.Name)
68
		dumpOptComment(b, e.Comment, depth+1)
69
	case *YearDirective:
70
		indent(b, depth)
71
		fmt.Fprintf(b, "YearDirective %s\n", e.Span)
72
		indent(b, depth+1)
73
		fmt.Fprintf(b, "Year: %d\n", e.Year)
74
	case *DecimalMarkDirective:
75
		indent(b, depth)
76
		fmt.Fprintf(b, "DecimalMarkDirective %s\n", e.Span)
77
		indent(b, depth+1)
78
		fmt.Fprintf(b, "Mark: %q\n", string(e.Mark))
79
	case *MarketPriceDirective:
80
		dumpMarketPriceDirective(b, e, depth)
81
	case *DefaultCommodityDirective:
82
		indent(b, depth)
83
		fmt.Fprintf(b, "DefaultCommodityDirective %s\n", e.Span)
84
		dumpAmount(b, &e.Amount, depth+1)
85
	case *ApplyDirective:
86
		indent(b, depth)
87
		fmt.Fprintf(b, "ApplyDirective %s\n", e.Span)
88
		indent(b, depth+1)
89
		fmt.Fprintf(b, "Expr: %q\n", e.Expr)
90
		dumpOptComment(b, e.Comment, depth+1)
91
	case *EndDirective:
92
		indent(b, depth)
93
		fmt.Fprintf(b, "EndDirective %s\n", e.Span)
94
		indent(b, depth+1)
95
		fmt.Fprintf(b, "Expr: %q\n", e.Expr)
96
		dumpOptComment(b, e.Comment, depth+1)
97
	case *CommentBlockDirective:
98
		indent(b, depth)
99
		fmt.Fprintf(b, "CommentBlockDirective %s\n", e.Span)
100
		indent(b, depth+1)
101
		fmt.Fprintf(b, "Header: %q\n", e.Header)
102
		indent(b, depth+1)
103
		fmt.Fprintf(b, "Content: %q\n", e.Content)
104
		dumpOptComment(b, e.Comment, depth+1)
105
	case *IgnoredDirective:
106
		indent(b, depth)
107
		fmt.Fprintf(b, "IgnoredDirective %s\n", e.Span)
108
	default:
109
		indent(b, depth)
110
		fmt.Fprintf(b, "Unknown %T\n", e)
111
	}
112
}
113
114
func indent(b *strings.Builder, depth int) {
115
	b.WriteString(strings.Repeat("  ", depth))
116
}
117
118
func dumpTransaction(b *strings.Builder, t *Transaction, depth int) {
119
	indent(b, depth)
120
	fmt.Fprintf(b, "Transaction %s\n", t.Span)
121
	indent(b, depth+1)
122
	fmt.Fprintf(b, "Date: %s\n", dumpDate(t.Date))
123
	if t.SecondDate != nil {
124
		indent(b, depth+1)
125
		fmt.Fprintf(b, "SecondDate: %s\n", dumpDate(*t.SecondDate))
126
	}
127
	if t.Status != nil {
128
		indent(b, depth+1)
129
		fmt.Fprintf(b, "State: %q\n", t.Status.Value)
130
	}
131
	if t.Code != nil {
132
		indent(b, depth+1)
133
		fmt.Fprintf(b, "Code: %q\n", *t.Code)
134
	}
135
	if t.Payee != nil {
136
		indent(b, depth+1)
137
		fmt.Fprintf(b, "Payee: %q %s\n", t.Payee.Name, t.Payee.Span)
138
	}
139
	if t.Note != nil {
140
		indent(b, depth+1)
141
		fmt.Fprintf(b, "Note: %q\n", *t.Note)
142
	}
143
	dumpOptComment(b, t.Comment, depth+1)
144
	if len(t.HeaderComments) > 0 {
145
		indent(b, depth+1)
146
		fmt.Fprintf(b, "HeaderComments %s\n", t.Span)
147
		for _, c := range t.HeaderComments {
148
			dumpComment(b, &c, depth+2)
149
		}
150
	}
151
	for _, p := range t.Postings {
152
		dumpPosting(b, p, depth+1)
153
	}
154
}
155
156
func dumpAutomatedTransaction(b *strings.Builder, t *AutomatedTransaction, depth int) {
157
	indent(b, depth)
158
	fmt.Fprintf(b, "AutomatedTransaction %s\n", t.Span)
159
	indent(b, depth+1)
160
	fmt.Fprintf(b, "Expr: %q\n", t.Expr)
161
	dumpOptComment(b, t.Comment, depth+1)
162
	if len(t.HeaderComments) > 0 {
163
		indent(b, depth+1)
164
		fmt.Fprintf(b, "HeaderComments %s\n", t.Span)
165
		for _, c := range t.HeaderComments {
166
			dumpComment(b, c, depth+2)
167
		}
168
	}
169
	for _, p := range t.Postings {
170
		dumpPosting(b, p, depth+1)
171
	}
172
}
173
174
func dumpPeriodicTransaction(b *strings.Builder, t *PeriodicTransaction, depth int) {
175
	indent(b, depth)
176
	fmt.Fprintf(b, "PeriodicTransaction %s\n", t.Span)
177
	indent(b, depth+1)
178
	fmt.Fprintf(b, "Period: %q\n", t.Period.Raw)
179
	if t.Period.From != nil {
180
		indent(b, depth+1)
181
		fmt.Fprintf(b, "From: %s\n", dumpDate(*t.Period.From))
182
	}
183
	if t.Period.To != nil {
184
		indent(b, depth+1)
185
		fmt.Fprintf(b, "To: %s\n", dumpDate(*t.Period.To))
186
	}
187
	if t.Status != nil {
188
		indent(b, depth+1)
189
		fmt.Fprintf(b, "Status: %q\n", t.Status.Value)
190
	}
191
	if t.Code != nil {
192
		indent(b, depth+1)
193
		fmt.Fprintf(b, "Code: %q\n", *t.Code)
194
	}
195
	if t.Description != nil {
196
		indent(b, depth+1)
197
		fmt.Fprintf(b, "Description: %q\n", *t.Description)
198
	}
199
	dumpOptComment(b, t.Comment, depth+1)
200
	if len(t.HeaderComments) > 0 {
201
		indent(b, depth+1)
202
		fmt.Fprintf(b, "HeaderComments\n")
203
		for _, c := range t.HeaderComments {
204
			dumpComment(b, c, depth+2)
205
		}
206
	}
207
	for _, p := range t.Postings {
208
		dumpPosting(b, p, depth+1)
209
	}
210
}
211
212
func dumpPosting(b *strings.Builder, p *Posting, depth int) {
213
	indent(b, depth)
214
	fmt.Fprintf(b, "Posting %s\n", p.Span)
215
	if p.Type != PostingReal {
216
		indent(b, depth+1)
217
		fmt.Fprintf(b, "Type: %s\n", p.Type)
218
	}
219
	if p.Status != nil {
220
		indent(b, depth+1)
221
		fmt.Fprintf(b, "Status: %q\n", p.Status.Value)
222
	}
223
	dumpAccount(b, p.Account, depth+1)
224
	if p.Amount != nil {
225
		dumpAmount(b, p.Amount, depth+1)
226
	} else {
227
		indent(b, depth+1)
228
		fmt.Fprintf(b, "Amount: <elided>\n")
229
	}
230
	if p.Cost != nil {
231
		dumpCost(b, p.Cost, depth+1)
232
	}
233
	if p.Balance != nil {
234
		dumpBalanceAssertion(b, p.Balance, depth+1)
235
	}
236
	dumpOptComment(b, p.Comment, depth+1)
237
	if len(p.Comments) > 0 {
238
		for _, c := range p.Comments {
239
			dumpComment(b, &c, depth+1)
240
		}
241
	}
242
}
243
244
func dumpAmount(b *strings.Builder, a *Amount, depth int) {
245
	indent(b, depth)
246
	fmt.Fprintf(b, "Amount %s\n", a.Span)
247
	indent(b, depth+1)
248
	fmt.Fprintf(b, "Quantity: %s\n", a.Quantity.String())
249
	indent(b, depth+1)
250
	fmt.Fprintf(b, "Commodity: %q\n", a.Commodity)
251
	indent(b, depth+1)
252
	fmt.Fprintf(b, "CommodityPos: %s\n", a.CommodityPos)
253
	indent(b, depth+1)
254
	fmt.Fprintf(b, "HasSpace: %v\n", a.HasSpace)
255
	if a.IsExpr {
256
		indent(b, depth+1)
257
		fmt.Fprintf(b, "IsExpr: true\n")
258
	}
259
	if a.Expr != "" {
260
		indent(b, depth+1)
261
		fmt.Fprintf(b, "Expr: %q\n", a.Expr)
262
	}
263
	indent(b, depth+1)
264
	fmt.Fprintf(b, "Precision: %d\n", a.QuantityFmt.Precision)
265
	indent(b, depth+1)
266
	fmt.Fprintf(b, "Decimal: %q\n", string(a.QuantityFmt.Decimal))
267
	if a.QuantityFmt.Thousands != 0 {
268
		indent(b, depth+1)
269
		fmt.Fprintf(b, "Thousands: %q\n", string(a.QuantityFmt.Thousands))
270
	}
271
}
272
273
func dumpCost(b *strings.Builder, c *Cost, depth int) {
274
	indent(b, depth)
275
	if c.IsTotal {
276
		fmt.Fprintf(b, "Cost(total) %s\n", c.Span)
277
	} else {
278
		fmt.Fprintf(b, "Cost(unit) %s\n", c.Span)
279
	}
280
	dumpAmount(b, &c.Amount, depth+1)
281
}
282
283
func dumpBalanceAssertion(b *strings.Builder, ba *BalanceAssertion, depth int) {
284
	indent(b, depth)
285
	fmt.Fprintf(b, "BalanceAssertion %s\n", ba.Span)
286
	indent(b, depth+1)
287
	fmt.Fprintf(b, "IsStrict: %v\n", ba.IsStrict)
288
	indent(b, depth+1)
289
	fmt.Fprintf(b, "IsInclusive: %v\n", ba.IsInclusive)
290
	dumpAmount(b, &ba.Amount, depth+1)
291
}
292
293
func dumpAccount(b *strings.Builder, a Account, depth int) {
294
	indent(b, depth)
295
	fmt.Fprintf(b, "Account %q %s\n", a.Name, a.Span)
296
}
297
298
func dumpAccountDirective(b *strings.Builder, a *AccountDirective, depth int) {
299
	indent(b, depth)
300
	fmt.Fprintf(b, "AccountDirective %s\n", a.Span)
301
	dumpAccount(b, a.Account, depth+1)
302
	dumpOptComment(b, a.Comment, depth+1)
303
}
304
305
func dumpCommodityDirective(b *strings.Builder, c *CommodityDirective, depth int) {
306
	indent(b, depth)
307
	fmt.Fprintf(b, "CommodityDirective %s\n", c.Span)
308
	indent(b, depth+1)
309
	fmt.Fprintf(b, "Commodity: %q\n", c.Commodity)
310
	dumpAmount(b, &c.Format, depth+1)
311
	dumpOptComment(b, c.Comment, depth+1)
312
}
313
314
func dumpMarketPriceDirective(b *strings.Builder, m *MarketPriceDirective, depth int) {
315
	indent(b, depth)
316
	fmt.Fprintf(b, "MarketPriceDirective %s\n", m.Span)
317
	indent(b, depth+1)
318
	fmt.Fprintf(b, "Date: %s\n", dumpDate(m.DateTime.Date))
319
	if m.DateTime.Time != nil {
320
		indent(b, depth+1)
321
		fmt.Fprintf(b, "Time: %02d:%02d:%02d\n", m.DateTime.Time.Hour, m.DateTime.Time.Minute, m.DateTime.Time.Second)
322
	}
323
	indent(b, depth+1)
324
	fmt.Fprintf(b, "Commodity: %q\n", m.Commodity)
325
	dumpAmount(b, &m.Amount, depth+1)
326
}
327
328
func dumpComment(b *strings.Builder, c *Comment, depth int) {
329
	indent(b, depth)
330
	fmt.Fprintf(b, "Comment %s\n", c.Span)
331
	indent(b, depth+1)
332
	fmt.Fprintf(b, "Marker: %q\n", string(c.Marker))
333
	indent(b, depth+1)
334
	fmt.Fprintf(b, "Text: %q\n", c.Text)
335
}
336
337
func dumpOptComment(b *strings.Builder, c *Comment, depth int) {
338
	if c == nil {
339
		return
340
	}
341
	dumpComment(b, c, depth)
342
}
343
344
func dumpDate(d Date) string {
345
	return fmt.Sprintf("%04d%s%02d%s%02d", d.Year, string(d.Sep), d.Month, string(d.Sep), d.Day)
346
}