-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
121 lines (100 loc) · 2.41 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// The doc-extract utility extracts text from Go source comments
// tagged with the special token "+extract" in the first line.
//
// Usage: doc-extract <source dir> <output file>
//
// "source dir" is a directory containing Go source files ending
// in .go, and "output file" is the file to write.
//
// Source files are processed in lexicographic order, except that a file
// named doc.go is always processed first. Comments within a file are
// extracted in the order they appear. This predictable order allows
// you to add, for instance, a header to the output file by adding it to
// doc.go.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"path"
"sort"
"strings"
)
type file struct {
file *ast.File
name string
}
func sortedFiles(pkg *ast.Package) []file {
files := make([]file, 0, len(pkg.Files))
for name, f := range pkg.Files {
files = append(files, file{file: f, name: path.Base(name)})
}
// Sort files in lexicographic order, except give priority to doc.go
sort.Slice(files, func(i, j int) bool {
ni, nj := files[i].name, files[j].name
if ni == "doc.go" {
return true
}
if nj == "doc.go" {
return false
}
return ni < nj
})
return files
}
func extractComment(cgrp *ast.CommentGroup) (string, bool) {
s := cgrp.Text()
parts := strings.SplitN(s, "\n", 2)
if strings.TrimSpace(parts[0]) == "+extract" {
return parts[1], true
}
return "", false
}
func extractPackageComments(pkg *ast.Package) []string {
files := sortedFiles(pkg)
var comments []string
for _, f := range files {
for _, c := range f.file.Comments {
s, ok := extractComment(c)
if ok {
comments = append(comments, s)
}
}
}
return comments
}
func main() {
if len(os.Args) < 3 {
fmt.Printf("usage: %s <source dir> <output file>\n", os.Args[0])
return
}
srcDir := os.Args[1]
outFile := os.Args[2]
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, srcDir, nil, parser.ParseComments)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing files in %s: %s\n", srcDir, err)
os.Exit(1)
}
var out io.Writer
if outFile == "-" {
out = os.Stdout
} else {
f, err := os.Create(outFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating output file %s: %s\n", outFile, err)
os.Exit(1)
}
defer f.Close()
out = f
}
for _, pkg := range pkgs {
comments := extractPackageComments(pkg)
for _, c := range comments {
fmt.Fprintln(out, c)
}
}
}