1
0
mirror of https://github.com/golang/go synced 2024-10-03 16:41:28 -06:00

- gofmt-ify walk.go

- fixed several bugs

R=rsc
http://go/go-review/1015015
This commit is contained in:
Robert Griesemer 2009-10-26 19:21:13 -07:00
parent 5a75ac88c9
commit ae3c9992ae

View File

@ -69,17 +69,27 @@ func walkBlockStmt(v Visitor, b *BlockStmt) {
// Walk visits each of the children of n.
//
func Walk(v Visitor, node interface{}) {
if node != nil && !v.Visit(node) {
if node == nil || !v.Visit(node) {
return;
}
// walk children
// (the order of the cases matches the order
// of the corresponding declaration in ast.go)
switch n := node.(type) {
// Comments and fields
case *Comment:
// nothing to do
case *CommentGroup:
for _, c := range n.List {
Walk(v, c);
}
// TODO(gri): Keep comments in a list/vector instead
// of linking them via Next. Following next will lead
// to multiple visits and potentially n^2 behavior
// since Doc and Comments fields point into the global
// comments list.
case *Field:
walkCommentGroup(v, n.Doc);
@ -91,13 +101,18 @@ func Walk(v Visitor, node interface{}) {
walkCommentGroup(v, n.Comment);
// Expressions
case *BadExpr, *Ident, *Ellipsis, *BasicLit:
// nothing to do
case *StringList:
for _, x := range n.Strings {
Walk(v, x);
}
case *FuncLit:
Walk(v, n.Type);
if n != nil {
Walk(v, n.Type);
}
walkBlockStmt(v, n.Body);
case *CompositeLit:
@ -109,9 +124,7 @@ func Walk(v Visitor, node interface{}) {
case *SelectorExpr:
Walk(v, n.X);
if n.Sel != nil {
Walk(v, n.Sel);
}
walkIdent(v, n.Sel);
case *IndexExpr:
Walk(v, n.X);
@ -163,9 +176,15 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.Value);
// Statements
case *BadStmt:
// nothing to do
case *DeclStmt:
Walk(v, n.Decl);
case *EmptyStmt:
// nothing to do
case *LabeledStmt:
walkIdent(v, n.Label);
Walk(v, n.Stmt);
@ -252,7 +271,6 @@ func Walk(v Visitor, node interface{}) {
}
walkCommentGroup(v, n.Comment);
case *ValueSpec:
walkCommentGroup(v, n.Doc);
walkIdentList(v, n.Names);
@ -266,6 +284,9 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.Type);
walkCommentGroup(v, n.Comment);
case *BadDecl:
// nothing to do
case *GenDecl:
walkCommentGroup(v, n.Doc);
for _, s := range n.Specs {
@ -278,7 +299,9 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.Recv);
}
walkIdent(v, n.Name);
Walk(v, n.Type);
if n.Type != nil {
Walk(v, n.Type);
}
walkBlockStmt(v, n.Body);
// Files and packages