all repos

clerk @ 0b03250

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
lexer & parser & ast..., 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 *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
	if t.Period != nil {
178
		indent(b, depth+1)
179
		fmt.Fprintf(b, "Period: %q\n", t.Period.Raw)
180
		if t.Period.From != nil {
181
			indent(b, depth+1)
182
			fmt.Fprintf(b, "From: %s\n", dumpDate(*t.Period.From))
183
		}
184
		if t.Period.To != nil {
185
			indent(b, depth+1)
186
			fmt.Fprintf(b, "To: %s\n", dumpDate(*t.Period.To))
187
		}
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
	if t.Note != nil {
202
		indent(b, depth+1)
203
		fmt.Fprintf(b, "Note: %q\n", *t.Note)
204
	}
205
	dumpOptComment(b, t.Comment, depth+1)
206
	if len(t.HeaderComments) > 0 {
207
		indent(b, depth+1)
208
		fmt.Fprintf(b, "HeaderComments\n")
209
		for _, c := range t.HeaderComments {
210
			dumpComment(b, c, depth+2)
211
		}
212
	}
213
	for _, p := range t.Postings {
214
		dumpPosting(b, p, depth+1)
215
	}
216
}
217
218
func dumpPosting(b *strings.Builder, p *Posting, depth int) {
219
	indent(b, depth)
220
	fmt.Fprintf(b, "Posting %s\n", p.Span)
221
	if p.Type != PostingReal {
222
		indent(b, depth+1)
223
		fmt.Fprintf(b, "Type: %s\n", p.Type)
224
	}
225
	if p.Status != nil {
226
		indent(b, depth+1)
227
		fmt.Fprintf(b, "Status: %q\n", p.Status.Value)
228
	}
229
	dumpAccount(b, p.Account, depth+1)
230
	if p.Amount != nil {
231
		dumpAmount(b, p.Amount, depth+1)
232
	} else {
233
		indent(b, depth+1)
234
		fmt.Fprintf(b, "Amount: <elided>\n")
235
	}
236
	if p.Cost != nil {
237
		dumpCost(b, p.Cost, depth+1)
238
	}
239
	if p.Balance != nil {
240
		dumpBalanceAssertion(b, p.Balance, depth+1)
241
	}
242
	dumpOptComment(b, p.Comment, depth+1)
243
	if len(p.Comments) > 0 {
244
		for _, c := range p.Comments {
245
			dumpComment(b, &c, depth+1)
246
		}
247
	}
248
}
249
250
func dumpAmount(b *strings.Builder, a *Amount, depth int) {
251
	indent(b, depth)
252
	fmt.Fprintf(b, "Amount %s\n", a.Span)
253
	indent(b, depth+1)
254
	fmt.Fprintf(b, "Quantity: %s\n", a.Quantity.String())
255
	indent(b, depth+1)
256
	fmt.Fprintf(b, "Commodity: %q\n", a.Commodity)
257
	indent(b, depth+1)
258
	fmt.Fprintf(b, "CommodityPos: %s\n", a.CommodityPos)
259
	indent(b, depth+1)
260
	fmt.Fprintf(b, "HasSpace: %v\n", a.HasSpace)
261
	if a.IsExpr {
262
		indent(b, depth+1)
263
		fmt.Fprintf(b, "IsExpr: true\n")
264
	}
265
	if a.Expr != "" {
266
		indent(b, depth+1)
267
		fmt.Fprintf(b, "Expr: %q\n", a.Expr)
268
	}
269
	indent(b, depth+1)
270
	fmt.Fprintf(b, "Precision: %d\n", a.QuantityFmt.Precision)
271
	indent(b, depth+1)
272
	fmt.Fprintf(b, "Decimal: %q\n", string(a.QuantityFmt.Decimal))
273
	if a.QuantityFmt.Thousands != 0 {
274
		indent(b, depth+1)
275
		fmt.Fprintf(b, "Thousands: %q\n", string(a.QuantityFmt.Thousands))
276
	}
277
}
278
279
func dumpCost(b *strings.Builder, c *Cost, depth int) {
280
	indent(b, depth)
281
	if c.IsTotal {
282
		fmt.Fprintf(b, "Cost(total) %s\n", c.Span)
283
	} else {
284
		fmt.Fprintf(b, "Cost(unit) %s\n", c.Span)
285
	}
286
	dumpAmount(b, c.Amount, depth+1)
287
}
288
289
func dumpBalanceAssertion(b *strings.Builder, ba *BalanceAssertion, depth int) {
290
	indent(b, depth)
291
	fmt.Fprintf(b, "BalanceAssertion %s\n", ba.Span)
292
	indent(b, depth+1)
293
	fmt.Fprintf(b, "IsStrict: %v\n", ba.IsStrict)
294
	indent(b, depth+1)
295
	fmt.Fprintf(b, "IsInclusive: %v\n", ba.IsInclusive)
296
	dumpAmount(b, &ba.Amount, depth+1)
297
}
298
299
func dumpAccount(b *strings.Builder, a Account, depth int) {
300
	indent(b, depth)
301
	fmt.Fprintf(b, "Account %q %s\n", a.Name, a.Span)
302
}
303
304
func dumpAccountDirective(b *strings.Builder, a *AccountDirective, depth int) {
305
	indent(b, depth)
306
	fmt.Fprintf(b, "AccountDirective %s\n", a.Span)
307
	dumpAccount(b, a.Account, depth+1)
308
	dumpOptComment(b, a.Comment, depth+1)
309
}
310
311
func dumpCommodityDirective(b *strings.Builder, c *CommodityDirective, depth int) {
312
	indent(b, depth)
313
	fmt.Fprintf(b, "CommodityDirective %s\n", c.Span)
314
	indent(b, depth+1)
315
	fmt.Fprintf(b, "Commodity: %q\n", c.Commodity)
316
	if c.Format != nil {
317
		dumpAmount(b, c.Format, depth+1)
318
	}
319
	dumpOptComment(b, c.Comment, depth+1)
320
}
321
322
func dumpMarketPriceDirective(b *strings.Builder, m *MarketPriceDirective, depth int) {
323
	indent(b, depth)
324
	fmt.Fprintf(b, "MarketPriceDirective %s\n", m.Span)
325
	indent(b, depth+1)
326
	fmt.Fprintf(b, "Date: %s\n", dumpDate(m.DateTime.Date))
327
	if m.DateTime.Time != nil {
328
		indent(b, depth+1)
329
		fmt.Fprintf(b, "Time: %02d:%02d:%02d\n", m.DateTime.Time.Hour, m.DateTime.Time.Minute, m.DateTime.Time.Second)
330
	}
331
	indent(b, depth+1)
332
	fmt.Fprintf(b, "Commodity: %q\n", m.Commodity)
333
	dumpAmount(b, &m.Amount, depth+1)
334
}
335
336
func dumpComment(b *strings.Builder, c *Comment, depth int) {
337
	indent(b, depth)
338
	fmt.Fprintf(b, "Comment %s\n", c.Span)
339
	indent(b, depth+1)
340
	fmt.Fprintf(b, "Marker: %q\n", string(c.Marker))
341
	indent(b, depth+1)
342
	fmt.Fprintf(b, "Text: %q\n", c.Text)
343
}
344
345
func dumpOptComment(b *strings.Builder, c *Comment, depth int) {
346
	if c == nil {
347
		return
348
	}
349
	dumpComment(b, c, depth)
350
}
351
352
func dumpDate(d Date) string {
353
	return fmt.Sprintf("%04d%s%02d%s%02d", d.Year, string(d.Sep), d.Month, string(d.Sep), d.Day)
354
}