Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
108d35bd
Commit
108d35bd
authored
Dec 18, 2013
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/ast: added example illustrating CommentMap use.
R=bradfitz CC=golang-dev
https://golang.org/cl/43930043
parent
9d1832f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
0 deletions
+74
-0
src/pkg/go/ast/example_test.go
src/pkg/go/ast/example_test.go
+74
-0
No files found.
src/pkg/go/ast/example_test.go
View file @
108d35bd
...
@@ -5,8 +5,10 @@
...
@@ -5,8 +5,10 @@
package
ast_test
package
ast_test
import
(
import
(
"bytes"
"fmt"
"fmt"
"go/ast"
"go/ast"
"go/format"
"go/parser"
"go/parser"
"go/token"
"go/token"
)
)
...
@@ -134,3 +136,75 @@ func main() {
...
@@ -134,3 +136,75 @@ func main() {
// 57 . }
// 57 . }
// 58 }
// 58 }
}
}
// This example illustrates how to remove a variable declaration
// in a Go program while maintaining correct comment association
// using an ast.CommentMap.
func
ExampleCommentMap
()
{
// src is the input for which we create the AST that we
// are going to manipulate.
src
:=
`
// This is the package comment.
package main
// This comment is associated with the hello constant.
const hello = "Hello, World!" // line comment 1
// This comment is associated with the foo variable.
var foo = hello // line comment 2
// This comment is associated with the main function.
func main() {
fmt.Println(hello) // line comment 3
}
`
// Create the AST by parsing src.
fset
:=
token
.
NewFileSet
()
// positions are relative to fset
f
,
err
:=
parser
.
ParseFile
(
fset
,
"src.go"
,
src
,
parser
.
ParseComments
)
if
err
!=
nil
{
panic
(
err
)
}
// Create an ast.CommentMap from the ast.File's comments.
// This helps keeping the association between comments
// and AST nodes.
cmap
:=
ast
.
NewCommentMap
(
fset
,
f
,
f
.
Comments
)
// Remove the first variable declaration from the list of declarations.
f
.
Decls
=
removeFirstVarDecl
(
f
.
Decls
)
// Use the comment map to filter comments that don't belong anymore
// (the comments associated with the variable declaration), and create
// the new comments list.
f
.
Comments
=
cmap
.
Filter
(
f
)
.
Comments
()
// Print the modified AST.
var
buf
bytes
.
Buffer
if
err
:=
format
.
Node
(
&
buf
,
fset
,
f
);
err
!=
nil
{
panic
(
err
)
}
fmt
.
Printf
(
"%s"
,
buf
.
Bytes
())
// output:
// // This is the package comment.
// package main
//
// // This comment is associated with the hello constant.
// const hello = "Hello, World!" // line comment 1
//
// // This comment is associated with the main function.
// func main() {
// fmt.Println(hello) // line comment 3
// }
}
func
removeFirstVarDecl
(
list
[]
ast
.
Decl
)
[]
ast
.
Decl
{
for
i
,
decl
:=
range
list
{
if
gen
,
ok
:=
decl
.
(
*
ast
.
GenDecl
);
ok
&&
gen
.
Tok
==
token
.
VAR
{
copy
(
list
[
i
:
],
list
[
i
+
1
:
])
return
list
[
:
len
(
list
)
-
1
]
}
}
panic
(
"variable declaration not found"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment