...
1
2
3
4
5
6
7
8 package godoc
9
10 import (
11 "bytes"
12 "go/ast"
13 "go/parser"
14 "go/token"
15 pathpkg "path"
16
17 "golang.org/x/tools/godoc/vfs"
18 )
19
20 var linePrefix = []byte("//line ")
21
22
23
24
25
26
27 func replaceLinePrefixCommentsWithBlankLine(src []byte) {
28 for {
29 i := bytes.Index(src, linePrefix)
30 if i < 0 {
31 break
32 }
33
34 if i == 0 || src[i-1] == '\n' {
35
36 for i < len(src) && src[i] != '\n' {
37 src[i] = ' '
38 i++
39 }
40 } else {
41
42 i += len(linePrefix)
43 }
44
45 src = src[i:]
46 }
47 }
48
49 func (c *Corpus) parseFile(fset *token.FileSet, filename string, mode parser.Mode) (*ast.File, error) {
50 src, err := vfs.ReadFile(c.fs, filename)
51 if err != nil {
52 return nil, err
53 }
54
55
56
57 replaceLinePrefixCommentsWithBlankLine(src)
58
59 return parser.ParseFile(fset, filename, src, mode)
60 }
61
62 func (c *Corpus) parseFiles(fset *token.FileSet, relpath string, abspath string, localnames []string) (map[string]*ast.File, error) {
63 files := make(map[string]*ast.File)
64 for _, f := range localnames {
65 absname := pathpkg.Join(abspath, f)
66 file, err := c.parseFile(fset, absname, parser.ParseComments)
67 if err != nil {
68 return nil, err
69 }
70 files[pathpkg.Join(relpath, f)] = file
71 }
72
73 return files, nil
74 }
75
View as plain text