/
token.go
192 lines (172 loc) · 3.29 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package toml
import (
"fmt"
)
const (
TokEOF rune = -(iota) + 1
TokNL
TokIdent
TokString
TokBasic
TokLiteral
TokBasicMulti
TokLiteralMulti
TokInteger
TokFloat
TokBool
TokDate
TokDatetime
TokTime
TokComment
TokIllegal
TokBegArray
TokEndArray
TokBegInline
TokEndInline
TokBegRegularTable
TokEndRegularTable
TokBegArrayTable
TokEndArrayTable
TokEqual
TokDot
TokComma
TokNewline
)
var constants = map[string]rune{
"true": TokBool,
"false": TokBool,
"inf": TokFloat,
"+inf": TokFloat,
"-inf": TokFloat,
"nan": TokFloat,
"+nan": TokFloat,
"-nan": TokFloat,
}
type Position struct {
Line int
Column int
}
func (p Position) IsValid() bool {
return p.Line >= 1
}
func (p Position) IsZero() bool {
if p.IsValid() {
return false
}
return p.Line == 0 && p.Column == 0
}
func (p Position) String() string {
return fmt.Sprintf("%d:%d", p.Line, p.Column)
}
func (p Position) Less(other Position) bool {
return p.Line < other.Line
}
type Token struct {
Literal string
Raw string
Type rune
Pos Position
}
func (t Token) isZero() bool {
return t.Literal == "" && t.Type == 0
}
func (t Token) isComment() bool {
return t.Type == TokComment
}
func (t Token) isNL() bool {
return t.Type == TokNL
}
func (t Token) isTable() bool {
return t.Type == TokBegRegularTable || t.Type == TokBegArrayTable
}
func (t Token) IsIdent() bool {
switch t.Type {
case TokIdent, TokString, TokBasic, TokLiteral, TokInteger:
return true
default:
return false
}
}
func (t Token) isValue() bool {
switch t.Type {
case TokInteger, TokFloat, TokBool, TokDate, TokTime, TokDatetime:
return true
case TokString, TokBasic, TokLiteral, TokBasicMulti, TokLiteralMulti:
return true
default:
return false
}
}
func (t Token) isString() bool {
switch t.Type {
case TokString, TokBasic, TokLiteral, TokBasicMulti, TokLiteralMulti:
return true
default:
return false
}
}
func (t Token) IsValid() bool {
return t.Type != TokIllegal
}
func (t Token) IsNumber() bool {
return t.Type == TokInteger || t.Type == TokFloat
}
func (t Token) IsTime() bool {
return t.Type == TokDatetime || t.Type == TokDate || t.Type == TokTime
}
func (t Token) String() string {
var prefix string
switch t.Type {
default:
prefix = "unknown"
case TokEOF:
return "<eof>"
case TokNL:
return "<nl>"
case TokIdent:
prefix = "ident"
case TokString, TokBasic, TokLiteral, TokBasicMulti, TokLiteralMulti:
prefix = "string"
case TokInteger:
prefix = "integer"
case TokFloat:
prefix = "float"
case TokBool:
prefix = "boolean"
case TokDate:
prefix = "date"
case TokDatetime:
prefix = "datetime"
case TokTime:
prefix = "time"
case TokComment:
prefix = "comment"
case TokIllegal:
prefix = "illegal"
case TokBegArray:
return "<begin-array>"
case TokEndArray:
return "<end-array>"
case TokBegInline:
return "<begin-inline>"
case TokEndInline:
return "<end-inline>"
case TokBegRegularTable:
return "<begin-regular-table>"
case TokEndRegularTable:
return "<end-regular-table>"
case TokBegArrayTable:
return "<begin-array-table>"
case TokEndArrayTable:
return "<end-array-table>"
case TokEqual:
return "<equal>"
case TokDot:
return "<dot>"
case TokComma:
return "<comma>"
case TokNewline:
return "<newline>"
}
return fmt.Sprintf("<%s(%s)>", prefix, t.Literal)
}