-
Notifications
You must be signed in to change notification settings - Fork 8
/
expr.go
186 lines (159 loc) · 3.76 KB
/
expr.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
package cuetsy
import (
"bytes"
"fmt"
"math/bits"
"cuelang.org/go/cue"
"github.com/xlab/treeprint"
)
type exprNode struct {
// The parent node - backlink up the tree. The contained cue.Value in that node either:
// - Returns this node (and possibly others) when its Expr() method is called.
// - Is the return from calling Default() on the parent cue.Value
// - Is the return from calling cue.Dereference() on the parent cue.Value
parent *exprNode
// The cue.Value representing this node.
self cue.Value
// The op produced from calling Expr() on self.
op cue.Op
// Child nodes returned from calling self.Expr()
children []*exprNode
// The default value for this node. nil if there is no default.
dfault *exprNode
// Indicator that the node came from a default
isdefault bool
// The underlying value to which self is a reference. nil if self is not a reference.
ref *exprNode
// Path to the referenced value
refpath cue.Path
// Indicator that the node is part of the tree through dereferencing
isref bool
}
func exprTree(v cue.Value) *exprNode {
op, args := v.Expr()
dv, has := v.Default()
_, path := v.ReferencePath()
n := &exprNode{
op: op,
self: v,
}
var doargs, dodefault bool
switch v.IncompleteKind() {
case cue.ListKind:
dodefault = has && !v.Equals(dv)
doargs = op != cue.NoOp || dodefault
case cue.StructKind:
doargs = op != cue.NoOp || has
dodefault = has
default:
doargs = op != cue.NoOp || has
dodefault = has
}
if dodefault {
n.dfault = exprTree(dv)
n.dfault.parent = n
n.dfault.isdefault = true
}
if len(path.Selectors()) > 0 {
n.ref = exprTree(cue.Dereference(v))
n.refpath = path
n.ref.parent = n
n.ref.isref = true
}
if doargs {
for _, cv := range args {
cn := exprTree(cv)
cn.parent = n
n.children = append(n.children, cn)
}
}
return n
}
func (n *exprNode) Walk(f func(x *exprNode) bool) {
if !f(n) {
return
}
if n.ref != nil {
n.ref.Walk(f)
}
for _, c := range n.children {
c.Walk(f)
}
if n.dfault != nil {
n.dfault.Walk(f)
}
}
func (n *exprNode) String() string {
tp := treeprint.NewWithRoot(n.printSelf())
n.treeprint(tp)
return tp.String()
}
func (n *exprNode) treeprint(tp treeprint.Tree) {
if n.isLeaf() {
if !n.isRoot() {
tp.AddNode(n.printSelf())
}
return
}
var b treeprint.Tree
if n.isRoot() {
b = tp
tp.SetMetaValue(n.opString())
} else {
b = tp.AddMetaBranch(n.opString(), n.printSelf())
}
for _, cn := range n.children {
cn.treeprint(b)
}
if n.ref != nil {
// n.ref.treeprint(b.AddMetaBranch(fmt.Sprintf("ref:%s", n.refpath), n.ref.kindStr()))
n.ref.treeprint(b.AddMetaBranch(fmt.Sprintf("ref:%s", n.refpath), ""))
}
if n.dfault != nil {
n.dfault.treeprint(b.AddMetaBranch("*", ""))
}
}
func (n *exprNode) printSelf() string {
return fmt.Sprintf("%s%s", n.kindStr(), n.attrStr())
}
func (n *exprNode) opString() string {
return n.op.String()
}
func (n *exprNode) isRoot() bool {
return n.parent == nil && !n.isref && !n.isdefault
}
func (n *exprNode) isLeaf() bool {
return n.ref == nil && n.dfault == nil && len(n.children) == 0
}
func (n *exprNode) kindStr() string {
switch n.self.Kind() {
case cue.BottomKind, cue.StructKind, cue.ListKind:
ik := n.self.IncompleteKind()
if bits.OnesCount16(uint16(ik)) != 1 {
return ik.String()
}
if ik != cue.ListKind {
return fmt.Sprintf("(%s)", ik.String())
}
l := n.self.Len()
if l.IsConcrete() {
return "(olist)"
} else {
return "(clist)"
}
default:
str := fmt.Sprint(n.self)
if len(str) < 12 {
return str
}
return str[:12] + "..."
}
}
func (n *exprNode) attrStr() string {
attrs := n.self.Attributes(cue.ValueAttr)
var buf bytes.Buffer
for _, attr := range attrs {
fmt.Fprintf(&buf, " @%s(%s)", attr.Name(), attr.Contents())
}
return buf.String()
}