all repos

clerk @ 66c8add

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
parser: support C conversion directive, 14 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 *ConversionDirective:
82
		dumpConversionDirective(b, e, depth)
83
	case *DefaultCommodityDirective:
84
		indent(b, depth)
85
		fmt.Fprintf(b, "DefaultCommodityDirective %s\n", e.Span)
86
		dumpAmount(b, &e.Amount, depth+1)
87
	case *ApplyDirective:
88
		indent(b, depth)
89
		fmt.Fprintf(b, "ApplyDirective %s\n", e.Span)
90
		indent(b, depth+1)
91
		fmt.Fprintf(b, "Expr: %q\n", e.Expr)
92
		dumpOptComment(b, e.Comment, depth+1)
93
	case *EndDirective:
94
		indent(b, depth)
95
		fmt.Fprintf(b, "EndDirective %s\n", e.Span)
96
		indent(b, depth+1)
97
		fmt.Fprintf(b, "Expr: %q\n", e.Expr)
98
		dumpOptComment(b, e.Comment, depth+1)
99
	case *CommentBlockDirective:
100
		indent(b, depth)
101
		fmt.Fprintf(b, "CommentBlockDirective %s\n", e.Span)
102
		indent(b, depth+1)
103
		fmt.Fprintf(b, "Header: %q\n", e.Header)
104
		indent(b, depth+1)
105
		fmt.Fprintf(b, "Content: %q\n", e.Content)
106
		dumpOptComment(b, e.Comment, depth+1)
107
	case *IgnoredDirective:
108
		indent(b, depth)
109
		fmt.Fprintf(b, "IgnoredDirective %s\n", e.Span)
110
	default:
111
		indent(b, depth)
112
		fmt.Fprintf(b, "Unknown %T\n", e)
113
	}
