forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctx.go
65 lines (56 loc) · 1.05 KB
/
ctx.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
package ion
import "fmt"
// ctx is the current reader or writer context.
type ctx uint8
const (
ctxAtTopLevel ctx = iota
ctxInStruct
ctxInList
ctxInSexp
)
func ctxToContainerType(c ctx) Type {
switch c {
case ctxInList:
return ListType
case ctxInSexp:
return SexpType
case ctxInStruct:
return StructType
default:
return NoType
}
}
func containerTypeToCtx(t Type) ctx {
switch t {
case ListType:
return ctxInList
case SexpType:
return ctxInSexp
case StructType:
return ctxInStruct
default:
panic(fmt.Sprintf("type %v is not a container type", t))
}
}
// ctxstack is a context stack.
type ctxstack struct {
arr []ctx
}
// peek returns the current context.
func (c *ctxstack) peek() ctx {
if len(c.arr) == 0 {
return ctxAtTopLevel
}
return c.arr[len(c.arr)-1]
}
// push pushes a new context onto the stack.
func (c *ctxstack) push(ctx ctx) {
c.arr = append(c.arr, ctx)
}
// pop pops the top context off the stack.
func (c *ctxstack) pop() {
if len(c.arr) == 0 {
panic("pop called at top level")
}
c.arr = c.arr[:len(c.arr)-1]
}