-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathparse.go
200 lines (170 loc) · 4.21 KB
/
parse.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package parse
import (
"go/ast"
"go/parser"
"go/token"
"sort"
"strings"
)
const C = "\"C\""
type GciImports struct {
// original index of import group, include doc, name, path and comment
Start, End int
Name, Path string
}
type ImportList []*GciImports
func (l ImportList) Len() int {
return len(l)
}
func (l ImportList) Less(i, j int) bool {
if strings.Compare(l[i].Path, l[j].Path) == 0 {
return strings.Compare(l[i].Name, l[j].Name) < 0
}
return strings.Compare(l[i].Path, l[j].Path) < 0
}
func (l ImportList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
/*
* AST considers a import block as below:
* ```
* Doc
* Name Path Comment
* ```
* An example is like below:
* ```
* // test
* test "fmt" // test
* ```
* getImports return a import block with name, start and end index
*/
func getImports(imp *ast.ImportSpec) (start, end int, name string) {
if imp.Doc != nil {
// doc poc need minus one to get the first index of comment
start = int(imp.Doc.Pos()) - 1
} else {
if imp.Name != nil {
// name pos need minus one too
start = int(imp.Name.Pos()) - 1
} else {
// path pos start without quote, need minus one for it
start = int(imp.Path.Pos()) - 1
}
}
if imp.Name != nil {
name = imp.Name.Name
}
if imp.Comment != nil {
end = int(imp.Comment.End())
} else {
end = int(imp.Path.End())
}
return
}
func ParseFile(src []byte, filename string) (ImportList, int, int, int, int, error) {
fileSet := token.NewFileSet()
f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments)
if err != nil {
return nil, 0, 0, 0, 0, err
}
if len(f.Imports) == 0 {
return nil, 0, 0, 0, 0, NoImportError{}
}
var (
// headEnd means the start of import block
headEnd int
// tailStart means the end + 1 of import block
tailStart int
// cStart means the start of C import block
cStart int
// cEnd means the end of C import block
cEnd int
data ImportList
)
for index, decl := range f.Decls {
switch decl.(type) {
// skip BadDecl and FuncDecl
case *ast.GenDecl:
genDecl := decl.(*ast.GenDecl)
if genDecl.Tok == token.IMPORT {
// there are two cases, both end with linebreak:
// 1.
// import (
// "xxxx"
// )
// 2.
// import "xxx"
if headEnd == 0 {
headEnd = int(decl.Pos()) - 1
}
tailStart = int(decl.End())
if tailStart > len(src) {
tailStart = len(src)
}
for _, spec := range genDecl.Specs {
imp := spec.(*ast.ImportSpec)
// there are only one C import block
// ensure C import block is the first import block
if imp.Path.Value == C {
/*
common case:
// #include <png.h>
import "C"
notice that decl.Pos() == genDecl.Pos() > genDecl.Doc.Pos()
*/
if genDecl.Doc != nil {
cStart = int(genDecl.Doc.Pos()) - 1
// if C import block is the first, update headEnd
if index == 0 {
headEnd = cStart
}
} else {
/*
special case:
import "C"
*/
cStart = int(decl.Pos()) - 1
}
cEnd = int(decl.End())
continue
}
start, end, name := getImports(imp)
data = append(data, &GciImports{
Start: start,
End: end,
Name: name,
Path: strings.Trim(imp.Path.Value, `"`),
})
}
}
}
}
sort.Sort(data)
return data, headEnd, tailStart, cStart, cEnd, nil
}
// IsGeneratedFileByComment reports whether the source file is generated code.
// Using a bit laxer rules than https://golang.org/s/generatedcode to
// match more generated code.
// Taken from https://github.com/golangci/golangci-lint.
func IsGeneratedFileByComment(in string) bool {
const (
genCodeGenerated = "code generated"
genDoNotEdit = "do not edit"
genAutoFile = "autogenerated file" // easyjson
genAutoGenerated = "automatically generated" // genny
)
markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile, genAutoGenerated}
in = strings.ToLower(in)
for _, marker := range markers {
if strings.Contains(in, marker) {
return true
}
}
return false
}
type NoImportError struct{}
func (n NoImportError) Error() string {
return "No imports"
}
func (i NoImportError) Is(err error) bool {
_, ok := err.(NoImportError)
return ok
}