File: log.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (219 lines) | stat: -rw-r--r-- 5,584 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2025 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adt

import (
	"fmt"
	"log"
	"strings"

	"cuelang.org/go/cue/token"
)

// Assert panics if the condition is false. Assert can be used to check for
// conditions that are considers to break an internal variant or unexpected
// condition, but that nonetheless probably will be handled correctly down the
// line. For instance, a faulty condition could lead to error being caught
// down the road, but resulting in an inaccurate error message. In production
// code it is better to deal with the bad error message than to panic.
//
// It is advisable for each use of Assert to document how the error is expected
// to be handled down the line.
func Assertf(c *OpContext, b bool, format string, args ...interface{}) {
	if c.Strict && !b {
		panic(fmt.Sprintf("assertion failed: "+format, args...))
	}
}

// Assertf either panics or reports an error to c if the condition is not met.
func (c *OpContext) Assertf(pos token.Pos, b bool, format string, args ...interface{}) {
	if !b {
		if c.Strict {
			panic(fmt.Sprintf("assertion failed: "+format, args...))
		}
		c.addErrf(0, pos, format, args...)
	}
}

func init() {
	log.SetFlags(0)
}

var pMap = map[*Vertex]int{}

func (c *OpContext) Logf(v *Vertex, format string, args ...interface{}) {
	if c.LogEval == 0 {
		return
	}
	w := &strings.Builder{}

	c.logID++
	fmt.Fprintf(w, "%3d ", c.logID)

	if c.nest > 0 {
		for i := 0; i < c.nest; i++ {
			w.WriteString("... ")
		}
	}

	if v == nil {
		fmt.Fprintf(w, format, args...)
		_ = log.Output(2, w.String())
		return
	}

	p := pMap[v]
	if p == 0 {
		p = len(pMap) + 1
		pMap[v] = p
	}
	disjunctInfo := c.disjunctInfo()
	fmt.Fprintf(w, "[n:%d/%v %s%s] ",
		p, v.Path(), c.PathToString(v.Path()), disjunctInfo)

	for i, a := range args {
		switch x := a.(type) {
		case Node:
			args[i] = c.Str(x)
		case Feature:
			args[i] = x.SelectorString(c)
		}
	}
	fmt.Fprintf(w, format, args...)

	_ = log.Output(2, w.String())
}

// PathToString creates a pretty-printed path of the given list of features.
func (c *OpContext) PathToString(path []Feature) string {
	var b strings.Builder
	for i, f := range path {
		if i > 0 {
			b.WriteByte('.')
		}
		b.WriteString(f.SelectorString(c))
	}
	return b.String()
}

type disjunctInfo struct {
	node            *nodeContext
	disjunctionID   int  // unique ID for sequence
	disjunctionSeq  int  // index into node.disjunctions
	numDisjunctions int  // number of disjunctions
	crossProductSeq int  // index into node.disjuncts (previous results)
	numPrevious     int  // index into node.disjuncts (previous results)
	numDisjuncts    int  // index into node.disjuncts (previous results)
	disjunctID      int  // unique ID for disjunct
	disjunctSeq     int  // index into node.disjunctions[disjunctionSeq].disjuncts
	holeID          int  // unique ID for hole
	lhs             Node // current LHS expression
	rhs             Node // current RHS expression
}

func (c *OpContext) currentDisjunct() *disjunctInfo {
	if len(c.disjunctStack) == 0 {
		panic("no disjunct")
	}
	return &c.disjunctStack[len(c.disjunctStack)-1]
}

func (n *nodeContext) pushDisjunctionTask() *disjunctInfo {
	c := n.ctx
	c.currentDisjunctionID++
	id := disjunctInfo{
		node:          n,
		disjunctionID: c.currentDisjunctionID,
	}
	c.disjunctStack = append(c.disjunctStack, id)

	n.Logf("========= DISJUNCTION %d =========", c.currentDisjunctionID)
	c.nest += 1

	return c.currentDisjunct()
}

func (n *nodeContext) nextDisjunction(index, num, hole int) {
	d := n.ctx.currentDisjunct()

	d.disjunctionSeq = index + 1
	d.numDisjunctions = num
	d.holeID = hole
}

func (n *nodeContext) nextCrossProduct(index, num int, v *nodeContext) *disjunctInfo {
	d := n.ctx.currentDisjunct()

	d.crossProductSeq = index + 1
	d.numPrevious = num
	d.lhs = v.node.Value()

	return d
}

func (n *nodeContext) nextDisjunct(index, num int, expr Node) {
	d := n.ctx.currentDisjunct()

	d.disjunctSeq = index + 1
	d.numDisjuncts = num
	d.rhs = expr
}

func (n *nodeContext) logDoDisjunct() *disjunctInfo {
	c := n.ctx
	c.stats.Disjuncts++

	d := c.currentDisjunct()

	d.disjunctID = int(c.stats.Disjuncts)

	n.Logf("====== Do DISJUNCT %v & %v ======", d.lhs, d.rhs)

	return d
}

func (d disjunctInfo) pop() {
	c := d.node.ctx
	c.nest -= 1
	c.disjunctStack = c.disjunctStack[:len(c.disjunctStack)-1]
}

// disjunctInfo prints a header for log to indicate the current disjunct.
func (c *OpContext) disjunctInfo() string {
	if len(c.disjunctStack) == 0 {
		return ""
	}
	var b strings.Builder
	for i, d := range c.disjunctStack {
		if i != len(c.disjunctStack)-1 && d.disjunctID == 0 {
			continue
		}
		if i != 0 {
			b.WriteString(" =>")
		}
		// which disjunct
		fmt.Fprintf(&b, " D%d:H%d:%d/%d",
			d.disjunctionID, d.holeID, d.disjunctionSeq, d.numDisjunctions)
		if d.crossProductSeq != 0 {
			fmt.Fprintf(&b, " P%d/%d", d.crossProductSeq, d.numPrevious)
		}
		if d.disjunctID != 0 {
			fmt.Fprintf(&b, " d%d:%d/%d",
				d.disjunctID, d.disjunctSeq, d.numDisjuncts,
			)
		}
	}
	return b.String()
}