github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/ast/traverse.go (about)

     1  package ast
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  // GetAllNodesOfType returns all of the nodes of the tree that match the type
     8  // provided. The type should be a pointer to an object in the ast package.
     9  //
    10  // The nodes returned may reference each other and there is no guaranteed order
    11  // in which the nodes are returned.
    12  func GetAllNodesOfType(root Node, t reflect.Type) []Node {
    13  	nodes := []Node{}
    14  
    15  	if root == nil {
    16  		return []Node{}
    17  	}
    18  
    19  	if reflect.TypeOf(root) == t {
    20  		nodes = append(nodes, root)
    21  	}
    22  
    23  	for _, c := range root.Children() {
    24  		nodes = append(nodes, GetAllNodesOfType(c, t)...)
    25  	}
    26  
    27  	return nodes
    28  }