]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: strip example output comment from synthesized main function
authorAndrew Gerrand <adg@golang.org>
Mon, 1 Oct 2012 22:35:20 +0000 (08:35 +1000)
committerAndrew Gerrand <adg@golang.org>
Mon, 1 Oct 2012 22:35:20 +0000 (08:35 +1000)
R=gri
CC=gobot, golang-dev
https://golang.org/cl/6524047

src/pkg/go/doc/example.go

index 8fcee33af08eb6da191e55e3623b8e308a4c1b0a..581471ae24973b1b9c86cc9df74daa6115349852 100644 (file)
@@ -61,7 +61,7 @@ func Examples(files ...*ast.File) []*Example {
                                Code:     f.Body,
                                Play:     playExample(file, f.Body),
                                Comments: file.Comments,
-                               Output:   exampleOutput(f, file.Comments),
+                               Output:   exampleOutput(f.Body, file.Comments),
                        })
                }
                if !hasTests && numDecl > 1 && len(flist) == 1 {
@@ -78,14 +78,14 @@ func Examples(files ...*ast.File) []*Example {
 
 var outputPrefix = regexp.MustCompile(`(?i)^[[:space:]]*output:`)
 
-func exampleOutput(fun *ast.FuncDecl, comments []*ast.CommentGroup) string {
+func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) string {
        // find the last comment in the function
        var last *ast.CommentGroup
        for _, cg := range comments {
-               if cg.Pos() < fun.Pos() {
+               if cg.Pos() < b.Pos() {
                        continue
                }
-               if cg.End() > fun.End() {
+               if cg.End() > b.End() {
                        break
                }
                last = cg
@@ -163,8 +163,6 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {
                }
        }
 
-       // TODO(adg): look for other unresolved identifiers and, if found, give up.
-
        // Synthesize new imports.
        importDecl := &ast.GenDecl{
                Tok:    token.IMPORT,
@@ -179,12 +177,7 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {
                importDecl.Specs = append(importDecl.Specs, s)
        }
 
-       // Synthesize main function.
-       funcDecl := &ast.FuncDecl{
-               Name: ast.NewIdent("main"),
-               Type: &ast.FuncType{},
-               Body: body,
-       }
+       // TODO(adg): look for other unresolved identifiers and, if found, give up.
 
        // Filter out comments that are outside the function body.
        var comments []*ast.CommentGroup
@@ -195,6 +188,27 @@ func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {
                comments = append(comments, c)
        }
 
+       // Strip "Output:" commment and adjust body end position.
+       if len(comments) > 0 {
+               last := comments[len(comments)-1]
+               if outputPrefix.MatchString(last.Text()) {
+                       comments = comments[:len(comments)-1]
+                       // Copy body, as the original may be used elsewhere.
+                       body = &ast.BlockStmt{
+                               Lbrace: body.Pos(),
+                               List:   body.List,
+                               Rbrace: last.Pos(),
+                       }
+               }
+       }
+
+       // Synthesize main function.
+       funcDecl := &ast.FuncDecl{
+               Name: ast.NewIdent("main"),
+               Type: &ast.FuncType{},
+               Body: body,
+       }
+
        // Synthesize file.
        f := &ast.File{
                Name:     ast.NewIdent("main"),