• Roger Peppe's avatar
    the AST walker currently provides no way to find out how the · 80e17d67
    Roger Peppe authored
    nodes in the tree are nested with respect to one another.
    a simple change to the Visitor interface makes it possible
    to do this (for example to maintain a current node-depth, or a
    knowledge of the name of the current function).
    
    Visit(nil) is called at the end of a node's children;
    this make possible the channel-based interface below,
    amongst other possibilities.
    
    It is still just as simple to get the original behaviour - just
    return the same Visitor from Visit.
    
    Here are a couple of possible Visitor types.
    
    // closure-based
    type FVisitor func(n interface{}) FVisitor
    func (f FVisitor) Visit(n interface{}) Visitor {
    	return f(n);
    }
    
    // channel-based
    type CVisitor chan Visit;
    type Visit struct {
    	node interface{};
    	reply chan CVisitor;
    };
    func (v CVisitor) Visit(n interface{}) Visitor
    {
    	if n == nil {
    		close(v);
    	} else {
    		reply := make(chan CVisitor);
    		v <- Visit{n, reply};
    		r := <-reply;
    		if r == nil {
    			return nil;
    		}
    		return r;
    	}
    	return nil;
    }
    
    R=gri
    CC=rsc
    https://golang.org/cl/166047
    80e17d67
Name
Last commit
Last update
..
5a Loading commit data...
5c Loading commit data...
5g Loading commit data...
5l Loading commit data...
6a Loading commit data...
6c Loading commit data...
6g Loading commit data...
6l Loading commit data...
8a Loading commit data...
8c Loading commit data...
8g Loading commit data...
8l Loading commit data...
cc Loading commit data...
cgo Loading commit data...
cov Loading commit data...
ebnflint Loading commit data...
gc Loading commit data...
godefs Loading commit data...
godoc Loading commit data...
gofmt Loading commit data...
gopack Loading commit data...
gotest Loading commit data...
goyacc Loading commit data...
hgpatch Loading commit data...
ld Loading commit data...
nm Loading commit data...
prof Loading commit data...
clean.bash Loading commit data...
make.bash Loading commit data...