1package astwalk 2 3import ( 4 "go/ast" 5 "strings" 6) 7 8type commentWalker struct { 9 visitor CommentVisitor 10} 11 12func (w *commentWalker) WalkFile(f *ast.File) { 13 if !w.visitor.EnterFile(f) { 14 return 15 } 16 17 for _, cg := range f.Comments { 18 visitCommentGroups(cg, w.visitor.VisitComment) 19 } 20} 21 22func visitCommentGroups(cg *ast.CommentGroup, visit func(*ast.CommentGroup)) { 23 var group []*ast.Comment 24 visitGroup := func(list []*ast.Comment) { 25 if len(list) == 0 { 26 return 27 } 28 cg := &ast.CommentGroup{List: list} 29 visit(cg) 30 } 31 for _, comment := range cg.List { 32 if strings.HasPrefix(comment.Text, "/*") { 33 visitGroup(group) 34 group = group[:0] 35 visitGroup([]*ast.Comment{comment}) 36 } else { 37 group = append(group, comment) 38 } 39 } 40 visitGroup(group) 41} 42