search.go 420 B

123456789101112131415161718192021
  1. package astutils
  2. import "github.com/kyleconroy/sqlc/internal/sql/ast"
  3. type nodeSearch struct {
  4. list *ast.List
  5. check func(ast.Node) bool
  6. }
  7. func (s *nodeSearch) Visit(node ast.Node) Visitor {
  8. if s.check(node) {
  9. s.list.Items = append(s.list.Items, node)
  10. }
  11. return s
  12. }
  13. func Search(root ast.Node, f func(ast.Node) bool) *ast.List {
  14. ns := &nodeSearch{check: f, list: &ast.List{}}
  15. Walk(ns, root)
  16. return ns.list
  17. }