clerk/journal/ast/ast.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com fix: journal used to parse unset transaction status as *; print expressions, 2 months ago
olexsmir@gmail.com fix: journal used to parse unset transaction status as *; print expressions, 2 months ago
| 1 | package ast |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "olexsmir.xyz/clerk/journal/token" |
| 7 | ) |
| 8 | |
| 9 | type Journal struct { |
| 10 | Entries []Entry |
| 11 | Errors []*ParseError |
| 12 | } |
| 13 | |
| 14 | type Entry interface { |
| 15 | entryNode() |
| 16 | } |
| 17 | |
| 18 | type ParseError struct { |
| 19 | Span token.Span |
| 20 | Message string |
| 21 | } |
| 22 | |
| 23 | type FileError struct { |
| 24 | Path string |
| 25 | Span token.Span |
| 26 | Message string |
| 27 | } |
| 28 | |
| 29 | type Date struct { |
| 30 | Year, Month, Day int |
| 31 | Sep byte // '-' '/' '.' |
| 32 | Span token.Span |
| 33 | } |
| 34 | |
| 35 | // Compare returns -1 if d is before other, 0 if equal, 1 if after. |
| 36 | func (d Date) Compare(other Date) int { |
| 37 | if d.Year != other.Year { |
| 38 | if d.Year < other.Year { |
| 39 | return -1 |
| 40 | } |
| 41 | return 1 |
| 42 | } |
| 43 | if d.Month != other.Month { |
| 44 | if d.Month < other.Month { |
| 45 | return -1 |
| 46 | } |
| 47 | return 1 |
| 48 | } |
| 49 | if d.Day != other.Day { |
| 50 | if d.Day < other.Day { |
| 51 | return -1 |
| 52 | } |
| 53 | return 1 |
| 54 | } |
| 55 | return 0 |
| 56 | } |
| 57 | |
| 58 | type Time struct { |
| 59 | Hour, Minute, Second int |
| 60 | Span token.Span |
| 61 | } |
| 62 | |
| 63 | type DateTime struct { |
| 64 | Date Date |
| 65 | Time *Time |
| 66 | Span token.Span |
| 67 | } |
| 68 | |
| 69 | type Comment struct { |
| 70 | Marker byte // ';' '#' '%' '*' |
| 71 | Text string |
| 72 | Span token.Span |
| 73 | } |
| 74 | |
| 75 | func (Comment) entryNode() {} |
| 76 | |
| 77 | type StatusType int |
| 78 | |
| 79 | func (s StatusType) String() string { |
| 80 | switch s { |
| 81 | case StatusCleared: |
| 82 | return "*" |
| 83 | case StatusPending: |
| 84 | return "!" |
| 85 | case StatusNone: |
| 86 | return "" |
| 87 | default: |
| 88 | panic("unreachable") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const ( |
| 93 | StatusNone StatusType = iota // not set |
| 94 | StatusCleared // * cleared |
| 95 | StatusPending // ! pending |
| 96 | ) |
| 97 | |
| 98 | type Status struct { |
| 99 | Value StatusType |
| 100 | Span token.Span |
| 101 | } |
| 102 | |
| 103 | type Code struct { |
| 104 | Value string |
| 105 | Span token.Span |
| 106 | } |
| 107 | |
| 108 | type Note struct { |
| 109 | Value string |
| 110 | Span token.Span |
| 111 | } |
| 112 | |
| 113 | type Description struct { |
| 114 | Value string |
| 115 | Span token.Span |
| 116 | } |
| 117 | |
| 118 | type Expr struct { |
| 119 | Value string |
| 120 | Span token.Span |
| 121 | } |
| 122 | |
| 123 | type Payee struct { |
| 124 | Name string |
| 125 | Span token.Span |
| 126 | } |
| 127 | |
| 128 | type SubAccount struct { |
| 129 | Name string |
| 130 | Span token.Span |
| 131 | } |
| 132 | |
| 133 | type Account struct { |
| 134 | Name []SubAccount // ['expenses' 'food'] |
| 135 | Span token.Span |
| 136 | } |
| 137 | |
| 138 | func (a Account) String() string { |
| 139 | if len(a.Name) == 0 { |
| 140 | return "" |
| 141 | } |
| 142 | var name strings.Builder |
| 143 | name.Grow(len(a.Name)) |
| 144 | name.WriteString(a.Name[0].Name) |
| 145 | for _, s := range a.Name[1:] { |
| 146 | name.WriteByte(':') |
| 147 | name.WriteString(s.Name) |
| 148 | } |
| 149 | return name.String() |
| 150 | } |