114
}
115
116
func indent(b *strings.Builder, depth int) {
117
	b.WriteString(strings.Repeat("  ", depth))
118
}
119
120
func dumpTransaction(b *strings.Builder, t *Transaction, depth int) {
121
	indent(b, depth)
122
	fmt.Fprintf(b, "Transaction %s\n", t.Span)
123
	indent(b, depth+1)
124
	fmt.Fprintf(b, "Date: %s\n", dumpDate(t.Date))
125
	if t.SecondDate != nil {
126
		indent(b, depth+1)
127
		fmt.Fprintf(b, "SecondDate: %s\n", dumpDate(*t.SecondDate))
128
	}
129
	if t.Status != nil {
130
		indent(b, depth+1)
131
		fmt.Fprintf(b, "State: %q\n", t.Status.Value)
132
	}
133
	if t.Code != nil {
134
		indent(b, depth+1)
135
		fmt.Fprintf(b, "Code: %q\n", *t.Code)
136
	}
137
	if t.Payee != nil {
138
		indent(b, depth+1)
139
		fmt.Fprintf(b, "Payee: %q %s\n", t.Payee.Name, t.Payee.Span)
140
	}
141
	if t.Note != nil {
142
		indent(b, depth+1)
143
		fmt.Fprintf(b, "Note: %q\n", *t.Note)
144
	}
145
	dumpOptComment(b, t.Comment, depth+1)
146
	if len(t.HeaderComments) > 0 {
147
		indent(b, depth+1)
148
		fmt.Fprintf(b, "HeaderComments %s\n", t.Span)
149
		for _, c := range t.HeaderComments {
150
			dumpComment(b, &c, depth+2)
151
		}
152
	}
153
	for _, p := range t.Postings {
154
		dumpPosting(b, p, depth+1)
155
	}
156
}
157
158
func dumpAutomatedTransaction(b *strings.Builder, t *AutomatedTransaction, depth int) {
159
	indent(b, depth)
160
	fmt.Fprintf(b, "AutomatedTransaction %s\n", t.Span)
161
	indent(b, depth+1)
162
	fmt.Fprintf(b, "Expr: %q\n", t.Expr)
163
	dumpOptComment(b, t.Comment, depth+1)
164
	if len(t.HeaderComments) > 0 {
165
		indent(b, depth+1)
166
		fmt.Fprintf(b, "HeaderComments %s\n", t.Span)
167
		for _, c := range t.HeaderComments {
168
			dumpComment(b, c, depth+2)
169
		}
170
	}
171
	for _, p := range t.Postings {
172
		dumpPosting(b, p, depth+1)
173
	}
174
}
175
176
func dumpPeriodicTransaction(b *strings.Builder, t *PeriodicTransaction, depth int) {
177
	indent(b, depth)
178
	fmt.Fprintf(b, "PeriodicTransaction %s\n", t.Span)
179
	indent(b, depth+1)
180
	fmt.Fprintf(b, "Period: %q\n", t.Period.Raw)
181
	if t.Period.From != nil {
182
		indent(b, depth+1)
183
		fmt.Fprintf(b, "From: %s\n", dumpDate(*t.Period.From))
184
	}
185
	if t.Period.To != nil {
186
		indent(b, depth+1)
187
		fmt.Fprintf(b, "To: %s\n", dumpDate(*t.Period.To))
188
	}
189
	if t.Status != nil {
190
		indent(b, depth+1)
191
		fmt.Fprintf(b, "Status: %q\n", t.Status.Value)
192
	}
193
	if t.Code != nil {
194
		indent(b, depth+1)
195
		fmt.Fprintf(b, "Code: %q\n", *t.Code)
196
	}
197
	if t.Description != nil {
198
		indent(b, depth+1)
199
		fmt.Fprintf(b, "Description: %q\n", *t.Description)
200
	}
201
	dumpOptComment(b, t.Comment, depth+1)
202
	if len(t.HeaderComments) > 0 {
203
		indent(b, depth+1)
204
		fmt.Fprintf(b, "HeaderComments\n")
205
		for _, c := range t.HeaderComments {
206
			dumpComment(b, c, depth+2)
207
		}
208
	}
209
	for _, p := range t.Postings {
210
		dumpPosting(b, p, depth+1)
211
	}
212
}
213
214
func dumpPosting(b *strings.Builder, p *Posting, depth int) {
215
	indent(b, depth)
216
	fmt.Fprintf(b, "Posting %s\n", p.Span)
217
	if p.Type != PostingReal {
218
		indent(b, depth+1)
219
		fmt.Fprintf(b, "Type: %s\n", p.Type)
220
	}
221
	if p.Status != nil {
222
		indent(b, depth+1)
223
		fmt.Fprintf(b, "Status: %q\n", p.Status.Value)
224
	}
225
	dumpAccount(b, p.Account, depth+1)
226
	if p.Amount != nil {
227
		dumpAmount(b, p.Amount, depth+1)
228
	} else {
229
		indent(b, depth+1)
230
		fmt.Fprintf(b, "Amount: <elided>\n")
231
	}
232
	if p.Cost != nil {
233
		dumpCost(b, p.Cost, depth+1)
234
	}
235
	if p.Balance != nil {
236
		dumpBalanceAssertion(b, p.Balance, depth+1)
237
	}
238
	dumpOptComment(b, p.Comment, depth+1)
239
	if len(p.Comments) > 0 {
240
		for _, c := range p.Comments {
241
			dumpComment(b, &c, depth+1)
242
		}
243
	}
244
}
245
246
func dumpAmount(b *strings.Builder, a *Amount, depth int) {
247
	indent(b, depth)
248
	fmt.Fprintf(b, "Amount %s\n", a.Span)
249
	indent(b, depth+1)
250
	fmt.Fprintf(b, "Quantity: %s\n", a.Quantity.String())
251
	indent(b, depth+1)
252
	fmt.Fprintf(b, "Commodity: %q\n", a.Commodity)
253
	indent(b, depth+1)
254
	fmt.Fprintf(b, "CommodityPos: %s\n", a.CommodityPos)
255
	indent(b, depth+1)
256
	fmt.Fprintf(b, "HasSpace: %v\n", a.HasSpace)
257
	if a.IsExpr {
258
		indent(b, depth+1)
259
		fmt.Fprintf(b, "IsExpr: true\n")
260
	}
261
	if a.Expr != "" {
262
		indent(b, depth+1)
263
		fmt.Fprintf(b, "Expr: %q\n", a.Expr)
264
	}
265
	indent(b, depth+1)
266
	fmt.Fprintf(b, "Precision: %d\n", a.QuantityFmt.Precision)
267
	indent(b, depth+1)
268
	fmt.Fprintf(b, "Decimal: %q\n", string(a.QuantityFmt.Decimal))
269
	if a.QuantityFmt.Thousands != 0 {
270
		indent(b, depth+1)
271
		fmt.Fprintf(b, "Thousands: %q\n", string(a.QuantityFmt.Thousands))
272
	}
273
}
274
275
func dumpCost(b *strings.Builder, c *Cost, depth int) {
276
	indent(b, depth)
277
	if c.IsTotal {
278
		fmt.Fprintf(b, "Cost(total) %s\n", c.Span)
279
	} else {
280
		fmt.Fprintf(b, "Cost(unit) %s\n", c.Span)
281
	}
282
	dumpAmount(b, &c.Amount, depth+1)
283
}
284
285
func dumpBalanceAssertion(b *strings.Builder, ba *BalanceAssertion, depth int) {
286
	indent(b, depth)
287
	fmt.Fprintf(b, "BalanceAssertion %s\n", ba.Span)
288
	indent(b, depth+1)
289
	fmt.Fprintf(b, "IsStrict: %v\n", ba.IsStrict)
290
	indent(b, depth+1)
291
	fmt.Fprintf(b, "IsInclusive: %v\n", ba.IsInclusive)
292
	dumpAmount(b, &ba.Amount, depth+1)
293
}
294
295
func dumpAccount(b *strings.Builder, a Account, depth int) {
296
	indent(b, depth)
297
	fmt.Fprintf(b, "Account %q %s\n", a.Name, a.Span)
298
}
299
300
func dumpAccountDirective(b *strings.Builder, a *AccountDirective, depth int) {
301
	indent(b, depth)
302
	fmt.Fprintf(b, "AccountDirective %s\n", a.Span)
303
	dumpAccount(b, a.Account, depth+1)
304
	dumpOptComment(b, a.Comment, depth+1)
305
}
306
307
func dumpCommodityDirective(b *strings.Builder, c *CommodityDirective, depth int) {
308
	indent(b, depth)
309
	fmt.Fprintf(b, "CommodityDirective %s\n", c.Span)
310
	indent(b, depth+1)
311
	fmt.Fprintf(b, "Commodity: %q\n", c.Commodity)
312
	if c.Format.QuantityFmt.Decimal != 0 {
313
		dumpAmount(b, &c.Format, depth+1)
314
	}
315
	dumpOptComment(b, c.Comment, depth+1)
316
}
317
318
func dumpMarketPriceDirective(b *strings.Builder, m *MarketPriceDirective, depth int) {
319
	indent(b, depth)
320
	fmt.Fprintf(b, "MarketPriceDirective %s\n", m.Span)
321
	indent(b, depth+1)
322
	fmt.Fprintf(b, "Date: %s\n", dumpDate(m.DateTime.Date))
323
	if m.DateTime.Time != nil {
324
		indent(b, depth+1)
325
		fmt.Fprintf(b, "Time: %02d:%02d:%02d\n", m.DateTime.Time.Hour, m.DateTime.Time.Minute, m.DateTime.Time.Second)
326
	}
327
	indent(b, depth+1)
328
	fmt.Fprintf(b, "Commodity: %q\n", m.Commodity)
329
	dumpAmount(b, &m.Amount, depth+1)
330
}
331
332
func dumpConversionDirective(b *strings.Builder, c *ConversionDirective, depth int) {
333
	indent(b, depth)
334
	fmt.Fprintf(b, "ConversionDirective %s\n", c.Span)
335
	indent(b, depth+1)
336
	fmt.Fprintf(b, "From:\n")
337
	dumpAmount(b, &c.From, depth+2)
338
	indent(b, depth+1)
339
	fmt.Fprintf(b, "To:\n")
340
	dumpAmount(b, &c.To, depth+2)
341
	dumpOptComment(b, c.Comment, depth+1)
342
}
343
344
func dumpComment(b *strings.Builder, c *Comment, depth int) {
345
	indent(b, depth)
346
	fmt.Fprintf(b, "Comment %s\n", c.Span)
347
	indent(b, depth+1)
348
	fmt.Fprintf(b, "Marker: %q\n", string(c.Marker))
349
	indent(b, depth+1)
350
	fmt.Fprintf(b, "Text: %q\n", c.Text)
351
}
352
353
func dumpOptComment(b *strings.Builder, c *Comment, depth int) {
354
	if c == nil {
355
		return
356
	}
357
	dumpComment(b, c, depth)
358
}
359
360
func dumpDate(d Date) string {
361
	return fmt.Sprintf("%04d%s%02d%s%02d", d.Year, string(d.Sep), d.Month, string(d.Sep), d.Day)
362
}