-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract.go
352 lines (322 loc) · 8.31 KB
/
extract.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package expect
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/scanner"
"golang.org/x/mod/modfile"
)
const commentStart = "@"
const commentStartLen = len(commentStart)
// Identifier is the type for an identifier in an Note argument list.
type Identifier string
// Parse collects all the notes present in a file.
// If content is nil, the filename specified is read and parsed, otherwise the
// content is used and the filename is used for positions and error messages.
// Each comment whose text starts with @ is parsed as a comma-separated
// sequence of notes.
// See the package documentation for details about the syntax of those
// notes.
func Parse(fset *token.FileSet, filename string, content []byte) ([]*Note, error) {
var src interface{}
if content != nil {
src = content
}
switch filepath.Ext(filename) {
case ".go":
// TODO: We should write this in terms of the scanner.
// there are ways you can break the parser such that it will not add all the
// comments to the ast, which may result in files where the tests are silently
// not run.
file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if file == nil {
return nil, err
}
return ExtractGo(fset, file)
case ".mod":
file, err := modfile.Parse(filename, content, nil)
if err != nil {
return nil, err
}
f := fset.AddFile(filename, -1, len(content))
f.SetLinesForContent(content)
notes, err := extractMod(fset, file)
if err != nil {
return nil, err
}
// Since modfile.Parse does not return an *ast, we need to add the offset
// within the file's contents to the file's base relative to the fileset.
for _, note := range notes {
note.Pos += token.Pos(f.Base())
}
return notes, nil
}
return nil, nil
}
// extractMod collects all the notes present in a go.mod file.
// Each comment whose text starts with @ is parsed as a comma-separated
// sequence of notes.
// See the package documentation for details about the syntax of those
// notes.
// Only allow notes to appear with the following format: "//@mark()" or // @mark()
func extractMod(fset *token.FileSet, file *modfile.File) ([]*Note, error) {
var notes []*Note
for _, stmt := range file.Syntax.Stmt {
comment := stmt.Comment()
if comment == nil {
continue
}
// Handle the case for markers of `// indirect` to be on the line before
// the require statement.
// TODO(golang/go#36894): have a more intuitive approach for // indirect
for _, cmt := range comment.Before {
text, adjust := getAdjustedNote(cmt.Token)
if text == "" {
continue
}
parsed, err := parse(fset, token.Pos(int(cmt.Start.Byte)+adjust), text)
if err != nil {
return nil, err
}
notes = append(notes, parsed...)
}
// Handle the normal case for markers on the same line.
for _, cmt := range comment.Suffix {
text, adjust := getAdjustedNote(cmt.Token)
if text == "" {
continue
}
parsed, err := parse(fset, token.Pos(int(cmt.Start.Byte)+adjust), text)
if err != nil {
return nil, err
}
notes = append(notes, parsed...)
}
}
return notes, nil
}
// ExtractGo collects all the notes present in an AST.
// Each comment whose text starts with @ is parsed as a comma-separated
// sequence of notes.
// See the package documentation for details about the syntax of those
// notes.
func ExtractGo(fset *token.FileSet, file *ast.File) ([]*Note, error) {
var notes []*Note
for _, g := range file.Comments {
for _, c := range g.List {
text, adjust := getAdjustedNote(c.Text)
if text == "" {
continue
}
parsed, err := parse(fset, token.Pos(int(c.Pos())+adjust), text)
if err != nil {
return nil, err
}
notes = append(notes, parsed...)
}
}
return notes, nil
}
func getAdjustedNote(text string) (string, int) {
if strings.HasPrefix(text, "/*") {
text = strings.TrimSuffix(text, "*/")
}
text = text[2:] // remove "//" or "/*" prefix
// Allow notes to appear within comments.
// For example:
// "// //@mark()" is valid.
// "// @mark()" is not valid.
// "// /*@mark()*/" is not valid.
var adjust int
if i := strings.Index(text, commentStart); i > 2 {
// Get the text before the commentStart.
pre := text[i-2 : i]
if pre != "//" {
return "", 0
}
text = text[i:]
adjust = i
}
if !strings.HasPrefix(text, commentStart) {
return "", 0
}
text = text[commentStartLen:]
return text, commentStartLen + adjust + 1
}
const invalidToken rune = 0
type tokens struct {
scanner scanner.Scanner
current rune
err error
base token.Pos
}
func (t *tokens) Init(base token.Pos, text string) *tokens {
t.base = base
t.scanner.Init(strings.NewReader(text))
t.scanner.Mode = scanner.GoTokens
t.scanner.Whitespace ^= 1 << '\n' // don't skip new lines
t.scanner.Error = func(s *scanner.Scanner, msg string) {
t.Errorf("%v", msg)
}
return t
}
func (t *tokens) Consume() string {
t.current = invalidToken
return t.scanner.TokenText()
}
func (t *tokens) Token() rune {
if t.err != nil {
return scanner.EOF
}
if t.current == invalidToken {
t.current = t.scanner.Scan()
}
return t.current
}
func (t *tokens) Skip(r rune) int {
i := 0
for t.Token() == '\n' {
t.Consume()
i++
}
return i
}
func (t *tokens) TokenString() string {
return scanner.TokenString(t.Token())
}
func (t *tokens) Pos() token.Pos {
return t.base + token.Pos(t.scanner.Position.Offset)
}
func (t *tokens) Errorf(msg string, args ...interface{}) {
if t.err != nil {
return
}
t.err = fmt.Errorf(msg, args...)
}
func parse(fset *token.FileSet, base token.Pos, text string) ([]*Note, error) {
t := new(tokens).Init(base, text)
notes := parseComment(t)
if t.err != nil {
return nil, fmt.Errorf("%v:%s", fset.Position(t.Pos()), t.err)
}
return notes, nil
}
func parseComment(t *tokens) []*Note {
var notes []*Note
for {
t.Skip('\n')
switch t.Token() {
case scanner.EOF:
return notes
case scanner.Ident:
notes = append(notes, parseNote(t))
default:
t.Errorf("unexpected %s parsing comment, expect identifier", t.TokenString())
return nil
}
switch t.Token() {
case scanner.EOF:
return notes
case ',', '\n':
t.Consume()
default:
t.Errorf("unexpected %s parsing comment, expect separator", t.TokenString())
return nil
}
}
}
func parseNote(t *tokens) *Note {
n := &Note{
Pos: t.Pos(),
Name: t.Consume(),
}
switch t.Token() {
case ',', '\n', scanner.EOF:
// no argument list present
return n
case '(':
n.Args = parseArgumentList(t)
return n
default:
t.Errorf("unexpected %s parsing note", t.TokenString())
return nil
}
}
func parseArgumentList(t *tokens) []interface{} {
args := []interface{}{} // @name() is represented by a non-nil empty slice.
t.Consume() // '('
t.Skip('\n')
for t.Token() != ')' {
args = append(args, parseArgument(t))
if t.Token() != ',' {
break
}
t.Consume()
t.Skip('\n')
}
if t.Token() != ')' {
t.Errorf("unexpected %s parsing argument list", t.TokenString())
return nil
}
t.Consume() // ')'
return args
}
func parseArgument(t *tokens) interface{} {
switch t.Token() {
case scanner.Ident:
v := t.Consume()
switch v {
case "true":
return true
case "false":
return false
case "nil":
return nil
case "re":
if t.Token() != scanner.String && t.Token() != scanner.RawString {
t.Errorf("re must be followed by string, got %s", t.TokenString())
return nil
}
pattern, _ := strconv.Unquote(t.Consume()) // can't fail
re, err := regexp.Compile(pattern)
if err != nil {
t.Errorf("invalid regular expression %s: %v", pattern, err)
return nil
}
return re
default:
return Identifier(v)
}
case scanner.String, scanner.RawString:
v, _ := strconv.Unquote(t.Consume()) // can't fail
return v
case scanner.Int:
s := t.Consume()
v, err := strconv.ParseInt(s, 0, 0)
if err != nil {
t.Errorf("cannot convert %v to int: %v", s, err)
}
return v
case scanner.Float:
s := t.Consume()
v, err := strconv.ParseFloat(s, 64)
if err != nil {
t.Errorf("cannot convert %v to float: %v", s, err)
}
return v
case scanner.Char:
t.Errorf("unexpected char literal %s", t.Consume())
return nil
default:
t.Errorf("unexpected %s parsing argument", t.TokenString())
return nil
}
}