all repos

clerk @ ed49a0d

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
add support of subdirectives and it's highlights, 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         Status     // optional */! status
16
	Code           *Code      // optional (123) code
17
	Payee          *Payee     // optional payee
18
	Note           *Note      // 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           *Code        // optional (123) code
40
	Description    *Description // 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           Expr
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
	CommoditySpan token.Span   // span of the commodity within the amount
99
	HasSpace      bool         // "$10" vs "$ 10"
100
	IsExpr        bool         // e.g: *-1
101
	Expr          string       // expression text e.g. "amount * -1". set only if IsExpr is true
102
	Span          token.Span
103
}
104
105
type Cost struct {
106
	IsTotal bool // @ vs @@
107
	Amount  Amount
108
	Span    token.Span
109
}
110
111
type BalanceAssertion struct {
112
	IsStrict     bool // ==  vs =
113
	IsInclusive  bool // =* or === (inclusive of subaccounts)
114
	IsAssignment bool // := balance assignment (not a check)
115
	Amount       Amount
116
	Cost         *Cost // price after @/@@
117
	Span         token.Span
118
}
119
120
type CommodityPos int
121
122
func (c CommodityPos) String() string {
123
	if c == CommodityBefore {
124
		return "Before"
125
	}
126
	return "After"
127
}
128
129
const (
130
	CommodityBefore CommodityPos = iota
131
	CommodityAfter
132
)
133
134
type QuantityFormat struct {
135
	Decimal   byte // '.' or ','; 0 when the literal has no explicit decimal mark
136
	Thousands byte // ',' '.' ' ' or 0
137
	Precision int
138
}