Skip to content

Commit 6f4983e

Browse files
committed
internal/astinternal: more compact representation of relative positions
The `.WithRel` suffix reads a little verbose, so use a slightly more compact format that should still be reasonably intuitive to read. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I84d8a48b5b3d600e34916012cad096d0fb031424 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200226 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 33fe828 commit 6f4983e

File tree

6 files changed

+150
-154
lines changed

6 files changed

+150
-154
lines changed

encoding/toml/testdata/decode/example.txtar

+44-44
Original file line numberDiff line numberDiff line change
@@ -41,46 +41,46 @@ color = "gray"
4141
Elts: []ast.Decl{
4242
*ast.Field{
4343
Label: *ast.Ident{
44-
NamePos: token.Pos("example.toml:4:1").WithRel("newline")
44+
NamePos: token.Pos("example.toml:4:1", newline)
4545
}
4646
Value: *ast.BasicLit{
4747
ValuePos: token.Pos("example.toml:4:9")
4848
}
4949
}
5050
*ast.Field{
5151
Label: *ast.Ident{
52-
NamePos: token.Pos("example.toml:6:2").WithRel("newline")
52+
NamePos: token.Pos("example.toml:6:2", newline)
5353
}
5454
Value: *ast.StructLit{
55-
Lbrace: token.Pos("-").WithRel("blank")
55+
Lbrace: token.Pos("-", blank)
5656
Elts: []ast.Decl{
5757
*ast.Field{
5858
Label: *ast.Ident{
59-
NamePos: token.Pos("example.toml:7:1").WithRel("newline")
59+
NamePos: token.Pos("example.toml:7:1", newline)
6060
}
6161
Value: *ast.BasicLit{
6262
ValuePos: token.Pos("example.toml:7:8")
6363
}
6464
}
6565
}
66-
Rbrace: token.Pos("-").WithRel("newline")
66+
Rbrace: token.Pos("-", newline)
6767
}
6868
}
6969
*ast.Field{
7070
Label: *ast.Ident{
71-
NamePos: token.Pos("example.toml:11:2").WithRel("newline")
71+
NamePos: token.Pos("example.toml:11:2", newline)
7272
}
7373
Value: *ast.StructLit{
74-
Lbrace: token.Pos("-").WithRel("blank")
74+
Lbrace: token.Pos("-", blank)
7575
Elts: []ast.Decl{
7676
*ast.Field{
7777
Label: *ast.Ident{
78-
NamePos: token.Pos("example.toml:12:1").WithRel("newline")
78+
NamePos: token.Pos("example.toml:12:1", newline)
7979
}
8080
}
8181
*ast.Field{
8282
Label: *ast.Ident{
83-
NamePos: token.Pos("example.toml:13:1").WithRel("newline")
83+
NamePos: token.Pos("example.toml:13:1", newline)
8484
}
8585
Value: *ast.ListLit{
8686
Elts: []ast.Expr{
@@ -98,7 +98,7 @@ color = "gray"
9898
}
9999
*ast.Field{
100100
Label: *ast.Ident{
101-
NamePos: token.Pos("example.toml:14:1").WithRel("newline")
101+
NamePos: token.Pos("example.toml:14:1", newline)
102102
}
103103
Value: *ast.ListLit{
104104
Elts: []ast.Expr{
@@ -124,181 +124,181 @@ color = "gray"
124124
}
125125
*ast.Field{
126126
Label: *ast.Ident{
127-
NamePos: token.Pos("example.toml:15:1").WithRel("newline")
127+
NamePos: token.Pos("example.toml:15:1", newline)
128128
}
129129
Value: *ast.StructLit{
130130
Lbrace: token.Pos("example.toml:15:16")
131131
Elts: []ast.Decl{
132132
*ast.Field{
133133
Label: *ast.Ident{
134-
NamePos: token.Pos("example.toml:15:18").WithRel("blank")
134+
NamePos: token.Pos("example.toml:15:18", blank)
135135
}
136136
Value: *ast.BasicLit{
137137
ValuePos: token.Pos("example.toml:15:24")
138138
}
139139
}
140140
*ast.Field{
141141
Label: *ast.Ident{
142-
NamePos: token.Pos("example.toml:15:30").WithRel("blank")
142+
NamePos: token.Pos("example.toml:15:30", blank)
143143
}
144144
Value: *ast.BasicLit{
145145
ValuePos: token.Pos("example.toml:15:37")
146146
}
147147
}
148148
}
149-
Rbrace: token.Pos("-").WithRel("blank")
149+
Rbrace: token.Pos("-", blank)
150150
}
151151
}
152152
}
153-
Rbrace: token.Pos("-").WithRel("newline")
153+
Rbrace: token.Pos("-", newline)
154154
}
155155
}
156156
*ast.Field{
157157
Label: *ast.Ident{
158-
NamePos: token.Pos("example.toml:17:2").WithRel("newline")
158+
NamePos: token.Pos("example.toml:17:2", newline)
159159
}
160160
Value: *ast.StructLit{
161-
Lbrace: token.Pos("-").WithRel("blank")
162-
Rbrace: token.Pos("-").WithRel("newline")
161+
Lbrace: token.Pos("-", blank)
162+
Rbrace: token.Pos("-", newline)
163163
}
164164
}
165165
*ast.Field{
166166
Label: *ast.Ident{
167-
NamePos: token.Pos("example.toml:19:2").WithRel("newline")
167+
NamePos: token.Pos("example.toml:19:2", newline)
168168
}
169169
Value: *ast.StructLit{
170170
Elts: []ast.Decl{
171171
*ast.Field{
172172
Label: *ast.Ident{
173-
NamePos: token.Pos("example.toml:19:10").WithRel("blank")
173+
NamePos: token.Pos("example.toml:19:10", blank)
174174
}
175175
Value: *ast.StructLit{
176-
Lbrace: token.Pos("-").WithRel("blank")
176+
Lbrace: token.Pos("-", blank)
177177
Elts: []ast.Decl{
178178
*ast.Field{
179179
Label: *ast.Ident{
180-
NamePos: token.Pos("example.toml:20:1").WithRel("newline")
180+
NamePos: token.Pos("example.toml:20:1", newline)
181181
}
182182
Value: *ast.BasicLit{
183183
ValuePos: token.Pos("example.toml:20:6")
184184
}
185185
}
186186
*ast.Field{
187187
Label: *ast.Ident{
188-
NamePos: token.Pos("example.toml:21:1").WithRel("newline")
188+
NamePos: token.Pos("example.toml:21:1", newline)
189189
}
190190
Value: *ast.BasicLit{
191191
ValuePos: token.Pos("example.toml:21:8")
192192
}
193193
}
194194
}
195-
Rbrace: token.Pos("-").WithRel("newline")
195+
Rbrace: token.Pos("-", newline)
196196
}
197197
}
198198
}
199199
}
200200
}
201201
*ast.Field{
202202
Label: *ast.Ident{
203-
NamePos: token.Pos("example.toml:23:2").WithRel("newline")
203+
NamePos: token.Pos("example.toml:23:2", newline)
204204
}
205205
Value: *ast.StructLit{
206206
Elts: []ast.Decl{
207207
*ast.Field{
208208
Label: *ast.Ident{
209-
NamePos: token.Pos("example.toml:23:10").WithRel("blank")
209+
NamePos: token.Pos("example.toml:23:10", blank)
210210
}
211211
Value: *ast.StructLit{
212-
Lbrace: token.Pos("-").WithRel("blank")
212+
Lbrace: token.Pos("-", blank)
213213
Elts: []ast.Decl{
214214
*ast.Field{
215215
Label: *ast.Ident{
216-
NamePos: token.Pos("example.toml:24:1").WithRel("newline")
216+
NamePos: token.Pos("example.toml:24:1", newline)
217217
}
218218
Value: *ast.BasicLit{
219219
ValuePos: token.Pos("example.toml:24:6")
220220
}
221221
}
222222
*ast.Field{
223223
Label: *ast.Ident{
224-
NamePos: token.Pos("example.toml:25:1").WithRel("newline")
224+
NamePos: token.Pos("example.toml:25:1", newline)
225225
}
226226
Value: *ast.BasicLit{
227227
ValuePos: token.Pos("example.toml:25:8")
228228
}
229229
}
230230
}
231-
Rbrace: token.Pos("-").WithRel("newline")
231+
Rbrace: token.Pos("-", newline)
232232
}
233233
}
234234
}
235235
}
236236
}
237237
*ast.Field{
238238
Label: *ast.Ident{
239-
NamePos: token.Pos("example.toml:27:3").WithRel("newline")
239+
NamePos: token.Pos("example.toml:27:3", newline)
240240
}
241241
Value: *ast.ListLit{
242-
Lbrack: token.Pos("-").WithRel("blank")
242+
Lbrack: token.Pos("-", blank)
243243
Elts: []ast.Expr{
244244
*ast.StructLit{
245-
Lbrace: token.Pos("-").WithRel("newline")
245+
Lbrace: token.Pos("-", newline)
246246
Elts: []ast.Decl{
247247
*ast.Field{
248248
Label: *ast.Ident{
249-
NamePos: token.Pos("example.toml:28:1").WithRel("newline")
249+
NamePos: token.Pos("example.toml:28:1", newline)
250250
}
251251
Value: *ast.BasicLit{
252252
ValuePos: token.Pos("example.toml:28:8")
253253
}
254254
}
255255
*ast.Field{
256256
Label: *ast.Ident{
257-
NamePos: token.Pos("example.toml:29:1").WithRel("newline")
257+
NamePos: token.Pos("example.toml:29:1", newline)
258258
}
259259
Value: *ast.BasicLit{
260260
ValuePos: token.Pos("example.toml:29:7")
261261
}
262262
}
263263
}
264-
Rbrace: token.Pos("-").WithRel("newline")
264+
Rbrace: token.Pos("-", newline)
265265
}
266266
*ast.StructLit{
267-
Lbrace: token.Pos("-").WithRel("newline")
268-
Rbrace: token.Pos("-").WithRel("newline")
267+
Lbrace: token.Pos("-", newline)
268+
Rbrace: token.Pos("-", newline)
269269
}
270270
*ast.StructLit{
271-
Lbrace: token.Pos("-").WithRel("newline")
271+
Lbrace: token.Pos("-", newline)
272272
Elts: []ast.Decl{
273273
*ast.Field{
274274
Label: *ast.Ident{
275-
NamePos: token.Pos("example.toml:34:1").WithRel("newline")
275+
NamePos: token.Pos("example.toml:34:1", newline)
276276
}
277277
Value: *ast.BasicLit{
278278
ValuePos: token.Pos("example.toml:34:8")
279279
}
280280
}
281281
*ast.Field{
282282
Label: *ast.Ident{
283-
NamePos: token.Pos("example.toml:35:1").WithRel("newline")
283+
NamePos: token.Pos("example.toml:35:1", newline)
284284
}
285285
Value: *ast.BasicLit{
286286
ValuePos: token.Pos("example.toml:35:7")
287287
}
288288
}
289289
*ast.Field{
290290
Label: *ast.Ident{
291-
NamePos: token.Pos("example.toml:37:1").WithRel("newline")
291+
NamePos: token.Pos("example.toml:37:1", newline)
292292
}
293293
Value: *ast.BasicLit{
294294
ValuePos: token.Pos("example.toml:37:9")
295295
}
296296
}
297297
}
298-
Rbrace: token.Pos("-").WithRel("newline")
298+
Rbrace: token.Pos("-", newline)
299299
}
300300
}
301-
Rbrack: token.Pos("-").WithRel("newline")
301+
Rbrack: token.Pos("-", newline)
302302
}
303303
}
304304
}

internal/astinternal/debug.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ type debugPrinter struct {
5858
level int
5959
}
6060

61-
var (
62-
typeTokenPos = reflect.TypeFor[token.Pos]()
63-
typeTokenToken = reflect.TypeFor[token.Token]()
64-
)
65-
6661
// value produces the given value, omitting type information if
6762
// its type is the same as implied type. It reports whether
6863
// anything was produced.
@@ -99,17 +94,18 @@ func (d *debugPrinter) value0(v reflect.Value, impliedType reflect.Type) {
9994
}
10095

10196
t := v.Type()
102-
switch t {
97+
switch v := v.Interface().(type) {
10398
// Simple types which can stringify themselves.
104-
case typeTokenPos, typeTokenToken:
105-
d.printf("%s(%q)", t, v)
99+
case token.Pos:
100+
d.printf("%s(%q", t, v)
106101
// Show relative positions too, if there are any, as they affect formatting.
107-
if t == typeTokenPos {
108-
pos := v.Interface().(token.Pos)
109-
if pos.HasRelPos() {
110-
d.printf(".WithRel(%q)", pos.RelPos())
111-
}
102+
if v.HasRelPos() {
103+
d.printf(", %v", v.RelPos())
112104
}
105+
d.printf(")")
106+
return
107+
case token.Token:
108+
d.printf("%s(%q)", t, v)
113109
return
114110
}
115111

0 commit comments

Comments
 (0)