Skip to content

Commit

Permalink
cue/ast: add WalkVisitor to share with cue/ast/astutil
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: I5a6b7f2f12bb95a0850de86fbc2374cff480bdd6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194006
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Apr 30, 2024
1 parent 72ba528 commit bf733fb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 226 deletions.
30 changes: 15 additions & 15 deletions cue/ast/astutil/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ type ErrFunc func(pos token.Pos, msg string, args ...interface{})
// Resolve resolves all identifiers in a file. Unresolved identifiers are
// recorded in Unresolved. It will not overwrite already resolved values.
func Resolve(f *ast.File, errFn ErrFunc) {
walk(&scope{errFn: errFn, identFn: resolveIdent}, f)
ast.WalkVisitor(f, &scope{errFn: errFn, identFn: resolveIdent})
}

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

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

func (s *scope) After(n ast.Node) {}
func (s *scope) Before(n ast.Node) (w visitor) {
func (s *scope) Before(n ast.Node) (w ast.Visitor) {
switch x := n.(type) {
case *ast.File:
s := newScope(x, s, x, x.Decls)
// Support imports.
for _, d := range x.Decls {
walk(s, d)
ast.WalkVisitor(d, s)
}
return nil

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

case *ast.Comprehension:
s = scopeClauses(s, x.Clauses)
walk(s, x.Value)
ast.WalkVisitor(x.Value, s)
return nil

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

switch label := n.(type) {
case *ast.ParenExpr:
walk(s, label)
ast.WalkVisitor(label, s)

case *ast.Interpolation:
walk(s, label)
ast.WalkVisitor(label, s)

case *ast.ListLit:
if len(label.Elts) != 1 {
Expand Down Expand Up @@ -317,7 +317,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
}
}
})
walk(s, expr)
ast.WalkVisitor(expr, s)
}

if n := x.Value; n != nil {
Expand All @@ -329,7 +329,7 @@ func (s *scope) Before(n ast.Node) (w visitor) {
n = alias.Expr
}
s.inField = true
walk(s, n)
ast.WalkVisitor(n, s)
s.inField = false
}

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

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

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

case *ast.SelectorExpr:
walk(s, x.X)
ast.WalkVisitor(x.X, s)
return nil

case *ast.Ident:
Expand Down Expand Up @@ -410,20 +410,20 @@ func scopeClauses(s *scope, clauses []ast.Clause) *scope {
for _, c := range clauses {
switch x := c.(type) {
case *ast.ForClause:
walk(s, x.Source)
ast.WalkVisitor(x.Source, s)
s = newScope(s.file, s, x, nil)
if x.Key != nil {
s.insert(x.Key.Name, x.Key, x)
}
s.insert(x.Value.Name, x.Value, x)

case *ast.LetClause:
walk(s, x.Expr)
ast.WalkVisitor(x.Expr, s)
s = newScope(s.file, s, x, nil)
s.insert(x.Ident.Name, x.Ident, x)

default:
walk(s, c)
ast.WalkVisitor(c, s)
}
}
return s
Expand Down
6 changes: 3 additions & 3 deletions cue/ast/astutil/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func Sanitize(f *ast.File) error {
}

// Gather all names.
walk(&scope{
ast.WalkVisitor(f, &scope{
errFn: z.errf,
nameFn: z.addName,
identFn: z.markUsed,
}, f)
})
if z.errs != nil {
return z.errs
}
Expand All @@ -67,7 +67,7 @@ func Sanitize(f *ast.File) error {
index: make(map[string]entry),
}
z.fileScope = s
walk(s, f)
ast.WalkVisitor(f, s)
if z.errs != nil {
return z.errs
}
Expand Down
199 changes: 0 additions & 199 deletions cue/ast/astutil/walk.go

This file was deleted.

0 comments on commit bf733fb

Please sign in to comment.