all repos

clerk @ e6f9851

missing tooling for ledger/hledger

clerk/journal/token/token.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
remove duplication of token.Pos.File in token.Span, 1 month ago
1
package token
2
3
import "fmt"
4
5
type Type int
6
7
type Token struct {
8
	Type    Type
9
	Literal string
10
	Span    Span
11
}
12
13
type Span struct {
14
	File       string // absolute path, "" for unknown
15
	Start, End Pos
16
}
17
18
func (s Span) String() string {
19
	if s.File != "" {
20
		return fmt.Sprintf("%s:%d:%d-%d:%d", s.File,
21
			s.Start.Line, s.Start.Col,
22
			s.End.Line, s.End.Col)
23
	}
24
	return fmt.Sprintf("%d:%d-%d:%d",
25
		s.Start.Line, s.Start.Col,
26
		s.End.Line, s.End.Col)
27
}
28
29
type Pos struct {
30
	Offset int
31
	Line   int
32
	Col    int
33
}
34
35
//go:generate go tool stringer -type=Type
36
const (
37
	ILLEGAL Type = iota
38
	EOF
39
40
	WHITESPACE // singe space or tab
41
	INDENT     // leading whitespace (posting/subdirective marker)
42
	NEWLINE    // \n
43
44
	INT     // 42
45
	DECIMAL // 420.69
46
	STRING  // quoted "string"
47
	TEXT    // free text, description, payee, account names
48
49
	BANG      // ! status
50
	STAR      // * status or comment marker
51
	PERCENT   // % comment
52
	HASH      // # comment
53
	SEMICOLON // ; comment or line break
54
	COLON     // :
55
56
	EQ     // =
57
	EQEQ   // ==
58
	EQEQEQ // ===
59
	EQSTAR // =* inclusive balance assertion
60
	AT     // @
61
	ATAT   // @@
62
	PIPE   // |
63
	PLUS   // +
64
	MINUS  // -
65
	TILDE  // ~
66
67
	LPAREN       // (
68
	RPAREN       // )
69
	LBRACE       // {
70
	LBRACELBRACE // {{
71
	RBRACERBRACE // }}
72
	RBRACE       // }
73
	LBRACKET     // [
74
	RBRACKET     // ]
75
76
	COMMODITYMARK // USD, $, £, "my fund"
77
	DATE          // 2024/01/01 / 2024-01-01
78
	TIME          // 12:00:00
79
	PARENEXPR     // (...) parenthesized expression in amount
80
81
	// directives
82
	COMMENTKW   // "comment"
83
	ACCOUNT     // "account"
84
	COMMODITY   // "commodity"
85
	INCLUDE     // "include"
86
	ALIAS       // "alias"
87
	PAYEE       // "payee"
88
	TAG         // "tag"
89
	APPLY       // "apply"
90
	END         // "end"
91
	YEAR        // "Y" or "year"
92
	DECIMALMARK // "decimal-mark"
93
	D           // "D" default commodity
94
	P           // "P" market price
95
	N           // "N" ignored price commodity
96
	C           // "C" commodity conversion
97
)