func (p NodeSet) Collect() (items []Node, err error) {
	if p.Err != nil {
		return nil, p.Err
	}
	p.Data.ForEach(func(node Node) error {
		items = append(items, node)
		return nil
	})
	return
}
Collect collects all nodes of the node set.
package gopq
import (
	"errors"
	"os"
	"syscall"
	"github.com/goplus/gop/ast"
	"github.com/goplus/gop/parser"
	"github.com/goplus/gop/token"
)
var (
	ErrNotFound = syscall.ENOENT
	ErrBreak = syscall.ELOOP
	ErrTooManyNodes = errors.New("too many nodes")
)
type Node interface {
	ast.Node
	ForEach(filter func(node Node) error) error
	Obj() interface{}
}
type NodeEnum interface {
	ForEach(filter func(node Node) error) error
}
type NodeSet struct {
	Data NodeEnum
	Err  error
}
func NewSource(
	fset *token.FileSet, path string,
	filter func(os.FileInfo) bool, mode parser.Mode) (doc NodeSet, err error) {
	pkgs, err := parser.ParseDir(fset, path, filter, mode)
	if err != nil {
		return
	}
	return NodeSet{Data: &oneNode{astPackages(pkgs)}}, nil
}
func NewSourceFrom(
	fset *token.FileSet, fs parser.FileSystem, path string,
	filter func(os.FileInfo) bool, mode parser.Mode) (doc NodeSet, err error) {
	pkgs, err := parser.ParseFSDir(fset, fs, path, filter, mode)
	if err != nil {
		return
	}
	return NodeSet{Data: &oneNode{astPackages(pkgs)}}, nil
}
func (p NodeSet) Ok() bool {
	return p.Err == nil
}
func (p NodeSet) FuncDecl() NodeSet {
	return p.Match(func(node Node) bool {
		_, ok := node.Obj().(*ast.FuncDecl)
		return ok
	})
}
func (p NodeSet) GenDecl(tok token.Token) NodeSet {
	return p.Match(func(node Node) bool {
		decl, ok := node.Obj().(*ast.GenDecl)
		return ok && decl.Tok == tok
	})
}
func (p NodeSet) TypeSpec() NodeSet {
	return p.GenDecl(token.TYPE).Child()
}
func (p NodeSet) VarSpec() NodeSet {
	return p.GenDecl(token.VAR).Child()
}
func (p NodeSet) ConstSpec() NodeSet {
	return p.GenDecl(token.CONST).Child()
}
func (p NodeSet) ImportSpec() NodeSet {
	return p.GenDecl(token.IMPORT).Child()
}
type oneNode struct {
	Node
}
func (p *oneNode) ForEach(filter func(node Node) error) error {
	return filter(p.Node)
}
func (p *oneNode) Cached() int {
	return 1
}
func (p NodeSet) One() NodeSet {
	if _, ok := p.Data.(*oneNode); ok {
		return p
	}
	node, err := p.CollectOne()
	if err != nil {
		return NodeSet{Err: err}
	}
	return NodeSet{Data: &oneNode{node}}
}
func One(node Node) NodeSet {
	return NodeSet{Data: &oneNode{node}}
}
type fixNodes struct {
	nodes []Node
}
func (p *fixNodes) ForEach(filter func(node Node) error) error {
	for _, node := range p.nodes {
		if filter(node) == ErrBreak {
			return ErrBreak
		}
	}
	return nil
}
func (p *fixNodes) Cached() int {
	return len(p.nodes)
}
func Nodes(nodes ...Node) NodeSet {
	return NodeSet{Data: &fixNodes{nodes}}
}
type cached interface {
	Cached() int
}
func (p NodeSet) Cache() NodeSet {
	if _, ok := p.Data.(cached); ok {
		return p
	}
	nodes, err := p.Collect()
	if err != nil {
		return NodeSet{Err: err}
	}
	return NodeSet{Data: &fixNodes{nodes}}
}
type anyNodes struct {
	data NodeEnum
}
func (p *anyNodes) ForEach(filter func(node Node) error) error {
	return p.data.ForEach(func(node Node) error {
		return anyForEach(node, filter)
	})
}
func anyForEach(p Node, filter func(node Node) error) error {
	if err := filter(p); err == ErrBreak {
		return err
	}
	return p.ForEach(func(node Node) error {
		return anyForEach(node, filter)
	})
}
func (p NodeSet) Any() (ret NodeSet) {
	if p.Err != nil {
		return p
	}
	return NodeSet{Data: &anyNodes{p.Data}}
}
type childNodes struct {
	data NodeEnum
}
func (p *childNodes) ForEach(filter func(node Node) error) error {
	return p.data.ForEach(func(node Node) error {
		return node.ForEach(filter)
	})
}
func (p NodeSet) Child() NodeSet {
	if p.Err != nil {
		return p
	}
	return NodeSet{Data: &childNodes{p.Data}}
}
type matchedNodes struct {
	data  NodeEnum
	match func(node Node) bool
}
func (p *matchedNodes) ForEach(filter func(node Node) error) error {
	return p.data.ForEach(func(node Node) error {
		if p.match(node) {
			return filter(node)
		}
		return ErrNotFound
	})
}
func (p NodeSet) Match(match func(node Node) bool) (ret NodeSet) {
	if p.Err != nil {
		return p
	}
	return NodeSet{Data: &matchedNodes{p.Data, match}}
}
func (p NodeSet) Name() []string {
	return p.ToString(NameOf)
}
func (p NodeSet) ToString(str func(node Node) string) (items []string) {
	if p.Err != nil {
		return nil
	}
	p.Data.ForEach(func(node Node) error {
		items = append(items, str(node))
		return nil
	})
	return
}