/
ast.go
308 lines (265 loc) · 5.35 KB
/
ast.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package toml
import (
"fmt"
"sort"
)
type Node interface {
Pos() Position
fmt.Stringer
isEmpty() bool
withComment(string, string)
}
type comment struct {
pre string
post string
}
func (c *comment) isZero() bool {
return c.pre == "" && c.post == ""
}
func (c *comment) withComment(pre, post string) {
c.pre = pre
c.post = post
}
type Option struct {
comment
key Token
value Node
}
func (o *Option) String() string {
return o.key.Literal
}
func (o *Option) Pos() Position {
return o.key.Pos
}
func (o *Option) isEmpty() bool {
return o.value == nil || o.value.isEmpty()
}
type Literal struct {
comment
token Token
}
func (i *Literal) String() string {
return i.token.Literal
}
func (i *Literal) Pos() Position {
return i.token.Pos
}
func (i *Literal) isEmpty() bool {
return false
}
type Array struct {
comment
pos Position
nodes []Node
}
func (a *Array) isEmpty() bool {
return len(a.nodes) == 0
}
func (a *Array) isMultiline() bool {
var prev Position
for _, n := range a.nodes {
curr := n.Pos()
if !prev.IsZero() && curr.Line != prev.Line {
return true
}
prev = curr
}
return false
}
func (a *Array) String() string {
return "array"
}
func (a *Array) Pos() Position {
return a.pos
}
func (a *Array) Append(n Node) {
a.nodes = append(a.nodes, n)
}
type tableType int8
const (
tableImplicit tableType = -(iota + 1)
tableRegular
tableArray
tableItem
tableInline
)
func (t tableType) isContainer() bool {
return t == tableImplicit || t == tableRegular || t == tableArray
}
func (t tableType) canNest() bool {
return t == tableImplicit || t == tableRegular || t == tableItem
}
func (t tableType) String() string {
switch t {
case tableImplicit:
return "implicit"
case tableRegular:
return "regular"
case tableArray:
return "array"
case tableItem:
return "item"
case tableInline:
return "inline"
default:
return "unknown"
}
}
type Table struct {
comment
key Token
kind tableType
nodes []Node
}
func (t *Table) String() string {
return t.key.Literal
}
func (t *Table) Pos() Position {
return t.key.Pos
}
func (t *Table) isEmpty() bool {
return len(t.nodes) == 0
}
func (t *Table) isRoot() bool {
return t.key.isZero()
}
func (t *Table) listOptions() []*Option {
var vs []*Option
for _, n := range t.nodes {
o, ok := n.(*Option)
if ok {
vs = append(vs, o)
}
}
sort.Slice(vs, func(i, j int) bool {
return vs[i].Pos().Less(vs[j].Pos())
})
return vs
}
func (t *Table) listTables() []*Table {
var vs []*Table
for _, n := range t.nodes {
t, ok := n.(*Table)
if ok {
vs = append(vs, t)
}
}
sort.Slice(vs, func(i, j int) bool {
return vs[i].Pos().Less(vs[j].Pos())
})
return vs
}
func (t *Table) retrieveTable(tok Token) (*Table, error) {
at := searchNodes(tok.Literal, t.nodes)
if at < len(t.nodes) {
switch x := t.nodes[at].(type) {
case *Option:
if x.key.Literal == tok.Literal {
return nil, fmt.Errorf("%s: option", tok.Literal)
}
case *Table:
if x.key.Literal != tok.Literal {
break
}
if x.isArray() && len(x.nodes) > 0 {
return x.nodes[len(x.nodes)-1].(*Table), nil
}
return x, nil
default:
}
}
x := &Table{
key: tok,
kind: tableImplicit,
}
return x, t.registerTable(x)
}
func (t *Table) registerTable(n *Table) error {
if t.isInline() {
return fmt.Errorf("can not register table to inline table")
}
at := searchNodes(n.key.Literal, t.nodes)
if at < len(t.nodes) {
switch x := t.nodes[at].(type) {
case *Option:
if x.key.Literal == n.key.Literal {
return fmt.Errorf("%s: option already exists", n.key.Literal)
}
case *Table:
if x.key.Literal != n.key.Literal {
break
}
if x.isImplicit() {
t.nodes[at] = mergeTables(n, x)
return nil
}
if x.isArray() {
if n.kind != tableItem {
return fmt.Errorf("%s: invalid table type (%s)", x.key.Literal, n.kind)
}
x.nodes = append(x.nodes, n)
return nil
}
return fmt.Errorf("%s: table already exists", n.key.Literal)
default:
}
}
if n.kind == tableItem {
n = &Table{
key: n.key,
kind: tableArray,
nodes: []Node{n},
}
}
t.nodes = appendNode(t.nodes, n, at)
return nil
}
func (t *Table) registerOption(o *Option) error {
at := searchNodes(o.key.Literal, t.nodes)
if at < len(t.nodes) {
switch x := t.nodes[at].(type) {
case *Option:
if x.key.Literal == o.key.Literal {
return fmt.Errorf("%s: option already exists", x.key.Literal)
}
case *Table:
if x.key.Literal == o.key.Literal {
return fmt.Errorf("%s: table already exists", x.key.Literal)
}
default:
}
}
t.nodes = appendNode(t.nodes, o, at)
if t.isImplicit() {
t.kind = tableRegular
}
return nil
}
func (t *Table) isArray() bool {
return t.kind == tableArray
}
func (t *Table) isInline() bool {
return t.key.Literal == "" && t.kind == tableInline
}
func (t *Table) isImplicit() bool {
return t.kind == tableImplicit
}
func mergeTables(t, n *Table) *Table {
t.nodes = append(t.nodes, n.nodes...)
t.kind = tableRegular
sort.Slice(t.nodes, func(i, j int) bool {
return t.nodes[i].String() <= t.nodes[j].String()
})
return t
}
func searchNodes(str string, nodes []Node) int {
return sort.Search(len(nodes), func(i int) bool {
return str <= nodes[i].String()
})
}
func appendNode(nodes []Node, n Node, at int) []Node {
if at >= len(nodes) {
return append(nodes, n)
}
return append(nodes[:at], append([]Node{n}, nodes[at:]...)...)
}