summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/v2/unstable/builder.go
blob: 9538e30df93a2b7b7ca82992321059ab9e70c475 (plain) (blame)
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
package unstable

// root contains a full AST.
//
// It is immutable once constructed with Builder.
type root struct {
	nodes []Node
}

// Iterator over the top level nodes.
func (r *root) Iterator() Iterator {
	it := Iterator{}
	if len(r.nodes) > 0 {
		it.node = &r.nodes[0]
	}
	return it
}

func (r *root) at(idx reference) *Node {
	return &r.nodes[idx]
}

type reference int

const invalidReference reference = -1

func (r reference) Valid() bool {
	return r != invalidReference
}

type builder struct {
	tree    root
	lastIdx int
}

func (b *builder) Tree() *root {
	return &b.tree
}

func (b *builder) NodeAt(ref reference) *Node {
	return b.tree.at(ref)
}

func (b *builder) Reset() {
	b.tree.nodes = b.tree.nodes[:0]
	b.lastIdx = 0
}

func (b *builder) Push(n Node) reference {
	b.lastIdx = len(b.tree.nodes)
	b.tree.nodes = append(b.tree.nodes, n)
	return reference(b.lastIdx)
}

func (b *builder) PushAndChain(n Node) reference {
	newIdx := len(b.tree.nodes)
	b.tree.nodes = append(b.tree.nodes, n)
	if b.lastIdx >= 0 {
		b.tree.nodes[b.lastIdx].next = newIdx - b.lastIdx
	}
	b.lastIdx = newIdx
	return reference(b.lastIdx)
}

func (b *builder) AttachChild(parent reference, child reference) {
	b.tree.nodes[parent].child = int(child) - int(parent)
}

func (b *builder) Chain(from reference, to reference) {
	b.tree.nodes[from].next = int(to) - int(from)
}