Skip to content

Commit bf733fb

Browse files
committed
cue/ast: add WalkVisitor to share with cue/ast/astutil
We have two copies of this walk code between the two packages. Even though we have never exposed the APIs to end users before, in practice the interface has been closely tied to their implementations for some time now, so it seems fine to settle for it for now. It's likely that once iterators in Go become widespread, we will want to revisit what better APIs we want to expose here. Adding WalkVisitor shouldn't preclude that, particularly since "Walk" is not really a verb that one would use for an iterator API anyway. That is being tracked in https://cuelang.org/issue/2953. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I5a6b7f2f12bb95a0850de86fbc2374cff480bdd6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194006 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 72ba528 commit bf733fb

File tree

4 files changed

+32
-226
lines changed

4 files changed

+32
-226
lines changed

cue/ast/astutil/resolve.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ type ErrFunc func(pos token.Pos, msg string, args ...interface{})
7575
// Resolve resolves all identifiers in a file. Unresolved identifiers are
7676
// recorded in Unresolved. It will not overwrite already resolved values.
7777
func Resolve(f *ast.File, errFn ErrFunc) {
78-
walk(&scope{errFn: errFn, identFn: resolveIdent}, f)
78+
ast.WalkVisitor(f, &scope{errFn: errFn, identFn: resolveIdent})
7979
}
8080

8181
// Resolve resolves all identifiers in an expression.
8282
// It will not overwrite already resolved values.
8383
func ResolveExpr(e ast.Expr, errFn ErrFunc) {
8484
f := &ast.File{}
85-
walk(&scope{file: f, errFn: errFn, identFn: resolveIdent}, e)
85+
ast.WalkVisitor(e, &scope{file: f, errFn: errFn, identFn: resolveIdent})
8686
}
8787

8888
// A Scope maintains the set of named language entities declared
@@ -250,13 +250,13 @@ func (s *scope) lookup(name string) (p *scope, obj ast.Node, node entry) {
250250
}
251251

252252
func (s *scope) After(n ast.Node) {}
253-
func (s *scope) Before(n ast.Node) (w visitor) {
253+
func (s *scope) Before(n ast.Node) (w ast.Visitor) {
254254
switch x := n.(type) {
255255
case *ast.File:
256256
s := newScope(x, s, x, x.Decls)
257257
// Support imports.
258258
for _, d := range x.Decls {
259-
walk(s, d)
259+
ast.WalkVisitor(d, s)
260260
}
261261
return nil
262262

@@ -265,7 +265,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
265265

266266
case *ast.Comprehension:
267267
s = scopeClauses(s, x.Clauses)
268-
walk(s, x.Value)
268+
ast.WalkVisitor(x.Value, s)
269269
return nil
270270

271271
case *ast.Field:
@@ -277,10 +277,10 @@ func (s *scope) Before(n ast.Node) (w visitor) {
277277

278278
switch label := n.(type) {
279279
case *ast.ParenExpr:
280-
walk(s, label)
280+
ast.WalkVisitor(label, s)
281281

282282
case *ast.Interpolation:
283-
walk(s, label)
283+
ast.WalkVisitor(label, s)
284284

285285
case *ast.ListLit:
286286
if len(label.Elts) != 1 {
@@ -317,7 +317,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
317317
}
318318
}
319319
})
320-
walk(s, expr)
320+
ast.WalkVisitor(expr, s)
321321
}
322322

323323
if n := x.Value; n != nil {
@@ -329,7 +329,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
329329
n = alias.Expr
330330
}
331331
s.inField = true
332-
walk(s, n)
332+
ast.WalkVisitor(n, s)
333333
s.inField = false
334334
}
335335

@@ -342,7 +342,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
342342
delete(s.index, name) // The same name may still appear in another scope
343343

344344
if x.Expr != nil {
345-
walk(s, x.Expr)
345+
ast.WalkVisitor(x.Expr, s)
346346
}
347347
s.index[name] = saved
348348
return nil
@@ -354,7 +354,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
354354
delete(s.index, name) // The same name may still appear in another scope
355355

356356
if x.Expr != nil {
357-
walk(s, x.Expr)
357+
ast.WalkVisitor(x.Expr, s)
358358
}
359359
s.index[name] = saved
360360
return nil
@@ -367,7 +367,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
367367
// that resolve in a list.
368368

369369
case *ast.SelectorExpr:
370-
walk(s, x.X)
370+
ast.WalkVisitor(x.X, s)
371371
return nil
372372

373373
case *ast.Ident:
@@ -410,20 +410,20 @@ func scopeClauses(s *scope, clauses []ast.Clause) *scope {
410410
for _, c := range clauses {
411411
switch x := c.(type) {
412412
case *ast.ForClause:
413-
walk(s, x.Source)
413+
ast.WalkVisitor(x.Source, s)
414414
s = newScope(s.file, s, x, nil)
415415
if x.Key != nil {
416416
s.insert(x.Key.Name, x.Key, x)
417417
}
418418
s.insert(x.Value.Name, x.Value, x)
419419

420420
case *ast.LetClause:
421-
walk(s, x.Expr)
421+
ast.WalkVisitor(x.Expr, s)
422422
s = newScope(s.file, s, x, nil)
423423
s.insert(x.Ident.Name, x.Ident, x)
424424

425425
default:
426-
walk(s, c)
426+
ast.WalkVisitor(c, s)
427427
}
428428
}
429429
return s

cue/ast/astutil/sanitize.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ func Sanitize(f *ast.File) error {
5050
}
5151

5252
// Gather all names.
53-
walk(&scope{
53+
ast.WalkVisitor(f, &scope{
5454
errFn: z.errf,
5555
nameFn: z.addName,
5656
identFn: z.markUsed,
57-
}, f)
57+
})
5858
if z.errs != nil {
5959
return z.errs
6060
}
@@ -67,7 +67,7 @@ func Sanitize(f *ast.File) error {
6767
index: make(map[string]entry),
6868
}
6969
z.fileScope = s
70-
walk(s, f)
70+
ast.WalkVisitor(f, s)
7171
if z.errs != nil {
7272
return z.errs
7373
}

cue/ast/astutil/walk.go

Lines changed: 0 additions & 199 deletions
This file was deleted.

0 commit comments

Comments
 (0)