author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 12:58:35 +0100 | |
changeset 265 | 05c40b36d3b2 |
parent 260 | 445e01aede7e |
permissions | -rw-r--r-- |
260 | 1 |
package tracker |
2 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
3 |
import "github.com/pelletier/go-toml/v2/unstable" |
260 | 4 |
|
5 |
// KeyTracker is a tracker that keeps track of the current Key as the AST is |
|
6 |
// walked. |
|
7 |
type KeyTracker struct { |
|
8 |
k []string |
|
9 |
} |
|
10 |
||
11 |
// UpdateTable sets the state of the tracker with the AST table node. |
|
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
12 |
func (t *KeyTracker) UpdateTable(node *unstable.Node) { |
260 | 13 |
t.reset() |
14 |
t.Push(node) |
|
15 |
} |
|
16 |
||
17 |
// UpdateArrayTable sets the state of the tracker with the AST array table node. |
|
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
18 |
func (t *KeyTracker) UpdateArrayTable(node *unstable.Node) { |
260 | 19 |
t.reset() |
20 |
t.Push(node) |
|
21 |
} |
|
22 |
||
23 |
// Push the given key on the stack. |
|
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
24 |
func (t *KeyTracker) Push(node *unstable.Node) { |
260 | 25 |
it := node.Key() |
26 |
for it.Next() { |
|
27 |
t.k = append(t.k, string(it.Node().Data)) |
|
28 |
} |
|
29 |
} |
|
30 |
||
31 |
// Pop key from stack. |
|
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
32 |
func (t *KeyTracker) Pop(node *unstable.Node) { |
260 | 33 |
it := node.Key() |
34 |
for it.Next() { |
|
35 |
t.k = t.k[:len(t.k)-1] |
|
36 |
} |
|
37 |
} |
|
38 |
||
39 |
// Key returns the current key |
|
40 |
func (t *KeyTracker) Key() []string { |
|
41 |
k := make([]string, len(t.k)) |
|
42 |
copy(k, t.k) |
|
43 |
return k |
|
44 |
} |
|
45 |
||
46 |
func (t *KeyTracker) reset() { |
|
47 |
t.k = t.k[:0] |
|
48 |
} |