1
0
mirror of https://github.com/golang/go synced 2024-11-20 10:54:49 -07: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. // Walk visits each of the children of n.
// //
func Walk(v Visitor, node interface{}) { func Walk(v Visitor, node interface{}) {
if node != nil && !v.Visit(node) { if node == nil || !v.Visit(node) {
return; return;
} }
// walk children // walk children
// (the order of the cases matches the order
// of the corresponding declaration in ast.go)
switch n := node.(type) { switch n := node.(type) {
// Comments and fields // Comments and fields
case *Comment:
// nothing to do
case *CommentGroup: case *CommentGroup:
for _, c := range n.List { for _, c := range n.List {
Walk(v, c); 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: case *Field:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc);
@ -91,13 +101,18 @@ func Walk(v Visitor, node interface{}) {
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment);
// Expressions // Expressions
case *BadExpr, *Ident, *Ellipsis, *BasicLit:
// nothing to do
case *StringList: case *StringList:
for _, x := range n.Strings { for _, x := range n.Strings {
Walk(v, x); Walk(v, x);
} }
case *FuncLit: case *FuncLit:
Walk(v, n.Type); if n != nil {
Walk(v, n.Type);
}
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body);
case *CompositeLit: case *CompositeLit:
@ -109,9 +124,7 @@ func Walk(v Visitor, node interface{}) {
case *SelectorExpr: case *SelectorExpr:
Walk(v, n.X); Walk(v, n.X);
if n.Sel != nil { walkIdent(v, n.Sel);
Walk(v, n.Sel);
}
case *IndexExpr: case *IndexExpr:
Walk(v, n.X); Walk(v, n.X);
@ -163,9 +176,15 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.Value); Walk(v, n.Value);
// Statements // Statements
case *BadStmt:
// nothing to do
case *DeclStmt: case *DeclStmt:
Walk(v, n.Decl); Walk(v, n.Decl);
case *EmptyStmt:
// nothing to do
case *LabeledStmt: case *LabeledStmt:
walkIdent(v, n.Label); walkIdent(v, n.Label);
Walk(v, n.Stmt); Walk(v, n.Stmt);
@ -252,7 +271,6 @@ func Walk(v Visitor, node interface{}) {
} }
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment);
case *ValueSpec: case *ValueSpec:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc);
walkIdentList(v, n.Names); walkIdentList(v, n.Names);
@ -266,6 +284,9 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.Type); Walk(v, n.Type);
walkCommentGroup(v, n.Comment); walkCommentGroup(v, n.Comment);
case *BadDecl:
// nothing to do
case *GenDecl: case *GenDecl:
walkCommentGroup(v, n.Doc); walkCommentGroup(v, n.Doc);
for _, s := range n.Specs { for _, s := range n.Specs {
@ -278,7 +299,9 @@ func Walk(v Visitor, node interface{}) {
Walk(v, n.Recv); Walk(v, n.Recv);
} }
walkIdent(v, n.Name); walkIdent(v, n.Name);
Walk(v, n.Type); if n.Type != nil {
Walk(v, n.Type);
}
walkBlockStmt(v, n.Body); walkBlockStmt(v, n.Body);
// Files and packages // Files and packages