all repos

clerk @ olex/oxrnnk

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
formatter, 20 hours ago
1
package ast
2
3
import (
4
	"olexsmir.xyz/clerk/internal/decimal"
5
	"olexsmir.xyz/clerk/journal/token"
6
)
7
8
type BlankLine struct{ Span token.Span }
9
10
func (BlankLine) entryNode() {}
11
12
type Transaction struct {
13
	Date           Date
14
	SecondDate     *Date     // optional =2026-05-18 date
15
	Status         *Status   // optional */! status
16
	Code           *string   // optional (123) code
17
	Payee          *Payee    // optional payee
18
	Note           *string   // part after |
19
	Comment        *Comment  // inline ; on header line
20
	HeaderComments []*Comment // indented ; lines before first posting
21
	Postings       []*Posting
22
	Span           token.Span
23
}
24
25
func (Transaction) entryNode() {}
26
27
type Period struct {
28
	Raw  string // "monthly", "every 2 weeks"
29
	From *Date
30
	To   *Date
31
	Span token.Span
32
}
33
34
func (Period) entryNode() {}
35
36
type PeriodicTransaction struct {
37
	Period         Period   // period-expr
38
	Status         *Status  // optional */! status
39
	Code           *string  // optional (123) code
40
	Description    *string  // optional description
41
	Comment        *Comment // optional inline comment
42
	HeaderComments []*Comment
43
	Postings       []*Posting
44
	Span           token.Span
45
}
46
47
func (PeriodicTransaction) entryNode() {}
48
49
type AutomatedTransaction struct {
50
	Expr           string
51
	Postings       []*Posting
52
	Comment        *Comment   // inline ; on header line
53
	HeaderComments []*Comment // indented ; lines before first posting
54
	Span           token.Span
55
}
56
57
func (AutomatedTransaction) entryNode() {}
58
59
type PostingType int
60
61
const (
62
	PostingReal              PostingType = iota
63
	PostingVirtualBalanced               // '['
64
	PostingVirtualUnbalanced             // '('
65
)
66
67
func (p PostingType) String() string {
68
	switch p {
69
	case PostingReal:
70
		return "real"
71
	case PostingVirtualBalanced:
72
		return "balanced virtual"
73
	case PostingVirtualUnbalanced:
74
		return "unbalanced virtual"
75
	default:
76
		panic("unreachable")
77
	}
78
}
79
80
type Posting struct {
81
	Type     PostingType
82
	Status   *Status
83
	Account  Account
84
	Amount   *Amount // nil == auto-balancing
85
	Cost     *Cost   // @ @@
86
	Balance  *BalanceAssertion
87
	Comment  *Comment
88
	Comments []Comment // continuation comment lines
89
	Span     token.Span
90
}
91
92
type Amount struct {
93
	IsNegative   bool
94
	Quantity     decimal.Decimal
95
	QuantityFmt  QuantityFormat
96
	Commodity    string
97
	CommodityPos CommodityPos // Before | After
98
	HasSpace     bool         // "$10" vs "$ 10"
99
	IsExpr       bool         // e.g: *-1
100
	Expr         string       // expression text e.g. "amount * -1". set only if IsExpr is true
101
	Span         token.Span
102
}
103
104
type Cost struct {
105
	IsTotal bool // @ vs @@
106
	Amount  Amount
107
	Span    token.Span
108
}
109
110
type BalanceAssertion struct {
111
	IsStrict    bool // ==  vs =
112
	IsInclusive bool // ===
113
	Amount      Amount
114
	Span        token.Span
115
}
116
117
type CommodityPos int
118
119
func (c CommodityPos) String() string {
120
	if c == CommodityBefore {
121
		return "Before"
122
	}
123
	return "After"
124
}
125
126
const (
127
	CommodityBefore CommodityPos = iota
128
	CommodityAfter
129
)
130
131
type QuantityFormat struct {
132
	Decimal   byte // '.' or ','
133
	Thousands byte // ',' '.' ' ' or 0
134
	Precision int
135
}