github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/syntax/walk.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file implements syntax tree walking.
     6  
     7  package syntax
     8  
     9  // Inspect traverses an AST in pre-order: it starts by calling f(root);
    10  // root must not be nil. If f returns true, Inspect invokes f recursively
    11  // for each of the non-nil children of root, followed by a call of f(nil).
    12  //
    13  // See Walk for caveats about shared nodes.
    14  func Inspect(root Node, f func(Node) bool)
    15  
    16  // Walk traverses an AST in pre-order: It starts by calling
    17  // v.Visit(node); node must not be nil. If the visitor w returned by
    18  // v.Visit(node) is not nil, Walk is invoked recursively with visitor
    19  // w for each of the non-nil children of node, followed by a call of
    20  // w.Visit(nil).
    21  //
    22  // Some nodes may be shared among multiple parent nodes (e.g., types in
    23  // field lists such as type T in "a, b, c T"). Such shared nodes are
    24  // walked multiple times.
    25  // TODO(gri) Revisit this design. It may make sense to walk those nodes
    26  // only once. A place where this matters is types2.TestResolveIdents.
    27  func Walk(root Node, v Visitor)
    28  
    29  // A Visitor's Visit method is invoked for each node encountered by Walk.
    30  // If the result visitor w is not nil, Walk visits each of the children
    31  // of node with the visitor w, followed by a call of w.Visit(nil).
    32  type Visitor interface {
    33  	Visit(node Node) (w Visitor)
    34  }