all repos

clerk @ 6573acb6af84cdef8bdde568c92ebc1509eb600f

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ast: inline leaf value+span wrapper structs, 1 month 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         StatusType // optional */! status
16
	StatusSpan     token.Span
17
	Code           string // optional (123) code
18
	CodeSpan       token.Span
19
	Payee          string // optional payee
20
	PayeeSpan      token.Span
21
	Note           string // part after |
22
	NoteSpan       token.Span
23
	Comment        *Comment   // inline ; on header line
24
	HeaderComments []*Comment // indented ; lines before first posting
25
	Postings       []Posting
26
	Span           token.Span
27
}
28
29
func (Transaction) entryNode() {}
30
31
type Period struct {
32
	Raw  string // "monthly", "every 2 weeks"
33
	From *Date
34
	To   *Date
35
	Span token.Span
36
}
37
38
func (Period) entryNode() {}
39
40
type PeriodicTransaction struct {
41
	Period          Period // period-expr
42
	Status          StatusType
43
	StatusSpan      token.Span
44
	Code            string
45
	CodeSpan        token.Span
46
	Description     string
47
	DescriptionSpan token.Span
48
	Comment         *Comment // optional inline comment
49
	HeaderComments  []*Comment
50
	Postings        []Posting
51
	Span            token.Span
52
}
53
54
func (PeriodicTransaction) entryNode() {}
55
56
type AutomatedTransaction struct {
57
	Expr           string
58
	ExprSpan       token.Span
59
	Postings       []Posting
60
	Comment        *Comment   // inline ; on header line
61
	HeaderComments []*Comment // indented ; lines before first posting
62
	Span           token.Span
63
}
64
65
func (AutomatedTransaction) entryNode() {}
66
67
type PostingType int
68
69
const (
70
	PostingReal              PostingType = iota
71
	PostingVirtualBalanced               // '['
72
	PostingVirtualUnbalanced             // '('
73
)
74
75
func (p PostingType) String() string {
76
	switch p {
77
	case PostingReal:
78
		return "real"
79
	case PostingVirtualBalanced:
80
		return "balanced virtual"
81
	case PostingVirtualUnbalanced:
82
		return "unbalanced virtual"
83
	default:
84
		panic("unreachable")
85
	}
86
}
87
88
type Posting struct {
89
	Type       PostingType
90
	Status     StatusType
91
	StatusSpan token.Span
92
	Account    Account
93
	Amount     *Amount // nil == auto-balancing
94
	Cost       *Cost   // @ @@
95
	Balance    *BalanceAssertion
96
	Comment    *Comment
97
	Comments   []Comment // continuation comment lines
98
	Span       token.Span
99
}
100
101
type Amount struct {
102
	IsNegative    bool
103
	Quantity      decimal.Decimal
104
	QuantityFmt   QuantityFormat
105
	Commodity     string
106
	CommodityPos  CommodityPos // Before | After
107
	CommoditySpan token.Span   // span of the commodity within the amount
108
	HasSpace      bool         // "$10" vs "$ 10"
109
	IsExpr        bool         // e.g: *-1
110
	Expr          string       // expression text e.g. "amount * -1". set only if IsExpr is true
111
	Span          token.Span
112
}
113
114
type Cost struct {
115
	IsTotal bool // @ vs @@
116
	Amount  Amount
117
	Span    token.Span
118
}
119
120
type BalanceAssertion struct {
121
	IsStrict     bool // ==  vs =
122
	IsInclusive  bool // =* or === (inclusive of subaccounts)
123
	IsAssignment bool // := balance assignment (not a check)
124
	Amount       Amount
125
	Cost         *Cost // price after @/@@
126
	Span         token.Span
127
}
128
129
type CommodityPos int
130
131
func (c CommodityPos) String() string {
132
	if c == CommodityBefore {
133
		return "Before"
134
	}
135
	return "After"
136
}
137
138
const (
139
	CommodityBefore CommodityPos = iota
140
	CommodityAfter
141
)
142
143
type QuantityFormat struct {
144
	Decimal   byte // '.' or ','; 0 when the literal has no explicit decimal mark
145
	Thousands byte // ',' '.' ' ' or 0
146
	Precision int
147
}