File: scope_stack.go

package info (click to toggle)
golang-github-wader-gojq 0.0~git20231105.2b6d9e2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 960 kB
  • sloc: yacc: 642; makefile: 84
file content (52 lines) | stat: -rw-r--r-- 813 bytes parent folder | download | duplicates (4)
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
package gojq

type scopeStack struct {
	data  []scopeBlock
	index int
	limit int
}

type scopeBlock struct {
	value scope
	next  int
}

func newScopeStack() *scopeStack {
	return &scopeStack{index: -1, limit: -1}
}

func (s *scopeStack) push(v scope) {
	b := scopeBlock{v, s.index}
	i := s.index + 1
	if i <= s.limit {
		i = s.limit + 1
	}
	s.index = i
	if i < len(s.data) {
		s.data[i] = b
	} else {
		s.data = append(s.data, b)
	}
}

func (s *scopeStack) pop() scope {
	b := s.data[s.index]
	s.index = b.next
	return b.value
}

func (s *scopeStack) empty() bool {
	return s.index < 0
}

func (s *scopeStack) save() (index, limit int) {
	index, limit = s.index, s.limit
	if s.index > s.limit {
		s.limit = s.index
	}
	return
}

func (s *scopeStack) restore(index, limit int) {
	s.index, s.limit = index, limit
}