all repos

clerk @ 7136c82232d67825b9ccd590797f85659c3a23bd

missing tooling for ledger/hledger

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

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